From ef0404604e36a45ea950439c2396eb0ea3b5bdf9 Mon Sep 17 00:00:00 2001 From: Pixel Date: Sat, 26 Dec 2009 20:15:19 +0100 Subject: Template system rework. --- dalos-luafilter.lua | 19 +++++++++++++++---- dalos-struct.lua | 34 ++++++++++++++++++++++++++++++++-- dalos.lua | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 96 insertions(+), 8 deletions(-) diff --git a/dalos-luafilter.lua b/dalos-luafilter.lua index 0d83fb4..9c87f93 100644 --- a/dalos-luafilter.lua +++ b/dalos-luafilter.lua @@ -1,4 +1,6 @@ dalosp.luafilter = { + templates = {}, + default_code = [[ -- available globals: @@ -101,21 +103,26 @@ end local data, otype, tname = f() if otype ~= "Lua Filter" then error("Wrong template type: " .. otype) end - self.extra.code = data.template.code + self:apply_template(data.template) end, gen_template = function (self) return { code = self.extra.close } end, + apply_template = function (self, data) + self.extra.code = data.code + end, + configure = function (self) local okay = false local text = iup.text { multiline = "Yes", font = "Courier", expand = "Yes", value = self.extra.code } local bok = iup.button { title = "Ok", action = function () okay = true return iup.CLOSE end } local bimport = iup.button { title = "Import", action = function() self:load_template() return iup.CLOSE end } local bexport = iup.button { title = "Export", action = function() self:save_template(self:gen_template()) end } + local busetpl = iup.button { title = "Use Template", action = function() self:use_template() end } local bcancel = iup.button { title = "Cancel", action = function () okay = false return iup.CLOSE end } - local dlg = iup.dialog { iup.vbox { text, iup.hbox { bok, bimport, bexport, iup.fill{}, bcancel, normalizesize = "Horizontal" } }, title = "Code for " .. self.name, size = "600x300" } + local dlg = iup.dialog { iup.vbox { text, iup.hbox { bok, bimport, bexport, busetpl, iup.fill{}, bcancel, normalizesize = "Horizontal" } }, title = "Code for " .. self.name, size = "600x300" } local r = dlg:popup() -- if r ~= iup.NOERROR then return end local newcode = text.value @@ -151,13 +158,17 @@ end obj.load_code = dalosp.luafilter.load_code obj.run_in_localenv = dalosp.luafilter.run_in_localenv - obj.load_template = dalosp.luafilter.load_template obj.gen_template = dalosp.luafilter.gen_template + obj.apply_template = dalosp.luafilter.apply_template obj:load_code(extra.code) return obj end, + + register_template = function (data, tname) + dalosp.luafilters.templates[tname] = data + end, } dalos.luafilter = dalosp.luafilter.create -dalos:register_obj("Lua Filter", dalos.luafilter, "Programmable") +dalos:register_obj("Lua Filter", dalos.luafilter, "Programmable", dalosp.luafilter.register_template) diff --git a/dalos-struct.lua b/dalos-struct.lua index 7d6b8b0..0835b7d 100644 --- a/dalos-struct.lua +++ b/dalos-struct.lua @@ -6,6 +6,8 @@ local function imLoadImage(fname) end dalosp.struct = { + templates = {}, + images = { up = imLoadImage "12-em-up.tga", down = imLoadImage "12-em-down.tga", @@ -48,6 +50,7 @@ dalosp.struct = { }, get_settings = function (self) + return { self.extra.entries } end, configure = function (self) @@ -151,6 +154,15 @@ dalosp.struct = { mx.redraw = "All" end, + gen_template = function (self) + return self:get_settings() + end, + + apply_template = function (self, data) + self.extra.entries = data.entries + self:cacheoffset() + end, + cfg_draw_cb = function (self, lin, col, x1, x2, y1, y2, cv) if col == 1 then dalosp.struct.images.plus:cdCanvasPutImageRect(cv, x1 + 3, y2 + 5, 12, 12, 0, 12, 0, 12) @@ -301,6 +313,8 @@ dalosp.struct = { obj.cacheoffset = dalosp.struct.cacheoffset obj.isunique = dalosp.struct.isunique obj.getunique = dalosp.struct.getunique + obj.gen_template = dalosp.struct.gen_template + obj.apply_template = dalosp.struct.apply_template obj:cacheoffset() @@ -350,7 +364,19 @@ dalosp.struct = { iup.fill {}, iup.button { title = "Ok", - action = function (self) return iup.CLOSE end + action = function (self) return iup.CLOSE end, + }, + iup.button { + title = "Import", + action = function() obj:load_template() return iup.CLOSE end, + }, + iup.button { + title = "Export", + action = function() obj:save_template(self:gen_template()) end, + }, + iup.button { + title = "Use Template", + action = function() obj:use_template() end, }, iup.fill {}, }, @@ -367,7 +393,11 @@ dalosp.struct = { return obj end, + + register_template = function (data, tname) + dalosp.struct.templates[tname] = data + end, } dalos.struct = dalosp.struct.create -dalos:register_obj("Struct", dalos.struct, "Programmable") +dalos:register_obj("Struct", dalos.struct, "Programmable", dalosp.struct.register_template) diff --git a/dalos.lua b/dalos.lua index c13ffd3..047a54f 100644 --- a/dalos.lua +++ b/dalos.lua @@ -35,11 +35,11 @@ dalosp.cross = { } dalos.version = { MAJOR = 0, MINOR = 1, suffix = "alpha" } dalos.version.string = dalos.version.MAJOR .. "." .. dalos.version.MINOR .. dalos.version.suffix -function dalos:register_obj(name, constructor, category) +function dalos:register_obj(name, constructor, category, registertemplate) if self.objectstypes_by_name[name] then error("An object type of that name already exists: " .. name) end - table.insert(self.objectstypes, { name = name, constructor = constructor, counter = 1, category = category }) + table.insert(self.objectstypes, { name = name, constructor = constructor, counter = 1, category = category, registertemplate = registertemplate }) self.objectstypes_by_name[name] = #self.objectstypes if self.active_menu then self.active_menu:update_objects() @@ -672,6 +672,7 @@ dalosp.menu = { tobj = dalos.objectstypes[tobj] dalos:register_obj(otype .. "::" .. tname, function(d, tab) tobj.constructor(d, tab, data) end, "Template") + if tobj.registertemplate then tobj.registertemplate(data, tname) end local d = dalos.active_canvas d:setstatus(dalos.stypes.INFO, "Template properly loaded") @@ -816,6 +817,50 @@ dalosp.menu = { } dalosp.object = { + use_template = function (self) + local templates = dalosp.luafilter.templates + local ttpllst = {} + for n, v in pairs(templates) do + table.insert(ttpllst, n) + end + if #ttpllst == 0 then + local dlg = iup.messagedlg { + dialogtype = "Warning", + title = "No template", + value = "There's no template to use.\nPlease load templates from the main menu first.", + } + dlg:popup() + return iup.DEFAULT + end + table.sort(ttpllst) + local tpllst = "|" + for i, n in ipairs(ttpllst) do + tpllst = tpllst .. n .. "|" + end + local s, tind = iup.GetParam("Use Template", nil, "Template: %i" .. tpllst .. "\n", 0) + if not s then return iup.DEFAULT end + self:apply_template(dalosp.luafilter.templates[ttpllst[tind]]) + end, + + load_template = function (self) + local dlg = iup.filedlg { + dialogtype = "Open", + filter = "*.dtpl", + } + iup.Popup(dlg) + if (dlg.status + 0) == -1 then return end + + local s, v = pcall(Input, dlg.value) + if not s then error("Problem loading file " .. dlg.value) end + local f = preload(v) + v:destroy() + if not f then error("Syntax error loading file " .. dlg.value) end + local data, otype, tname = f() + if otype ~= self.ntype then error("Wrong template type: " .. otype) end + + self:apply_template(data.template) + end, + default_draw = function (self, cv, x, y, w, h) local x1, x2, y1, y2 = x, x + w, y, y + h y1, y2 = cv:InvertYAxis(y2), cv:InvertYAxis(y1) @@ -955,6 +1000,8 @@ dalosp.object = { houtputs = {}, get_linked_input = dalosp.object.get_linked_input, save_template = dalosp.object.save_template, + load_template = dalosp.object.load_template, + use_template = dalosp.object.use_template, dcanvas = dcanvas, } -- cgit v1.2.3