\hypertarget{lua_script_8edc-example}{}\section{lua\+\_\+script.\+edc}
This example shows the usage of lua scripting to create and animate some objects in the canvas.


\begin{DoxyCodeInclude}
1 color\_classes \{
2     color\_class \{ name: "test\_colour"; color: 255 255 255 255; \}
3 \}
4 
5 fonts \{
6    font: "Vera.ttf" "default";
7 \}
8 
9 images \{
10     image: "bubble.png" COMP;
11     image: "test.png" COMP;
12 \}
13 
14 collections \{
15    group \{
16       name: "main";
17       lua\_script\_only: 1;
18       lua\_script \{
19          --// stick object private/local vars here
20          local D;
21          local count = 0;
22          local fndata = 99;
23          local text\_geom;
24 
25          --// Functions to print tables.
26          local print\_table, print\_table\_start;
27 
28          function print\_table\_start(table, space, name)
29             print(space .. name .. ": ");
30             print(space .. "\{");
31             print\_table(table, space .. "  ");
32             print(space .. "\}");
33          end
34 
35          function print\_table(table, space)
36             for k, v in pairs(table) do 
37                if type(v) == "table" then
38                   print\_table\_start(v, space, k);
39                elseif type(v) == "string" then
40                   print(space .. k .. ': "' .. v .. '";')
41                else
42                   print(space .. k .. ": " .. v .. ";")
43                end
44             end
45          end
46 
47          local function mycb3 (v)
48             print("lua::callback transition " .. D.val .. " v: " .. v);
49             d = \{\};
50             d = edje.size(d);
51             print("lua::objsize= " .. d.w .. " , " .. d.h);
52             sz = \{w=v * 80, h=v * 40\};
53             D.rect:geom(((d.w / 2) * math.sin(v * 2 * math.pi)) + ((d.w - sz.w) / 2),
54                         ((d.h / 2) * math.cos(v * 2 * math.pi)) + ((d.h - sz.h) / 2),
55                         sz.w, sz.h);
56             D.rect:color(255, 128, v * 255, 255);
57             d = D.rect:move(d);
58             print("lua::pos= " .. d.x .. " , " .. d.y);
59  
60             r = D.rect:above();
61             if (r ~= nil) then
62                print("lua::rcol");
63                r:color(20, v * 255, 60, 255);
64             else
65                print("lua::r none!!!!!!!!!!!!!!1");
66             end
67             d = edje.size();
68             D.clip:geom(10, 10, d.w - 20, d.h - 20);
69             c = D.clip:clipees();
70             for i=1,#c,1 do
71                d = c[i]:geom();
72                print("lua::" .. i .. " geom = " .. d.x .. "," .. d.y .. " " .. d.w .. "x" .. d.h);
73             end
74             return true;  --// repeat the timer
75          end
76  
77          local function mycb2 ()
78             print("lua::callback animator " .. count .. " seconds: " .. edje.seconds() .. " looptime: " ..
       edje.looptime());
79             edje.color\_class("test\_colour", 255, (count * 10) % 255, 255, 255);
80             edje.text\_class("test\_text\_class", "Sans:style=Bold", ((count * 3) % 100) + 8);
81             if (5 > (count % 10)) then
82                D.text:font("default", 32);
83             else
84                D.text:font("Sans:style=Bold", 32);
85             end
86             edje\_geom = edje.geom();
87             text\_geom = D.text:geom();
88             D.text:move((edje\_geom.w - text\_geom.w) / 2, (edje\_geom.h - text\_geom.h) / 8);
89             return true;  --// repeat the timer
90          end
91  
92          local function mycb ()
93             print("lua::callback timer " .. count .. " fndata = " .. fndata);
94             count = count + 1; --// keep count of calls - object data
95             fndata = fndata + 3; --// play with object vars to see if they persist
96             D.tim = edje.timer(0.25, mycb); --// inside cb add new timer
97             return false; --// cease repeating the timer
98          end
99  
100          --// init object here
101          D = \{\}; --// data is empty table to start
102          D.val = math.random(); --// start with some random value so
103          fndata = fndata + D.val; --// func data start point
104          print("lua::init ... " .. D.val);
105          edje.echo("lua::echo('hello world')");
106 
107          --// How to check the edje version.
108          version = edje.version();
109          print("The edje version number is " .. version.major .. "." .. version.minor);
110 
111          --// actually add the timer to call mycb in 1.23 sec
112          D.tim = edje.timer(1.23, mycb);
113          D.tra = edje.transition(5.0, mycb3);
114          D.ani = edje.animator(mycb2);
115          edje\_geom = edje.geom();
116  
117          if (edje.spanky) then edje.spanky(); end
118 
119          local date = edje.date();
120          print("lua:: date: " ..
121             date.year .. "|" ..
122             date.month .. "|" ..
123             date.day .. "|" ..
124             date.yearday .. "|" ..
125             date.weekday .. "|" ..
126             date.hour .. "|" ..
127             date.min .. "|" ..
128             date.sec
129          );
130 
131          --// send some random edje message
132          edje.messagesend(7, "none"      );
133          edje.messagesend(7, "sig",      "signal", "source");
134          edje.messagesend(7, "str",      "hello world");
135          edje.messagesend(7, "int",      987);
136          edje.messagesend(7, "float",    987.321);
137          edje.messagesend(7, "strset",   \{"hello", "there", "world"\});
138          edje.messagesend(7, "intset",   \{1, 2, 3\});
139          edje.messagesend(7, "floatset", \{1.1, 2.2, 3.3\});
140          edje.messagesend(7, "strint",   "hello world", 7);
141          edje.messagesend(7, "strfloat", "hello world", 7.654);
142          edje.messagesend(7, "strintset","hello world", \{1, 2, 3\});
143 
144          D.edje = edje.edje();
145          D.edje:file("plain/edje/group");
146          D.edje:show();
147                   
148          D.rect = edje.rect();
149          D.rect:geom  (5, 10, 50, 30);
150          D.rect:color (255, 128, 60, 255);
151          D.rect:show  ();
152                   
153          D.rect2 = edje.rect();
154          D.rect2:geom  (50, 50, 50, 50);
155          D.rect2:color (20, 30, 60, 120);
156          D.rect2:show  ();
157 
158          D.clip = edje.rect();
159          D.clip:geom  (10, 10, 150, 150);
160          D.clip:color (200, 200, 50, 200);
161          D.clip:show  ();
162  
163          D.rect2:clip(D.clip);
164          D.rect:clip(D.clip);
165 
166          D.text = edje.text();
167          D.text:geom  (50, 5, 150, 50);
168          D.text:color (255, 0, 0, 255);
169          D.text:font("Sans:style=Bold", 32);
170          D.text:text("Lua rocks!");
171          text\_geom = D.text:geom();
172          print(D.text:text());
173          D.text:show();
174 
175         --// Put a few bogus API calls here to test the bogus API protection,
176         --// If the bogus API protection works, these should get ignored, but everything else runs
       smoothly.
177         --// Otherwise, the map is not done, the bubbles are not done, but the timers keep runinng.
178          bogus.failme(1, "two", D.rect);
179          temp = bogus.failme2();
180          D.text.bogus();
181          edje.bogus2();
182 
183          --// Fun with maps!
184          D.map = edje.map(4);  --// 4 is the only supported map size at the moment.
185          --// These all do the same thing.
186          --// Note, lua likes to start at 1, C (and thus evas) at 0.  I choose to agree with C.
187          D.map:coord(0, 50, 50, 0);
188          D.map:coord(1, 100, 50, 0);
189          D.map:coord(2, 100, 100, 0);
190          D.map:coord(3, 50, 100, 0);
191          D.map:populate(50, 50, 50, 50, 0);
192          D.map:populate(D.rect2, 0);
193          D.map:populate(D.rect2);
194 
195          --// print the results
196          D.coord = D.map:coord(0);
197          print("lua::map coords for point 0 x=" .. D.coord.x .. " y=" .. D.coord.y .. " z=" .. D.coord.z);
198          D.coord = D.map:coord(1);
199          print("lua::map coords for point 1 x=" .. D.coord.x .. " y=" .. D.coord.y .. " z=" .. D.coord.z);
200          D.coord = D.map:coord(2);
201          print("lua::map coords for point 2 x=" .. D.coord.x .. " y=" .. D.coord.y .. " z=" .. D.coord.z);
202          D.coord = D.map:coord(3);
203          print("lua::map coords for point 3 x=" .. D.coord.x .. " y=" .. D.coord.y .. " z=" .. D.coord.z);
204 
205          D.map:smooth(false);
206          D.map:alpha(true);
207 
208          if (D.map:alpha()) then
209             print("lua::map is alpha");
210          end
211 
212          if (D.map:smooth()) then
213             print("lua::map is smooooth");
214          end
215 
216          if (D.map:clockwise()) then
217             print("lua::map is clockwise");
218          end
219 
220          D.map:color(255, 255, 255, 255); // set all points to this colour.
221          D.map:color(1, 255, 0, 255, 255);  // set just one point to this colour.
222 
223          D.map:lighting(75, 75, 10, 255, 255, 255, 0, 255, 0);  // Ambient light and a 3D light source.
224 
225          --// Toss it around.
226          D.map:rotate(45.0, 75, 75);
227          D.map:zoom(1.5, 1.5, 75, 75);
228          D.map:rotate3d(10.0, 20.0, 30.0, 75, 75, 0);
229          D.map:perspective(200, 200, 0, 20);
230 
231          --// For image UV mapping.
232          D.map:uv(0, 0.0, 0.0);
233          D.map:uv(1, 50.0, 0.0);
234          D.map:uv(2, 50.0, 50.0);
235          D.map:uv(3, 0.0, 50.0);
236 
237          --// Actually apply the resulting transformations.
238          D.rect2:map(D.map);
239          D.rect2:map\_enable(true);
240          if (D.rect2:map\_enable()) then
241             print("lua::map enabled");
242          end
243 
244          D.rect2:map\_source(D.rect);  --// Don't think this is actually implemented in evas.
245 
246 --//         D.map:dup();
247 --//         D.map:size();  --// perhaps overide the # operator?  For now it's only gonna return 4 anyway.
248 
249          --// example of deleting something
250          --// D.tim:del();
251 
252          --// test the color\_class stuff
253          colour = edje.color\_class("test\_colour");
254          print("lua::color\_class= " .. colour.r .. "," .. colour.g .. "," .. colour.b .. "," .. colour.a);
255          colour = edje.color\_class("test\_colour", 32, 64, 255, 128);
256          print("lua::color\_class= " .. colour.r .. "," .. colour.g .. "," .. colour.b .. "," .. colour.a);
257          colour = edje.color\_class("test\_colour", \{ r=255, g=0, b=255, a=255 \});
258          print("lua::color\_class= " .. colour.r .. "," .. colour.g .. "," .. colour.b .. "," .. colour.a);
259          text = edje.text\_class("test\_text\_class", "Sans:style=Bold", 8);
260          print("lua::text\_class= " .. text.font .. " size " .. text.size);
261  
262          --// Do something bad, just to see what happens.
263 --//         edje.color\_class(nil);
264  
265          --// shutdown func - generally empty or not there. everything garbage collected for you
266          function shutdown ()
267             print("lua::shutdown ... " .. D.val);
268          end
269 
270          function show ()
271             print("lua::show ... " .. D.val);
272          end
273 
274          function hide ()
275             print("lua::hide ... " .. D.val);
276          end
277 
278          function move (x, y)
279             print("lua::move x=" .. x .. " x=" .. y);
280             D.edje:move(0, 0);
281          end
282 
283          function resize (w, h)
284             print("lua::resize w=" .. w .. " h=" .. h);
285             D.text:move((w - text\_geom.w) / 2, (h - text\_geom.h) / 8);
286             D.edje:resize(w, h);
287          end
288 
289          function message (id, type, ...)
290             print("lua::message id=" .. id .. " type=" .. type);
291             --// handle your message type here. check id + type then use the
292             --// vararg appropriately. they are the same as the params passed 
293             --// to edje:messagesend() (if any are passed at all).  Any array
294             --// arguments are passed as a single table.
295 
296             if ("none" == type) then
297                print("lua::message no args");
298             elseif ("strset" == type) then
299                strs = ... ;
300                print\_table\_start(strs, "", "lua::message strings");
301             elseif ("intset" == type) then
302                ints = ... ;
303                print\_table\_start(ints, "", "lua::message ints");
304             elseif ("floatset" == type) then
305                floats = ... ;
306                print\_table\_start(floats, "", "lua::message floats");
307             elseif ("strintset" == type) then
308                str, ints = ... ;
309                print("lua::message " .. str);
310                print\_table\_start(ints, "", "lua::message ints");
311             elseif ("strfloatset" == type) then
312                str, floats = ... ;
313                print("lua::message " .. str);
314                print\_table\_start(floats, "", "lua::message floats");
315             else
316                print("lua::message " .. ... );
317             end
318          end
319 
320          function signal (sig, src)
321             print("lua::signal sig= " .. sig .. " src= " .. src);
322          end
323       \}
324    \}
325 
326    // The group name NEEDS a / in it, 
327    // or the part below that tries to swallow it won't work.
328    // Leaving just the lua part visible.
329    group \{
330       name: "bubbles/lua";
331       lua\_script\_only: 1;
332       lua\_script \{
333          local bubbles = \{ \};
334          local bubbleCols = 8;
335          local bubbleRows = 6;
336 
337          for i = 1, bubbleRows do
338             row = \{ \};
339             for j = 1, bubbleCols do
340                image = edje.image();
341                image:image("bubble.png");
342                image:show();
343                table.insert(row, image);
344             end
345             table.insert(bubbles, row);
346          end
347 
348          function resize (w, h)
349             for i = 1, bubbleRows do
350                for j = 1, bubbleCols do
351                   w1 = w / bubbleCols;
352                   h1 = h / bubbleRows;
353                   bubbles[i][j]:geom((j - 1) * w1, (i - 1) * h1, w1, h1);
354                   if ((1 == i) or (1 == j) or (bubbleRows == i) or (bubbleCols == j)) then
355                      bubbles[i][j]:color(0, 255, 0, 200);
356                   else
357                      bubbles[i][j]:color(math.random(200) + 55, 0, math.random(255) + 55, 200);
358                   end
359                end
360             end
361          end
362       \}
363    \}
364 
365    group \{
366       name: "plain/edje/group";
367       parts \{
368          part \{
369             name: "background";
370             type: RECT;
371             mouse\_events: 0;
372             description \{
373                state: "default" 0.0;
374                color: 0 0 0 255;
375             \}
376          \}
377 
378          // A lua group embedded in an edje group.
379          part \{
380             name: "bubbles\_lua";
381             type: GROUP;
382             source: "bubbles/lua";
383             mouse\_events: 0;
384             description \{ state: "default" 0.0; \}
385          \}
386 
387          part \{
388             name: "background\_image";
389             type: IMAGE;
390             mouse\_events: 0;
391             description \{
392                state: "default" 0.0;
393                aspect\_preference: HORIZONTAL;
394                color\_class: "test\_colour";
395                image \{ normal: "test.png"; \}
396             \}
397          \}
398 
399          part \{
400             name: "some\_text";
401             type: TEXT;
402             mouse\_events: 0;
403             description \{
404                state: "default" 0;
405                text
406                \{
407                   text: "This is test text.";
408                   text\_class: "test\_text\_class";
409                \}
410             \}
411          \}
412 
413       \}
414    \}
415 
416 \}
417 
\end{DoxyCodeInclude}
 