Skip to content

Commit 94df796

Browse files
authored
NinePatch should rebuild after atlas resize/rebuild (#2736)
* NinePatch should rebuild after atlas resize/rebuild * test atlas version * Update CHANGELOG
1 parent 44afd37 commit 94df796

File tree

5 files changed

+38
-5
lines changed

5 files changed

+38
-5
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
You can grab pre-release versions from PyPi. See the available versions from the
44
Arcade [PyPi Release History](https://pypi.org/project/arcade/#history) page.
55

6+
## 3.3.1
7+
8+
- Fixed an issue causing NinePatch to not render correctly
9+
- TextureAtlas now as a `version` attribute that is incremented when the
10+
atlas is resized or rebuilt. This way it's easy to track when texture coordinates
11+
has changed.
12+
- Added `Text.visible` (bool) property to control the visibility of text objects.
13+
614
## 3.3.0
715

816
- Fixed an issue causing a crash when closing the window

arcade/gui/nine_patch.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def __init__(
7878
self._initialized = False
7979
self._texture = texture
8080
self._custom_atlas = atlas
81-
self._geometry_cache: tuple[int, int, int, int, Rect] | None = None
81+
self._geometry_cache: tuple[int, int, int, int, int, Rect] | None = None
8282

8383
# pixel texture co-ordinate start and end of central box.
8484
self._left = left
@@ -325,16 +325,20 @@ def _init_deferred(self):
325325
# References for the texture
326326
self._atlas = self._custom_atlas or self._ctx.default_atlas
327327
self._add_to_atlas(self.texture)
328-
329-
# NOTE: Important to create geometry after the texture is added to the atlas
330-
# self._create_geometry(LBWH(0, 0, self.width, self.height))
331328
self._initialized = True
332329

333330
def _create_geometry(self, rect: Rect):
334331
"""Create vertices for the 9-patch texture."""
335332
# NOTE: This was ported from glsl geometry shader to python
336333
# Simulate old uniforms
337-
cache_key = (self._left, self._right, self._bottom, self._top, rect)
334+
cache_key = (
335+
self._atlas.version,
336+
self._left,
337+
self._right,
338+
self._bottom,
339+
self._top,
340+
rect,
341+
)
338342
if cache_key == self._geometry_cache:
339343
return
340344
self._geometry_cache = cache_key

arcade/texture_atlas/atlas_default.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def __init__(
112112
self._ctx = ctx or get_window().ctx
113113
self._max_size = self._ctx.info.MAX_VIEWPORT_DIMS
114114
self._size: tuple[int, int] = size
115+
self._version = 0
115116
self._allocator = Allocator(*self._size)
116117
self._auto_resize = auto_resize
117118
self._capacity = capacity
@@ -736,6 +737,7 @@ def resize(self, size: tuple[int, int], force=False) -> None:
736737
vertices=UV_TEXTURE_WIDTH * self._capacity * 6,
737738
)
738739

740+
self._version += 1
739741
# duration = time.perf_counter() - resize_start
740742
# LOG.info("[%s] Atlas resize took %s seconds", id(self), duration)
741743

@@ -769,6 +771,8 @@ def rebuild(self) -> None:
769771
for texture in sorted(textures, key=lambda x: x.image.size[1]):
770772
self._add(texture, create_finalizer=False)
771773

774+
self._version += 1
775+
772776
def use_uv_texture(self, unit: int = 0) -> None:
773777
"""
774778
Bind the texture coordinate texture to a channel.

arcade/texture_atlas/base.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def __init__(self, ctx: ArcadeContext | None):
6464
self._ctx = ctx or arcade.get_window().ctx
6565
self._size: tuple[int, int] = 0, 0
6666
self._layers: int = 1
67+
self._version = 0
6768

6869
@property
6970
def ctx(self) -> ArcadeContext:
@@ -85,6 +86,17 @@ def texture(self) -> Texture2D:
8586
"""The OpenGL texture for this atlas."""
8687
return self._texture
8788

89+
@property
90+
def version(self) -> int:
91+
"""
92+
The version of the atlas.
93+
94+
This is incremented every time the atlas is rebuilt or resized.
95+
It can be used to check if the atlas has changed since last
96+
time it was used.
97+
"""
98+
return self._version
99+
88100
@property
89101
def width(self) -> int:
90102
"""Hight of the atlas in pixels."""

tests/unit/atlas/test_basics.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,12 @@ def buf_check(atlas):
137137
assert len(atlas._texture_uvs._data.tobytes()) == len(atlas._texture_uvs.texture.read())
138138

139139
buf_check(atlas)
140+
version = atlas.version
140141
atlas.resize((200, 200))
142+
assert atlas.version != version
141143
buf_check(atlas)
144+
145+
version = atlas.version
142146
atlas.rebuild()
147+
assert atlas.version != version
143148
buf_check(atlas)

0 commit comments

Comments
 (0)