Skip to content

Commit 1cac45b

Browse files
authored
feat(gui): use rect in ninepatch draw (#2192)
* feat(gui): use rect in ninepatch draw
1 parent b8250d1 commit 1cac45b

14 files changed

+83
-92
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ repos:
2020
rev: 'v1.10.1'
2121
hooks:
2222
- id: mypy
23-
args: [ arcade, --explicit-package-bases ]
23+
args: [ --explicit-package-bases ]
2424
- repo: https://github.com/RobertCraigie/pyright-python
25-
rev: v1.1.369
25+
rev: v1.1.370
2626
hooks:
2727
- id: pyright

arcade/application.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
from arcade.clock import Clock, FixedClock
2222
from arcade.color import TRANSPARENT_BLACK
2323
from arcade.context import ArcadeContext
24-
from arcade.types import Color, RGBANormalized, RGBOrA255
25-
from arcade.types.rect import LBWH, Rect
24+
from arcade.types import LBWH, Color, Rect, RGBANormalized, RGBOrA255
2625
from arcade.utils import is_raspberry_pi
2726

2827
LOG = logging.getLogger(__name__)
@@ -244,7 +243,6 @@ def __init__(
244243
self.center_window()
245244

246245
if enable_polling:
247-
248246
self.keyboard = pyglet.window.key.KeyStateHandler()
249247

250248
if pyglet.options["headless"]:

arcade/examples/particle_fireworks.py

Lines changed: 47 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
If Python and Arcade are installed, this example can be run from the command line with:
77
python -m arcade.examples.particle_fireworks
88
"""
9+
910
import random
1011
from typing import Optional
1112

1213
import pyglet
1314
from pyglet.math import Vec2
1415

1516
import arcade
16-
from arcade.types import Point, PathOrTexture
17+
from arcade.types import Point, PathOrTexture, LBWH
1718
from arcade.math import rand_in_rect, clamp, lerp, rand_in_circle, rand_on_circle
1819
from arcade.particles import (
1920
Emitter,
@@ -23,8 +24,6 @@
2324
EmitMaintainCount,
2425
EmitBurst,
2526
)
26-
from arcade import LBWH
27-
2827

2928
SCREEN_WIDTH = 800
3029
SCREEN_HEIGHT = 600
@@ -68,10 +67,8 @@ def make_spinner():
6867
center_xy=(SCREEN_WIDTH / 2, SPINNER_HEIGHT - 5),
6968
emit_controller=EmitterIntervalWithTime(0.025, 2.0),
7069
particle_factory=lambda emitter: FadeParticle(
71-
filename_or_texture=random.choice(STAR_TEXTURES),
72-
change_xy=(0, 6.0),
73-
lifetime=0.2
74-
)
70+
filename_or_texture=random.choice(STAR_TEXTURES), change_xy=(0, 6.0), lifetime=0.2
71+
),
7572
)
7673
spinner.change_angle = 16.28
7774
return spinner
@@ -89,9 +86,9 @@ def make_rocket(emit_done_cb):
8986
lifetime=random.uniform(1.0, 1.5),
9087
start_alpha=100,
9188
end_alpha=0,
92-
mutation_callback=rocket_smoke_mutator
89+
mutation_callback=rocket_smoke_mutator,
9390
),
94-
emit_done_cb=emit_done_cb
91+
emit_done_cb=emit_done_cb,
9592
)
9693
rocket.change_x = random.uniform(-1.0, 1.0)
9794
rocket.change_y = random.uniform(5.0, 7.25)
@@ -104,10 +101,8 @@ def make_flash(prev_emitter):
104101
center_xy=prev_emitter.get_pos(),
105102
emit_controller=EmitBurst(3),
106103
particle_factory=lambda emitter: FadeParticle(
107-
filename_or_texture=FLASH_TEXTURE,
108-
change_xy=rand_in_circle((0.0, 0.0), 3.5),
109-
lifetime=0.15
110-
)
104+
filename_or_texture=FLASH_TEXTURE, change_xy=rand_in_circle((0.0, 0.0), 3.5), lifetime=0.15
105+
),
111106
)
112107

113108

@@ -119,38 +114,40 @@ def make_puff(prev_emitter):
119114
particle_factory=lambda emitter: FadeParticle(
120115
filename_or_texture=PUFF_TEXTURE,
121116
change_xy=Vec2(*rand_in_circle((0.0, 0.0), 0.4)) + Vec2(0.3, 0.0),
122-
lifetime=4.0
123-
)
117+
lifetime=4.0,
118+
),
124119
)
125120

126121

127122
class AnimatedAlphaParticle(LifetimeParticle):
128123
"""A custom particle that animates between three different alpha levels"""
129124

130125
def __init__(
131-
self,
132-
filename_or_texture: Optional[PathOrTexture],
133-
change_xy: Vec2,
134-
start_alpha: int = 0,
135-
duration1: float = 1.0,
136-
mid_alpha: int = 255,
137-
duration2: float = 1.0,
138-
end_alpha: int = 0,
139-
center_xy: Point = (0.0, 0.0),
140-
angle: float = 0,
141-
change_angle: float = 0,
142-
scale: float = 1.0,
143-
mutation_callback=None,
126+
self,
127+
filename_or_texture: Optional[PathOrTexture],
128+
change_xy: Vec2,
129+
start_alpha: int = 0,
130+
duration1: float = 1.0,
131+
mid_alpha: int = 255,
132+
duration2: float = 1.0,
133+
end_alpha: int = 0,
134+
center_xy: Point = (0.0, 0.0),
135+
angle: float = 0,
136+
change_angle: float = 0,
137+
scale: float = 1.0,
138+
mutation_callback=None,
144139
):
145-
super().__init__(filename_or_texture,
146-
change_xy,
147-
duration1 + duration2,
148-
center_xy,
149-
angle,
150-
change_angle,
151-
scale,
152-
start_alpha,
153-
mutation_callback)
140+
super().__init__(
141+
filename_or_texture,
142+
change_xy,
143+
duration1 + duration2,
144+
center_xy,
145+
angle,
146+
change_angle,
147+
scale,
148+
start_alpha,
149+
mutation_callback,
150+
)
154151
self.start_alpha = start_alpha
155152
self.in_duration = duration1
156153
self.mid_alpha = mid_alpha
@@ -197,8 +194,8 @@ def __init__(self):
197194
mid_alpha=128,
198195
duration2=random.uniform(2.0, 6.0),
199196
end_alpha=0,
200-
center_xy=rand_in_rect(LBWH(0.0, 0.0, SCREEN_WIDTH, SCREEN_HEIGHT))
201-
)
197+
center_xy=rand_in_rect(LBWH(0.0, 0.0, SCREEN_WIDTH, SCREEN_HEIGHT)),
198+
),
202199
)
203200
self.emitters.append(stars)
204201

@@ -217,8 +214,8 @@ def __init__(self):
217214
mid_alpha=255,
218215
duration2=random.uniform(5.0, 10.0),
219216
end_alpha=0,
220-
center_xy=rand_in_circle((0.0, 0.0), 50)
221-
)
217+
center_xy=rand_in_circle((0.0, 0.0), 50),
218+
),
222219
)
223220
self.emitters.append(self.cloud)
224221

@@ -237,7 +234,7 @@ def launch_random_firework(self, _delta_time):
237234
self.emitters.append(rocket)
238235

239236
def launch_ringed_firework(self, _delta_time):
240-
""""Firework that has a basic explosion and a ring of sparks of a different color"""
237+
""" "Firework that has a basic explosion and a ring of sparks of a different color"""
241238
rocket = make_rocket(self.explode_ringed_firework)
242239
self.emitters.append(rocket)
243240

@@ -267,8 +264,8 @@ def explode_firework(self, prev_emitter):
267264
filename_or_texture=spark_texture,
268265
change_xy=rand_in_circle(center=(0.0, 0.0), radius=9.0),
269266
lifetime=random.uniform(0.5, 1.2),
270-
mutation_callback=firework_spark_mutator
271-
)
267+
mutation_callback=firework_spark_mutator,
268+
),
272269
)
273270
self.emitters.append(sparks)
274271

@@ -285,8 +282,8 @@ def explode_ringed_firework(self, prev_emitter):
285282
filename_or_texture=spark_texture,
286283
change_xy=rand_in_circle((0.0, 0.0), 8.0),
287284
lifetime=random.uniform(0.55, 0.8),
288-
mutation_callback=firework_spark_mutator
289-
)
285+
mutation_callback=firework_spark_mutator,
286+
),
290287
)
291288
self.emitters.append(sparks)
292289

@@ -297,8 +294,8 @@ def explode_ringed_firework(self, prev_emitter):
297294
filename_or_texture=ring_texture,
298295
change_xy=rand_on_circle(center=(0.0, 0.0), radius=5.0),
299296
lifetime=random.uniform(1.0, 1.6),
300-
mutation_callback=firework_spark_mutator
301-
)
297+
mutation_callback=firework_spark_mutator,
298+
),
302299
)
303300
self.emitters.append(ring)
304301

@@ -319,8 +316,8 @@ def explode_sparkle_firework(self, prev_emitter):
319316
mid_alpha=0,
320317
duration2=random.uniform(0.1, 0.2),
321318
end_alpha=255,
322-
mutation_callback=firework_spark_mutator
323-
)
319+
mutation_callback=firework_spark_mutator,
320+
),
324321
)
325322
self.emitters.append(sparks)
326323

arcade/gui/examples/exp_hidden_password.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ def __init__(self):
3737
self.username_input = grid.add(UIInputText(height=25), col_num=1, row_num=0).with_border()
3838

3939
grid.add(UILabel(text="Password:"), col_num=0, row_num=1)
40-
self.password_input = grid.add(
41-
UIPasswordInput(height=25), col_num=1, row_num=1
42-
).with_border()
40+
self.password_input = grid.add(UIPasswordInput(height=25), col_num=1, row_num=1).with_border()
4341

4442
self.login_button = grid.add(UIFlatButton(text="Login"), col_num=0, row_num=2, col_span=2)
4543
self.login_button.on_click = self.on_login

arcade/gui/nine_patch.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,10 @@ def height(self) -> int:
193193
"""The height of the texture in pixels."""
194194
return self.texture.height
195195

196-
def draw_sized(
196+
def draw_rect(
197197
self,
198198
*,
199-
position: tuple[float, float] = (0.0, 0.0),
200-
size: tuple[float, float],
199+
rect: arcade.types.Rect,
201200
pixelated: bool = False,
202201
**kwargs,
203202
):
@@ -210,8 +209,7 @@ def draw_sized(
210209
smaller than the total size of the border areas.
211210
212211
213-
:param position: Bottom left offset of the texture in pixels
214-
:param size: Size of the 9-patch as width, height in pixels
212+
:param rect: Rectangle to draw the 9-patch texture in
215213
:param pixelated: Whether to draw with nearest neighbor interpolation
216214
"""
217215
self.program.set_uniform_safe("texture_id", self._atlas.get_texture_id(self._texture))
@@ -220,10 +218,10 @@ def draw_sized(
220218
else:
221219
self._atlas.texture.filter = self._ctx.LINEAR, self._ctx.LINEAR
222220

223-
self.program["position"] = position
221+
self.program["position"] = rect.bottom_left
224222
self.program["start"] = self._left, self._bottom
225223
self.program["end"] = self.width - self._right, self.height - self._top
226-
self.program["size"] = size
224+
self.program["size"] = rect.size
227225
self.program["t_size"] = self._texture.size
228226

229227
self._atlas.use_uv_texture(0)

arcade/gui/surface.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
from arcade.color import TRANSPARENT_BLACK
1010
from arcade.gl import Framebuffer
1111
from arcade.gui.nine_patch import NinePatchTexture
12-
from arcade.types import RGBA255, Point
13-
from arcade.types.rect import LBWH, Rect
12+
from arcade.types import LBWH, RGBA255, Point, Rect
1413

1514

1615
class Surface:
@@ -118,7 +117,7 @@ def draw_texture(
118117
f"Ninepatch does not support an alpha != 255 yet, but got {alpha}"
119118
)
120119

121-
tex.draw_sized(size=(width, height))
120+
tex.draw_rect(rect=LBWH(0, 0, width, height))
122121
else:
123122
arcade.draw_texture_rect(tex, LBWH(x, y, width, height), angle=angle, alpha=alpha)
124123

arcade/gui/ui_manager.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from typing_extensions import TypeGuard
1919

2020
import arcade
21-
from arcade import LBWH, Rect
2221
from arcade.gui.events import (
2322
UIKeyPressEvent,
2423
UIKeyReleaseEvent,
@@ -34,7 +33,7 @@
3433
)
3534
from arcade.gui.surface import Surface
3635
from arcade.gui.widgets import UIWidget
37-
from arcade.types import AnchorPoint, Point2
36+
from arcade.types import LBWH, AnchorPoint, Point2, Rect
3837

3938
W = TypeVar("W", bound=UIWidget)
4039

arcade/gui/widgets/text.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from pyglet.text.document import AbstractDocument
99

1010
import arcade
11-
from arcade import LBWH
1211
from arcade.gui.events import (
1312
UIEvent,
1413
UIMouseDragEvent,
@@ -23,7 +22,7 @@
2322
from arcade.gui.surface import Surface
2423
from arcade.gui.widgets import UIWidget
2524
from arcade.gui.widgets.layout import UIAnchorLayout
26-
from arcade.types import RGBA255, Color, RGBOrA255
25+
from arcade.types import LBWH, RGBA255, Color, RGBOrA255
2726

2827

2928
class UILabel(UIWidget):

tests/unit/gui/test_layouting_anchorlayout.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from arcade import LBWH
1+
from pyglet.math import Vec2
2+
23
from arcade.gui import UIDummy, UIBoxLayout
34
from arcade.gui.widgets.layout import UIAnchorLayout
4-
from pyglet.math import Vec2
5+
from arcade.types import LBWH
56

67

78
def test_place_widget(window):

tests/unit/gui/test_layouting_boxlayout.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from _pytest.python_api import approx
2+
from pyglet.math import Vec2
23

3-
from arcade import LBWH
44
from arcade.gui import UIBoxLayout, UIManager
55
from arcade.gui.widgets import UIDummy
6-
from pyglet.math import Vec2
6+
from arcade.types import LBWH
77

88

99
# Vertical

0 commit comments

Comments
 (0)