-
Notifications
You must be signed in to change notification settings - Fork 0
/
elements.py
603 lines (492 loc) · 23.8 KB
/
elements.py
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
import pyglet
import cocos
import os
from cocos import layer
from cocos.text import *
from cocos.actions import *
from cocos.sprite import *
from pyglet.window.key import symbol_string
import resources
import events
import cfg
import scaling as sc
x, y = director.window.width, director.window.height
reswidth, resheight = [int(res) for res in cfg.resolution.split("x")]
titleLabel = cocos.text.Label(
"Game Title",
font_name=resources.font[1],
font_size=50,
anchor_y="top",
anchor_x="center"
)
"""Main Menu Button. Sized and aligned to fit vertically on the game's main menu, as title buttons. Hardcoded to only use 4, but can be extended and used elsewhere."""
class MainMenuButton(layer.Layer):
is_event_handler = True
def __init__(self, label, eventName, buttonorder = 1, animate=True):
super().__init__()
global x, y
self.eventName = eventName
self.label = label
self.buttonorder = buttonorder
self.bgImage = Sprite("menuButton.png") # Replace with proper resource pack image
self.lbl = Label(self.label, anchor_x="center", anchor_y="center", dpi=110)
self.x = x / 2
self.TOPBTNPOS = 0.68
self.y = y * (self.TOPBTNPOS - (0.16 * (buttonorder - 1)))
self.width = self.bgImage.width
self.height = self.bgImage.height
self.add(self.bgImage, z = 0)
self.add(self.lbl, z = 1)
self.width_range = [int(self.x - (self.bgImage.width / 2)), int(self.x + (self.bgImage.width / 2))]
self.height_range = [int(self.y - (self.bgImage.height / 2)), int(self.y + (self.bgImage.height / 2))]
if animate:
self.animate()
self.schedule_interval(self.setWH, 1)
self.resume_scheduler()
def setWH(self, dt):
x, y = director.window.width, director.window.height
nmin = sc.scale(int(self.x - (self.bgImage.width / 2)), int(self.y - (self.bgImage.height / 2)))
nmax = sc.scale(int(self.x + (self.bgImage.width / 2)), int(self.y + (self.bgImage.height / 2)))
self.width_range = [int(nmin[0]), int(nmax[0])]
self.height_range = [int(nmin[1]), int(nmax[1])]
def on_mouse_motion(self, x, y, dx, dy):
if x in range(self.width_range[0], self.width_range[1]) and y in range(self.height_range[0], self.height_range[1]):
self.bgImage.image = pyglet.resource.image("menuButtonHovered.png")
self.lbl.element.color = (255, 255, 255, (self.lbl.element.color[-1]))
else:
self.bgImage.image = pyglet.resource.image("menuButton.png")
self.lbl.element.color = (255, 255, 255, (self.lbl.element.color[-1]))
def on_mouse_press(self, x, y, buttons, modifiers):
if x in range(self.width_range[0], self.width_range[1]) and y in range(self.height_range[0], self.height_range[1]):
self.eventName()
self.bgImage.image = pyglet.resource.image("menuButtonClicked.png")
self.lbl.element.color = (0, 0, 0, (self.lbl.element.color[-1]))
def animate(self):
mvx = self.x
mvy = self.y
# ODD
if not self.buttonorder % 2 == 0:
self.x = -self.width
self.do(Delay(0.7) + AccelDeccel(MoveTo((mvx, mvy), 1.5)))
# EVEN
else:
self.x = x + self.width
self.do(Delay(0.7) + AccelDeccel(MoveTo((mvx, mvy), 1.5)))
"""Text Box. For inputting text."""
class TextBox(layer.Layer):
is_event_handler = True
def __init__(self, parent, selfx, selfy, default_text = "", charLimit = 1):
super().__init__()
self.px = parent.x
self.py = parent.y
self.x = selfx
self.y = selfy
self.charLimit = charLimit
self.inputLabel = Label(default_text, font_size = 20, anchor_x = "center", anchor_y = "center", color=(0, 0, 0, 255))
self.bgImage = Sprite("textBox.png")
self.width = self.bgImage.width
self.height = self.bgImage.height
self.active = False
self.add(self.bgImage)
self.add(self.inputLabel)
self.parentx = 0
self.parenty = 0
self.showing = self._showing = False
self.width_range = [int((self.parentx + self.x) - (self.bgImage.width / 2)), int((self.parentx + self.x) + (self.bgImage.width / 2))]
self.height_range = [int((self.parenty + self.y) - (self.bgImage.height / 2)), int((self.parenty + self.y) + (self.bgImage.height / 2))]
self.schedule_interval(self.setWH, 1)
self.resume_scheduler()
self.changed = False
def setWH(self, dt):
x, y = director.window.width, director.window.height
nmin = sc.scale(int((self.parentx + self.x) - (self.bgImage.width / 2)), int((self.parenty + self.y) - (self.bgImage.height / 2)))
nmax = sc.scale(int((self.parentx + self.x) + (self.bgImage.width / 2)), int((self.parenty + self.y) + (self.bgImage.height / 2)))
self.width_range = [int(nmin[0]), int(nmax[0])]
self.height_range = [int(nmin[1]), int(nmax[1])]
def on_mouse_motion(self, x, y, dx, dy):
if self.showing:
if not self.active:
if x in range(self.width_range[0], self.width_range[1]) and y in range(self.height_range[0], self.height_range[1]):
self.bgImage.image = pyglet.resource.image("textBoxHovered.png")
else:
self.bgImage.image = pyglet.resource.image("textBox.png")
def on_mouse_press(self, x, y, buttons, modifiers):
if self.showing:
if x in range(self.width_range[0], self.width_range[1]) and y in range(self.height_range[0], self.height_range[1]):
self.active = True
self.bgImage.image = pyglet.resource.image("textBoxEntered.png")
self.inputLabel.element.text = ""
else:
self.active = False
self.bgImage.image = pyglet.resource.image("textBox.png")
def on_key_press(self, key, modifiers):
try:
if self.active:
num = int(symbol_string(key)[-1])
self.inputLabel.element.text = self.inputLabel.element.text + symbol_string(key)[-1]
if len(self.inputLabel.element.text) > (self.charLimit - 1):
self.active = False
self.bgImage.image = pyglet.resource.image("textBox.png")
self.changed = True
except ValueError:
pass
def get_text(self):
return self.inputLabel.element.text
@property
def showing(self):
return self._showing
@showing.setter
def showing(self, value):
self._showing = value
# TODO: Active and non-active checking
"""Section Button. For activating a section of a page"""
class sectionButton(layer.Layer):
is_event_handler = True
def __init__(self, label, eventName, active = False):
super().__init__()
global x, y
self.eventName = eventName
self.clickable = True
self.bgImage = Sprite("settingsCategoryButton.png")
self.lbl = Label(label, anchor_x="center", anchor_y="center", dpi=110)
self.x = 0
self.y = 0
self.width_range = [int(self.x - (self.bgImage.width / 2)), int(self.x + (self.bgImage.width / 2))]
self.height_range = [int(self.y - (self.bgImage.height / 2)), int(self.y + (self.bgImage.height / 2))]
self.add(self.bgImage)
self.add(self.lbl)
self.active = self._active = active
self.schedule_interval(self.setWH, 1)
self.resume_scheduler()
def setWH(self, dt):
x, y = director.window.width, director.window.height
nmin = sc.scale(int(self.x - (self.bgImage.width / 2)), int(self.y - (self.bgImage.height / 2)))
nmax = sc.scale(int(self.x + (self.bgImage.width / 2)), int(self.y + (self.bgImage.height / 2)))
self.width_range = [int(nmin[0]), int(nmax[0])]
self.height_range = [int(nmin[1]), int(nmax[1])]
def on_mouse_motion(self, x, y, dx, dy):
if self.clickable:
if not self.active:
if x in range(self.width_range[0], self.width_range[1]) and y in range(self.height_range[0], self.height_range[1]):
self.bgImage.image = pyglet.resource.image("settingsCategoryButtonHovered.png")
self.lbl.element.color = (255, 255, 255, 255)
else:
self.bgImage.image = pyglet.resource.image("settingsCategoryButton.png")
self.lbl.element.color = (255, 255, 255, 255)
def on_mouse_press(self, x, y, buttons, modifiers):
if self.clickable:
if x in range(self.width_range[0], self.width_range[1]) and y in range(self.height_range[0], self.height_range[1]):
self.active = True
self.eventName()
@property
def active(self):
return self._active
@active.setter
def active(self, value):
self._active = value
if value:
self.bgImage.image = pyglet.resource.image("settingsCategoryButtonClicked.png")
self.lbl.element.color = (0, 0, 0, 255)
else:
self.bgImage.image = pyglet.resource.image("settingsCategoryButton.png")
self.lbl.element.color = (255, 255, 255, 255)
"""Large Button. Same as toggle button, but for activating events"""
class LargeButton(layer.Layer):
is_event_handler = True
def __init__(self, label, eventName, active = False, showing = True, *args, **kwargs):
super().__init__()
global x, y
self.eventName = eventName
self.active = active
if "eventparam" in kwargs:
self.eventparam = kwargs["eventparam"]
if "responseTrue" in kwargs:
self.responseTrue = kwargs["responseTrue"]
self.bgImage = Sprite("largeButton.png")
self.lbl = Label(label, anchor_x="center", anchor_y="center", dpi=110, font_size=16)
self.width = self.bgImage.width
self.height = self.bgImage.height
self.width_range = [0,0]
self.height_range = [0,0]
self.add(self.bgImage)
self.add(self.lbl)
self.showing = self._showing = showing
def setWH(self, dt):
x, y = director.window.width, director.window.height
nmin = sc.scale(int((self.parent.x + self.x) - (self.bgImage.width / 2)), int((self.parent.y + self.y) - (self.bgImage.height / 2)))
nmax = sc.scale(int((self.parent.x + self.x) + (self.bgImage.width / 2)), int((self.parent.y + self.y) + (self.bgImage.height / 2)))
self.width_range = [int(nmin[0]), int(nmax[0])]
self.height_range = [int(nmin[1]), int(nmax[1])]
def on_mouse_motion(self, x, y, dx, dy):
if self.showing:
if self.active:
self.bgImage.image = pyglet.resource.image("largeButtonClicked.png")
self.lbl.element.color = (0, 0, 0, 255)
elif x in range(self.width_range[0], self.width_range[1]) and y in range(self.height_range[0], self.height_range[1]):
self.bgImage.image = pyglet.resource.image("largeButtonHovered.png")
self.lbl.element.color = (255, 255, 255, 255)
else:
self.bgImage.image = pyglet.resource.image("largeButton.png")
self.lbl.element.color = (255, 255, 255, 255)
def on_mouse_press(self, x, y, buttons, modifiers):
if self.showing:
if x in range(self.width_range[0], self.width_range[1]) and y in range(self.height_range[0], self.height_range[1]):
self.bgImage.image = pyglet.resource.image("largeButtonClicked.png")
self.active = True
self.lbl.element.color = (0, 0, 0, 255)
if hasattr(self, "eventparam"):
response = self.eventName(*self.eventparam)
else:
response = self.eventName()
if hasattr(self, "responseTrue") and response:
self.responseTrue()
self.active = False
def on_enter(self):
super().on_enter()
self.schedule(self.setWH)
self.resume_scheduler()
@property
def showing(self):
return self._showing
@showing.setter
def showing(self, value):
self._showing = value
"""Toggle Button. For toggling a setting on or off. Can alter a value in a dictionary and run a command simultaneously."""
class ToggleButton(layer.Layer):
is_event_handler = True
def __init__(self, configDict, section, option, command = None, showing = True):
super().__init__()
global x, y
self.command = command
self.configDict = configDict
self.section = section
self.option = option
self.showing = self._showing = showing
self.lbl = Label("YES", anchor_x="center", anchor_y="center", dpi=105)
self.bgImage = Sprite("largeButton.png")
self.active = True if self.configDict[self.section][self.option] == "True" else False
self.changed = False
if self.active:
self.bgImage.image = pyglet.resource.image("largeToggledButton.png")
self.lbl.element.text = "YES"
else:
self.bgImage.image = pyglet.resource.image("largeButton.png")
self.lbl.element.text = "NO"
self.width = self.bgImage.width
self.height = self.bgImage.height
self.lbl.x = 0
self.lbl.y = 0
self.add(self.bgImage)
self.add(self.lbl)
self.width_range = [0, 0]
self.height_range = [0, 0]
def setWH(self, dt):
x, y = director.window.width, director.window.height
nmin = sc.scale(int(self.parent.x + (self.x - (self.width / 2))), int(self.parent.y + (self.y - (self.height / 2))))
nmax = sc.scale(int(self.parent.x + (self.x + (self.width / 2))), int(self.parent.y + (self.y + (self.height / 2))))
self.width_range = [int(nmin[0]), int(nmax[0])]
self.height_range = [int(nmin[1]), int(nmax[1])]
def on_mouse_motion(self, x, y, dx, dy):
if self.showing:
if x in range(self.width_range[0], self.width_range[1]) and y in range(self.height_range[0], self.height_range[1]):
if self.active:
self.bgImage.image = pyglet.resource.image("largeToggledButtonHovered.png")
else:
self.bgImage.image = pyglet.resource.image("largeButtonHovered.png")
else:
if self.active:
self.bgImage.image = pyglet.resource.image("largeToggledButton.png")
else:
self.bgImage.image = pyglet.resource.image("largeButton.png")
def on_mouse_press(self, x, y, buttons, modifiers):
if self.showing:
if x in range(self.width_range[0], self.width_range[1]) and y in range(self.height_range[0], self.height_range[1]):
self.active = not self.active
self.changed = True
self.configDict[self.section][self.option] = str(self.active)
if callable(self.command):
self.command(self.active)
if self.active:
self.bgImage.image = pyglet.resource.image("largeToggledButtonClicked.png")
self.lbl.element.text = "YES"
else:
self.bgImage.image = pyglet.resource.image("largeButtonClicked.png")
self.lbl.element.text = "NO"
@property
def showing(self):
return self._showing
@showing.setter
def showing(self, value):
self._showing = value
def on_enter(self):
super().on_enter()
self.schedule_interval(self.setWH, 1)
self.resume_scheduler()
"""Medium button. Same as small button, but can contain more text"""
class mediumButton(layer.Layer):
is_event_handler = True
def __init__(self, label, eventName, active = False, *args, **kwargs):
super().__init__()
global x, y
self.eventName = eventName
self.active = active
if "eventparam" in kwargs:
self.eventparam = kwargs["eventparam"]
self.bgImage = Sprite("mediumButton.png")
self.lbl = Label(label, anchor_x="center", anchor_y="center", dpi=110, font_size=16)
self.x = 0
self.y = 0
self.px = 0
self.py = 0
self.width_range = [int((self.px + self.x) - (self.bgImage.width / 2)), int((self.px + self.x) + (self.bgImage.width / 2))]
self.height_range = [int((self.py + self.y) - (self.bgImage.height / 2)), int((self.py + self.y) + (self.bgImage.height / 2))]
self.add(self.bgImage)
self.add(self.lbl)
self.hide(0.01)
self.showing = False
self.schedule_interval(self.setWH, 1)
self.resume_scheduler()
def setWH(self, dt):
x, y = director.window.width, director.window.height
nmin = sc.scale(int((self.px + self.x) - (self.bgImage.width / 2)), int((self.py + self.y) - (self.bgImage.height / 2)))
nmax = sc.scale(int((self.px + self.x) + (self.bgImage.width / 2)), int((self.py + self.y) + (self.bgImage.height / 2)))
self.width_range = [int(nmin[0]), int(nmax[0])]
self.height_range = [int(nmin[1]), int(nmax[1])]
def on_mouse_motion(self, x, y, dx, dy):
if self.showing:
if self.active:
self.bgImage.image = pyglet.resource.image("mediumButtonClicked.png")
self.lbl.element.color = (0, 0, 0, 255)
elif x in range(self.width_range[0], self.width_range[1]) and y in range(self.height_range[0], self.height_range[1]):
self.bgImage.image = pyglet.resource.image("mediumButtonHovered.png")
self.lbl.element.color = (255, 255, 255, 255)
else:
self.bgImage.image = pyglet.resource.image("mediumButton.png")
self.lbl.element.color = (255, 255, 255, 255)
def on_mouse_press(self, x, y, buttons, modifiers):
if self.showing:
if x in range(self.width_range[0], self.width_range[1]) and y in range(self.height_range[0], self.height_range[1]):
self.bgImage.image = pyglet.resource.image("mediumButtonClicked.png")
self.active = True
self.lbl.element.color = (0, 0, 0, 255)
if hasattr(self, "eventparam"):
self.eventName(self.eventparam)
else:
self.eventName()
self.active = False
def show(self, interval = 0.5):
for child in self.get_children():
child.do(FadeIn(interval))
self.showing = True
def hide(self, interval = 0.5):
for child in self.get_children():
child.do(FadeOut(interval))
self.showing = False
"""Small button. A button for placing in small spaces, or to activate non-crucial functions"""
class smallButton(layer.Layer):
is_event_handler = True
def __init__(self, label, eventName, active = False, *args, **kwargs):
super().__init__()
global x, y
self.eventName = eventName
self.active = active
if "eventparam" in kwargs:
self.eventparam = kwargs["eventparam"]
self.bgImage = Sprite("smallButton.png")
self.lbl = Label(label, anchor_x="center", anchor_y="center", dpi=110, font_size=16)
self.x = 0
self.y = 0
self.px = 0
self.py = 0
self.width_range = [int((self.px + self.x) - (self.bgImage.width / 2)), int((self.px + self.x) + (self.bgImage.width / 2))]
self.height_range = [int((self.py + self.y) - (self.bgImage.height / 2)), int((self.py + self.y) + (self.bgImage.height / 2))]
self.add(self.bgImage)
self.add(self.lbl)
self.hide(0.01)
self.showing = False
self.schedule_interval(self.setWH, 1)
self.resume_scheduler()
def setWH(self, dt):
x, y = director.window.width, director.window.height
nmin = sc.scale(int((self.px + self.x) - (self.bgImage.width / 2)), int((self.py + self.y) - (self.bgImage.height / 2)))
nmax = sc.scale(int((self.px + self.x) + (self.bgImage.width / 2)), int((self.py + self.y) + (self.bgImage.height / 2)))
self.width_range = [int(nmin[0]), int(nmax[0])]
self.height_range = [int(nmin[1]), int(nmax[1])]
def on_mouse_motion(self, x, y, dx, dy):
if self.showing:
if self.active:
self.bgImage.image = pyglet.resource.image("smallButtonClicked.png")
self.lbl.element.color = (0, 0, 0, 255)
elif x in range(self.width_range[0], self.width_range[1]) and y in range(self.height_range[0], self.height_range[1]):
self.bgImage.image = pyglet.resource.image("smallButtonHovered.png")
self.lbl.element.color = (255, 255, 255, 255)
else:
self.bgImage.image = pyglet.resource.image("smallButton.png")
self.lbl.element.color = (255, 255, 255, 255)
def on_mouse_press(self, x, y, buttons, modifiers):
if self.showing:
if x in range(self.width_range[0], self.width_range[1]) and y in range(self.height_range[0], self.height_range[1]):
self.bgImage.image = pyglet.resource.image("smallButtonClicked.png")
self.active = True
self.lbl.element.color = (0, 0, 0, 255)
if hasattr(self, "eventparam"):
self.eventName(self.eventparam)
else:
self.eventName()
self.active = False
def show(self, interval = 0.5):
for child in self.get_children():
child.do(FadeIn(interval))
self.showing = True
def hide(self, interval = 0.5):
for child in self.get_children():
child.do(FadeOut(interval))
self.showing = False
class SettingsAboutBox(layer.Layer):
is_event_handler = True
def __init__(self):
super().__init__()
events.settingsevents.push_handlers(self)
self.infoBox = Sprite("infoBox.png")
self.bgDimmer = layer.ColorLayer(0, 0, 0, 20)
self.exitButton = smallButton("X", events.settingsevents.aboutPopupHide)
self.bgDimmer.opacity = 100
self.infoBox.x = reswidth / 2
self.infoBox.y = resheight / 2
self.bgDimmer.width = reswidth
self.bgDimmer.height = resheight
self.exitButton.x = self.infoBox.x + ((self.infoBox.width / 2) * 0.91)
self.exitButton.y = self.infoBox.y + ((self.infoBox.height / 2) * 0.85)
self.active = False
self.title = Label("title", anchor_x="center", anchor_y="center", font_size=39, color=(0, 0, 0, 255))
self.title.x = self.infoBox.x
self.title.y = self.infoBox.y + ((self.infoBox.height / 2) * 0.8)
self.desc = Label("desc", anchor_x="center", anchor_y="top", font_size=17, multiline = True, width=(self.infoBox.width * 0.87), height=(self.infoBox.height * 0.4), color=(0, 0, 0, 255), align="center")
self.desc.x = self.infoBox.x
self.desc.y = self.infoBox.y + ((self.infoBox.height / 2) * 0.6)
self.add(self.infoBox, z=5)
self.add(self.bgDimmer, z=4)
self.add(self.exitButton, z=5)
self.add(self.title, z=5)
self.add(self.desc, z=5)
self.bgDimmer.do(FadeOut(0.00001))
self.infoBox.do(FadeOut(0.00001))
self.exitButton.do(FadeOut(0.00001))
self.title.do(FadeOut(0.00001))
self.desc.do(FadeOut(0.00001))
def showAboutPopup(self):
if not self.active:
self.infoBox.do(FadeIn(0.75))
self.bgDimmer.do(FadeTo(150, 0.75))
self.title.do(FadeIn(0.75))
self.desc.do(FadeIn(0.75))
self.exitButton.show()
self.active = True
def hideAboutPopup(self):
if self.active:
self.infoBox.do(FadeOut(0.75))
self.bgDimmer.do(FadeTo(0, 0.75))
self.title.do(FadeOut(0.75))
self.desc.do(FadeOut(0.75))
self.exitButton.hide()
self.active = False