-
Notifications
You must be signed in to change notification settings - Fork 3
/
PageManager.angelscript
408 lines (360 loc) · 10.2 KB
/
PageManager.angelscript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
class Page
{
private Button@[] m_buttons;
private string m_soonButton;
private uint m_first;
private vector2 m_numberOffset;
private TouchGapDetector m_touchGapDetector;
private PageProperties@ m_props;
Page(const uint firstItem, PageProperties@ props)
{
@m_props = @props;
m_first = firstItem;
m_numberOffset = g_scale.scale(props.numberOffset);
const uint numButtons = m_props.rows * m_props.columns;
m_buttons.resize(numButtons);
vector2 cursor = getScreenOffset();
const vector2 screenOffset(getScreenOffset());
for (uint t = 0; t < m_buttons.length(); t++)
{
setupItemButton(t, cursor);
cursor.x += getButtonSize().x;
if (cursor.x + getButtonSize().x > GetScreenSize().x - screenOffset.x + 1.0f)
{
cursor.x = screenOffset.x;
cursor.y += getButtonSize().y;
}
}
}
Button@ getButton(const uint buttonIdx)
{
if (buttonIdx < m_buttons.length())
return @(m_buttons[buttonIdx]);
else
return null;
}
private string getSpriteName(const uint itemIdx) const
{
if (m_props.useUniqueButtons)
{
return (m_props.buttonName + itemIdx + m_props.buttonSufix);
}
else
{
return m_props.buttonName;
}
}
private void setupItemButton(const uint t, const vector2 &in cursor)
{
const uint currentItem = getItem(t);
string sprite;
if (currentItem >= m_props.numItems)
{
sprite = m_props.emptyButton;
}
else if (!m_props.itemChooser.validateItem(currentItem) && !m_props.useUniqueButtons)
{
sprite = m_props.lockedButton;
}
else
{
sprite = getSpriteName(currentItem);
}
const vector2 origin(V2_HALF);
const float buttonScale = g_scale.getScale();
@m_buttons[t] = Button(sprite, cursor + computeButtonOriginOffsetFix(currentItem), buttonScale, origin);
m_buttons[t].setSound(m_props.itemSelectSound);
}
vector2 computeButtonOriginOffsetFix(const uint currentItem) const
{
const vector2 origin(V2_HALF);
const float buttonScale = g_scale.getScale();
return (GetSpriteFrameSize(getSpriteName(currentItem)) * origin * buttonScale);
}
void update(const vector2 &in offset)
{
m_touchGapDetector.update();
const bool isOffseting = (offset != vector2(0,0));
for (uint t = 0; t < m_buttons.length(); t++)
{
const uint currentItem = getItem(t);
if (isOffseting && !m_buttons[t].isInScreen(offset))
{
continue;
}
if (isValidItem(currentItem)/* || !m_buttons[t].isAnimationFinished()*/)
{
m_buttons[t].update();
// if the user had used the swyper, disable the button for this time
if (m_touchGapDetector.getLastMaxTouchGap(0) > g_scale.scale(48.0f))
m_buttons[t].setPressed(false);
if (m_buttons[t].isPressed() && isValidItem(currentItem))
{
m_buttons[t].setPressed(false);
if (m_props.itemChooser.validateItem(currentItem))
m_props.itemChooser.performAction(currentItem);
else
m_props.itemChooser.performDenialAction(currentItem);
}
}
}
}
bool isValidItem(const uint item) const
{
return item < m_props.numItems;
}
void draw(const vector2 &in offset)
{
const bool isOffseting = (offset != vector2(0,0));
for (uint t = 0; t < m_buttons.length(); t++)
{
if (isOffseting && !m_buttons[t].isInScreen(offset))
{
continue;
}
m_buttons[t].draw(offset);
if (m_props.showNumbers)
{
drawNumber(t, offset);
}
const vector2 buttonPos = m_buttons[t].getPos();
const vector2 halfButtonSize = m_buttons[t].getSize() * 0.5f;
if (m_props.useUniqueButtons && !m_props.itemChooser.validateItem(getItem(t)))
{
drawScaledSprite(m_props.lockedButton, buttonPos + halfButtonSize + offset - computeButtonOriginOffsetFix(getItem(t)),
g_scale.getScale(), V2_HALF, COLOR_WHITE);
}
m_props.itemChooser.itemDrawCallback(getItem(t), buttonPos - computeButtonOriginOffsetFix(t), offset);
}
}
private void drawNumber(const uint t, const vector2 &in offset)
{
const uint currentItem = getItem(t);
if (currentItem < m_props.numItems && m_props.itemChooser.validateItem(currentItem))
{
vector2 spriteSize = getButtonSize();
vector2 pos = m_buttons[t].getPos();
drawCenteredText(pos + m_numberOffset + offset + (spriteSize * 0.5f) - computeButtonOriginOffsetFix(currentItem), "" + (getItem(t) + 1), m_props.font, g_scale.scale(m_props.fontScale), m_buttons[t].getColor());
}
}
uint getItem(const uint t)
{
return t + m_first;
}
uint getButtonCount()
{
return m_buttons.length();
}
vector2 getButtonSize()
{
return GetSpriteSize(getSpriteName(0)) * g_scale.getScale();
}
float getLineWidth()
{
return float(m_props.columns) * getButtonSize().x;
}
float getHeight()
{
return float(m_props.rows) * getButtonSize().y;
}
vector2 getScreenOffset()
{
return vector2((GetScreenSize().x - getLineWidth()) / 2.0f, (GetScreenSize().y - getHeight()) / 2.0f);
}
private uint getItem(const uint t) const
{
return m_first + t;
}
}
class PageManager : UILayer
{
private PageProperties@ m_props;
private Page@[] m_pages;
private Swyper m_swyper;
private vector2 m_backButtonOffset;
private vector2 m_offset;
private vector2 pageCounterSpriteFrameSize;
PageManager(PageProperties@ props, const int startPage = 0)
{
@m_props = @props;
const uint buttonsPerPage = props.columns * props.rows;
const uint numPages = uint(ceil(float(props.numItems) / float(buttonsPerPage)));
m_swyper = Swyper(numPages, startPage);
m_backButtonOffset = vector2(0,0);
while (m_pages.length() * buttonsPerPage < m_props.numItems)
{
m_pages.insertLast(Page(buttonsPerPage * m_pages.length(), @m_props));
}
addButton("back_button", props.forwardButton, m_props.backButtonNormPos, m_props.backButtonNormPos);
addButton("forw_button", props.backButton, m_props.forwardButtonNormPos, m_props.forwardButtonNormPos);
enableBounceEffects("back_button");
enableBounceEffects("forw_button");
if (m_props.pageCounterSprite != "")
{
SetupSpriteRects(m_props.pageCounterSprite, m_props.pageCounterSpriteColumns, m_props.pageCounterSpriteRows);
pageCounterSpriteFrameSize = GetSpriteFrameSize(m_props.pageCounterSprite);
}
}
Button@ getButton(const uint pageIdx, const uint buttonIdx)
{
if (pageIdx < m_pages.length())
return @(m_pages[pageIdx].getButton(buttonIdx));
else
return null;
}
void setCurrentPage(const uint page)
{
m_swyper.setCurrentPage(page);
}
int pageOf(const uint itemIdx)
{
const uint buttonsPerPage = m_props.columns * m_props.rows;
return itemIdx / buttonsPerPage;
}
uint getNumButtonsPerPage() const
{
return (m_props.columns * m_props.rows);
}
void enableBounceEffects(const string &in buttonName)
{
UIButton@ button = getButton(buttonName);
const float diff = 0.03f;
button.setBounce(vector2(1.0f - diff, 1.0f - diff), vector2(1.0f + diff, 1.0f + diff), 400);
}
string getName() const
{
return "PageManager";
}
bool isAlwaysActive() const
{
return false;
}
uint getCurrentPage()
{
return m_swyper.getCurrentPage();
}
uint getPageCount() const
{
return m_pages.length();
}
void nextPage()
{
if (m_swyper.getCurrentPage() < int(m_pages.length() - 1))
{
m_swyper.setCurrentPage(m_swyper.getCurrentPage() + 1);
}
}
bool isLastPage()
{
return (m_swyper.isLastPage());
}
bool isFirstPage()
{
return (m_swyper.isFirstPage());
}
void priorPage()
{
if (m_swyper.getCurrentPage() > 0)
{
m_swyper.setCurrentPage(m_swyper.getCurrentPage() - 1);
}
}
void update()
{
UILayer::update();
m_swyper.update();
UIButton@ backButton = getButton("back_button");
bool firstPage = isFirstPage();
if (backButton !is null)
{
/*if (firstPage)
{
backButton.setScale(g_scale.getScale() + ((abs(sin(GetTimeF() / 200.0f)) * 0.06f) * g_scale.getScale()));
}*/
if (backButton.isPressed())
{
if (firstPage)
{
g_stateManager.setState(g_gameStateFactory.createMenuState());
}
else
{
priorPage();
}
backButton.setPressed(false);
}
}
if (isButtonPressed("forw_button"))
{
nextPage();
setButtonPressed("forw_button", false);
}
fadeForwardArrows();
m_pages[m_swyper.getCurrentPage()].update(V2_ZERO);
}
void fadeForwardArrows()
{
if (isLastPage())
{
UIButton@ button = getButton("forw_button");
FloatColor newColor(button.getCustomColor());
newColor.a *= 0.96f;
button.setCustomColor(newColor.getUInt());
}
else
{
UIButton@ button = getButton("forw_button");
FloatColor newColor(button.getCustomColor());
newColor.a += 0.05f;
if (newColor.a > 1.0f)
newColor.a = 1.0f;
button.setCustomColor(newColor.getUInt());
}
}
float getGlobalOffset() const
{
const float fNumPages = float(m_pages.length());
float globalOffset = float(m_swyper.getCurrentPage()) / fNumPages;
const float piece = 1.0f / fNumPages;
return (globalOffset + (piece *-m_offset.x)) * 2.0f;
}
void draw()
{
UILayer::draw();
const vector2 screenSize(GetScreenSize());
const int nextPage = m_swyper.getNextPage();
float offsetX = m_swyper.getOffset().x;
if (nextPage ==-1)
{
offsetX *= 0.3333f;
}
const uint currentPage = m_swyper.getCurrentPage();
m_pages[currentPage].draw(vector2(offsetX * screenSize.x, 0));
if (nextPage != -1)
{
m_offset = m_swyper.getOffset();
const float nextPageOffset = (nextPage > int(currentPage)) ? screenSize.x : -screenSize.x;
const vector2 offset(vector2((m_offset.x * screenSize.x) + nextPageOffset, 0));
m_pages[nextPage].draw(offset);
}
if (m_pages.length() > 1 && m_props.pageCounterSprite != "")
{
drawPageCounter(currentPage);
}
}
void drawPageCounter(const uint currentPage)
{
const uint numPages = m_pages.length();
const vector2 spriteOffset(g_scale.scale(pageCounterSpriteFrameSize * vector2(1,0)));
const float fNumPages = float(m_pages.length());
vector2 rail((m_props.pageCounterNormalizedPos * GetScreenSize()) - (fNumPages * spriteOffset * 0.5f));
const uint lastFrame = (m_props.pageCounterSpriteColumns * m_props.pageCounterSpriteRows) - 1;
SetupSpriteRects(m_props.pageCounterSprite, m_props.pageCounterSpriteColumns, m_props.pageCounterSpriteRows);
for (uint t = 0; t < numPages; t++)
{
SetSpriteRect(m_props.pageCounterSprite, (currentPage == t) ? t : lastFrame);
drawScaledSprite(m_props.pageCounterSprite, rail, g_scale.getScale(), V2_ZERO, m_props.pageCounterColor);
rail += spriteOffset;
}
}
}