Skip to content

Fix build for Menue tutorial, allow deepcopy of Color #1668

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 3 commits into from
Apr 2, 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
15 changes: 9 additions & 6 deletions arcade/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,10 @@ def get_resource_handle_paths(handle: str) -> List[Path]:
image_number_sheet = ':assets:images/spritesheets/number_sheet.png'
image_tiles = ':assets:images/spritesheets/tiles.png'
image_anim = ':assets:images/test_textures/anim.gif'
image_test_texture = ':assets:images/test_textures/test_texture.png'
image_xy_square = ':assets:images/test_textures/xy_square.png'
image_diffuse = ':assets:images/test_textures/normal_mapping/diffuse.jpg'
image_normal = ':assets:images/test_textures/normal_mapping/normal.jpg'
image_test_texture = ':assets:images/test_textures/test_texture.png'
image_xy_square = ':assets:images/test_textures/xy_square.png'
image_bomb = ':assets:images/tiles/bomb.png'
image_box_crate = ':assets:images/tiles/boxCrate.png'
image_box_crate_double = ':assets:images/tiles/boxCrate_double.png'
Expand Down Expand Up @@ -744,15 +744,18 @@ def get_resource_handle_paths(handle: str) -> List[Path]:
map_test_objects = ':assets:tiled_maps/test_objects.json'
gui_button_square_blue = ':system:gui_basic_assets/button_square_blue.png'
gui_button_square_blue_pressed = ':system:gui_basic_assets/button_square_blue_pressed.png'
gui_larger = ':system:gui_basic_assets/icons/larger.png'
gui_smaller = ':system:gui_basic_assets/icons/smaller.png'
gui_shield_gold = ':system:gui_basic_assets/items/shield_gold.png'
gui_sword_gold = ':system:gui_basic_assets/items/sword_gold.png'
gui_red_button_hover = ':system:gui_basic_assets/red_button_hover.png'
gui_red_button_normal = ':system:gui_basic_assets/red_button_normal.png'
gui_red_button_press = ':system:gui_basic_assets/red_button_press.png'
gui_slider_bar = ':system:gui_basic_assets/slider_bar.png'
gui_slider_thumb = ':system:gui_basic_assets/slider_thumb.png'
gui_larger = ':system:gui_basic_assets/icons/larger.png'
gui_smaller = ':system:gui_basic_assets/icons/smaller.png'
gui_shield_gold = ':system:gui_basic_assets/items/shield_gold.png'
gui_sword_gold = ':system:gui_basic_assets/items/sword_gold.png'
gui_circle_switch_off = ':system:gui_basic_assets/toggle/circle_switch_off.png'
gui_circle_switch_on = ':system:gui_basic_assets/toggle/circle_switch_on.png'
gui_switch_green = ':system:gui_basic_assets/toggle/switch_green.png'
gui_switch_red = ':system:gui_basic_assets/toggle/switch_red.png'
gui_dark_blue_gray_panel = ':system:gui_basic_assets/window/dark_blue_gray_panel.png'
gui_grey_panel = ':system:gui_basic_assets/window/grey_panel.png'
14 changes: 9 additions & 5 deletions arcade/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
Module specifying data custom types used for type hinting.
"""
from array import array
from pathlib import Path
from collections import namedtuple
from collections.abc import ByteString
from pathlib import Path
from typing import (
Iterable,
List,
Expand All @@ -15,21 +15,21 @@
Union,
TYPE_CHECKING, TypeVar
)

from pytiled_parser import Properties

from arcade.utils import (
IntOutsideRangeError,
ByteRangeError,
NormalizedRangeError
)
from pytiled_parser import Properties

if TYPE_CHECKING:
from arcade.texture import Texture


MAX_UINT24 = 0xFFFFFF
MAX_UINT32 = 0xFFFFFFFF


ChannelType = TypeVar('ChannelType')

RGB = Tuple[ChannelType, ChannelType, ChannelType]
Expand Down Expand Up @@ -72,6 +72,7 @@ class Color(RGBA255):
:param a: the alpha or transparency channel of the color, between
0 and 255
"""

def __new__(cls, r: int, g: int, b: int, a: int = 255):

if not 0 <= r <= 255:
Expand All @@ -91,6 +92,10 @@ def __new__(cls, r: int, g: int, b: int, a: int = 255):
# https://github.com/python/mypy/issues/8541
return super().__new__(cls, (r, g, b, a)) # type: ignore

def __deepcopy__(self, _):
"""Allow to deepcopy Colors"""
return Color(r=self.r, g=self.g, b=self.b, a=self.a)

def __repr__(self) -> str:
return f"{self.__class__.__name__}(r={self.r}, g={self.g}, b={self.b}, a={self.a})"

Expand Down Expand Up @@ -344,7 +349,6 @@ def from_hex_string(cls, code: str) -> "Color":

ColorLike = Union[RGB, RGBA255]


# Point = Union[Tuple[float, float], List[float]]
# Vector = Point
Point = Tuple[float, float]
Expand Down
15 changes: 10 additions & 5 deletions tests/unit/color/test_color_type.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import math
from copy import deepcopy
from itertools import product
from typing import Iterable, Callable, Tuple, TypeVar
from typing import Iterable, Callable, Tuple

import pytest

import arcade.color as colors
from arcade.types import Color


# This seems better as fixtures, but it's added here for consistency
# with the rest of the framework's beginner-accessible test styling.
OK_NORMALIZED = (0.0, 1.0)
Expand All @@ -17,14 +17,14 @@

def at_least_one_in(i: Iterable) -> Callable[[Iterable], bool]:
"""Return a callable which returns true when at least one elt is in iterable i"""

def _at_least_one_in(checked: Iterable):
return bool(set(checked) & frozenset(i))

return _at_least_one_in


def test_color_from_iterable_noncolor_iterables():

for length, type_converter in product((3, 4), (list, tuple, lambda a: a)):
iterable = type_converter(range(length))
color = Color.from_iterable(iterable)
Expand Down Expand Up @@ -66,14 +66,14 @@ def test_color_from_uint32():


def test_color_from_normalized():

# spot check conversion of acceptable human-ish values
float_steps = (1/255, 2/255, 3/255, 4/255)
float_steps = (1 / 255, 2 / 255, 3 / 255, 4 / 255)
assert Color.from_normalized(float_steps[:3]) == (1, 2, 3, 255)
assert Color.from_normalized(float_steps) == (1, 2, 3, 4)

# some helper callables
at_least_one_bad = at_least_one_in(BAD_NORMALIZED)

def local_convert(i: Iterable[float]) -> Tuple[int]:
"""Local helper converter, normalized float to byte ints"""
return tuple(math.floor(c * 255) for c in i)
Expand Down Expand Up @@ -153,3 +153,8 @@ def test_color_normalized_property():
assert colors.WHITE.normalized == (1.0, 1.0, 1.0, 1.0)
assert colors.TRANSPARENT_BLACK.normalized == (0.0, 0.0, 0.0, 0.0)
assert colors.GRAY.normalized == (128 / 255, 128 / 255, 128 / 255, 1.0)


def test_deepcopy_color():
expected_color = Color(255, 255, 255, 255)
assert deepcopy(expected_color) == expected_color
6 changes: 3 additions & 3 deletions tests/unit/resources/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def get_referenced_resources():
return resources



def test_resource_listing_exists():
"""
Find all the resources listed in the __init__.py file and check for their existence.
Expand Down Expand Up @@ -49,7 +48,7 @@ def test_resource_listing_is_complete():
if any(path.is_relative_to(skip_path) for skip_path in skip_paths):
continue
paths_in_resources.add(path)

# Temporarily ignore the following files. This is a problem with duplicate variable names
# created by the make_resources_init.py script and should be resolved in the future.
# - sounds: These exist in several formats
Expand All @@ -61,4 +60,5 @@ def test_resource_listing_is_complete():
paths_in_resources.remove(arcade.resources.RESOURCE_DIR / "assets" / "images" / "items" / "ladderTop.png")
paths_in_resources.remove(arcade.resources.RESOURCE_DIR / "assets" / "images" / "items" / "ladderMid.png")

assert paths_in_resources == paths_in_module, "Resources listed in __init__.py does not match the resources directory"
assert paths_in_module - paths_in_resources == set(), "Some resources are not listed in __init__.py"
assert paths_in_resources - paths_in_module == set(), "Some resources are listed in __init__.py, but not in folder"
2 changes: 1 addition & 1 deletion util/make_resources_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def main() -> None:

lines = get_module_lines()

for item in RESOURCE_ROOT.glob('**/*'):
for item in sorted(RESOURCE_ROOT.glob('**/*')):
if item.is_dir():
continue
if item.suffix in IGNORE_MEDIA_TYPES:
Expand Down