1 -- ase -- allegro-sprite-editor: the ultimate sprites factory
2 -- Copyright (C) 2001-2004 by David A. Capello
4 -- Read "LEGAL.txt" for more information.
7 local function get_layer_by_name (sprite, layer, name)
8 if layer.readable == false then
12 if layer.name and strcmp (layer.name, name) == 0 then
16 if layer_is_set (layer) then
21 sub = get_layer_by_name (sprite, it, name)
33 local function count_layers (layer)
36 if layer.parent.type == GFXOBJ_SPRITE then
42 if layer_is_set (layer) then
43 local it = layer.layers;
45 count = count + count_layers (it);
53 -- Layer *GetLayerByName (const char *name);
54 function GetLayerByName (name)
55 local sprite = current_sprite;
57 if sprite and name then
58 return get_layer_by_name (sprite, sprite.set, name);
64 -- const char *GetUniqueLayerName (void);
65 function GetUniqueLayerName ()
66 local sprite = current_sprite;
69 return _("Layer") .. " " .. count_layers (sprite.set);
75 -- void SelectLayer (Layer *layer);
76 function SelectLayer (layer)
77 if current_sprite then
78 sprite_set_layer (current_sprite, layer);
82 -- Layer *NewLayer (const char *name, int x, int y, int w, int h);
83 -- creates a new layer with the "name" in the current sprite (in the
84 -- current frame) with the specified position and size (if w=h=0 the
85 -- routine will use the sprite dimension)
86 function NewLayer (name, x, y, w, h)
87 local sprite = current_sprite;
89 local image, frame, index;
91 if sprite and name then
92 if not w or w == 0 then w = sprite.w; end
93 if not h or h == 0 then h = sprite.h; end
96 image = image_new (sprite.imgtype, w, h);
102 layer = layer_image_new (sprite.imgtype, w, h);
108 -- clear with mask color
109 image_clear (image, 0);
111 -- configure layer name and blend mode
112 layer_set_name (layer, name);
113 layer_set_blend_mode (layer, BLEND_MODE_NORMAL);
115 -- add image in the layer stock
116 index = stock_add_image (layer.stock, image);
118 -- create frame (XXX in the current frpos? --dacap)
119 frame = frame_new (sprite.frpos, index, x, y, 255);
122 layer_add_frame (layer, frame);
125 if undo_is_enabled (sprite.undo) then
126 undo_open (sprite.undo);
127 undo_add_layer (sprite.undo, sprite.set, layer);
128 undo_set_layer (sprite.undo, sprite);
129 undo_close (sprite.undo);
132 -- add the layer in the sprite set
133 layer_add_layer (sprite.set, layer);
135 -- select the new layer
136 sprite_set_layer (sprite, layer);
142 -- Layer *NewLayerSet (const char *name);
143 -- creates a new layer set with the "name" in the current sprite
144 function NewLayerSet (name)
145 local sprite = current_sprite;
148 if sprite and name then
150 layer = layer_set_new ();
155 -- configure layer name and blend mode
156 layer_set_name (layer, name);
158 -- add the layer in the sprite set
159 layer_add_layer (sprite.set, layer);
161 -- select the new layer
162 sprite_set_layer (sprite, layer);
168 -- void RemoveLayer (void);
169 -- removes the current selected layer
170 function RemoveLayer ()
171 local sprite = current_sprite;
173 if sprite and sprite.layer then
174 local layer = sprite.layer;
175 local parent = layer.parent;
178 -- select: previous layer, or next layer, or parent (if it is not
179 -- the main layer of sprite set)
181 layer_select = layer.prev;
182 elseif layer.next then
183 layer_select = layer.next;
184 elseif parent != sprite.set then
185 layer_select = parent;
191 if undo_is_enabled (sprite.undo) then
192 undo_open (sprite.undo);
193 undo_set_layer (sprite.undo, sprite);
194 undo_remove_layer (sprite.undo, layer);
195 undo_close (sprite.undo);
198 -- select other layer
199 sprite_set_layer (sprite, layer_select);
202 layer_remove_layer (parent, layer);
209 -- void MoveLayerTop (void);
210 -- moves the current layer in the top of the main set
211 function MoveLayerTop ()
212 if current_sprite and current_sprite.layer then
213 local layer = current_sprite.layer;
215 layer_remove_layer (layer.parent, layer);
217 layer_add_layer (current_sprite.set, layer);
221 -- void MoveLayerBottom (void);
222 -- moves the current layer in the bottom of the main set
223 function MoveLayerBottom ()
224 if current_sprite and current_sprite.layer then
225 local layer = current_sprite.layer;
227 layer_remove_layer (layer.parent, layer);
229 layer_add_layer (current_sprite.set, layer);
230 layer_move_layer (current_sprite.set, layer, nil);
234 -- void MoveLayerBefore (Layer *this_one);
235 -- moves the current layer above the layer "this_one"
236 function MoveLayerBefore (this_one)
237 if current_sprite and current_sprite.layer then
238 local layer = current_sprite.layer;
242 layer_dest = current_sprite.set;
244 layer_dest = this_one;
248 layer_remove_layer (layer.parent, layer);
249 layer_add_layer (layer_dest.parent, layer);
250 layer_move_layer (layer_dest.parent, layer, layer_dest);
255 -- void MoveLayerAfter (Layer *this_one);
256 -- moves the current layer below the layer "this_one" (if that layer
257 -- is a set, the layer is put in the top of the layer set)
258 function MoveLayerAfter (this_one)
259 if current_sprite and current_sprite.layer then
260 local layer = current_sprite.layer;
264 layer_dest = current_sprite.set;
266 layer_dest = this_one;
270 layer_remove_layer (layer.parent, layer);
272 -- insert in the first position of the set
273 if layer_is_set (layer_dest) then
274 layer_add_layer (layer_dest, layer);
275 -- insert below the layer
277 layer_add_layer (layer_dest.parent, layer);
278 layer_move_layer (layer_dest.parent, layer, layer_dest.prev);