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.lua | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) (limited to 'dalos.lua') 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