summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPixel <pixel@nobis-crew.org>2009-12-22 08:41:00 +0100
committerPixel <pixel@nobis-crew.org>2009-12-22 08:41:00 +0100
commit1a74fe712bb4d1c3f968e943b2f8de89420a6a3f (patch)
treedb65b18118695e61fb822504f3eba168472ca3cb
parentf65c63f2db769bbc39e7941b3e1572b69e544971 (diff)
Adding luafilter!
-rw-r--r--dalos-luafilter.lua138
-rw-r--r--dalos.lua62
2 files changed, 198 insertions, 2 deletions
diff --git a/dalos-luafilter.lua b/dalos-luafilter.lua
new file mode 100644
index 0000000..b267142
--- /dev/null
+++ b/dalos-luafilter.lua
@@ -0,0 +1,138 @@
+dalosp.luafilter = {
+ default_code = [[
+-- available globals:
+
+-- ninputs, noutputs: numbers
+
+-- get_input(ind): handle
+-- del_output(ind): nil
+-- new_output(ind): nil
+-- set_color(c) : nil
+
+function activate()
+end
+
+function read(ind, count, userdata, offset)
+end
+
+function seek(ind, offset)
+end
+
+function input_change(ind)
+end
+]],
+
+ get_settings = function (self)
+ return { ninputs = self.ninputs, noutputs = self.noutputs, code = self.extra.code }
+ end,
+
+ run_in_localenv = function (self, f, ...)
+ local localenv = self.extra.localenv
+ local metatable = getmetatable(_G)
+ 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)
+ end
+ setmetatable(_G, metatable)
+
+ if type(f) ~= "function" then f = localenv[f] end
+ local rets = { true }
+ if f then rets = { pcall(f, ...) } end
+
+ metatable.__newindex, metatable.__index = oldni, oldi
+ setmetatable(_G, metatable)
+
+ if not rets[1] then error(rets[2]) end
+ table.remove(rets, 1)
+ return unpack(rets)
+ end,
+
+ load_code = function (self, code)
+ self.extra.localenv = {
+ ninputs = self.ninputs + 0,
+ noutputs = self.noutputs + 0,
+ get_input = function(ind) return self:get_linked_input(ind) end,
+ del_output = function(ind) self:set_houtput(nil, ind) end,
+ new_output = function(ind, size, name)
+ self:set_houtput(dalos.luahandle{
+ size = size,
+ getname = function ()
+ return name
+ end,
+ do_read = function (lh, count, userdata)
+ return self:run_in_localenv("read", ind, count, userdata, lh.offset)
+ end,
+ do_seek = function (lh)
+ return self:run_in_localenv("seek", ind, lh.offset)
+ end,
+ }, ind)
+ end,
+ set_color = function(c) self.color = c self:draw() end,
+ }
+ if code and code ~= "" then
+ local f = loadstring(code)
+ if f then self:run_in_localenv(f) end
+ end
+ end,
+
+ input_change = function (self, ind)
+ self:run_in_localenv("input_change", ind)
+ 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 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 r = dlg:popup()
+-- if r ~= iup.NOERROR then return end
+ local newcode = text.value
+ if newcode and okay then
+ self.extra.code = newcode
+ self:load_code(newcode)
+ end
+ end,
+
+ activate = function (self)
+ self:run_in_localenv "activate"
+ end,
+
+ create = function (d, tab, settings)
+ tab.ninputs = settings and settings.ninputs
+ tab.noutputs = settings and settings.noutputs
+ tab.otype = dalos.objtype.LUA_FILTER
+ tab.configure = dalosp.luafilter.configure
+ tab.activate = dalosp.luafilter.activate
+ tab.input_change = dalosp.luafilter.input_change
+ tab.default_name = "Lua Filter"
+ tab.get_settings = dalosp.luafilter.get_settings
+ tab.ntype = "Lua Filter"
+ local extra = { localenv = {} }
+ extra.code = settings and settings.code
+ if not extra.code or extra.code == "" then extra.code = dalosp.luafilter.default_code end
+ local s = true
+ while not s and not tab.ninputs or not tab.noutputs do
+ s, tab.ninputs, tab.noutputs = iup.GetParam("Lua Filter", nil, "Inputs number: %i\nOutputs number: %i\n", 1, 1)
+ end
+
+ local obj = dalos.object(d, tab, extra)
+
+ obj.load_code = dalosp.luafilter.load_code
+ obj.run_in_localenv = dalosp.luafilter.run_in_localenv
+ obj:load_code(extra.code)
+
+ return obj
+ end,
+}
+
+dalos.luafilter = dalosp.luafilter.create
+dalos:register_obj("Lua Filter", dalos.luafilter)
diff --git a/dalos.lua b/dalos.lua
index 4b2eb80..d8169e5 100644
--- a/dalos.lua
+++ b/dalos.lua
@@ -2,6 +2,22 @@ loadmodule "luaiup"
loadmodule "luahandle"
loadmodule "lualibs"
+--[[
+load "indent.lua"
+
+function testIndenter(i)
+ local lib = IndentationLib
+ local str = ""
+ local inp = Input "dalos.lua"
+ str = inp:readfile()
+
+ local colorTable = lib.defaultColorTable
+ print(lib.indentCode(str, 4, colorTable, i))
+end
+
+testIndenter()
+]]--
+
load "iupe-dbuffer.lua"
load "iupe-tview.lua"
@@ -167,6 +183,18 @@ dalosp.canvas = {
self:draw()
end,
+ delobj = function (self, ind)
+ local obj = self.objects[ind]
+ for i = 1, obj.obj.noutputs do
+ self:destroylink(obj, i)
+ end
+ for i = 1, obj.obj.ninputs do
+ self:destroylink_dst(obj, i)
+ end
+ table.remove(self.objects, ind)
+ self:draw()
+ end,
+
findobj = function (self, x, y)
local obj, ind
@@ -213,6 +241,7 @@ dalosp.canvas = {
end,
motion_cb = function (self, x, y, status)
+ self.stateful.mousepos = { x = x, y = y }
if self.stateful.panning then
local ox, oy = self.ox, self.oy
local dx, dy = x - ox, y - oy
@@ -288,6 +317,18 @@ dalosp.canvas = {
end
end,
+ destroylink_dst = function (self, dst, ind)
+ if not ind then ind = dst.obj.curinput end
+ local olddst = dst.obj.inputs[ind]
+ if olddst then
+ dst.obj.inputs[ind] = nil
+ dst.obj:input_change(ind)
+
+ olddst.obj.obj.outputs[olddst.ind] = nil
+ olddst.obj.obj:output_change(olddst.ind)
+ end
+ end,
+
button_cb = function (self, button, pressed, x, y, status)
if not pressed then pressed = 0 end
if not x then x = self.ox end
@@ -374,7 +415,7 @@ dalosp.canvas = {
self.menu.y = nil
self:draw()
elseif self.stateful.moving then
- -- check for the trash can
+ -- dropped somewhere... do something useful here ?
end
self.stateful.panning = nil
self.stateful.moving = nil
@@ -403,6 +444,19 @@ dalosp.canvas = {
self:draw()
end,
+ keypress_cb = function (self, c, press)
+ if press ~= 1 then return end
+ local obj, ind = self:findobj(self.stateful.mousepos.x, self.stateful.mousepos.y)
+ if c == iup.K_cS then
+ if obj then self:delobj(ind) end
+ elseif c == iup.K_n then
+ if not obj then return end
+ local s, newname = iup.GetParam("Renaming", nil, "Set name: %s\n", obj.obj.name)
+ if s and newname then obj.obj.name = newname end
+ self:draw()
+ end
+ end,
+
create = function (tab)
tab.border = "No"
tab.expand = "Yes"
@@ -419,7 +473,9 @@ dalosp.canvas = {
r.motion_cb = dalosp.canvas.motion_cb
r.button_cb = dalosp.canvas.button_cb
r.wheel_cb = dalosp.canvas.wheel_cb
+ r.keypress_cb = dalosp.canvas.keypress_cb
r.addobj = dalosp.canvas.addobj
+ r.delobj = dalosp.canvas.delobj
r.findobj = dalosp.canvas.findobj
r.moveobj = dalosp.canvas.moveobj
r.setobjplace = dalosp.canvas.setobjplace
@@ -427,6 +483,7 @@ dalosp.canvas = {
r.setstatus = dalosp.canvas.setstatus
r.createlink = dalosp.canvas.createlink
r.destroylink = dalosp.canvas.destroylink
+ r.destroylink_dst = dalosp.canvas.destroylink_dst
r.drawlink = dalosp.canvas.drawlink
r.drawxmenu = dalosp.canvas.drawxmenu
r.origin = { x = 0, y = 0 }
@@ -817,6 +874,7 @@ load "dalos-textbuffer.lua"
load "dalos-input.lua"
load "dalos-tee.lua"
load "dalos-buffer.lua"
+load "dalos-luafilter.lua"
----------------
@@ -833,4 +891,4 @@ function dalos:main()
dlg:hide()
end
-dalos:main()
+if not archive_main then dalos:main() end