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,42 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
from pygame.locals import *
|
|
3
|
+
from .Mouse_collision import MouseCollision
|
|
4
|
+
from .Pressed_button import PressedButton
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class ElementBehavior(MouseCollision, PressedButton):
|
|
8
|
+
"""Unified base behavior for all interactive PyForgeUI widgets."""
|
|
9
|
+
|
|
10
|
+
_timer_initialized = False
|
|
11
|
+
|
|
12
|
+
def __init__(self, config: dict) -> None:
|
|
13
|
+
MouseCollision.__init__(self, config)
|
|
14
|
+
PressedButton.__init__(self, config)
|
|
15
|
+
self.new_events(time=config.get("time", 500))
|
|
16
|
+
|
|
17
|
+
def events(self, event: pygame.event.Event) -> bool:
|
|
18
|
+
"""Process a Pygame event. Returns True if the event was consumed by this widget."""
|
|
19
|
+
return False
|
|
20
|
+
|
|
21
|
+
def reactivate_pressed(self, event: pygame.event.Event) -> None:
|
|
22
|
+
if event.type == self.EVENT_NEW:
|
|
23
|
+
self.states["presses_touch"] = True
|
|
24
|
+
|
|
25
|
+
def new_events(self, time: int) -> None:
|
|
26
|
+
self.EVENT_NEW = pygame.USEREVENT + self.define_event()
|
|
27
|
+
if not ElementBehavior._timer_initialized or time != 500:
|
|
28
|
+
pygame.time.set_timer(self.EVENT_NEW, time)
|
|
29
|
+
ElementBehavior._timer_initialized = True
|
|
30
|
+
|
|
31
|
+
def define_event(self) -> int:
|
|
32
|
+
return 1
|
|
33
|
+
|
|
34
|
+
def filter_rects_collision(self, rects: dict, mouse_pos: tuple[int, int], draws: list, option: bool = False) -> None:
|
|
35
|
+
for rect, draw in zip(rects, draws):
|
|
36
|
+
if rects[rect].collidepoint(mouse_pos):
|
|
37
|
+
if option is True:
|
|
38
|
+
self.pressed_button(rects[rect], pygame.mouse.get_pressed(), mouse_pos, draw)
|
|
39
|
+
else:
|
|
40
|
+
self.mouse_collision(rects[rect], mouse_pos, draw)
|
|
41
|
+
if all(not rects[r].collidepoint(mouse_pos) for r in rects):
|
|
42
|
+
self.states["detect_hover"], self.states["presses_touch"] = True, True
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class MouseCollision:
|
|
5
|
+
"""Mixin/Base providing hover detection, coordinate translation, and spatial geometry."""
|
|
6
|
+
|
|
7
|
+
def __init__(self, config: dict) -> None:
|
|
8
|
+
self.screen: pygame.Surface = config["screen"]
|
|
9
|
+
self.window = config.get("window", None)
|
|
10
|
+
pos = config.get("position", (0, 0))
|
|
11
|
+
self.position: tuple = tuple(pos) if isinstance(pos, (tuple, list)) else pos
|
|
12
|
+
self.sound_hover = config.get("sound_hover", None)
|
|
13
|
+
self.detect_mouse: bool = config.get("detect_mouse", True)
|
|
14
|
+
self.visible: bool = config.get("visible", True)
|
|
15
|
+
self.enabled: bool = config.get("enabled", True)
|
|
16
|
+
self.tooltip_text: str | None = config.get("tooltip", None)
|
|
17
|
+
if not hasattr(self, "states") or not isinstance(self.states, dict):
|
|
18
|
+
self.states = {}
|
|
19
|
+
self.states.update({"detect_hover": True, "is_hovered": False, **config.get("states", {})})
|
|
20
|
+
|
|
21
|
+
def get_mouse_pos(self) -> tuple[int, int]:
|
|
22
|
+
return self.window.get_mouse_pos() if self.window else pygame.mouse.get_pos()
|
|
23
|
+
|
|
24
|
+
def get_bounding_rect(self) -> pygame.Rect:
|
|
25
|
+
"""Returns a single bounding pygame.Rect for layout and container calculations."""
|
|
26
|
+
rect_attr = getattr(self, "rect", None)
|
|
27
|
+
if isinstance(rect_attr, pygame.Rect):
|
|
28
|
+
return rect_attr
|
|
29
|
+
if isinstance(rect_attr, dict):
|
|
30
|
+
rects = []
|
|
31
|
+
for v in rect_attr.values():
|
|
32
|
+
if isinstance(v, pygame.Rect):
|
|
33
|
+
rects.append(v)
|
|
34
|
+
elif hasattr(v, "get_bounding_rect"):
|
|
35
|
+
rects.append(v.get_bounding_rect())
|
|
36
|
+
elif hasattr(v, "rect") and isinstance(v.rect, pygame.Rect):
|
|
37
|
+
rects.append(v.rect)
|
|
38
|
+
if rects:
|
|
39
|
+
base = rects[0].copy()
|
|
40
|
+
for r in rects[1:]:
|
|
41
|
+
base.union_ip(r)
|
|
42
|
+
return base
|
|
43
|
+
if isinstance(self.position, (tuple, list)):
|
|
44
|
+
if len(self.position) >= 4 and isinstance(self.position[0], (int, float)):
|
|
45
|
+
return pygame.Rect(int(self.position[0]), int(self.position[1]), int(self.position[2]), int(self.position[3]))
|
|
46
|
+
if len(self.position) == 2 and isinstance(self.position[0], (int, float)):
|
|
47
|
+
return pygame.Rect(int(self.position[0]), int(self.position[1]), 0, 0)
|
|
48
|
+
return pygame.Rect(0, 0, 0, 0)
|
|
49
|
+
|
|
50
|
+
def move(self, dx: int, dy: int) -> None:
|
|
51
|
+
"""Translates this element and any nested rects/children by (dx, dy)."""
|
|
52
|
+
if dx == 0 and dy == 0:
|
|
53
|
+
return
|
|
54
|
+
if isinstance(self.position, (tuple, list)) and self.position:
|
|
55
|
+
if isinstance(self.position[0], (tuple, list)):
|
|
56
|
+
# Polygon points
|
|
57
|
+
self.position = [(p[0] + dx, p[1] + dy) for p in self.position]
|
|
58
|
+
if hasattr(self, "hover_position") and isinstance(self.hover_position, (tuple, list)):
|
|
59
|
+
self.hover_position = [(p[0] + dx, p[1] + dy) for p in self.hover_position]
|
|
60
|
+
elif len(self.position) == 2:
|
|
61
|
+
self.position = (self.position[0] + dx, self.position[1] + dy)
|
|
62
|
+
elif len(self.position) >= 4:
|
|
63
|
+
self.position = (self.position[0] + dx, self.position[1] + dy, *self.position[2:])
|
|
64
|
+
|
|
65
|
+
self._translate_rect_obj(getattr(self, "rect", None), dx, dy)
|
|
66
|
+
|
|
67
|
+
@classmethod
|
|
68
|
+
def _translate_rect_obj(cls, obj, dx: int, dy: int) -> None:
|
|
69
|
+
if isinstance(obj, pygame.Rect):
|
|
70
|
+
obj.x += dx
|
|
71
|
+
obj.y += dy
|
|
72
|
+
elif isinstance(obj, dict):
|
|
73
|
+
for v in obj.values():
|
|
74
|
+
if isinstance(v, (pygame.Rect, dict)):
|
|
75
|
+
cls._translate_rect_obj(v, dx, dy)
|
|
76
|
+
elif hasattr(v, "move"):
|
|
77
|
+
v.move(dx, dy)
|
|
78
|
+
|
|
79
|
+
def set_position(self, x: int, y: int) -> None:
|
|
80
|
+
"""Moves the element so its top-left corner is at (x, y)."""
|
|
81
|
+
if isinstance(self.position, (tuple, list)) and self.position and isinstance(self.position[0], (int, float)):
|
|
82
|
+
dx = int(x - self.position[0])
|
|
83
|
+
dy = int(y - self.position[1])
|
|
84
|
+
else:
|
|
85
|
+
bounds = self.get_bounding_rect()
|
|
86
|
+
dx = int(x - bounds.x)
|
|
87
|
+
dy = int(y - bounds.y)
|
|
88
|
+
self.move(dx, dy)
|
|
89
|
+
|
|
90
|
+
def mouse_collision(self, rect: pygame.Rect, mouse_pos: tuple[int, int], draw=None) -> bool:
|
|
91
|
+
if not self.visible or not self.enabled:
|
|
92
|
+
self.states["is_hovered"] = False
|
|
93
|
+
return False
|
|
94
|
+
if rect.collidepoint(mouse_pos):
|
|
95
|
+
self.states["is_hovered"] = True
|
|
96
|
+
self.draw_hover_effect() if draw is None else draw()
|
|
97
|
+
if self.states["detect_hover"]:
|
|
98
|
+
if self.sound_hover:
|
|
99
|
+
self.sound_hover.play(loops=0)
|
|
100
|
+
self.states["detect_hover"] = False
|
|
101
|
+
return True
|
|
102
|
+
else:
|
|
103
|
+
self.states["is_hovered"] = False
|
|
104
|
+
self.states["detect_hover"] = True
|
|
105
|
+
return False
|
|
106
|
+
|
|
107
|
+
def draw_hover_effect(self):
|
|
108
|
+
raise NotImplementedError
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class PressedButton:
|
|
5
|
+
"""Mixin providing click, release, hold-repeat, and command callback execution."""
|
|
6
|
+
|
|
7
|
+
def __init__(self, config: dict) -> None:
|
|
8
|
+
self.sound_touch = config.get("sound_touch", None)
|
|
9
|
+
self.pressed: bool = config.get("pressed", True)
|
|
10
|
+
if not hasattr(self, "states") or not isinstance(self.states, dict):
|
|
11
|
+
self.states = {}
|
|
12
|
+
self.states.update({
|
|
13
|
+
"presses_touch": True,
|
|
14
|
+
"click_time": None,
|
|
15
|
+
"active": False,
|
|
16
|
+
**config.get("states", {}),
|
|
17
|
+
})
|
|
18
|
+
self.repeat: bool = config.get("repeat_button", False)
|
|
19
|
+
self.repeat_interval: int = config.get("repeat_interval", 200)
|
|
20
|
+
num_cmds = config.get("number_commands", 4)
|
|
21
|
+
self.commands: list = [config.get(f"command{i}") for i in range(1, num_cmds)]
|
|
22
|
+
if callable(config.get("command")):
|
|
23
|
+
self.commands.insert(0, config.get("command"))
|
|
24
|
+
|
|
25
|
+
def pressed_button(self, rect: pygame.Rect, pressed_mouse: tuple, mouse_pos: tuple[int, int], draw=None, repeat: bool | None = None) -> bool:
|
|
26
|
+
if not getattr(self, "visible", True) or not getattr(self, "enabled", True):
|
|
27
|
+
return False
|
|
28
|
+
|
|
29
|
+
fired = False
|
|
30
|
+
|
|
31
|
+
def execute(sound=False, active=False, presses=True, time=None, command=True):
|
|
32
|
+
nonlocal fired
|
|
33
|
+
if sound and self.sound_touch:
|
|
34
|
+
self.sound_touch.play(loops=0)
|
|
35
|
+
self.states["active"] = active
|
|
36
|
+
self.states["presses_touch"] = presses
|
|
37
|
+
self.states["click_time"] = None if time is None else time
|
|
38
|
+
if command:
|
|
39
|
+
fired = True
|
|
40
|
+
self.execute_commands()
|
|
41
|
+
|
|
42
|
+
repeat = self.repeat if repeat is None else repeat
|
|
43
|
+
current_time = pygame.time.get_ticks()
|
|
44
|
+
|
|
45
|
+
if pressed_mouse[0] and rect.collidepoint(mouse_pos) and self.states["presses_touch"]:
|
|
46
|
+
execute(False, True, False, current_time, False)
|
|
47
|
+
|
|
48
|
+
if not repeat and not pressed_mouse[0] and self.states["active"]:
|
|
49
|
+
if rect.collidepoint(mouse_pos):
|
|
50
|
+
execute(True)
|
|
51
|
+
else:
|
|
52
|
+
self.states["active"] = False
|
|
53
|
+
self.states["presses_touch"] = True
|
|
54
|
+
elif self.states["click_time"] is not None and repeat:
|
|
55
|
+
if pressed_mouse[0] and rect.collidepoint(mouse_pos):
|
|
56
|
+
if current_time - self.states["click_time"] >= self.repeat_interval:
|
|
57
|
+
execute(True, active=True, presses=False, time=current_time, command=True)
|
|
58
|
+
else:
|
|
59
|
+
self.states["active"] = False
|
|
60
|
+
self.states["presses_touch"] = True
|
|
61
|
+
self.states["click_time"] = None
|
|
62
|
+
|
|
63
|
+
if pressed_mouse[0] and not rect.collidepoint(mouse_pos) and self.states["active"]:
|
|
64
|
+
self.states["active"], self.states["presses_touch"] = False, True
|
|
65
|
+
|
|
66
|
+
if self.states["active"]:
|
|
67
|
+
self.draw_pressed_effect() if draw is None else draw()
|
|
68
|
+
|
|
69
|
+
return fired
|
|
70
|
+
|
|
71
|
+
def draw_pressed_effect(self):
|
|
72
|
+
return NotImplementedError
|
|
73
|
+
|
|
74
|
+
def execute_commands(self, *args) -> None:
|
|
75
|
+
if callable(self.commands):
|
|
76
|
+
try:
|
|
77
|
+
self.commands(*args)
|
|
78
|
+
except TypeError:
|
|
79
|
+
self.commands()
|
|
80
|
+
return
|
|
81
|
+
for command in self.commands:
|
|
82
|
+
if callable(command):
|
|
83
|
+
try:
|
|
84
|
+
command(*args)
|
|
85
|
+
except TypeError:
|
|
86
|
+
command()
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
from ..Behavior.Mouse_collision import MouseCollision
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class StackLayout(MouseCollision):
|
|
6
|
+
"""Automatic vertical or horizontal box layout manager."""
|
|
7
|
+
|
|
8
|
+
def __init__(self, config: dict) -> None:
|
|
9
|
+
super().__init__(config)
|
|
10
|
+
self.orientation: str = config.get("orientation", "vertical").lower()
|
|
11
|
+
self.spacing: int = config.get("spacing", 10)
|
|
12
|
+
self.padding: int = config.get("padding", 8)
|
|
13
|
+
self.children: list = []
|
|
14
|
+
self.rect = pygame.Rect(int(self.position[0]), int(self.position[1]), 0, 0)
|
|
15
|
+
for child in config.get("children", []):
|
|
16
|
+
self.add_child(child)
|
|
17
|
+
|
|
18
|
+
def add_child(self, child):
|
|
19
|
+
if child not in self.children:
|
|
20
|
+
self.children.append(child)
|
|
21
|
+
self.reflow()
|
|
22
|
+
return child
|
|
23
|
+
|
|
24
|
+
def remove_child(self, child) -> None:
|
|
25
|
+
if child in self.children:
|
|
26
|
+
self.children.remove(child)
|
|
27
|
+
self.reflow()
|
|
28
|
+
|
|
29
|
+
def reflow(self) -> None:
|
|
30
|
+
x_start = int(self.position[0]) + self.padding
|
|
31
|
+
y_start = int(self.position[1]) + self.padding
|
|
32
|
+
cx, cy = x_start, y_start
|
|
33
|
+
max_w, max_h = 0, 0
|
|
34
|
+
|
|
35
|
+
for child in self.children:
|
|
36
|
+
if not getattr(child, "visible", True):
|
|
37
|
+
continue
|
|
38
|
+
if hasattr(child, "set_position"):
|
|
39
|
+
child.set_position(cx, cy)
|
|
40
|
+
bounds = child.get_bounding_rect() if hasattr(child, "get_bounding_rect") else getattr(child, "rect", pygame.Rect(cx, cy, 0, 0))
|
|
41
|
+
if self.orientation == "vertical":
|
|
42
|
+
cy += bounds.height + self.spacing
|
|
43
|
+
max_w = max(max_w, bounds.width)
|
|
44
|
+
max_h = cy - y_start - self.spacing
|
|
45
|
+
else:
|
|
46
|
+
cx += bounds.width + self.spacing
|
|
47
|
+
max_h = max(max_h, bounds.height)
|
|
48
|
+
max_w = cx - x_start - self.spacing
|
|
49
|
+
|
|
50
|
+
self.rect = pygame.Rect(
|
|
51
|
+
int(self.position[0]),
|
|
52
|
+
int(self.position[1]),
|
|
53
|
+
max(0, max_w + self.padding * 2),
|
|
54
|
+
max(0, max_h + self.padding * 2),
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
def move(self, dx: int, dy: int) -> None:
|
|
58
|
+
if dx == 0 and dy == 0:
|
|
59
|
+
return
|
|
60
|
+
super().move(dx, dy)
|
|
61
|
+
for child in self.children:
|
|
62
|
+
if hasattr(child, "move"):
|
|
63
|
+
child.move(dx, dy)
|
|
64
|
+
|
|
65
|
+
def events(self, event: pygame.event.Event) -> bool:
|
|
66
|
+
if not self.visible or not self.enabled:
|
|
67
|
+
return False
|
|
68
|
+
for child in self.children:
|
|
69
|
+
if hasattr(child, "events") and child.events(event):
|
|
70
|
+
return True
|
|
71
|
+
return False
|
|
72
|
+
|
|
73
|
+
def draw(self) -> None:
|
|
74
|
+
if not self.visible:
|
|
75
|
+
return
|
|
76
|
+
for child in self.children:
|
|
77
|
+
if hasattr(child, "draw"):
|
|
78
|
+
child.draw()
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class GridLayout(MouseCollision):
|
|
82
|
+
"""Automatic 2D grid layout manager for inventory slots, skill trees, and button matrices."""
|
|
83
|
+
|
|
84
|
+
def __init__(self, config: dict) -> None:
|
|
85
|
+
super().__init__(config)
|
|
86
|
+
self.columns: int = max(1, int(config.get("columns", 3)))
|
|
87
|
+
self.cell_size: tuple[int, int] | None = config.get("cell_size", None)
|
|
88
|
+
self.spacing_x: int = config.get("spacing_x", config.get("spacing", 10))
|
|
89
|
+
self.spacing_y: int = config.get("spacing_y", config.get("spacing", 10))
|
|
90
|
+
self.padding: int = config.get("padding", 8)
|
|
91
|
+
self.children: list = []
|
|
92
|
+
self.rect = pygame.Rect(int(self.position[0]), int(self.position[1]), 0, 0)
|
|
93
|
+
for child in config.get("children", []):
|
|
94
|
+
self.add_child(child)
|
|
95
|
+
|
|
96
|
+
def add_child(self, child):
|
|
97
|
+
if child not in self.children:
|
|
98
|
+
self.children.append(child)
|
|
99
|
+
self.reflow()
|
|
100
|
+
return child
|
|
101
|
+
|
|
102
|
+
def remove_child(self, child) -> None:
|
|
103
|
+
if child in self.children:
|
|
104
|
+
self.children.remove(child)
|
|
105
|
+
self.reflow()
|
|
106
|
+
|
|
107
|
+
def reflow(self) -> None:
|
|
108
|
+
x0 = int(self.position[0]) + self.padding
|
|
109
|
+
y0 = int(self.position[1]) + self.padding
|
|
110
|
+
if self.cell_size:
|
|
111
|
+
cw, ch = self.cell_size
|
|
112
|
+
else:
|
|
113
|
+
cw, ch = 64, 32
|
|
114
|
+
for child in self.children:
|
|
115
|
+
b = child.get_bounding_rect() if hasattr(child, "get_bounding_rect") else getattr(child, "rect", None)
|
|
116
|
+
if isinstance(b, pygame.Rect):
|
|
117
|
+
cw = max(cw, b.width)
|
|
118
|
+
ch = max(ch, b.height)
|
|
119
|
+
|
|
120
|
+
visible_children = [c for c in self.children if getattr(c, "visible", True)]
|
|
121
|
+
for idx, child in enumerate(visible_children):
|
|
122
|
+
row = idx // self.columns
|
|
123
|
+
col = idx % self.columns
|
|
124
|
+
cx = x0 + col * (cw + self.spacing_x)
|
|
125
|
+
cy = y0 + row * (ch + self.spacing_y)
|
|
126
|
+
if hasattr(child, "set_position"):
|
|
127
|
+
child.set_position(cx, cy)
|
|
128
|
+
|
|
129
|
+
rows = (len(visible_children) + self.columns - 1) // self.columns if visible_children else 0
|
|
130
|
+
cols = min(len(visible_children), self.columns) if visible_children else 0
|
|
131
|
+
total_w = cols * cw + max(0, cols - 1) * self.spacing_x + self.padding * 2
|
|
132
|
+
total_h = rows * ch + max(0, rows - 1) * self.spacing_y + self.padding * 2
|
|
133
|
+
self.rect = pygame.Rect(int(self.position[0]), int(self.position[1]), total_w, total_h)
|
|
134
|
+
|
|
135
|
+
def move(self, dx: int, dy: int) -> None:
|
|
136
|
+
if dx == 0 and dy == 0:
|
|
137
|
+
return
|
|
138
|
+
super().move(dx, dy)
|
|
139
|
+
for child in self.children:
|
|
140
|
+
if hasattr(child, "move"):
|
|
141
|
+
child.move(dx, dy)
|
|
142
|
+
|
|
143
|
+
def events(self, event: pygame.event.Event) -> bool:
|
|
144
|
+
if not self.visible or not self.enabled:
|
|
145
|
+
return False
|
|
146
|
+
for child in self.children:
|
|
147
|
+
if hasattr(child, "events") and child.events(event):
|
|
148
|
+
return True
|
|
149
|
+
return False
|
|
150
|
+
|
|
151
|
+
def draw(self) -> None:
|
|
152
|
+
if not self.visible:
|
|
153
|
+
return
|
|
154
|
+
for child in self.children:
|
|
155
|
+
if hasattr(child, "draw"):
|
|
156
|
+
child.draw()
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
from .Panel import Panel
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class Modal(Panel):
|
|
6
|
+
"""Modal dialog box with backdrop dimming, input blocking, message text, and action buttons."""
|
|
7
|
+
|
|
8
|
+
def __init__(self, config: dict) -> None:
|
|
9
|
+
screen_w, screen_h = config["screen"].get_size()
|
|
10
|
+
w = config.get("width", 360)
|
|
11
|
+
h = config.get("height", 180)
|
|
12
|
+
if "position" not in config:
|
|
13
|
+
config["position"] = ((screen_w - w) // 2, (screen_h - h) // 2, w, h)
|
|
14
|
+
config.setdefault("visible", False)
|
|
15
|
+
config.setdefault("draggable", True)
|
|
16
|
+
super().__init__(config)
|
|
17
|
+
self.message: str = config.get("message", "")
|
|
18
|
+
self.overlay_alpha: int = config.get("overlay_alpha", 150)
|
|
19
|
+
self.block_background: bool = config.get("block_background", True)
|
|
20
|
+
self.on_confirm = config.get("on_confirm", None)
|
|
21
|
+
self.on_cancel = config.get("on_cancel", None)
|
|
22
|
+
|
|
23
|
+
from ..Elements_Factory import ElementsFactory
|
|
24
|
+
self.factory = ElementsFactory({
|
|
25
|
+
"screen": self.screen,
|
|
26
|
+
"window": self.window,
|
|
27
|
+
"font": self.font,
|
|
28
|
+
"color": self.color,
|
|
29
|
+
"hover_color": config.get("hover_color", (255, 199, 51)),
|
|
30
|
+
"sound_hover": self.sound_hover,
|
|
31
|
+
"sound_touch": self.sound_touch,
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
self._overlay_surface = pygame.Surface(self.screen.get_size(), pygame.SRCALPHA)
|
|
35
|
+
self._overlay_surface.fill((0, 0, 0, self.overlay_alpha))
|
|
36
|
+
self._msg_surfaces = [self.font.render(line, True, self.color) for line in self.message.split("\n")] if self.message else []
|
|
37
|
+
|
|
38
|
+
confirm_text = config.get("confirm_text", "OK")
|
|
39
|
+
cancel_text = config.get("cancel_text", "Cancel")
|
|
40
|
+
btn_y = self.rect.bottom - 38
|
|
41
|
+
if confirm_text:
|
|
42
|
+
self.confirm_button = self.factory.create_TextButton({
|
|
43
|
+
"position": (self.rect.centerx - 70 if cancel_text else self.rect.centerx - 20, btn_y),
|
|
44
|
+
"text": confirm_text,
|
|
45
|
+
"command1": self._handle_confirm,
|
|
46
|
+
})
|
|
47
|
+
self.add_child(self.confirm_button)
|
|
48
|
+
if cancel_text:
|
|
49
|
+
self.cancel_button = self.factory.create_TextButton({
|
|
50
|
+
"position": (self.rect.centerx + 25, btn_y),
|
|
51
|
+
"text": cancel_text,
|
|
52
|
+
"command1": self._handle_cancel,
|
|
53
|
+
})
|
|
54
|
+
self.add_child(self.cancel_button)
|
|
55
|
+
|
|
56
|
+
def _handle_confirm(self) -> None:
|
|
57
|
+
self.close()
|
|
58
|
+
if callable(self.on_confirm):
|
|
59
|
+
self.on_confirm()
|
|
60
|
+
|
|
61
|
+
def _handle_cancel(self) -> None:
|
|
62
|
+
self.close()
|
|
63
|
+
if callable(self.on_cancel):
|
|
64
|
+
self.on_cancel()
|
|
65
|
+
|
|
66
|
+
def open(self) -> None:
|
|
67
|
+
self.visible = True
|
|
68
|
+
|
|
69
|
+
def close(self) -> None:
|
|
70
|
+
self.visible = False
|
|
71
|
+
|
|
72
|
+
def events(self, event: pygame.event.Event) -> bool:
|
|
73
|
+
if not self.visible or not self.enabled:
|
|
74
|
+
return False
|
|
75
|
+
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
|
|
76
|
+
for child in self.children:
|
|
77
|
+
bounds = child.get_bounding_rect() if hasattr(child, "get_bounding_rect") else getattr(child, "rect", None)
|
|
78
|
+
if isinstance(bounds, pygame.Rect) and bounds.collidepoint(event.pos):
|
|
79
|
+
if hasattr(child, "execute_commands"):
|
|
80
|
+
if getattr(child, "sound_touch", None):
|
|
81
|
+
child.sound_touch.play(loops=0)
|
|
82
|
+
child.execute_commands()
|
|
83
|
+
return True
|
|
84
|
+
super().events(event)
|
|
85
|
+
return self.block_background
|
|
86
|
+
|
|
87
|
+
def draw(self) -> None:
|
|
88
|
+
if not self.visible:
|
|
89
|
+
return
|
|
90
|
+
if self._overlay_surface.get_size() != self.screen.get_size():
|
|
91
|
+
self._overlay_surface = pygame.Surface(self.screen.get_size(), pygame.SRCALPHA)
|
|
92
|
+
self._overlay_surface.fill((0, 0, 0, self.overlay_alpha))
|
|
93
|
+
self.screen.blit(self._overlay_surface, (0, 0))
|
|
94
|
+
super().draw()
|
|
95
|
+
y_cursor = self.rect.y + self.header_height + 20
|
|
96
|
+
for surf in self._msg_surfaces:
|
|
97
|
+
tx = self.rect.centerx - surf.get_width() // 2
|
|
98
|
+
self.screen.blit(surf, (tx, y_cursor))
|
|
99
|
+
y_cursor += surf.get_height() + 4
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
DialogBox = Modal
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
from ..Behavior.Element_behavior import ElementBehavior
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class Panel(ElementBehavior):
|
|
6
|
+
"""Container widget with optional header bar, dragging, clipping, and child management."""
|
|
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, 24))
|
|
11
|
+
self.title: str = config.get("title", config.get("text", ""))
|
|
12
|
+
self.draggable: bool = config.get("draggable", False)
|
|
13
|
+
self.clip_children: bool = config.get("clip_children", False)
|
|
14
|
+
self.color: tuple = config.get("color", (255, 255, 255))
|
|
15
|
+
self.color_back: tuple = config.get("color_back", (32, 36, 44))
|
|
16
|
+
self.header_color: tuple = config.get("header_color", (48, 54, 66))
|
|
17
|
+
self.border_color: tuple = config.get("border_color", (110, 120, 140))
|
|
18
|
+
self.border: int = config.get("border", 2)
|
|
19
|
+
self.border_radius: int = config.get("border_radius", 6)
|
|
20
|
+
self.header_height: int = config.get("header_height", 30 if self.title else 0)
|
|
21
|
+
|
|
22
|
+
if len(self.position) >= 4:
|
|
23
|
+
self.rect = pygame.Rect(*self.position[:4])
|
|
24
|
+
else:
|
|
25
|
+
self.rect = pygame.Rect(self.position[0], self.position[1], config.get("width", 300), config.get("height", 220))
|
|
26
|
+
|
|
27
|
+
self.children: list = list(config.get("children", []))
|
|
28
|
+
self.dragging: bool = False
|
|
29
|
+
self._drag_offset: tuple[int, int] = (0, 0)
|
|
30
|
+
self._update_surfaces()
|
|
31
|
+
|
|
32
|
+
def _update_surfaces(self) -> None:
|
|
33
|
+
self._title_surface = self.font.render(self.title, True, self.color) if self.title else None
|
|
34
|
+
self.header_rect = pygame.Rect(self.rect.x, self.rect.y, self.rect.width, self.header_height if self.header_height > 0 else 28)
|
|
35
|
+
|
|
36
|
+
def add_child(self, child, relative_pos: tuple[int, int] | None = None):
|
|
37
|
+
if relative_pos is not None and hasattr(child, "set_position"):
|
|
38
|
+
y_off = self.header_height if self.title else 0
|
|
39
|
+
child.set_position(self.rect.x + relative_pos[0], self.rect.y + y_off + relative_pos[1])
|
|
40
|
+
if child not in self.children:
|
|
41
|
+
self.children.append(child)
|
|
42
|
+
return child
|
|
43
|
+
|
|
44
|
+
def remove_child(self, child) -> None:
|
|
45
|
+
if child in self.children:
|
|
46
|
+
self.children.remove(child)
|
|
47
|
+
|
|
48
|
+
def move(self, dx: int, dy: int) -> None:
|
|
49
|
+
if dx == 0 and dy == 0:
|
|
50
|
+
return
|
|
51
|
+
super().move(dx, dy)
|
|
52
|
+
self.header_rect.move_ip(dx, dy)
|
|
53
|
+
for child in self.children:
|
|
54
|
+
if hasattr(child, "move"):
|
|
55
|
+
child.move(dx, dy)
|
|
56
|
+
|
|
57
|
+
def events(self, event: pygame.event.Event) -> bool:
|
|
58
|
+
if not self.visible or not self.enabled:
|
|
59
|
+
return False
|
|
60
|
+
|
|
61
|
+
for child in reversed(self.children):
|
|
62
|
+
if hasattr(child, "events") and child.events(event):
|
|
63
|
+
return True
|
|
64
|
+
|
|
65
|
+
if self.draggable:
|
|
66
|
+
drag_zone = self.header_rect if self.title else self.rect
|
|
67
|
+
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
|
|
68
|
+
if drag_zone.collidepoint(event.pos):
|
|
69
|
+
self.dragging = True
|
|
70
|
+
self._drag_offset = (event.pos[0] - self.rect.x, event.pos[1] - self.rect.y)
|
|
71
|
+
return True
|
|
72
|
+
elif event.type == pygame.MOUSEBUTTONUP and event.button == 1:
|
|
73
|
+
if self.dragging:
|
|
74
|
+
self.dragging = False
|
|
75
|
+
return True
|
|
76
|
+
elif event.type == pygame.MOUSEMOTION and self.dragging:
|
|
77
|
+
new_x = event.pos[0] - self._drag_offset[0]
|
|
78
|
+
new_y = event.pos[1] - self._drag_offset[1]
|
|
79
|
+
self.move(new_x - self.rect.x, new_y - self.rect.y)
|
|
80
|
+
return True
|
|
81
|
+
|
|
82
|
+
return False
|
|
83
|
+
|
|
84
|
+
def draw(self) -> None:
|
|
85
|
+
if not self.visible:
|
|
86
|
+
return
|
|
87
|
+
pygame.draw.rect(self.screen, self.color_back, self.rect, border_radius=self.border_radius)
|
|
88
|
+
if self.title and self.header_height > 0:
|
|
89
|
+
hdr = pygame.Rect(self.rect.x, self.rect.y, self.rect.width, self.header_height)
|
|
90
|
+
pygame.draw.rect(
|
|
91
|
+
self.screen,
|
|
92
|
+
self.header_color,
|
|
93
|
+
hdr,
|
|
94
|
+
border_top_left_radius=self.border_radius,
|
|
95
|
+
border_top_right_radius=self.border_radius,
|
|
96
|
+
)
|
|
97
|
+
if self._title_surface:
|
|
98
|
+
ty = hdr.centery - self._title_surface.get_height() // 2
|
|
99
|
+
self.screen.blit(self._title_surface, (hdr.x + 10, ty))
|
|
100
|
+
|
|
101
|
+
if self.border > 0:
|
|
102
|
+
pygame.draw.rect(self.screen, self.border_color, self.rect, self.border, border_radius=self.border_radius)
|
|
103
|
+
|
|
104
|
+
prev_clip = self.screen.get_clip()
|
|
105
|
+
if self.clip_children:
|
|
106
|
+
content_rect = pygame.Rect(
|
|
107
|
+
self.rect.x + self.border,
|
|
108
|
+
self.rect.y + self.header_height + self.border,
|
|
109
|
+
max(0, self.rect.width - self.border * 2),
|
|
110
|
+
max(0, self.rect.height - self.header_height - self.border * 2),
|
|
111
|
+
)
|
|
112
|
+
self.screen.set_clip(content_rect)
|
|
113
|
+
|
|
114
|
+
for child in self.children:
|
|
115
|
+
if getattr(child, "visible", True) and hasattr(child, "draw"):
|
|
116
|
+
child.draw()
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
if self.clip_children:
|
|
120
|
+
self.screen.set_clip(prev_clip)
|
|
121
|
+
|
|
122
|
+
def draw_hover_effect(self) -> None:
|
|
123
|
+
pass
|