Lua: gui library supports textboxes, for numbers or strings.
git-svn-id: svn://pulkomandy.tk/GrafX2/trunk@2056 416bcca6-2ee7-4201-b75f-2eb2f807beb1
This commit is contained in:
@@ -12,12 +12,10 @@ local form = gui.dialog{
|
||||
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()
|
||||
@@ -27,6 +25,23 @@ local form = gui.dialog{
|
||||
x=6, y=18, w=54, h=14, key=27, click=function()
|
||||
return true; -- causes closing
|
||||
end},
|
||||
gui.textbox{
|
||||
x=6, y=90, nbchar=8, decimal=1,
|
||||
min=450, max=1450, maxchar=8, value = 1234,
|
||||
change=function()
|
||||
-- do nothing
|
||||
end
|
||||
},
|
||||
gui.textbox{
|
||||
x=6, y=104, nbchar=10, maxchar=20, value = "test"
|
||||
},
|
||||
gui.textbox{
|
||||
x=6, y=118, nbchar=8, decimal=0, min=0,
|
||||
maxchar=8, value = 456,
|
||||
change=function()
|
||||
-- do nothing
|
||||
end
|
||||
},
|
||||
}
|
||||
|
||||
form:run()
|
||||
|
||||
@@ -6,7 +6,40 @@
|
||||
gui = {
|
||||
|
||||
--
|
||||
-- dialog() --
|
||||
-- colors
|
||||
--
|
||||
black = 0,
|
||||
dark = 1,
|
||||
light = 2,
|
||||
white = 3,
|
||||
|
||||
-- "do nothing" function. Used as default callback
|
||||
donothing = function(self)
|
||||
end,
|
||||
|
||||
-- Metatable that hides the field "value" behind a property,
|
||||
-- and calls render() after it's set.
|
||||
propvalue = {
|
||||
__newindex = function(self, index, value)
|
||||
if index == "value" then
|
||||
self._value = value
|
||||
-- extra processing
|
||||
self:render()
|
||||
else
|
||||
rawset(self, index, value)
|
||||
end
|
||||
end,
|
||||
__index = function(self, index )
|
||||
if index == "value" then
|
||||
return self._value
|
||||
else
|
||||
return rawget( self, index )
|
||||
end
|
||||
end
|
||||
},
|
||||
|
||||
--
|
||||
-- dialog()
|
||||
--
|
||||
dialog = function(args)
|
||||
local dia = {
|
||||
@@ -22,21 +55,26 @@ dialog = function(args)
|
||||
--
|
||||
-- dialog.run() --
|
||||
--
|
||||
run = function(this)
|
||||
windowopen(this.w,this.h, this.title or "");
|
||||
run = function(self)
|
||||
windowopen(self.w,self.h, self.title or "");
|
||||
-- examine all elements
|
||||
for _,widget in ipairs(this.widgets) do
|
||||
widget:render()
|
||||
for _,widget in ipairs(self.widgets) do
|
||||
widget:create()
|
||||
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;
|
||||
local c = self.callbacks[button]
|
||||
if c ~= nil then
|
||||
-- run the callback
|
||||
local retvalue = c:click()
|
||||
-- stop the form if it returns non-nil
|
||||
if retvalue ~= nil then
|
||||
windowclose();
|
||||
return retvalue;
|
||||
end
|
||||
end
|
||||
end
|
||||
until key == 27;
|
||||
@@ -51,7 +89,7 @@ dialog = function(args)
|
||||
table.insert(dia.widgets, value)
|
||||
-- clickable widgets take up an auto-numbered id
|
||||
if (value.click) then
|
||||
dia.callbacks[id] = value.click
|
||||
dia.callbacks[id] = value
|
||||
id=id+1
|
||||
end
|
||||
end
|
||||
@@ -60,7 +98,7 @@ dialog = function(args)
|
||||
end,
|
||||
|
||||
--
|
||||
-- button() --
|
||||
-- button()
|
||||
--
|
||||
button = function(args)
|
||||
local but = {
|
||||
@@ -70,42 +108,118 @@ button = function(args)
|
||||
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);
|
||||
click = args.click or gui.donothing,
|
||||
create = args.repeatable and function(self)
|
||||
windowrepeatbutton(self.x, self.y, self.w, self.h, self.label, self.key or -1);
|
||||
end
|
||||
or function(this)
|
||||
windowbutton(this.x, this.y, this.w, this.h, this.label, this.key or -1);
|
||||
or function(self)
|
||||
windowbutton(self.x, self.y, self.w, self.h, self.label, self.key or -1);
|
||||
end
|
||||
}
|
||||
return but;
|
||||
end,
|
||||
|
||||
--
|
||||
-- label() --
|
||||
-- label()
|
||||
--
|
||||
label = function(args)
|
||||
local fld = {
|
||||
local lbl = {
|
||||
x = args.x,
|
||||
y = args.y,
|
||||
value = args.value,
|
||||
_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);
|
||||
fg = args.fg or gui.black,
|
||||
bg = args.bg or gui.light,
|
||||
render = function(self)
|
||||
if type(self.format) then
|
||||
windowprint(self.x, self.y, string.format(self.format, self._value), self.fg, self.bg);
|
||||
else
|
||||
windowprint(this.x, this.y, this.value, this.fg, this.bg);
|
||||
windowprint(self.x, self.y, self._value, self.fg, self.bg);
|
||||
end
|
||||
end
|
||||
end,
|
||||
}
|
||||
return fld;
|
||||
lbl.create = lbl.render
|
||||
setmetatable(lbl, gui.propvalue)
|
||||
return lbl;
|
||||
end,
|
||||
|
||||
|
||||
-- "do nothing" function. Used as default callback
|
||||
donothing = function(this)
|
||||
--
|
||||
-- textbox
|
||||
--
|
||||
textbox = function(args)
|
||||
local txtbox = {
|
||||
x = args.x,
|
||||
y = args.y,
|
||||
nbchar = args.nbchar, -- visible size in characters
|
||||
--format = args.format, -- numeric, decimal, path
|
||||
decimal = args.decimal or 0,
|
||||
min = args.min,
|
||||
max = args.max,
|
||||
maxchar = args.maxchar, -- internal size
|
||||
_value = args.value,
|
||||
change = args.change or gui.donothing,
|
||||
--fg = args.fg or gui.black,
|
||||
--bg = args.bg or gui.light,
|
||||
create = function(self)
|
||||
windowinput(self.x, self.y, self.nbchar)
|
||||
self:render()
|
||||
end,
|
||||
render = function(self)
|
||||
local val = tostring(self._value)
|
||||
if string.len(val) < self.nbchar then
|
||||
val = string.rep(" ",self.nbchar - string.len(val)) .. val;
|
||||
elseif string.len(val) > self.nbchar then
|
||||
val = string.sub(val, 1, self.nbchar-1) .. gui.char.ellipsis
|
||||
end
|
||||
windowprint(self.x, self.y, val, gui.black, gui.light);
|
||||
end,
|
||||
click = function(self)
|
||||
local inputtype
|
||||
if (type(self._value) == "number" and ((self.min ~= nil and self.min<0) or self.decimal > 0)) then
|
||||
inputtype = 3 -- entry as double
|
||||
elseif (type(self._value) == "number") then
|
||||
inputtype = 1 -- entry as unsigned int
|
||||
else
|
||||
inputtype = 0 -- entry as string
|
||||
end
|
||||
local accept, val = windowreadline(self.x, self.y, self._value, self.nbchar, self.maxchar, self.decimal, inputtype);
|
||||
|
||||
if accept then
|
||||
if (inputtype == 1 or inputtype == 3) then
|
||||
val = tonumber(val)
|
||||
-- round the decimal places
|
||||
val = gui.round(val, self.decimal)
|
||||
end
|
||||
if (self.min ~= nil and val < self.min) then
|
||||
val = self.min
|
||||
end
|
||||
if (self.max ~= nil and val > self.max) then
|
||||
val = self.max
|
||||
end
|
||||
|
||||
self._value = val
|
||||
end
|
||||
self:render()
|
||||
end
|
||||
}
|
||||
setmetatable(txtbox, gui.propvalue)
|
||||
return txtbox;
|
||||
end
|
||||
|
||||
}
|
||||
|
||||
gui.round = function(val, ipt)
|
||||
local mult = 10^ipt
|
||||
return math.floor(val * mult + 0.5) / mult
|
||||
end
|
||||
|
||||
-- Character constants. May be useful in screens
|
||||
gui.char = {
|
||||
ellipsis = string.char(133), -- ...
|
||||
arrowup = string.char(24),
|
||||
arrowdown = string.char(25),
|
||||
arrowleft = string.char(27),
|
||||
arrowright = string.char(26),
|
||||
vertical = string.char(18), -- double-ended arrow
|
||||
horizontal = string.char(29) -- double-ended arrow
|
||||
}
|
||||
Reference in New Issue
Block a user