-
Notifications
You must be signed in to change notification settings - Fork 374
Expand file tree
/
Copy pathrect.py
More file actions
417 lines (361 loc) · 12.6 KB
/
Copy pathrect.py
File metadata and controls
417 lines (361 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import array
from arcade import gl
from arcade.color import WHITE
from arcade.math import rotate_point
from arcade.sprite import BasicSprite
from arcade.texture import Texture
from arcade.texture_atlas.base import TextureAtlasBase
from arcade.types import LBWH, LRBT, XYWH, Color, Point2List, Rect, RGBOrA255
from arcade.window_commands import get_window
from .helpers import _generic_draw_line_strip
def draw_texture_rect(
texture: Texture,
rect: Rect,
*,
color: Color = WHITE,
angle=0.0,
blend=True,
alpha=255,
pixelated=False,
atlas: TextureAtlasBase | None = None,
) -> None:
"""
Draw a texture on a rectangle.
Args:
texture:
The texture to draw.
rect:
Rectangle to draw the texture on.
color:
Color multiplier for the texture. Defaults to white.
angle:
Rotation of the texture in degrees. Defaults to zero.
blend:
If True, enable alpha blending. Defaults to True.
alpha:
Transparency of image. 0 is fully transparent, 255 (default) is fully opaque.
atlas:
The texture atlas the texture resides in.
if not supplied the default texture atlas is used
"""
ctx = get_window().ctx
# Clamp alpha to 0-255
alpha_normalized = max(0, min(255, alpha)) / 255.0
blend_state = ctx.is_enabled(ctx.BLEND)
if blend:
ctx.enable(ctx.BLEND)
else:
ctx.disable(ctx.BLEND)
atlas = atlas or ctx.default_atlas
program = ctx.sprite_program_single_simple
texture_id, _ = atlas.add(texture)
if pixelated:
atlas.texture.filter = gl.NEAREST, gl.NEAREST
program.set_uniform_safe("uv_offset_bias", 0.0)
else:
atlas.texture.filter = gl.LINEAR, gl.LINEAR
program.set_uniform_safe("uv_offset_bias", 1.0)
atlas.texture.use(unit=0)
atlas.use_uv_texture(unit=1)
geometry = ctx.geometry_empty
program["pos_rot"] = rect.x, rect.y, 0, angle
program["color"] = color.normalized
program["size"] = rect.width, rect.height
program["texture_id"] = texture_id
program["spritelist_color"] = 1.0, 1.0, 1.0, alpha_normalized
geometry.render(program, mode=gl.TRIANGLE_STRIP, vertices=4)
if blend_state:
ctx.enable(ctx.BLEND)
else:
ctx.disable(ctx.BLEND)
def draw_sprite(
sprite: BasicSprite,
*,
blend: bool = True,
alpha=255,
pixelated=False,
atlas: TextureAtlasBase | None = None,
) -> None:
"""
Draw a sprite.
Args:
sprite:
The sprite to draw.
blend:
Draw the sprite with or without alpha blending
alpha:
Fade the sprite from completely transparent to opaque (range: 0 to 255)
pixelated:
If true the sprite will be render in pixelated style. Otherwise smooth/linear
atlas:
The texture atlas the texture resides in.
if not supplied the default texture atlas is used
"""
draw_texture_rect(
sprite.texture,
rect=XYWH(sprite.center_x, sprite.center_y, sprite.width, sprite.height),
color=sprite.color,
angle=sprite._angle,
blend=blend,
alpha=alpha,
pixelated=pixelated,
atlas=atlas,
)
def draw_sprite_rect(
sprite: BasicSprite,
rect: Rect,
*,
blend: bool = True,
alpha=255,
pixelated=False,
atlas: TextureAtlasBase | None = None,
) -> None:
"""Draw a sprite.
Args:
sprite:
The sprite to draw.
rect:
The location and size of the sprite
blend:
Draw the sprite with or without alpha blending
alpha:
Fade the sprite from completely transparent to opaque (range: 0 to 255)
pixelated:
If true the sprite will be render in pixelated style.
Otherwise smooth/linear
atlas:
The texture atlas the texture resides in.
if not supplied the default texture atlas is used
"""
draw_texture_rect(
sprite.texture,
rect=rect,
color=sprite.color,
angle=sprite._angle,
blend=blend,
alpha=alpha,
pixelated=pixelated,
atlas=atlas,
)
def draw_lrbt_rectangle_outline(
left: float,
right: float,
bottom: float,
top: float,
color: RGBOrA255,
border_width: float = 1,
) -> None:
"""
Draw a rectangle by specifying left, right, bottom and top edges.
Args:
left:
The x coordinate of the left edge of the rectangle.
right:
The x coordinate of the right edge of the rectangle.
bottom:
The y coordinate of the rectangle bottom.
top:
The y coordinate of the top of the rectangle.
color:
The outline color as an RGBA :py:class:`tuple`, RGB
:py:class:`tuple`, or a :py:class:`.Color` instance.
border_width:
The width of the border in pixels. Defaults to one.
Raises:
ValueError: Raised if left > right or top < bottom.
"""
if left > right:
raise ValueError("Left coordinate must be less than or equal to the right coordinate")
if bottom > top:
raise ValueError("Bottom coordinate must be less than or equal to the top coordinate")
draw_rect_outline(LRBT(left, right, bottom, top), color, border_width)
def draw_lbwh_rectangle_outline(
left: float,
bottom: float,
width: float,
height: float,
color: RGBOrA255,
border_width: float = 1,
) -> None:
"""
Draw a rectangle extending from bottom left to top right
Args:
left:
The x coordinate of the left edge of the rectangle.
bottom:
The y coordinate of the bottom of the rectangle.
width:
The width of the rectangle.
height:
The height of the rectangle.
color:
The outline color as an RGBA :py:class:`tuple`, RGB
:py:class:`tuple`, or a :py:class:`.Color` instance.
border_width:
The width of the border in pixels. Defaults to one.
"""
draw_rect_outline(LBWH(left, bottom, width, height), color, border_width)
def draw_lrbt_rectangle_filled(
left: float, right: float, bottom: float, top: float, color: RGBOrA255
) -> None:
"""
Draw a rectangle by specifying left, right, bottom and top edges.
Args:
left:
The x coordinate of the left edge of the rectangle.
right:
The x coordinate of the right edge of the rectangle.
bottom:
The y coordinate of the rectangle bottom.
top:
The y coordinate of the top of the rectangle.
color:
The fill color as an RGBA :py:class:`tuple`,
RGB :py:class:`tuple`, or a :py:class:`.Color` instance.
Raises:
ValueError: Raised if left > right or top < bottom.
"""
if left > right:
raise ValueError(
f"Left coordinate {left} must be less than or equal to the right coordinate {right}"
)
if bottom > top:
raise ValueError(
f"Bottom coordinate {bottom} must be less than or equal to the top coordinate {top}"
)
draw_rect_filled(LRBT(left, right, bottom, top), color)
def draw_lbwh_rectangle_filled(
left: float, bottom: float, width: float, height: float, color: RGBOrA255
) -> None:
"""
Draw a filled rectangle extending from bottom left to top right
Args:
left:
The x coordinate of the left edge of the rectangle.
bottom:
The y coordinate of the bottom of the rectangle.
width:
The width of the rectangle.
height:
The height of the rectangle.
color:
The fill color as an RGBA :py:class:`tuple`, RGB
:py:class:`tuple`, :py:class:`.Color` instance
"""
draw_rect_filled(LBWH(left, bottom, width, height), color)
def draw_rect_outline(
rect: Rect, color: RGBOrA255, border_width: float = 1, tilt_angle: float = 0
) -> None:
"""
Draw a rectangle outline.
Args:
rect:
The rectangle to draw. a :py:class:`~arcade.Rect` instance.
color:
The outline color as an RGBA :py:class:`tuple`,
RGB :py:class:`tuple`, or :py:class:`.Color` instance.
border_width:
width of the lines, in pixels.
tilt_angle:
rotation of the rectangle. Defaults to zero (clockwise).
"""
HALF_BORDER = border_width / 2
# Extremely unrolled code below
# fmt: off
left = rect.left
right = rect.right
bottom = rect.bottom
top = rect.top
x = rect.x
y = rect.y
# o = outer, i = inner
#
# o_left, o_top o_right, o_top
# +----------------------------------------+
# | i_left, i_top i_right, i_top |
# | +----------------------------------+ |
# | | | |
# | | | |
# | | | |
# | +----------------------------------+ |
# | i_left, i_bottom i_right , i_bottom |
# +----------------------------------------+
# o_left, o_bottom o_right, o_bottom
i_left = left + HALF_BORDER
i_bottom = bottom + HALF_BORDER
i_right = right - HALF_BORDER
i_top = top - HALF_BORDER
o_left = left - HALF_BORDER
o_bottom = bottom - HALF_BORDER
o_right = right + HALF_BORDER
o_top = top + HALF_BORDER
# Declared separately because the code below seems to break mypy
point_list: Point2List
# This is intentionally unrolled to minimize repacking tuples
if tilt_angle == 0:
point_list = (
(o_left , o_top ),
(i_left , i_top ),
(o_right , o_top ),
(i_right , i_top ),
(o_right , o_bottom),
(i_right , i_bottom),
(o_left , o_bottom),
(i_left , i_bottom),
(o_left , o_top ),
(i_left , i_top )
)
else:
point_list = (
rotate_point(o_left , o_top , x, y, tilt_angle),
rotate_point(i_left , i_top , x, y, tilt_angle),
rotate_point(o_right , o_top , x, y, tilt_angle),
rotate_point(i_right , i_top , x, y, tilt_angle),
rotate_point(o_right , o_bottom, x, y, tilt_angle),
rotate_point(i_right , i_bottom, x, y, tilt_angle),
rotate_point(o_left , o_bottom, x, y, tilt_angle),
rotate_point(i_left , i_bottom, x, y, tilt_angle),
rotate_point(o_left , o_top , x, y, tilt_angle),
rotate_point(i_left , i_top , x, y, tilt_angle)
)
# fmt: on
_generic_draw_line_strip(point_list, color, gl.TRIANGLE_STRIP)
def draw_rect_filled(rect: Rect, color: RGBOrA255, tilt_angle: float = 0) -> None:
"""
Draw a filled-in rectangle.
Args:
rect:
The rectangle to draw. a :py:class:`~arcade.Rect` instance.
color:
The fill color as an RGBA :py:class:`tuple`,
RGB :py:class:`tuple`, or :py:class:`.Color` instance.
tilt_angle:
rotation of the rectangle (clockwise). Defaults to zero.
"""
# Fail if we don't have a window, context, or right GL abstractions
window = get_window()
ctx = window.ctx
program = ctx.shape_rectangle_filled_unbuffered_program # type: ignore
geometry = ctx.shape_rectangle_filled_unbuffered_geometry
buffer = ctx.shape_rectangle_filled_unbuffered_buffer # type: ignore
# Validate & normalize to a pass the shader an RGBA float uniform
color_normalized = Color.from_iterable(color).normalized
# contextmanager will restore state which existed before
with ctx.enabled(ctx.BLEND):
# Pass data to the shader
program["color"] = color_normalized
program["shape"] = rect.width, rect.height, tilt_angle
buffer.orphan()
buffer.write(data=array.array("f", (rect.x, rect.y)))
geometry.render(program, instances=1)
# These might be "oddly specific" and also needs docstrings. Disabling or 3.0.0
# def draw_rect_outline_kwargs(
# color: RGBAOrA255 = WHITE, border_width: int = 1, tilt_angle: float = 0, **kwargs: AsFloat
# ) -> None:
# rect = Rect.from_kwargs(**kwargs)
# draw_rect_outline(rect, color, border_width, tilt_angle)
# def draw_rect_filled_kwargs(
# color: RGBAOrA255 = WHITE, tilt_angle: float = 0, **kwargs: AsFloat
# ) -> None:
# rect = Rect.from_kwargs(**kwargs)
# draw_rect_filled(rect, color, tilt_angle)