Skip to content

Commit 3c03b23

Browse files
DigiDuncanCleptomaniaeinarfpushfoo
authored
Name scale properties on Sprites to things that make sense (#2021)
* incomplete scale fix * fix a bunch of tests * fix more tests * typo * fix the one broken test and speed up .scale setter * fix docstrings * Fix examples * typing go brrr * Remove overly strict Point annotation * Fix misuse of Point annotation which should be Point2 * Annotate HitBox scale __init__ arguments as Point2 * Annotate BasicSprite scale return as Point2 * Fix set_size tests w/ notes on problems with == in pyglet==2.1dev2 * Add temp fix for pyglet 2.1dev2 * Remove Vec2 from test_set_size() * Update BasicSprite.scale and tests for it * Make .scale convert to Vec2 on return * Update scale unit tests to use tuples * Fix formatting to make CI happy * Add optimized validation for BasicSprite.size + tests * Optimize & clean up BasicSprite.scale_x setter * Remove if check around texture since we are guaranteed to have one now * Rename scale_x argument from new_value to new_scale_x * Unpack self._scale into old_scale_* * Remove redundant scale setting for y * Apply new scale to the hitbox first to raise exceptions earlier * Optimize & clean up BasicSprite.scale_y setter * Remove if check around texture since we are guaranteed to have one now * Rename scale_x argument from new_value to new_scale_x * Unpack self._scale into old_scale_* * Remove redundant scale setting for x * Apply new scale to the hitbox first to raise exceptions earlier * Optimize and clean up scale setter * Rename new_value to new_scale * Assign to scale_x and scale_y instead of immediate tuple creation * Add comments about hot code path asking not to DRY it * Add exception checks for unpack * Reorder and reduce use of dot and index acccess * Remove extra line * Revert use of assert in size.setter since digi was right * Clean up rescale_relative_to_point's insides * Significantly redeuce dot and index access in rescale_relative_to_point * Precache re-used quantities * Comments for clarity * Update rescale_relative_to_point's docstring * Use scale instead of removed scale_xy * Convert to more pyglet/Google-style * Unify rescale*_relative_to_point methods * Add vector unpack check logic to rescale_relative_to_point * Update signature annotations * Update docstring * Rename factor argument to scale_by * Delete body of rescale_xy_relative_to_point * Update docstring to point to rescale_relative_to_point with deprecation * Add @warning wrapper to rescale_xy_relative_to_point * Apply auto-formatting * Fix typo * Make Sphinx build * Fix use of Point with Point2 * Add detailed explanation of seemingly strange Vec2 usage --------- Co-authored-by: Darren Eberly <darren@eber.ly> Co-authored-by: Einar Forselv <eforselv@gmail.com> Co-authored-by: pushfoo <36696816+pushfoo@users.noreply.github.com>
1 parent 0a6cd88 commit 3c03b23

File tree

10 files changed

+298
-172
lines changed

10 files changed

+298
-172
lines changed

arcade/__init__.py

+11
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ def configure_logging(level: Optional[int] = None):
5858
# noinspection PyPep8
5959
import pyglet
6060

61+
# TODO: Remove ASAP after pyglet >= 2.1dev2 is out
62+
if pyglet.version == "2.1.dev2":
63+
# Temporary monkeypatch via deletion since dev2 still includes
64+
# overly-specific __eq__ behavior. Later pyglet commits restore
65+
# equality with same-valued tuples by deleting the __eq__ methods.
66+
from pyglet import math as _pyglet_math
67+
68+
del _pyglet_math.Vec2.__eq__
69+
del _pyglet_math.Vec3.__eq__
70+
del _pyglet_math.Vec4.__eq__
71+
6172
# Env variable shortcut for headless mode
6273
if os.environ.get("ARCADE_HEADLESS"):
6374
pyglet.options["headless"] = True

arcade/examples/particle_fireworks.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ def firework_spark_mutator(particle: FadeParticle):
360360

361361

362362
def rocket_smoke_mutator(particle: LifetimeParticle):
363-
particle.scale = lerp(0.5, 3.0, particle.lifetime_elapsed / particle.lifetime_original)
363+
particle.scale = lerp(0.5, 3.0, particle.lifetime_elapsed / particle.lifetime_original) # type: ignore
364364

365365

366366
def main():

arcade/examples/sprite_health.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def __init__(self, bar_list: arcade.SpriteList) -> None:
5252
scale=SPRITE_SCALING_PLAYER,
5353
)
5454
self.indicator_bar: IndicatorBar = IndicatorBar(
55-
self, bar_list, (self.center_x, self.center_y), scale=1.5,
55+
self, bar_list, (self.center_x, self.center_y), scale=(1.5, 1.5),
5656
)
5757
self.health: int = PLAYER_HEALTH
5858

@@ -98,7 +98,7 @@ def __init__(
9898
width: int = 100,
9999
height: int = 4,
100100
border_size: int = 4,
101-
scale: float = 1.0,
101+
scale: Tuple[float, float] = (1.0, 1.0),
102102
) -> None:
103103
# Store the reference to the owner and the sprite list
104104
self.owner: Player = owner
@@ -110,7 +110,7 @@ def __init__(
110110
self._center_x: float = 0.0
111111
self._center_y: float = 0.0
112112
self._fullness: float = 0.0
113-
self._scale: float = 1.0
113+
self._scale: Tuple[float, float] = (1.0, 1.0)
114114

115115
# Create the boxes needed to represent the indicator bar
116116
self._background_box: arcade.SpriteSolidColor = arcade.SpriteSolidColor(
@@ -206,8 +206,8 @@ def fullness(self, new_fullness: float) -> None:
206206
else:
207207
# Set the full_box to be visible incase it wasn't then update the bar
208208
self.full_box.visible = True
209-
self.full_box.width = self._bar_width * new_fullness * self.scale
210-
self.full_box.left = self._center_x - (self._bar_width / 2) * self.scale
209+
self.full_box.width = self._bar_width * new_fullness * self.scale[0]
210+
self.full_box.left = self._center_x - (self._bar_width / 2) * self.scale[0]
211211

212212
@property
213213
def position(self) -> Tuple[float, float]:
@@ -224,15 +224,15 @@ def position(self, new_position: Tuple[float, float]) -> None:
224224
self.full_box.position = new_position
225225

226226
# Make sure full_box is to the left of the bar instead of the middle
227-
self.full_box.left = self._center_x - (self._bar_width / 2) * self.scale
227+
self.full_box.left = self._center_x - (self._bar_width / 2) * self.scale[0]
228228

229229
@property
230-
def scale(self) -> float:
230+
def scale(self) -> Tuple[float, float]:
231231
"""Returns the scale of the bar."""
232232
return self._scale
233233

234234
@scale.setter
235-
def scale(self, value: float) -> None:
235+
def scale(self, value: Tuple[float, float]) -> None:
236236
"""Sets the new scale of the bar."""
237237
# Check if the scale has changed. If so, change the bar's scale
238238
if value != self.scale:

arcade/hitbox/base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def __init__(
103103
self,
104104
points: Point2List,
105105
position: Point2 = (0.0, 0.0),
106-
scale: tuple[float, float] = (1.0, 1.0),
106+
scale: Point2 = (1.0, 1.0),
107107
):
108108
self._points = points
109109
self._position = position
@@ -249,7 +249,7 @@ def __init__(
249249
*,
250250
position: tuple[float, float] = (0.0, 0.0),
251251
angle: float = 0.0,
252-
scale: tuple[float, float] = (1.0, 1.0),
252+
scale: Point2 = (1.0, 1.0),
253253
):
254254
super().__init__(points, position=position, scale=scale)
255255
self._angle: float = angle

arcade/pymunk_physics_engine.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def velocity_callback(
189189

190190
# Set the physics shape to the sprite's hitbox
191191
poly = sprite.hit_box.points
192-
scaled_poly = [[x * sprite.scale for x in z] for z in poly]
192+
scaled_poly = [[x * sprite.scale_x for x in z] for z in poly]
193193
shape = pymunk.Poly(body, scaled_poly, radius=radius) # type: ignore
194194

195195
# Set collision type, used in collision callbacks

arcade/sprite/animated.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -335,5 +335,5 @@ def update_animation(self, delta_time: float = 1 / 60) -> None:
335335
if self._texture is None:
336336
print("Error, no texture set")
337337
else:
338-
self.width = self._texture.width * self.scale
339-
self.height = self._texture.height * self.scale
338+
self.width = self._texture.width * self.scale_x
339+
self.height = self._texture.height * self.scale_x

0 commit comments

Comments
 (0)