vali-ui 1.1.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.
- vali/__init__.py +24 -0
- vali/core/__init__.py +0 -0
- vali/core/app.py +49 -0
- vali/core/events.py +25 -0
- vali/core/window.py +155 -0
- vali/graphics/__init__.py +2 -0
- vali/graphics/animation.py +42 -0
- vali/layouts/__init__.py +2 -0
- vali/layouts/basic.py +151 -0
- vali/screens/__init__.py +2 -0
- vali/screens/screen.py +49 -0
- vali/themes/__init__.py +14 -0
- vali/utils/__init__.py +2 -0
- vali/utils/resources.py +27 -0
- vali/widgets/__init__.py +10 -0
- vali/widgets/base.py +27 -0
- vali/widgets/button.py +50 -0
- vali/widgets/display.py +80 -0
- vali/widgets/inputs.py +295 -0
- vali/widgets/label.py +33 -0
- vali_ui-1.1.0.dist-info/METADATA +145 -0
- vali_ui-1.1.0.dist-info/RECORD +25 -0
- vali_ui-1.1.0.dist-info/WHEEL +5 -0
- vali_ui-1.1.0.dist-info/licenses/LICENSE +17 -0
- vali_ui-1.1.0.dist-info/top_level.txt +1 -0
vali/__init__.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"""VALI: a lightweight, beginner-friendly Python desktop UI framework."""
|
|
2
|
+
|
|
3
|
+
from .core.app import App
|
|
4
|
+
from .core.window import Window
|
|
5
|
+
from .core.events import Event
|
|
6
|
+
from .widgets import (
|
|
7
|
+
Widget, Label, Button, TextInput, Checkbox, Switch, Slider,
|
|
8
|
+
Dropdown, RadioGroup, Image, ProgressBar, Panel,
|
|
9
|
+
)
|
|
10
|
+
from .layouts import Layout, VerticalLayout, HorizontalLayout, GridLayout, AnchorLayout
|
|
11
|
+
from .screens import Screen, ScreenManager
|
|
12
|
+
from .themes import Theme, DARK, LIGHT
|
|
13
|
+
from .graphics import Animation, fade, slide
|
|
14
|
+
from .utils import ResourceManager
|
|
15
|
+
|
|
16
|
+
__version__ = "1.1.0"
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
"App", "Window", "Event", "Widget", "Label", "Button", "TextInput",
|
|
20
|
+
"Checkbox", "Switch", "Slider", "Dropdown", "RadioGroup", "Image",
|
|
21
|
+
"ProgressBar", "Panel", "Layout", "VerticalLayout", "HorizontalLayout",
|
|
22
|
+
"GridLayout", "AnchorLayout", "Screen", "ScreenManager", "Theme", "DARK",
|
|
23
|
+
"LIGHT", "Animation", "fade", "slide", "ResourceManager", "__version__",
|
|
24
|
+
]
|
vali/core/__init__.py
ADDED
|
File without changes
|
vali/core/app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
from .window import Window
|
|
2
|
+
from ..screens import ScreenManager
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class App:
|
|
6
|
+
"""Main application object. Owns one window and a screen manager."""
|
|
7
|
+
|
|
8
|
+
def __init__(self):
|
|
9
|
+
self.window = None
|
|
10
|
+
self.screens = ScreenManager()
|
|
11
|
+
|
|
12
|
+
def create_window(self, title="VALI Application", width=800, height=600, *,
|
|
13
|
+
size=None, icon=None, background=None, theme="light",
|
|
14
|
+
resizable=False, fps=60, min_size=(320, 240), vsync=False):
|
|
15
|
+
"""Create the application window.
|
|
16
|
+
|
|
17
|
+
Supports both create_window(title, 800, 600) and the older
|
|
18
|
+
create_window(title, size=(800, 600)) form.
|
|
19
|
+
"""
|
|
20
|
+
if size is not None:
|
|
21
|
+
width, height = size
|
|
22
|
+
if self.window is not None:
|
|
23
|
+
self.window.close()
|
|
24
|
+
self.window = Window(
|
|
25
|
+
title, (width, height), icon=icon, background=background,
|
|
26
|
+
theme=theme, resizable=resizable, fps=fps,
|
|
27
|
+
screen_manager=self.screens, min_size=min_size, vsync=vsync,
|
|
28
|
+
)
|
|
29
|
+
return self.window
|
|
30
|
+
|
|
31
|
+
def add(self, widget):
|
|
32
|
+
if self.window is None:
|
|
33
|
+
raise RuntimeError("Call create_window() before app.add().")
|
|
34
|
+
return self.window.add(widget)
|
|
35
|
+
|
|
36
|
+
def run(self):
|
|
37
|
+
if self.window is None:
|
|
38
|
+
raise RuntimeError("No window has been created.")
|
|
39
|
+
self.window.run()
|
|
40
|
+
|
|
41
|
+
def quit(self):
|
|
42
|
+
if self.window is not None:
|
|
43
|
+
self.window.quit()
|
|
44
|
+
|
|
45
|
+
def set_theme(self, theme):
|
|
46
|
+
if self.window is None:
|
|
47
|
+
raise RuntimeError("Create a window before setting its theme.")
|
|
48
|
+
self.window.set_theme(theme)
|
|
49
|
+
return self
|
vali/core/events.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
class Event:
|
|
2
|
+
"""A small callback collection."""
|
|
3
|
+
|
|
4
|
+
def __init__(self):
|
|
5
|
+
self._callbacks = []
|
|
6
|
+
|
|
7
|
+
def connect(self, callback):
|
|
8
|
+
if not callable(callback):
|
|
9
|
+
raise TypeError("callback must be callable")
|
|
10
|
+
if callback not in self._callbacks:
|
|
11
|
+
self._callbacks.append(callback)
|
|
12
|
+
return callback
|
|
13
|
+
|
|
14
|
+
def disconnect(self, callback):
|
|
15
|
+
self._callbacks.remove(callback)
|
|
16
|
+
|
|
17
|
+
def emit(self, *args, **kwargs):
|
|
18
|
+
for callback in tuple(self._callbacks):
|
|
19
|
+
callback(*args, **kwargs)
|
|
20
|
+
|
|
21
|
+
def clear(self):
|
|
22
|
+
self._callbacks.clear()
|
|
23
|
+
|
|
24
|
+
def __len__(self):
|
|
25
|
+
return len(self._callbacks)
|
vali/core/window.py
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
import pygame
|
|
3
|
+
from ..widgets.base import Widget
|
|
4
|
+
from ..themes import DARK, LIGHT, Theme
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def _default_icon():
|
|
8
|
+
surface = pygame.Surface((32, 32), pygame.SRCALPHA)
|
|
9
|
+
pygame.draw.rect(surface, (35, 43, 58), (1, 1, 30, 30), border_radius=8)
|
|
10
|
+
pygame.draw.line(surface, (255, 255, 255), (8, 8), (15, 24), 4)
|
|
11
|
+
pygame.draw.line(surface, (255, 255, 255), (15, 24), (24, 8), 4)
|
|
12
|
+
return surface
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class Window:
|
|
16
|
+
"""Desktop window. Pygame is an implementation detail of VALI."""
|
|
17
|
+
|
|
18
|
+
def __init__(self, title, size, *, icon=None, background=None,
|
|
19
|
+
theme="light", resizable=False, fps=60, screen_manager=None,
|
|
20
|
+
min_size=(320, 240), vsync=False):
|
|
21
|
+
if not isinstance(size, (tuple, list)) or len(size) != 2:
|
|
22
|
+
raise ValueError("size must be a (width, height) pair")
|
|
23
|
+
if int(size[0]) <= 0 or int(size[1]) <= 0:
|
|
24
|
+
raise ValueError("window dimensions must be positive")
|
|
25
|
+
if int(fps) <= 0:
|
|
26
|
+
raise ValueError("fps must be positive")
|
|
27
|
+
if len(min_size) != 2 or min(min_size) <= 0:
|
|
28
|
+
raise ValueError("min_size must contain two positive values")
|
|
29
|
+
pygame.init()
|
|
30
|
+
self.title = str(title)
|
|
31
|
+
self.width, self.height = int(size[0]), int(size[1])
|
|
32
|
+
self.fps = int(fps)
|
|
33
|
+
self.min_size = tuple(map(int, min_size))
|
|
34
|
+
self.vsync = bool(vsync)
|
|
35
|
+
self.running = False
|
|
36
|
+
self.widgets = []
|
|
37
|
+
self.screen_manager = screen_manager
|
|
38
|
+
self.theme = self._resolve_theme(theme)
|
|
39
|
+
self._custom_background = background is not None
|
|
40
|
+
self.background = background if background is not None else self.theme.background
|
|
41
|
+
self._resizable = bool(resizable)
|
|
42
|
+
flags = pygame.RESIZABLE if self._resizable else 0
|
|
43
|
+
self.surface = pygame.display.set_mode((self.width, self.height), flags, vsync=int(self.vsync))
|
|
44
|
+
pygame.display.set_caption(self.title)
|
|
45
|
+
self.set_icon(icon)
|
|
46
|
+
self.clock = pygame.time.Clock()
|
|
47
|
+
self.closed = False
|
|
48
|
+
|
|
49
|
+
@staticmethod
|
|
50
|
+
def _resolve_theme(theme):
|
|
51
|
+
if isinstance(theme, Theme):
|
|
52
|
+
return theme
|
|
53
|
+
if theme == "dark":
|
|
54
|
+
return DARK
|
|
55
|
+
if theme == "light":
|
|
56
|
+
return LIGHT
|
|
57
|
+
raise ValueError("theme must be 'light', 'dark', or a Theme instance")
|
|
58
|
+
|
|
59
|
+
def set_theme(self, theme):
|
|
60
|
+
self.theme = self._resolve_theme(theme)
|
|
61
|
+
if not self._custom_background:
|
|
62
|
+
self.background = self.theme.background
|
|
63
|
+
return self
|
|
64
|
+
|
|
65
|
+
def set_background(self, color):
|
|
66
|
+
if not isinstance(color, (tuple, list)) or len(color) not in (3, 4):
|
|
67
|
+
raise ValueError("background must be an RGB or RGBA color")
|
|
68
|
+
self.background = tuple(color)
|
|
69
|
+
self._custom_background = True
|
|
70
|
+
return self
|
|
71
|
+
|
|
72
|
+
def set_icon(self, icon=None):
|
|
73
|
+
if icon is None:
|
|
74
|
+
image = _default_icon()
|
|
75
|
+
elif isinstance(icon, pygame.Surface):
|
|
76
|
+
image = icon
|
|
77
|
+
else:
|
|
78
|
+
path = Path(icon).expanduser()
|
|
79
|
+
if not path.is_file():
|
|
80
|
+
raise FileNotFoundError(f"Window icon not found: {path}")
|
|
81
|
+
image = pygame.image.load(str(path))
|
|
82
|
+
if image.get_width() <= 0 or image.get_height() <= 0:
|
|
83
|
+
raise ValueError("icon image is empty")
|
|
84
|
+
try:
|
|
85
|
+
image = image.convert_alpha()
|
|
86
|
+
except pygame.error:
|
|
87
|
+
image = image.copy()
|
|
88
|
+
pygame.display.set_icon(pygame.transform.smoothscale(image, (32, 32)))
|
|
89
|
+
return self
|
|
90
|
+
|
|
91
|
+
def add(self, *widgets):
|
|
92
|
+
for widget in widgets:
|
|
93
|
+
if not isinstance(widget, Widget) and not all(hasattr(widget, name) for name in ("draw", "handle_event", "visible", "enabled")):
|
|
94
|
+
raise TypeError("window.add() expects a VALI widget or layout")
|
|
95
|
+
target = self.screen_manager.current if self.screen_manager else None
|
|
96
|
+
if target is not None:
|
|
97
|
+
target.add(widget)
|
|
98
|
+
else:
|
|
99
|
+
widget.parent = self
|
|
100
|
+
self.widgets.append(widget)
|
|
101
|
+
return widgets[0] if len(widgets) == 1 else widgets
|
|
102
|
+
|
|
103
|
+
def remove(self, widget):
|
|
104
|
+
target = self.screen_manager.current if self.screen_manager else None
|
|
105
|
+
collection = target.children if target is not None else self.widgets
|
|
106
|
+
collection.remove(widget)
|
|
107
|
+
widget.parent = None
|
|
108
|
+
return widget
|
|
109
|
+
|
|
110
|
+
def quit(self):
|
|
111
|
+
self.running = False
|
|
112
|
+
|
|
113
|
+
def close(self):
|
|
114
|
+
self.quit()
|
|
115
|
+
if not self.closed and pygame.get_init():
|
|
116
|
+
pygame.quit()
|
|
117
|
+
self.closed = True
|
|
118
|
+
|
|
119
|
+
def _active_widgets(self):
|
|
120
|
+
current = self.screen_manager.current if self.screen_manager else None
|
|
121
|
+
return current.children if current is not None else self.widgets
|
|
122
|
+
|
|
123
|
+
def run(self):
|
|
124
|
+
if self.closed:
|
|
125
|
+
raise RuntimeError("This window has been closed. Create a new window.")
|
|
126
|
+
self.running = True
|
|
127
|
+
try:
|
|
128
|
+
while self.running:
|
|
129
|
+
targets = tuple(self._active_widgets())
|
|
130
|
+
for event in pygame.event.get():
|
|
131
|
+
if event.type == pygame.QUIT:
|
|
132
|
+
self.quit()
|
|
133
|
+
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
|
|
134
|
+
self.quit()
|
|
135
|
+
elif event.type == pygame.VIDEORESIZE and self._resizable:
|
|
136
|
+
self.width = max(self.min_size[0], event.w)
|
|
137
|
+
self.height = max(self.min_size[1], event.h)
|
|
138
|
+
self.surface = pygame.display.set_mode(
|
|
139
|
+
(self.width, self.height), pygame.RESIZABLE,
|
|
140
|
+
vsync=int(self.vsync),
|
|
141
|
+
)
|
|
142
|
+
else:
|
|
143
|
+
for widget in targets:
|
|
144
|
+
if getattr(widget, "visible", True) and getattr(widget, "enabled", True):
|
|
145
|
+
widget.handle_event(event)
|
|
146
|
+
if not self.running:
|
|
147
|
+
break
|
|
148
|
+
self.surface.fill(self.background)
|
|
149
|
+
for widget in tuple(self._active_widgets()):
|
|
150
|
+
if getattr(widget, "visible", True):
|
|
151
|
+
widget.draw(self.surface)
|
|
152
|
+
pygame.display.flip()
|
|
153
|
+
self.clock.tick(self.fps)
|
|
154
|
+
finally:
|
|
155
|
+
self.close()
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import time
|
|
2
|
+
|
|
3
|
+
def fade(start, end, progress):
|
|
4
|
+
t = max(0.0, min(1.0, float(progress)))
|
|
5
|
+
return start + (end-start)*t
|
|
6
|
+
|
|
7
|
+
def slide(start_x, start_y, end_x, end_y, progress):
|
|
8
|
+
t = max(0.0, min(1.0, float(progress)))
|
|
9
|
+
return (start_x+(end_x-start_x)*t, start_y+(end_y-start_y)*t)
|
|
10
|
+
|
|
11
|
+
class Animation:
|
|
12
|
+
"""Time-based numeric animation. Call update() regularly from your app loop."""
|
|
13
|
+
def __init__(self, start, end, duration, *, easing=None, on_update=None, on_complete=None):
|
|
14
|
+
if duration <= 0:
|
|
15
|
+
raise ValueError("duration must be positive")
|
|
16
|
+
self.start_value, self.end_value = start, end
|
|
17
|
+
self.duration = float(duration)
|
|
18
|
+
self.easing = easing or (lambda t: t)
|
|
19
|
+
self.on_update, self.on_complete = on_update, on_complete
|
|
20
|
+
self.started_at = None
|
|
21
|
+
self.value = start
|
|
22
|
+
self.finished = False
|
|
23
|
+
|
|
24
|
+
def start(self, now=None):
|
|
25
|
+
self.started_at = time.monotonic() if now is None else float(now)
|
|
26
|
+
self.finished = False
|
|
27
|
+
self.value = self.start_value
|
|
28
|
+
return self
|
|
29
|
+
|
|
30
|
+
def update(self, now=None):
|
|
31
|
+
if self.started_at is None:
|
|
32
|
+
self.start()
|
|
33
|
+
current = time.monotonic() if now is None else float(now)
|
|
34
|
+
progress = max(0.0, min(1.0, (current-self.started_at)/self.duration))
|
|
35
|
+
self.value = fade(self.start_value, self.end_value, self.easing(progress))
|
|
36
|
+
if self.on_update:
|
|
37
|
+
self.on_update(self.value)
|
|
38
|
+
if progress >= 1.0 and not self.finished:
|
|
39
|
+
self.finished = True
|
|
40
|
+
if self.on_complete:
|
|
41
|
+
self.on_complete()
|
|
42
|
+
return self.value
|
vali/layouts/__init__.py
ADDED
vali/layouts/basic.py
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
class Layout:
|
|
2
|
+
"""Base container for widgets and nested layouts."""
|
|
3
|
+
def __init__(self, x=0, y=0, *, spacing=10, padding=0):
|
|
4
|
+
self.x, self.y = int(x), int(y)
|
|
5
|
+
self.spacing, self.padding = int(spacing), int(padding)
|
|
6
|
+
self.children = []
|
|
7
|
+
self.visible = True
|
|
8
|
+
self.enabled = True
|
|
9
|
+
self.parent = None
|
|
10
|
+
|
|
11
|
+
def add(self, *widgets):
|
|
12
|
+
for widget in widgets:
|
|
13
|
+
if not hasattr(widget, "draw") or not hasattr(widget, "handle_event"):
|
|
14
|
+
raise TypeError("layout.add() expects widgets or layouts")
|
|
15
|
+
widget.parent = self
|
|
16
|
+
self.children.append(widget)
|
|
17
|
+
return self
|
|
18
|
+
|
|
19
|
+
def remove(self, widget):
|
|
20
|
+
self.children.remove(widget)
|
|
21
|
+
widget.parent = None
|
|
22
|
+
return widget
|
|
23
|
+
|
|
24
|
+
def draw(self, surface):
|
|
25
|
+
if self.visible:
|
|
26
|
+
for child in tuple(self.children):
|
|
27
|
+
if getattr(child, "visible", True):
|
|
28
|
+
child.draw(surface)
|
|
29
|
+
|
|
30
|
+
def handle_event(self, event):
|
|
31
|
+
if self.visible and self.enabled:
|
|
32
|
+
for child in tuple(self.children):
|
|
33
|
+
if getattr(child, "visible", True) and getattr(child, "enabled", True):
|
|
34
|
+
child.handle_event(event)
|
|
35
|
+
|
|
36
|
+
def set_position(self, x, y):
|
|
37
|
+
self.x, self.y = int(x), int(y)
|
|
38
|
+
return self
|
|
39
|
+
|
|
40
|
+
def set_visible(self, visible=True):
|
|
41
|
+
self.visible = bool(visible)
|
|
42
|
+
return self
|
|
43
|
+
|
|
44
|
+
def set_enabled(self, enabled=True):
|
|
45
|
+
self.enabled = bool(enabled)
|
|
46
|
+
return self
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class VerticalLayout(Layout):
|
|
50
|
+
def __init__(self, x=0, y=0, *, spacing=10, padding=0, width=None):
|
|
51
|
+
super().__init__(x, y, spacing=spacing, padding=padding)
|
|
52
|
+
self.width = width
|
|
53
|
+
|
|
54
|
+
def arrange(self):
|
|
55
|
+
cursor = self.y + self.padding
|
|
56
|
+
for child in self.children:
|
|
57
|
+
child.x = self.x + self.padding
|
|
58
|
+
child.y = cursor
|
|
59
|
+
height = getattr(child, "height", None) or getattr(child, "size", 24) + 10
|
|
60
|
+
cursor += height + self.spacing
|
|
61
|
+
|
|
62
|
+
def draw(self, surface):
|
|
63
|
+
self.arrange()
|
|
64
|
+
super().draw(surface)
|
|
65
|
+
|
|
66
|
+
def handle_event(self, event):
|
|
67
|
+
self.arrange()
|
|
68
|
+
super().handle_event(event)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class HorizontalLayout(Layout):
|
|
72
|
+
def arrange(self):
|
|
73
|
+
cursor = self.x + self.padding
|
|
74
|
+
for child in self.children:
|
|
75
|
+
child.x, child.y = cursor, self.y + self.padding
|
|
76
|
+
cursor += (getattr(child, "width", None) or 120) + self.spacing
|
|
77
|
+
|
|
78
|
+
def draw(self, surface):
|
|
79
|
+
self.arrange()
|
|
80
|
+
super().draw(surface)
|
|
81
|
+
|
|
82
|
+
def handle_event(self, event):
|
|
83
|
+
self.arrange()
|
|
84
|
+
super().handle_event(event)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
class GridLayout(Layout):
|
|
88
|
+
def __init__(self, x=0, y=0, *, columns=2, cell_width=160, cell_height=60,
|
|
89
|
+
spacing=10, padding=0):
|
|
90
|
+
super().__init__(x, y, spacing=spacing, padding=padding)
|
|
91
|
+
if int(columns) < 1 or int(cell_width) < 1 or int(cell_height) < 1:
|
|
92
|
+
raise ValueError("columns and cell dimensions must be positive")
|
|
93
|
+
self.columns, self.cell_width, self.cell_height = int(columns), int(cell_width), int(cell_height)
|
|
94
|
+
|
|
95
|
+
def arrange(self):
|
|
96
|
+
for index, child in enumerate(self.children):
|
|
97
|
+
col, row = index % self.columns, index // self.columns
|
|
98
|
+
child.x = self.x + self.padding + col * (self.cell_width + self.spacing)
|
|
99
|
+
child.y = self.y + self.padding + row * (self.cell_height + self.spacing)
|
|
100
|
+
|
|
101
|
+
def draw(self, surface):
|
|
102
|
+
self.arrange()
|
|
103
|
+
super().draw(surface)
|
|
104
|
+
|
|
105
|
+
def handle_event(self, event):
|
|
106
|
+
self.arrange()
|
|
107
|
+
super().handle_event(event)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
class AnchorLayout(Layout):
|
|
111
|
+
"""Positions children against a parent-sized rectangle.
|
|
112
|
+
|
|
113
|
+
Anchors: top_left, top, top_right, left, center, right,
|
|
114
|
+
bottom_left, bottom, bottom_right. Set each child's ``anchor`` attribute.
|
|
115
|
+
"""
|
|
116
|
+
VALID = {"top_left", "top", "top_right", "left", "center", "right",
|
|
117
|
+
"bottom_left", "bottom", "bottom_right"}
|
|
118
|
+
|
|
119
|
+
def __init__(self, x=0, y=0, width=800, height=600, *, padding=0):
|
|
120
|
+
super().__init__(x, y, spacing=0, padding=padding)
|
|
121
|
+
self.width, self.height = int(width), int(height)
|
|
122
|
+
|
|
123
|
+
def arrange(self):
|
|
124
|
+
for child in self.children:
|
|
125
|
+
anchor = getattr(child, "anchor", "top_left")
|
|
126
|
+
if anchor not in self.VALID:
|
|
127
|
+
raise ValueError(f"Invalid anchor: {anchor}")
|
|
128
|
+
width = int(getattr(child, "width", 0) or 0)
|
|
129
|
+
height = int(getattr(child, "height", 0) or 0)
|
|
130
|
+
left = self.x + self.padding
|
|
131
|
+
right = self.x + self.width - self.padding
|
|
132
|
+
top = self.y + self.padding
|
|
133
|
+
bottom = self.y + self.height - self.padding
|
|
134
|
+
positions = {
|
|
135
|
+
"top_left": (left, top), "top": ((left+right-width)//2, top),
|
|
136
|
+
"top_right": (right-width, top), "left": (left, (top+bottom-height)//2),
|
|
137
|
+
"center": ((left+right-width)//2, (top+bottom-height)//2),
|
|
138
|
+
"right": (right-width, (top+bottom-height)//2),
|
|
139
|
+
"bottom_left": (left, bottom-height),
|
|
140
|
+
"bottom": ((left+right-width)//2, bottom-height),
|
|
141
|
+
"bottom_right": (right-width, bottom-height),
|
|
142
|
+
}
|
|
143
|
+
child.x, child.y = positions[anchor]
|
|
144
|
+
|
|
145
|
+
def draw(self, surface):
|
|
146
|
+
self.arrange()
|
|
147
|
+
super().draw(surface)
|
|
148
|
+
|
|
149
|
+
def handle_event(self, event):
|
|
150
|
+
self.arrange()
|
|
151
|
+
super().handle_event(event)
|
vali/screens/__init__.py
ADDED
vali/screens/screen.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
class Screen:
|
|
2
|
+
def __init__(self):
|
|
3
|
+
self.children = []
|
|
4
|
+
|
|
5
|
+
def add(self, *widgets):
|
|
6
|
+
for widget in widgets:
|
|
7
|
+
widget.parent = self
|
|
8
|
+
self.children.append(widget)
|
|
9
|
+
return self
|
|
10
|
+
|
|
11
|
+
def remove(self, widget):
|
|
12
|
+
self.children.remove(widget)
|
|
13
|
+
|
|
14
|
+
class ScreenManager:
|
|
15
|
+
def __init__(self):
|
|
16
|
+
self._screens = {}
|
|
17
|
+
self.current_name = None
|
|
18
|
+
|
|
19
|
+
@property
|
|
20
|
+
def current(self):
|
|
21
|
+
return self._screens.get(self.current_name)
|
|
22
|
+
|
|
23
|
+
def add(self, name, screen):
|
|
24
|
+
if not isinstance(name, str) or not name:
|
|
25
|
+
raise ValueError("screen name must be a non-empty string")
|
|
26
|
+
if not isinstance(screen, Screen):
|
|
27
|
+
raise TypeError("screen must be a vali.Screen")
|
|
28
|
+
self._screens[name] = screen
|
|
29
|
+
if self.current_name is None:
|
|
30
|
+
self.current_name = name
|
|
31
|
+
return screen
|
|
32
|
+
|
|
33
|
+
def show(self, name):
|
|
34
|
+
if name not in self._screens:
|
|
35
|
+
raise KeyError(f"Unknown screen: {name}")
|
|
36
|
+
self.current_name = name
|
|
37
|
+
return self.current
|
|
38
|
+
|
|
39
|
+
def get(self, name):
|
|
40
|
+
return self._screens[name]
|
|
41
|
+
|
|
42
|
+
def remove(self, name):
|
|
43
|
+
screen = self._screens.pop(name)
|
|
44
|
+
if self.current_name == name:
|
|
45
|
+
self.current_name = next(iter(self._screens), None)
|
|
46
|
+
return screen
|
|
47
|
+
|
|
48
|
+
def __contains__(self, name):
|
|
49
|
+
return name in self._screens
|
vali/themes/__init__.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
|
|
3
|
+
@dataclass(frozen=True)
|
|
4
|
+
class Theme:
|
|
5
|
+
name: str
|
|
6
|
+
background: tuple
|
|
7
|
+
foreground: tuple
|
|
8
|
+
accent: tuple
|
|
9
|
+
surface: tuple
|
|
10
|
+
muted: tuple = (130, 135, 145)
|
|
11
|
+
|
|
12
|
+
DARK = Theme("dark", (25,28,36), (240,242,247), (85,135,220), (39,44,55), (155,160,175))
|
|
13
|
+
LIGHT = Theme("light", (245,247,250), (35,43,58), (55,105,190), (255,255,255), (130,135,145))
|
|
14
|
+
__all__ = ["Theme", "DARK", "LIGHT"]
|
vali/utils/__init__.py
ADDED
vali/utils/resources.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
import pygame
|
|
3
|
+
|
|
4
|
+
class ResourceManager:
|
|
5
|
+
"""Caches loaded image and font resources."""
|
|
6
|
+
def __init__(self):
|
|
7
|
+
self._images = {}
|
|
8
|
+
self._fonts = {}
|
|
9
|
+
|
|
10
|
+
def image(self, path, *, alpha=True):
|
|
11
|
+
key = (str(Path(path).resolve()), bool(alpha))
|
|
12
|
+
if key not in self._images:
|
|
13
|
+
image = pygame.image.load(key[0])
|
|
14
|
+
if pygame.display.get_init() and pygame.display.get_surface():
|
|
15
|
+
image = image.convert_alpha() if alpha else image.convert()
|
|
16
|
+
self._images[key] = image
|
|
17
|
+
return self._images[key]
|
|
18
|
+
|
|
19
|
+
def font(self, path=None, size=24):
|
|
20
|
+
key = (str(Path(path).resolve()) if path else None, int(size))
|
|
21
|
+
if key not in self._fonts:
|
|
22
|
+
self._fonts[key] = pygame.font.Font(key[0], key[1])
|
|
23
|
+
return self._fonts[key]
|
|
24
|
+
|
|
25
|
+
def clear(self):
|
|
26
|
+
self._images.clear()
|
|
27
|
+
self._fonts.clear()
|
vali/widgets/__init__.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
from .base import Widget
|
|
2
|
+
from .label import Label
|
|
3
|
+
from .button import Button
|
|
4
|
+
from .inputs import TextInput, Checkbox, Switch, Slider, Dropdown, RadioGroup
|
|
5
|
+
from .display import Image, ProgressBar, Panel
|
|
6
|
+
|
|
7
|
+
__all__ = [
|
|
8
|
+
"Widget", "Label", "Button", "TextInput", "Checkbox", "Switch",
|
|
9
|
+
"Slider", "Dropdown", "RadioGroup", "Image", "ProgressBar", "Panel",
|
|
10
|
+
]
|
vali/widgets/base.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
class Widget:
|
|
2
|
+
"""Base class for every VALI UI component."""
|
|
3
|
+
|
|
4
|
+
def __init__(self, x=0, y=0, width=None, height=None):
|
|
5
|
+
self.x, self.y = int(x), int(y)
|
|
6
|
+
self.width, self.height = width, height
|
|
7
|
+
self.visible = True
|
|
8
|
+
self.enabled = True
|
|
9
|
+
self.parent = None
|
|
10
|
+
|
|
11
|
+
def draw(self, surface):
|
|
12
|
+
raise NotImplementedError
|
|
13
|
+
|
|
14
|
+
def handle_event(self, event):
|
|
15
|
+
pass
|
|
16
|
+
|
|
17
|
+
def set_position(self, x, y):
|
|
18
|
+
self.x, self.y = int(x), int(y)
|
|
19
|
+
return self
|
|
20
|
+
|
|
21
|
+
def set_visible(self, visible=True):
|
|
22
|
+
self.visible = bool(visible)
|
|
23
|
+
return self
|
|
24
|
+
|
|
25
|
+
def set_enabled(self, enabled=True):
|
|
26
|
+
self.enabled = bool(enabled)
|
|
27
|
+
return self
|
vali/widgets/button.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
from .base import Widget
|
|
3
|
+
from ..core.events import Event
|
|
4
|
+
|
|
5
|
+
class Button(Widget):
|
|
6
|
+
def __init__(self, text, x=0, y=0, width=160, height=44, *,
|
|
7
|
+
on_click=None, background=(55, 105, 190),
|
|
8
|
+
hover_background=(42, 88, 165), color=(255, 255, 255),
|
|
9
|
+
size=20, radius=9, font=None):
|
|
10
|
+
super().__init__(x, y, int(width), int(height))
|
|
11
|
+
if self.width <= 0 or self.height <= 0:
|
|
12
|
+
raise ValueError("button dimensions must be positive")
|
|
13
|
+
self.text = str(text)
|
|
14
|
+
self.on_click = on_click
|
|
15
|
+
self.clicked = Event()
|
|
16
|
+
if on_click is not None:
|
|
17
|
+
self.clicked.connect(on_click)
|
|
18
|
+
self.background, self.hover_background = background, hover_background
|
|
19
|
+
self.color, self.size, self.radius = color, int(size), int(radius)
|
|
20
|
+
self.font_name = font
|
|
21
|
+
self._font = None
|
|
22
|
+
self._hovered = self._pressed = False
|
|
23
|
+
|
|
24
|
+
@property
|
|
25
|
+
def rect(self):
|
|
26
|
+
return pygame.Rect(self.x, self.y, self.width, self.height)
|
|
27
|
+
|
|
28
|
+
def handle_event(self, event):
|
|
29
|
+
if not self.enabled or not self.visible:
|
|
30
|
+
return
|
|
31
|
+
if event.type == pygame.MOUSEMOTION:
|
|
32
|
+
self._hovered = self.rect.collidepoint(event.pos)
|
|
33
|
+
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
|
|
34
|
+
self._pressed = self.rect.collidepoint(event.pos)
|
|
35
|
+
elif event.type == pygame.MOUSEBUTTONUP and event.button == 1:
|
|
36
|
+
did_click = self._pressed and self.rect.collidepoint(event.pos)
|
|
37
|
+
self._pressed = False
|
|
38
|
+
if did_click:
|
|
39
|
+
self.clicked.emit()
|
|
40
|
+
def draw(self, surface):
|
|
41
|
+
if not self.visible:
|
|
42
|
+
return
|
|
43
|
+
color = self.hover_background if self._hovered else self.background
|
|
44
|
+
if not self.enabled:
|
|
45
|
+
color = (150, 150, 150)
|
|
46
|
+
pygame.draw.rect(surface, color, self.rect, border_radius=self.radius)
|
|
47
|
+
if self._font is None:
|
|
48
|
+
self._font = pygame.font.Font(self.font_name, self.size)
|
|
49
|
+
text = self._font.render(self.text, True, self.color)
|
|
50
|
+
surface.blit(text, text.get_rect(center=self.rect.center))
|
vali/widgets/display.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
import pygame
|
|
3
|
+
from .base import Widget
|
|
4
|
+
|
|
5
|
+
class Image(Widget):
|
|
6
|
+
def __init__(self, path, x=0, y=0, width=None, height=None):
|
|
7
|
+
super().__init__(x, y, width, height)
|
|
8
|
+
self.path = Path(path)
|
|
9
|
+
if not self.path.is_file():
|
|
10
|
+
raise FileNotFoundError(self.path)
|
|
11
|
+
self._source = pygame.image.load(str(self.path))
|
|
12
|
+
if self.width is None:
|
|
13
|
+
self.width = self._source.get_width()
|
|
14
|
+
if self.height is None:
|
|
15
|
+
self.height = self._source.get_height()
|
|
16
|
+
self.image = self._scale(self._source)
|
|
17
|
+
|
|
18
|
+
def _scale(self, source):
|
|
19
|
+
if self.width and self.height:
|
|
20
|
+
return pygame.transform.smoothscale(source, (int(self.width), int(self.height)))
|
|
21
|
+
return source
|
|
22
|
+
|
|
23
|
+
def draw(self, surface):
|
|
24
|
+
if self.visible:
|
|
25
|
+
surface.blit(self.image, (self.x, self.y))
|
|
26
|
+
|
|
27
|
+
class ProgressBar(Widget):
|
|
28
|
+
def __init__(self, x=0, y=0, width=220, height=18, *, value=0,
|
|
29
|
+
background=(220,225,232), foreground=(55,125,205)):
|
|
30
|
+
super().__init__(x, y, int(width), int(height))
|
|
31
|
+
if self.width <= 0 or self.height <= 0:
|
|
32
|
+
raise ValueError("progress bar dimensions must be positive")
|
|
33
|
+
self.value = max(0.0, min(100.0, float(value)))
|
|
34
|
+
self.background, self.foreground = background, foreground
|
|
35
|
+
|
|
36
|
+
def set_value(self, value):
|
|
37
|
+
self.value = max(0.0, min(100.0, float(value)))
|
|
38
|
+
return self
|
|
39
|
+
|
|
40
|
+
def draw(self, surface):
|
|
41
|
+
if not self.visible:
|
|
42
|
+
return
|
|
43
|
+
rect = pygame.Rect(self.x, self.y, self.width, self.height)
|
|
44
|
+
pygame.draw.rect(surface, self.background, rect, border_radius=max(1,self.height//2))
|
|
45
|
+
fill = rect.copy()
|
|
46
|
+
fill.width = int(self.width * self.value / 100)
|
|
47
|
+
if fill.width:
|
|
48
|
+
pygame.draw.rect(surface, self.foreground, fill, border_radius=max(1,self.height//2))
|
|
49
|
+
|
|
50
|
+
class Panel(Widget):
|
|
51
|
+
def __init__(self, x=0, y=0, width=300, height=180, *, color=(255,255,255),
|
|
52
|
+
radius=10, border_color=None, border_width=0):
|
|
53
|
+
super().__init__(x,y,int(width),int(height))
|
|
54
|
+
if self.width <= 0 or self.height <= 0:
|
|
55
|
+
raise ValueError("panel dimensions must be positive")
|
|
56
|
+
self.color, self.radius = color, int(radius)
|
|
57
|
+
self.border_color, self.border_width = border_color, int(border_width)
|
|
58
|
+
self.children = []
|
|
59
|
+
|
|
60
|
+
def add(self, *widgets):
|
|
61
|
+
for widget in widgets:
|
|
62
|
+
widget.parent = self
|
|
63
|
+
self.children.append(widget)
|
|
64
|
+
return self
|
|
65
|
+
|
|
66
|
+
def handle_event(self, event):
|
|
67
|
+
for child in tuple(self.children):
|
|
68
|
+
if child.visible and child.enabled:
|
|
69
|
+
child.handle_event(event)
|
|
70
|
+
|
|
71
|
+
def draw(self, surface):
|
|
72
|
+
if not self.visible:
|
|
73
|
+
return
|
|
74
|
+
rect = pygame.Rect(self.x,self.y,self.width,self.height)
|
|
75
|
+
pygame.draw.rect(surface,self.color,rect,border_radius=self.radius)
|
|
76
|
+
if self.border_color and self.border_width:
|
|
77
|
+
pygame.draw.rect(surface,self.border_color,rect,self.border_width,border_radius=self.radius)
|
|
78
|
+
for child in self.children:
|
|
79
|
+
if child.visible:
|
|
80
|
+
child.draw(surface)
|
vali/widgets/inputs.py
ADDED
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
from .base import Widget
|
|
3
|
+
from ..core.events import Event
|
|
4
|
+
|
|
5
|
+
class TextInput(Widget):
|
|
6
|
+
"""Single-line text field with cursor navigation and change events."""
|
|
7
|
+
def __init__(self, x=0, y=0, width=260, height=42, *, placeholder="",
|
|
8
|
+
value="", on_change=None, max_length=256, password=False):
|
|
9
|
+
super().__init__(x, y, int(width), int(height))
|
|
10
|
+
if self.width <= 0 or self.height <= 0 or int(max_length) < 1:
|
|
11
|
+
raise ValueError("text input dimensions and max_length must be positive")
|
|
12
|
+
self.placeholder, self.value = str(placeholder), str(value)[:int(max_length)]
|
|
13
|
+
self.max_length = int(max_length)
|
|
14
|
+
self.password = bool(password)
|
|
15
|
+
self.changed = Event()
|
|
16
|
+
if on_change:
|
|
17
|
+
self.changed.connect(on_change)
|
|
18
|
+
self.focused = False
|
|
19
|
+
self.cursor = len(self.value)
|
|
20
|
+
self._font = None
|
|
21
|
+
self._scroll_x = 0
|
|
22
|
+
|
|
23
|
+
def set_value(self, value, *, emit=True):
|
|
24
|
+
value = str(value)[:self.max_length]
|
|
25
|
+
changed = value != self.value
|
|
26
|
+
self.value = value
|
|
27
|
+
self.cursor = min(self.cursor, len(self.value))
|
|
28
|
+
if changed and emit:
|
|
29
|
+
self.changed.emit(self.value)
|
|
30
|
+
return self
|
|
31
|
+
|
|
32
|
+
def _replace_value(self, value):
|
|
33
|
+
if value != self.value:
|
|
34
|
+
self.value = value[:self.max_length]
|
|
35
|
+
self.cursor = min(self.cursor, len(self.value))
|
|
36
|
+
self.changed.emit(self.value)
|
|
37
|
+
|
|
38
|
+
def handle_event(self, event):
|
|
39
|
+
if not self.enabled or not self.visible:
|
|
40
|
+
return
|
|
41
|
+
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
|
|
42
|
+
self.focused = pygame.Rect(self.x, self.y, self.width, self.height).collidepoint(event.pos)
|
|
43
|
+
if self.focused:
|
|
44
|
+
if self._font is None:
|
|
45
|
+
self._font = pygame.font.Font(None, 24)
|
|
46
|
+
shown = "•" * len(self.value) if self.password else self.value
|
|
47
|
+
local_x = max(0, event.pos[0] - self.x - 10 + self._scroll_x)
|
|
48
|
+
self.cursor = min(len(shown), min(range(len(shown)+1), key=lambda i: abs(self._font.size(shown[:i])[0]-local_x)))
|
|
49
|
+
elif event.type == pygame.KEYDOWN and self.focused:
|
|
50
|
+
old = self.value
|
|
51
|
+
if event.key == pygame.K_BACKSPACE and self.cursor:
|
|
52
|
+
self.value = self.value[:self.cursor-1] + self.value[self.cursor:]
|
|
53
|
+
self.cursor -= 1
|
|
54
|
+
elif event.key == pygame.K_DELETE and self.cursor < len(self.value):
|
|
55
|
+
self.value = self.value[:self.cursor] + self.value[self.cursor+1:]
|
|
56
|
+
elif event.key == pygame.K_LEFT:
|
|
57
|
+
self.cursor = max(0, self.cursor-1)
|
|
58
|
+
elif event.key == pygame.K_RIGHT:
|
|
59
|
+
self.cursor = min(len(self.value), self.cursor+1)
|
|
60
|
+
elif event.key == pygame.K_HOME:
|
|
61
|
+
self.cursor = 0
|
|
62
|
+
elif event.key == pygame.K_END:
|
|
63
|
+
self.cursor = len(self.value)
|
|
64
|
+
elif event.key in (pygame.K_RETURN, pygame.K_KP_ENTER, pygame.K_ESCAPE):
|
|
65
|
+
self.focused = False
|
|
66
|
+
elif event.unicode and event.unicode.isprintable() and len(self.value) < self.max_length:
|
|
67
|
+
self.value = self.value[:self.cursor] + event.unicode + self.value[self.cursor:]
|
|
68
|
+
self.cursor += len(event.unicode)
|
|
69
|
+
if old != self.value:
|
|
70
|
+
self.changed.emit(self.value)
|
|
71
|
+
|
|
72
|
+
def draw(self, surface):
|
|
73
|
+
if not self.visible:
|
|
74
|
+
return
|
|
75
|
+
rect = pygame.Rect(self.x, self.y, self.width, self.height)
|
|
76
|
+
pygame.draw.rect(surface, (255, 255, 255), rect, border_radius=6)
|
|
77
|
+
pygame.draw.rect(surface, (65, 125, 220) if self.focused else (180, 185, 195), rect, 2, border_radius=6)
|
|
78
|
+
if self._font is None:
|
|
79
|
+
self._font = pygame.font.Font(None, 24)
|
|
80
|
+
shown = ("•" * len(self.value)) if self.password else self.value
|
|
81
|
+
display = shown if self.value else self.placeholder
|
|
82
|
+
color = (35, 40, 50) if self.value else (140, 145, 155)
|
|
83
|
+
text = self._font.render(display, True, color)
|
|
84
|
+
available = self.width - 20
|
|
85
|
+
cursor_px = self._font.size(shown[:self.cursor])[0]
|
|
86
|
+
if cursor_px - self._scroll_x > available:
|
|
87
|
+
self._scroll_x = cursor_px - available
|
|
88
|
+
elif cursor_px < self._scroll_x:
|
|
89
|
+
self._scroll_x = cursor_px
|
|
90
|
+
self._scroll_x = max(0, self._scroll_x)
|
|
91
|
+
clip = surface.get_clip()
|
|
92
|
+
surface.set_clip(rect.inflate(-14, -4))
|
|
93
|
+
surface.blit(text, (self.x + 9 - self._scroll_x, self.y + (self.height-text.get_height())//2))
|
|
94
|
+
if self.focused:
|
|
95
|
+
cx = self.x + 9 + cursor_px - self._scroll_x
|
|
96
|
+
pygame.draw.line(surface, (35, 40, 50), (cx, self.y+9), (cx, self.y+self.height-9), 1)
|
|
97
|
+
surface.set_clip(clip)
|
|
98
|
+
|
|
99
|
+
class Checkbox(Widget):
|
|
100
|
+
def __init__(self, text="", x=0, y=0, *, checked=False, on_change=None):
|
|
101
|
+
super().__init__(x, y, 190, 26)
|
|
102
|
+
self.text, self.checked = str(text), bool(checked)
|
|
103
|
+
self.changed = Event()
|
|
104
|
+
if on_change:
|
|
105
|
+
self.changed.connect(on_change)
|
|
106
|
+
self._font = None
|
|
107
|
+
|
|
108
|
+
def handle_event(self, event):
|
|
109
|
+
if not self.enabled or not self.visible:
|
|
110
|
+
return
|
|
111
|
+
if event.type == pygame.MOUSEBUTTONUP and event.button == 1:
|
|
112
|
+
if pygame.Rect(self.x, self.y, max(190, self.width or 190), 28).collidepoint(event.pos):
|
|
113
|
+
self.checked = not self.checked
|
|
114
|
+
self.changed.emit(self.checked)
|
|
115
|
+
|
|
116
|
+
def draw(self, surface):
|
|
117
|
+
if not self.visible:
|
|
118
|
+
return
|
|
119
|
+
box = pygame.Rect(self.x, self.y + 2, 20, 20)
|
|
120
|
+
pygame.draw.rect(surface, (255, 255, 255), box, border_radius=4)
|
|
121
|
+
pygame.draw.rect(surface, (65, 105, 180), box, 2, border_radius=4)
|
|
122
|
+
if self.checked:
|
|
123
|
+
pygame.draw.line(surface, (40, 120, 75), (self.x+4, self.y+12), (self.x+9, self.y+17), 3)
|
|
124
|
+
pygame.draw.line(surface, (40, 120, 75), (self.x+9, self.y+17), (self.x+17, self.y+6), 3)
|
|
125
|
+
if self._font is None:
|
|
126
|
+
self._font = pygame.font.Font(None, 23)
|
|
127
|
+
surface.blit(self._font.render(self.text, True, (35, 43, 58)), (self.x+29, self.y+2))
|
|
128
|
+
|
|
129
|
+
class Switch(Checkbox):
|
|
130
|
+
"""Toggle switch control with the same checked/on_change API as Checkbox."""
|
|
131
|
+
def draw(self, surface):
|
|
132
|
+
if not self.visible:
|
|
133
|
+
return
|
|
134
|
+
track = pygame.Rect(self.x, self.y+3, 42, 22)
|
|
135
|
+
pygame.draw.rect(surface, (65, 130, 205) if self.checked else (155, 160, 170),
|
|
136
|
+
track, border_radius=11)
|
|
137
|
+
cx = self.x + (31 if self.checked else 11)
|
|
138
|
+
pygame.draw.circle(surface, (255,255,255), (cx, self.y+14), 8)
|
|
139
|
+
if self._font is None:
|
|
140
|
+
self._font = pygame.font.Font(None, 23)
|
|
141
|
+
surface.blit(self._font.render(self.text, True, (35,43,58)), (self.x+51, self.y+3))
|
|
142
|
+
def handle_event(self, event):
|
|
143
|
+
if not self.enabled or not self.visible:
|
|
144
|
+
return
|
|
145
|
+
if event.type == pygame.MOUSEBUTTONUP and event.button == 1:
|
|
146
|
+
if pygame.Rect(self.x, self.y, 190, 28).collidepoint(event.pos):
|
|
147
|
+
self.checked = not self.checked
|
|
148
|
+
self.changed.emit(self.checked)
|
|
149
|
+
|
|
150
|
+
class Slider(Widget):
|
|
151
|
+
def __init__(self, x=0, y=0, width=220, *, minimum=0, maximum=100,
|
|
152
|
+
value=50, on_change=None):
|
|
153
|
+
super().__init__(x, y, int(width), 24)
|
|
154
|
+
if self.width <= 0:
|
|
155
|
+
raise ValueError("slider width must be positive")
|
|
156
|
+
if maximum <= minimum:
|
|
157
|
+
raise ValueError("maximum must be greater than minimum")
|
|
158
|
+
self.minimum, self.maximum = minimum, maximum
|
|
159
|
+
self.value = max(minimum, min(maximum, value))
|
|
160
|
+
self.changed = Event()
|
|
161
|
+
if on_change:
|
|
162
|
+
self.changed.connect(on_change)
|
|
163
|
+
self.dragging = False
|
|
164
|
+
|
|
165
|
+
def _update(self, px):
|
|
166
|
+
ratio = max(0.0, min(1.0, (px-self.x) / self.width))
|
|
167
|
+
value = self.minimum + ratio * (self.maximum-self.minimum)
|
|
168
|
+
if value != self.value:
|
|
169
|
+
self.value = value
|
|
170
|
+
self.changed.emit(self.value)
|
|
171
|
+
|
|
172
|
+
def handle_event(self, event):
|
|
173
|
+
if not self.enabled or not self.visible:
|
|
174
|
+
return
|
|
175
|
+
track = pygame.Rect(self.x, self.y, self.width, 24)
|
|
176
|
+
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1 and track.collidepoint(event.pos):
|
|
177
|
+
self.dragging = True
|
|
178
|
+
self._update(event.pos[0])
|
|
179
|
+
elif event.type == pygame.MOUSEMOTION and self.dragging:
|
|
180
|
+
self._update(event.pos[0])
|
|
181
|
+
elif event.type == pygame.MOUSEBUTTONUP and event.button == 1:
|
|
182
|
+
self.dragging = False
|
|
183
|
+
|
|
184
|
+
def draw(self, surface):
|
|
185
|
+
if not self.visible:
|
|
186
|
+
return
|
|
187
|
+
cy = self.y + 12
|
|
188
|
+
pygame.draw.line(surface, (190,195,205), (self.x,cy), (self.x+self.width,cy), 5)
|
|
189
|
+
ratio = (self.value-self.minimum)/(self.maximum-self.minimum)
|
|
190
|
+
px = int(self.x + ratio*self.width)
|
|
191
|
+
pygame.draw.line(surface, (65,105,190), (self.x,cy), (px,cy), 5)
|
|
192
|
+
pygame.draw.circle(surface, (45,95,180), (px,cy), 9)
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
class Dropdown(Widget):
|
|
196
|
+
"""Compact dropdown selector. on_change receives the selected value."""
|
|
197
|
+
def __init__(self, options, x=0, y=0, width=200, height=40, *,
|
|
198
|
+
value=None, on_change=None):
|
|
199
|
+
super().__init__(x, y, int(width), int(height))
|
|
200
|
+
self.options = list(options)
|
|
201
|
+
if not self.options:
|
|
202
|
+
raise ValueError("Dropdown requires at least one option")
|
|
203
|
+
self.value = value if value in self.options else self.options[0]
|
|
204
|
+
self.opened = False
|
|
205
|
+
self.changed = Event()
|
|
206
|
+
if on_change:
|
|
207
|
+
self.changed.connect(on_change)
|
|
208
|
+
self._font = None
|
|
209
|
+
|
|
210
|
+
def set_value(self, value):
|
|
211
|
+
if value not in self.options:
|
|
212
|
+
raise ValueError(f"Unknown dropdown option: {value}")
|
|
213
|
+
if value != self.value:
|
|
214
|
+
self.value = value
|
|
215
|
+
self.changed.emit(value)
|
|
216
|
+
self.opened = False
|
|
217
|
+
return self
|
|
218
|
+
|
|
219
|
+
def handle_event(self, event):
|
|
220
|
+
if not self.enabled or not self.visible or event.type != pygame.MOUSEBUTTONUP or event.button != 1:
|
|
221
|
+
return
|
|
222
|
+
rect = pygame.Rect(self.x, self.y, self.width, self.height)
|
|
223
|
+
if rect.collidepoint(event.pos):
|
|
224
|
+
self.opened = not self.opened
|
|
225
|
+
return
|
|
226
|
+
if self.opened:
|
|
227
|
+
for index, option in enumerate(self.options):
|
|
228
|
+
item = pygame.Rect(self.x, self.y+self.height*(index+1), self.width, self.height)
|
|
229
|
+
if item.collidepoint(event.pos):
|
|
230
|
+
self.set_value(option)
|
|
231
|
+
return
|
|
232
|
+
self.opened = False
|
|
233
|
+
|
|
234
|
+
def draw(self, surface):
|
|
235
|
+
if not self.visible:
|
|
236
|
+
return
|
|
237
|
+
if self._font is None:
|
|
238
|
+
self._font = pygame.font.Font(None, 23)
|
|
239
|
+
rect = pygame.Rect(self.x, self.y, self.width, self.height)
|
|
240
|
+
pygame.draw.rect(surface, (255,255,255), rect, border_radius=5)
|
|
241
|
+
pygame.draw.rect(surface, (80,110,160), rect, 1, border_radius=5)
|
|
242
|
+
surface.blit(self._font.render(str(self.value), True, (35,43,58)), (self.x+10, self.y+(self.height-22)//2))
|
|
243
|
+
if self.opened:
|
|
244
|
+
for index, option in enumerate(self.options):
|
|
245
|
+
item = pygame.Rect(self.x, self.y+self.height*(index+1), self.width, self.height)
|
|
246
|
+
pygame.draw.rect(surface, (245,247,250), item)
|
|
247
|
+
pygame.draw.rect(surface, (180,185,195), item, 1)
|
|
248
|
+
surface.blit(self._font.render(str(option), True, (35,43,58)), (item.x+10, item.y+(self.height-22)//2))
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
class RadioGroup(Widget):
|
|
252
|
+
"""Vertical radio choices. on_change receives the selected value."""
|
|
253
|
+
def __init__(self, options, x=0, y=0, *, value=None, spacing=8, on_change=None):
|
|
254
|
+
super().__init__(x, y)
|
|
255
|
+
self.options = list(options)
|
|
256
|
+
if not self.options:
|
|
257
|
+
raise ValueError("RadioGroup requires at least one option")
|
|
258
|
+
self.value = value if value in self.options else self.options[0]
|
|
259
|
+
self.spacing = int(spacing)
|
|
260
|
+
self.width = 220
|
|
261
|
+
self.height = len(self.options)*28 + max(0, len(self.options)-1)*self.spacing
|
|
262
|
+
self.changed = Event()
|
|
263
|
+
if on_change:
|
|
264
|
+
self.changed.connect(on_change)
|
|
265
|
+
self._font = None
|
|
266
|
+
|
|
267
|
+
def set_value(self, value):
|
|
268
|
+
if value not in self.options:
|
|
269
|
+
raise ValueError(f"Unknown radio option: {value}")
|
|
270
|
+
if value != self.value:
|
|
271
|
+
self.value = value
|
|
272
|
+
self.changed.emit(value)
|
|
273
|
+
return self
|
|
274
|
+
|
|
275
|
+
def handle_event(self, event):
|
|
276
|
+
if not self.enabled or not self.visible or event.type != pygame.MOUSEBUTTONUP or event.button != 1:
|
|
277
|
+
return
|
|
278
|
+
for index, option in enumerate(self.options):
|
|
279
|
+
row = pygame.Rect(self.x, self.y+index*(28+self.spacing), self.width, 28)
|
|
280
|
+
if row.collidepoint(event.pos):
|
|
281
|
+
self.set_value(option)
|
|
282
|
+
break
|
|
283
|
+
|
|
284
|
+
def draw(self, surface):
|
|
285
|
+
if not self.visible:
|
|
286
|
+
return
|
|
287
|
+
if self._font is None:
|
|
288
|
+
self._font = pygame.font.Font(None, 23)
|
|
289
|
+
for index, option in enumerate(self.options):
|
|
290
|
+
cy = self.y+index*(28+self.spacing)+12
|
|
291
|
+
pygame.draw.circle(surface, (255,255,255), (self.x+10,cy), 9)
|
|
292
|
+
pygame.draw.circle(surface, (65,105,180), (self.x+10,cy), 9, 2)
|
|
293
|
+
if option == self.value:
|
|
294
|
+
pygame.draw.circle(surface, (65,105,180), (self.x+10,cy), 5)
|
|
295
|
+
surface.blit(self._font.render(str(option), True, (35,43,58)), (self.x+28,cy-10))
|
vali/widgets/label.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
from .base import Widget
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class Label(Widget):
|
|
6
|
+
def __init__(self, text, x=0, y=0, *, size=24, color=None, font=None):
|
|
7
|
+
super().__init__(x, y)
|
|
8
|
+
self.text = str(text)
|
|
9
|
+
self.size = int(size)
|
|
10
|
+
if self.size <= 0:
|
|
11
|
+
raise ValueError("label font size must be positive")
|
|
12
|
+
self.color = color or (35, 43, 58)
|
|
13
|
+
self.font_name = font
|
|
14
|
+
if not pygame.font.get_init():
|
|
15
|
+
pygame.font.init()
|
|
16
|
+
self._font = pygame.font.Font(self.font_name, self.size)
|
|
17
|
+
self._measure()
|
|
18
|
+
|
|
19
|
+
def _measure(self):
|
|
20
|
+
self.width, self.height = self._font.size(self.text)
|
|
21
|
+
|
|
22
|
+
def set_text(self, text):
|
|
23
|
+
self.text = str(text)
|
|
24
|
+
self._measure()
|
|
25
|
+
return self
|
|
26
|
+
|
|
27
|
+
def set_color(self, color):
|
|
28
|
+
self.color = tuple(color)
|
|
29
|
+
return self
|
|
30
|
+
|
|
31
|
+
def draw(self, surface):
|
|
32
|
+
if self.visible:
|
|
33
|
+
surface.blit(self._font.render(self.text, True, self.color), (self.x, self.y))
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: vali-ui
|
|
3
|
+
Version: 1.1.0
|
|
4
|
+
Summary: VALI - a lightweight Python desktop UI framework
|
|
5
|
+
Author: Eyal Laros
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Keywords: gui,ui,pygame,desktop,widgets,framework
|
|
8
|
+
Classifier: Development Status :: 4 - Beta
|
|
9
|
+
Classifier: Environment :: Win32 (MS Windows)
|
|
10
|
+
Classifier: Environment :: X11 Applications
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Topic :: Software Development :: User Interfaces
|
|
19
|
+
Requires-Python: >=3.10
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: pygame>=2.5
|
|
23
|
+
Provides-Extra: dev
|
|
24
|
+
Requires-Dist: build>=1.2; extra == "dev"
|
|
25
|
+
Requires-Dist: pytest>=8; extra == "dev"
|
|
26
|
+
Requires-Dist: ruff>=0.6; extra == "dev"
|
|
27
|
+
Requires-Dist: twine>=5; extra == "dev"
|
|
28
|
+
Dynamic: license-file
|
|
29
|
+
|
|
30
|
+
# VALI 1.1.0
|
|
31
|
+
|
|
32
|
+
**VALI** is a small Python desktop UI framework. It uses Pygame internally, but application code works with VALI classes and does not need to import Pygame.
|
|
33
|
+
|
|
34
|
+
## Install
|
|
35
|
+
|
|
36
|
+
The intended package-install command after the first PyPI publication is:
|
|
37
|
+
|
|
38
|
+
```powershell
|
|
39
|
+
py -m pip install vali-ui
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
The Python import name is `vali`:
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
import vali
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
> The Python import name is `vali`, while the PyPI distribution name is `vali-ui`.
|
|
49
|
+
|
|
50
|
+
## Quick start
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
import vali
|
|
54
|
+
|
|
55
|
+
app = vali.App()
|
|
56
|
+
app.create_window("My first VALI app", 900, 600, theme="dark")
|
|
57
|
+
|
|
58
|
+
app.add(vali.Label("Hello from VALI", 30, 30, size=34))
|
|
59
|
+
app.add(vali.Button("Click me", 30, 100, on_click=lambda: print("Clicked!")))
|
|
60
|
+
app.add(vali.TextInput(30, 170, placeholder="Type something..."))
|
|
61
|
+
|
|
62
|
+
app.run()
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
You can also use the older size-pair style:
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
app.create_window("My app", size=(900, 600))
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Window options
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
app.create_window(
|
|
75
|
+
"My app",
|
|
76
|
+
1000,
|
|
77
|
+
700,
|
|
78
|
+
icon="assets/app.png",
|
|
79
|
+
theme="dark",
|
|
80
|
+
resizable=True,
|
|
81
|
+
min_size=(480, 320),
|
|
82
|
+
fps=60,
|
|
83
|
+
)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
- `icon` accepts an image path or a Pygame `Surface` (only if your own code already uses Pygame).
|
|
87
|
+
- If no icon is supplied, VALI draws its own simple V icon; it does not use the Pygame snake icon.
|
|
88
|
+
- `window.set_icon(path)` changes the icon after creation.
|
|
89
|
+
- `window.set_theme("light")`, `window.set_theme("dark")`, or `app.set_theme(...)` changes the window theme/background.
|
|
90
|
+
- `window.set_background((r, g, b))` sets a custom background color.
|
|
91
|
+
|
|
92
|
+
## Included components
|
|
93
|
+
|
|
94
|
+
### Core
|
|
95
|
+
|
|
96
|
+
- `App`, `Window`, `Event`
|
|
97
|
+
- Window sizing, resize minimums, FPS cap, custom icon, close handling
|
|
98
|
+
- Multiple screens using `Screen` and `ScreenManager`
|
|
99
|
+
|
|
100
|
+
### Widgets
|
|
101
|
+
|
|
102
|
+
- `Label`, `Button`
|
|
103
|
+
- `TextInput` (cursor movement, Home/End, Backspace/Delete, max length, password display)
|
|
104
|
+
- `Checkbox`, `Switch`, `Slider`
|
|
105
|
+
- `Dropdown`, `RadioGroup`
|
|
106
|
+
- `Image`, `ProgressBar`, `Panel`
|
|
107
|
+
|
|
108
|
+
### Layouts
|
|
109
|
+
|
|
110
|
+
- `VerticalLayout`, `HorizontalLayout`, `GridLayout`
|
|
111
|
+
- `AnchorLayout` with nine anchor positions (`top_left`, `center`, `bottom_right`, etc.)
|
|
112
|
+
- Nested layouts and panels
|
|
113
|
+
|
|
114
|
+
### Utilities
|
|
115
|
+
|
|
116
|
+
- `Theme`, `DARK`, `LIGHT`
|
|
117
|
+
- `Animation`, `fade`, `slide`
|
|
118
|
+
- `ResourceManager` for image/font caching
|
|
119
|
+
|
|
120
|
+
## Examples
|
|
121
|
+
|
|
122
|
+
Run the showcase after installing the development copy or from the source folder:
|
|
123
|
+
|
|
124
|
+
```powershell
|
|
125
|
+
py examples\showcase.py
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Development / build
|
|
129
|
+
|
|
130
|
+
```powershell
|
|
131
|
+
py -m pip install -e ".[dev]"
|
|
132
|
+
py -m pytest
|
|
133
|
+
py -m build
|
|
134
|
+
py -m twine check dist\*
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
The project distribution name is `vali-ui`; the import package remains `vali`.
|
|
138
|
+
|
|
139
|
+
## Version 1.1.0 scope
|
|
140
|
+
|
|
141
|
+
VALI 1.1.0 is a desktop 2D UI foundation. It is not a native-widget toolkit and does not claim full mobile support, screen-reader accessibility, advanced constraint-based responsive layouts, or a full-featured text editor. Those are separate engineering milestones rather than hidden promises.
|
|
142
|
+
|
|
143
|
+
## License
|
|
144
|
+
|
|
145
|
+
MIT. See `LICENSE`.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
vali/__init__.py,sha256=7YiBWSk6jh1L8p2hP1xo4iTMjOhrDnF4qEojgEz7cBo,968
|
|
2
|
+
vali/core/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
+
vali/core/app.py,sha256=UtB8zfonaBadsEwNqP588rZVXZIo6GwVBeNmm7N1lDM,1646
|
|
4
|
+
vali/core/events.py,sha256=2DaonEzUaGYj6Slfabu3AZDfGL4txsYg4oP--yUEa1o,661
|
|
5
|
+
vali/core/window.py,sha256=_6tjnoQ2p6ZC1HvVjA5Ng8iXXUKUIWj_QE8u22IdSlc,6353
|
|
6
|
+
vali/graphics/__init__.py,sha256=mO4PQ0yfAeBqd_TtwrqSMMnvWo3y0WTWWV_j9aocfRM,87
|
|
7
|
+
vali/graphics/animation.py,sha256=Q4OoecfAiDAURPlzEV6ItWSlvpOGQI_2TbQNg_vE7JY,1621
|
|
8
|
+
vali/layouts/__init__.py,sha256=kyujHQ-2RdVWbWNMinZqBW9fTEHYxLyIP0n2UTb2MHk,175
|
|
9
|
+
vali/layouts/basic.py,sha256=M-aSRP77S1mW5iHRv6g3SsF1y_F3NNXA46Vvoh3PrAw,5450
|
|
10
|
+
vali/screens/__init__.py,sha256=n6qoruOGk2XLqMdLaCvWILBThE901QX45Sq1xJtzOEU,80
|
|
11
|
+
vali/screens/screen.py,sha256=FcL_OFEmk8n7zy14sfMU-u-bjquQVqpRf3t--tYab0g,1383
|
|
12
|
+
vali/themes/__init__.py,sha256=_YvTAsU7OHit6V31ZjBjqFvWlm0RUpHyYRfUvJvE_qs,423
|
|
13
|
+
vali/utils/__init__.py,sha256=W8WPJuyqFG8aC0KcDJr4kBKjHiZRQlcPWFyhGU9ntAE,69
|
|
14
|
+
vali/utils/resources.py,sha256=scbQ5U53u6zek5hvyqnmFI_cKIWYMVRw4bqja6hF8Fo,912
|
|
15
|
+
vali/widgets/__init__.py,sha256=z-mEwIo1MaEEvvZVntN8mZXg1V0cZmcpexXMX8UsgMI,358
|
|
16
|
+
vali/widgets/base.py,sha256=KZMOuGVorigGAveObDJSv-P2MIeqY82zDMIZf_Pw5p0,701
|
|
17
|
+
vali/widgets/button.py,sha256=kYtKRc-eb8XkzhmmQCc88xtA5Fm-_nQQ4piVUQqFcFU,2151
|
|
18
|
+
vali/widgets/display.py,sha256=9b0c5op3nzGqf5p8EfxgOu9JzyzgjWAncymkFqw70zI,3185
|
|
19
|
+
vali/widgets/inputs.py,sha256=aBEm2dNBIH-57qp9huAuyM3wtadam3qVVmFhlvCVc2M,13535
|
|
20
|
+
vali/widgets/label.py,sha256=q_xA9pVJkwCL5fHiMAM0_d52gGva2aCvaw23UzHMlQU,976
|
|
21
|
+
vali_ui-1.1.0.dist-info/licenses/LICENSE,sha256=rA_uP1ZQOz_ImidCzosbSlJ9ZV2ks2gnm5ApqpHO40Y,808
|
|
22
|
+
vali_ui-1.1.0.dist-info/METADATA,sha256=s_sQJZG9ph1iyMm-pmpI2cDNvTz1LbaqGTg3iLGy-M8,4130
|
|
23
|
+
vali_ui-1.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
24
|
+
vali_ui-1.1.0.dist-info/top_level.txt,sha256=kiuAGOl8oNQfPPC6a37duXMnDjmV6YkyX8oqjYBMk1k,5
|
|
25
|
+
vali_ui-1.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Eyal Laros
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
vali
|