@@ -25,11 +25,16 @@ defmodule Scenic.Primitive.Sprites do
25
25
are executed in order when the primitive renders.
26
26
27
27
`[ {{src_x, src_y}, {src_w, src_h}, {dst_x, dst_y}, {dst_w, dst_h}} ]`
28
+ or
29
+ `[ {{src_x, src_y}, {src_w, src_h}, {dst_x, dst_y}, {dst_w, dst_h}}, alpha ]`
28
30
29
31
Each draw command is an x/y position and width/height of a rectangle in
30
32
the source image, followed by the x/y position and width/height
31
33
rectangle in the destination space.
32
34
35
+ An optional alpha channel can be set in last position to apply a transparency
36
+ effect on the sprite.
37
+
33
38
In other words, This copies rectangular images from the source
34
39
indicated by image_id and draws them in the coordinate space of
35
40
the graph.
@@ -59,14 +64,16 @@ defmodule Scenic.Primitive.Sprites do
59
64
You should add/modify primitives via the helper functions in
60
65
[`Scenic.Primitives`](Scenic.Primitives.html#sprites/3)
61
66
62
- This example draws the same source rectangle twice in different locations.
63
- The first is at full size, the second is expanded 10x.
67
+ This example draws the same source rectangle three times in different locations.
68
+ The first is at full size, the second is expanded 10x, the third is with a
69
+ 50% transparency effect.
64
70
65
71
```elixir
66
72
graph
67
73
|> sprites( { "images/my_sprites.png", [
68
74
{{0,0}, {10, 20}, {10, 10}, {10, 20}},
69
75
{{0,0}, {10, 20}, {100, 100}, {100, 200}},
76
+ {{0,0}, {10, 20}, {100, 100}, {100, 200}, 0.5}
70
77
]})
71
78
```
72
79
"""
@@ -81,7 +88,8 @@ defmodule Scenic.Primitive.Sprites do
81
88
{ sx :: number , sy :: number } ,
82
89
{ sw :: number , sh :: number } ,
83
90
{ dx :: number , dy :: number } ,
84
- { dw :: number , dh :: number }
91
+ { dw :: number , dh :: number } ,
92
+ alpha :: number
85
93
}
86
94
@ type draw_cmds :: [ draw_cmd ( ) ]
87
95
@@ -217,22 +225,39 @@ defmodule Scenic.Primitive.Sprites do
217
225
end
218
226
end
219
227
228
+ @ default_alpha 1
229
+
220
230
defp validate_commands ( commands ) do
221
- commands
222
- |> Enum . reduce ( { :ok , commands } , fn
223
- _ , { :error , _ } = error ->
224
- error
225
-
226
- { { src_x , src_y } , { src_w , src_h } , { dst_x , dst_y } , { dst_w , dst_h } } , acc
227
- when is_number ( src_x ) and is_number ( src_y ) and
228
- is_number ( src_w ) and is_number ( src_h ) and
229
- is_number ( dst_x ) and is_number ( dst_y ) and
230
- is_number ( dst_w ) and is_number ( dst_h ) ->
231
- acc
232
-
233
- cmd , _ ->
234
- { :error , :command , cmd }
235
- end )
231
+ validate =
232
+ Enum . reduce_while ( commands , { :ok , [ ] } , fn
233
+ { { src_x , src_y } , { src_w , src_h } , { dst_x , dst_y } , { dst_w , dst_h } } , { :ok , cmds }
234
+ when is_number ( src_x ) and is_number ( src_y ) and
235
+ is_number ( src_w ) and is_number ( src_h ) and
236
+ is_number ( dst_x ) and is_number ( dst_y ) and
237
+ is_number ( dst_w ) and is_number ( dst_h ) ->
238
+ { :cont ,
239
+ { :ok ,
240
+ [
241
+ { { src_x , src_y } , { src_w , src_h } , { dst_x , dst_y } , { dst_w , dst_h } , @ default_alpha }
242
+ | cmds
243
+ ] } }
244
+
245
+ cmd = { { src_x , src_y } , { src_w , src_h } , { dst_x , dst_y } , { dst_w , dst_h } , alpha } , { :ok , cmds }
246
+ when is_number ( src_x ) and is_number ( src_y ) and
247
+ is_number ( src_w ) and is_number ( src_h ) and
248
+ is_number ( dst_x ) and is_number ( dst_y ) and
249
+ is_number ( dst_w ) and is_number ( dst_h ) and
250
+ is_number ( alpha ) ->
251
+ { :cont , { :ok , [ cmd | cmds ] } }
252
+
253
+ cmd , _ ->
254
+ { :halt , { :error , :command , cmd } }
255
+ end )
256
+
257
+ case validate do
258
+ { :ok , cmds } -> { :ok , Enum . reverse ( cmds ) }
259
+ error -> error
260
+ end
236
261
end
237
262
238
263
# --------------------------------------------------------
0 commit comments