Skip to content

Refactor hitbox algo cache names into an instance property #1793

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions arcade/hitbox/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ class HitBoxAlgorithm:
users can also repurpose them for other tasks.
"""

#: The name of the algorithm
name = "base"

#: Whether points for this algorithm should be cached
cache = True

def __init__(self):
self._cache_name = self.__class__.__name__

@property
def param_str(self) -> str:
def cache_name(self) -> str:
"""
A string representation of the parameters used to create this algorithm.

Expand All @@ -37,7 +37,7 @@ def param_str(self) -> str:
distinguishing different configurations of a particular hit box
algorithm.
"""
return ""
return self._cache_name

def calculate(self, image: Image, **kwargs) -> PointList:
"""
Expand Down
1 change: 0 additions & 1 deletion arcade/hitbox/bounding_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ class BoundingHitBoxAlgorithm(HitBoxAlgorithm):
"""
A simple hit box algorithm that returns a hit box around the entire image.
"""
name = "bounding_box"
cache = False

def calculate(self, image: Image, **kwargs) -> PointList:
Expand Down
13 changes: 3 additions & 10 deletions arcade/hitbox/pymunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,14 @@ class PymunkHitBoxAlgorithm(HitBoxAlgorithm):
This is a more accurate algorithm generating more points. The
point count can be controlled with the ``detail`` parameter.
"""
name = "pymunk"

#: The default detail when creating a new instance.
default_detail = 4.5

def __init__(self, *, detail: Optional[float] = None):
super().__init__()
self.detail = detail or self.default_detail

@property
def param_str(self) -> str:
"""
Return a string representation of the parameters used to create this algorithm.

This is used in caching.
"""
return f"detail={self.detail}"
self._cache_name += f"|detail={self.detail}"

def __call__(self, *, detail: Optional[float] = None) -> "PymunkHitBoxAlgorithm":
"""Create a new instance with new default values"""
Expand Down
1 change: 0 additions & 1 deletion arcade/hitbox/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class SimpleHitBoxAlgorithm(HitBoxAlgorithm):
Simple hit box algorithm. This algorithm attempts to trim out transparent pixels
from an image to create a hit box.
"""
name = "simple"

def calculate(self, image: Image, **kwargs) -> PointList:
"""
Expand Down
2 changes: 1 addition & 1 deletion arcade/texture/texture.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def create_cache_name(
if not isinstance(hit_box_algorithm, HitBoxAlgorithm):
raise TypeError(f"Expected HitBoxAlgorithm, got {type(hit_box_algorithm)}")

return f"{hash}|{vertex_order}|{hit_box_algorithm.name}|{hit_box_algorithm.param_str}"
return f"{hash}|{vertex_order}|{hit_box_algorithm.cache_name}|"

@classmethod
def create_atlas_name(
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/htibox/test_hitbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ def test_calculate_hit_box_points_detailed():
hitbox.calculate_hit_box_points_detailed(image)


def test_param_str():
def test_texture_cache_name():
# These algos don't have any parameters
assert hitbox.algo_simple.param_str == ""
assert hitbox.algo_bounding_box.param_str == ""
assert hitbox.algo_simple.cache_name == "SimpleHitBoxAlgorithm"
assert hitbox.algo_bounding_box.cache_name == "BoundingHitBoxAlgorithm"

# Detailed has a detail parameter for the number of points
# Test default value and specifying a value
assert hitbox.algo_detailed.param_str == "detail=4.5"
assert hitbox.algo_detailed(detail=10.0).param_str == "detail=10.0"
assert hitbox.algo_detailed.cache_name == "PymunkHitBoxAlgorithm|detail=4.5"
assert hitbox.algo_detailed(detail=10.0).cache_name == "PymunkHitBoxAlgorithm|detail=10.0"


def test_call_override():
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/texture/test_texture.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ def test_create():
assert texture.file_path is None
assert texture.crop_values is None
assert texture.image_data.hash == "7a12e561363385e9dfeeab326368731c030ed4b374e7f5897ac819159d2884c5"
assert texture.cache_name == f"{texture.image_data.hash}|{texture._vertex_order}|{texture.hit_box_algorithm.name}|"
assert texture.cache_name == f"{texture.image_data.hash}|{texture._vertex_order}|{texture.hit_box_algorithm.cache_name}|"

with pytest.raises(TypeError):
_ = arcade.Texture("not valid image data")


def test_create_override_name():
texture = arcade.Texture(Image.new("RGBA", (10, 10)), hash="test")
assert texture.cache_name == f"test|{texture._vertex_order}|{texture.hit_box_algorithm.name}|"
assert texture.cache_name == f"test|{texture._vertex_order}|{texture.hit_box_algorithm.cache_name}|"


def test_hitbox_algo_selection():
Expand Down