PlayPy 0.2.1__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.
- playpy/__init__.py +222 -0
- playpy/builtin.py +638 -0
- playpy/data/default_icon.ppm +5 -0
- playpy/elements.py +205 -0
- playpy/resources.py +147 -0
- playpy/state.py +565 -0
- playpy/workspace.py +432 -0
- playpy-0.2.1.dist-info/METADATA +191 -0
- playpy-0.2.1.dist-info/RECORD +12 -0
- playpy-0.2.1.dist-info/WHEEL +5 -0
- playpy-0.2.1.dist-info/licenses/LICENSE +21 -0
- playpy-0.2.1.dist-info/top_level.txt +1 -0
playpy/__init__.py
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
from .resources import *
|
|
2
|
+
from .state import *
|
|
3
|
+
from .elements import *
|
|
4
|
+
from .workspace import *
|
|
5
|
+
from .builtin import *
|
|
6
|
+
|
|
7
|
+
__doc__ = r'''
|
|
8
|
+
# PlayPy
|
|
9
|
+
|
|
10
|
+
PlayPy is a lightweight Python library for creating simple games and interactive applications with ease. It provides a straightforward API for handling graphics, input, and basic game mechanics, making it ideal for beginners and those looking to quickly prototype their ideas.
|
|
11
|
+
|
|
12
|
+
## Requirements
|
|
13
|
+
|
|
14
|
+
- Python `>=3.11`
|
|
15
|
+
- `pygame(-ce) >=2.6.1`
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
To install PlayPy, enter the following line into your terminal: `pip install playpy`, or `python -m pip install playpy` if the first line does not work.
|
|
20
|
+
|
|
21
|
+
## Core Concepts
|
|
22
|
+
|
|
23
|
+
### Workspace
|
|
24
|
+
|
|
25
|
+
`Workspace` owns the window, render loop, input state, scene stack, and modal stack.
|
|
26
|
+
|
|
27
|
+
Key methods:
|
|
28
|
+
|
|
29
|
+
- `run()` starts runtime loop
|
|
30
|
+
- `quit()` stops runtime loop
|
|
31
|
+
- `queue_scene_change(scene)` replaces the current scene
|
|
32
|
+
- `queue_scene_push(scene)` / `queue_scene_pop()` stacks scenes
|
|
33
|
+
- `queue_modal_push(element)` / `queue_modal_pop()` manages overlays
|
|
34
|
+
|
|
35
|
+
### Layout Values
|
|
36
|
+
|
|
37
|
+
PlayPy uses two rectangle value types:
|
|
38
|
+
|
|
39
|
+
- `FRect(*args)`
|
|
40
|
+
Relative scale (fraction of parent rectangle).
|
|
41
|
+
- `Rect(*args)`
|
|
42
|
+
Absolute pixel offsets applied on top of scale.
|
|
43
|
+
- Overloads are: `x, y, w, h` \ `topleft, size` \ `(x, y, w, h)` \ `(topleft, size)`
|
|
44
|
+
|
|
45
|
+
Final element rect = `scale * parent_size + offset`.
|
|
46
|
+
|
|
47
|
+
### Parenting
|
|
48
|
+
|
|
49
|
+
Any `UIElement` can contain children. Assigning parent wires it automatically:
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
child.parent = parent
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Or use helpers:
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
parent.add_child(child)
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
You can use the `parent`, `ancestors`, `children`, and `descendants` properties to find ancestors or descendants of an element.
|
|
62
|
+
|
|
63
|
+
You can use other built-in methods to tell descendance:
|
|
64
|
+
- `is_parent_of(child)`, `is_child_of(parent)`
|
|
65
|
+
- `is_ancestor_of(descendant)`, `is_descendant_of(ancestor)`
|
|
66
|
+
|
|
67
|
+
## Built-in Elements
|
|
68
|
+
|
|
69
|
+
- `UIPanel`: colored rectangle container
|
|
70
|
+
- `UIScrollablePanel`: panel with wheel scrolling
|
|
71
|
+
- `UIText`: wrapped text rendering with alignment
|
|
72
|
+
- `UIButton`: clickable button with hover/pressed colors
|
|
73
|
+
- `UITextbox`: single-line text input with placeholder/caret
|
|
74
|
+
- `Scene`: root container for scene lifecycle
|
|
75
|
+
|
|
76
|
+
## Modifiers
|
|
77
|
+
|
|
78
|
+
Modifiers attach style/behavior to a single element.
|
|
79
|
+
|
|
80
|
+
- `UIPadding(scale=0, offset=10)` - Adds padding to the element.
|
|
81
|
+
- `UIOutline(color, width, edge_type)` - Adds an outline to the element.
|
|
82
|
+
- `UIBorderRadius(radius)` - Adds a border radius (rounded corners) to the element.
|
|
83
|
+
- `UIGradient(start_color, end_color, direction)` - Converts the background color of the element to a gradient.
|
|
84
|
+
- `UIFont(font_path=None, font_size=None, bold=None, italic=None, antialias=None)` - Changes the font of the text.
|
|
85
|
+
- `GlobalElement()` - If added to a `Workspace` descended element, makes this object shown and handled even when another scene is running.
|
|
86
|
+
|
|
87
|
+
Attach/get/remove:
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
element.set_modifier(plp.UIOutline((0, 0, 0), 2, "middle"))
|
|
91
|
+
outline = element.get_modifier(plp.UIOutline)
|
|
92
|
+
element.remove_modifier(plp.UIOutline)
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Event Helpers
|
|
96
|
+
|
|
97
|
+
Decorator helpers create `Event` elements attached to a workspace, scene, or element.
|
|
98
|
+
|
|
99
|
+
- `@on_start(target)` - Runs when the scene/workspace starts running/
|
|
100
|
+
- `@on_update(target)`
|
|
101
|
+
- `@on_quit(target)`
|
|
102
|
+
- `@on_scene_change(target)`
|
|
103
|
+
- `@on_modal_change(target)`
|
|
104
|
+
- `@create_event(target, condition)` for custom conditions
|
|
105
|
+
|
|
106
|
+
Events created on the main workspace will trigger in all scenes. Pass in `global_event=false` to disable this.
|
|
107
|
+
|
|
108
|
+
Example:
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
@plp.on_update(ws)
|
|
112
|
+
def tick(w: plp.Workspace):
|
|
113
|
+
if plp.Key.ESCAPE in w.input.key_downs:
|
|
114
|
+
w.quit()
|
|
115
|
+
|
|
116
|
+
@plp.on_update(ws, global_event=false)
|
|
117
|
+
def tick(w: plp.Workspace):
|
|
118
|
+
if plp.Key.UP in w.input.key_downs:
|
|
119
|
+
w.push_scene(s)
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Scene and Modal Behavior
|
|
123
|
+
|
|
124
|
+
- If a modal is active, input is only routed to the modal tree
|
|
125
|
+
- Scene descendants are clipped to the scene rectangle for both drawing and hit-testing
|
|
126
|
+
- `Workspace` tracks scene/modal transitions with:
|
|
127
|
+
- `current_scene`, `previous_scene`, `scene_changed`
|
|
128
|
+
- `current_modal`, `previous_modal`, `modal_changed`
|
|
129
|
+
- Scenes can implement lifecycle hooks:
|
|
130
|
+
- `on_enter`, `on_exit`, `on_pause`, `on_resume`
|
|
131
|
+
|
|
132
|
+
## Input State
|
|
133
|
+
|
|
134
|
+
Read per-frame input from `workspace.input`:
|
|
135
|
+
|
|
136
|
+
- `keys_pressed`, `key_downs`, `key_ups` contain `plp.Key` values
|
|
137
|
+
- `mouse_buttons_pressed`, `mouse_downs`, `mouse_ups` contain `plp.MouseButton` values
|
|
138
|
+
- `mouse_pos`, `mouse_delta`, `mouse_wheel`
|
|
139
|
+
- `text_input`
|
|
140
|
+
- `dt`, `runtime`, `quit`
|
|
141
|
+
- `key_held(key)`, `key_up(key)`, `key_down(key)`
|
|
142
|
+
- `mousebutton_held(mb)`, `mousebutton_up(mb)`, `mousebutton_down(mb)`
|
|
143
|
+
|
|
144
|
+
### Hover State
|
|
145
|
+
|
|
146
|
+
You can check the hovered objects and top hovered object using the helper methods in `Workspace`.
|
|
147
|
+
|
|
148
|
+
- `is_mouse_top(element)`, `just_hovered(element)`, `just_unhovered(element)` are for checking only the top hovered object.
|
|
149
|
+
- `is_mouse_over(element)`, `just_hovered_inclusive(element)`, `just_unhovered_inclusive(element)` are for checking all of the hovered objects.
|
|
150
|
+
|
|
151
|
+
event helpers come with these too.
|
|
152
|
+
|
|
153
|
+
- `@on_hover(scope, hovered)`, `@on_unhover(scope, hovered)`, `while_hovered(scope, hovered)` for only top.
|
|
154
|
+
- `@on_hover_inclusive(scope, hovered)`, `@on_unhover_inclusive(scope, hovered)`, `while_hovered_inclusive(scope, hovered)` for all.
|
|
155
|
+
|
|
156
|
+
These are useful because they can then be used to check if the user is clicking an object
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
obj = plp.UIPanel(plp.FRect(1, 1, 0, 0), plp.empty_rect(), (0, 0, 0))
|
|
160
|
+
|
|
161
|
+
@while_hovered(ws, obj)
|
|
162
|
+
def click_check(w: plp.Workspace):
|
|
163
|
+
if w.input.mousebutton_down(plp.MouseButton.LEFT):
|
|
164
|
+
print("LMB down!")
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
'''
|
|
168
|
+
|
|
169
|
+
__all__ = [
|
|
170
|
+
"ColorValue",
|
|
171
|
+
"RectValue",
|
|
172
|
+
"FRectValue",
|
|
173
|
+
"CoordinateValue",
|
|
174
|
+
"FCoordinateValue",
|
|
175
|
+
"Key",
|
|
176
|
+
"MouseButton",
|
|
177
|
+
"InputState",
|
|
178
|
+
"FRect",
|
|
179
|
+
"Rect",
|
|
180
|
+
"empty_rect",
|
|
181
|
+
"empty_frect",
|
|
182
|
+
"UIModifier",
|
|
183
|
+
"UIElement",
|
|
184
|
+
"Scene",
|
|
185
|
+
"init",
|
|
186
|
+
"Workspace",
|
|
187
|
+
"DATA_DIR",
|
|
188
|
+
"DEFAULT_ICON_PATH",
|
|
189
|
+
"__version__",
|
|
190
|
+
"enter_debug_mode",
|
|
191
|
+
"init",
|
|
192
|
+
"Log",
|
|
193
|
+
"LogWarning",
|
|
194
|
+
"LogError",
|
|
195
|
+
"InitializationError",
|
|
196
|
+
"InitializationWarning",
|
|
197
|
+
"Severity",
|
|
198
|
+
"log",
|
|
199
|
+
"UIPadding",
|
|
200
|
+
"UIOutline",
|
|
201
|
+
"UIBorderRadius",
|
|
202
|
+
"UIGradient",
|
|
203
|
+
"UIFont",
|
|
204
|
+
"UIPanel",
|
|
205
|
+
"UIScrollablePanel",
|
|
206
|
+
"UIText",
|
|
207
|
+
"UIButton",
|
|
208
|
+
"UITextbox",
|
|
209
|
+
"Event",
|
|
210
|
+
"create_event",
|
|
211
|
+
"on_start",
|
|
212
|
+
"on_update",
|
|
213
|
+
"on_quit",
|
|
214
|
+
"on_scene_change",
|
|
215
|
+
"on_modal_change",
|
|
216
|
+
"on_hover",
|
|
217
|
+
"on_unhover",
|
|
218
|
+
"while_hovered",
|
|
219
|
+
"on_hover_inclusive",
|
|
220
|
+
"on_unhover_inclusive",
|
|
221
|
+
"while_hovered_inclusive",
|
|
222
|
+
]
|