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,116 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
from ..Behavior.Element_behavior import ElementBehavior
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class TabContainer(ElementBehavior):
|
|
6
|
+
"""Tabbed container switching between named pages of child widgets."""
|
|
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.tab_height: int = config.get("tab_height", 32)
|
|
12
|
+
self.color: tuple = config.get("color", (255, 255, 255))
|
|
13
|
+
self.color_back: tuple = config.get("color_back", (34, 38, 46))
|
|
14
|
+
self.tab_color: tuple = config.get("tab_color", (50, 56, 68))
|
|
15
|
+
self.active_tab_color: tuple = config.get("active_tab_color", (75, 135, 215))
|
|
16
|
+
self.hover_color: tuple = config.get("hover_color", (255, 199, 51))
|
|
17
|
+
self.border_color: tuple = config.get("border_color", (110, 120, 140))
|
|
18
|
+
self.border: int = config.get("border", 2)
|
|
19
|
+
|
|
20
|
+
if len(self.position) >= 4:
|
|
21
|
+
self.rect = pygame.Rect(*self.position[:4])
|
|
22
|
+
else:
|
|
23
|
+
self.rect = pygame.Rect(self.position[0], self.position[1], config.get("width", 400), config.get("height", 260))
|
|
24
|
+
|
|
25
|
+
self.tabs: dict[str, list] = {}
|
|
26
|
+
self.tab_rects: dict[str, pygame.Rect] = {}
|
|
27
|
+
self._tab_surfaces: dict[str, pygame.Surface] = {}
|
|
28
|
+
self.active_tab: str | None = None
|
|
29
|
+
|
|
30
|
+
for tab_name in config.get("tabs", []):
|
|
31
|
+
self.add_tab(tab_name)
|
|
32
|
+
|
|
33
|
+
def add_tab(self, name: str, children: list | None = None) -> None:
|
|
34
|
+
if name not in self.tabs:
|
|
35
|
+
self.tabs[name] = list(children) if children else []
|
|
36
|
+
if self.active_tab is None:
|
|
37
|
+
self.active_tab = name
|
|
38
|
+
self._rebuild_tab_headers()
|
|
39
|
+
|
|
40
|
+
def add_to_tab(self, tab_name: str, widget, relative_pos: tuple[int, int] | None = None):
|
|
41
|
+
if tab_name not in self.tabs:
|
|
42
|
+
self.add_tab(tab_name)
|
|
43
|
+
if relative_pos is not None and hasattr(widget, "set_position"):
|
|
44
|
+
widget.set_position(self.rect.x + relative_pos[0], self.rect.y + self.tab_height + relative_pos[1])
|
|
45
|
+
self.tabs[tab_name].append(widget)
|
|
46
|
+
return widget
|
|
47
|
+
|
|
48
|
+
def select_tab(self, name: str) -> None:
|
|
49
|
+
if name in self.tabs:
|
|
50
|
+
self.active_tab = name
|
|
51
|
+
self.execute_commands(name)
|
|
52
|
+
|
|
53
|
+
def _rebuild_tab_headers(self) -> None:
|
|
54
|
+
self.tab_rects.clear()
|
|
55
|
+
self._tab_surfaces.clear()
|
|
56
|
+
x_cursor = self.rect.x
|
|
57
|
+
for name in self.tabs:
|
|
58
|
+
surf = self.font.render(name, True, self.color)
|
|
59
|
+
w = max(80, surf.get_width() + 24)
|
|
60
|
+
self.tab_rects[name] = pygame.Rect(x_cursor, self.rect.y, w, self.tab_height)
|
|
61
|
+
self._tab_surfaces[name] = surf
|
|
62
|
+
x_cursor += w + 4
|
|
63
|
+
|
|
64
|
+
def move(self, dx: int, dy: int) -> None:
|
|
65
|
+
if dx == 0 and dy == 0:
|
|
66
|
+
return
|
|
67
|
+
super().move(dx, dy)
|
|
68
|
+
for r in self.tab_rects.values():
|
|
69
|
+
r.move_ip(dx, dy)
|
|
70
|
+
for widget_list in self.tabs.values():
|
|
71
|
+
for w in widget_list:
|
|
72
|
+
if hasattr(w, "move"):
|
|
73
|
+
w.move(dx, dy)
|
|
74
|
+
|
|
75
|
+
def events(self, event: pygame.event.Event) -> bool:
|
|
76
|
+
if not self.visible or not self.enabled:
|
|
77
|
+
return False
|
|
78
|
+
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
|
|
79
|
+
for name, trect in self.tab_rects.items():
|
|
80
|
+
if trect.collidepoint(event.pos):
|
|
81
|
+
if self.sound_touch:
|
|
82
|
+
self.sound_touch.play(loops=0)
|
|
83
|
+
self.select_tab(name)
|
|
84
|
+
return True
|
|
85
|
+
if self.active_tab and self.active_tab in self.tabs:
|
|
86
|
+
for widget in reversed(self.tabs[self.active_tab]):
|
|
87
|
+
if hasattr(widget, "events") and widget.events(event):
|
|
88
|
+
return True
|
|
89
|
+
return False
|
|
90
|
+
|
|
91
|
+
def draw(self) -> None:
|
|
92
|
+
if not self.visible:
|
|
93
|
+
return
|
|
94
|
+
body_rect = pygame.Rect(self.rect.x, self.rect.y + self.tab_height, self.rect.width, max(0, self.rect.height - self.tab_height))
|
|
95
|
+
pygame.draw.rect(self.screen, self.color_back, body_rect, border_bottom_left_radius=6, border_bottom_right_radius=6, border_top_right_radius=6)
|
|
96
|
+
pygame.draw.rect(self.screen, self.border_color, body_rect, self.border, border_bottom_left_radius=6, border_bottom_right_radius=6, border_top_right_radius=6)
|
|
97
|
+
|
|
98
|
+
mouse_pos = self.get_mouse_pos()
|
|
99
|
+
for name, trect in self.tab_rects.items():
|
|
100
|
+
bg = self.active_tab_color if name == self.active_tab else self.tab_color
|
|
101
|
+
pygame.draw.rect(self.screen, bg, trect, border_top_left_radius=6, border_top_right_radius=6)
|
|
102
|
+
if self.detect_mouse and trect.collidepoint(mouse_pos):
|
|
103
|
+
pygame.draw.rect(self.screen, self.hover_color, trect, 2, border_top_left_radius=6, border_top_right_radius=6)
|
|
104
|
+
else:
|
|
105
|
+
pygame.draw.rect(self.screen, self.border_color, trect, 1, border_top_left_radius=6, border_top_right_radius=6)
|
|
106
|
+
surf = self._tab_surfaces[name]
|
|
107
|
+
self.screen.blit(surf, (trect.centerx - surf.get_width() // 2, trect.centery - surf.get_height() // 2))
|
|
108
|
+
|
|
109
|
+
if self.active_tab and self.active_tab in self.tabs:
|
|
110
|
+
for widget in self.tabs[self.active_tab]:
|
|
111
|
+
if getattr(widget, "visible", True) and hasattr(widget, "draw"):
|
|
112
|
+
widget.draw()
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def draw_hover_effect(self) -> None:
|
|
116
|
+
pass
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
from ..Behavior.Element_behavior import ElementBehavior
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class CheckBox(ElementBehavior):
|
|
6
|
+
"""Boolean checkbox control with optional text label and callback support."""
|
|
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.size: int = config.get("size", 22)
|
|
13
|
+
self.checked: bool = config.get("checked", config.get("value", False))
|
|
14
|
+
self.color: tuple = config.get("color", (255, 255, 255))
|
|
15
|
+
self.color_back: tuple = config.get("color_back", (45, 48, 56))
|
|
16
|
+
self.check_color: tuple = config.get("check_color", (80, 210, 120))
|
|
17
|
+
self.hover_color: tuple = config.get("hover_color", (255, 199, 51))
|
|
18
|
+
self.border_color: tuple = config.get("border_color", (180, 185, 195))
|
|
19
|
+
self.border: int = config.get("border", 2)
|
|
20
|
+
self.spacing: int = config.get("spacing", 8)
|
|
21
|
+
self.on_change = config.get("on_change", None)
|
|
22
|
+
self._update_surfaces()
|
|
23
|
+
|
|
24
|
+
def _update_surfaces(self) -> None:
|
|
25
|
+
x, y = int(self.position[0]), int(self.position[1])
|
|
26
|
+
self.box_rect = pygame.Rect(x, y, self.size, self.size)
|
|
27
|
+
self._label_surface = self.font.render(self.text, True, self.color) if self.text else None
|
|
28
|
+
self._label_hover_surface = self.font.render(self.text, True, self.hover_color) if self.text else None
|
|
29
|
+
if self._label_surface:
|
|
30
|
+
label_y = y + (self.size - self._label_surface.get_height()) // 2
|
|
31
|
+
self.label_pos = (x + self.size + self.spacing, label_y)
|
|
32
|
+
total_w = self.size + self.spacing + self._label_surface.get_width()
|
|
33
|
+
total_h = max(self.size, self._label_surface.get_height())
|
|
34
|
+
self.rect = pygame.Rect(x, min(y, label_y), total_w, total_h)
|
|
35
|
+
else:
|
|
36
|
+
self.label_pos = (x, y)
|
|
37
|
+
self.rect = self.box_rect.copy()
|
|
38
|
+
|
|
39
|
+
def move(self, dx: int, dy: int) -> None:
|
|
40
|
+
super().move(dx, dy)
|
|
41
|
+
self.box_rect.x += dx
|
|
42
|
+
self.box_rect.y += dy
|
|
43
|
+
self.label_pos = (self.label_pos[0] + dx, self.label_pos[1] + dy)
|
|
44
|
+
|
|
45
|
+
def toggle(self) -> bool:
|
|
46
|
+
self.set_checked(not self.checked)
|
|
47
|
+
return self.checked
|
|
48
|
+
|
|
49
|
+
def set_checked(self, value: bool) -> None:
|
|
50
|
+
new_val = bool(value)
|
|
51
|
+
if self.checked != new_val:
|
|
52
|
+
self.checked = new_val
|
|
53
|
+
if callable(self.on_change):
|
|
54
|
+
self.on_change(self.checked)
|
|
55
|
+
self.execute_commands(self.checked)
|
|
56
|
+
|
|
57
|
+
def get_value(self) -> bool:
|
|
58
|
+
return self.checked
|
|
59
|
+
|
|
60
|
+
def events(self, event: pygame.event.Event) -> bool:
|
|
61
|
+
if not self.visible or not self.enabled:
|
|
62
|
+
return False
|
|
63
|
+
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
|
|
64
|
+
if self.rect.collidepoint(event.pos):
|
|
65
|
+
if self.sound_touch:
|
|
66
|
+
self.sound_touch.play(loops=0)
|
|
67
|
+
self.toggle()
|
|
68
|
+
self.states["presses_touch"] = False
|
|
69
|
+
return True
|
|
70
|
+
return False
|
|
71
|
+
|
|
72
|
+
def draw(self) -> None:
|
|
73
|
+
if not self.visible:
|
|
74
|
+
return
|
|
75
|
+
pygame.draw.rect(self.screen, self.color_back, self.box_rect, border_radius=4)
|
|
76
|
+
pygame.draw.rect(self.screen, self.border_color, self.box_rect, self.border, border_radius=4)
|
|
77
|
+
if self._label_surface:
|
|
78
|
+
self.screen.blit(self._label_surface, self.label_pos)
|
|
79
|
+
|
|
80
|
+
mouse_pos = self.get_mouse_pos()
|
|
81
|
+
if self.detect_mouse:
|
|
82
|
+
self.mouse_collision(self.rect, mouse_pos, self.draw_hover_effect)
|
|
83
|
+
|
|
84
|
+
if self.checked:
|
|
85
|
+
inner = self.box_rect.inflate(-6, -6)
|
|
86
|
+
pygame.draw.rect(self.screen, self.check_color, inner, border_radius=2)
|
|
87
|
+
|
|
88
|
+
def draw_hover_effect(self) -> None:
|
|
89
|
+
pygame.draw.rect(self.screen, self.hover_color, self.box_rect, self.border, border_radius=4)
|
|
90
|
+
if self._label_hover_surface:
|
|
91
|
+
self.screen.blit(self._label_hover_surface, self.label_pos)
|
|
92
|
+
|
|
93
|
+
def change_item(self, config: dict) -> None:
|
|
94
|
+
self.position = config.get("position", self.position)
|
|
95
|
+
self.text = config.get("text", self.text)
|
|
96
|
+
self.checked = config.get("checked", self.checked)
|
|
97
|
+
self.color = config.get("color", self.color)
|
|
98
|
+
self.hover_color = config.get("hover_color", self.hover_color)
|
|
99
|
+
self.detect_mouse = config.get("detect_mouse", self.detect_mouse)
|
|
100
|
+
self.pressed = config.get("pressed", self.pressed)
|
|
101
|
+
self._update_surfaces()
|
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
from .Text_button import TextButton
|
|
3
|
+
from .Scroll_bar import ScrollBar
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class ComboBox(TextButton):
|
|
7
|
+
def __init__(self, config: dict) -> None:
|
|
8
|
+
super().__init__(config)
|
|
9
|
+
from ..Elements_Factory import ElementsFactory
|
|
10
|
+
self.type_dropdown: str = self.get_icon()
|
|
11
|
+
self.dropdown: list[int] = config.get("size", [self.font.size(self.text)[0] + self.font.size(self.type_dropdown)[0], 100])
|
|
12
|
+
self.hover_dropdown: tuple[int, int, int] = config.get("hover_dropdown", (135, 206, 235))
|
|
13
|
+
self.replace_text: bool = config.get("replace_text", False)
|
|
14
|
+
self.adapt_dropdown: bool = config.get("adapt_dropdown", True)
|
|
15
|
+
self.draw_scroll: bool = config.get("draw_scroll", True)
|
|
16
|
+
self.command_dropdown = config.get("command_dropdown", None)
|
|
17
|
+
self.anim_height_dropdown: int = 0
|
|
18
|
+
self.is_dropdown_open: bool = False
|
|
19
|
+
self.options: list[str] = []
|
|
20
|
+
self.option_buttons: dict = {}
|
|
21
|
+
self._last_layout_rect: pygame.Rect | None = None
|
|
22
|
+
self.padding_x: int = config.get("padding_x", 6)
|
|
23
|
+
self.padding_y: int = config.get("padding_y", 6)
|
|
24
|
+
self.item_spacing: int = config.get("item_spacing", 6)
|
|
25
|
+
|
|
26
|
+
self.factory = ElementsFactory({
|
|
27
|
+
"screen": self.screen,
|
|
28
|
+
"window": self.window,
|
|
29
|
+
"font": self.font,
|
|
30
|
+
"color": self.color,
|
|
31
|
+
"hover_color": self.hover_color,
|
|
32
|
+
"sound_hover": self.sound_hover,
|
|
33
|
+
"sound_touch": self.sound_touch,
|
|
34
|
+
})
|
|
35
|
+
self.button_dropdown = self.factory.create_TextButton({
|
|
36
|
+
"position": (self.position[0] + self.font.size(self.text)[0], int(self.position[1])),
|
|
37
|
+
"text": self.type_dropdown,
|
|
38
|
+
"command1": lambda: setattr(self, "is_dropdown_open", not self.is_dropdown_open),
|
|
39
|
+
"command2": self.command_dropdown if callable(self.command_dropdown) else None,
|
|
40
|
+
})
|
|
41
|
+
self._combo_hover_surface = self.font.render(f"{self.text}{self.type_dropdown}", True, self.hover_color)
|
|
42
|
+
self.rect: dict[str, object] = {
|
|
43
|
+
"button": pygame.Rect(*self.position, *self.font.size(self.text)),
|
|
44
|
+
"dropdown": self.button_dropdown,
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
def _update_surfaces(self) -> None:
|
|
48
|
+
self._text_surface = self.font.render(self.text, True, self.color)
|
|
49
|
+
self._hover_surface = self.font.render(self.text, True, self.hover_color)
|
|
50
|
+
if hasattr(self, "type_dropdown"):
|
|
51
|
+
self._combo_hover_surface = self.font.render(f"{self.text}{self.type_dropdown}", True, self.hover_color)
|
|
52
|
+
if hasattr(self, "rect") and isinstance(self.rect, dict):
|
|
53
|
+
self.rect["button"] = pygame.Rect(*self.position, *self._text_surface.get_size())
|
|
54
|
+
else:
|
|
55
|
+
self.rect = pygame.Rect(*self.position, *self._text_surface.get_size())
|
|
56
|
+
|
|
57
|
+
def move(self, dx: int, dy: int) -> None:
|
|
58
|
+
if dx == 0 and dy == 0:
|
|
59
|
+
return
|
|
60
|
+
self.position = (self.position[0] + dx, self.position[1] + dy)
|
|
61
|
+
if isinstance(self.rect, dict) and "button" in self.rect:
|
|
62
|
+
self.rect["button"].move_ip(dx, dy)
|
|
63
|
+
self.button_dropdown.move(dx, dy)
|
|
64
|
+
for btn in self.option_buttons.values():
|
|
65
|
+
btn.move(dx, dy)
|
|
66
|
+
if hasattr(self, "scroll"):
|
|
67
|
+
self.scroll.move(dx, dy)
|
|
68
|
+
if hasattr(self, "dropdown_rect") and isinstance(self.dropdown_rect, pygame.Rect):
|
|
69
|
+
self.dropdown_rect.move_ip(dx, dy)
|
|
70
|
+
|
|
71
|
+
def get_icon(self) -> str:
|
|
72
|
+
raise NotImplementedError
|
|
73
|
+
|
|
74
|
+
def is_vertical(self) -> bool:
|
|
75
|
+
return False
|
|
76
|
+
|
|
77
|
+
def _get_rect_dropdown(self) -> pygame.Rect:
|
|
78
|
+
raise NotImplementedError
|
|
79
|
+
|
|
80
|
+
def _adapt_size_dropdown(self) -> None:
|
|
81
|
+
raise NotImplementedError
|
|
82
|
+
|
|
83
|
+
def _check_buttons_position(self, i: int, text: str = "", first: bool = False) -> tuple[int, int]:
|
|
84
|
+
raise NotImplementedError
|
|
85
|
+
|
|
86
|
+
def _update_scroll_before_draw(self) -> None:
|
|
87
|
+
pass
|
|
88
|
+
|
|
89
|
+
def _post_repeat_charge(self, i: int, text: str, button: object) -> None:
|
|
90
|
+
pass
|
|
91
|
+
|
|
92
|
+
def _create_scroll(self) -> None:
|
|
93
|
+
if self.is_vertical():
|
|
94
|
+
self.scroll = self.factory.create_ScrollBar({
|
|
95
|
+
"position": (self.position[0] + self.dropdown[0], self.position[1] + self.font.get_height(), 20, self.dropdown[1]),
|
|
96
|
+
"type_of_orientation": "vertical",
|
|
97
|
+
"thumb_height": 20,
|
|
98
|
+
"color_bar": (135, 206, 235),
|
|
99
|
+
})
|
|
100
|
+
else:
|
|
101
|
+
self.scroll = self.factory.create_ScrollBar({
|
|
102
|
+
"position": (
|
|
103
|
+
self.position[0] + (self.font.size(self.text)[0] + self.font.size(self.type_dropdown)[0]),
|
|
104
|
+
int(self.position[1] + (self.font.get_height() / 2) + self.dropdown[1]),
|
|
105
|
+
self.dropdown[0],
|
|
106
|
+
20,
|
|
107
|
+
),
|
|
108
|
+
"type_of_orientation": "horizontal",
|
|
109
|
+
"thumb_width": 20,
|
|
110
|
+
"color_bar": (135, 206, 235),
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
def draw(self) -> None:
|
|
114
|
+
self.screen.blit(self._text_surface, self.position)
|
|
115
|
+
self.button_dropdown.draw()
|
|
116
|
+
if self.is_dropdown_open:
|
|
117
|
+
self.draw_rect_dropdown()
|
|
118
|
+
else:
|
|
119
|
+
self.anim_height_dropdown = 0
|
|
120
|
+
self.button_dropdown.change_item({"color": self.color})
|
|
121
|
+
if self.detect_mouse:
|
|
122
|
+
self.mouse_collision(self.rect["button"], self.get_mouse_pos(), self.draw_hover_effect)
|
|
123
|
+
if self.pressed:
|
|
124
|
+
self.pressed_button(self.rect["button"], pygame.mouse.get_pressed(), self.get_mouse_pos())
|
|
125
|
+
|
|
126
|
+
def draw_hover_effect(self) -> pygame.Rect:
|
|
127
|
+
return self.screen.blit(self._combo_hover_surface, self.position)
|
|
128
|
+
|
|
129
|
+
def _visible_buttons(self) -> list:
|
|
130
|
+
if self.replace_text:
|
|
131
|
+
return [btn for btn in self.option_buttons.values() if btn.text != self.text]
|
|
132
|
+
return list(self.option_buttons.values())
|
|
133
|
+
|
|
134
|
+
def draw_rect_dropdown(self) -> None:
|
|
135
|
+
self.button_dropdown.change_item({"color": self.hover_dropdown})
|
|
136
|
+
if self.adapt_dropdown and self.option_buttons:
|
|
137
|
+
self._adapt_size_dropdown()
|
|
138
|
+
self.dropdown_rect = self._get_rect_dropdown()
|
|
139
|
+
pygame.draw.rect(self.screen, self.hover_dropdown, self.dropdown_rect)
|
|
140
|
+
pygame.draw.rect(self.screen, self.color, self.dropdown_rect, 2)
|
|
141
|
+
if self.draw_scroll and self.option_buttons:
|
|
142
|
+
self._rebuild_scroll()
|
|
143
|
+
for button in self._visible_buttons():
|
|
144
|
+
self._draw_option_buttons(button)
|
|
145
|
+
if hasattr(self, "scroll"):
|
|
146
|
+
self._draw_scroll()
|
|
147
|
+
|
|
148
|
+
def _draw_option_buttons(self, button: object) -> None:
|
|
149
|
+
if self._is_button_visible(button):
|
|
150
|
+
button.draw()
|
|
151
|
+
|
|
152
|
+
def _is_button_visible(self, button: object) -> bool:
|
|
153
|
+
if self.is_vertical():
|
|
154
|
+
return button.rect.bottom <= self.dropdown_rect.bottom and button.rect.top >= self.dropdown_rect.top
|
|
155
|
+
else:
|
|
156
|
+
return button.rect.right <= self.dropdown_rect.right and button.rect.left >= self.dropdown_rect.left
|
|
157
|
+
|
|
158
|
+
def _draw_scroll(self) -> None:
|
|
159
|
+
self._update_scroll_before_draw()
|
|
160
|
+
self.scroll.draw()
|
|
161
|
+
|
|
162
|
+
def _rebuild_scroll(self) -> None:
|
|
163
|
+
self._create_scroll()
|
|
164
|
+
if hasattr(self, "dropdown_rect") and isinstance(self.dropdown_rect, pygame.Rect):
|
|
165
|
+
self.scroll.scroll_area = self.dropdown_rect
|
|
166
|
+
self.rect["scroll"] = self.scroll.rect
|
|
167
|
+
self.scroll.update_elements(self._visible_buttons())
|
|
168
|
+
self.draw_scroll = False
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def charge_elements(self, options: dict) -> None:
|
|
172
|
+
self._last_layout_rect = None
|
|
173
|
+
for i, (option, action) in enumerate(options.items()):
|
|
174
|
+
pos = self._check_buttons_position(i, option, first=(i == 0))
|
|
175
|
+
button = self.factory.create_TextButton({
|
|
176
|
+
"text": option,
|
|
177
|
+
"position": pos,
|
|
178
|
+
"command1": lambda idx=i: self._select_option(idx) if self.replace_text else None,
|
|
179
|
+
"command2": action if callable(action) else None,
|
|
180
|
+
})
|
|
181
|
+
self._last_layout_rect = button.rect
|
|
182
|
+
self._repeat_charge(f"elements_{i}", option, button, i)
|
|
183
|
+
if self.replace_text and self.text in self.option_buttons:
|
|
184
|
+
self.charge_buttons([btn for btn in self.option_buttons.values() if btn.text != self.text], True)
|
|
185
|
+
|
|
186
|
+
def charge_buttons(self, buttons: list, first: bool = False) -> None:
|
|
187
|
+
self._last_layout_rect = None
|
|
188
|
+
for i, button in enumerate(buttons):
|
|
189
|
+
is_first = first if i == 0 else False
|
|
190
|
+
button.position = self._check_buttons_position(i, button.text, is_first)
|
|
191
|
+
if hasattr(button, "rect"):
|
|
192
|
+
if isinstance(button.rect, pygame.Rect):
|
|
193
|
+
button.rect.topleft = button.position
|
|
194
|
+
self._last_layout_rect = button.rect
|
|
195
|
+
elif isinstance(button.rect, dict) and "button" in button.rect:
|
|
196
|
+
button.rect["button"].topleft = button.position
|
|
197
|
+
self._last_layout_rect = button.rect["button"]
|
|
198
|
+
self._repeat_charge(f"buttons_{i}", button.text, button, i)
|
|
199
|
+
|
|
200
|
+
def _repeat_charge(self, key, text, button, i) -> None:
|
|
201
|
+
self.option_buttons[text] = button
|
|
202
|
+
self.rect[key] = button
|
|
203
|
+
if text not in self.options:
|
|
204
|
+
self.options.append(text)
|
|
205
|
+
self._post_repeat_charge(i, text, button)
|
|
206
|
+
|
|
207
|
+
def _select_option(self, index: int) -> None:
|
|
208
|
+
if 0 <= index < len(self.options):
|
|
209
|
+
self.text = self.options[index]
|
|
210
|
+
self._update_surfaces()
|
|
211
|
+
self.option_buttons[self.options[index]].position = self.position
|
|
212
|
+
self.option_buttons[self.options[index]].rect = pygame.Rect(*self.position, *self.font.size(self.text))
|
|
213
|
+
self.button_dropdown.change_item({"position": (self.position[0] + self.font.size(self.text)[0], int(self.position[1]))})
|
|
214
|
+
self.button_dropdown.rect = pygame.Rect(self.button_dropdown.position, self.font.size(self.button_dropdown.text))
|
|
215
|
+
self.is_dropdown_open = False
|
|
216
|
+
self.charge_buttons([button for button in self.option_buttons.values() if button.text != self.text], True)
|
|
217
|
+
self.adapt_dropdown = True
|
|
218
|
+
self._adapt_size_dropdown()
|
|
219
|
+
self._rebuild_scroll()
|
|
220
|
+
|
|
221
|
+
def events(self, event) -> bool:
|
|
222
|
+
if not self.visible or not self.enabled:
|
|
223
|
+
return False
|
|
224
|
+
if self.is_dropdown_open and hasattr(self, "scroll"):
|
|
225
|
+
if self.scroll.events(event):
|
|
226
|
+
return True
|
|
227
|
+
return False
|
|
228
|
+
|
|
229
|
+
def return_buttons(self, button: str):
|
|
230
|
+
return self.option_buttons[button]
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
class ComboBoxDown(ComboBox):
|
|
234
|
+
def get_icon(self) -> str:
|
|
235
|
+
return " V"
|
|
236
|
+
|
|
237
|
+
def is_vertical(self) -> bool:
|
|
238
|
+
return True
|
|
239
|
+
|
|
240
|
+
def _get_rect_dropdown(self) -> pygame.Rect:
|
|
241
|
+
if self.anim_height_dropdown < self.dropdown[1]:
|
|
242
|
+
self.anim_height_dropdown = min(self.dropdown[1], self.anim_height_dropdown + max(1, self.dropdown[1] // 15))
|
|
243
|
+
return pygame.Rect(self.position[0], self.position[1] + self.font.get_height(), self.dropdown[0], self.anim_height_dropdown)
|
|
244
|
+
|
|
245
|
+
def _adapt_size_dropdown(self) -> None:
|
|
246
|
+
vis = self._visible_buttons()
|
|
247
|
+
count = max(1, len(vis))
|
|
248
|
+
self.dropdown[1] = self.padding_y * 2 + count * self.font.get_height() + max(0, count - 1) * self.item_spacing
|
|
249
|
+
if vis:
|
|
250
|
+
max_w = max(self.font.size(b.text)[0] for b in vis)
|
|
251
|
+
self.dropdown[0] = max(self.dropdown[0], max_w + self.padding_x * 2)
|
|
252
|
+
self.adapt_dropdown = False
|
|
253
|
+
|
|
254
|
+
def _check_buttons_position(self, i: int, text: str = "", first: bool = False) -> tuple[int, int]:
|
|
255
|
+
x = int(self.position[0] + self.padding_x)
|
|
256
|
+
if self._last_layout_rect is None or first:
|
|
257
|
+
y = int(self.position[1] + self.font.get_height() + self.padding_y)
|
|
258
|
+
else:
|
|
259
|
+
y = int(self._last_layout_rect.bottom + self.item_spacing)
|
|
260
|
+
return (x, y)
|
|
261
|
+
|
|
262
|
+
def _update_scroll_before_draw(self) -> None:
|
|
263
|
+
self.scroll.rect["rect"].height = self.dropdown_rect.height
|
|
264
|
+
|
|
265
|
+
def _post_repeat_charge(self, i: int, text: str, button: object) -> None:
|
|
266
|
+
self.dropdown[0] = max(self.dropdown[0], self.font.size(button.text)[0] + self.padding_x * 2)
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
class ComboBoxUp(ComboBox):
|
|
270
|
+
def get_icon(self) -> str:
|
|
271
|
+
return " ^"
|
|
272
|
+
|
|
273
|
+
def is_vertical(self) -> bool:
|
|
274
|
+
return True
|
|
275
|
+
|
|
276
|
+
def _get_rect_dropdown(self) -> pygame.Rect:
|
|
277
|
+
if self.anim_height_dropdown < self.dropdown[1]:
|
|
278
|
+
self.anim_height_dropdown = min(self.dropdown[1], self.anim_height_dropdown + max(1, self.dropdown[1] // 15))
|
|
279
|
+
return pygame.Rect(self.position[0], self.position[1] - self.anim_height_dropdown, self.dropdown[0], self.anim_height_dropdown)
|
|
280
|
+
|
|
281
|
+
def _adapt_size_dropdown(self) -> None:
|
|
282
|
+
vis = self._visible_buttons()
|
|
283
|
+
count = max(1, len(vis))
|
|
284
|
+
self.dropdown[1] = self.padding_y * 2 + count * self.font.get_height() + max(0, count - 1) * self.item_spacing
|
|
285
|
+
if vis:
|
|
286
|
+
max_w = max(self.font.size(b.text)[0] for b in vis)
|
|
287
|
+
self.dropdown[0] = max(self.dropdown[0], max_w + self.padding_x * 2)
|
|
288
|
+
self.adapt_dropdown = False
|
|
289
|
+
|
|
290
|
+
def _check_buttons_position(self, i: int, text: str = "", first: bool = False) -> tuple[int, int]:
|
|
291
|
+
x = int(self.position[0] + self.padding_x)
|
|
292
|
+
if self._last_layout_rect is None or first:
|
|
293
|
+
y = int(self.position[1] - self.font.get_height() - self.padding_y)
|
|
294
|
+
else:
|
|
295
|
+
y = int(self._last_layout_rect.top - self.font.get_height() - self.item_spacing)
|
|
296
|
+
return (x, y)
|
|
297
|
+
|
|
298
|
+
def _update_scroll_before_draw(self) -> None:
|
|
299
|
+
if self.dropdown_rect.height != self.dropdown[1]:
|
|
300
|
+
self.scroll.rect["rect"].y = self.scroll.rect["thumb"].y = self.dropdown_rect.y
|
|
301
|
+
self.scroll.rect["rect"].height = self.dropdown_rect.height
|
|
302
|
+
|
|
303
|
+
def _post_repeat_charge(self, i: int, text: str, button: object) -> None:
|
|
304
|
+
self.dropdown[0] = max(self.dropdown[0], self.font.size(button.text)[0] + self.padding_x * 2)
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
class ComboBoxRight(ComboBox):
|
|
308
|
+
def get_icon(self) -> str:
|
|
309
|
+
return " >"
|
|
310
|
+
|
|
311
|
+
def _dropdown_start_x(self) -> int:
|
|
312
|
+
return int(self.position[0] + self.font.size(self.text)[0] + self.font.size(self.type_dropdown)[0] + 4)
|
|
313
|
+
|
|
314
|
+
def _dropdown_top_y(self) -> int:
|
|
315
|
+
return int(self.position[1] - self.padding_y // 2)
|
|
316
|
+
|
|
317
|
+
def _get_rect_dropdown(self) -> pygame.Rect:
|
|
318
|
+
if self.anim_height_dropdown < self.dropdown[0]:
|
|
319
|
+
self.anim_height_dropdown = min(self.dropdown[0], self.anim_height_dropdown + max(1, self.dropdown[0] // 15))
|
|
320
|
+
return pygame.Rect(self._dropdown_start_x(), self._dropdown_top_y(), self.anim_height_dropdown, self.dropdown[1])
|
|
321
|
+
|
|
322
|
+
def _adapt_size_dropdown(self) -> None:
|
|
323
|
+
vis = self._visible_buttons()
|
|
324
|
+
if vis:
|
|
325
|
+
left_bound = min(b.rect.left for b in vis)
|
|
326
|
+
right_bound = max(b.rect.right for b in vis)
|
|
327
|
+
self.dropdown[0] = (right_bound - left_bound) + self.padding_x * 2
|
|
328
|
+
self.dropdown[1] = self.font.get_height() + self.padding_y * 2
|
|
329
|
+
self.adapt_dropdown = False
|
|
330
|
+
|
|
331
|
+
def _check_buttons_position(self, i: int, text: str = "", first: bool = False) -> tuple[int, int]:
|
|
332
|
+
y = int(self._dropdown_top_y() + self.padding_y)
|
|
333
|
+
if self._last_layout_rect is None or first:
|
|
334
|
+
x = int(self._dropdown_start_x() + self.padding_x)
|
|
335
|
+
else:
|
|
336
|
+
x = int(self._last_layout_rect.right + self.item_spacing * 2)
|
|
337
|
+
return (x, y)
|
|
338
|
+
|
|
339
|
+
def _update_scroll_before_draw(self) -> None:
|
|
340
|
+
self.scroll.rect["rect"].x = self.dropdown_rect.x
|
|
341
|
+
self.scroll.rect["rect"].y = self.dropdown_rect.bottom
|
|
342
|
+
self.scroll.rect["thumb"].y = self.dropdown_rect.bottom
|
|
343
|
+
self.scroll.rect["rect"].width = self.dropdown_rect.width
|
|
344
|
+
self.scroll.rect["thumb"].x = max(
|
|
345
|
+
self.scroll.rect["rect"].left,
|
|
346
|
+
min(self.scroll.rect["thumb"].x, self.scroll.rect["rect"].right - self.scroll.thumb_size),
|
|
347
|
+
)
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
class ComboBoxLeft(ComboBox):
|
|
351
|
+
def get_icon(self) -> str:
|
|
352
|
+
return " <"
|
|
353
|
+
|
|
354
|
+
def _dropdown_top_y(self) -> int:
|
|
355
|
+
return int(self.position[1] - self.padding_y // 2)
|
|
356
|
+
|
|
357
|
+
def _get_rect_dropdown(self) -> pygame.Rect:
|
|
358
|
+
if self.anim_height_dropdown < self.dropdown[0]:
|
|
359
|
+
self.anim_height_dropdown = min(self.dropdown[0], self.anim_height_dropdown + max(1, self.dropdown[0] // 15))
|
|
360
|
+
return pygame.Rect(self.position[0] - self.anim_height_dropdown - 4, self._dropdown_top_y(), self.anim_height_dropdown, self.dropdown[1])
|
|
361
|
+
|
|
362
|
+
def _adapt_size_dropdown(self) -> None:
|
|
363
|
+
vis = self._visible_buttons()
|
|
364
|
+
if vis:
|
|
365
|
+
left_bound = min(b.rect.left for b in vis)
|
|
366
|
+
right_bound = max(b.rect.right for b in vis)
|
|
367
|
+
self.dropdown[0] = (right_bound - left_bound) + self.padding_x * 2
|
|
368
|
+
self.dropdown[1] = self.font.get_height() + self.padding_y * 2
|
|
369
|
+
self.adapt_dropdown = False
|
|
370
|
+
|
|
371
|
+
def _check_buttons_position(self, i: int, text: str = "", first: bool = False) -> tuple[int, int]:
|
|
372
|
+
y = int(self._dropdown_top_y() + self.padding_y)
|
|
373
|
+
if self._last_layout_rect is None or first:
|
|
374
|
+
x = int(self.position[0] - self.font.size(text)[0] - self.padding_x - 4)
|
|
375
|
+
else:
|
|
376
|
+
x = int(self._last_layout_rect.left - self.font.size(text)[0] - self.item_spacing * 2)
|
|
377
|
+
return (x, y)
|
|
378
|
+
|
|
379
|
+
def _update_scroll_before_draw(self) -> None:
|
|
380
|
+
self.scroll.rect["rect"].y = self.dropdown_rect.bottom
|
|
381
|
+
self.scroll.rect["thumb"].y = self.dropdown_rect.bottom
|
|
382
|
+
if self.dropdown_rect.width != self.dropdown[0]:
|
|
383
|
+
self.scroll.rect["rect"].x = self.scroll.rect["thumb"].x = self.dropdown_rect.x
|
|
384
|
+
else:
|
|
385
|
+
self.scroll.rect["rect"].x = self.dropdown_rect.x
|
|
386
|
+
self.scroll.rect["rect"].width = self.dropdown_rect.width
|