-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.py
1339 lines (1090 loc) · 41 KB
/
ui.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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# name: ui.py
# auth: christian bitter
# desc: building simple ui with pygame
# TODO:
# we need to ensure that rendering is blitting to the callers x, y coordinates
# Controls render into their surface in local coordinates. For that, they have a local rendering surface.
# Control captions/ text can be aligned horizontally and vertically.
# UI elements are re-rendered upon invalidation. In that case the elements paint method is called, causing the
# UI elements surface buffer to be refreshed, so that it can be blitted with valid content subsequently.
# todo: ui element text box
# UI element Button allows to have an image set.
# We make no distinction between clicked or not for now (which would justify an Image Button).
# todo: on mouse over event
# todo: on focus event
# todo: ui element label
# todo: support for rendering form with background and alpha blending of controls
# todo: having to forward the pygame event loop events to the controls is cumbersome, there
# should be something cleaner
# todo: there should be something like overlays - where we have one form and can create smaller one blending it
# over the existing one.
# todo: add unit testing
# todo: controls should have a global coordinate pair as well, this would simplify the hit-testing
# todo: screen transitions either in the screen or in the UI
# todo: initialization needs to be fixed, because for some ui elements we need to set fields that are used in the super
# initialization routine
import os
import pygame
import re
from enum import Enum, IntFlag
C_BLACK = (0, 0, 0, 255)
C_RED = (255, 0, 0, 255)
C_GREEN = (0, 255, 0, 255)
C_BLUE = (0, 0, 255, 255)
C_MENUGRAY = (192, 192, 192, 255)
C_FORMBLUE = (32, 32, 128, 255)
C_ELEMENT_BORDER_DARKGRAY = (64, 64, 64, 255)
C_WHITE = (255, 255, 255, 255)
C_BTN_FACE = (168, 168, 168, 255)
C_BTN_BORDER = (128, 128, 128, 255)
I_MARGIN = 3
def xy_inside(x: int, y: int, x0: int, y0: int, w: int, h: int) -> bool:
return x0 <= x <= x0 + w and y0 <= y <= y0 + h
class UIEvent:
SCREEN_TRANSITION: int = 99999
@staticmethod
def transition_screen(event_from: str, event_to: str):
return pygame.event.Event(
pygame.USEREVENT,
{
"mode": UIEvent.SCREEN_TRANSITION,
"source": event_from,
"target": event_to,
},
)
class FillStyle(Enum):
Empty = 1
Colour = 2
Image = 3
class VerticalAlignment(Enum):
Center = 0
Top = 1
Bottom = 2
def __str__(self):
return self.name
class HorizontalAlignment(Enum):
Center = 0
Left = 1
Right = 2
def __str__(self):
return self.name
class FontStyle(IntFlag):
"""
Style of a font
"""
Normal = (0,)
Bold = (1,)
Italic = (2,)
Underline = 8
def __str__(self):
return self.name
class UIElement(object):
"""
This is the base type of all UI elements. Critically, it has a name/id and some
spatial location and extent.
"""
def __init__(
self, name, x: int = None, y: int = None, w: int = 0, h: int = 0, **kwargs
):
"""Constructor for UIElement"""
super().__init__()
self._name = name
self._x0 = x
self._y0 = y
self._w = w
self._h = h
self._x1 = None
self._y1 = None
if self._x0 is not None and self._w is not None:
self._x1 = x + w
if self._y0 is not None and self._h is not None:
self._y1 = y + h
self._client_rect = (0, 0, self._w, self._h)
self._invalidated = kwargs.get("invalidated", False)
self._has_focus = kwargs.get("has_focus", False)
@property
def name(self):
return self._name
@property
def x(self):
return self._x0
@x.setter
def x(self, x: int):
if not x:
raise ValueError("x")
if x < 0:
raise ValueError("x < 0")
if self._x0 != x:
self._x0 = x
self._x1 = self._x0 + self._w
self.invalidate()
@property
def y(self):
return self._y0
@y.setter
def y(self, y: int):
if not y:
raise ValueError("y")
if y < 0:
raise ValueError("y < 0")
if self._y0 != y:
self._y0 = y
self._y1 = self._y0 + self._h
self.invalidate()
@property
def width(self):
return self._w
@width.setter
def width(self, w: int):
if w is None:
raise ValueError("w cannot be None")
if w < 0:
raise ValueError("w < 0")
if self._w != w:
self._w = w
self._x1 = self._x0 + w
self.invalidate()
@property
def height(self):
return self._h
@height.setter
def height(self, h: int = None):
if h is None:
raise ValueError("h cannot be None")
if h < 0:
raise ValueError("h < 0")
if self._h != h:
self._h = h
self._y1 = self._y0 + h
self.invalidate()
def invalidate(self, **kwargs):
self._client_rect = (0, 0, self._w, self._h)
self._invalidated = True
def get_bounds(self):
return self._x0, self._y0, self._x1, self._y1
@property
def has_focus(self):
return self._has_focus
@has_focus.setter
def has_focus(self, f):
self._has_focus = f
def __repr__(self):
return "UIElement: {} ({})".format(self._name, type(self))
def initialize(self) -> None:
pass
def finalize(self) -> None:
pass
def update(self, t) -> None:
pass
def process_event(self, e) -> None:
pass
class Renderable(UIElement):
"""
The basic class for all ui renderable elements
"""
def __init__(
self, name, x: int = None, y: int = None, w: int = None, h: int = None, **kwargs
):
"""Constructor for Renderable"""
super().__init__(name, x=x, y=y, w=w, h=h, kwargs=kwargs)
self._fill_style = kwargs.get("fill_style", FillStyle.Empty)
self._background_colour = kwargs.get("background_colour", (128, 128, 128, 255))
self._background_image = kwargs.get("background_image", None)
self._colour = kwargs.get("colour", (0, 0, 0, 255))
self._show_border = kwargs.get("show_border", True)
self._show_caption = kwargs.get("show_caption", False)
self._font_size = kwargs.get(
"font_size", 20
) # width and height is determined by font
self._font_style = kwargs.get("font_style", FontStyle.Normal)
self._font = pygame.font.Font(
kwargs.get("font", pygame.font.get_default_font()), self._font_size
)
self._font_colour = kwargs.get("font_colour", C_BLACK)
self._text_caption = None
self._text_bounds = None
self._caption = None
self._surface = None
self._caption = kwargs.get("caption", "")
self.__update_caption()
self._is_visible = kwargs.get("visible", True)
self._z_order = kwargs.get("z", 0)
self._caption_halign = kwargs.get("caption_halign", HorizontalAlignment.Left)
self._caption_valign = kwargs.get("caption_valign", VerticalAlignment.Center)
self._apply_font_style()
if self.width is not None and self.height is not None:
if self.width == 0 or self.height == 0:
print("{} has set width and/or height to 0 px".format(self._name))
self._surface = pygame.Surface(
(self.width, self.height), flags=pygame.SRCALPHA
)
else:
print(
"{}' surface was not initialized, because either width ({}) or height ({}) was not provided {}.".format(
self._name, self.width, self.height, (w, h)
)
)
self.invalidate()
def __update_caption(self):
self._text_caption = self._font.render(self._caption, 1, self._font_colour)
self._text_bounds = self._text_caption.get_rect()
# we add a safety buffer around the text bounds to allow for the real bounds
_w, _h = (
self._text_bounds[2] + 2 * I_MARGIN,
self._text_bounds[3] + 2 * I_MARGIN,
)
if self.width is None or self.width < _w:
self.width = _w
if self.height is None or self.height < _h:
self.height = _h
def _apply_font_style(self):
if self._font_style & FontStyle.Bold:
self._font.set_bold(True)
if self._font_style & FontStyle.Italic:
self._font.set_italic(True)
if self._font_style & FontStyle.Underline:
self._font.set_underline(True)
@property
def z_order(self):
return self._z_order
@property
def is_visible(self):
return self._is_visible
@property
def background_colour(self):
return self._background_colour
@property
def colour(self):
return self._colour
@property
def show_border(self):
return self._show_border
def invalidate(self, **kwargs):
UIElement.invalidate(self, **kwargs)
# if we need to create a new surface we do otherwise we just clear
if "clear_only" in kwargs:
self._surface.fill(C_BLACK)
else:
if self._w is not None and self._h is not None:
# TODO: we need to see if we need per pixel alpha
self._surface = pygame.Surface(
(self._w, self._h), flags=pygame.SRCALPHA
)
self._client_rect = (0, 0, self._w, self._h)
@property
def caption(self):
return self._caption
@caption.setter
def caption(self, c):
if c is None:
return
self._caption = c
self.__update_caption()
@property
def show_caption(self):
return self._show_caption
def _paint(self):
if self._invalidated:
if self._surface is None:
raise ValueError(
"Cannot paint into None surface: {}".format(self._name)
)
if self._fill_style == FillStyle.Colour:
pygame.draw.rect(
self._surface, self._background_colour, self._client_rect, 0
)
elif self._fill_style == FillStyle.Image:
if not self._background_image:
raise ValueError("background fill image but image not provided")
scaled_bgimg = pygame.transform.scale(
self._background_image, (self._w, self._h)
)
self._surface.blit(scaled_bgimg, dest=self._client_rect)
else:
pass
if self._show_border:
pygame.draw.rect(self._surface, self._colour, self._client_rect, 1)
if self._show_caption and self._caption != "":
# We place text in accordance with the chosen alignment.
# This means vertically and horizontally, inside the parent's bounding box. The parent's
# bounding box is at least so wide, so as to be able to capture the text.
# It does not need to be recomputed every time - this can be placed after the initialization
dest_pos = [0, 0]
c_halign = self._caption_halign
c_valign = self._caption_valign
if c_halign == HorizontalAlignment.Left:
dest_pos[0] = I_MARGIN
elif c_halign == HorizontalAlignment.Center:
dest_pos[0] = int(0.5 * (self._w - self._text_bounds[2]))
elif c_halign == HorizontalAlignment.Right:
dest_pos[0] = self._w - I_MARGIN - self._text_bounds[2]
else:
raise ValueError(
"unknown horizontal alignment setting {}".format(c_halign)
)
if c_valign == VerticalAlignment.Top:
dest_pos[1] = I_MARGIN
elif c_valign == VerticalAlignment.Center:
dest_pos[1] = int(0.5 * (self._h - self._text_bounds[3]))
elif c_valign == VerticalAlignment.Bottom:
dest_pos[1] = self._h - I_MARGIN - self._text_bounds[3]
else:
raise ValueError(
"unknown vertical alignment setting {}".format(c_valign)
)
self._surface.blit(self._text_caption, dest=tuple(dest_pos))
self._invalidated = False
if self._surface is None:
raise ValueError("Failed _paint {} - surface is None.".format(self._name))
def render(self, buffer):
if self._invalidated:
self._paint()
if self._is_visible:
buffer.blit(self._surface, (self.x, self.y))
class Clickable(Renderable):
""""""
def __init__(
self, name, x: int = None, y: int = None, w: int = None, h: int = None, **kwargs
):
"""Constructor for Clickable"""
super().__init__(name, x=x, y=y, w=w, h=h, **kwargs)
self._is_clicked = kwargs.get("is_clicked", False)
def unclick(self):
self._is_clicked = False
def clicked(self, mx, my, button):
previous_state = self._is_clicked
if xy_inside(mx, my, self._x0, self._y0, self._w, self._h):
self._is_clicked = True
else:
self._is_clicked = False
# upon state change we invalidate
if previous_state != self._is_clicked:
self.invalidate()
return self._is_clicked, self
def on_click(self, sender, event_args):
# when clicked this fires, it needs to be overwritten in a derived class
pass
class UIImage(Clickable):
"""
A component to show an image
"""
def __init__(
self,
name: str,
image_fp: str,
x: int,
y: int,
w: int = None,
h: int = None,
**kwargs
):
"""Constructor for UIImage"""
if not image_fp or not os.path.exists(image_fp):
raise ValueError("Image path not provided or does not exist")
img = pygame.image.load(image_fp)
if w is None:
w = img.get_width()
if h is None:
h = img.get_height()
s_img = pygame.transform.scale(img, (w, h))
s = pygame.Surface((w, h))
s.blit(s_img, (0, 0))
del img
kwargs["background_image"] = s
kwargs["fill_style"] = FillStyle.Image
self._image_fp = image_fp
super(UIImage, self).__init__(name=name, x=x, y=y, w=w, h=h, **kwargs)
def __repr__(self):
return "[UIImage] {}: {}".format(self._name, self._image_fp)
@property
def image(self):
return self._background_image
@property
def image_path(self):
return self._image_fp
def render(self, buffer):
if self._invalidated:
self._paint()
Renderable.render(self, buffer)
buffer.blit(
self._background_image, (self._x0, self._y0, self.width, self.height)
)
class Canvas(Clickable):
"""
A canvas is a simple renderable, which only exists for the purposes of drawing into it
"""
def __init__(self, name: str, x: int, y: int, w: int, h: int, **kwargs):
"""Constructor for Canvas"""
kwargs["show_border"] = False
kwargs["show_caption"] = False
super(Canvas, self).__init__(name=name, x=x, y=y, w=w, h=h, **kwargs)
def render(self, buffer):
if self.is_visible:
return
if self._invalidated:
self._paint()
buffer.blit(self._surface, (self.x, self.y))
class Label(Renderable):
"""
Label holding a single line of text
"""
def __init__(self, name: str, x: int, y: int, caption: str = None, **kwargs):
"""Constructor for TextLabel"""
if caption is None:
print("Caption ({}) cannot be none - set to empty string".format(name))
caption = ""
if os.linesep in caption:
print(
"Label ({}) does not support multiple lines of text/ line breaks".format(
name
)
)
kwargs["caption"] = caption
kwargs["show_caption"] = True
if "background_colour" not in kwargs:
kwargs["background_colour"] = C_MENUGRAY
if "fill_style" not in kwargs:
kwargs["fill_style"] = FillStyle.Colour
if "caption_halign" not in kwargs:
kwargs["caption_halign"] = HorizontalAlignment.Left
if "caption_valign" not in kwargs:
kwargs["caption_valign"] = VerticalAlignment.Center
if "colour" not in kwargs:
kwargs["colour"] = C_ELEMENT_BORDER_DARKGRAY
super(Label, self).__init__(name=name, x=x, y=y, **kwargs)
class TextBox(Label):
"""
A simple text box with a blinking cursor
"""
__CURSOR__ = "|"
__BLINK_ON__ = 1
__BLINK_OFF__ = 0
__IGNORE_KEYS__ = [pygame.K_LSHIFT, pygame.K_LALT, pygame.K_LCTRL]
def __init__(
self, name: str, x: int, y: int, max_chars: int = 20, w: int = 200, **kwargs
):
"""Constructor for TextBox"""
kwargs["show_border"] = True
kwargs["background_colour"] = C_WHITE
self._is_clicked = False
self._blink_state = 0
self._change_state_millis = 200
self._dmillis = 0
self._maxchars = max_chars
self._cursor_added = False
super(TextBox, self).__init__(name=name, x=x, y=y, w=w, **kwargs)
@property
def maxchars(self):
return self._maxchars
def update(self, t):
if self._has_focus:
self._dmillis += t
if (
self._blink_state == TextBox.__BLINK_OFF__
and self._dmillis >= self._change_state_millis
):
self._blink_state = TextBox.__BLINK_ON__
self._dmillis = 0
if (
self._blink_state == TextBox.__BLINK_ON__
and self._dmillis >= self._change_state_millis
):
self._blink_state = TextBox.__BLINK_OFF__
self._dmillis = 0
self.invalidate()
else:
self._dmillis = 0
self._blink_state = 0
def clicked(self, mx, my, button):
clicked, sender = Clickable.clicked(self, mx, my, button)
if clicked:
self.has_focus = True
else:
self.has_focus = False
return clicked, sender
def add_char(self, c) -> None:
if len(self.caption) < self._maxchars:
self.caption = self.caption + c
def remove_char(self) -> None:
_len = len(self.caption)
if _len > 0:
self.caption = self.caption[0 : (_len - 1)]
def _paint(self):
# add the blinking ...
t_caption = self.caption
if self._blink_state == 1:
self.caption = self.caption + TextBox.__CURSOR__
Label._paint(self)
self.caption = t_caption
def process_event(self, e) -> None:
if e.key == pygame.K_BACKSPACE:
self.remove_char()
elif e.key == pygame.K_RETURN:
self.has_focus = False
else:
self.add_char(e.unicode)
class MultiLineLabel(Label):
"""
TODO MultiLineLabel - horizontal alignment
TODO MultiLineLabel - vertical alignment
TODO MultiLineLabel - new line handling - newline should be handled explicitly, when they are the single line element
https://www.pygame.org/docs/ref/font.html
The text can only be a single line: newline characters are not rendered.
"""
def __init__(self, name: str, x: int, y: int, caption: str, **kwargs):
"""Constructor for MultiLineLabel"""
w = kwargs.get("width", 200)
kwargs["width"] = w
kwargs["show_caption"] = False
super(MultiLineLabel, self).__init__(
name=name, x=x, y=y, caption=caption, **kwargs
)
w_available = w - 2 * I_MARGIN
self._lines = MultiLineLabel.split_text(
self._font, text=caption, label_width=w_available
)
self._text_lines = ["".join(_line).strip() for _line in self._lines]
h_i = I_MARGIN
for l_i in self._text_lines:
s_i = self._font.render(l_i, 0, self._font_colour)
h_i += s_i.get_height()
h_i += I_MARGIN
self.width = w
self.height = h_i
self.invalidate()
@staticmethod
def split_text(font, text: str, label_width: int):
if font is None:
raise ValueError("Font is not defined")
if text is None:
raise ValueError("Text is None")
if label_width is None:
raise ValueError("Label width is None")
if label_width < 0:
raise ValueError("Label width < 1")
if text == "":
return text
frag_delim = ".,!?();:"
s = text.strip()
s = re.sub(
"([{}])".format(frag_delim), r" \1 ", s
) # put space around the first match group
s = re.sub(
r"\s{2,}", " ", s
) # collapse 2 white space characters into a single space
# now simply split on space and get as many tokens into a line as possible
s = re.split(r"(\s+)", s)
sx = []
for i, t in enumerate(s):
if t == "":
continue
if t == os.linesep or t == "\n":
t = " "
if (
i < len(s) - 1 and s[i] == " " and s[i + 1] in frag_delim
): # collapse ' ', '.' into '.'
continue
sx.append(t)
s = sx
temp_width = 0
temp_sentence = []
out_tokens = []
# far and ask whether this is in the limits.
for i, t in enumerate(s):
r = font.render(t, 1, (0, 0, 0))
w = r.get_width()
# TODO: we do not put sentence delimiters as an opening token
# still there is a breaking issue
# instead we pull the last token of the previous line with it
if temp_width + w >= label_width:
# remove ending spaces from the temps
x_sentence = temp_sentence.copy()
_seq_len = len(x_sentence) - 1
if x_sentence[_seq_len] == " ":
x_sentence = x_sentence[0:_seq_len]
if (
t in frag_delim
): # if t is a sentence/ fragement delimiter, we do not put on the next line
x_sentence.append(t)
t = None
temp_sentence = []
out_tokens.append(x_sentence)
temp_width = 0
if t is None:
continue
temp_sentence.append(t)
temp_width += w
if len(temp_sentence) > 0:
out_tokens.append(temp_sentence)
return out_tokens
def _destpos_from_aligment(self, text_bounds):
dest_pos = [0, 0]
c_halign = self._caption_halign
if c_halign == HorizontalAlignment.Left:
dest_pos[0] = I_MARGIN
elif c_halign == HorizontalAlignment.Center:
dest_pos[0] = int(0.5 * (self._w - text_bounds[2]))
elif c_halign == HorizontalAlignment.Right:
dest_pos[0] = self._w - I_MARGIN - text_bounds[2]
else:
raise ValueError("unknown horizontal alignment setting {}".format(c_halign))
dest_pos[1] = I_MARGIN
return dest_pos
def _paint(self):
if self._surface is None:
raise ValueError("Cannot paint into None surface: {}".format(self._name))
if self._fill_style == FillStyle.Colour:
pygame.draw.rect(
self._surface, self._background_colour, self._client_rect, 0
)
elif self._fill_style == FillStyle.Image:
if not self._background_image:
raise ValueError("background fill image but image not provided")
scaled_bgimg = pygame.transform.scale(
self._background_image, (self._w, self._h)
)
self._surface.blit(scaled_bgimg, dest=self._client_rect)
else:
pass
if self._show_border:
pygame.draw.rect(self._surface, self._colour, self._client_rect, 1)
if self._show_caption and self._caption != "":
# We place text in accordance with the chosen alignment.
# This means vertically and horizontally, inside the parent's bounding box. The parent's
# bounding box is at least so wide, so as to be able to capture the text.
# It does not need to be recomputed every time - this can be placed after the initialization
dst = self._destpos_from_aligment(self._client_rect)
x_i = dst[0]
y_i = I_MARGIN
for s_i in self._text_lines:
d_pos = [x_i, y_i]
s_i = self._font.render(s_i, 0, self._font_colour)
self._surface.blit(s_i, dest=tuple(d_pos))
y_i += s_i.get_height()
self._invalidated = False
def render(self, buffer):
if not self._show_caption and not self._show_border:
return
if self._invalidated:
self._paint()
buffer.blit(self._surface, (self.x, self.y))
class Button(Clickable):
"""
A button is an element that can be clicked. It is in either of two states, clicked or not clicked.
When the user clicks a button an event is fired and you may react to it.
"""
def __init__(
self,
name,
x: int,
y: int,
w: int = None,
h: int = None,
image_fp: str = None,
**kwargs
):
"""Constructor for Button"""
# if we do have a caption but not an explicit show_caption, assume the default of show
if "caption" in kwargs and "show_caption" not in kwargs:
kwargs["show_caption"] = True
if "background_colour" not in kwargs:
kwargs["background_colour"] = C_BTN_FACE
if "fill_style" not in kwargs:
kwargs["fill_style"] = FillStyle.Colour
if "colour" not in kwargs:
kwargs["colour"] = C_BTN_BORDER
if image_fp is not None and os.path.exists(image_fp):
kwargs["background_image"] = pygame.image.load(image_fp)
super().__init__(name=name, x=x, y=y, w=w, h=h, **kwargs)
def _paint(self):
Clickable._paint(self)
if self._is_clicked:
pygame.draw.rect(self._surface, C_RED, self._client_rect, 2)
class MenuItem(Button):
""""""
def __init__(self, name: str, caption: str, w: int = None, h: int = None, **kwargs):
"""Constructor for MenuItem"""
kwargs["caption"] = caption
kwargs["show_caption"] = True
kwargs["show_border"] = True
kwargs["fill_style"] = FillStyle.Colour
super().__init__(name=name, x=0, y=0, w=w, h=h, **kwargs)
class ClickableContainer(Clickable):
"""
TODO: most of the menu stuff belongs into a clickable container
"""
def __init__(
self, name: str, x: int, y: int, w: int = None, h: int = None, **kwargs
):
"""Constructor for ClickableContainer"""
super(ClickableContainer, self).__init__(x=x, y=y, w=w, h=h)
self._items = {}
self._item_names = []
self._iterm_inner_margin = kwargs.get("item_inner_margin", 2)
@property
def item_names(self):
return self._item_names
def __repr__(self):
return "ClickableContainer: {} - {} items".format(
self._name, len(self._item_names)
)
@property
def items(self):
return self._items
def __getitem__(self, item):
if item is None:
raise ValueError("getitem - key not provided")
if isinstance(item, int):
return self._items[self._item_names[item]]
elif isinstance(item, str):
if item in self._items:
return self._items[item]
else:
self._items.get(item)
def remove_item(self, item_name: str) -> None:
"""
Remove an item from the items.
:param item_name:
:return: None
"""
if item_name is None:
raise ValueError("Item name not provided")
if item_name not in self._item_names:
raise ValueError("Item does not exist")
# update item names and items
del self._items[item_name]
del self._item_names[self._item_names.index(item_name)]
self.invalidate()
def add_item(self, item, item_name: str):
"""
Add an item to the existing items.
:param item: the item to add
:param item_name: (str) the item's name
"""
if item is None:
raise ValueError("Item not provided")
if item_name is None:
raise ValueError("Item name not provided")
i_margin = self._iterm_inner_margin
i_x = i_margin
if len(self._item_names) < 1: # first item
if self._show_caption:
i_y = i_margin + self._text_bounds[3]
else:
i_y = i_margin
self.height = self.height + item.height
else:
last_item = self._items[self._item_names[len(self._item_names) - 1]]
i_y = last_item.y + last_item.height + i_margin
self.height = i_y + item.height + i_margin
item.x = i_x
item.y = i_y
if item.width >= self.width:
self.width = 2 * i_margin + item.width
for _, mi in self._items.items():
mi.width = item.width
else:
item.width = self.width - 2 * i_margin
# now add and invalidate
self._items[item_name] = item
self._item_names.append(item_name)
self.invalidate()
class Menu(Clickable):
"""
A game menu - Single container.
It does not support hotkeys, nesting and more common gui functionalty.
TODO: reuse the clicable container.
"""
MENU_ITEM_INNER_MARGIN = 3
def __init__(self, name, x, y, **kwargs):
"""Constructor for Menu"""
# if the user did not override these settings then we add our defaults
if "colour" not in kwargs:
kwargs["colour"] = C_BLACK
if "background_colour" not in kwargs:
kwargs["background_colour"] = C_MENUGRAY
if "show_border" not in kwargs:
kwargs["show_border"] = True
if "fill_style" not in kwargs:
kwargs["fill_style"] = FillStyle.Colour
super().__init__(
name=name,
x=x,
y=y,
w=2 * Menu.MENU_ITEM_INNER_MARGIN,
h=2 * Menu.MENU_ITEM_INNER_MARGIN,
**kwargs
)
self._items = {}
self._item_names = []
def add_item(self, mni: MenuItem):