e2D 1.4.19__py3-none-any.whl → 1.4.23__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- e2D/__init__.py +79 -29
- e2D/__init__.pyi +293 -5
- e2D/colors.py +53 -23
- e2D/envs.py +91 -35
- e2D/utils.py +443 -53
- e2D/winrec.py +197 -12
- {e2d-1.4.19.dist-info → e2d-1.4.23.dist-info}/METADATA +3 -2
- e2d-1.4.23.dist-info/RECORD +13 -0
- {e2d-1.4.19.dist-info → e2d-1.4.23.dist-info}/WHEEL +1 -1
- {e2d-1.4.19.dist-info → e2d-1.4.23.dist-info/licenses}/LICENSE +21 -21
- e2d-1.4.19.dist-info/RECORD +0 -13
- {e2d-1.4.19.dist-info → e2d-1.4.23.dist-info}/top_level.txt +0 -0
e2D/utils.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
from typing import Any, Callable, Literal
|
|
3
3
|
import pygame as pg
|
|
4
|
-
from e2D import *
|
|
5
|
-
from e2D.colors import *
|
|
4
|
+
from e2D import * # type: ignore
|
|
5
|
+
from e2D.colors import * # type: ignore
|
|
6
6
|
|
|
7
7
|
import math as _mt
|
|
8
8
|
|
|
@@ -18,6 +18,10 @@ def NEW_FONT(size, name:__LITERAL_FONTS__="arial", bold:bool=False, italic:bool=
|
|
|
18
18
|
FONT_ARIAL_16 = NEW_FONT(16)
|
|
19
19
|
FONT_ARIAL_32 = NEW_FONT(32)
|
|
20
20
|
FONT_ARIAL_64 = NEW_FONT(64)
|
|
21
|
+
FONT_MONOSPACE_16 = NEW_FONT(16, "cascadiamonoregular")
|
|
22
|
+
FONT_MONOSPACE_32 = NEW_FONT(32, "cascadiamonoregular")
|
|
23
|
+
FONT_MONOSPACE_64 = NEW_FONT(64, "cascadiamonoregular")
|
|
24
|
+
|
|
21
25
|
|
|
22
26
|
|
|
23
27
|
__LITERAL_PIVOT_POSITIONS__ = Literal["top_left", "top_center", "top_right", "center_left", "center_center", "center_right", "bottom_left", "bottom_center", "bottom_right"]
|
|
@@ -33,32 +37,51 @@ __PIVOT_POSITIONS_MULTIPLIER__ = dict(zip(("top_left", "top_center", "top_right"
|
|
|
33
37
|
class Mouse:
|
|
34
38
|
def __init__(self, parent) -> None:
|
|
35
39
|
self.parent = parent
|
|
36
|
-
self.
|
|
37
|
-
self.__last_frame_position__ = Vector2D.new_zero()
|
|
38
|
-
self.
|
|
39
|
-
self.__last_frame_movement__ = Vector2D.new_zero()
|
|
40
|
-
|
|
40
|
+
self.__last_frame_position_number__ :int= 0
|
|
41
|
+
self.__last_frame_position__ :Vector2D= Vector2D.new_zero()
|
|
42
|
+
self.__last_frame_movement_number__ :int= 0
|
|
43
|
+
self.__last_frame_movement__ :Vector2D= Vector2D.new_zero()
|
|
44
|
+
self.__last_frame_wheel_number__ :int= 0
|
|
45
|
+
self.__last_frame_wheel_delta__ :int= 0
|
|
46
|
+
|
|
41
47
|
self.__pressed__ : tuple[bool, bool, bool] = (False, False, False)
|
|
42
|
-
self.update()
|
|
48
|
+
self.update()
|
|
49
|
+
|
|
50
|
+
@property
|
|
51
|
+
def wheel_delta(self) -> int:
|
|
52
|
+
for event in self.parent.events:
|
|
53
|
+
if event.type == pg.MOUSEWHEEL:
|
|
54
|
+
if self.__last_frame_wheel_number__ != self.parent.current_frame:
|
|
55
|
+
self.__last_frame_wheel_delta__ = event.y
|
|
56
|
+
self.__last_frame_wheel_number__ = self.parent.current_frame
|
|
57
|
+
return self.__last_frame_wheel_delta__
|
|
58
|
+
|
|
59
|
+
self.__last_frame_wheel_delta__ = 0
|
|
60
|
+
self.__last_frame_wheel_number__ = self.parent.current_frame
|
|
61
|
+
return self.__last_frame_wheel_delta__
|
|
43
62
|
|
|
44
63
|
@property
|
|
45
64
|
def position(self) -> Vector2D:
|
|
46
|
-
if self.
|
|
65
|
+
if self.__last_frame_position_number__ != self.parent.current_frame:
|
|
47
66
|
self.__last_frame_position__ = Vector2D(*pg.mouse.get_pos())
|
|
48
|
-
self.
|
|
67
|
+
self.__last_frame_position_number__ = self.parent.current_frame
|
|
49
68
|
return self.__last_frame_position__
|
|
50
69
|
@position.setter
|
|
51
70
|
def position(self, new_position:Vector2D) -> None:
|
|
52
|
-
self.
|
|
71
|
+
self.__last_frame_position_number__ = self.parent.current_frame
|
|
53
72
|
self.__last_frame_position__ = new_position
|
|
54
73
|
pg.mouse.set_pos(self.__last_frame_position__())
|
|
55
74
|
|
|
56
75
|
@property
|
|
57
76
|
def last_frame_movement(self) -> Vector2D:
|
|
58
|
-
if self.
|
|
77
|
+
if self.__last_frame_movement_number__ != self.parent.current_frame:
|
|
59
78
|
self.__last_frame_movement__ = Vector2D(*pg.mouse.get_rel())
|
|
60
|
-
self.
|
|
79
|
+
self.__last_frame_movement_number__ = self.parent.current_frame
|
|
61
80
|
return self.__last_frame_movement__
|
|
81
|
+
@last_frame_movement.setter
|
|
82
|
+
def last_frame_movement(self, new_movement:Vector2D) -> None:
|
|
83
|
+
self.__last_frame_movement_number__ = self.parent.current_frame
|
|
84
|
+
self.__last_frame_movement__ = new_movement
|
|
62
85
|
|
|
63
86
|
def update(self) -> None:
|
|
64
87
|
self.__last_pressed__ = self.__pressed__
|
|
@@ -95,79 +118,115 @@ class Keyboard:
|
|
|
95
118
|
|
|
96
119
|
class Util:
|
|
97
120
|
def __init__(self) -> None:
|
|
98
|
-
self.rootEnv
|
|
99
|
-
self.surface : pg.Surface
|
|
121
|
+
self.rootEnv : Any
|
|
122
|
+
self.surface : pg.Surface= None # type: ignore
|
|
100
123
|
self.id : int|str
|
|
101
124
|
self.is_hovered :bool= False
|
|
102
|
-
|
|
103
|
-
def
|
|
125
|
+
self.hidden :bool= False
|
|
126
|
+
def hide(self) -> None:
|
|
127
|
+
self.hidden = True
|
|
128
|
+
def show(self) -> None:
|
|
129
|
+
self.hidden = False
|
|
130
|
+
def __render__(self) -> None: pass
|
|
131
|
+
def __draw__(self) -> None: pass
|
|
132
|
+
def __update__(self) -> None: pass
|
|
104
133
|
|
|
105
134
|
class InputCell(Util):
|
|
106
135
|
def __init__(self,
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
check_when_adding : Callable[[str], str] = lambda new_text: new_text
|
|
136
|
+
initial_value : str,
|
|
137
|
+
position : Vector2D,
|
|
138
|
+
size : Vector2D,
|
|
139
|
+
prefix : str|None = None,
|
|
140
|
+
text_color : Color|pg.Color = Color.white(),
|
|
141
|
+
bg_color : None|Color|pg.Color = None,
|
|
142
|
+
border_color : Color|pg.Color = Color.white(),
|
|
143
|
+
border_width : float = 0,
|
|
144
|
+
border_radius : int|list[int]|tuple[int,int,int,int] = -1,
|
|
145
|
+
margin : Vector2D = Vector2D.zero(),
|
|
146
|
+
pivot_position : __LITERAL_PIVOT_POSITIONS__ = "center_center",
|
|
147
|
+
font : pg.font.Font = FONT_ARIAL_32,
|
|
148
|
+
personalized_surface : pg.Surface|None = None,
|
|
149
|
+
on_enter_pressed : Callable[[str], Any] = lambda full_text: ...,
|
|
150
|
+
check_when_adding : Callable[[str], str] = lambda new_text: new_text,
|
|
123
151
|
) -> None:
|
|
124
152
|
super().__init__()
|
|
153
|
+
|
|
154
|
+
self.value = initial_value
|
|
125
155
|
|
|
126
|
-
self.id = id
|
|
127
|
-
self.on_enter_pressed = on_enter_pressed
|
|
128
|
-
self.check_when_adding = check_when_adding
|
|
129
|
-
self.prefix = prefix if prefix != None else ""
|
|
130
|
-
|
|
131
|
-
self.text_color = text_color
|
|
132
|
-
self.font = font
|
|
133
|
-
self.surface = personalized_surface
|
|
134
|
-
self.bg_color = bg_color
|
|
135
156
|
# size = Vector2D(*self.text_box.get_size()) + self.margin * 2
|
|
136
157
|
self.size = size
|
|
137
158
|
self.position = (position - size * __PIVOT_POSITIONS_MULTIPLIER__[pivot_position] + margin)
|
|
159
|
+
|
|
160
|
+
self.prefix = prefix if prefix != None else ""
|
|
161
|
+
|
|
162
|
+
self.font = font
|
|
163
|
+
|
|
138
164
|
self.bg_rect = [0, 0] + size()
|
|
139
|
-
|
|
140
|
-
self.border_color = border_color
|
|
165
|
+
|
|
141
166
|
self.border_radius = [border_radius]*4 if not any(isinstance(border_radius, cls) for cls in {tuple, list}) else border_radius
|
|
142
167
|
self.border_width = border_width
|
|
168
|
+
|
|
169
|
+
self.margin_rect = (margin * -1)() + size()
|
|
143
170
|
|
|
144
|
-
self.
|
|
171
|
+
self.on_enter_pressed = on_enter_pressed
|
|
172
|
+
self.check_when_adding = check_when_adding
|
|
173
|
+
|
|
145
174
|
self.update_text()
|
|
146
175
|
|
|
176
|
+
self.surface = personalized_surface
|
|
147
177
|
self.text_surface = pg.Surface(self.size(), pg.SRCALPHA, 32).convert_alpha()
|
|
178
|
+
|
|
179
|
+
self.text_color = text_color
|
|
180
|
+
self.bg_color = bg_color
|
|
181
|
+
self.border_color = border_color
|
|
182
|
+
|
|
183
|
+
@property
|
|
184
|
+
def text_color(self) -> Color:
|
|
185
|
+
return unpygamize_color(self.__text_color__)
|
|
186
|
+
@text_color.setter
|
|
187
|
+
def text_color(self, new_color:Color|pg.Color) -> None:
|
|
188
|
+
self.__text_color__ = pygamize_color(new_color)
|
|
189
|
+
@property
|
|
190
|
+
def bg_color(self) -> Color|None:
|
|
191
|
+
return unpygamize_color(self.__bg_color__) if self.__bg_color__ else None
|
|
192
|
+
@bg_color.setter
|
|
193
|
+
def bg_color(self, new_color:Color|pg.Color|None) -> None:
|
|
194
|
+
self.__bg_color__ = pygamize_color(new_color) if new_color else None
|
|
195
|
+
@property
|
|
196
|
+
def border_color(self) -> Color:
|
|
197
|
+
return unpygamize_color(self.__border_color__)
|
|
198
|
+
@border_color.setter
|
|
199
|
+
def border_color(self, new_color:Color|pg.Color) -> None:
|
|
200
|
+
self.__border_color__ = pygamize_color(new_color)
|
|
201
|
+
|
|
202
|
+
def __draw__(self) -> None:
|
|
203
|
+
if self.hidden: return
|
|
204
|
+
self.text_surface.fill(TRANSPARENT_COLOR_PYG)
|
|
148
205
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
if self.bg_color != None:
|
|
152
|
-
pg.draw.rect(self.text_surface, self.bg_color(), self.bg_rect, 0, -1, *self.border_radius)
|
|
206
|
+
if self.__bg_color__ is not None:
|
|
207
|
+
pg.draw.rect(self.text_surface, self.__bg_color__(), self.bg_rect, 0, -1, *self.border_radius)
|
|
153
208
|
|
|
154
209
|
self.text_surface.blit(self.text_box, self.text_position())
|
|
155
210
|
|
|
156
211
|
if self.rootEnv.selected_util != self:
|
|
157
212
|
if self.border_width:
|
|
158
|
-
pg.draw.rect(self.text_surface, self.
|
|
213
|
+
pg.draw.rect(self.text_surface, self.__border_color__(), self.margin_rect, self.border_width, -1, *self.border_radius)
|
|
159
214
|
else:
|
|
160
|
-
|
|
215
|
+
k = 127.5 + 127.5 * _mt.sin(self.rootEnv.runtime_seconds * 10)
|
|
216
|
+
pg.draw.rect(self.text_surface, pg.Color(k, k, k), self.margin_rect, self.border_width if self.border_width else 10, -1, *self.border_radius)
|
|
161
217
|
|
|
162
218
|
self.surface.blit(self.text_surface, self.position())
|
|
163
219
|
|
|
164
|
-
def
|
|
220
|
+
def __update__(self) -> None:
|
|
221
|
+
if self.hidden: return
|
|
165
222
|
self.is_hovered = self.position.x < self.rootEnv.mouse.position.x < self.position.x + self.size.x and\
|
|
166
223
|
self.position.y < self.rootEnv.mouse.position.y < self.position.y + self.size.y
|
|
224
|
+
|
|
167
225
|
if self.rootEnv.mouse.get_key(0, "just_pressed"):
|
|
168
226
|
if self.is_hovered:
|
|
169
227
|
self.rootEnv.selected_util = self if self.rootEnv.selected_util != self else None
|
|
170
228
|
self.update_text()
|
|
229
|
+
|
|
171
230
|
if self.rootEnv.selected_util == self:
|
|
172
231
|
for event in self.rootEnv.events:
|
|
173
232
|
if event.type == pg.TEXTINPUT:
|
|
@@ -183,9 +242,340 @@ class InputCell(Util):
|
|
|
183
242
|
self.update_text()
|
|
184
243
|
|
|
185
244
|
def update_text(self) -> None:
|
|
186
|
-
self.text_box = self.font.render(self.prefix + self.value, True, self.
|
|
245
|
+
self.text_box = self.font.render(self.prefix + self.value, True, self.__text_color__())
|
|
187
246
|
if self.rootEnv != None and self.rootEnv.selected_util == self:
|
|
188
247
|
# self.text_position = self.position + self.size * Vector2D(.85, .5) - Vector2D(*self.text_box.get_size()) * Vector2D(1, .5) - self.position
|
|
189
248
|
self.text_position = self.position + self.size * .5 - Vector2D(*self.text_box.get_size()) * Vector2D(.5, .5) - self.position
|
|
190
249
|
else:
|
|
191
250
|
self.text_position = self.position + self.size * .5 - Vector2D(*self.text_box.get_size()) * Vector2D(.5, .5) - self.position
|
|
251
|
+
|
|
252
|
+
class Slider(Util):
|
|
253
|
+
def __init__(self,
|
|
254
|
+
text : str,
|
|
255
|
+
position : Vector2D,
|
|
256
|
+
size : Vector2D,
|
|
257
|
+
min_value : float = 0,
|
|
258
|
+
max_value : float = 100,
|
|
259
|
+
step : float = 1,
|
|
260
|
+
color : Color|pg.Color = Color(200, 200, 200),
|
|
261
|
+
handleColour : Color|pg.Color = Color.white(),
|
|
262
|
+
initial_value : float = 0,
|
|
263
|
+
rounded : bool = True,
|
|
264
|
+
handleRadius : float = 10,
|
|
265
|
+
text_offset : V2 = V2(1.1, .5),
|
|
266
|
+
text_pivot : __LITERAL_PIVOT_POSITIONS__ = "center_center",
|
|
267
|
+
personalized_surface : pg.Surface|None = None,
|
|
268
|
+
) -> None:
|
|
269
|
+
super().__init__()
|
|
270
|
+
|
|
271
|
+
self.text = text
|
|
272
|
+
self.selected = False
|
|
273
|
+
self.min = min_value
|
|
274
|
+
self.max = max_value
|
|
275
|
+
self.step = step
|
|
276
|
+
|
|
277
|
+
self.position = position
|
|
278
|
+
self.size = size
|
|
279
|
+
|
|
280
|
+
self.value = clamp(initial_value, self.min, self.max)
|
|
281
|
+
|
|
282
|
+
self.radius = self.size.y // 2 if rounded else 0
|
|
283
|
+
self.text_offset = text_offset
|
|
284
|
+
self.text_pivot = text_pivot
|
|
285
|
+
|
|
286
|
+
self.handleRadius = handleRadius
|
|
287
|
+
self.surface = personalized_surface # type: ignore
|
|
288
|
+
|
|
289
|
+
self.hidden = False
|
|
290
|
+
|
|
291
|
+
self.color = color
|
|
292
|
+
self.handleColour = handleColour
|
|
293
|
+
|
|
294
|
+
@property
|
|
295
|
+
def color(self) -> Color:
|
|
296
|
+
return unpygamize_color(self.__color__)
|
|
297
|
+
@color.setter
|
|
298
|
+
def color(self, new_color:Color|pg.Color) -> None:
|
|
299
|
+
self.__color__ = pygamize_color(new_color)
|
|
300
|
+
@property
|
|
301
|
+
def handleColour(self) -> Color:
|
|
302
|
+
return unpygamize_color(self.__handleColour__)
|
|
303
|
+
@handleColour.setter
|
|
304
|
+
def handleColour(self, new_color:Color|pg.Color) -> None:
|
|
305
|
+
self.__handleColour__ = pygamize_color(new_color)
|
|
306
|
+
|
|
307
|
+
def __draw__(self) -> None:
|
|
308
|
+
if self.hidden: return
|
|
309
|
+
pg.draw.rect(self.surface, self.__color__, self.position() + self.size())
|
|
310
|
+
|
|
311
|
+
if self.radius:
|
|
312
|
+
pg.draw.circle(self.surface, self.__color__, (self.position.x, self.position.y + self.size.y // 2), self.radius)
|
|
313
|
+
pg.draw.circle(self.surface, self.__color__, (self.position.x + self.size.x, self.position.y + self.size.y // 2), self.radius)
|
|
314
|
+
|
|
315
|
+
circle = V2(int(self.position.x + (self.value - self.min) / (self.max - self.min) * self.size.x), self.position.y + self.size.y // 2)
|
|
316
|
+
|
|
317
|
+
pg.draw.circle(self.surface, self.__color__, circle(), self.handleRadius * 1.25)
|
|
318
|
+
pg.draw.circle(self.surface, self.__handleColour__, circle(), self.handleRadius)
|
|
319
|
+
self.rootEnv.print(self.text.format(round(self.value, 2)), self.position + self.size * self.text_offset, pivot_position=self.text_pivot)
|
|
320
|
+
|
|
321
|
+
def __update__(self) -> None:
|
|
322
|
+
if self.hidden: return
|
|
323
|
+
x,y = self.rootEnv.mouse.position
|
|
324
|
+
|
|
325
|
+
if self.rootEnv.mouse.get_key(0, "just_pressed") and self.__contains__(x, y):
|
|
326
|
+
self.rootEnv.selected_util = self
|
|
327
|
+
elif self.rootEnv.mouse.get_key(0, "just_released"):
|
|
328
|
+
self.rootEnv.selected_util = None
|
|
329
|
+
|
|
330
|
+
if self.rootEnv.selected_util == self:
|
|
331
|
+
new_value = (x - self.position.x) / self.size.x * self.max + self.min
|
|
332
|
+
self.value = clamp(new_value, self.min, self.max)
|
|
333
|
+
|
|
334
|
+
def __contains__(self, x, y) -> bool:
|
|
335
|
+
handleX = self.position.x + (self.value - self.min) / (self.max - self.min) * self.size.x
|
|
336
|
+
handleY = self.position.y + self.size.y // 2
|
|
337
|
+
return (handleX - x) ** 2 + (handleY - y) ** 2 <= self.handleRadius ** 2
|
|
338
|
+
|
|
339
|
+
def setValue(self, value) -> None:
|
|
340
|
+
self.value = clamp(value, self.min, self.max)
|
|
341
|
+
|
|
342
|
+
def getValue(self) -> float:
|
|
343
|
+
return self.value
|
|
344
|
+
|
|
345
|
+
class Button(Util):
|
|
346
|
+
def __init__(self,
|
|
347
|
+
text : str,
|
|
348
|
+
position : V2|Vector2D,
|
|
349
|
+
size : V2|Vector2D,
|
|
350
|
+
callback : Callable[[], None]|Callable[[], None] = lambda: None,
|
|
351
|
+
default_bg_color : Color|pg.Color = BLUE_COLOR_PYG,
|
|
352
|
+
hovered_bg_color : Color|pg.Color = CYAN_COLOR_PYG,
|
|
353
|
+
border_color : Color|pg.Color = WHITE_COLOR_PYG,
|
|
354
|
+
text_color : Color|pg.Color = WHITE_COLOR_PYG,
|
|
355
|
+
font : pg.font.Font = FONT_ARIAL_32,
|
|
356
|
+
border_radius : float = 10,
|
|
357
|
+
border_width : float = 10,
|
|
358
|
+
starting_hidden : bool = False,
|
|
359
|
+
args : list = [],
|
|
360
|
+
activation_mode : __LITERAL_KEY_MODE_TYPES__ = "just_pressed",
|
|
361
|
+
pivot_position : __LITERAL_PIVOT_POSITIONS__ = "top_left",
|
|
362
|
+
personalized_surface : pg.Surface|None = None,
|
|
363
|
+
) -> None:
|
|
364
|
+
super().__init__()
|
|
365
|
+
|
|
366
|
+
self.text = text
|
|
367
|
+
self.font = font
|
|
368
|
+
|
|
369
|
+
self.callback = callback
|
|
370
|
+
|
|
371
|
+
self.border_radius = border_radius
|
|
372
|
+
self.__size__ = size
|
|
373
|
+
self.__border_width__ = border_width
|
|
374
|
+
|
|
375
|
+
self.pivot_position :__LITERAL_PIVOT_POSITIONS__= pivot_position
|
|
376
|
+
self.update_position(position)
|
|
377
|
+
|
|
378
|
+
self.hidden = starting_hidden
|
|
379
|
+
self.args = args
|
|
380
|
+
|
|
381
|
+
self.activation_mode = activation_mode
|
|
382
|
+
|
|
383
|
+
self.hovered = False
|
|
384
|
+
|
|
385
|
+
self.text_color = text_color
|
|
386
|
+
self.default_bg_color = default_bg_color
|
|
387
|
+
self.border_color = border_color
|
|
388
|
+
self.hovered_bg_color = hovered_bg_color
|
|
389
|
+
|
|
390
|
+
self.surface = personalized_surface #type: ignore
|
|
391
|
+
self.update_surface()
|
|
392
|
+
|
|
393
|
+
def update_position(self, new_position:V2) -> None:
|
|
394
|
+
self.position = new_position - self.__size__ * __PIVOT_POSITIONS_MULTIPLIER__[self.pivot_position]
|
|
395
|
+
|
|
396
|
+
def update_pivoting(self, new_pivot_position:__LITERAL_PIVOT_POSITIONS__) -> None:
|
|
397
|
+
self.pivot_position = new_pivot_position
|
|
398
|
+
self.update_position(self.position)
|
|
399
|
+
|
|
400
|
+
def update_surface(self, render=False) -> None:
|
|
401
|
+
self.buffer_surface = pg.Surface((self.__size__ + self.__border_width__ * 2)(), pg.SRCALPHA, 32).convert_alpha()
|
|
402
|
+
if render: self.__render__()
|
|
403
|
+
|
|
404
|
+
@property
|
|
405
|
+
def size(self) -> V2:
|
|
406
|
+
return self.__size__
|
|
407
|
+
@size.setter
|
|
408
|
+
def size(self, new_size:V2|Vector2D) -> None:
|
|
409
|
+
self.__size__ = new_size
|
|
410
|
+
self.update_surface(render=True)
|
|
411
|
+
|
|
412
|
+
@property
|
|
413
|
+
def border_width(self) -> float:
|
|
414
|
+
return self.__border_width__
|
|
415
|
+
@border_width.setter
|
|
416
|
+
def border_width(self, new_width:float) -> None:
|
|
417
|
+
# self.position -= (self.__border_width__ - new_width) * .5
|
|
418
|
+
self.__border_width__ = new_width
|
|
419
|
+
self.update_surface(render=True)
|
|
420
|
+
|
|
421
|
+
@property
|
|
422
|
+
def text_color(self) -> Color:
|
|
423
|
+
return unpygamize_color(self.__text_color__)
|
|
424
|
+
@text_color.setter
|
|
425
|
+
def text_color(self, new_color:Color|pg.Color) -> None:
|
|
426
|
+
self.__text_color__ = pygamize_color(new_color)
|
|
427
|
+
@property
|
|
428
|
+
def default_bg_color(self) -> Color:
|
|
429
|
+
return unpygamize_color(self.__default_bg_color__)
|
|
430
|
+
@default_bg_color.setter
|
|
431
|
+
def default_bg_color(self, new_color:Color|pg.Color) -> None:
|
|
432
|
+
self.__default_bg_color__ = pygamize_color(new_color)
|
|
433
|
+
@property
|
|
434
|
+
def border_color(self) -> Color:
|
|
435
|
+
return unpygamize_color(self.__border_color__)
|
|
436
|
+
@border_color.setter
|
|
437
|
+
def border_color(self, new_color:Color|pg.Color) -> None:
|
|
438
|
+
self.__border_color__ = pygamize_color(new_color)
|
|
439
|
+
@property
|
|
440
|
+
def hovered_bg_color(self) -> Color:
|
|
441
|
+
return unpygamize_color(self.__hovered_bg_color__)
|
|
442
|
+
@hovered_bg_color.setter
|
|
443
|
+
def hovered_bg_color(self, new_color:Color|pg.Color) -> None:
|
|
444
|
+
self.__hovered_bg_color__ = pygamize_color(new_color)
|
|
445
|
+
|
|
446
|
+
def __render__(self) -> None:
|
|
447
|
+
self.buffer_surface.fill(TRANSPARENT_COLOR_PYG)
|
|
448
|
+
|
|
449
|
+
color = self.__hovered_bg_color__ if self.hovered else self.__default_bg_color__
|
|
450
|
+
pg.draw.rect(self.buffer_surface, self.__border_color__, V2.zero()() + (self.size + self.border_width * 2)(), border_radius=self.border_radius)
|
|
451
|
+
pg.draw.rect(self.buffer_surface, color, (V2.zero() + self.border_width)() + self.size(), border_radius=self.border_radius)
|
|
452
|
+
|
|
453
|
+
# TODO:
|
|
454
|
+
# not only size * .5 but also internal pivoting on the corners and sides
|
|
455
|
+
self.rootEnv.print(self.text, self.border_width + self.size * .5, color=self.__text_color__, font=self.font, pivot_position="center_center", personalized_surface=self.buffer_surface)
|
|
456
|
+
|
|
457
|
+
def __draw__(self) -> None:
|
|
458
|
+
if self.hidden: return
|
|
459
|
+
self.surface.blit(self.buffer_surface, (self.position)())
|
|
460
|
+
|
|
461
|
+
def __update__(self) -> None:
|
|
462
|
+
if self.hidden: return
|
|
463
|
+
|
|
464
|
+
old_overed = self.hovered
|
|
465
|
+
self.hovered = \
|
|
466
|
+
self.position.x < self.rootEnv.mouse.position.x < self.position.x + self.size.x and \
|
|
467
|
+
self.position.y < self.rootEnv.mouse.position.y < self.position.y + self.size.y
|
|
468
|
+
if self.hovered != old_overed:
|
|
469
|
+
self.__render__()
|
|
470
|
+
|
|
471
|
+
if self.hovered and self.rootEnv.mouse.get_key(0, self.activation_mode):
|
|
472
|
+
self.callback(*self.args)
|
|
473
|
+
self.rootEnv.selected_util = self
|
|
474
|
+
self.__render__()
|
|
475
|
+
elif self.rootEnv.selected_util == self:
|
|
476
|
+
self.rootEnv.selected_util = None
|
|
477
|
+
self.__render__()
|
|
478
|
+
|
|
479
|
+
class Label(Util):
|
|
480
|
+
def __init__(self,
|
|
481
|
+
text : str,
|
|
482
|
+
position : V2|Vector2D,
|
|
483
|
+
size : V2|Vector2D,
|
|
484
|
+
default_color : Color|pg.Color = TRANSPARENT_COLOR_PYG,
|
|
485
|
+
border_color : Color|pg.Color = WHITE_COLOR_PYG,
|
|
486
|
+
text_color : Color|pg.Color = WHITE_COLOR_PYG,
|
|
487
|
+
font : pg.font.Font = FONT_ARIAL_32,
|
|
488
|
+
border_radius : float = 10,
|
|
489
|
+
border_width : float = 10,
|
|
490
|
+
starting_hidden : bool = False,
|
|
491
|
+
personalized_surface : pg.Surface|None = None,
|
|
492
|
+
pivot_position : __LITERAL_PIVOT_POSITIONS__ = "top_left",
|
|
493
|
+
) -> None:
|
|
494
|
+
super().__init__()
|
|
495
|
+
|
|
496
|
+
self.__text__ = text
|
|
497
|
+
self.font = font
|
|
498
|
+
|
|
499
|
+
self.border_radius = border_radius
|
|
500
|
+
self.__size__ = size
|
|
501
|
+
self.__border_width__ = border_width
|
|
502
|
+
|
|
503
|
+
self.pivot_position :__LITERAL_PIVOT_POSITIONS__= pivot_position
|
|
504
|
+
self.update_position(position)
|
|
505
|
+
|
|
506
|
+
self.hidden = starting_hidden
|
|
507
|
+
|
|
508
|
+
self.text_color = text_color
|
|
509
|
+
self.default_color = default_color
|
|
510
|
+
self.border_color = border_color
|
|
511
|
+
|
|
512
|
+
self.surface = personalized_surface # type: ignore
|
|
513
|
+
self.update_surface()
|
|
514
|
+
|
|
515
|
+
def update_position(self, new_position:V2) -> None:
|
|
516
|
+
self.position = new_position - (self.__size__ + self.border_width * 2) * __PIVOT_POSITIONS_MULTIPLIER__[self.pivot_position]
|
|
517
|
+
|
|
518
|
+
def update_pivoting(self, new_pivot_position:__LITERAL_PIVOT_POSITIONS__="top_left") -> None:
|
|
519
|
+
self.pivot_position = new_pivot_position
|
|
520
|
+
self.update_position(self.position)
|
|
521
|
+
|
|
522
|
+
def update_surface(self, render=False) -> None:
|
|
523
|
+
self.buffer_surface = pg.Surface((self.__size__ + self.__border_width__ * 2)(), pg.SRCALPHA, 32).convert_alpha()
|
|
524
|
+
if render: self.__render__()
|
|
525
|
+
|
|
526
|
+
@property
|
|
527
|
+
def text(self) -> str:
|
|
528
|
+
return self.__text__
|
|
529
|
+
@text.setter
|
|
530
|
+
def text(self, new_text:str) -> None:
|
|
531
|
+
self.__text__ = new_text
|
|
532
|
+
self.__render__()
|
|
533
|
+
|
|
534
|
+
@property
|
|
535
|
+
def size(self) -> V2:
|
|
536
|
+
return self.__size__
|
|
537
|
+
@size.setter
|
|
538
|
+
def size(self, new_size:V2|Vector2D) -> None:
|
|
539
|
+
self.__size__ = new_size
|
|
540
|
+
self.update_surface(render=True)
|
|
541
|
+
|
|
542
|
+
@property
|
|
543
|
+
def border_width(self) -> float:
|
|
544
|
+
return self.__border_width__
|
|
545
|
+
@border_width.setter
|
|
546
|
+
def border_width(self, new_width:float) -> None:
|
|
547
|
+
self.__border_width__ = new_width
|
|
548
|
+
self.update_surface(render=True)
|
|
549
|
+
|
|
550
|
+
@property
|
|
551
|
+
def text_color(self) -> Color:
|
|
552
|
+
return unpygamize_color(self.__text_color__)
|
|
553
|
+
@text_color.setter
|
|
554
|
+
def text_color(self, new_color:Color|pg.Color) -> None:
|
|
555
|
+
self.__text_color__ = pygamize_color(new_color)
|
|
556
|
+
@property
|
|
557
|
+
def default_color(self) -> Color:
|
|
558
|
+
return unpygamize_color(self.__default_color__)
|
|
559
|
+
@default_color.setter
|
|
560
|
+
def default_color(self, new_color:Color|pg.Color) -> None:
|
|
561
|
+
self.__default_color__ = pygamize_color(new_color)
|
|
562
|
+
@property
|
|
563
|
+
def border_color(self) -> Color:
|
|
564
|
+
return unpygamize_color(self.__border_color__)
|
|
565
|
+
@border_color.setter
|
|
566
|
+
def border_color(self, new_color:Color|pg.Color) -> None:
|
|
567
|
+
self.__border_color__ = pygamize_color(new_color)
|
|
568
|
+
|
|
569
|
+
def __render__(self) -> None:
|
|
570
|
+
self.buffer_surface.fill(TRANSPARENT_COLOR_PYG)
|
|
571
|
+
|
|
572
|
+
pg.draw.rect(self.buffer_surface, self.__border_color__, V2.zero()() + (self.size + self.border_width * 2)(), border_radius=self.border_radius)
|
|
573
|
+
pg.draw.rect(self.buffer_surface, self.__default_color__, (V2.zero() + self.border_width)() + self.size(), border_radius=self.border_radius)
|
|
574
|
+
|
|
575
|
+
# TODO:
|
|
576
|
+
# not only size * .5 but also internal pivoting on the corners and sides
|
|
577
|
+
self.rootEnv.print(self.text, self.border_width + self.size * .5, color=self.__text_color__, font=self.font, pivot_position="center_center", personalized_surface=self.buffer_surface)
|
|
578
|
+
|
|
579
|
+
def __draw__(self) -> None:
|
|
580
|
+
if self.hidden: return
|
|
581
|
+
self.surface.blit(self.buffer_surface, (self.position)())
|