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,79 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
from ..Behavior.Mouse_collision import MouseCollision
|
|
3
|
+
from ..Behavior.Element_behavior import ElementBehavior
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class Image(MouseCollision):
|
|
7
|
+
"""Static or hoverable image/sprite element supporting surfaces, file paths, and scaling."""
|
|
8
|
+
|
|
9
|
+
def __init__(self, config: dict) -> None:
|
|
10
|
+
super().__init__(config)
|
|
11
|
+
self.size: tuple[int, int] | None = config.get("size", None)
|
|
12
|
+
self.border_color: tuple | None = config.get("border_color", None)
|
|
13
|
+
self.border: int = config.get("border", 0)
|
|
14
|
+
self.hover_color: tuple = config.get("hover_color", (255, 199, 51))
|
|
15
|
+
self._raw_surface = self._load_surface(config.get("image", config.get("surface", None)))
|
|
16
|
+
self._update_surfaces()
|
|
17
|
+
|
|
18
|
+
def _load_surface(self, source) -> pygame.Surface:
|
|
19
|
+
if isinstance(source, pygame.Surface):
|
|
20
|
+
return source.convert_alpha() if pygame.display.get_init() and pygame.display.get_surface() else source.copy()
|
|
21
|
+
if isinstance(source, str) and source:
|
|
22
|
+
loaded = pygame.image.load(source)
|
|
23
|
+
return loaded.convert_alpha() if pygame.display.get_init() and pygame.display.get_surface() else loaded
|
|
24
|
+
# Fallback placeholder icon surface if no image path/surface provided
|
|
25
|
+
w, h = self.size if self.size else (48, 48)
|
|
26
|
+
surf = pygame.Surface((w, h), pygame.SRCALPHA)
|
|
27
|
+
surf.fill((70, 80, 100, 255))
|
|
28
|
+
pygame.draw.rect(surf, (140, 160, 190), surf.get_rect(), 2)
|
|
29
|
+
pygame.draw.line(surf, (140, 160, 190), (4, h - 8), (w // 2, h // 2), 2)
|
|
30
|
+
pygame.draw.line(surf, (140, 160, 190), (w // 2, h // 2), (w - 4, h - 12), 2)
|
|
31
|
+
return surf
|
|
32
|
+
|
|
33
|
+
def _update_surfaces(self) -> None:
|
|
34
|
+
if self.size:
|
|
35
|
+
self.surface = pygame.transform.smoothscale(self._raw_surface, self.size)
|
|
36
|
+
else:
|
|
37
|
+
self.surface = self._raw_surface
|
|
38
|
+
x, y = int(self.position[0]), int(self.position[1])
|
|
39
|
+
self.rect = pygame.Rect(x, y, *self.surface.get_size())
|
|
40
|
+
|
|
41
|
+
def draw(self) -> None:
|
|
42
|
+
if not self.visible:
|
|
43
|
+
return
|
|
44
|
+
self.screen.blit(self.surface, self.position[:2])
|
|
45
|
+
if self.border > 0 and self.border_color:
|
|
46
|
+
pygame.draw.rect(self.screen, self.border_color, self.rect, self.border)
|
|
47
|
+
if self.detect_mouse:
|
|
48
|
+
self.mouse_collision(self.rect, self.get_mouse_pos(), self.draw_hover_effect)
|
|
49
|
+
|
|
50
|
+
def draw_hover_effect(self) -> None:
|
|
51
|
+
pygame.draw.rect(self.screen, self.hover_color, self.rect, max(2, self.border))
|
|
52
|
+
|
|
53
|
+
def change_item(self, config: dict) -> None:
|
|
54
|
+
if "image" in config or "surface" in config:
|
|
55
|
+
self._raw_surface = self._load_surface(config.get("image", config.get("surface")))
|
|
56
|
+
if "size" in config:
|
|
57
|
+
self.size = config["size"]
|
|
58
|
+
if "position" in config:
|
|
59
|
+
self.position = config["position"]
|
|
60
|
+
self._update_surfaces()
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class ImageButton(Image, ElementBehavior):
|
|
64
|
+
"""Interactive image/sprite button supporting hover, click tint/border, and commands."""
|
|
65
|
+
|
|
66
|
+
def __init__(self, config: dict) -> None:
|
|
67
|
+
Image.__init__(self, config)
|
|
68
|
+
ElementBehavior.__init__(self, config)
|
|
69
|
+
self.pressed_color: tuple = config.get("pressed_color", (135, 206, 235))
|
|
70
|
+
|
|
71
|
+
def draw(self) -> None:
|
|
72
|
+
if not self.visible:
|
|
73
|
+
return
|
|
74
|
+
super().draw()
|
|
75
|
+
if self.pressed:
|
|
76
|
+
self.pressed_button(self.rect, pygame.mouse.get_pressed(), self.get_mouse_pos())
|
|
77
|
+
|
|
78
|
+
def draw_pressed_effect(self) -> None:
|
|
79
|
+
pygame.draw.rect(self.screen, self.pressed_color, self.rect, max(3, self.border + 1))
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
from pygame.locals import *
|
|
3
|
+
from ..Behavior.Element_behavior import ElementBehavior
|
|
4
|
+
class InputText(ElementBehavior):
|
|
5
|
+
def __init__(self,config:dict):
|
|
6
|
+
super().__init__(config)
|
|
7
|
+
self.font = config.get("font", pygame.font.Font(None, 25))
|
|
8
|
+
self.text = config.get("text","")
|
|
9
|
+
self.color=config.get("color",(0,0,0))
|
|
10
|
+
self.color_back=config.get("color_back",(255,255,255))
|
|
11
|
+
self.hover_color = config.get("hover_color", (255, 199, 51))
|
|
12
|
+
self.pressed_color=config.get("pressed_color",(135,206,235))
|
|
13
|
+
self.border_color=config.get("border_color",(127,127,127))
|
|
14
|
+
self.border=config.get("border",2)
|
|
15
|
+
self.rect = pygame.Rect(*self.position)
|
|
16
|
+
self._text_surface = self.font.render(self.text, True, self.color)
|
|
17
|
+
def change_text(self,event):
|
|
18
|
+
if self.states["active"] and event.type==KEYDOWN:
|
|
19
|
+
if event.key == K_BACKSPACE:self.text=self.text[:-1]
|
|
20
|
+
else:self.text+=event.unicode
|
|
21
|
+
self._text_surface = self.font.render(self.text, True, self.color)
|
|
22
|
+
def events(self, event):
|
|
23
|
+
self.change_text(event)
|
|
24
|
+
def draw(self):
|
|
25
|
+
pygame.draw.rect(self.screen,self.color_back,self.rect)
|
|
26
|
+
if self.detect_mouse:self.mouse_collision(self.rect,self.get_mouse_pos())
|
|
27
|
+
if self.pressed:self.pressed_button(self.rect,pygame.mouse.get_pressed(),self.get_mouse_pos())
|
|
28
|
+
input_player=pygame.draw.rect(self.screen,self.border_color,self.rect,self.border)
|
|
29
|
+
self.screen.blit(self._text_surface, (input_player.x+5, input_player.y-2))
|
|
30
|
+
def pressed_button(self, rect, pressed_mouse, mouse_pos, draw=None, repeat: bool = None):
|
|
31
|
+
if pressed_mouse[0] and self.states["presses_touch"]:
|
|
32
|
+
if rect.collidepoint(mouse_pos):
|
|
33
|
+
self.states["active"] = True
|
|
34
|
+
self.states["presses_touch"] = False
|
|
35
|
+
if self.sound_touch: self.sound_touch.play(loops=0)
|
|
36
|
+
else: self.states["active"], self.states["presses_touch"] = False, False
|
|
37
|
+
if not pressed_mouse[0]: self.states["presses_touch"] = True
|
|
38
|
+
if self.states["active"]: self.draw_pressed_effect() if draw is None else draw()
|
|
39
|
+
def draw_hover_effect(self):return pygame.draw.rect(self.screen,self.hover_color,self.rect)
|
|
40
|
+
def draw_pressed_effect(self):return pygame.draw.rect(self.screen,self.pressed_color,self.rect)
|
|
41
|
+
def show_player(self):return self.text
|
|
42
|
+
Input_text = InputText
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
from ..Behavior.Element_behavior import ElementBehavior
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class KeybindButton(ElementBehavior):
|
|
6
|
+
"""Control-mapping button that listens for the next key press when clicked."""
|
|
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.action_label: str = config.get("action_label", config.get("label", ""))
|
|
12
|
+
self.key_code: int = config.get("key", pygame.K_SPACE)
|
|
13
|
+
self.listening: bool = False
|
|
14
|
+
self.color: tuple = config.get("color", (255, 255, 255))
|
|
15
|
+
self.color_back: tuple = config.get("color_back", (50, 55, 68))
|
|
16
|
+
self.listening_color: tuple = config.get("listening_color", (215, 130, 40))
|
|
17
|
+
self.hover_color: tuple = config.get("hover_color", (255, 199, 51))
|
|
18
|
+
self.border_color: tuple = config.get("border_color", (160, 165, 175))
|
|
19
|
+
self.on_change = config.get("on_change", None)
|
|
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", 180), config.get("height", 32))
|
|
24
|
+
self._update_surfaces()
|
|
25
|
+
|
|
26
|
+
def get_key_name(self) -> str:
|
|
27
|
+
return pygame.key.name(self.key_code).upper()
|
|
28
|
+
|
|
29
|
+
def _format_display(self) -> str:
|
|
30
|
+
key_txt = "PRESS KEY..." if self.listening else f"[{self.get_key_name()}]"
|
|
31
|
+
return f"{self.action_label}: {key_txt}" if self.action_label else key_txt
|
|
32
|
+
|
|
33
|
+
def _update_surfaces(self) -> None:
|
|
34
|
+
txt = self._format_display()
|
|
35
|
+
self._text_surface = self.font.render(txt, True, self.color)
|
|
36
|
+
self._hover_surface = self.font.render(txt, True, self.hover_color)
|
|
37
|
+
|
|
38
|
+
def set_key(self, key_code: int) -> None:
|
|
39
|
+
self.key_code = key_code
|
|
40
|
+
self.listening = False
|
|
41
|
+
self._update_surfaces()
|
|
42
|
+
if callable(self.on_change):
|
|
43
|
+
self.on_change(self.key_code)
|
|
44
|
+
self.execute_commands(self.key_code)
|
|
45
|
+
|
|
46
|
+
def events(self, event: pygame.event.Event) -> bool:
|
|
47
|
+
if not self.visible or not self.enabled:
|
|
48
|
+
return False
|
|
49
|
+
if self.listening and event.type == pygame.KEYDOWN:
|
|
50
|
+
if event.key == pygame.K_ESCAPE:
|
|
51
|
+
self.listening = False
|
|
52
|
+
self._update_surfaces()
|
|
53
|
+
else:
|
|
54
|
+
self.set_key(event.key)
|
|
55
|
+
return True
|
|
56
|
+
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
|
|
57
|
+
if self.rect.collidepoint(event.pos):
|
|
58
|
+
if self.sound_touch:
|
|
59
|
+
self.sound_touch.play(loops=0)
|
|
60
|
+
self.listening = not self.listening
|
|
61
|
+
self._update_surfaces()
|
|
62
|
+
return True
|
|
63
|
+
elif self.listening:
|
|
64
|
+
self.listening = False
|
|
65
|
+
self._update_surfaces()
|
|
66
|
+
return False
|
|
67
|
+
|
|
68
|
+
def draw(self) -> None:
|
|
69
|
+
if not self.visible:
|
|
70
|
+
return
|
|
71
|
+
bg = self.listening_color if self.listening else self.color_back
|
|
72
|
+
pygame.draw.rect(self.screen, bg, self.rect, border_radius=4)
|
|
73
|
+
pygame.draw.rect(self.screen, self.border_color, self.rect, 2, border_radius=4)
|
|
74
|
+
tx = self.rect.centerx - self._text_surface.get_width() // 2
|
|
75
|
+
ty = self.rect.centery - self._text_surface.get_height() // 2
|
|
76
|
+
self.screen.blit(self._text_surface, (tx, ty))
|
|
77
|
+
if self.detect_mouse:
|
|
78
|
+
self.mouse_collision(self.rect, self.get_mouse_pos(), self.draw_hover_effect)
|
|
79
|
+
|
|
80
|
+
def draw_hover_effect(self) -> None:
|
|
81
|
+
pygame.draw.rect(self.screen, self.hover_color, self.rect, 2, border_radius=4)
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
from ..Behavior.Element_behavior import ElementBehavior
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class NumberSpinner(ElementBehavior):
|
|
6
|
+
"""Numeric selector with decrement (-) and increment (+) buttons and display box."""
|
|
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.min_value: float = float(config.get("min_value", 0))
|
|
12
|
+
self.max_value: float = float(config.get("max_value", 99))
|
|
13
|
+
self.step: float = float(config.get("step", 1))
|
|
14
|
+
self.value: float = max(self.min_value, min(self.max_value, float(config.get("value", self.min_value))))
|
|
15
|
+
self.color: tuple = config.get("color", (255, 255, 255))
|
|
16
|
+
self.color_back: tuple = config.get("color_back", (45, 48, 56))
|
|
17
|
+
self.button_color: tuple = config.get("button_color", (65, 72, 86))
|
|
18
|
+
self.hover_color: tuple = config.get("hover_color", (255, 199, 51))
|
|
19
|
+
self.border_color: tuple = config.get("border_color", (160, 165, 175))
|
|
20
|
+
self.on_change = config.get("on_change", None)
|
|
21
|
+
|
|
22
|
+
if len(self.position) >= 4:
|
|
23
|
+
x, y, w, h = [int(v) for v in self.position[:4]]
|
|
24
|
+
else:
|
|
25
|
+
x, y = int(self.position[0]), int(self.position[1])
|
|
26
|
+
w, h = config.get("width", 120), config.get("height", 30)
|
|
27
|
+
|
|
28
|
+
btn_w = max(24, h)
|
|
29
|
+
self.minus_rect = pygame.Rect(x, y, btn_w, h)
|
|
30
|
+
self.value_rect = pygame.Rect(x + btn_w, y, max(36, w - btn_w * 2), h)
|
|
31
|
+
self.plus_rect = pygame.Rect(self.value_rect.right, y, btn_w, h)
|
|
32
|
+
self.rect = pygame.Rect(x, y, self.plus_rect.right - x, h)
|
|
33
|
+
self._minus_surf = self.font.render("-", True, self.color)
|
|
34
|
+
self._plus_surf = self.font.render("+", True, self.color)
|
|
35
|
+
self._update_surfaces()
|
|
36
|
+
|
|
37
|
+
def _format_val(self) -> str:
|
|
38
|
+
if self.step.is_integer() and self.value.is_integer():
|
|
39
|
+
return str(int(self.value))
|
|
40
|
+
return f"{self.value:.2f}"
|
|
41
|
+
|
|
42
|
+
def _update_surfaces(self) -> None:
|
|
43
|
+
self._val_surf = self.font.render(self._format_val(), True, self.color)
|
|
44
|
+
|
|
45
|
+
def move(self, dx: int, dy: int) -> None:
|
|
46
|
+
super().move(dx, dy)
|
|
47
|
+
self.minus_rect.move_ip(dx, dy)
|
|
48
|
+
self.value_rect.move_ip(dx, dy)
|
|
49
|
+
self.plus_rect.move_ip(dx, dy)
|
|
50
|
+
|
|
51
|
+
def set_value(self, val: float) -> None:
|
|
52
|
+
clamped = max(self.min_value, min(self.max_value, float(val)))
|
|
53
|
+
if abs(clamped - self.value) > 1e-6:
|
|
54
|
+
self.value = clamped
|
|
55
|
+
self._update_surfaces()
|
|
56
|
+
if callable(self.on_change):
|
|
57
|
+
self.on_change(self.value)
|
|
58
|
+
self.execute_commands(self.value)
|
|
59
|
+
|
|
60
|
+
def get_value(self) -> float:
|
|
61
|
+
return int(self.value) if (self.step.is_integer() and self.value.is_integer()) else self.value
|
|
62
|
+
|
|
63
|
+
def increment(self) -> None:
|
|
64
|
+
self.set_value(self.value + self.step)
|
|
65
|
+
|
|
66
|
+
def decrement(self) -> None:
|
|
67
|
+
self.set_value(self.value - self.step)
|
|
68
|
+
|
|
69
|
+
def events(self, event: pygame.event.Event) -> bool:
|
|
70
|
+
if not self.visible or not self.enabled:
|
|
71
|
+
return False
|
|
72
|
+
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
|
|
73
|
+
if self.minus_rect.collidepoint(event.pos):
|
|
74
|
+
if self.sound_touch:
|
|
75
|
+
self.sound_touch.play(loops=0)
|
|
76
|
+
self.decrement()
|
|
77
|
+
return True
|
|
78
|
+
if self.plus_rect.collidepoint(event.pos):
|
|
79
|
+
if self.sound_touch:
|
|
80
|
+
self.sound_touch.play(loops=0)
|
|
81
|
+
self.increment()
|
|
82
|
+
return True
|
|
83
|
+
return False
|
|
84
|
+
|
|
85
|
+
def draw(self) -> None:
|
|
86
|
+
if not self.visible:
|
|
87
|
+
return
|
|
88
|
+
mouse_pos = self.get_mouse_pos()
|
|
89
|
+
pygame.draw.rect(self.screen, self.button_color, self.minus_rect, border_top_left_radius=4, border_bottom_left_radius=4)
|
|
90
|
+
pygame.draw.rect(self.screen, self.color_back, self.value_rect)
|
|
91
|
+
pygame.draw.rect(self.screen, self.button_color, self.plus_rect, border_top_right_radius=4, border_bottom_right_radius=4)
|
|
92
|
+
pygame.draw.rect(self.screen, self.border_color, self.rect, 2, border_radius=4)
|
|
93
|
+
|
|
94
|
+
if self.detect_mouse:
|
|
95
|
+
if self.minus_rect.collidepoint(mouse_pos):
|
|
96
|
+
pygame.draw.rect(self.screen, self.hover_color, self.minus_rect, 2, border_top_left_radius=4, border_bottom_left_radius=4)
|
|
97
|
+
elif self.plus_rect.collidepoint(mouse_pos):
|
|
98
|
+
pygame.draw.rect(self.screen, self.hover_color, self.plus_rect, 2, border_top_right_radius=4, border_bottom_right_radius=4)
|
|
99
|
+
|
|
100
|
+
self.screen.blit(self._minus_surf, (self.minus_rect.centerx - self._minus_surf.get_width() // 2, self.minus_rect.centery - self._minus_surf.get_height() // 2))
|
|
101
|
+
self.screen.blit(self._val_surf, (self.value_rect.centerx - self._val_surf.get_width() // 2, self.value_rect.centery - self._val_surf.get_height() // 2))
|
|
102
|
+
self.screen.blit(self._plus_surf, (self.plus_rect.centerx - self._plus_surf.get_width() // 2, self.plus_rect.centery - self._plus_surf.get_height() // 2))
|
|
103
|
+
|
|
104
|
+
def draw_hover_effect(self) -> None:
|
|
105
|
+
pass
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
from ..Behavior.Element_behavior import ElementBehavior
|
|
3
|
+
class PolygonButton(ElementBehavior):
|
|
4
|
+
def __init__(self,config:dict):
|
|
5
|
+
super().__init__(config)
|
|
6
|
+
self.hover_position = config.get("hover_position",self.position)
|
|
7
|
+
self.color = config.get("color", (255, 255, 255))
|
|
8
|
+
self.hover_color = config.get("hover_color", (255, 199, 51))
|
|
9
|
+
self.rect = pygame.draw.polygon(self.screen, self.color, self.position).copy()
|
|
10
|
+
def draw(self):
|
|
11
|
+
pygame.draw.polygon(self.screen, self.color, self.position)
|
|
12
|
+
if self.detect_mouse:self.mouse_collision(self.rect,self.get_mouse_pos())
|
|
13
|
+
if self.pressed:self.pressed_button(self.rect,pygame.mouse.get_pressed(),self.get_mouse_pos())
|
|
14
|
+
def draw_hover_effect(self):return pygame.draw.polygon(self.screen, self.hover_color, self.hover_position)
|
|
15
|
+
def change_item(self,config:dict):
|
|
16
|
+
self.color=config.get("color",self.color)
|
|
17
|
+
self.detect_mouse=config.get("detect_mouse",self.detect_mouse)
|
|
18
|
+
self.pressed=config.get("pressed",self.pressed)
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
from ..Behavior.Mouse_collision import MouseCollision
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class ProgressBar(MouseCollision):
|
|
6
|
+
"""HUD / Loading progress bar with smooth interpolation and optional text overlay."""
|
|
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, 20))
|
|
11
|
+
self.min_value: float = float(config.get("min_value", 0.0))
|
|
12
|
+
self.max_value: float = float(config.get("max_value", 100.0))
|
|
13
|
+
self.value: float = float(config.get("value", self.max_value))
|
|
14
|
+
self._display_value: float = self.value
|
|
15
|
+
self.smooth_speed: float = float(config.get("smooth_speed", 0.2))
|
|
16
|
+
self.color: tuple = config.get("color", (255, 255, 255))
|
|
17
|
+
self.color_back: tuple = config.get("color_back", (40, 44, 52))
|
|
18
|
+
self.fill_color: tuple = config.get("fill_color", (75, 200, 110))
|
|
19
|
+
self.border_color: tuple = config.get("border_color", (180, 185, 195))
|
|
20
|
+
self.border: int = config.get("border", 2)
|
|
21
|
+
self.border_radius: int = config.get("border_radius", 5)
|
|
22
|
+
self.show_text: bool = config.get("show_text", True)
|
|
23
|
+
self.show_percentage: bool = config.get("show_percentage", False)
|
|
24
|
+
self.label: str = config.get("label", config.get("text", ""))
|
|
25
|
+
if len(self.position) >= 4:
|
|
26
|
+
self.rect = pygame.Rect(*self.position[:4])
|
|
27
|
+
else:
|
|
28
|
+
self.rect = pygame.Rect(self.position[0], self.position[1], config.get("width", 200), config.get("height", 24))
|
|
29
|
+
self._update_surfaces()
|
|
30
|
+
|
|
31
|
+
def _format_text(self) -> str:
|
|
32
|
+
if not self.show_text:
|
|
33
|
+
return ""
|
|
34
|
+
span = max(1e-6, self.max_value - self.min_value)
|
|
35
|
+
pct = int(round(((self.value - self.min_value) / span) * 100))
|
|
36
|
+
if self.show_percentage:
|
|
37
|
+
val_part = f"{pct}%"
|
|
38
|
+
else:
|
|
39
|
+
val_part = f"{int(round(self.value))}/{int(round(self.max_value))}"
|
|
40
|
+
return f"{self.label}: {val_part}" if self.label else val_part
|
|
41
|
+
|
|
42
|
+
def _update_surfaces(self) -> None:
|
|
43
|
+
txt = self._format_text()
|
|
44
|
+
self._text_surface = self.font.render(txt, True, self.color) if txt else None
|
|
45
|
+
|
|
46
|
+
def set_value(self, value: float) -> None:
|
|
47
|
+
clamped = max(self.min_value, min(self.max_value, float(value)))
|
|
48
|
+
if abs(clamped - self.value) > 1e-6:
|
|
49
|
+
self.value = clamped
|
|
50
|
+
self._update_surfaces()
|
|
51
|
+
|
|
52
|
+
def get_value(self) -> float:
|
|
53
|
+
return self.value
|
|
54
|
+
|
|
55
|
+
def events(self, event: pygame.event.Event) -> bool:
|
|
56
|
+
return False
|
|
57
|
+
|
|
58
|
+
def draw(self) -> None:
|
|
59
|
+
if not self.visible:
|
|
60
|
+
return
|
|
61
|
+
if abs(self._display_value - self.value) > 0.05:
|
|
62
|
+
self._display_value += (self.value - self._display_value) * self.smooth_speed
|
|
63
|
+
else:
|
|
64
|
+
self._display_value = self.value
|
|
65
|
+
|
|
66
|
+
pygame.draw.rect(self.screen, self.color_back, self.rect, border_radius=self.border_radius)
|
|
67
|
+
|
|
68
|
+
span = self.max_value - self.min_value
|
|
69
|
+
ratio = max(0.0, min(1.0, (self._display_value - self.min_value) / span)) if span > 0 else 0.0
|
|
70
|
+
fill_w = int(self.rect.width * ratio)
|
|
71
|
+
if fill_w > 0:
|
|
72
|
+
fill_rect = pygame.Rect(self.rect.x, self.rect.y, fill_w, self.rect.height)
|
|
73
|
+
pygame.draw.rect(self.screen, self.fill_color, fill_rect, border_radius=self.border_radius)
|
|
74
|
+
|
|
75
|
+
if self.border > 0:
|
|
76
|
+
pygame.draw.rect(self.screen, self.border_color, self.rect, self.border, border_radius=self.border_radius)
|
|
77
|
+
|
|
78
|
+
if self._text_surface:
|
|
79
|
+
tx = self.rect.centerx - self._text_surface.get_width() // 2
|
|
80
|
+
ty = self.rect.centery - self._text_surface.get_height() // 2
|
|
81
|
+
self.screen.blit(self._text_surface, (tx, ty))
|
|
82
|
+
|
|
83
|
+
def draw_hover_effect(self) -> None:
|
|
84
|
+
pass
|
|
85
|
+
|
|
86
|
+
def change_item(self, config: dict) -> None:
|
|
87
|
+
if "value" in config:
|
|
88
|
+
self.set_value(config["value"])
|
|
89
|
+
if "max_value" in config:
|
|
90
|
+
self.max_value = float(config["max_value"])
|
|
91
|
+
if "fill_color" in config:
|
|
92
|
+
self.fill_color = config["fill_color"]
|
|
93
|
+
if "label" in config or "text" in config:
|
|
94
|
+
self.label = config.get("label", config.get("text", self.label))
|
|
95
|
+
self._update_surfaces()
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
from ..Behavior.Element_behavior import ElementBehavior
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class RadioGroup:
|
|
6
|
+
"""Controller managing mutually exclusive RadioButton instances."""
|
|
7
|
+
|
|
8
|
+
def __init__(self, config: dict | None = None) -> None:
|
|
9
|
+
config = config or {}
|
|
10
|
+
self.buttons: list["RadioButton"] = []
|
|
11
|
+
self.selected: "RadioButton | None" = None
|
|
12
|
+
self.on_change = config.get("on_change", None)
|
|
13
|
+
|
|
14
|
+
def add(self, button: "RadioButton") -> "RadioButton":
|
|
15
|
+
if button not in self.buttons:
|
|
16
|
+
self.buttons.append(button)
|
|
17
|
+
button.group = self
|
|
18
|
+
if button.selected or self.selected is None:
|
|
19
|
+
self.select(button, trigger_callback=False)
|
|
20
|
+
return button
|
|
21
|
+
|
|
22
|
+
def select(self, target: "RadioButton", trigger_callback: bool = True) -> None:
|
|
23
|
+
changed = self.selected is not target
|
|
24
|
+
self.selected = target
|
|
25
|
+
for btn in self.buttons:
|
|
26
|
+
btn.selected = (btn is target)
|
|
27
|
+
if changed and trigger_callback:
|
|
28
|
+
if callable(self.on_change):
|
|
29
|
+
self.on_change(self.get_value())
|
|
30
|
+
|
|
31
|
+
def get_value(self):
|
|
32
|
+
if self.selected is None:
|
|
33
|
+
return None
|
|
34
|
+
return self.selected.value if self.selected.value is not None else self.selected.text
|
|
35
|
+
|
|
36
|
+
def events(self, event: pygame.event.Event) -> bool:
|
|
37
|
+
for btn in self.buttons:
|
|
38
|
+
if btn.events(event):
|
|
39
|
+
return True
|
|
40
|
+
return False
|
|
41
|
+
|
|
42
|
+
def draw(self) -> None:
|
|
43
|
+
for btn in self.buttons:
|
|
44
|
+
btn.draw()
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class RadioButton(ElementBehavior):
|
|
48
|
+
"""Single circular radio option button that can belong to a RadioGroup."""
|
|
49
|
+
|
|
50
|
+
def __init__(self, config: dict) -> None:
|
|
51
|
+
super().__init__(config)
|
|
52
|
+
self.font: pygame.font.Font = config.get("font", pygame.font.Font(None, 25))
|
|
53
|
+
self.text: str = config.get("text", "")
|
|
54
|
+
self.value = config.get("value", self.text)
|
|
55
|
+
self.radius: int = config.get("radius", 10)
|
|
56
|
+
self.selected: bool = config.get("selected", False)
|
|
57
|
+
self.color: tuple = config.get("color", (255, 255, 255))
|
|
58
|
+
self.color_back: tuple = config.get("color_back", (45, 48, 56))
|
|
59
|
+
self.active_color: tuple = config.get("active_color", (85, 170, 255))
|
|
60
|
+
self.hover_color: tuple = config.get("hover_color", (255, 199, 51))
|
|
61
|
+
self.border_color: tuple = config.get("border_color", (180, 185, 195))
|
|
62
|
+
self.spacing: int = config.get("spacing", 8)
|
|
63
|
+
self.group: RadioGroup | None = None
|
|
64
|
+
self._update_surfaces()
|
|
65
|
+
grp = config.get("group", None)
|
|
66
|
+
if isinstance(grp, RadioGroup):
|
|
67
|
+
grp.add(self)
|
|
68
|
+
|
|
69
|
+
def _update_surfaces(self) -> None:
|
|
70
|
+
x, y = int(self.position[0]), int(self.position[1])
|
|
71
|
+
diameter = self.radius * 2
|
|
72
|
+
self.circle_rect = pygame.Rect(x, y, diameter, diameter)
|
|
73
|
+
self._label_surface = self.font.render(self.text, True, self.color) if self.text else None
|
|
74
|
+
self._label_hover_surface = self.font.render(self.text, True, self.hover_color) if self.text else None
|
|
75
|
+
if self._label_surface:
|
|
76
|
+
label_y = y + (diameter - self._label_surface.get_height()) // 2
|
|
77
|
+
self.label_pos = (x + diameter + self.spacing, label_y)
|
|
78
|
+
total_w = diameter + self.spacing + self._label_surface.get_width()
|
|
79
|
+
total_h = max(diameter, self._label_surface.get_height())
|
|
80
|
+
self.rect = pygame.Rect(x, min(y, label_y), total_w, total_h)
|
|
81
|
+
else:
|
|
82
|
+
self.label_pos = (x, y)
|
|
83
|
+
self.rect = self.circle_rect.copy()
|
|
84
|
+
|
|
85
|
+
def move(self, dx: int, dy: int) -> None:
|
|
86
|
+
super().move(dx, dy)
|
|
87
|
+
self.circle_rect.x += dx
|
|
88
|
+
self.circle_rect.y += dy
|
|
89
|
+
self.label_pos = (self.label_pos[0] + dx, self.label_pos[1] + dy)
|
|
90
|
+
|
|
91
|
+
def select(self) -> None:
|
|
92
|
+
if self.group is not None:
|
|
93
|
+
self.group.select(self, trigger_callback=True)
|
|
94
|
+
else:
|
|
95
|
+
self.selected = True
|
|
96
|
+
self.execute_commands(self.value)
|
|
97
|
+
|
|
98
|
+
def events(self, event: pygame.event.Event) -> bool:
|
|
99
|
+
if not self.visible or not self.enabled:
|
|
100
|
+
return False
|
|
101
|
+
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
|
|
102
|
+
if self.rect.collidepoint(event.pos):
|
|
103
|
+
if self.sound_touch:
|
|
104
|
+
self.sound_touch.play(loops=0)
|
|
105
|
+
self.select()
|
|
106
|
+
return True
|
|
107
|
+
return False
|
|
108
|
+
|
|
109
|
+
def draw(self) -> None:
|
|
110
|
+
if not self.visible:
|
|
111
|
+
return
|
|
112
|
+
center = self.circle_rect.center
|
|
113
|
+
pygame.draw.circle(self.screen, self.color_back, center, self.radius)
|
|
114
|
+
pygame.draw.circle(self.screen, self.border_color, center, self.radius, 2)
|
|
115
|
+
|
|
116
|
+
if self._label_surface:
|
|
117
|
+
self.screen.blit(self._label_surface, self.label_pos)
|
|
118
|
+
|
|
119
|
+
if self.detect_mouse:
|
|
120
|
+
self.mouse_collision(self.rect, self.get_mouse_pos(), self.draw_hover_effect)
|
|
121
|
+
|
|
122
|
+
if self.selected:
|
|
123
|
+
pygame.draw.circle(self.screen, self.active_color, center, max(3, self.radius - 4))
|
|
124
|
+
|
|
125
|
+
def draw_hover_effect(self) -> None:
|
|
126
|
+
pygame.draw.circle(self.screen, self.hover_color, self.circle_rect.center, self.radius, 2)
|
|
127
|
+
if self._label_hover_surface:
|
|
128
|
+
self.screen.blit(self._label_hover_surface, self.label_pos)
|