@@ -25,11 +25,16 @@ defmodule Scenic.Primitive.Sprites do
2525 are executed in order when the primitive renders.
2626
2727 `[ {{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 ]`
2830
2931 Each draw command is an x/y position and width/height of a rectangle in
3032 the source image, followed by the x/y position and width/height
3133 rectangle in the destination space.
3234
35+ An optional alpha channel can be set in last position to apply a transparency
36+ effect on the sprite.
37+
3338 In other words, This copies rectangular images from the source
3439 indicated by image_id and draws them in the coordinate space of
3540 the graph.
@@ -59,14 +64,16 @@ defmodule Scenic.Primitive.Sprites do
5964 You should add/modify primitives via the helper functions in
6065 [`Scenic.Primitives`](Scenic.Primitives.html#sprites/3)
6166
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.
6470
6571 ```elixir
6672 graph
6773 |> sprites( { "images/my_sprites.png", [
6874 {{0,0}, {10, 20}, {10, 10}, {10, 20}},
6975 {{0,0}, {10, 20}, {100, 100}, {100, 200}},
76+ {{0,0}, {10, 20}, {100, 100}, {100, 200}, 0.5}
7077 ]})
7178 ```
7279 """
@@ -81,7 +88,8 @@ defmodule Scenic.Primitive.Sprites do
8188 { sx :: number , sy :: number } ,
8289 { sw :: number , sh :: number } ,
8390 { dx :: number , dy :: number } ,
84- { dw :: number , dh :: number }
91+ { dw :: number , dh :: number } ,
92+ alpha :: number
8593 }
8694 @ type draw_cmds :: [ draw_cmd ( ) ]
8795
@@ -217,22 +225,39 @@ defmodule Scenic.Primitive.Sprites do
217225 end
218226 end
219227
228+ @ default_alpha 1
229+
220230 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
236261 end
237262
238263 # --------------------------------------------------------
0 commit comments