pyforgeui 1.0.0__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.
- pyforgeui/Behavior/Element_behavior.py +42 -0
- pyforgeui/Behavior/Mouse_collision.py +108 -0
- pyforgeui/Behavior/Pressed_button.py +86 -0
- pyforgeui/Behavior/__init__.py +3 -0
- pyforgeui/Containers/Layout.py +156 -0
- pyforgeui/Containers/Modal.py +102 -0
- pyforgeui/Containers/Panel.py +123 -0
- pyforgeui/Containers/Tab_container.py +116 -0
- pyforgeui/Containers/__init__.py +4 -0
- pyforgeui/Elements/Check_box.py +101 -0
- pyforgeui/Elements/Combo_box.py +386 -0
- pyforgeui/Elements/Image_element.py +79 -0
- pyforgeui/Elements/Input_text.py +42 -0
- pyforgeui/Elements/Keybind_button.py +81 -0
- pyforgeui/Elements/Number_spinner.py +105 -0
- pyforgeui/Elements/Polygon_button.py +18 -0
- pyforgeui/Elements/Progress_bar.py +95 -0
- pyforgeui/Elements/Radio_button.py +128 -0
- pyforgeui/Elements/Scroll_bar.py +208 -0
- pyforgeui/Elements/Slider.py +146 -0
- pyforgeui/Elements/Text.py +25 -0
- pyforgeui/Elements/Text_button.py +13 -0
- pyforgeui/Elements/Toggle_switch.py +114 -0
- pyforgeui/Elements/Tooltip.py +83 -0
- pyforgeui/Elements/__init__.py +15 -0
- pyforgeui/Elements_Factory.py +120 -0
- pyforgeui/UI_Manager.py +104 -0
- pyforgeui/__init__.py +81 -0
- pyforgeui-1.0.0.dist-info/METADATA +150 -0
- pyforgeui-1.0.0.dist-info/RECORD +34 -0
- pyforgeui-1.0.0.dist-info/WHEEL +5 -0
- pyforgeui-1.0.0.dist-info/licenses/LICENSE +21 -0
- pyforgeui-1.0.0.dist-info/licenses/LICENSE.md +621 -0
- pyforgeui-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
from ..Behavior.Element_behavior import ElementBehavior
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class ScrollBar(ElementBehavior):
|
|
6
|
+
def __init__(self, config: dict):
|
|
7
|
+
super().__init__(config)
|
|
8
|
+
rect = pygame.Rect(*self.position)
|
|
9
|
+
self.type_of_orientation = config.get("type_of_orientation", "vertical").lower()
|
|
10
|
+
self.hover_color = config.get("hover_color", (255, 199, 51))
|
|
11
|
+
self.color = config.get("color", (200, 200, 200))
|
|
12
|
+
self.color_thumb = config.get("color_bar", (135, 206, 235))
|
|
13
|
+
self.wheel_step = config.get("wheel_step", 18)
|
|
14
|
+
self.scroll_area: pygame.Rect | None = config.get("scroll_area", None)
|
|
15
|
+
if self.type_of_orientation == "vertical":
|
|
16
|
+
self.thumb_size = config.get("thumb_height", max(20, int(self.position[3] * config.get("thumb_ratio", 0.2))))
|
|
17
|
+
self.thumb_rect = pygame.Rect(rect.x, rect.y, rect.width, self.thumb_size)
|
|
18
|
+
self.content_size = rect.height
|
|
19
|
+
else:
|
|
20
|
+
self.thumb_size = config.get("thumb_width", max(20, int(self.position[2] * config.get("thumb_ratio", 0.2))))
|
|
21
|
+
self.thumb_rect = pygame.Rect(rect.x, rect.y, self.thumb_size, rect.height)
|
|
22
|
+
self.content_size = rect.width
|
|
23
|
+
self.elements = None
|
|
24
|
+
self.initial_positions = []
|
|
25
|
+
self.dragging = False
|
|
26
|
+
self.drag_offset = 0
|
|
27
|
+
self.rect = {"rect": rect, "thumb": self.thumb_rect}
|
|
28
|
+
|
|
29
|
+
def move(self, dx: int, dy: int) -> None:
|
|
30
|
+
if dx == 0 and dy == 0:
|
|
31
|
+
return
|
|
32
|
+
super().move(dx, dy)
|
|
33
|
+
if isinstance(self.scroll_area, pygame.Rect):
|
|
34
|
+
self.scroll_area.move_ip(dx, dy)
|
|
35
|
+
if self.initial_positions:
|
|
36
|
+
self.initial_positions = [(x + dx, y + dy) for (x, y) in self.initial_positions]
|
|
37
|
+
|
|
38
|
+
def _get_wheel_hit_rect(self) -> pygame.Rect:
|
|
39
|
+
hit = self.rect["rect"].copy()
|
|
40
|
+
if isinstance(self.scroll_area, pygame.Rect):
|
|
41
|
+
hit.union_ip(self.scroll_area)
|
|
42
|
+
if self.elements and self.initial_positions:
|
|
43
|
+
min_x = min(x for x, _ in self.initial_positions)
|
|
44
|
+
min_y = min(y for _, y in self.initial_positions)
|
|
45
|
+
content_rect = pygame.Rect(min_x, min_y, max(hit.right - min_x, 100), max(hit.height, 40))
|
|
46
|
+
hit.union_ip(content_rect)
|
|
47
|
+
return hit
|
|
48
|
+
|
|
49
|
+
def _step_thumb(self, delta_pixels: int) -> None:
|
|
50
|
+
if self.type_of_orientation == "vertical":
|
|
51
|
+
new_pos = self.thumb_rect.y + delta_pixels
|
|
52
|
+
new_pos = max(self.rect["rect"].top, min(new_pos, self.rect["rect"].bottom - self.thumb_size))
|
|
53
|
+
self.thumb_rect.y = new_pos
|
|
54
|
+
else:
|
|
55
|
+
new_pos = self.thumb_rect.x + delta_pixels
|
|
56
|
+
new_pos = max(self.rect["rect"].left, min(new_pos, self.rect["rect"].right - self.thumb_size))
|
|
57
|
+
self.thumb_rect.x = new_pos
|
|
58
|
+
self.scroll_elements()
|
|
59
|
+
|
|
60
|
+
def events(self, event) -> bool:
|
|
61
|
+
if not self.visible or not self.enabled:
|
|
62
|
+
return False
|
|
63
|
+
if event.type == pygame.MOUSEWHEEL:
|
|
64
|
+
mouse_pos = self.get_mouse_pos()
|
|
65
|
+
if self._get_wheel_hit_rect().collidepoint(mouse_pos):
|
|
66
|
+
direction = -event.y if event.y != 0 else event.x
|
|
67
|
+
if direction != 0:
|
|
68
|
+
self._step_thumb(int(direction * self.wheel_step))
|
|
69
|
+
return True
|
|
70
|
+
elif event.type == pygame.MOUSEBUTTONDOWN:
|
|
71
|
+
if event.button in (4, 5):
|
|
72
|
+
if self._get_wheel_hit_rect().collidepoint(event.pos):
|
|
73
|
+
direction = -1 if event.button == 4 else 1
|
|
74
|
+
self._step_thumb(direction * self.wheel_step)
|
|
75
|
+
return True
|
|
76
|
+
elif event.button == 1:
|
|
77
|
+
if self.thumb_rect.collidepoint(event.pos):
|
|
78
|
+
self.dragging = True
|
|
79
|
+
self.drag_offset = (
|
|
80
|
+
(event.pos[1] - self.thumb_rect.y)
|
|
81
|
+
if self.type_of_orientation == "vertical"
|
|
82
|
+
else (event.pos[0] - self.thumb_rect.x)
|
|
83
|
+
)
|
|
84
|
+
return True
|
|
85
|
+
elif self.rect["rect"].collidepoint(event.pos):
|
|
86
|
+
if self.type_of_orientation == "vertical":
|
|
87
|
+
target = event.pos[1] - self.thumb_size // 2
|
|
88
|
+
self.thumb_rect.y = max(self.rect["rect"].top, min(target, self.rect["rect"].bottom - self.thumb_size))
|
|
89
|
+
else:
|
|
90
|
+
target = event.pos[0] - self.thumb_size // 2
|
|
91
|
+
self.thumb_rect.x = max(self.rect["rect"].left, min(target, self.rect["rect"].right - self.thumb_size))
|
|
92
|
+
self.scroll_elements()
|
|
93
|
+
return True
|
|
94
|
+
elif event.type == pygame.MOUSEBUTTONUP and event.button == 1:
|
|
95
|
+
if self.dragging:
|
|
96
|
+
self.dragging = False
|
|
97
|
+
return True
|
|
98
|
+
elif event.type == pygame.MOUSEMOTION and self.dragging:
|
|
99
|
+
if self.type_of_orientation == "vertical":
|
|
100
|
+
new_pos = event.pos[1] - self.drag_offset
|
|
101
|
+
new_pos = max(self.rect["rect"].top, min(new_pos, self.rect["rect"].bottom - self.thumb_size))
|
|
102
|
+
self.thumb_rect.y = new_pos
|
|
103
|
+
else:
|
|
104
|
+
new_pos = event.pos[0] - self.drag_offset
|
|
105
|
+
new_pos = max(self.rect["rect"].left, min(new_pos, self.rect["rect"].right - self.thumb_size))
|
|
106
|
+
self.thumb_rect.x = new_pos
|
|
107
|
+
self.scroll_elements()
|
|
108
|
+
return True
|
|
109
|
+
return False
|
|
110
|
+
|
|
111
|
+
def scroll_elements(self):
|
|
112
|
+
if not self.elements:
|
|
113
|
+
return
|
|
114
|
+
max_scroll = self.content_size
|
|
115
|
+
if max_scroll <= 0:
|
|
116
|
+
proportion = 0.0
|
|
117
|
+
else:
|
|
118
|
+
if self.type_of_orientation == "vertical":
|
|
119
|
+
denom = self.rect["rect"].height - self.thumb_size
|
|
120
|
+
proportion = (self.thumb_rect.y - self.rect["rect"].y) / denom if denom > 0 else 0.0
|
|
121
|
+
else:
|
|
122
|
+
denom = self.rect["rect"].width - self.thumb_size
|
|
123
|
+
proportion = (self.thumb_rect.x - self.rect["rect"].x) / denom if denom > 0 else 0.0
|
|
124
|
+
offset = int(proportion * max_scroll)
|
|
125
|
+
for el, (x0, y0) in zip(self.elements, self.initial_positions):
|
|
126
|
+
if self.type_of_orientation == "vertical":
|
|
127
|
+
old_pos = el.position[1]
|
|
128
|
+
new_pos = y0 - offset
|
|
129
|
+
el.position = (x0, new_pos)
|
|
130
|
+
else:
|
|
131
|
+
old_pos = el.position[0]
|
|
132
|
+
new_pos = x0 - offset
|
|
133
|
+
el.position = (new_pos, y0)
|
|
134
|
+
delta = new_pos - old_pos
|
|
135
|
+
|
|
136
|
+
def add_delta(item, d):
|
|
137
|
+
if isinstance(item, pygame.Rect):
|
|
138
|
+
if self.type_of_orientation == "vertical":
|
|
139
|
+
item.y += d
|
|
140
|
+
else:
|
|
141
|
+
item.x += d
|
|
142
|
+
elif isinstance(item, dict):
|
|
143
|
+
for v in item.values():
|
|
144
|
+
add_delta(v, d)
|
|
145
|
+
elif hasattr(item, "rect") and hasattr(item, "position"):
|
|
146
|
+
add_delta(item.rect, d)
|
|
147
|
+
if self.type_of_orientation == "vertical":
|
|
148
|
+
item.position = (item.position[0], item.position[1] + d)
|
|
149
|
+
else:
|
|
150
|
+
item.position = (item.position[0] + d, item.position[1])
|
|
151
|
+
|
|
152
|
+
if isinstance(el.rect, dict):
|
|
153
|
+
for key in el.rect:
|
|
154
|
+
add_delta(el.rect[key], delta)
|
|
155
|
+
else:
|
|
156
|
+
add_delta(el.rect, delta)
|
|
157
|
+
if hasattr(el, "scroll") and isinstance(el.scroll, ScrollBar) and el.scroll.elements:
|
|
158
|
+
el.scroll.initial_positions = [(sub_el.position[0], sub_el.position[1]) for sub_el in el.scroll.elements]
|
|
159
|
+
if callable(self.commands):
|
|
160
|
+
self.commands(proportion)
|
|
161
|
+
|
|
162
|
+
def draw(self):
|
|
163
|
+
pygame.draw.rect(self.screen, self.color, self.rect["rect"])
|
|
164
|
+
pygame.draw.rect(self.screen, self.color_thumb, self.thumb_rect)
|
|
165
|
+
if self.detect_mouse:
|
|
166
|
+
self.mouse_collision(self.thumb_rect, self.get_mouse_pos())
|
|
167
|
+
if self.pressed:
|
|
168
|
+
self.pressed_button(self.thumb_rect, pygame.mouse.get_pressed(), self.get_mouse_pos())
|
|
169
|
+
|
|
170
|
+
def draw_hover_effect(self):
|
|
171
|
+
return pygame.draw.rect(self.screen, self.hover_color, self.thumb_rect)
|
|
172
|
+
|
|
173
|
+
def update_elements(self, elements: list):
|
|
174
|
+
self.elements = elements
|
|
175
|
+
self.initial_positions = [(el.position[0], el.position[1]) for el in self.elements]
|
|
176
|
+
if self.elements:
|
|
177
|
+
if self.type_of_orientation == "vertical":
|
|
178
|
+
start_pos = min(y for _, y in self.initial_positions)
|
|
179
|
+
end_pos = self.return_rect()
|
|
180
|
+
visible_span = self.rect["rect"].height
|
|
181
|
+
else:
|
|
182
|
+
start_pos = min(x for x, _ in self.initial_positions)
|
|
183
|
+
end_pos = self.return_rect()
|
|
184
|
+
visible_span = self.rect["rect"].width
|
|
185
|
+
self.content_size = max(0, (end_pos - start_pos) - visible_span + 24)
|
|
186
|
+
else:
|
|
187
|
+
self.content_size = 0
|
|
188
|
+
|
|
189
|
+
def return_rect(self):
|
|
190
|
+
def get_bound(val):
|
|
191
|
+
if isinstance(val, pygame.Rect):
|
|
192
|
+
return val.bottom if self.type_of_orientation == "vertical" else val.right
|
|
193
|
+
elif isinstance(val, dict):
|
|
194
|
+
return max(get_bound(v) for v in val.values() if isinstance(v, (pygame.Rect, dict)))
|
|
195
|
+
elif hasattr(val, "rect"):
|
|
196
|
+
return get_bound(val.rect)
|
|
197
|
+
return 0
|
|
198
|
+
|
|
199
|
+
max_bound = 0
|
|
200
|
+
if self.elements:
|
|
201
|
+
for el in self.elements:
|
|
202
|
+
rect = getattr(el, "rect", None)
|
|
203
|
+
if isinstance(rect, dict):
|
|
204
|
+
for v in rect.values():
|
|
205
|
+
max_bound = max(max_bound, get_bound(v))
|
|
206
|
+
else:
|
|
207
|
+
max_bound = max(max_bound, get_bound(rect))
|
|
208
|
+
return max_bound
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
from ..Behavior.Element_behavior import ElementBehavior
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class Slider(ElementBehavior):
|
|
6
|
+
"""Numeric range slider supporting horizontal/vertical orientation, step snapping, and value display."""
|
|
7
|
+
|
|
8
|
+
def __init__(self, config: dict) -> None:
|
|
9
|
+
super().__init__(config)
|
|
10
|
+
self.font: pygame.font.Font = config.get("font", pygame.font.Font(None, 22))
|
|
11
|
+
self.orientation: str = config.get("orientation", config.get("type_of_orientation", "horizontal")).lower()
|
|
12
|
+
self.min_value: float = float(config.get("min_value", 0.0))
|
|
13
|
+
self.max_value: float = float(config.get("max_value", 100.0))
|
|
14
|
+
self.step: float = float(config.get("step", 0.0))
|
|
15
|
+
initial_val = float(config.get("value", self.min_value))
|
|
16
|
+
self.value: float = max(self.min_value, min(self.max_value, initial_val))
|
|
17
|
+
self.show_value: bool = config.get("show_value", True)
|
|
18
|
+
self.label: str = config.get("label", config.get("text", ""))
|
|
19
|
+
self.color: tuple = config.get("color", (255, 255, 255))
|
|
20
|
+
self.track_color: tuple = config.get("track_color", (65, 70, 82))
|
|
21
|
+
self.fill_color: tuple = config.get("fill_color", (85, 170, 255))
|
|
22
|
+
self.thumb_color: tuple = config.get("thumb_color", (240, 245, 255))
|
|
23
|
+
self.hover_color: tuple = config.get("hover_color", (255, 199, 51))
|
|
24
|
+
self.on_change = config.get("on_change", None)
|
|
25
|
+
self.dragging: bool = False
|
|
26
|
+
|
|
27
|
+
if len(self.position) >= 4:
|
|
28
|
+
self.rect = pygame.Rect(*self.position[:4])
|
|
29
|
+
else:
|
|
30
|
+
w, h = (180, 12) if self.orientation == "horizontal" else (12, 180)
|
|
31
|
+
self.rect = pygame.Rect(self.position[0], self.position[1], w, h)
|
|
32
|
+
|
|
33
|
+
self.thumb_radius: int = config.get("thumb_radius", max(8, (self.rect.height if self.orientation == "horizontal" else self.rect.width) // 2 + 4))
|
|
34
|
+
self._update_surfaces()
|
|
35
|
+
|
|
36
|
+
def _format_value(self) -> str:
|
|
37
|
+
if self.step >= 1 or (self.step == 0 and self.min_value.is_integer() and self.max_value.is_integer()):
|
|
38
|
+
val_str = str(int(round(self.value)))
|
|
39
|
+
else:
|
|
40
|
+
val_str = f"{self.value:.2f}"
|
|
41
|
+
return f"{self.label}: {val_str}" if self.label else val_str
|
|
42
|
+
|
|
43
|
+
def _update_surfaces(self) -> None:
|
|
44
|
+
txt = self._format_value() if (self.show_value or self.label) else ""
|
|
45
|
+
self._value_surface = self.font.render(txt, True, self.color) if txt else None
|
|
46
|
+
|
|
47
|
+
def get_ratio(self) -> float:
|
|
48
|
+
span = self.max_value - self.min_value
|
|
49
|
+
if span <= 0:
|
|
50
|
+
return 0.0
|
|
51
|
+
return max(0.0, min(1.0, (self.value - self.min_value) / span))
|
|
52
|
+
|
|
53
|
+
def _thumb_center(self) -> tuple[int, int]:
|
|
54
|
+
ratio = self.get_ratio()
|
|
55
|
+
if self.orientation == "horizontal":
|
|
56
|
+
cx = int(self.rect.left + ratio * self.rect.width)
|
|
57
|
+
cy = self.rect.centery
|
|
58
|
+
else:
|
|
59
|
+
cx = self.rect.centerx
|
|
60
|
+
cy = int(self.rect.bottom - ratio * self.rect.height)
|
|
61
|
+
return (cx, cy)
|
|
62
|
+
|
|
63
|
+
def get_thumb_rect(self) -> pygame.Rect:
|
|
64
|
+
cx, cy = self._thumb_center()
|
|
65
|
+
r = self.thumb_radius
|
|
66
|
+
return pygame.Rect(cx - r, cy - r, r * 2, r * 2)
|
|
67
|
+
|
|
68
|
+
def set_value(self, new_val: float, trigger_callback: bool = True) -> None:
|
|
69
|
+
clamped = max(self.min_value, min(self.max_value, float(new_val)))
|
|
70
|
+
if self.step > 0:
|
|
71
|
+
steps = round((clamped - self.min_value) / self.step)
|
|
72
|
+
clamped = max(self.min_value, min(self.max_value, self.min_value + steps * self.step))
|
|
73
|
+
if abs(clamped - self.value) > 1e-6:
|
|
74
|
+
self.value = clamped
|
|
75
|
+
self._update_surfaces()
|
|
76
|
+
if trigger_callback:
|
|
77
|
+
if callable(self.on_change):
|
|
78
|
+
self.on_change(self.value)
|
|
79
|
+
self.execute_commands(self.value)
|
|
80
|
+
|
|
81
|
+
def get_value(self) -> float:
|
|
82
|
+
return self.value
|
|
83
|
+
|
|
84
|
+
def _set_from_mouse(self, pos: tuple[int, int]) -> None:
|
|
85
|
+
if self.orientation == "horizontal":
|
|
86
|
+
ratio = (pos[0] - self.rect.left) / max(1, self.rect.width)
|
|
87
|
+
else:
|
|
88
|
+
ratio = (self.rect.bottom - pos[1]) / max(1, self.rect.height)
|
|
89
|
+
ratio = max(0.0, min(1.0, ratio))
|
|
90
|
+
raw_val = self.min_value + ratio * (self.max_value - self.min_value)
|
|
91
|
+
self.set_value(raw_val, trigger_callback=True)
|
|
92
|
+
|
|
93
|
+
def events(self, event: pygame.event.Event) -> bool:
|
|
94
|
+
if not self.visible or not self.enabled:
|
|
95
|
+
return False
|
|
96
|
+
hit_rect = self.rect.union(self.get_thumb_rect())
|
|
97
|
+
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
|
|
98
|
+
if hit_rect.collidepoint(event.pos):
|
|
99
|
+
self.dragging = True
|
|
100
|
+
if self.sound_touch:
|
|
101
|
+
self.sound_touch.play(loops=0)
|
|
102
|
+
self._set_from_mouse(event.pos)
|
|
103
|
+
return True
|
|
104
|
+
elif event.type == pygame.MOUSEBUTTONUP and event.button == 1:
|
|
105
|
+
if self.dragging:
|
|
106
|
+
self.dragging = False
|
|
107
|
+
return True
|
|
108
|
+
elif event.type == pygame.MOUSEMOTION and self.dragging:
|
|
109
|
+
self._set_from_mouse(event.pos)
|
|
110
|
+
return True
|
|
111
|
+
return False
|
|
112
|
+
|
|
113
|
+
def draw(self) -> None:
|
|
114
|
+
if not self.visible:
|
|
115
|
+
return
|
|
116
|
+
border_rad = min(self.rect.width, self.rect.height) // 2
|
|
117
|
+
pygame.draw.rect(self.screen, self.track_color, self.rect, border_radius=border_rad)
|
|
118
|
+
|
|
119
|
+
ratio = self.get_ratio()
|
|
120
|
+
if self.orientation == "horizontal":
|
|
121
|
+
fill_w = int(self.rect.width * ratio)
|
|
122
|
+
if fill_w > 0:
|
|
123
|
+
fill_rect = pygame.Rect(self.rect.x, self.rect.y, fill_w, self.rect.height)
|
|
124
|
+
pygame.draw.rect(self.screen, self.fill_color, fill_rect, border_radius=border_rad)
|
|
125
|
+
else:
|
|
126
|
+
fill_h = int(self.rect.height * ratio)
|
|
127
|
+
if fill_h > 0:
|
|
128
|
+
fill_rect = pygame.Rect(self.rect.x, self.rect.bottom - fill_h, self.rect.width, fill_h)
|
|
129
|
+
pygame.draw.rect(self.screen, self.fill_color, fill_rect, border_radius=border_rad)
|
|
130
|
+
|
|
131
|
+
cx, cy = self._thumb_center()
|
|
132
|
+
pygame.draw.circle(self.screen, self.thumb_color, (cx, cy), self.thumb_radius)
|
|
133
|
+
|
|
134
|
+
if self.detect_mouse:
|
|
135
|
+
self.mouse_collision(self.rect.union(self.get_thumb_rect()), self.get_mouse_pos(), self.draw_hover_effect)
|
|
136
|
+
|
|
137
|
+
if self._value_surface:
|
|
138
|
+
if self.orientation == "horizontal":
|
|
139
|
+
val_pos = (self.rect.x, self.rect.y - self._value_surface.get_height() - 6)
|
|
140
|
+
else:
|
|
141
|
+
val_pos = (self.rect.right + 10, self.rect.centery - self._value_surface.get_height() // 2)
|
|
142
|
+
self.screen.blit(self._value_surface, val_pos)
|
|
143
|
+
|
|
144
|
+
def draw_hover_effect(self) -> None:
|
|
145
|
+
cx, cy = self._thumb_center()
|
|
146
|
+
pygame.draw.circle(self.screen, self.hover_color, (cx, cy), self.thumb_radius, 2)
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
from ..Behavior.Mouse_collision import MouseCollision
|
|
3
|
+
class Text(MouseCollision):
|
|
4
|
+
def __init__(self,config:dict):
|
|
5
|
+
MouseCollision.__init__(self, config)
|
|
6
|
+
self.font = config.get("font", pygame.font.Font(None, 25))
|
|
7
|
+
self.text = config.get("text", "")
|
|
8
|
+
self.color = config.get("color", (255, 255, 255))
|
|
9
|
+
self.hover_color = config.get("hover_color", (255, 199, 51))
|
|
10
|
+
self._update_surfaces()
|
|
11
|
+
def _update_surfaces(self):
|
|
12
|
+
self._text_surface = self.font.render(self.text, True, self.color)
|
|
13
|
+
self._hover_surface = self.font.render(self.text, True, self.hover_color)
|
|
14
|
+
self.rect = pygame.Rect(*self.position, *self._text_surface.get_size())
|
|
15
|
+
def draw(self):
|
|
16
|
+
self.screen.blit(self._text_surface, self.position)
|
|
17
|
+
if self.detect_mouse:self.mouse_collision(self.rect,self.get_mouse_pos(),self.draw_hover_effect)
|
|
18
|
+
def draw_hover_effect(self):return self.screen.blit(self._hover_surface, self.position)
|
|
19
|
+
def change_item(self,config:dict):
|
|
20
|
+
self.position = config.get("position",self.position)
|
|
21
|
+
self.color=config.get("color",self.color)
|
|
22
|
+
self.hover_color=config.get("hover_color",self.hover_color)
|
|
23
|
+
self.text=config.get("text",self.text)
|
|
24
|
+
self.detect_mouse=config.get("detect_mouse",self.detect_mouse)
|
|
25
|
+
self._update_surfaces()
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
from ..Behavior.Element_behavior import ElementBehavior
|
|
3
|
+
from .Text import Text
|
|
4
|
+
class TextButton(Text,ElementBehavior):
|
|
5
|
+
def __init__(self,config:dict):
|
|
6
|
+
Text.__init__(self, config)
|
|
7
|
+
ElementBehavior.__init__(self, config)
|
|
8
|
+
def draw(self):
|
|
9
|
+
super().draw()
|
|
10
|
+
if self.pressed:self.pressed_button(self.rect,pygame.mouse.get_pressed(),self.get_mouse_pos())
|
|
11
|
+
def change_item(self,config:dict):
|
|
12
|
+
super().change_item(config)
|
|
13
|
+
self.pressed=config.get("pressed",self.pressed)
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
from ..Behavior.Element_behavior import ElementBehavior
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class ToggleSwitch(ElementBehavior):
|
|
6
|
+
"""Animated pill-shaped boolean toggle switch with optional label."""
|
|
7
|
+
|
|
8
|
+
def __init__(self, config: dict) -> None:
|
|
9
|
+
super().__init__(config)
|
|
10
|
+
self.font: pygame.font.Font = config.get("font", pygame.font.Font(None, 25))
|
|
11
|
+
self.text: str = config.get("text", "")
|
|
12
|
+
self.width: int = config.get("width", 48)
|
|
13
|
+
self.height: int = config.get("height", 24)
|
|
14
|
+
self.value: bool = config.get("value", config.get("checked", False))
|
|
15
|
+
self.color: tuple = config.get("color", (255, 255, 255))
|
|
16
|
+
self.off_color: tuple = config.get("off_color", (90, 95, 110))
|
|
17
|
+
self.on_color: tuple = config.get("on_color", (65, 195, 115))
|
|
18
|
+
self.knob_color: tuple = config.get("knob_color", (250, 250, 250))
|
|
19
|
+
self.hover_color: tuple = config.get("hover_color", (255, 199, 51))
|
|
20
|
+
self.spacing: int = config.get("spacing", 10)
|
|
21
|
+
self.on_change = config.get("on_change", None)
|
|
22
|
+
self._anim_progress: float = 1.0 if self.value else 0.0
|
|
23
|
+
self._update_surfaces()
|
|
24
|
+
|
|
25
|
+
def _update_surfaces(self) -> None:
|
|
26
|
+
x, y = int(self.position[0]), int(self.position[1])
|
|
27
|
+
self.track_rect = pygame.Rect(x, y, self.width, self.height)
|
|
28
|
+
self._label_surface = self.font.render(self.text, True, self.color) if self.text else None
|
|
29
|
+
self._label_hover_surface = self.font.render(self.text, True, self.hover_color) if self.text else None
|
|
30
|
+
if self._label_surface:
|
|
31
|
+
label_y = y + (self.height - self._label_surface.get_height()) // 2
|
|
32
|
+
self.label_pos = (x + self.width + self.spacing, label_y)
|
|
33
|
+
total_w = self.width + self.spacing + self._label_surface.get_width()
|
|
34
|
+
total_h = max(self.height, self._label_surface.get_height())
|
|
35
|
+
self.rect = pygame.Rect(x, min(y, label_y), total_w, total_h)
|
|
36
|
+
else:
|
|
37
|
+
self.label_pos = (x, y)
|
|
38
|
+
self.rect = self.track_rect.copy()
|
|
39
|
+
|
|
40
|
+
def move(self, dx: int, dy: int) -> None:
|
|
41
|
+
super().move(dx, dy)
|
|
42
|
+
self.track_rect.x += dx
|
|
43
|
+
self.track_rect.y += dy
|
|
44
|
+
self.label_pos = (self.label_pos[0] + dx, self.label_pos[1] + dy)
|
|
45
|
+
|
|
46
|
+
def toggle(self) -> bool:
|
|
47
|
+
self.set_value(not self.value)
|
|
48
|
+
return self.value
|
|
49
|
+
|
|
50
|
+
def set_value(self, value: bool) -> None:
|
|
51
|
+
new_val = bool(value)
|
|
52
|
+
if self.value != new_val:
|
|
53
|
+
self.value = new_val
|
|
54
|
+
if callable(self.on_change):
|
|
55
|
+
self.on_change(self.value)
|
|
56
|
+
self.execute_commands(self.value)
|
|
57
|
+
|
|
58
|
+
def get_value(self) -> bool:
|
|
59
|
+
return self.value
|
|
60
|
+
|
|
61
|
+
def events(self, event: pygame.event.Event) -> bool:
|
|
62
|
+
if not self.visible or not self.enabled:
|
|
63
|
+
return False
|
|
64
|
+
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
|
|
65
|
+
if self.rect.collidepoint(event.pos):
|
|
66
|
+
if self.sound_touch:
|
|
67
|
+
self.sound_touch.play(loops=0)
|
|
68
|
+
self.toggle()
|
|
69
|
+
self.states["presses_touch"] = False
|
|
70
|
+
return True
|
|
71
|
+
return False
|
|
72
|
+
|
|
73
|
+
def draw(self) -> None:
|
|
74
|
+
if not self.visible:
|
|
75
|
+
return
|
|
76
|
+
target = 1.0 if self.value else 0.0
|
|
77
|
+
if abs(self._anim_progress - target) > 0.01:
|
|
78
|
+
self._anim_progress += (target - self._anim_progress) * 0.25
|
|
79
|
+
else:
|
|
80
|
+
self._anim_progress = target
|
|
81
|
+
|
|
82
|
+
track_col = tuple(
|
|
83
|
+
int(self.off_color[i] + (self.on_color[i] - self.off_color[i]) * self._anim_progress)
|
|
84
|
+
for i in range(3)
|
|
85
|
+
)
|
|
86
|
+
radius = self.height // 2
|
|
87
|
+
pygame.draw.rect(self.screen, track_col, self.track_rect, border_radius=radius)
|
|
88
|
+
|
|
89
|
+
if self._label_surface:
|
|
90
|
+
self.screen.blit(self._label_surface, self.label_pos)
|
|
91
|
+
|
|
92
|
+
mouse_pos = self.get_mouse_pos()
|
|
93
|
+
if self.detect_mouse:
|
|
94
|
+
self.mouse_collision(self.rect, mouse_pos, self.draw_hover_effect)
|
|
95
|
+
|
|
96
|
+
pad = 3
|
|
97
|
+
knob_radius = radius - pad
|
|
98
|
+
min_cx = self.track_rect.left + radius
|
|
99
|
+
max_cx = self.track_rect.right - radius
|
|
100
|
+
cx = int(min_cx + (max_cx - min_cx) * self._anim_progress)
|
|
101
|
+
cy = self.track_rect.centery
|
|
102
|
+
pygame.draw.circle(self.screen, self.knob_color, (cx, cy), knob_radius)
|
|
103
|
+
|
|
104
|
+
def draw_hover_effect(self) -> None:
|
|
105
|
+
pygame.draw.rect(self.screen, self.hover_color, self.track_rect, 2, border_radius=self.height // 2)
|
|
106
|
+
if self._label_hover_surface:
|
|
107
|
+
self.screen.blit(self._label_hover_surface, self.label_pos)
|
|
108
|
+
|
|
109
|
+
def change_item(self, config: dict) -> None:
|
|
110
|
+
self.position = config.get("position", self.position)
|
|
111
|
+
self.text = config.get("text", self.text)
|
|
112
|
+
if "value" in config or "checked" in config:
|
|
113
|
+
self.value = bool(config.get("value", config.get("checked", self.value)))
|
|
114
|
+
self._update_surfaces()
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
from ..Behavior.Mouse_collision import MouseCollision
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class Tooltip(MouseCollision):
|
|
6
|
+
"""Floating tooltip widget that renders after a hover delay or on demand."""
|
|
7
|
+
|
|
8
|
+
def __init__(self, config: dict) -> None:
|
|
9
|
+
config.setdefault("position", (0, 0))
|
|
10
|
+
super().__init__(config)
|
|
11
|
+
self.font: pygame.font.Font = config.get("font", pygame.font.Font(None, 20))
|
|
12
|
+
self.text: str = config.get("text", "")
|
|
13
|
+
self.color: tuple = config.get("color", (255, 255, 255))
|
|
14
|
+
self.color_back: tuple = config.get("color_back", (35, 38, 45))
|
|
15
|
+
self.border_color: tuple = config.get("border_color", (255, 199, 51))
|
|
16
|
+
self.border: int = config.get("border", 1)
|
|
17
|
+
self.padding: int = config.get("padding", 6)
|
|
18
|
+
self.delay_ms: int = config.get("delay_ms", 350)
|
|
19
|
+
self.offset: tuple[int, int] = config.get("offset", (14, 18))
|
|
20
|
+
self.target = config.get("target", None)
|
|
21
|
+
self._hover_start_time: int | None = None
|
|
22
|
+
self._update_surfaces()
|
|
23
|
+
|
|
24
|
+
def _update_surfaces(self) -> None:
|
|
25
|
+
lines = self.text.split("\n") if self.text else [""]
|
|
26
|
+
self._line_surfaces = [self.font.render(line, True, self.color) for line in lines]
|
|
27
|
+
width = max((s.get_width() for s in self._line_surfaces), default=0) + self.padding * 2
|
|
28
|
+
height = sum(s.get_height() for s in self._line_surfaces) + self.padding * 2
|
|
29
|
+
self._box_surface = pygame.Surface((max(1, width), max(1, height)), pygame.SRCALPHA)
|
|
30
|
+
self._box_surface.fill((*self.color_back[:3], 240))
|
|
31
|
+
if self.border > 0:
|
|
32
|
+
pygame.draw.rect(self._box_surface, self.border_color, self._box_surface.get_rect(), self.border, border_radius=4)
|
|
33
|
+
y_cursor = self.padding
|
|
34
|
+
for surf in self._line_surfaces:
|
|
35
|
+
self._box_surface.blit(surf, (self.padding, y_cursor))
|
|
36
|
+
y_cursor += surf.get_height()
|
|
37
|
+
self.rect = pygame.Rect(self.position[0], self.position[1], width, height)
|
|
38
|
+
|
|
39
|
+
def set_text(self, text: str) -> None:
|
|
40
|
+
if text != self.text:
|
|
41
|
+
self.text = text
|
|
42
|
+
self._update_surfaces()
|
|
43
|
+
|
|
44
|
+
def change_item(self, config: dict) -> None:
|
|
45
|
+
self.text = config.get("text", self.text)
|
|
46
|
+
self.color = config.get("color", self.color)
|
|
47
|
+
self.color_back = config.get("color_back", self.color_back)
|
|
48
|
+
self.border_color = config.get("border_color", self.border_color)
|
|
49
|
+
self._update_surfaces()
|
|
50
|
+
|
|
51
|
+
def draw_at(self, mouse_pos: tuple[int, int], text: str | None = None) -> None:
|
|
52
|
+
if text is not None and text != self.text:
|
|
53
|
+
self.set_text(text)
|
|
54
|
+
if not self.text:
|
|
55
|
+
return
|
|
56
|
+
screen_w, screen_h = self.screen.get_size()
|
|
57
|
+
x = mouse_pos[0] + self.offset[0]
|
|
58
|
+
y = mouse_pos[1] + self.offset[1]
|
|
59
|
+
if x + self.rect.width > screen_w - 4:
|
|
60
|
+
x = max(4, mouse_pos[0] - self.rect.width - 8)
|
|
61
|
+
if y + self.rect.height > screen_h - 4:
|
|
62
|
+
y = max(4, mouse_pos[1] - self.rect.height - 8)
|
|
63
|
+
self.position = (x, y)
|
|
64
|
+
self.rect.topleft = self.position
|
|
65
|
+
self.screen.blit(self._box_surface, self.position)
|
|
66
|
+
|
|
67
|
+
def draw(self) -> None:
|
|
68
|
+
if not self.visible or not self.text:
|
|
69
|
+
return
|
|
70
|
+
mouse_pos = self.get_mouse_pos()
|
|
71
|
+
if self.target is not None:
|
|
72
|
+
bounds = self.target.get_bounding_rect() if hasattr(self.target, "get_bounding_rect") else getattr(self.target, "rect", None)
|
|
73
|
+
is_over = bounds.collidepoint(mouse_pos) if isinstance(bounds, pygame.Rect) else False
|
|
74
|
+
if is_over:
|
|
75
|
+
now = pygame.time.get_ticks()
|
|
76
|
+
if self._hover_start_time is None:
|
|
77
|
+
self._hover_start_time = now
|
|
78
|
+
elif now - self._hover_start_time >= self.delay_ms:
|
|
79
|
+
self.draw_at(mouse_pos)
|
|
80
|
+
else:
|
|
81
|
+
self._hover_start_time = None
|
|
82
|
+
else:
|
|
83
|
+
self.draw_at(mouse_pos)
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from .Text import *
|
|
2
|
+
from .Text_button import *
|
|
3
|
+
from .Polygon_button import *
|
|
4
|
+
from .Input_text import *
|
|
5
|
+
from .Scroll_bar import *
|
|
6
|
+
from .Combo_box import *
|
|
7
|
+
from .Check_box import *
|
|
8
|
+
from .Toggle_switch import *
|
|
9
|
+
from .Slider import *
|
|
10
|
+
from .Progress_bar import *
|
|
11
|
+
from .Image_element import *
|
|
12
|
+
from .Radio_button import *
|
|
13
|
+
from .Number_spinner import *
|
|
14
|
+
from .Keybind_button import *
|
|
15
|
+
from .Tooltip import *
|