pygame-widget-kit 0.0.1__tar.gz → 0.0.2__tar.gz
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.
- pygame_widget_kit-0.0.2/PKG-INFO +194 -0
- pygame_widget_kit-0.0.2/README.md +172 -0
- {pygame_widget_kit-0.0.1 → pygame_widget_kit-0.0.2}/pyproject.toml +1 -1
- pygame_widget_kit-0.0.2/src/main.py +109 -0
- {pygame_widget_kit-0.0.1 → pygame_widget_kit-0.0.2}/src/pygame_widget_kit/Button.py +25 -7
- pygame_widget_kit-0.0.2/src/pygame_widget_kit/Radio.py +125 -0
- {pygame_widget_kit-0.0.1 → pygame_widget_kit-0.0.2}/src/pygame_widget_kit/Select.py +44 -39
- {pygame_widget_kit-0.0.1 → pygame_widget_kit-0.0.2}/src/pygame_widget_kit/Text.py +4 -3
- {pygame_widget_kit-0.0.1 → pygame_widget_kit-0.0.2}/src/pygame_widget_kit/TextInput.py +72 -28
- {pygame_widget_kit-0.0.1 → pygame_widget_kit-0.0.2}/src/pygame_widget_kit/UIComponent.py +6 -6
- {pygame_widget_kit-0.0.1 → pygame_widget_kit-0.0.2}/src/pygame_widget_kit/Widget.py +2 -0
- {pygame_widget_kit-0.0.1 → pygame_widget_kit-0.0.2}/src/pygame_widget_kit/__init__.py +2 -1
- pygame_widget_kit-0.0.2/src/pygame_widget_kit.egg-info/PKG-INFO +194 -0
- {pygame_widget_kit-0.0.1 → pygame_widget_kit-0.0.2}/src/pygame_widget_kit.egg-info/SOURCES.txt +2 -0
- {pygame_widget_kit-0.0.1 → pygame_widget_kit-0.0.2}/src/pygame_widget_kit.egg-info/top_level.txt +1 -0
- pygame_widget_kit-0.0.1/PKG-INFO +0 -24
- pygame_widget_kit-0.0.1/README.md +0 -2
- pygame_widget_kit-0.0.1/src/pygame_widget_kit.egg-info/PKG-INFO +0 -24
- {pygame_widget_kit-0.0.1 → pygame_widget_kit-0.0.2}/LICENSE +0 -0
- {pygame_widget_kit-0.0.1 → pygame_widget_kit-0.0.2}/setup.cfg +0 -0
- {pygame_widget_kit-0.0.1 → pygame_widget_kit-0.0.2}/src/pygame_widget_kit/UIManager.py +0 -0
- {pygame_widget_kit-0.0.1 → pygame_widget_kit-0.0.2}/src/pygame_widget_kit.egg-info/dependency_links.txt +0 -0
- {pygame_widget_kit-0.0.1 → pygame_widget_kit-0.0.2}/src/pygame_widget_kit.egg-info/requires.txt +0 -0
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pygame-widget-kit
|
|
3
|
+
Version: 0.0.2
|
|
4
|
+
Summary: A GUI framework built on Pygame. Designed as a retained-mode UI system with a component tree, extensible widgets, and focus management.
|
|
5
|
+
Author-email: Sinan Orgu <sinanorguuu@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/sinanorgu/pygame-widget-kit
|
|
8
|
+
Project-URL: Repository, https://github.com/sinanorgu/pygame-widget-kit
|
|
9
|
+
Project-URL: Issues, https://github.com/sinanorgu/pygame-widget-kit/issues
|
|
10
|
+
Keywords: pygame,ui,gui,toolkit,widgets,interface,game-development,inputbox,buttons
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Topic :: Software Development :: Libraries :: pygame
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Requires-Python: >=3.7
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
License-File: LICENSE
|
|
20
|
+
Requires-Dist: pygame>=2.0.0
|
|
21
|
+
Dynamic: license-file
|
|
22
|
+
|
|
23
|
+
# pygame-widget-kit
|
|
24
|
+
|
|
25
|
+
[](https://pypi.org/project/pygame-widget-kit/)
|
|
26
|
+
|
|
27
|
+
A GUI framework built on Pygame. pygame-widget-kit is a retained-mode UI system with a component tree, event routing, focus and modal management, and extensible widgets such as Select/Dropdown and TextInput with mouse-based text selection and clipboard support.
|
|
28
|
+
|
|
29
|
+
PyPI: https://pypi.org/project/pygame-widget-kit/
|
|
30
|
+
|
|
31
|
+
## Features
|
|
32
|
+
|
|
33
|
+
- Retained-mode component tree with nested children
|
|
34
|
+
- Event routing with hover, focus, and active state handling
|
|
35
|
+
- Modal/dropdown support via `UIManager`
|
|
36
|
+
- Built-in widgets: `Button`, `Text`, `Select`, `Radio`, `TextInput`
|
|
37
|
+
- Text input filtering modes (text, number, hex, binary, octal)
|
|
38
|
+
- Clipboard copy/paste in `TextInput` (requires `pygame.scrap`)
|
|
39
|
+
|
|
40
|
+
## Installation
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
pip install pygame-widget-kit
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Pygame is listed as a dependency (pygame >= 2.0.0). Make sure you call `pygame.init()` before creating widgets.
|
|
47
|
+
|
|
48
|
+
## Usage
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
|
|
52
|
+
import pygame
|
|
53
|
+
from pygame_widget_kit import *
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
SCREEN_WIDTH = 900
|
|
57
|
+
SCREEN_HEIGHT = 720
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def update_label_from_input(input_box: TextInput, label: Text):
|
|
61
|
+
label.set_text(f"Label: {input_box.text_value}")
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def update_select_label(select: Select, label: Text):
|
|
65
|
+
label.set_text(f"Selected: {select.selected_value}")
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def main():
|
|
69
|
+
pygame.init()
|
|
70
|
+
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
|
71
|
+
clock = pygame.time.Clock()
|
|
72
|
+
|
|
73
|
+
root = Widget((0, 0, SCREEN_WIDTH, SCREEN_HEIGHT), border_color=None)
|
|
74
|
+
ui = UIManager(root)
|
|
75
|
+
|
|
76
|
+
title = Text("pygame-widget-kit demo", pos=(24, 20), text_color=(0, 0, 0))
|
|
77
|
+
root.add_child(title)
|
|
78
|
+
|
|
79
|
+
input_box = TextInput(rect=(24, 70, 220, 36), initial_text="")
|
|
80
|
+
update_button = Button("Update label", pos=(256, 70), size=(160, 36))
|
|
81
|
+
value_label = Text("Label: (empty)", pos=(432, 76), text_color=(0, 0, 0))
|
|
82
|
+
update_button.click_bind(update_label_from_input, input_box, value_label)
|
|
83
|
+
|
|
84
|
+
root.add_child(input_box)
|
|
85
|
+
root.add_child(update_button)
|
|
86
|
+
root.add_child(value_label)
|
|
87
|
+
|
|
88
|
+
input_types_title = Text("input types:", pos=(24, 150), text_color=(0, 0, 0))
|
|
89
|
+
root.add_child(input_types_title)
|
|
90
|
+
|
|
91
|
+
input_rows = [
|
|
92
|
+
("Allow all:", ALLOW_ALL_CHARS),
|
|
93
|
+
("Text only:", TEXT_ONLY),
|
|
94
|
+
("Number only:", NUMBER_ONLY),
|
|
95
|
+
("Hex only:", HEX_ONLY),
|
|
96
|
+
("Binary only:", BINARY_ONLY),
|
|
97
|
+
]
|
|
98
|
+
|
|
99
|
+
input_label_x = 24
|
|
100
|
+
input_box_x = 200
|
|
101
|
+
input_row_y = 190
|
|
102
|
+
input_row_gap = 44
|
|
103
|
+
|
|
104
|
+
for index, (label_text, mode) in enumerate(input_rows):
|
|
105
|
+
row_y = input_row_y + index * input_row_gap
|
|
106
|
+
row_label = Text(label_text, pos=(input_label_x, row_y + 6), text_color=(0, 0, 0))
|
|
107
|
+
row_input = TextInput(rect=(input_box_x, row_y, 240, 34), allowed_char_mode=mode)
|
|
108
|
+
root.add_child(row_label)
|
|
109
|
+
root.add_child(row_input)
|
|
110
|
+
|
|
111
|
+
select_label = Text("Select:", pos=(24, 430), text_color=(0, 0, 0))
|
|
112
|
+
select = Select(
|
|
113
|
+
rect=(100, 424, 160, 32),
|
|
114
|
+
options=["Easy", "Hard", "Expert"],
|
|
115
|
+
default_index=0,
|
|
116
|
+
z_index=2
|
|
117
|
+
)
|
|
118
|
+
select_value = Text("Selected: Easy", pos=(280, 430), text_color=(0, 0, 0))
|
|
119
|
+
select.bind_on_option_chance(update_select_label, select, select_value)
|
|
120
|
+
|
|
121
|
+
root.add_child(select_label)
|
|
122
|
+
root.add_child(select)
|
|
123
|
+
root.add_child(select_value)
|
|
124
|
+
|
|
125
|
+
radio_label = Text("Radio:", pos=(24, 490), text_color=(0, 0, 0))
|
|
126
|
+
radio = Radio(
|
|
127
|
+
(100, 486, 150, 28),
|
|
128
|
+
options=["OPTION A","OPTION B","OPTION C"],
|
|
129
|
+
default_index=0,
|
|
130
|
+
)
|
|
131
|
+
radio_value = Text("Radio: A", pos=(280, 490), text_color=(0, 0, 0))
|
|
132
|
+
|
|
133
|
+
root.add_child(radio_label)
|
|
134
|
+
root.add_child(radio)
|
|
135
|
+
root.add_child(radio_value)
|
|
136
|
+
|
|
137
|
+
radio_last_value = radio.get_value()
|
|
138
|
+
|
|
139
|
+
running = True
|
|
140
|
+
while running:
|
|
141
|
+
clock.tick(60)
|
|
142
|
+
for event in pygame.event.get():
|
|
143
|
+
if event.type == pygame.QUIT:
|
|
144
|
+
running = False
|
|
145
|
+
ui.handle_event(event)
|
|
146
|
+
|
|
147
|
+
current_radio_value = radio.get_value()
|
|
148
|
+
if current_radio_value != radio_last_value:
|
|
149
|
+
radio_value.set_text(f"Radio: {current_radio_value}")
|
|
150
|
+
radio_last_value = current_radio_value
|
|
151
|
+
|
|
152
|
+
screen.fill((250, 250, 250))
|
|
153
|
+
root.draw(screen)
|
|
154
|
+
pygame.display.flip()
|
|
155
|
+
|
|
156
|
+
pygame.quit()
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
if __name__ == "__main__":
|
|
160
|
+
main()
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
## Usage notes
|
|
167
|
+
|
|
168
|
+
- Use `UIManager` to handle events and manage focus/active state.
|
|
169
|
+
- Call `root.add_child(...)` to build a component tree. Child positions are relative to their parent.
|
|
170
|
+
- `TextInput` handles caret blink in `draw`, so you only need to call `draw` each frame.
|
|
171
|
+
- `Select` uses modal behavior through `UIManager`; clicks outside the dropdown close it.
|
|
172
|
+
- `Select` expects `options` as a list of strings and exposes `selected_value`.
|
|
173
|
+
- `Radio` expects `options` as a list of `(label, value)` tuples and exposes `get_value()`.
|
|
174
|
+
|
|
175
|
+
## TextInput allowed_char_mode
|
|
176
|
+
|
|
177
|
+
- `ALLOW_ALL_CHARS`
|
|
178
|
+
- `TEXT_ONLY`
|
|
179
|
+
- `NUMBER_ONLY`
|
|
180
|
+
- `HEX_ONLY`
|
|
181
|
+
- `BINARY_ONLY`
|
|
182
|
+
- `OCTAL_ONLY`
|
|
183
|
+
|
|
184
|
+
## Creating custom widgets
|
|
185
|
+
|
|
186
|
+
Subclass `UIComponent` and override any of the following hooks:
|
|
187
|
+
|
|
188
|
+
- `draw(surface)` for custom rendering
|
|
189
|
+
- `handle_event(event)` for keyboard/mouse logic
|
|
190
|
+
- `on_click(event)` for click behavior
|
|
191
|
+
|
|
192
|
+
## License
|
|
193
|
+
|
|
194
|
+
MIT
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
# pygame-widget-kit
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/pygame-widget-kit/)
|
|
4
|
+
|
|
5
|
+
A GUI framework built on Pygame. pygame-widget-kit is a retained-mode UI system with a component tree, event routing, focus and modal management, and extensible widgets such as Select/Dropdown and TextInput with mouse-based text selection and clipboard support.
|
|
6
|
+
|
|
7
|
+
PyPI: https://pypi.org/project/pygame-widget-kit/
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- Retained-mode component tree with nested children
|
|
12
|
+
- Event routing with hover, focus, and active state handling
|
|
13
|
+
- Modal/dropdown support via `UIManager`
|
|
14
|
+
- Built-in widgets: `Button`, `Text`, `Select`, `Radio`, `TextInput`
|
|
15
|
+
- Text input filtering modes (text, number, hex, binary, octal)
|
|
16
|
+
- Clipboard copy/paste in `TextInput` (requires `pygame.scrap`)
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
pip install pygame-widget-kit
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Pygame is listed as a dependency (pygame >= 2.0.0). Make sure you call `pygame.init()` before creating widgets.
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
|
|
30
|
+
import pygame
|
|
31
|
+
from pygame_widget_kit import *
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
SCREEN_WIDTH = 900
|
|
35
|
+
SCREEN_HEIGHT = 720
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def update_label_from_input(input_box: TextInput, label: Text):
|
|
39
|
+
label.set_text(f"Label: {input_box.text_value}")
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def update_select_label(select: Select, label: Text):
|
|
43
|
+
label.set_text(f"Selected: {select.selected_value}")
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def main():
|
|
47
|
+
pygame.init()
|
|
48
|
+
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
|
49
|
+
clock = pygame.time.Clock()
|
|
50
|
+
|
|
51
|
+
root = Widget((0, 0, SCREEN_WIDTH, SCREEN_HEIGHT), border_color=None)
|
|
52
|
+
ui = UIManager(root)
|
|
53
|
+
|
|
54
|
+
title = Text("pygame-widget-kit demo", pos=(24, 20), text_color=(0, 0, 0))
|
|
55
|
+
root.add_child(title)
|
|
56
|
+
|
|
57
|
+
input_box = TextInput(rect=(24, 70, 220, 36), initial_text="")
|
|
58
|
+
update_button = Button("Update label", pos=(256, 70), size=(160, 36))
|
|
59
|
+
value_label = Text("Label: (empty)", pos=(432, 76), text_color=(0, 0, 0))
|
|
60
|
+
update_button.click_bind(update_label_from_input, input_box, value_label)
|
|
61
|
+
|
|
62
|
+
root.add_child(input_box)
|
|
63
|
+
root.add_child(update_button)
|
|
64
|
+
root.add_child(value_label)
|
|
65
|
+
|
|
66
|
+
input_types_title = Text("input types:", pos=(24, 150), text_color=(0, 0, 0))
|
|
67
|
+
root.add_child(input_types_title)
|
|
68
|
+
|
|
69
|
+
input_rows = [
|
|
70
|
+
("Allow all:", ALLOW_ALL_CHARS),
|
|
71
|
+
("Text only:", TEXT_ONLY),
|
|
72
|
+
("Number only:", NUMBER_ONLY),
|
|
73
|
+
("Hex only:", HEX_ONLY),
|
|
74
|
+
("Binary only:", BINARY_ONLY),
|
|
75
|
+
]
|
|
76
|
+
|
|
77
|
+
input_label_x = 24
|
|
78
|
+
input_box_x = 200
|
|
79
|
+
input_row_y = 190
|
|
80
|
+
input_row_gap = 44
|
|
81
|
+
|
|
82
|
+
for index, (label_text, mode) in enumerate(input_rows):
|
|
83
|
+
row_y = input_row_y + index * input_row_gap
|
|
84
|
+
row_label = Text(label_text, pos=(input_label_x, row_y + 6), text_color=(0, 0, 0))
|
|
85
|
+
row_input = TextInput(rect=(input_box_x, row_y, 240, 34), allowed_char_mode=mode)
|
|
86
|
+
root.add_child(row_label)
|
|
87
|
+
root.add_child(row_input)
|
|
88
|
+
|
|
89
|
+
select_label = Text("Select:", pos=(24, 430), text_color=(0, 0, 0))
|
|
90
|
+
select = Select(
|
|
91
|
+
rect=(100, 424, 160, 32),
|
|
92
|
+
options=["Easy", "Hard", "Expert"],
|
|
93
|
+
default_index=0,
|
|
94
|
+
z_index=2
|
|
95
|
+
)
|
|
96
|
+
select_value = Text("Selected: Easy", pos=(280, 430), text_color=(0, 0, 0))
|
|
97
|
+
select.bind_on_option_chance(update_select_label, select, select_value)
|
|
98
|
+
|
|
99
|
+
root.add_child(select_label)
|
|
100
|
+
root.add_child(select)
|
|
101
|
+
root.add_child(select_value)
|
|
102
|
+
|
|
103
|
+
radio_label = Text("Radio:", pos=(24, 490), text_color=(0, 0, 0))
|
|
104
|
+
radio = Radio(
|
|
105
|
+
(100, 486, 150, 28),
|
|
106
|
+
options=["OPTION A","OPTION B","OPTION C"],
|
|
107
|
+
default_index=0,
|
|
108
|
+
)
|
|
109
|
+
radio_value = Text("Radio: A", pos=(280, 490), text_color=(0, 0, 0))
|
|
110
|
+
|
|
111
|
+
root.add_child(radio_label)
|
|
112
|
+
root.add_child(radio)
|
|
113
|
+
root.add_child(radio_value)
|
|
114
|
+
|
|
115
|
+
radio_last_value = radio.get_value()
|
|
116
|
+
|
|
117
|
+
running = True
|
|
118
|
+
while running:
|
|
119
|
+
clock.tick(60)
|
|
120
|
+
for event in pygame.event.get():
|
|
121
|
+
if event.type == pygame.QUIT:
|
|
122
|
+
running = False
|
|
123
|
+
ui.handle_event(event)
|
|
124
|
+
|
|
125
|
+
current_radio_value = radio.get_value()
|
|
126
|
+
if current_radio_value != radio_last_value:
|
|
127
|
+
radio_value.set_text(f"Radio: {current_radio_value}")
|
|
128
|
+
radio_last_value = current_radio_value
|
|
129
|
+
|
|
130
|
+
screen.fill((250, 250, 250))
|
|
131
|
+
root.draw(screen)
|
|
132
|
+
pygame.display.flip()
|
|
133
|
+
|
|
134
|
+
pygame.quit()
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
if __name__ == "__main__":
|
|
138
|
+
main()
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## Usage notes
|
|
145
|
+
|
|
146
|
+
- Use `UIManager` to handle events and manage focus/active state.
|
|
147
|
+
- Call `root.add_child(...)` to build a component tree. Child positions are relative to their parent.
|
|
148
|
+
- `TextInput` handles caret blink in `draw`, so you only need to call `draw` each frame.
|
|
149
|
+
- `Select` uses modal behavior through `UIManager`; clicks outside the dropdown close it.
|
|
150
|
+
- `Select` expects `options` as a list of strings and exposes `selected_value`.
|
|
151
|
+
- `Radio` expects `options` as a list of `(label, value)` tuples and exposes `get_value()`.
|
|
152
|
+
|
|
153
|
+
## TextInput allowed_char_mode
|
|
154
|
+
|
|
155
|
+
- `ALLOW_ALL_CHARS`
|
|
156
|
+
- `TEXT_ONLY`
|
|
157
|
+
- `NUMBER_ONLY`
|
|
158
|
+
- `HEX_ONLY`
|
|
159
|
+
- `BINARY_ONLY`
|
|
160
|
+
- `OCTAL_ONLY`
|
|
161
|
+
|
|
162
|
+
## Creating custom widgets
|
|
163
|
+
|
|
164
|
+
Subclass `UIComponent` and override any of the following hooks:
|
|
165
|
+
|
|
166
|
+
- `draw(surface)` for custom rendering
|
|
167
|
+
- `handle_event(event)` for keyboard/mouse logic
|
|
168
|
+
- `on_click(event)` for click behavior
|
|
169
|
+
|
|
170
|
+
## License
|
|
171
|
+
|
|
172
|
+
MIT
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
from pygame_widget_kit import *
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
SCREEN_WIDTH = 900
|
|
6
|
+
SCREEN_HEIGHT = 720
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def update_label_from_input(input_box: TextInput, label: Text):
|
|
10
|
+
label.set_text(f"Label: {input_box.text_value}")
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def update_select_label(select: Select, label: Text):
|
|
14
|
+
label.set_text(f"Selected: {select.selected_value}")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def main():
|
|
18
|
+
pygame.init()
|
|
19
|
+
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
|
20
|
+
clock = pygame.time.Clock()
|
|
21
|
+
|
|
22
|
+
root = Widget((0, 0, SCREEN_WIDTH, SCREEN_HEIGHT), border_color=None)
|
|
23
|
+
ui = UIManager(root)
|
|
24
|
+
|
|
25
|
+
title = Text("pygame-widget-kit demo", pos=(24, 20), text_color=(0, 0, 0))
|
|
26
|
+
root.add_child(title)
|
|
27
|
+
|
|
28
|
+
input_box = TextInput(rect=(24, 70, 220, 36), initial_text="")
|
|
29
|
+
update_button = Button("Update label", pos=(256, 70), size=(160, 36))
|
|
30
|
+
value_label = Text("Label: (empty)", pos=(432, 76), text_color=(0, 0, 0))
|
|
31
|
+
update_button.click_bind(update_label_from_input, input_box, value_label)
|
|
32
|
+
|
|
33
|
+
root.add_child(input_box)
|
|
34
|
+
root.add_child(update_button)
|
|
35
|
+
root.add_child(value_label)
|
|
36
|
+
|
|
37
|
+
input_types_title = Text("input types:", pos=(24, 150), text_color=(0, 0, 0))
|
|
38
|
+
root.add_child(input_types_title)
|
|
39
|
+
|
|
40
|
+
input_rows = [
|
|
41
|
+
("Allow all:", ALLOW_ALL_CHARS),
|
|
42
|
+
("Text only:", TEXT_ONLY),
|
|
43
|
+
("Number only:", NUMBER_ONLY),
|
|
44
|
+
("Hex only:", HEX_ONLY),
|
|
45
|
+
("Binary only:", BINARY_ONLY),
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
input_label_x = 24
|
|
49
|
+
input_box_x = 200
|
|
50
|
+
input_row_y = 190
|
|
51
|
+
input_row_gap = 44
|
|
52
|
+
|
|
53
|
+
for index, (label_text, mode) in enumerate(input_rows):
|
|
54
|
+
row_y = input_row_y + index * input_row_gap
|
|
55
|
+
row_label = Text(label_text, pos=(input_label_x, row_y + 6), text_color=(0, 0, 0))
|
|
56
|
+
row_input = TextInput(rect=(input_box_x, row_y, 240, 34), allowed_char_mode=mode)
|
|
57
|
+
root.add_child(row_label)
|
|
58
|
+
root.add_child(row_input)
|
|
59
|
+
|
|
60
|
+
select_label = Text("Select:", pos=(24, 430), text_color=(0, 0, 0))
|
|
61
|
+
select = Select(
|
|
62
|
+
rect=(100, 424, 160, 32),
|
|
63
|
+
options=["Easy", "Hard", "Expert"],
|
|
64
|
+
default_index=0,
|
|
65
|
+
z_index=2
|
|
66
|
+
)
|
|
67
|
+
select_value = Text("Selected: Easy", pos=(280, 430), text_color=(0, 0, 0))
|
|
68
|
+
select.bind_on_option_chance(update_select_label, select, select_value)
|
|
69
|
+
|
|
70
|
+
root.add_child(select_label)
|
|
71
|
+
root.add_child(select)
|
|
72
|
+
root.add_child(select_value)
|
|
73
|
+
|
|
74
|
+
radio_label = Text("Radio:", pos=(24, 490), text_color=(0, 0, 0))
|
|
75
|
+
radio = Radio(
|
|
76
|
+
(100, 486, 150, 28),
|
|
77
|
+
options=["OPTION A","OPTION B","OPTION C"],
|
|
78
|
+
default_index=0,
|
|
79
|
+
)
|
|
80
|
+
radio_value = Text("Radio: A", pos=(280, 490), text_color=(0, 0, 0))
|
|
81
|
+
|
|
82
|
+
root.add_child(radio_label)
|
|
83
|
+
root.add_child(radio)
|
|
84
|
+
root.add_child(radio_value)
|
|
85
|
+
|
|
86
|
+
radio_last_value = radio.get_value()
|
|
87
|
+
|
|
88
|
+
running = True
|
|
89
|
+
while running:
|
|
90
|
+
clock.tick(60)
|
|
91
|
+
for event in pygame.event.get():
|
|
92
|
+
if event.type == pygame.QUIT:
|
|
93
|
+
running = False
|
|
94
|
+
ui.handle_event(event)
|
|
95
|
+
|
|
96
|
+
current_radio_value = radio.get_value()
|
|
97
|
+
if current_radio_value != radio_last_value:
|
|
98
|
+
radio_value.set_text(f"Radio: {current_radio_value}")
|
|
99
|
+
radio_last_value = current_radio_value
|
|
100
|
+
|
|
101
|
+
screen.fill((250, 250, 250))
|
|
102
|
+
root.draw(screen)
|
|
103
|
+
pygame.display.flip()
|
|
104
|
+
|
|
105
|
+
pygame.quit()
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
if __name__ == "__main__":
|
|
109
|
+
main()
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import pygame
|
|
2
2
|
from .Text import *
|
|
3
3
|
from .UIComponent import *
|
|
4
|
+
from functools import partial
|
|
5
|
+
|
|
4
6
|
|
|
5
7
|
class Button(UIComponent):
|
|
6
8
|
def __init__(self, text_str = "Button",
|
|
@@ -8,20 +10,30 @@ class Button(UIComponent):
|
|
|
8
10
|
size = (200,40),
|
|
9
11
|
style=None,
|
|
10
12
|
z_index=0,
|
|
11
|
-
color=(
|
|
12
|
-
border_color =
|
|
13
|
+
color=(175,175,175),
|
|
14
|
+
border_color = (100,100,100),
|
|
13
15
|
hover_color=(150,150,150),
|
|
14
|
-
text_color = (0,0,0)
|
|
16
|
+
text_color = (0,0,0),
|
|
17
|
+
padding = (20,10)
|
|
18
|
+
):
|
|
15
19
|
self.text_str = text_str
|
|
16
20
|
self.size = size
|
|
17
21
|
self.text_color =text_color
|
|
18
|
-
self.
|
|
22
|
+
self.padding = padding
|
|
23
|
+
self.text = Text(self.text_str,pos = self.padding,text_color=text_color,bg_color=None)
|
|
24
|
+
|
|
25
|
+
self.click_function = None
|
|
26
|
+
|
|
19
27
|
|
|
20
28
|
rect = (pos[0],pos[1],self.size[0],self.size[1])
|
|
21
|
-
|
|
29
|
+
|
|
22
30
|
super().__init__(rect, style, z_index, color, border_color, hover_color)
|
|
23
31
|
self.add_child(self.text)
|
|
24
32
|
|
|
33
|
+
def click_bind(self, func, *args):
|
|
34
|
+
self.click_function = partial(func, *args)
|
|
35
|
+
|
|
36
|
+
|
|
25
37
|
def draw(self, surface):
|
|
26
38
|
if self.visible == False:
|
|
27
39
|
return
|
|
@@ -32,6 +44,9 @@ class Button(UIComponent):
|
|
|
32
44
|
|
|
33
45
|
if color is not None:
|
|
34
46
|
pygame.draw.rect(surface,color,self.absolute_rect,0)
|
|
47
|
+
if self.border_color:
|
|
48
|
+
pygame.draw.rect(surface,self.border_color,self.absolute_rect,2)
|
|
49
|
+
|
|
35
50
|
|
|
36
51
|
self.text.draw(surface)
|
|
37
52
|
|
|
@@ -44,5 +59,8 @@ class Button(UIComponent):
|
|
|
44
59
|
self.active = False
|
|
45
60
|
|
|
46
61
|
def on_click(self, event):
|
|
47
|
-
|
|
48
|
-
|
|
62
|
+
if self.click_function is not None:
|
|
63
|
+
self.click_function()
|
|
64
|
+
else:
|
|
65
|
+
print("Button clicked")
|
|
66
|
+
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
from .UIComponent import *
|
|
3
|
+
from .Widget import *
|
|
4
|
+
from .Button import *
|
|
5
|
+
|
|
6
|
+
class Radio(Widget):
|
|
7
|
+
def __init__(
|
|
8
|
+
self,
|
|
9
|
+
rect,
|
|
10
|
+
options: list[str],
|
|
11
|
+
default_index=0,
|
|
12
|
+
item_height=24,
|
|
13
|
+
spacing=6,
|
|
14
|
+
z_index=0,
|
|
15
|
+
color=(230, 230, 230),
|
|
16
|
+
border_color=(180, 180, 180),
|
|
17
|
+
check_color=(60, 160, 255),
|
|
18
|
+
text_color=(0, 0, 0),
|
|
19
|
+
):
|
|
20
|
+
super().__init__(rect, color=None, border_color=None, z_index=z_index)
|
|
21
|
+
|
|
22
|
+
self.options_text = options
|
|
23
|
+
self.selected_index = default_index
|
|
24
|
+
|
|
25
|
+
self.item_height = item_height
|
|
26
|
+
self.spacing = spacing
|
|
27
|
+
|
|
28
|
+
self.color = color
|
|
29
|
+
self.border_color = border_color
|
|
30
|
+
self.check_color = check_color
|
|
31
|
+
|
|
32
|
+
self.text_color = text_color
|
|
33
|
+
|
|
34
|
+
self.options:list[RadioOption] = []
|
|
35
|
+
self._build_options()
|
|
36
|
+
|
|
37
|
+
def _build_options(self):
|
|
38
|
+
y = 0
|
|
39
|
+
|
|
40
|
+
for i, text in enumerate(self.options_text):
|
|
41
|
+
opt = RadioOption(
|
|
42
|
+
rect=(0, y, self.rect[2], self.item_height),
|
|
43
|
+
index=i,
|
|
44
|
+
text=text,
|
|
45
|
+
radio=self,
|
|
46
|
+
color=self.color,
|
|
47
|
+
border_color=self.border_color,
|
|
48
|
+
check_color=self.check_color,
|
|
49
|
+
text_color=self.text_color,
|
|
50
|
+
)
|
|
51
|
+
self.options.append(opt)
|
|
52
|
+
self.add_child(opt)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
y += self.item_height + self.spacing
|
|
56
|
+
def get_value(self):
|
|
57
|
+
if not self.options_text:
|
|
58
|
+
return None
|
|
59
|
+
return self.options_text[self.selected_index]
|
|
60
|
+
|
|
61
|
+
def set_index(self, index):
|
|
62
|
+
if 0 <= index < len(self.options_text):
|
|
63
|
+
self.selected_index = index
|
|
64
|
+
|
|
65
|
+
def handle_event(self, event):
|
|
66
|
+
if not self.visible or not self.enabled:
|
|
67
|
+
return
|
|
68
|
+
for child in self.children:
|
|
69
|
+
child.handle_event(event)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class RadioOption(Button):
|
|
75
|
+
def __init__(
|
|
76
|
+
self,
|
|
77
|
+
rect,
|
|
78
|
+
index,
|
|
79
|
+
text:str,
|
|
80
|
+
radio:Radio,
|
|
81
|
+
color,
|
|
82
|
+
border_color,
|
|
83
|
+
check_color,
|
|
84
|
+
text_color,
|
|
85
|
+
):
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
self.radio = radio
|
|
91
|
+
self.index = index
|
|
92
|
+
self.check_color = check_color
|
|
93
|
+
|
|
94
|
+
self.circle_radius = rect[3] // 3
|
|
95
|
+
self.circle_center_offset = (self.circle_radius + 4, rect[3] // 2)
|
|
96
|
+
|
|
97
|
+
super().__init__(text_str=text,pos = (rect[0],rect[1]),size=(rect[2],rect[3]),text_color = text_color,
|
|
98
|
+
border_color=border_color,color=color,padding=(self.circle_radius*3,0))
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def draw(self, surface: pygame.Surface):
|
|
102
|
+
if not self.visible:
|
|
103
|
+
return
|
|
104
|
+
|
|
105
|
+
x, y, w, h = self.absolute_rect
|
|
106
|
+
cx = x + self.circle_center_offset[0]
|
|
107
|
+
cy = y + self.circle_center_offset[1]
|
|
108
|
+
|
|
109
|
+
# hover background
|
|
110
|
+
if self.hovered:
|
|
111
|
+
pygame.draw.rect(surface, (60, 60, 60), self.absolute_rect, 0)
|
|
112
|
+
|
|
113
|
+
# outer circle
|
|
114
|
+
pygame.draw.circle(surface, self.border_color, (cx, cy), self.circle_radius, 2)
|
|
115
|
+
|
|
116
|
+
# inner dot
|
|
117
|
+
if self.radio.selected_index == self.index:
|
|
118
|
+
pygame.draw.circle(surface, self.check_color, (cx, cy), self.circle_radius - 4)
|
|
119
|
+
|
|
120
|
+
self.text.draw(surface)
|
|
121
|
+
|
|
122
|
+
def on_click(self, event):
|
|
123
|
+
self.radio.selected_index = self.index
|
|
124
|
+
if self.click_function is not None:
|
|
125
|
+
self.click_function()
|