forked from pythonarcade/arcade
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gui.py
432 lines (372 loc) · 15.7 KB
/
gui.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
from typing import Tuple, Dict, Optional, Union
import arcade
# from abc import ABC, abstractmethod
class TextButton:
""" Text-based button """
def __init__(self,
center_x, center_y,
width, height,
text,
font_size=18, font_face: Union[str, Tuple[str, ...]] = "Arial", font_color = None,
face_color=arcade.color.LIGHT_GRAY,
highlight_color=arcade.color.WHITE,
shadow_color=arcade.color.GRAY,
button_height=2,
theme=None):
self.center_x = center_x
self.center_y = center_y
self.width = width
self.height = height
self.text = text
self.pressed = False
self.active = True
self.button_height = button_height
self.theme = theme
self.font_color = font_color
if self.theme:
self.normal_texture = self.theme.button_textures['normal']
self.hover_texture = self.theme.button_textures['hover']
self.clicked_texture = self.theme.button_textures['clicked']
self.locked_texture = self.theme.button_textures['locked']
self.font_size = self.theme.font_size
self.font_name = self.theme.font_name
self.font_color = self.theme.font_color
else:
self.font_size = font_size
self.font_face = font_face
self.face_color = face_color
self.highlight_color = highlight_color
self.shadow_color = shadow_color
self.font_name = font_face
if self.font_color is None:
self.font_color = self.face_color
def draw_color_theme(self):
arcade.draw_rectangle_filled(self.center_x, self.center_y, self.width,
self.height, self.face_color)
if not self.pressed:
color = self.shadow_color
else:
color = self.highlight_color
# Bottom horizontal
arcade.draw_line(self.center_x - self.width / 2, self.center_y - self.height / 2,
self.center_x + self.width / 2, self.center_y - self.height / 2,
color, self.button_height)
# Right vertical
arcade.draw_line(self.center_x + self.width / 2, self.center_y - self.height / 2,
self.center_x + self.width / 2, self.center_y + self.height / 2,
color, self.button_height)
if not self.pressed:
color = self.highlight_color
else:
color = self.shadow_color
# Top horizontal
arcade.draw_line(self.center_x - self.width / 2, self.center_y + self.height / 2,
self.center_x + self.width / 2, self.center_y + self.height / 2,
color, self.button_height)
# Left vertical
arcade.draw_line(self.center_x - self.width / 2, self.center_y - self.height / 2,
self.center_x - self.width / 2, self.center_y + self.height / 2,
color, self.button_height)
x = self.center_x
y = self.center_y
if not self.pressed:
x -= self.button_height
y += self.button_height
def draw_texture_theme(self):
if self.pressed:
arcade.draw_texture_rectangle(self.center_x, self.center_y, self.width, self.height, self.clicked_texture)
else:
arcade.draw_texture_rectangle(self.center_x, self.center_y, self.width, self.height, self.normal_texture)
def draw(self):
""" Draw the button """
if self.theme:
self.draw_texture_theme()
else:
self.draw_color_theme()
arcade.draw_text(self.text, self.center_x, self.center_y,
self.font_color, font_size=self.font_size,
font_name=self.font_name,
width=self.width, align="center",
anchor_x="center", anchor_y="center")
def on_press(self):
pass
def on_release(self):
pass
def check_mouse_press(self, x, y):
if x > self.center_x + self.width / 2:
return
if x < self.center_x - self.width / 2:
return
if y > self.center_y + self.height / 2:
return
if y < self.center_y - self.height / 2:
return
self.on_press()
def check_mouse_release(self, _x, _y):
if self.pressed:
self.on_release()
class SubmitButton(TextButton):
def __init__(self, textbox, on_submit, x, y, width=100, height=40, text="submit", theme=None):
super().__init__(x, y, width, height, text, theme=theme)
self.textbox = textbox
self.on_submit = on_submit
def on_press(self):
self.pressed = True
def on_release(self):
if self.pressed:
self.pressed = False
self.on_submit()
self.textbox.text_storage.text = ""
self.textbox.text_display.text = ""
class DialogueBox:
def __init__(self, x, y, width, height, color=None, theme=None):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.active = False
self.button_list = []
self.text_list = []
self.theme = theme
if self.theme:
self.texture = self.theme.dialogue_box_texture
def on_draw(self):
if self.active:
if self.theme:
arcade.draw_texture_rectangle(self.x, self.y, self.width, self.height, self.texture)
else:
arcade.draw_rectangle_filled(self.x, self.y, self.width, self.height, self.color)
for button in self.button_list:
button.draw()
for text in self.text_list:
text.draw()
def on_mouse_press(self, x, y, _button, _modifiers):
for button in self.button_list:
button.check_mouse_press(x, y)
def on_mouse_release(self, x, y, _button, _modifiers):
for button in self.button_list:
button.check_mouse_release(x, y)
class Text:
def __init__(self, text, x, y, color=arcade.color.BLACK, font_size=22, anchor_x="center",
anchor_y="center", width: int = 0,
align="center",
font_name=('Calibri', 'Arial'),
bold: bool = False,
italic: bool = False, rotation=0):
self.text = text
self.x = x
self.y = y
self.color = color
self.font_size = font_size
self.anchor_x = anchor_x
self.anchor_y = anchor_y
self.width = width
self.align = align
self.font_name = font_name
self.bold = bold
self.italic = italic
self.rotation = rotation
self.active = True
def draw(self):
arcade.draw_text(self.text, self.x, self.y, self.color, font_size=self.font_size,
anchor_x=self.anchor_x,
anchor_y=self.anchor_y,
width=self.width, align=self.align,
font_name=self.font_name, bold=self.bold,
italic=self.italic, rotation=self.rotation)
class TextDisplay:
def __init__(self, x, y, width=300, height=40, outline_color=arcade.color.BLACK,
shadow_color=arcade.color.WHITE_SMOKE, highlight_color=arcade.color.WHITE, theme=None):
self.x = x
self.y = y
self.width = width
self.height = height
self.outline_color = outline_color
self.shadow_color = shadow_color
self.highlight_color = highlight_color
self.highlighted = False
self.text = ""
self.left_text = ""
self.right_text = ""
self.symbol = "|"
self.cursor_index = 0
self.theme = theme
if self.theme:
self.texture = self.theme.text_box_texture
self.font_size = self.theme.font_size
self.font_color = self.theme.font_color
self.font_name = self.theme.font_name
else:
self.texture = None
self.font_size = 24
self.font_color = arcade.color.BLACK
self.font_name = ('Calibri', 'Arial')
def draw_text(self):
if self.highlighted:
arcade.draw_text(self.text[:self.cursor_index] + self.symbol + self.text[self.cursor_index:],
self.x-self.width/2.1, self.y, self.font_color, font_size=self.font_size,
anchor_y="center", font_name=self.font_name)
else:
arcade.draw_text(self.text, self.x-self.width/2.1, self.y, self.font_color, font_size=self.font_size,
anchor_y="center", font_name=self.font_name)
def color_theme_draw(self):
if self.highlighted:
arcade.draw_rectangle_filled(self.x, self.y, self.width, self.height, self.highlight_color)
else:
arcade.draw_rectangle_filled(self.x, self.y, self.width, self.height, self.shadow_color)
self.draw_text()
arcade.draw_rectangle_outline(self.x, self.y, self.width, self.height, self.outline_color, 2)
def texture_theme_draw(self):
arcade.draw_texture_rectangle(self.x, self.y, self.width, self.height, self.texture)
self.draw_text()
def draw(self):
if self.texture == "":
self.color_theme_draw()
else:
self.texture_theme_draw()
def on_press(self):
self.highlighted = True
def on_release(self):
pass
def check_mouse_press(self, x, y):
if x > self.x + self.width / 2:
self.highlighted = False
return
if x < self.x - self.width / 2:
self.highlighted = False
return
if y > self.y + self.height / 2:
self.highlighted = False
return
if y < self.y - self.height / 2:
self.highlighted = False
return
self.on_press()
def check_mouse_release(self, _x, _y):
if self.highlighted:
self.on_release()
def update(self, _delta_time, text, symbol, cursor_index):
self.text = text
self.symbol = symbol
self.cursor_index = cursor_index
class TextStorage:
def __init__(self, box_width, font_size=24, theme=None):
self.box_width = box_width
self.font_size = font_size
self.theme = theme
if self.theme:
self.font_size = self.theme.font_size
self.char_limit = self.box_width / self.font_size
self.text = ""
self.cursor_index = 1
self.cursor_symbol = "|"
self.local_cursor_index = 0
self.time = 0.0
self.left_index = 0
self.right_index = 1
self.visible_text = ""
def blink_cursor(self):
seconds = self.time % 60
if seconds > 0.1:
if self.cursor_symbol == "_":
self.cursor_symbol = "|"
else:
self.cursor_symbol = "_"
self.time = 0.0
def update(self, delta_time, key):
self.time += delta_time
# self.blink_cursor()
if key:
if key == arcade.key.BACKSPACE:
if self.cursor_index < len(self.text):
text = self.text[:self.cursor_index-1]
self.text = text + self.text[self.cursor_index:]
else:
self.text = self.text[:-1]
if self.cursor_index > 0:
self.cursor_index -= 1
if self.left_index > 0:
self.left_index -= 1
if self.right_index > 1:
self.right_index -= 1
elif key == arcade.key.LEFT:
if self.cursor_index > 0:
self.cursor_index -= 1
if 0 < self.left_index == self.cursor_index:
self.left_index -= 1
self.right_index -= 1
elif key == arcade.key.RIGHT:
if self.cursor_index < len(self.text):
self.cursor_index += 1
if len(self.text) > self.right_index == self.cursor_index:
self.right_index += 1
self.left_index += 1
else:
if self.cursor_index < len(self.text):
self.text = self.text[:self.cursor_index] + chr(key) + self.text[self.cursor_index:]
self.cursor_index += 1
self.right_index += 1
if len(self.text) > self.char_limit:
self.left_index += 1
else:
self.text += chr(key)
self.cursor_index += 1
self.right_index += 1
if len(self.text) >= self.char_limit:
self.left_index += 1
self.visible_text = self.text[self.left_index:self.right_index]
if self.cursor_index > self.left_index:
self.local_cursor_index = self.cursor_index - self.left_index
else:
self.local_cursor_index = self.left_index
return self.visible_text, self.cursor_symbol, self.local_cursor_index
class TextBox:
def __init__(self, x, y, width=300, height=40, theme=None, outline_color=arcade.color.BLACK, font_size=24,
shadow_color=arcade.color.WHITE_SMOKE, highlight_color=arcade.color.WHITE):
self.theme = theme
if self.theme:
self.text_display = TextDisplay(x, y, width, height, theme=self.theme)
self.text_storage = TextStorage(width, theme=self.theme)
else:
self.text_display = TextDisplay(x, y, width, height, outline_color, shadow_color, highlight_color)
self.text_storage = TextStorage(width, font_size)
self.text = ""
def draw(self):
self.text_display.draw()
def update(self, delta_time, key):
if self.text_display.highlighted:
self.text, symbol, cursor_index = self.text_storage.update(delta_time, key)
self.text_display.update(delta_time, self.text, symbol, cursor_index)
def check_mouse_press(self, x, y):
self.text_display.check_mouse_press(x, y)
def check_mouse_release(self, x, y):
self.text_display.check_mouse_release(x, y)
class Theme:
def __init__(self):
self.button_textures: Dict[str, Optional['', arcade.Texture]] =\
{'normal': '', 'hover': '', 'clicked': '', 'locked': '', }
self.menu_texture = ""
self.window_texture = ""
self.dialogue_box_texture = ""
self.text_box_texture = ""
self.font_color = arcade.color.BLACK
self.font_size = 24
self.font_name = ('Calibri', 'Arial')
def add_button_textures(self, normal, hover="", clicked="", locked=""):
self.button_textures['normal'] = arcade.load_texture(normal)
self.button_textures['hover'] = arcade.load_texture(hover)
self.button_textures['clicked'] = arcade.load_texture(clicked)
self.button_textures['locked'] = arcade.load_texture(locked)
def add_window_texture(self, window_texture):
self.window_texture = arcade.load_texture(window_texture)
def add_menu_texture(self, menu_texture):
self.menu_texture = arcade.load_texture(menu_texture)
def add_dialogue_box_texture(self, dialogue_box_texture):
self.dialogue_box_texture = arcade.load_texture(dialogue_box_texture)
def add_text_box_texture(self, text_box_texture):
self.text_box_texture = arcade.load_texture(text_box_texture)
def set_font(self, font_size, font_color, font_name=('Calibri', 'Arial')):
self.font_color = font_color
self.font_size = font_size
self.font_name = font_name