lua_script.edc

This example shows the usage of lua scripting to create and animate some objects in the canvas.

color_classes {
color_class { name: "test_colour"; color: 255 255 255 255; }
}
fonts {
font: "Vera.ttf" "default";
}
images {
image: "bubble.png" COMP;
image: "test.png" COMP;
}
collections {
group {
name: "main";
lua_script_only: 1;
lua_script {
--// stick object private/local vars here
local D;
local count = 0;
local fndata = 99;
local text_geom;
--// Functions to print tables.
local print_table, print_table_start;
function print_table_start(table, space, name)
print(space .. name .. ": ");
print(space .. "{");
print_table(table, space .. " ");
print(space .. "}");
end
function print_table(table, space)
for k, v in pairs(table) do
if type(v) == "table" then
print_table_start(v, space, k);
elseif type(v) == "string" then
print(space .. k .. ': "' .. v .. '";')
else
print(space .. k .. ": " .. v .. ";")
end
end
end
local function mycb3 (v)
print("lua::callback transition " .. D.val .. " v: " .. v);
d = {};
d = edje.size(d);
print("lua::objsize= " .. d.w .. " , " .. d.h);
sz = {w=v * 80, h=v * 40};
D.rect:geom(((d.w / 2) * math.sin(v * 2 * math.pi)) + ((d.w - sz.w) / 2),
((d.h / 2) * math.cos(v * 2 * math.pi)) + ((d.h - sz.h) / 2),
sz.w, sz.h);
D.rect:color(255, 128, v * 255, 255);
d = D.rect:move(d);
print("lua::pos= " .. d.x .. " , " .. d.y);
r = D.rect:above();
if (r ~= nil) then
print("lua::rcol");
r:color(20, v * 255, 60, 255);
else
print("lua::r none!!!!!!!!!!!!!!1");
end
d = edje.size();
D.clip:geom(10, 10, d.w - 20, d.h - 20);
c = D.clip:clipees();
for i=1,#c,1 do
d = c[i]:geom();
print("lua::" .. i .. " geom = " .. d.x .. "," .. d.y .. " " .. d.w .. "x" .. d.h);
end
return true; --// repeat the timer
end
local function mycb2 ()
print("lua::callback animator " .. count .. " seconds: " .. edje.seconds() .. " looptime: " .. edje.looptime());
edje.color_class("test_colour", 255, (count * 10) % 255, 255, 255);
edje.text_class("test_text_class", "Sans:style=Bold", ((count * 3) % 100) + 8);
if (5 > (count % 10)) then
D.text:font("default", 32);
else
D.text:font("Sans:style=Bold", 32);
end
edje_geom = edje.geom();
text_geom = D.text:geom();
D.text:move((edje_geom.w - text_geom.w) / 2, (edje_geom.h - text_geom.h) / 8);
return true; --// repeat the timer
end
local function mycb ()
print("lua::callback timer " .. count .. " fndata = " .. fndata);
count = count + 1; --// keep count of calls - object data
fndata = fndata + 3; --// play with object vars to see if they persist
D.tim = edje.timer(0.25, mycb); --// inside cb add new timer
return false; --// cease repeating the timer
end
--// init object here
D = {}; --// data is empty table to start
D.val = math.random(); --// start with some random value so
fndata = fndata + D.val; --// func data start point
print("lua::init ... " .. D.val);
edje.echo("lua::echo('hello world')");
--// How to check the edje version.
version = edje.version();
print("The edje version number is " .. version.major .. "." .. version.minor);
--// actually add the timer to call mycb in 1.23 sec
D.tim = edje.timer(1.23, mycb);
D.tra = edje.transition(5.0, mycb3);
D.ani = edje.animator(mycb2);
edje_geom = edje.geom();
if (edje.spanky) then edje.spanky(); end
local date = edje.date();
print("lua:: date: " ..
date.year .. "|" ..
date.month .. "|" ..
date.day .. "|" ..
date.yearday .. "|" ..
date.weekday .. "|" ..
date.hour .. "|" ..
date.min .. "|" ..
date.sec
);
--// send some random edje message
edje.messagesend(7, "none" );
edje.messagesend(7, "sig", "signal", "source");
edje.messagesend(7, "str", "hello world");
edje.messagesend(7, "int", 987);
edje.messagesend(7, "float", 987.321);
edje.messagesend(7, "strset", {"hello", "there", "world"});
edje.messagesend(7, "intset", {1, 2, 3});
edje.messagesend(7, "floatset", {1.1, 2.2, 3.3});
edje.messagesend(7, "strint", "hello world", 7);
edje.messagesend(7, "strfloat", "hello world", 7.654);
edje.messagesend(7, "strintset","hello world", {1, 2, 3});
D.edje = edje.edje();
D.edje:file("plain/edje/group");
D.edje:show();
D.rect = edje.rect();
D.rect:geom (5, 10, 50, 30);
D.rect:color (255, 128, 60, 255);
D.rect:show ();
D.rect2 = edje.rect();
D.rect2:geom (50, 50, 50, 50);
D.rect2:color (20, 30, 60, 120);
D.rect2:show ();
D.clip = edje.rect();
D.clip:geom (10, 10, 150, 150);
D.clip:color (200, 200, 50, 200);
D.clip:show ();
D.rect2:clip(D.clip);
D.rect:clip(D.clip);
D.text = edje.text();
D.text:geom (50, 5, 150, 50);
D.text:color (255, 0, 0, 255);
D.text:font("Sans:style=Bold", 32);
D.text:text("Lua rocks!");
text_geom = D.text:geom();
print(D.text:text());
D.text:show();
--// Put a few bogus API calls here to test the bogus API protection,
--// If the bogus API protection works, these should get ignored, but everything else runs smoothly.
--// Otherwise, the map is not done, the bubbles are not done, but the timers keep runinng.
bogus.failme(1, "two", D.rect);
temp = bogus.failme2();
D.text.bogus();
edje.bogus2();
--// Fun with maps!
D.map = edje.map(4); --// 4 is the only supported map size at the moment.
--// These all do the same thing.
--// Note, lua likes to start at 1, C (and thus evas) at 0. I choose to agree with C.
D.map:coord(0, 50, 50, 0);
D.map:coord(1, 100, 50, 0);
D.map:coord(2, 100, 100, 0);
D.map:coord(3, 50, 100, 0);
D.map:populate(50, 50, 50, 50, 0);
D.map:populate(D.rect2, 0);
D.map:populate(D.rect2);
--// print the results
D.coord = D.map:coord(0);
print("lua::map coords for point 0 x=" .. D.coord.x .. " y=" .. D.coord.y .. " z=" .. D.coord.z);
D.coord = D.map:coord(1);
print("lua::map coords for point 1 x=" .. D.coord.x .. " y=" .. D.coord.y .. " z=" .. D.coord.z);
D.coord = D.map:coord(2);
print("lua::map coords for point 2 x=" .. D.coord.x .. " y=" .. D.coord.y .. " z=" .. D.coord.z);
D.coord = D.map:coord(3);
print("lua::map coords for point 3 x=" .. D.coord.x .. " y=" .. D.coord.y .. " z=" .. D.coord.z);
D.map:smooth(false);
D.map:alpha(true);
if (D.map:alpha()) then
print("lua::map is alpha");
end
if (D.map:smooth()) then
print("lua::map is smooooth");
end
if (D.map:clockwise()) then
print("lua::map is clockwise");
end
D.map:color(255, 255, 255, 255); // set all points to this colour.
D.map:color(1, 255, 0, 255, 255); // set just one point to this colour.
D.map:lighting(75, 75, 10, 255, 255, 255, 0, 255, 0); // Ambient light and a 3D light source.
--// Toss it around.
D.map:rotate(45.0, 75, 75);
D.map:zoom(1.5, 1.5, 75, 75);
D.map:rotate3d(10.0, 20.0, 30.0, 75, 75, 0);
D.map:perspective(200, 200, 0, 20);
--// For image UV mapping.
D.map:uv(0, 0.0, 0.0);
D.map:uv(1, 50.0, 0.0);
D.map:uv(2, 50.0, 50.0);
D.map:uv(3, 0.0, 50.0);
--// Actually apply the resulting transformations.
D.rect2:map(D.map);
D.rect2:map_enable(true);
if (D.rect2:map_enable()) then
print("lua::map enabled");
end
D.rect2:map_source(D.rect); --// Don't think this is actually implemented in evas.
--// D.map:dup();
--// D.map:size(); --// perhaps overide the # operator? For now it's only gonna return 4 anyway.
--// example of deleting something
--// D.tim:del();
--// test the color_class stuff
colour = edje.color_class("test_colour");
print("lua::color_class= " .. colour.r .. "," .. colour.g .. "," .. colour.b .. "," .. colour.a);
colour = edje.color_class("test_colour", 32, 64, 255, 128);
print("lua::color_class= " .. colour.r .. "," .. colour.g .. "," .. colour.b .. "," .. colour.a);
colour = edje.color_class("test_colour", { r=255, g=0, b=255, a=255 });
print("lua::color_class= " .. colour.r .. "," .. colour.g .. "," .. colour.b .. "," .. colour.a);
text = edje.text_class("test_text_class", "Sans:style=Bold", 8);
print("lua::text_class= " .. text.font .. " size " .. text.size);
--// Do something bad, just to see what happens.
--// edje.color_class(nil);
--// shutdown func - generally empty or not there. everything garbage collected for you
function shutdown ()
print("lua::shutdown ... " .. D.val);
end
function show ()
print("lua::show ... " .. D.val);
end
function hide ()
print("lua::hide ... " .. D.val);
end
function move (x, y)
print("lua::move x=" .. x .. " x=" .. y);
D.edje:move(0, 0);
end
function resize (w, h)
print("lua::resize w=" .. w .. " h=" .. h);
D.text:move((w - text_geom.w) / 2, (h - text_geom.h) / 8);
D.edje:resize(w, h);
end
function message (id, type, ...)
print("lua::message id=" .. id .. " type=" .. type);
--// handle your message type here. check id + type then use the
--// vararg appropriately. they are the same as the params passed
--// to edje:messagesend() (if any are passed at all). Any array
--// arguments are passed as a single table.
if ("none" == type) then
print("lua::message no args");
elseif ("strset" == type) then
strs = ... ;
print_table_start(strs, "", "lua::message strings");
elseif ("intset" == type) then
ints = ... ;
print_table_start(ints, "", "lua::message ints");
elseif ("floatset" == type) then
floats = ... ;
print_table_start(floats, "", "lua::message floats");
elseif ("strintset" == type) then
str, ints = ... ;
print("lua::message " .. str);
print_table_start(ints, "", "lua::message ints");
elseif ("strfloatset" == type) then
str, floats = ... ;
print("lua::message " .. str);
print_table_start(floats, "", "lua::message floats");
else
print("lua::message " .. ... );
end
end
function signal (sig, src)
print("lua::signal sig= " .. sig .. " src= " .. src);
end
}
}
// The group name NEEDS a / in it,
// or the part below that tries to swallow it won't work.
// Leaving just the lua part visible.
group {
name: "bubbles/lua";
lua_script_only: 1;
lua_script {
local bubbles = { };
local bubbleCols = 8;
local bubbleRows = 6;
for i = 1, bubbleRows do
row = { };
for j = 1, bubbleCols do
image = edje.image();
image:image("bubble.png");
image:show();
table.insert(row, image);
end
table.insert(bubbles, row);
end
function resize (w, h)
for i = 1, bubbleRows do
for j = 1, bubbleCols do
w1 = w / bubbleCols;
h1 = h / bubbleRows;
bubbles[i][j]:geom((j - 1) * w1, (i - 1) * h1, w1, h1);
if ((1 == i) or (1 == j) or (bubbleRows == i) or (bubbleCols == j)) then
bubbles[i][j]:color(0, 255, 0, 200);
else
bubbles[i][j]:color(math.random(200) + 55, 0, math.random(255) + 55, 200);
end
end
end
end
}
}
group {
name: "plain/edje/group";
parts {
part {
name: "background";
type: RECT;
mouse_events: 0;
description {
state: "default" 0.0;
color: 0 0 0 255;
}
}
// A lua group embedded in an edje group.
part {
name: "bubbles_lua";
type: GROUP;
source: "bubbles/lua";
mouse_events: 0;
description { state: "default" 0.0; }
}
part {
name: "background_image";
type: IMAGE;
mouse_events: 0;
description {
state: "default" 0.0;
aspect_preference: HORIZONTAL;
color_class: "test_colour";
image { normal: "test.png"; }
}
}
part {
name: "some_text";
type: TEXT;
mouse_events: 0;
description {
state: "default" 0;
text
{
text: "This is test text.";
text_class: "test_text_class";
}
}
}
}
}
}