-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.py
2135 lines (1984 loc) · 89.5 KB
/
interface.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
from controls import controls
from vectors import Vector2D
import pygame
import config
from time import time as CurrentTime
from pyperclip import copy as CopyToClipboard
from pyperclip import paste as PasteFromClipboard
def CoordInBoundingBox(coord, bounding1, bounding2):
return ((bounding1.x <= coord.x <= bounding2.x) or \
(bounding2.x <= coord.x <= bounding1.x)) and \
((bounding1.y <= coord.y <= bounding2.y) or \
(bounding2.y <= coord.y <= bounding1.y))
def ScalePosition(lower_pos,
upper_pos,
positioning,
object_size=None,
scale_from=Vector2D(0.5, 0.5)):
new_x = (upper_pos.x - lower_pos.x) * positioning.x + lower_pos.x
new_y = (upper_pos.y - lower_pos.y) * positioning.y + lower_pos.y
if object_size is not None:
new_x -= scale_from.x * object_size.x
new_y -= scale_from.y * object_size.y
return Vector2D(new_x, new_y)
class BaseElement:
def __init__(self,
position,
is_active,
is_hidden,
tooltip_image=None,
tooltip_hover_time=2,
conditions=None):
self.image = None
if position is None:
self.position = Vector2D(0, 0)
else: # don't create a copy in case that's not what the user wants.
self.position = position
self.active = is_active
self.hidden = is_hidden
self.tooltip_image = tooltip_image
self.started_hovering = 0
self.tooltip_hover_time = tooltip_hover_time
self.previously_moved = False
self.hovering = False
self.conditions = conditions if conditions != None else []
def Update(self, shift=Vector2D(0, 0)):
if not self.active:
return False
for condition in self.conditions:
if not condition():
return False
if self.tooltip_image == None:
return
mouse_position = Vector2D(controls["mouse_position"]) - shift
relative = mouse_position - self.position
if (0 < relative.x < self.width) and (0 < relative.y < self.height):
if controls.MouseHasMoved or controls.MouseHasScrolled:
self.previously_moved = True
self.hovering = False
else:
if self.previously_moved:
self.started_hovering = CurrentTime()
elif (CurrentTime() -
self.started_hovering) > self.tooltip_hover_time:
self.hovering = True
self.previously_moved = False
def Draw(self, surface, shift=Vector2D(0, 0), draw_end=None):
if self.hidden:
return False
if self.active and draw_end != None and self.hovering and self.tooltip_image != None:
position = Vector2D(controls["mouse_position"]) + Vector2D(
3, 3) # offset so you can see the whole word
if isinstance(self.tooltip_image, BaseElement):
image = pygame.Surface(tuple(self.tooltip_image.size),
pygame.SRCALPHA, 32)
image.convert_alpha()
self.tooltip_image.Update(shift=shift)
self.tooltip_image.Draw(image)
else:
image = self.tooltip_image
tooltip_size = Vector2D(image.get_size())
if (position.x + tooltip_size.x) > config.settings["window_width"]:
position.x = config.settings["window_width"] - tooltip_size.x
if (position.y +
tooltip_size.y) > config.settings["window_height"]:
position.y = config.settings["window_height"] - tooltip_size.y
draw_end.append((image, tuple(position)))
def WithinBoundingBox(self, coord1, coord2):
return CoordInBoundingBox(self.position, coord1, coord2)
def AddConditions(self, *conditions):
self.conditions.append(conditions)
def RemoveCondition(self, condition):
if condition in self.conditions:
self.conditions.remove(condition)
def ClearConditions(self):
self.conditions = []
class ImageElement(BaseElement):
def __init__(self,
image,
position=None,
tooltip_info=[None, 2],
is_hidden=False):
super().__init__(position, False, is_hidden, *tooltip_info)
self.image = image
def Draw(self, surface, shift=Vector2D(0, 0), draw_end=None):
if super().Draw(surface, shift, draw_end) == False:
return False
surface.blit(self.image, tuple(self.position + shift))
class Rectangle(BaseElement):
def __init__(self,
size,
colour,
position=None,
is_active=True,
is_hidden=False):
super().__init__(position, is_active, is_hidden, None, 2)
self.size = size
self.width = self.size.x
self.height = self.size.y
self.colour = colour
self.UpdateImage()
def UpdateImage(self):
self.image = pygame.Surface(tuple(self.size), pygame.SRCALPHA, 32)
self.image.convert_alpha()
self.image.fill(self.colour)
def UpdateImageColour(self):
# a lighter version of UpdateImage that only changes the colour, not the size
self.image.fill(self.colour)
def Draw(self, surface, shift=Vector2D(0, 0), draw_end=None):
if super().Draw(surface, shift, draw_end) == False:
return False
surface.blit(self.image, tuple(self.position + shift))
class Label(BaseElement):
def __init__(self,
text,
font,
padding=Vector2D(0, 0),
outline_size=Vector2D(0, 0),
background_colour=(255, 255, 255, 0),
outline_colour=(0, 0, 0, 0),
text_colour=(0, 0, 0),
tooltip_info=[None, 2],
position=None,
is_active=True,
is_hidden=False):
super().__init__(position, is_active, is_hidden, *tooltip_info)
self.padding = padding
self.outline_size = outline_size
self.background_colour = background_colour
self.outline_colour = outline_colour
self.text_colour = text_colour
self.font = font
self.text = text # don't call UpdateImage(), text.setter calls that for us.
@property
def text(self):
return self._text
@text.setter
def text(self, new_text):
self._text = new_text
self.UpdateImage()
def UpdateImage(self):
image_size = Vector2D(self.font.size(
self.text)) + 2 * (self.padding + self.outline_size)
self.size = image_size
self.width = self.size.x
self.height = self.size.y
self.image = pygame.Surface(tuple(self.size), pygame.SRCALPHA, 32)
self.image.convert_alpha()
self.image.fill(self.outline_colour)
smaller_image = pygame.Surface(
tuple(self.size - 2 * self.outline_size), pygame.SRCALPHA, 32)
smaller_image.convert_alpha()
smaller_image.fill(self.background_colour)
label = self.font.render(self.text, 1, self.text_colour)
smaller_image.blit(label, tuple(self.padding))
self.image.blit(smaller_image, tuple(self.outline_size))
def Draw(self, surface, shift=Vector2D(0, 0), draw_end=None):
if super().Draw(surface, shift, draw_end) == False:
return False
surface.blit(self.image, tuple(self.position + shift))
def WithinBoundingBox(self, coord1, coord2):
corners = [
self.position, self.position + self.size,
self.position + Vector2D(self.width, 0),
self.position + Vector2D(0, self.height)
]
for corner in corners:
if CoordInBoundingBox(corner, coord1, coord2):
return True
return CoordInBoundingBox(coord1, corners[0],
corners[1]) # if 1 coord of bounding box
# is in the label, and it hasn't been detected yet, the bounding box is contained within
# the label
class AdvancedLabel(BaseElement):
def __init__(self,
text,
font,
size,
padding=Vector2D(0, 0),
outline_size=Vector2D(0, 0),
background_colour=(255, 255, 255, 0),
outline_colour=(0, 0, 0),
text_colour=(0, 0, 0),
position=None,
line_seperation=2,
auto_scroll_position=None,
tooltip_info=[None, 2],
is_active=True,
is_hidden=False):
super().__init__(position, is_active, is_hidden, *tooltip_info)
self.padding = padding
self.outline_size = outline_size
self.background_colour = background_colour
self.outline_colour = outline_colour
self.text_colour = text_colour
self.font = font
self.size = size
self.width = self.size.x
self.height = self.size.y
self.display_text = ['']
self.max_height = self.height
self.max_scroll = 0
self.current_scroll = 0
self.scroll_speed = config.settings["scroll_speed"] * 25
self.line_seperation = line_seperation
self.auto_scroll_position = auto_scroll_position
self.text = text # don't call UpdateImage(), text.setter calls that for us.
@property
def text(self):
return self._text
@text.setter
def text(self, new_text):
self._text = new_text
self.display_text = new_text.split("\n")
max_line_width = self.width - 2 * (self.padding.x +
self.outline_size.x)
new_display_text = []
for i in range(len(self.display_text)):
new_line = self.display_text[i]
if new_line == '': # i.e. an empty line that won't get caught
new_display_text.append(new_line)
while new_line != '':
text = new_line
new_line = ''
j = 0
while self.font.size(text)[0] > max_line_width:
new_line = text[-1] + new_line
text = text[:-1]
j += 1
new_display_text.append(text)
self.display_text = new_display_text
sizes = []
for line in self.display_text:
sizes.append(Vector2D(self.font.size(line)))
height = (len(sizes) - 1) * self.line_seperation
height += sum([size.y for size in sizes])
self.max_height = height + 2 * (self.padding.y + self.outline_size.y)
self.max_scroll = self.max_height - self.height
if self.auto_scroll_position != None:
self.current_scroll = self.max_scroll * self.auto_scroll_position
if self.current_scroll < 0:
self.current_scroll = 0
self.UpdateImage()
def UpdateImage(self):
self.image = pygame.Surface(tuple(self.size), pygame.SRCALPHA, 32)
self.image.convert_alpha()
self.image.fill(self.outline_colour)
smaller_image = pygame.Surface(
tuple(self.size - 2 * self.outline_size), pygame.SRCALPHA, 32)
smaller_image.convert_alpha()
smaller_image.fill(self.background_colour)
self.image.blit(smaller_image, tuple(self.outline_size))
added = Vector2D(0, 0)
for line in self.display_text:
position = self.padding + added - Vector2D(0, self.current_scroll)
if position.y >= self.height:
label = self.font.render(line, 1, self.text_colour)
self.image.blit(label, tuple(position))
break
elif position.y >= 0:
label = self.font.render(line, 1, self.text_colour)
self.image.blit(label, tuple(position))
else:
if position.y + self.font.size(line)[1] > 0:
label = self.font.render(line, 1, self.text_colour)
self.image.blit(label, tuple(position))
added.y += self.line_seperation + self.font.size(line)[1]
def Draw(self, surface, shift=Vector2D(0, 0), draw_end=None):
if super().Draw(surface, shift, draw_end) == False:
return False
surface.blit(self.image, tuple(self.position + shift))
def Update(self, shift=Vector2D(0, 0)):
if super().Update(shift) == False:
return False
for event in controls["events"]:
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 5: #scroll down
if self.current_scroll < self.max_scroll:
prev_scroll = self.current_scroll
self.current_scroll += self.scroll_speed
if self.current_scroll > self.max_scroll:
self.current_scroll = self.max_scroll
if self.current_scroll != prev_scroll:
self.UpdateImage()
elif event.button == 4: #scroll up
if self.current_scroll > 0:
prev_scroll = self.current_scroll
self.current_scroll -= self.scroll_speed
if self.current_scroll < 0:
self.current_scroll = 0
if self.current_scroll != prev_scroll:
self.UpdateImage()
def WithinBoundingBox(self, coord1, coord2):
corners = [
self.position, self.position + self.size,
self.position + Vector2D(self.width, 0),
self.position + Vector2D(0, self.height)
]
for corner in corners:
if CoordInBoundingBox(corner, coord1, coord2):
return True
return CoordInBoundingBox(coord1, corners[0],
corners[1]) # if 1 coord of bounding box
# is in the label, and it hasn't been detected yet, the bounding box is contained within
# the label
class CheckButton(BaseElement):
def __init__(self,
text,
font,
target=None,
arguments=None,
padding=Vector2D(5, 5),
outline_size=Vector2D(2, 2),
background_colour=(200, 200, 200),
pressed_background_colour=(150, 150, 150),
outline_colour=(0, 0, 0),
text_colour=(0, 0, 0),
top_left_padding=None,
tooltip_info=[None, 2],
position=None,
is_active=True,
is_hidden=False):
super().__init__(position, is_active, is_hidden, *tooltip_info)
self.pressed_image = None
self.unpressed_image = None
self.text = text
self.font = font
self.target = target
self.arguments = arguments
self.padding = padding
self.top_left_padding = top_left_padding
self.outline_size = outline_size
self.background_colour = background_colour
self.pressed_background_colour = pressed_background_colour
self.outline_colour = outline_colour
self.text_colour = text_colour
self.pressed = False
self.focused = False
self.UpdateImage()
def UpdateImage(self, update_size=True):
text_size = Vector2D(self.font.size(self.text))
if update_size:
self.size = text_size + (self.padding + self.outline_size) * 2
self.width = self.size.x
self.height = self.size.y
larger_image = pygame.Surface(tuple(self.size), pygame.SRCALPHA, 32)
larger_image.convert_alpha()
larger_image.fill(self.outline_colour)
self.pressed_image = larger_image.copy()
self.pressed_image.convert_alpha()
self.unpressed_image = larger_image.copy()
self.unpressed_image.convert_alpha()
image_size = self.size - self.outline_size * 2
smaller_pressed_image = pygame.Surface(tuple(image_size),
pygame.SRCALPHA, 32)
smaller_pressed_image.convert_alpha()
smaller_pressed_image.fill(self.pressed_background_colour)
smaller_unpressed_image = pygame.Surface(tuple(image_size),
pygame.SRCALPHA, 32)
smaller_unpressed_image.convert_alpha()
smaller_unpressed_image.fill(self.background_colour)
text_label = self.font.render(self.text, 1, self.text_colour)
smaller_pressed_image.blit(
text_label,
tuple(self.padding if self.top_left_padding is None else self.
top_left_padding))
smaller_unpressed_image.blit(
text_label,
tuple(self.padding if self.top_left_padding is None else self.
top_left_padding))
self.pressed_image.blit(smaller_pressed_image,
tuple(self.outline_size))
self.unpressed_image.blit(smaller_unpressed_image,
tuple(self.outline_size))
def __CallTarget(self):
if self.target != None:
if self.arguments == None:
self.target(self.pressed)
elif isinstance(self.arguments, (tuple, list, Vector2D)):
self.target(self.pressed, *self.arguments)
else:
self.target(self.pressed, self.arguments)
def _Press(self):
self.pressed = True
self.__CallTarget()
def _Unpress(self):
self.pressed = False
self.__CallTarget()
def Draw(self, surface, shift=Vector2D(0, 0), draw_end=None):
if super().Draw(surface, shift, draw_end) == False:
return False
image = self.pressed_image if self.pressed else self.unpressed_image
surface.blit(image, tuple(self.position + shift))
def Update(self, shift=Vector2D(0, 0)):
if super().Update(shift) == False:
return False
mouse_position = Vector2D(controls["mouse_position"]) - shift
relative = mouse_position - self.position
in_position = 0 < relative.x < self.width and 0 < relative.y < self.height
if controls.LeftClickDown:
if in_position:
self.focused = True
else:
self.focused = False
if controls.LeftClickRelease:
if in_position and self.focused:
if self.pressed:
self._Unpress()
else:
self._Press()
self.focused = False
def WithinBoundingBox(self, coord1, coord2):
corners = [
self.position, self.position + self.size,
self.position + Vector2D(self.width, 0),
self.position + Vector2D(0, self.height)
]
for corner in corners:
if CoordInBoundingBox(corner, coord1, coord2):
return True
return CoordInBoundingBox(coord1, corners[0],
corners[1]) # if 1 coord of bounding box
# is in the label, and it hasn't been detected yet, the bounding box is contained within
# the label
class CacheButton(CheckButton):
def __init__(self,
text,
font,
target=None,
arguments=None,
padding=Vector2D(5, 5),
outline_size=Vector2D(2, 2),
background_colour=(200, 200, 200),
pressed_background_colour=(150, 150, 150),
outline_colour=(0, 0, 0),
text_colour=(0, 0, 0),
top_left_padding=None,
tooltip_info=[None, 2],
position=None,
is_active=True,
is_hidden=False):
self.prev_pressed_image = None
self.prev_unpressed_image = None
self.use_prev_image = False
super().__init__(text, font, target, arguments, padding, outline_size,
background_colour, pressed_background_colour,
outline_colour, text_colour, top_left_padding,
tooltip_info, position, is_active, is_hidden)
def UpdateImage(
self
): # used so that the image is not misdrawn when run with threads
self.use_prev_image = True
self.prev_pressed_image = self.pressed_image
self.prev_unpressed_image = self.unpressed_image
super().UpdateImage()
self.use_prev_image = False
def Draw(self, surface, shift=Vector2D(0, 0), draw_end=None):
if BaseElement.Draw(self, surface, shift, draw_end) == False:
return False
if self.use_prev_image:
image = self.prev_pressed_image if self.pressed else self.prev_unpressed_image
else:
image = self.pressed_image if self.pressed else self.unpressed_image
surface.blit(image, tuple(self.position + shift))
class Button(CheckButton):
def __init__(self,
text,
font,
target=None,
arguments=None,
padding=Vector2D(5, 5),
outline_size=Vector2D(2, 2),
background_colour=(200, 200, 200),
pressed_background_colour=(150, 150, 150),
outline_colour=(0, 0, 0),
text_colour=(0, 0, 0),
press_time=0,
top_left_padding=None,
tooltip_info=[None, 2],
position=None,
is_active=True,
is_hidden=False):
super().__init__(text, font, target, arguments, padding, outline_size,
background_colour, pressed_background_colour,
outline_colour, text_colour, top_left_padding,
tooltip_info, position, is_active, is_hidden)
self.arguments = arguments
self.press_time = press_time
self.time_of_press = 0
def __CallTarget(self):
if self.target != None:
if self.arguments == None:
self.target()
elif isinstance(self.arguments, (tuple, list, Vector2D)):
self.target(*self.arguments)
else:
self.target(self.arguments)
def _Press(self):
self.pressed = True
self.time_of_press = CurrentTime()
self.__CallTarget()
def _Unpress(self):
self.pressed = False
def Update(self, shift=Vector2D(0, 0)):
BaseElement.Update(self, shift)
mouse_position = Vector2D(controls["mouse_position"]) - shift
if (CurrentTime() -
self.time_of_press) >= self.press_time and self.pressed:
self._Unpress()
relative = mouse_position - self.position
in_position = 0 < relative.x < self.width and 0 < relative.y < self.height
if controls.LeftClickDown:
if in_position:
self.focused = True
else:
self.focused = False
if controls.LeftClickRelease:
if in_position and self.focused and not self.pressed:
self._Press()
self.focused = False
class HoverButton(Button):
def __init__(self,
text,
font,
target=None,
arguments=None,
padding=Vector2D(5, 5),
outline_size=Vector2D(2, 2),
background_colour=(200, 200, 200),
pressed_background_colour=(150, 150, 150),
outline_colour=(0, 0, 0),
text_colour=(0, 0, 0),
hover_background_colour=(200, 200, 200),
hover_outline_colour=(255, 255, 255),
hover_text_colour=(255, 255, 255),
press_time=0,
top_left_padding=None,
tooltip_info=[None, 2],
position=None,
is_active=True,
is_hidden=False):
self.hover_background_colour = hover_background_colour
self.hover_outline_colour = hover_outline_colour
self.hover_text_colour = hover_text_colour
self.hovering = False
super().__init__(text, font, target, arguments, padding, outline_size,
background_colour, pressed_background_colour,
outline_colour, text_colour, press_time,
top_left_padding, tooltip_info, position, is_active,
is_hidden)
def UpdateImage(self, update_size=True):
super().UpdateImage(update_size)
self.hover_image = pygame.Surface(tuple(self.size), pygame.SRCALPHA,
32)
self.hover_image.convert_alpha()
self.hover_image.fill(self.hover_outline_colour)
image_size = self.size - self.outline_size * 2
smaller_hover_image = pygame.Surface(tuple(image_size),
pygame.SRCALPHA, 32)
smaller_hover_image.convert_alpha()
smaller_hover_image.fill(self.hover_background_colour)
text_label = self.font.render(self.text, 1, self.hover_text_colour)
smaller_hover_image.blit(
text_label,
tuple(self.padding if self.top_left_padding is None else self.
top_left_padding))
self.hover_image.blit(smaller_hover_image, tuple(self.outline_size))
def Draw(self, surface, shift=Vector2D(0, 0), draw_end=None):
if BaseElement.Draw(self, surface, shift, draw_end) == False:
return False
if self.pressed:
image = self.pressed_image
elif self.hovering:
image = self.hover_image
else:
image = self.unpressed_image
surface.blit(image, tuple(self.position + shift))
def Update(self, shift=Vector2D(0, 0), needs_release=True):
BaseElement.Update(self, shift)
mouse_position = Vector2D(controls["mouse_position"]) - shift
if (CurrentTime() -
self.time_of_press) >= self.press_time and self.pressed:
self._Unpress()
relative = mouse_position - self.position
in_position = 0 < relative.x < self.width and 0 < relative.y < self.height
if controls.LeftClickDown:
if in_position:
if not needs_release and not self.pressed:
self._Press()
else:
self.focused = True
else:
self.focused = False
if controls.LeftClickRelease:
if in_position and self.focused and not self.pressed:
self._Press()
self.focused = False
self.hovering = in_position
class FixedHoverButton(HoverButton):
def __init__(self,
text,
font,
size,
target=None,
arguments=None,
padding=Vector2D(5, 5),
outline_size=Vector2D(2, 2),
background_colour=(200, 200, 200),
pressed_background_colour=(150, 150, 150),
outline_colour=(0, 0, 0),
text_colour=(0, 0, 0),
hover_background_colour=(200, 200, 200),
hover_outline_colour=(255, 255, 255),
hover_text_colour=(255, 255, 255),
press_time=0,
top_left_padding=None,
tooltip_info=[None, 2],
position=None,
is_active=True,
is_hidden=False):
self.size = size
self.width = size.x
self.height = size.y
super().__init__(text, font, target, arguments, padding, outline_size,
background_colour, pressed_background_colour,
outline_colour, text_colour, hover_background_colour,
hover_outline_colour, hover_text_colour, press_time,
top_left_padding, tooltip_info, position, is_active,
is_hidden)
def UpdateImage(self):
super().UpdateImage(update_size=False)
self.hover_image = pygame.Surface(tuple(self.size), pygame.SRCALPHA,
32)
self.hover_image.convert_alpha()
self.hover_image.fill(self.hover_outline_colour)
image_size = self.size - self.outline_size * 2
smaller_hover_image = pygame.Surface(tuple(image_size),
pygame.SRCALPHA, 32)
smaller_hover_image.convert_alpha()
smaller_hover_image.fill(self.hover_background_colour)
text_label = self.font.render(self.text, 1, self.hover_text_colour)
smaller_hover_image.blit(
text_label,
tuple(self.padding if self.top_left_padding is None else self.
top_left_padding))
self.hover_image.blit(smaller_hover_image, tuple(self.outline_size))
def Draw(self,
surface,
shift=Vector2D(0, 0),
draw_end=None,
force_hover=None):
if force_hover != None:
prev_hover = self.hovering
self.hovering = force_hover
result = super().Draw(surface, shift=shift, draw_end=None)
self.hovering = prev_hover
return result
return super().Draw(surface, shift=shift, draw_end=draw_end)
def InvertColour(r, g, b):
return (255 - r, 255 - g, 255 - b)
class Entry(BaseElement):
def __init__(self,
font,
width,
initial_key_time=0.4,
key_time=0.04,
padding=Vector2D(5, 5),
outline_size=Vector2D(2, 2),
initial_text="",
empty_text="",
hide_text=False,
text_colour=(0, 0, 0),
empty_text_colour=(140, 140, 140),
background_colour=(200, 200, 200),
outline_colour=(0, 0, 0),
history_range=500,
tooltip_info=[None, 2],
position=None,
is_active=True,
is_hidden=False):
super().__init__(position, is_active, is_hidden, *tooltip_info)
self.history_range = history_range
self.history = [None] * self.history_range
self.future = [None] * self.history_range
self.text = initial_text
self.max_text_width = width - (outline_size.x + padding.x) * 2
self.display_text = ''
self.display_index = 0
self.font = font
self.width = width
self.font_height = self.font.size("ABCDEFGHIJKLMNOPQRSTUVWXYZ")[
1] # TODO check if still needed?
self.height = self.font_height + (padding.y + outline_size.y) * 2
self.size = Vector2D(self.width, self.height)
self.hide_text = hide_text
self.cursor_index = 0
self.UpdateText()
self.font = font
self.background_colour = background_colour
self.outline_colour = outline_colour
self.text_colour = text_colour
self.padding = padding
self.outline_size = outline_size
self.image = None
self.UpdateImage()
self.is_focused = False
self.initial_key_time = initial_key_time
self.key_time = key_time
self.time_of_key_press = 0
self.key_count = 0
self.time_of_cursor = 0
self.cursor_enabled = True
self.drag_select_index = 0
self.current_key_name = None
self.current_key_unicode = None
self.shift_pressed = 0
self.ctrl_pressed = 0
self.made_selection_replacement = False
self.time_of_last_scroll = 0
self.scroll_time = 0.05
self.empty_text = empty_text
self.empty_text_colour = empty_text_colour
def ClearHistoryAndFuture(self):
self.history = [None] * self.history_range
self.future = [None] * self.history_range
def UpdateTextHistory(self):
# wipe future, move histories back, add text to history
for i in range(self.history_range - 1):
self.history[i] = self.history[i + 1]
self.history[self.history_range - 1] = self.text
self.future = [None] * self.history_range
def Undo(self):
# move all futures forward by 1, add text to future,
# add last history to text, move all histories forward by 1
if self.history[self.history_range - 1] == None:
return
for i in range(self.history_range - 1):
self.future[self.history_range - 1 -
i] = self.future[self.history_range - 2 - i]
self.future[0] = self.text
self.text = self.history[self.history_range - 1]
for i in range(self.history_range - 1):
self.history[self.history_range - 1 -
i] = self.history[self.history_range - 2 - i]
self.history[0] = None
self.drag_select_index = None
self.UpdateText()
def Redo(self):
# move all histories backward by 1, last history is text,
# set text to first future, move all futures backward by 1
if self.future[0] == None:
return
for i in range(self.history_range - 1):
self.history[i] = self.history[i + 1]
self.history[self.history_range - 1] = self.text
self.text = self.future[0]
for i in range(self.history_range - 1):
self.future[i] = self.future[i + 1]
self.future[self.history_range - 1] = None
self.drag_select_index = None
self.UpdateText()
def UpdateText(self, text=None):
if text is not None:
self.text = text
if self.hide_text:
display_text = "*" * len(self.text)
else:
display_text = self.text
if self.cursor_index < self.display_index:
self.display_index = self.cursor_index
if self.cursor_index > 0 and self.made_selection_replacement:
self.made_selection_replacement = False
self.display_index -= 1
self.display_text = ''
if self.font.size(
display_text[self.display_index:])[0] <= self.max_text_width:
self.display_text = display_text[self.display_index:]
return
i = len(display_text) - 1
while self.font.size(display_text[self.display_index:i +
1])[0] > self.max_text_width:
i -= 1
self.display_text = display_text[self.display_index:i + 1]
if self.cursor_index > i:
i = self.cursor_index
while self.font.size(display_text[i:self.cursor_index +
1])[0] < self.max_text_width:
i -= 1
i += 1
self.display_index = i
self.display_text = display_text[i:self.cursor_index + 1]
def UpdateImage(self):
if self.outline_colour == 'TRANSPARENT' or self.background_colour == 'TRANSPARENT':
self.image = pygame.Surface(tuple(self.size), pygame.SRCALPHA, 32)
self.image.convert_alpha()
if self.outline_colour != 'TRANSPARENT':
vertical = pygame.Surface((self.outline_size.x, self.height))
vertical.fill(self.outline_colour)
horizontal = pygame.Surface((self.width, self.outline_size.y))
horizontal.fill(self.outline_colour)
self.image.blit(vertical, [0, 0])
self.image.blit(vertical,
[self.width - self.outline_size.x, 0])
self.image.blit(horizontal, [0, 0])
self.image.blit(horizontal,
[0, self.height - self.outline_size.y])
else:
self.image = pygame.Surface(tuple(self.size))
self.image.fill(self.outline_colour)
if self.background_colour == 'TRANSPARENT':
background_image = pygame.Surface(
tuple(self.size - self.outline_size * 2), pygame.SRCALPHA, 32)
background_image.convert_alpha()
else:
background_image = pygame.Surface(
tuple(self.size - self.outline_size * 2))
background_image.fill(self.background_colour)
self.image.blit(background_image, tuple(self.outline_size))
self.cursor_image = pygame.Surface((2, int(self.font_height / 10 * 7)))
self.cursor_image.fill(self.text_colour)
def Draw(self, surface, shift=Vector2D(0, 0), draw_end=None):
if super().Draw(surface, shift, draw_end) == False:
return False
surface.blit(self.image, tuple(self.position + shift))
label_padding = self.outline_size + self.padding + shift
if self.text == '':
label = self.font.render(self.empty_text, 1,
self.empty_text_colour)
else:
label = self.font.render(self.display_text, 1, self.text_colour)
surface.blit(label, tuple(self.position + label_padding))
if self.is_focused:
if self.drag_select_index != None and self.drag_select_index != self.cursor_index:
if self.cursor_index > self.drag_select_index:
min_index = self.drag_select_index
max_index = self.cursor_index - 1
else:
min_index = self.cursor_index
max_index = self.drag_select_index - 1
min_index -= self.display_index
max_index -= self.display_index
if min_index < 0:
min_index = 0
upper_index = len(self.display_text)
if max_index > upper_index:
max_index = upper_index
start_pos = self.font.size(self.display_text[:min_index])[0]
end_pos = self.font.size(self.display_text[:max_index + 1])[0]
# not exactly the same as per subtraction therefore necessary.
highlight_size = self.font.size(
self.display_text[min_index:max_index + 1])[0]
highlight = pygame.Surface(
(highlight_size, self.font_height - self.padding.y))
highlight.fill((0, 120, 215))
surface.blit(
highlight,
tuple(self.position + label_padding +
Vector2D(start_pos, 0)))
h_label = self.font.render(
self.display_text[min_index:max_index + 1], 1,
InvertColour(*self.text_colour))
surface.blit(
h_label,
tuple(self.position + label_padding +
Vector2D(end_pos - highlight_size, 0)))
if self.cursor_enabled:
target_index = self.cursor_index - self.display_index
if target_index > 0:
cursor_position = self.font.size(
self.display_text[:target_index])[0]
else:
cursor_position = 0
cursor_position = label_padding + Vector2D(
cursor_position, self.font_height // 20 * 3)
surface.blit(self.cursor_image,
tuple(self.position + cursor_position))
if (CurrentTime() - self.time_of_cursor) > 0.5:
self.cursor_enabled = not self.cursor_enabled
self.time_of_cursor = CurrentTime()
def GetCurrentCursorIndex(self, relative_position):
relative_position -= self.outline_size
relative_position -= self.padding
for i in range(1, len(self.display_text) + 1):
text_width = self.font.size(self.display_text[:i])[0]
if text_width > relative_position.x:
return i - 1 + self.display_index
return None
def findNextBreak(self, index, direction):
text_len = len(self.text)
while text_len > index > 0:
if self.text[index] in [' ', '-', '_', '.', ',']:
return index
index += direction
return index
def DoKeyPress(self, name, unicode):
if name == "escape":
self.is_focused = False
elif name == "backspace":
if self.drag_select_index != None:
if controls["mouse_pressed"][0]:
return
self.UpdateTextHistory()