Lua: work-in-progress library (made in Lua) for object-oriented GUI. Already implements windows, buttons, and dynamic labels.

git-svn-id: svn://pulkomandy.tk/GrafX2/trunk@2055 416bcca6-2ee7-4201-b75f-2eb2f807beb1
This commit is contained in:
Yves Rizoud 2013-02-08 01:34:03 +00:00
parent e499aa4767
commit ea72bfe13f
2 changed files with 144 additions and 0 deletions

View File

@ -0,0 +1,33 @@
--
-- test of GUI library
--
run("libs/gui.lua")
local counter = gui.label{x=10, y=54, value=0, format="% .3d"}
local form = gui.dialog{
title="Dialogtest",
w=100,
h=150,
counter,
gui.button{ label="+",
x=6, y=38, w=14, h=14, repeatable=true, click=function()
counter.value=counter.value+1;
counter:render();
end},
gui.button{ label="-",
x=26, y=38, w=14, h=14, repeatable=true, click=function()
counter.value=counter.value-1;
counter:render();
end},
gui.button{ label="Help",
x=6, y=70, w=54, h=14, click=function()
messagebox("Help screen");
end},
gui.button{ label="Close",
x=6, y=18, w=54, h=14, key=27, click=function()
return true; -- causes closing
end},
}
form:run()

View File

@ -0,0 +1,111 @@
--
-- Event-driven GUI library
--
--
gui = {
--
-- dialog() --
--
dialog = function(args)
local dia = {
title = args.title,
w = args.w,
h = args.h,
--
widgets = {},
-- an indexed array, starting at 1. Used for calling the relevant
-- callback when a numbered control is clicked.
callbacks = {},
--
-- dialog.run() --
--
run = function(this)
windowopen(this.w,this.h, this.title or "");
-- examine all elements
for _,widget in ipairs(this.widgets) do
widget:render()
end
repeat
local button, button2, key = windowdodialog();
if button > 0 then
local c = this.callbacks[button]
-- run the callback and stop the form if it returns true
if c ~= nil and c(this) then
break;
end
end
until key == 27;
windowclose();
end
}
local id = 1;
-- examine all elements
for _,value in ipairs(args) do
-- all arguments that are tables are assumed to be widgets
if type(value)=="table" then
table.insert(dia.widgets, value)
-- clickable widgets take up an auto-numbered id
if (value.click) then
dia.callbacks[id] = value.click
id=id+1
end
end
end
return dia;
end,
--
-- button() --
--
button = function(args)
local but = {
x = args.x,
y = args.y,
w = args.w,
h = args.h,
key = args.key,
label = args.label,
click = args.click or donothing,
render = args.repeatable and function(this)
windowrepeatbutton(this.x, this.y, this.w, this.h, this.label, this.key or -1);
end
or function(this)
windowbutton(this.x, this.y, this.w, this.h, this.label, this.key or -1);
end
}
return but;
end,
--
-- label() --
--
label = function(args)
local fld = {
x = args.x,
y = args.y,
value = args.value,
format = args.format,
fg = args.fg or 0,
bg = args.bg or 2,
render = function(this)
if type(this.format) then
windowprint(this.x, this.y, string.format(this.format, this.value), this.fg, this.bg);
else
windowprint(this.x, this.y, this.value, this.fg, this.bg);
end
end
}
return fld;
end,
-- "do nothing" function. Used as default callback
donothing = function(this)
end
}