boa-engine 1.0.0rc1__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.
- boa_engine/__init__.py +30 -0
- boa_engine/core/__init__.py +0 -0
- boa_engine/core/animation.py +184 -0
- boa_engine/core/assetmgr.py +199 -0
- boa_engine/core/audiomgr.py +397 -0
- boa_engine/core/camera.py +48 -0
- boa_engine/core/controllermgr.py +313 -0
- boa_engine/core/dependencies.py +43 -0
- boa_engine/core/eventbus.py +140 -0
- boa_engine/core/graphics.py +294 -0
- boa_engine/core/inputmgr.py +240 -0
- boa_engine/core/manifest.py +63 -0
- boa_engine/core/modmgr.py +119 -0
- boa_engine/core/overlaymgr.py +127 -0
- boa_engine/core/pluginmgr.py +94 -0
- boa_engine/core/profiler.py +45 -0
- boa_engine/core/scene.py +368 -0
- boa_engine/core/scenemgr.py +423 -0
- boa_engine/core/settingsmgr.py +185 -0
- boa_engine/core/spritesheet.py +26 -0
- boa_engine/core/texturecache.py +40 -0
- boa_engine/core/transitions.py +56 -0
- boa_engine/core/ui/__init__.py +29 -0
- boa_engine/core/ui/animated.py +30 -0
- boa_engine/core/ui/background.py +48 -0
- boa_engine/core/ui/button.py +51 -0
- boa_engine/core/ui/cache.py +65 -0
- boa_engine/core/ui/container.py +94 -0
- boa_engine/core/ui/focus.py +131 -0
- boa_engine/core/ui/guidebar.py +80 -0
- boa_engine/core/ui/hud.py +486 -0
- boa_engine/core/ui/image.py +34 -0
- boa_engine/core/ui/loading.py +23 -0
- boa_engine/core/ui/popup.py +85 -0
- boa_engine/core/ui/text.py +37 -0
- boa_engine/core/ui/theme.py +10 -0
- boa_engine/core/ui/toast.py +80 -0
- boa_engine/core/version.py +7 -0
- boa_engine/py.typed +0 -0
- boa_engine-1.0.0rc1.dist-info/METADATA +169 -0
- boa_engine-1.0.0rc1.dist-info/RECORD +45 -0
- boa_engine-1.0.0rc1.dist-info/WHEEL +5 -0
- boa_engine-1.0.0rc1.dist-info/licenses/LICENSE +674 -0
- boa_engine-1.0.0rc1.dist-info/licenses/THIRD_PARTY_NOTICES.md +27 -0
- boa_engine-1.0.0rc1.dist-info/top_level.txt +1 -0
boa_engine/__init__.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"""BOA Engine public API (beta). Imports are resolved lazily."""
|
|
2
|
+
from importlib import import_module
|
|
3
|
+
from .core.version import __version__, __license__, __license_id__
|
|
4
|
+
|
|
5
|
+
_EXPORTS = {
|
|
6
|
+
'GraphicsLibrary': 'graphics', 'SceneManager': 'scenemgr',
|
|
7
|
+
'Scene': 'scene', 'SceneObject': 'scene', 'RenderLayer': 'scene',
|
|
8
|
+
'AssetManager': 'assetmgr', 'AssetLoadError': 'assetmgr',
|
|
9
|
+
'AudioManager': 'audiomgr', 'Camera': 'camera', 'EventBus': 'eventbus',
|
|
10
|
+
'Event': 'eventbus', 'SettingsManager': 'settingsmgr',
|
|
11
|
+
'InputManager': 'inputmgr', 'ControllerManager': 'controllermgr',
|
|
12
|
+
'SpriteSheet': 'spritesheet', 'Animation': 'animation',
|
|
13
|
+
'AnimationSequence': 'animation', 'Wait': 'animation',
|
|
14
|
+
'ModManager': 'modmgr', 'PluginContext': 'pluginmgr',
|
|
15
|
+
'Profiler': 'profiler', 'TextureCache': 'texturecache',
|
|
16
|
+
'Transition': 'transitions', 'DependencyError': 'dependencies',
|
|
17
|
+
}
|
|
18
|
+
__all__ = ['__version__', '__license__', '__license_id__', *_EXPORTS]
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def __getattr__(name):
|
|
22
|
+
if name not in _EXPORTS:
|
|
23
|
+
raise AttributeError(f'module {__name__!r} has no attribute {name!r}')
|
|
24
|
+
value = getattr(import_module(f'.core.{_EXPORTS[name]}', __name__), name)
|
|
25
|
+
globals()[name] = value
|
|
26
|
+
return value
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def __dir__():
|
|
30
|
+
return sorted(set(globals()) | set(__all__))
|
|
File without changes
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import pygame
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class Animation:
|
|
5
|
+
def __init__(
|
|
6
|
+
self,
|
|
7
|
+
image: pygame.Surface,
|
|
8
|
+
rect: pygame.Rect,
|
|
9
|
+
animation: str = "fade-in",
|
|
10
|
+
duration: float = 1.0,
|
|
11
|
+
easing: str = "ease-out",
|
|
12
|
+
distance: int = 100,
|
|
13
|
+
):
|
|
14
|
+
self.image = image
|
|
15
|
+
self.rect = rect.copy()
|
|
16
|
+
|
|
17
|
+
self.animation = animation
|
|
18
|
+
self.duration = max(0.001, duration)
|
|
19
|
+
self.easing = easing
|
|
20
|
+
self.distance = distance
|
|
21
|
+
|
|
22
|
+
self.elapsed = 0.0
|
|
23
|
+
self.finished = False
|
|
24
|
+
|
|
25
|
+
def _ease(self, t):
|
|
26
|
+
t = max(0.0, min(1.0, t))
|
|
27
|
+
|
|
28
|
+
if self.easing == "ease-in":
|
|
29
|
+
return t**3
|
|
30
|
+
|
|
31
|
+
if self.easing == "ease-out":
|
|
32
|
+
return 1 - (1 - t) ** 3
|
|
33
|
+
|
|
34
|
+
if self.easing == "ease-in-out":
|
|
35
|
+
return 4 * t**3 if t < 0.5 else 1 - ((-2 * t + 2) ** 3) / 2
|
|
36
|
+
|
|
37
|
+
return t
|
|
38
|
+
|
|
39
|
+
def update(self, dt: float):
|
|
40
|
+
if self.finished:
|
|
41
|
+
return
|
|
42
|
+
|
|
43
|
+
self.elapsed = min(
|
|
44
|
+
self.elapsed + max(0.0, dt),
|
|
45
|
+
self.duration,
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
if self.elapsed >= self.duration:
|
|
49
|
+
self.finished = True
|
|
50
|
+
|
|
51
|
+
@property
|
|
52
|
+
def progress(self):
|
|
53
|
+
return self._ease(self.elapsed / self.duration)
|
|
54
|
+
|
|
55
|
+
def reset(self):
|
|
56
|
+
self.elapsed = 0.0
|
|
57
|
+
self.finished = False
|
|
58
|
+
|
|
59
|
+
def blit(self, surface: pygame.Surface):
|
|
60
|
+
t = self.progress
|
|
61
|
+
|
|
62
|
+
image = self.image
|
|
63
|
+
rect = self.rect.copy()
|
|
64
|
+
|
|
65
|
+
if self.animation == "fade-in":
|
|
66
|
+
image = self.image.copy()
|
|
67
|
+
image.set_alpha(round(255 * t))
|
|
68
|
+
|
|
69
|
+
elif self.animation == "fade-out":
|
|
70
|
+
image = self.image.copy()
|
|
71
|
+
image.set_alpha(round(255 * (1 - t)))
|
|
72
|
+
|
|
73
|
+
elif self.animation == "slide-in":
|
|
74
|
+
rect.x -= round(self.distance * (1 - t))
|
|
75
|
+
|
|
76
|
+
elif self.animation == "zoom-in":
|
|
77
|
+
scale = max(0.001, t)
|
|
78
|
+
|
|
79
|
+
width = max(1, round(self.rect.width * scale))
|
|
80
|
+
height = max(1, round(self.rect.height * scale))
|
|
81
|
+
|
|
82
|
+
image = pygame.transform.smoothscale(
|
|
83
|
+
self.image,
|
|
84
|
+
(width, height),
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
rect = image.get_rect(center=self.rect.center)
|
|
88
|
+
|
|
89
|
+
else:
|
|
90
|
+
raise ValueError(f"Unknown animation: {self.animation}")
|
|
91
|
+
|
|
92
|
+
surface.blit(image, rect)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
class Wait:
|
|
96
|
+
def __init__(self, duration: float):
|
|
97
|
+
self.duration = max(0.0, duration)
|
|
98
|
+
self.elapsed = 0.0
|
|
99
|
+
self.finished = self.duration == 0
|
|
100
|
+
|
|
101
|
+
def update(self, dt: float):
|
|
102
|
+
self.elapsed = min(
|
|
103
|
+
self.elapsed + max(0.0, dt),
|
|
104
|
+
self.duration,
|
|
105
|
+
)
|
|
106
|
+
self.finished = self.elapsed >= self.duration
|
|
107
|
+
|
|
108
|
+
def reset(self):
|
|
109
|
+
self.elapsed = 0.0
|
|
110
|
+
self.finished = self.duration == 0
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
class AnimationSequence:
|
|
114
|
+
def __init__(self, *steps):
|
|
115
|
+
self.steps = list(steps)
|
|
116
|
+
self.index = 0
|
|
117
|
+
self.finished = len(self.steps) == 0
|
|
118
|
+
|
|
119
|
+
@property
|
|
120
|
+
def current(self):
|
|
121
|
+
if self.finished:
|
|
122
|
+
return None
|
|
123
|
+
|
|
124
|
+
return self.steps[self.index]
|
|
125
|
+
|
|
126
|
+
def update(self, dt: float):
|
|
127
|
+
if self.finished:
|
|
128
|
+
return
|
|
129
|
+
|
|
130
|
+
remaining = max(0.0, dt)
|
|
131
|
+
|
|
132
|
+
while not self.finished:
|
|
133
|
+
step = self.current
|
|
134
|
+
|
|
135
|
+
available = max(
|
|
136
|
+
0.0,
|
|
137
|
+
step.duration - step.elapsed,
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
consumed = min(remaining, available)
|
|
141
|
+
step.update(consumed)
|
|
142
|
+
remaining -= consumed
|
|
143
|
+
|
|
144
|
+
if not step.finished:
|
|
145
|
+
break
|
|
146
|
+
|
|
147
|
+
self.index += 1
|
|
148
|
+
|
|
149
|
+
if self.index >= len(self.steps):
|
|
150
|
+
self.finished = True
|
|
151
|
+
break
|
|
152
|
+
|
|
153
|
+
if remaining <= 0:
|
|
154
|
+
break
|
|
155
|
+
|
|
156
|
+
def reset(self):
|
|
157
|
+
self.index = 0
|
|
158
|
+
self.finished = len(self.steps) == 0
|
|
159
|
+
|
|
160
|
+
for step in self.steps:
|
|
161
|
+
step.reset()
|
|
162
|
+
|
|
163
|
+
def blit(self, surface):
|
|
164
|
+
if not self.steps:
|
|
165
|
+
return
|
|
166
|
+
|
|
167
|
+
if self.finished:
|
|
168
|
+
for last in reversed(self.steps):
|
|
169
|
+
if isinstance(last, Animation):
|
|
170
|
+
last.blit(surface)
|
|
171
|
+
break
|
|
172
|
+
|
|
173
|
+
return
|
|
174
|
+
|
|
175
|
+
step = self.current
|
|
176
|
+
|
|
177
|
+
if isinstance(step, Animation):
|
|
178
|
+
step.blit(surface)
|
|
179
|
+
|
|
180
|
+
elif isinstance(step, Wait):
|
|
181
|
+
for previous in reversed(self.steps[: self.index]):
|
|
182
|
+
if isinstance(previous, Animation):
|
|
183
|
+
previous.blit(surface)
|
|
184
|
+
break
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
from .eventbus import publish
|
|
2
|
+
|
|
3
|
+
"""Single-thread asset registry and incremental preload jobs."""
|
|
4
|
+
from dataclasses import dataclass, field
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from time import perf_counter
|
|
7
|
+
import json
|
|
8
|
+
import sys
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class AssetLoadError(RuntimeError):
|
|
12
|
+
pass
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@dataclass
|
|
16
|
+
class Asset:
|
|
17
|
+
path: Path
|
|
18
|
+
kind: str
|
|
19
|
+
options: dict = field(default_factory=dict)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class AssetManager:
|
|
23
|
+
def __init__(self):
|
|
24
|
+
self.registry = {}
|
|
25
|
+
self.groups = {}
|
|
26
|
+
self.loaded = {}
|
|
27
|
+
self.timings = {}
|
|
28
|
+
self.persistent = set()
|
|
29
|
+
self.references = {}
|
|
30
|
+
self._keys = {}
|
|
31
|
+
self.texture_cache = None
|
|
32
|
+
self.overrides = {}
|
|
33
|
+
|
|
34
|
+
def register(
|
|
35
|
+
self, name, path, *, type="image", group="global", persistent=False, **options
|
|
36
|
+
):
|
|
37
|
+
asset = Asset(Path(path).resolve(), type, options)
|
|
38
|
+
if name in self.registry and self.registry[name] != asset:
|
|
39
|
+
raise ValueError(f"Conflicting asset: {name}")
|
|
40
|
+
self.registry[name] = asset
|
|
41
|
+
self.groups.setdefault(group, set()).add(name)
|
|
42
|
+
if persistent or group == "global":
|
|
43
|
+
self.persistent.add(name)
|
|
44
|
+
|
|
45
|
+
def get(self, name):
|
|
46
|
+
try:
|
|
47
|
+
return self._get(name)
|
|
48
|
+
except Exception as exc:
|
|
49
|
+
publish(self, "asset.error", name=name, error=str(exc))
|
|
50
|
+
asset = self.registry.get(name)
|
|
51
|
+
raise AssetLoadError(
|
|
52
|
+
f"Asset {name}: {getattr(asset, 'path', 'unregistered')}: {exc}"
|
|
53
|
+
) from exc
|
|
54
|
+
|
|
55
|
+
def _get(self, name):
|
|
56
|
+
if name not in self.loaded:
|
|
57
|
+
a = self.registry[name]
|
|
58
|
+
a = Asset(self.overrides.get(name, a.path), a.kind, a.options)
|
|
59
|
+
key = (a.path, a.kind, repr(sorted(a.options.items())))
|
|
60
|
+
if key in self._keys:
|
|
61
|
+
self.loaded[name] = self._keys[key]
|
|
62
|
+
publish(self, "asset.loaded", name=name)
|
|
63
|
+
return self.loaded[name]
|
|
64
|
+
start = perf_counter()
|
|
65
|
+
if a.kind == "image":
|
|
66
|
+
from .ui.cache import decode_image
|
|
67
|
+
|
|
68
|
+
value = decode_image(str(a.path))
|
|
69
|
+
elif a.kind == "font":
|
|
70
|
+
import pygame
|
|
71
|
+
|
|
72
|
+
if not pygame.font.get_init():
|
|
73
|
+
pygame.font.init()
|
|
74
|
+
value = pygame.font.Font(str(a.path), a.options.get("size", 28))
|
|
75
|
+
elif a.kind == "music":
|
|
76
|
+
if not a.path.is_file():
|
|
77
|
+
raise FileNotFoundError(a.path)
|
|
78
|
+
value = a.path
|
|
79
|
+
elif a.kind == "sound":
|
|
80
|
+
import pygame
|
|
81
|
+
|
|
82
|
+
value = pygame.mixer.Sound(str(a.path))
|
|
83
|
+
elif a.kind == "json":
|
|
84
|
+
value = json.loads(a.path.read_text(encoding="utf-8"))
|
|
85
|
+
elif a.kind == "text":
|
|
86
|
+
value = a.path.read_text(encoding="utf-8")
|
|
87
|
+
else:
|
|
88
|
+
raise ValueError(f"Unknown asset type: {a.kind}")
|
|
89
|
+
self._keys[key] = value
|
|
90
|
+
self.loaded[name] = value
|
|
91
|
+
self.timings[name] = (perf_counter() - start) * 1000
|
|
92
|
+
publish(self, "asset.loaded", name=name)
|
|
93
|
+
return self.loaded[name]
|
|
94
|
+
|
|
95
|
+
def set_overrides(self, overrides):
|
|
96
|
+
changed = {name for name in self.overrides.keys() | overrides.keys()
|
|
97
|
+
if self.overrides.get(name) != overrides.get(name)}
|
|
98
|
+
self.overrides = dict(overrides)
|
|
99
|
+
for name in changed:
|
|
100
|
+
self.loaded.pop(name, None)
|
|
101
|
+
self.timings.pop(name, None)
|
|
102
|
+
if self.texture_cache is not None:
|
|
103
|
+
self.texture_cache.invalidate(('asset', name))
|
|
104
|
+
publish(self, 'asset.override_changed', name=name)
|
|
105
|
+
live = {id(value) for value in self.loaded.values()}
|
|
106
|
+
self._keys = {k:v for k,v in self._keys.items() if id(v) in live}
|
|
107
|
+
|
|
108
|
+
def get_texture(self, name):
|
|
109
|
+
if self.texture_cache is None:
|
|
110
|
+
raise RuntimeError("No renderer attached")
|
|
111
|
+
if self.registry[name].kind != "image":
|
|
112
|
+
raise ValueError("Expected image asset")
|
|
113
|
+
return self.texture_cache.get(("asset", name), self.get(name))
|
|
114
|
+
|
|
115
|
+
def ready(self, group):
|
|
116
|
+
return self.groups.get(group, set()) <= self.loaded.keys()
|
|
117
|
+
|
|
118
|
+
def preload(self, group):
|
|
119
|
+
return LoadingJob(self, sorted(self.groups.get(group, ())))
|
|
120
|
+
|
|
121
|
+
def acquire(self, group):
|
|
122
|
+
if group is not None:
|
|
123
|
+
self.references[group] = self.references.get(group, 0) + 1
|
|
124
|
+
|
|
125
|
+
def release(self, group):
|
|
126
|
+
if group not in self.references:
|
|
127
|
+
return
|
|
128
|
+
self.references[group] -= 1
|
|
129
|
+
if self.references[group] <= 0:
|
|
130
|
+
del self.references[group]
|
|
131
|
+
self.unload_group(group)
|
|
132
|
+
|
|
133
|
+
def unload_group(self, group, *, keep_groups=()):
|
|
134
|
+
keep = self.persistent.union(
|
|
135
|
+
*(self.groups.get(g, set()) for g in (*keep_groups, *self.references))
|
|
136
|
+
)
|
|
137
|
+
for name in self.groups.get(group, set()) - keep:
|
|
138
|
+
if name in self.loaded:
|
|
139
|
+
self.loaded.pop(name)
|
|
140
|
+
if self.texture_cache is not None:
|
|
141
|
+
self.texture_cache.invalidate(("asset", name))
|
|
142
|
+
publish(self, "asset.unloaded", name=name)
|
|
143
|
+
live = {id(value) for value in self.loaded.values()}
|
|
144
|
+
self._keys = {k: v for k, v in self._keys.items() if id(v) in live}
|
|
145
|
+
|
|
146
|
+
def memory_bytes(self):
|
|
147
|
+
seen = set()
|
|
148
|
+
|
|
149
|
+
def size(value):
|
|
150
|
+
if id(value) in seen:
|
|
151
|
+
return 0
|
|
152
|
+
seen.add(id(value))
|
|
153
|
+
if hasattr(value, "get_pitch"):
|
|
154
|
+
return value.get_pitch() * value.get_height()
|
|
155
|
+
if hasattr(value, "get_raw"):
|
|
156
|
+
return len(value.get_raw())
|
|
157
|
+
n = sys.getsizeof(value)
|
|
158
|
+
if isinstance(value, dict):
|
|
159
|
+
n += sum(size(k) + size(v) for k, v in value.items())
|
|
160
|
+
elif isinstance(value, (list, tuple)):
|
|
161
|
+
n += sum(size(v) for v in value)
|
|
162
|
+
return n
|
|
163
|
+
|
|
164
|
+
return sum(size(v) for v in self.loaded.values())
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
class LoadingJob:
|
|
168
|
+
def __init__(self, manager, names):
|
|
169
|
+
self.manager = manager
|
|
170
|
+
self.names = names
|
|
171
|
+
self.index = 0
|
|
172
|
+
self.current = None
|
|
173
|
+
self.error = None
|
|
174
|
+
self.started = perf_counter()
|
|
175
|
+
|
|
176
|
+
@property
|
|
177
|
+
def done(self):
|
|
178
|
+
return self.index == len(self.names)
|
|
179
|
+
|
|
180
|
+
@property
|
|
181
|
+
def progress(self):
|
|
182
|
+
return self.index / len(self.names) if self.names else 1.0
|
|
183
|
+
|
|
184
|
+
@property
|
|
185
|
+
def elapsed(self):
|
|
186
|
+
return perf_counter() - self.started
|
|
187
|
+
|
|
188
|
+
def update(self, budget_ms=2):
|
|
189
|
+
start = perf_counter()
|
|
190
|
+
while not self.done and self.error is None:
|
|
191
|
+
self.current = self.names[self.index]
|
|
192
|
+
try:
|
|
193
|
+
self.manager.get(self.current)
|
|
194
|
+
except Exception as exc:
|
|
195
|
+
self.error = exc
|
|
196
|
+
raise
|
|
197
|
+
self.index += 1
|
|
198
|
+
if (perf_counter() - start) * 1000 >= budget_ms:
|
|
199
|
+
break
|