|
10 | 10 |
|
11 | 11 | # from arcade import Texture
|
12 | 12 | from arcade.texture import Texture
|
| 13 | +from arcade.types.rect import Rect |
13 | 14 |
|
14 | 15 | if TYPE_CHECKING:
|
15 | 16 | from arcade.hitbox import HitBoxAlgorithm
|
@@ -102,68 +103,55 @@ def flip_top_bottom(self) -> None:
|
102 | 103 | self._image = self._image.transpose(Transpose.FLIP_TOP_BOTTOM)
|
103 | 104 | self._flip_flags = (self._flip_flags[0], not self._flip_flags[1])
|
104 | 105 |
|
105 |
| - def get_image( |
106 |
| - self, x: int, y: int, width: int, height: int, origin: OriginChoices = "upper_left" |
107 |
| - ) -> Image.Image: |
| 106 | + def get_image(self, rect: Rect, y_up=False) -> Image.Image: |
108 | 107 | """
|
109 | 108 | Slice out an image from the sprite sheet.
|
110 | 109 |
|
111 | 110 | Args:
|
112 |
| - x: |
113 |
| - X position of the image |
114 |
| - y: |
115 |
| - Y position of the image |
116 |
| - width: |
117 |
| - Width of the image. |
118 |
| - height: |
119 |
| - Height of the image. |
120 |
| - origin: |
121 |
| - Origin of the image. Default is "upper_left". |
122 |
| - Options are "upper_left" or "lower_left". |
| 111 | + rect: |
| 112 | + The rectangle to crop out. |
| 113 | + y_up: |
| 114 | + Sets the coordinate space of the image to assert (0, 0) |
| 115 | + in the bottom left. |
123 | 116 | """
|
124 | 117 | # PIL box is a 4-tuple: left, upper, right, and lower
|
125 |
| - if origin == "upper_left": |
126 |
| - return self.image.crop((x, y, x + width, y + height)) |
127 |
| - elif origin == "lower_left": |
| 118 | + if y_up: |
128 | 119 | return self.image.crop(
|
129 |
| - (x, self.image.height - y - height, x + width, self.image.height - y) |
| 120 | + ( |
| 121 | + rect.left, |
| 122 | + self.image.height - rect.bottom - rect.height, |
| 123 | + rect.right, |
| 124 | + self.image.height - rect.bottom, |
| 125 | + ) |
130 | 126 | )
|
131 | 127 | else:
|
132 |
| - raise ValueError("Invalid value for origin. Must be 'upper_left' or 'lower_left'.") |
| 128 | + return self.image.crop( |
| 129 | + ( |
| 130 | + rect.left, |
| 131 | + rect.bottom, |
| 132 | + rect.right, |
| 133 | + rect.top, |
| 134 | + ) |
| 135 | + ) |
133 | 136 |
|
134 | 137 | # slice an image out of the sprite sheet
|
135 | 138 | def get_texture(
|
136 |
| - self, |
137 |
| - x: int, |
138 |
| - y: int, |
139 |
| - width: int, |
140 |
| - height: int, |
141 |
| - hit_box_algorithm: HitBoxAlgorithm | None = None, |
142 |
| - origin: OriginChoices = "upper_left", |
| 139 | + self, rect: Rect, hit_box_algorithm: HitBoxAlgorithm | None = None, y_up=False |
143 | 140 | ) -> Texture:
|
144 | 141 | """
|
145 | 142 | Slice out texture from the sprite sheet.
|
146 | 143 |
|
147 | 144 | Args:
|
148 |
| - x: |
149 |
| - X position of the texture (lower left corner). |
150 |
| - y: |
151 |
| - Y position of the texture (lower left corner). |
152 |
| - width: |
153 |
| - Width of the texture. |
154 |
| - height: |
155 |
| - Height of the texture. |
| 145 | + rect: |
| 146 | + The rectangle to crop out. |
156 | 147 | hit_box_algorithm:
|
157 | 148 | Hit box algorithm to use for the texture.
|
158 | 149 | If not provided, the default hit box algorithm will be used.
|
159 |
| - origin: |
160 |
| - Origin of the texture. Default is "upper_left". |
161 |
| - Options are "upper_left" or "lower_left". |
162 | 150 | """
|
163 |
| - im = self.get_image(x, y, width, height, origin=origin) |
| 151 | + im = self.get_image(rect, y_up) |
164 | 152 | texture = Texture(im, hit_box_algorithm=hit_box_algorithm)
|
165 | 153 | texture.file_path = self._path
|
166 |
| - texture.crop_values = x, y, width, height |
| 154 | + texture.crop_values = rect.lbwh_int |
167 | 155 | return texture
|
168 | 156 |
|
169 | 157 | def get_image_grid(
|
|
0 commit comments