|
2 | 2 |
|
3 | 3 | from typing import TYPE_CHECKING, Any, Iterable, TypeVar |
4 | 4 |
|
5 | | -from pyglet.math import Vec2 |
6 | | - |
7 | 5 | import arcade |
8 | 6 | from arcade.color import BLACK, WHITE |
9 | 7 | from arcade.exceptions import ReplacementWarning, warning |
@@ -262,49 +260,24 @@ def scale_y(self, new_scale_y: AsFloat): |
262 | 260 | sprite_list._update_size(self) |
263 | 261 |
|
264 | 262 | @property |
265 | | - def scale(self) -> Vec2: |
| 263 | + def scale(self) -> Point2: |
266 | 264 | """Get or set the x & y scale of the sprite as a pair of values. |
| 265 | + You may set both the x & y with a single scalar, but scale will always return |
| 266 | + a length 2 tuple of the x & y scale |
267 | 267 |
|
268 | | - You may set it to either a single value or a pair of values: |
269 | | -
|
270 | | - .. list-table:: |
271 | | - :header-rows: 0 |
272 | | -
|
273 | | - * - Single value |
274 | | - - ``sprite.scale = 2.0`` |
275 | | -
|
276 | | - * - Tuple or :py:class:`~pyglet,math.Vec2` |
277 | | - - ``sprite.scale = (1.0, 3.0)`` |
278 | | -
|
279 | | - The two-channel version is useful for making health bars and |
280 | | - other indicators. |
281 | | -
|
282 | | - .. note:: Returns a :py:class:`pyglet.math.Vec2` for |
283 | | - compatibility. |
284 | | -
|
285 | | - Arcade versions lower than 3,0 used one or both of the following |
286 | | - for scale: |
| 268 | + See :py:attr:`.scale_x` and :py:attr:`.scale_y` for individual access. |
287 | 269 |
|
288 | | - * A single :py:class:`float` on versions <= 2.6 |
289 | | - * A ``scale_xy`` property and exposing only the x component |
290 | | - on some intermediate dev releases |
291 | | -
|
292 | | - Although scale is internally stored as a :py:class:`tuple`, we |
293 | | - return a :py:class:`pyglet.math.Vec2` to allow the in-place |
294 | | - operators to work in addition to setting values directly: |
295 | | -
|
296 | | - * Old-style (``sprite.scale *= 2.0``) |
297 | | - * New-style (``sprite.scale *= 2.0, 2.0``) |
| 270 | + See :py:meth:`.scale_multiply_uniform` for uniform scaling. |
298 | 271 |
|
299 | 272 | .. note:: Negative scale values are supported. |
300 | 273 |
|
301 | | - This applies to both single-axis and dual-axis. |
302 | | - Negatives will flip & mirror the sprite, but the |
303 | | - with will use :py:func:`abs` to report total width |
304 | | - and height instead of negatives. |
| 274 | + This applies to both single-axis and dual-axis. |
| 275 | + Negatives will flip & mirror the sprite, but the |
| 276 | + with will use :py:func:`abs` to report total width |
| 277 | + and height instead of negatives. |
305 | 278 |
|
306 | 279 | """ |
307 | | - return Vec2(*self._scale) |
| 280 | + return self._scale |
308 | 281 |
|
309 | 282 | @scale.setter |
310 | 283 | def scale(self, new_scale: Point2 | AsFloat): |
@@ -607,6 +580,41 @@ def update_animation(self, delta_time: float = 1 / 60, *args, **kwargs) -> None: |
607 | 580 |
|
608 | 581 | # --- Scale methods ----- |
609 | 582 |
|
| 583 | + def add_scale(self, factor: AsFloat) -> None: |
| 584 | + """Add to the sprite's scale by the factor. |
| 585 | + This adds the factor to both the x and y scale values. |
| 586 | +
|
| 587 | + Args: |
| 588 | + factor: The factor to add to the sprite's scale. |
| 589 | + """ |
| 590 | + self._scale = self._scale[0] + factor, self._scale[1] + factor |
| 591 | + self._hit_box.scale = self._scale |
| 592 | + tex_width, tex_height = self._texture.size |
| 593 | + self._width = tex_width * self._scale[0] |
| 594 | + self._height = tex_height * self._scale[1] |
| 595 | + |
| 596 | + self.update_spatial_hash() |
| 597 | + for sprite_list in self.sprite_lists: |
| 598 | + sprite_list._update_size(self) |
| 599 | + |
| 600 | + def multiply_scale(self, factor: AsFloat) -> None: |
| 601 | + """multiply the sprite's scale by the factor. |
| 602 | + This multiplies both the x and y scale values by the factor. |
| 603 | +
|
| 604 | + Args: |
| 605 | + factor: The factor to scale up the sprite by. |
| 606 | + """ |
| 607 | + |
| 608 | + self._scale = self._scale[0] * factor, self._scale[1] * factor |
| 609 | + self._hit_box.scale = self._scale |
| 610 | + tex_width, tex_height = self._texture.size |
| 611 | + self._width = tex_width * factor |
| 612 | + self._height = tex_height * factor |
| 613 | + |
| 614 | + self.update_spatial_hash() |
| 615 | + for sprite_list in self.sprite_lists: |
| 616 | + sprite_list._update_size(self) |
| 617 | + |
610 | 618 | def rescale_relative_to_point(self, point: Point2, scale_by: AsFloat | Point2) -> None: |
611 | 619 | """Rescale the sprite and its distance from the passed point. |
612 | 620 |
|
@@ -695,7 +703,7 @@ def rescale_xy_relative_to_point(self, point: Point, factors_xy: Iterable[float] |
695 | 703 | Use :py:meth:`.rescale_relative_to_point` instead. |
696 | 704 |
|
697 | 705 | This was added during the 3.0 development cycle before scale was |
698 | | - made into a vector quantitity. |
| 706 | + made into a vector quantity. |
699 | 707 |
|
700 | 708 | This method can scale by different amounts on each axis. To |
701 | 709 | scale along only one axis, set the other axis to ``1.0`` in |
|
0 commit comments