summaryrefslogtreecommitdiff
path: root/test/etags/lua-src/allegro.lua
blob: c316b6f26a0d867f38349de354a6899d7315559a (plain)
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
-- ase -- allegro-sprite-editor: the ultimate sprites factory
-- Copyright (C) 2001-2004 by David A. Capello
--
-- Read "LEGAL.txt" for more information.

-- internal routine
local function get_layer_by_name (sprite, layer, name)
  if layer.readable == false then
    return nil;
  end

  if layer.name and strcmp (layer.name, name) == 0 then
    return layer;
  end

  if layer_is_set (layer) then
    local it, sub;

    it = layer.layers;
    while it do
      sub = get_layer_by_name (sprite, it, name)
      if sub then
	return sub;
      end
      it = it.next
    end
  end

  return nil;
end

-- internal routine
local function count_layers (layer)
  local count;

  if layer.parent.type == GFXOBJ_SPRITE then
    count = 0;
  else
    count = 1;
  end

  if layer_is_set (layer) then
    local it = layer.layers;
    while it do
      count = count + count_layers (it);
      it = it.next;
    end
  end

  return count;
end

-- Layer *GetLayerByName (const char *name);
function GetLayerByName (name)
  local sprite = current_sprite;

  if sprite and name then
    return get_layer_by_name (sprite, sprite.set, name);
  else
    return nil;
  end
end

-- const char *GetUniqueLayerName (void);
function GetUniqueLayerName ()
  local sprite = current_sprite;

  if sprite then
    return _("Layer") .. " " .. count_layers (sprite.set);
  else
    return nil;
  end
end

-- void SelectLayer (Layer *layer);
function SelectLayer (layer)
  if current_sprite then
    sprite_set_layer (current_sprite, layer);
  end
end

-- Layer *NewLayer (const char *name, int x, int y, int w, int h);
-- creates a new layer with the "name" in the current sprite (in the
-- current frame) with the specified position and size (if w=h=0 the
-- routine will use the sprite dimension)
function NewLayer (name, x, y, w, h)
  local sprite = current_sprite;
  local layer = nil;
  local image, frame, index;

  if sprite and name then
    if not w or w == 0 then w = sprite.w; end
    if not h or h == 0 then h = sprite.h; end
 
    -- new image
    image = image_new (sprite.imgtype, w, h);
    if not image then
      return nil;
    end

    -- new layer
    layer = layer_image_new (sprite.imgtype, w, h);
    if not layer then
      image_free (image);
      return nil;
    end

    -- clear with mask color
    image_clear (image, 0);

    -- configure layer name and blend mode
    layer_set_name (layer, name);
    layer_set_blend_mode (layer, BLEND_MODE_NORMAL);

    -- add image in the layer stock
    index = stock_add_image (layer.stock, image);

    -- create frame (XXX in the current frpos? --dacap)
    frame = frame_new (sprite.frpos, index, x, y, 255);

    -- add frame
    layer_add_frame (layer, frame);

    -- undo stuff
    if undo_is_enabled (sprite.undo) then
      undo_open (sprite.undo);
      undo_add_layer (sprite.undo, sprite.set, layer);
      undo_set_layer (sprite.undo, sprite);
      undo_close (sprite.undo);
    end

    -- add the layer in the sprite set
    layer_add_layer (sprite.set, layer);

    -- select the new layer
    sprite_set_layer (sprite, layer);
  end

  return layer;
end

-- Layer *NewLayerSet (const char *name);
-- creates a new layer set with the "name" in the current sprite
function NewLayerSet (name)
  local sprite = current_sprite;
  local layer = nil;

  if sprite and name then
    -- new layer
    layer = layer_set_new ();
    if not layer then
      return nil;
    end

    -- configure layer name and blend mode
    layer_set_name (layer, name);

    -- add the layer in the sprite set
    layer_add_layer (sprite.set, layer);

    -- select the new layer
    sprite_set_layer (sprite, layer);
  end

  return layer;
end

-- void RemoveLayer (void);
-- removes the current selected layer
function RemoveLayer ()
  local sprite = current_sprite;

  if sprite and sprite.layer then
    local layer = sprite.layer;
    local parent = layer.parent;
    local layer_select;

    -- select: previous layer, or next layer, or parent (if it is not
    -- the main layer of sprite set)
    if layer.prev then
      layer_select = layer.prev;
    elseif layer.next then
      layer_select = layer.next;
    elseif parent != sprite.set then
      layer_select = parent;
    else
      layer_select = nil;
    end

    -- undo stuff
    if undo_is_enabled (sprite.undo) then
      undo_open (sprite.undo);
      undo_set_layer (sprite.undo, sprite);
      undo_remove_layer (sprite.undo, layer);
      undo_close (sprite.undo);
    end

    -- select other layer
    sprite_set_layer (sprite, layer_select);

    -- remove the layer
    layer_remove_layer (parent, layer);

    -- destroy the layer
    layer_free (layer);
  end
end

-- void MoveLayerTop (void);
-- moves the current layer in the top of the main set
function MoveLayerTop ()
  if current_sprite and current_sprite.layer then
    local layer = current_sprite.layer;

    layer_remove_layer (layer.parent, layer);

    layer_add_layer (current_sprite.set, layer);
  end
end

-- void MoveLayerBottom (void);
-- moves the current layer in the bottom of the main set
function MoveLayerBottom ()
  if current_sprite and current_sprite.layer then
    local layer = current_sprite.layer;

    layer_remove_layer (layer.parent, layer);

    layer_add_layer (current_sprite.set, layer);
    layer_move_layer (current_sprite.set, layer, nil);
  end
end

-- void MoveLayerBefore (Layer *this_one);
-- moves the current layer above the layer "this_one"
function MoveLayerBefore (this_one)
  if current_sprite and current_sprite.layer then
    local layer = current_sprite.layer;
    local layer_dest;

    if not this_one then
      layer_dest = current_sprite.set;
    else
      layer_dest = this_one;
    end

    if layer_dest then
      layer_remove_layer (layer.parent, layer);
      layer_add_layer (layer_dest.parent, layer);
      layer_move_layer (layer_dest.parent, layer, layer_dest);
    end
  end
end

-- void MoveLayerAfter (Layer *this_one);
-- moves the current layer below the layer "this_one" (if that layer
-- is a set, the layer is put in the top of the layer set)
function MoveLayerAfter (this_one)
  if current_sprite and current_sprite.layer then
    local layer = current_sprite.layer;
    local layer_dest;

    if not this_one then
      layer_dest = current_sprite.set;
    else
      layer_dest = this_one;
    end

    if layer_dest then
      layer_remove_layer (layer.parent, layer);

      -- insert in the first position of the set
      if layer_is_set (layer_dest) then
	layer_add_layer (layer_dest, layer);
      -- insert below the layer
      else
	layer_add_layer (layer_dest.parent, layer);
	layer_move_layer (layer_dest.parent, layer, layer_dest.prev);
      end
    end
  end
end