summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPixel <pixel@nobis-crew.org>2009-12-23 17:17:07 +0100
committerPixel <pixel@nobis-crew.org>2009-12-23 17:17:07 +0100
commitc36c9d0a335f9dffd1baddced837557b4a669a79 (patch)
treed7cac6e401d1678ee9c5621ad6a8f8954bed928b
parentf045d8db17b4c7c2ecfec2ef24039fdb350bab07 (diff)
A few bugfixes, and adding the template system.
-rw-r--r--dalos-luafilter.lua25
-rw-r--r--dalos.lua70
2 files changed, 85 insertions, 10 deletions
diff --git a/dalos-luafilter.lua b/dalos-luafilter.lua
index 8181442..0abf40a 100644
--- a/dalos-luafilter.lua
+++ b/dalos-luafilter.lua
@@ -32,11 +32,9 @@ end
if not metatable then metatable = {} end
local oldni, oldi = metatable.__newindex, metatable.__index
metatable.__newindex = function (table, key, value)
--- print("Setting _G[" .. key .. "] = " .. tostring(value))
localenv[key] = value
end
metatable.__index = function (table, key)
--- print("Getting _G[" .. key .. "]")
local l = localenv[key]
if l then return localenv[key] end
return rawget(_G, key)
@@ -87,12 +85,32 @@ end
self:run_in_localenv("input_change", ind)
end,
+ load_template = function (self)
+ local dlg = iup.filedlg {
+ dialogtype = "Open",
+ filter = "*.dtpl",
+ }
+ iup.Popup(dlg)
+ if dlg.status == -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)
+ if not f then error("Syntax error loading file " .. dlg.value) end
+ local data, otype, tname = f()
+ if otype ~= "Lua Filter" then error("Wrong template type: " .. otype) end
+
+ self.extra.code = data.template.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{ code = self.extra.code } 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, iup.fill{}, bcancel, normalizesize = "Horizontal" } }, title = "Code for " .. self.name, size = "600x300" }
+ 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 r = dlg:popup()
-- if r ~= iup.NOERROR then return end
local newcode = text.value
@@ -128,6 +146,7 @@ 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:load_code(extra.code)
return obj
diff --git a/dalos.lua b/dalos.lua
index c966436..cfe96c0 100644
--- a/dalos.lua
+++ b/dalos.lua
@@ -41,8 +41,8 @@ function dalos:register_obj(name, constructor, category)
end
table.insert(self.objectstypes, { name = name, constructor = constructor, counter = 1, category = category })
self.objectstypes_by_name[name] = #self.objectstypes
- if self.activemenu then
- self.activemenu:update_objects()
+ if self.active_menu then
+ self.active_menu:update_objects()
end
end
@@ -523,6 +523,7 @@ dalosp.menu = {
action_load = function (self)
local dlg = iup.filedlg {
dialogtype = "Open",
+ filter = "*.dalos"
}
iup.Popup(dlg)
if dlg.status == -1 then return end
@@ -598,6 +599,7 @@ dalosp.menu = {
local save = { objects = s_obj, links = s_links, cross = s_cross, imports = dalosp.imports }
local dlg = iup.filedlg {
dialogtype = "Save",
+ dalos = "*.dalos",
}
iup.Popup(dlg)
if dlg.status ~= -1 then
@@ -608,6 +610,8 @@ dalosp.menu = {
v:write "if dalos.version.MAJOR < version.MAJOR or dalos.version.MAJOR == version.MAJOR and dalos.version.MINOR < version.MINOR then error 'Dalos version too old for this save.' end\n\nlocal "
dumpvars(v, save, "save")
v:write "return save"
+ else
+ error("Failed opening " .. dlg.value .. " for writing")
end
end
@@ -627,10 +631,12 @@ dalosp.menu = {
action_import = function (self)
local dlg = iup.filedlg {
dialogtype = "Open",
+ filter = "*.lua",
}
iup.Popup(dlg)
if dlg.status == -1 then return end
+ local d = dalos.active_canvas
dalosp.menu.load_file(dlg.value)
d:setstatus(dalos.stypes.INFO, "Properly imported")
end,
@@ -644,6 +650,30 @@ dalosp.menu = {
d:setstatus(dalos.stypes.INFO, "Properly reloaded all imported files")
end,
+ action_load_template = function (self)
+ local dlg = iup.filedlg {
+ dialogtype = "Open",
+ filter = "*.dtpl",
+ }
+ iup.Popup(dlg)
+ if dlg.status == -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)
+ if not f then error("Syntax error loading file " .. dlg.value) end
+ local data, otype, tname = f()
+
+ local tobj = dalos.objectstypes_by_name[otype]
+ if not tobj then error("Unknown template object type: " .. otype) end
+ tobj = dalos.objectstypes[tobj]
+
+ dalos:register_obj(otype .. "::" .. tname, function(d, tab) tobj.constructor(d, tab, data) end, "Template")
+ local d = dalos.active_canvas
+
+ d:setstatus(dalos.stypes.INFO, "Template properly loaded")
+ end,
+
action_exit = function (self)
return iup.CLOSE
end,
@@ -661,10 +691,11 @@ dalosp.menu = {
end,
update_objects = function (self)
- if dalos.dlg then
- local newmenu = dalos.menu {}
+ local d = dalos.active_canvas
+ if d and d.dialog then
+ local newmenu = dalos.menu(dalos.active_canvas)
-- copy anything from self to newmenu ? *shrug*
- dalos.dlg.menu = newmenu
+ d.dialog.menu = newmenu
end
end,
@@ -696,6 +727,8 @@ dalosp.menu = {
item_import.action = dalosp.menu.action_import
local item_reload = iup.item { title = "Reload all" }
item_reload.action = dalosp.menu.action_reload
+ local item_load_template = iup.item { title = "Load Template" }
+ item_load_template.action = dalosp.menu.action_load_template
local item_exit = iup.item { title = "Exit" }
item_exit.action = dalosp.menu.action_exit
local item_about = iup.item { title = "About" }
@@ -760,7 +793,7 @@ dalosp.menu = {
table.insert(east_menu, 1, iup.submenu { iup.menu(east_submenus[v]), title = v })
table.insert(west_menu, 1, iup.submenu { iup.menu(west_submenus[v]), title = v })
end
- local menu_file = iup.submenu { iup.menu { item_new, item_load, item_save, iup.separator {}, item_import, item_reload, iup.separator {}, item_exit }, title = "File" }
+ local menu_file = iup.submenu { iup.menu { item_new, item_load, item_save, iup.separator {}, item_import, item_reload, iup.separator {}, item_load_template, iup.separator {}, item_exit }, title = "File" }
local menu_add = iup.submenu { iup.menu(add_menu), title = "Add" }
local menu_cross = iup.submenu { iup.menu {
iup.submenu { iup.menu(north_menu), title = "North" },
@@ -861,6 +894,28 @@ dalosp.object = {
return {}
end,
+ save_template = function (self, template)
+ local dlg = iup.filedlg {
+ dialogtype = "Save",
+ filter = "*.dtpl",
+ }
+ iup.Popup(dlg)
+ if dlg.status == -1 then return end
+ local s, name, v
+ s, name = iup.GetParam("Export template", nil, "Template name: %s\n", "")
+ if not s then return end
+ s, v = pcall(Output, dlg.value)
+ if s then
+ v:write "---- Dalos template\nlocal "
+ dumpvars(v, dalos.version, "version")
+ v:write "if dalos.version.MAJOR < version.MAJOR or dalos.version.MAJOR == version.MAJOR and dalos.version.MINOR < version.MINOR then error 'Dalos version too old for this save.' end\n\nlocal "
+ dumpvars(v, template, "template")
+ v:write("return template, '" .. self.ntype .. "', '" .. name .. "'")
+ else
+ error("Failed opening " .. dlg.value .. " for writing")
+ end
+ end,
+
create = function (dcanvas, tab, extra)
if not tab then tab = {} end
if not tab.name then
@@ -895,6 +950,7 @@ dalosp.object = {
get_settings = tab.get_settings or dalosp.object.default_get_settings,
houtputs = {},
get_linked_input = dalosp.object.get_linked_input,
+ save_template = dalosp.object.save_template,
dcanvas = dcanvas,
}
@@ -980,7 +1036,7 @@ function dalos:main()
dlg = iup.dialog { iup.vbox { d, s }, title = "Dalos", menu = m, size = "400x250" }
- self.dialog = dlg
+ d.dialog = dlg
d:setstatus(dalos.stypes.INFO, "Dalos version " .. dalos.version.string .. " started")