summaryrefslogtreecommitdiff
path: root/dalos.lua
diff options
context:
space:
mode:
authorPixel <pixel@nobis-crew.org>2009-12-15 17:18:10 -0800
committerPixel <pixel@nobis-crew.org>2009-12-15 17:18:10 -0800
commitfcaf4590ac43d559048bd92ec1e096d5d875bed8 (patch)
treea1e5746f314f858583582816528b4940a5d52039 /dalos.lua
parent6d93362ebb4def3109d194818b2ab5c0081805ad (diff)
Lot of various changes, basic concept of "linking" is present.
Diffstat (limited to 'dalos.lua')
-rw-r--r--dalos.lua165
1 files changed, 147 insertions, 18 deletions
diff --git a/dalos.lua b/dalos.lua
index 5f153bc..6594e0e 100644
--- a/dalos.lua
+++ b/dalos.lua
@@ -11,6 +11,19 @@ if not dalosp then dalosp = {} end
if not dalos then dalos = {} end
dalosp.canvas = {
+ drawlink = function (self, src, dst, si, di)
+ local cvdb = self.cvdb
+
+ cvdb:Foreground(cd.BLACK)
+ local x1, y1, x2, y2
+
+ x1 = self.origin.x + src.x + src.w
+ x2 = self.origin.x + dst.x
+ y1 = self.origin.y + src.y + si * src.h / (src.obj.noutputs + 1)
+ y2 = self.origin.y + dst.y + di * src.h / (dst.obj.ninputs + 1)
+ cvdb:Line(x1, cvdb:InvertYAxis(y1), x2, cvdb:InvertYAxis(y2))
+ end,
+
draw = function (self)
local cvdb = self.cvdb
if not cvdb then return iup.DEFAULT end
@@ -18,6 +31,21 @@ dalosp.canvas = {
for k, v in ipairs(self.objects) do
v.obj:draw(cvdb, self.origin.x + v.x, self.origin.y + v.y, v.w, v.h)
+ for oi = 1, v.obj.noutputs do
+ local dest = v.obj.outputs[oi]
+ if dest then
+ for ii = 1, dest.obj.ninputs do
+ if dest.obj.inputs[ii] == v then
+ self:drawlink(v, dest, oi, ii)
+ end
+ end
+ end
+ end
+ end
+
+ if self.stateful.linking then
+ cvdb:Foreground(cd.BLACK)
+ cvdb:Line(self.bx, cvdb:InvertYAxis(self.by), self.ox, cvdb:InvertYAxis(self.oy))
end
cvdb:Flush()
@@ -62,6 +90,17 @@ dalosp.canvas = {
end,
focus_cb = function (self, focus)
+ if focus == 0 then self:button_cb() end
+ end,
+
+ stypes = {
+ INFO = 0,
+ WARNING = 1,
+ ERROR = 2,
+ },
+
+ setstatus = function (self, stype, msg)
+ -- todo: add a status line
end,
motion_cb = function (self, x, y, status)
@@ -71,15 +110,31 @@ dalosp.canvas = {
self:panning(dx, dy)
self.ox, self.oy = x, y
elseif self.stateful.leftbutton then
- if self.stateful.leftclk and not self.stateful.linking then self.stateful.linking = self.stateful.leftclk end
local linking = self.stateful.linking
- if linking then
- -- writing linking code...
+ self.stateful.dragging = true
+ local got_error = false
+ if self.stateful.leftclk and not self.stateful.linking then
+ linking = self.stateful.leftclk
+ if linking.obj.noutputs >= 1 then
+ self.stateful.linking = linking
+ else
+ self:setstatus(dalosp.canvas.stypes.ERROR, "Can't link: origin object doesn't have any output")
+ self.stateful.leftclk = nil
+ linking = nil
+ got_error = true
+ end
else
- -- start selection square ?
+ linking = self.stateful.linking
+ end
+ if linking then
+ self.ox, self.oy = x, y
+ self:draw()
+ elseif not got_error then
+ -- todo: start selection square ?
end
elseif self.stateful.rghtbutton then
if self.stateful.rghtclk and not self.stateful.moving then self.stateful.moving = self.stateful.rghtclk end
+ self.stateful.dragging = true
local moving = self.stateful.moving
if moving then
local ox, oy = self.ox, self.oy
@@ -87,12 +142,20 @@ dalosp.canvas = {
self:moveobj(moving, dx, dy)
self.ox, self.oy = x, y
else
- -- show X menu
+ -- todo: show X menu
end
end
end,
+ createlink = function (self, src, dst)
+ src.obj.outputs[src.obj.curoutput + 0] = dst
+ dst.obj.inputs[dst.obj.curinput + 0] = src
+ 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
+ if not y then y = self.oy end
if button == iup.BUTTON1 or not button then
if pressed == 1 then
self.stateful.leftbutton = true
@@ -104,13 +167,25 @@ dalosp.canvas = {
self.bx, self.by = x, y
end
else
- if not self.stateful.linking and self.stateful.leftclk then
+ if not self.stateful.linking and self.stateful.leftclk and not self.stateful.dragging then
self.stateful.leftclk.obj:activate()
+ elseif self.stateful.linking then
+ local dest = self:findobj(x,y)
+ if dest == self.stateful.linking then
+ self:setstatus(dalosp.canvas.stypes.ERROR, "Can't link: origin is the same as destination")
+ elseif dest.obj.ninputs <= 0 then
+ self:setstatus(dalosp.canvas.stypes.ERROR, "Can't link: destination has no input")
+ else
+ self:createlink(self.stateful.linking, dest)
+ end
+ self.stateful.linking = nil
+ self:draw()
end
self.stateful.panning = nil
self.stateful.linking = nil
self.stateful.leftclk = nil
self.stateful.leftbutton = nil
+ if not self.stateful.rghtbutton then self.stateful.dragging = nil end
end
end
@@ -122,17 +197,35 @@ dalosp.canvas = {
else
self.stateful.rghtclk = self:findobj(x, y)
self.ox, self.oy = x, y
+ self.bx, self.by = x, y
end
else
- if not self.stateful.moving and self.stateful.rghtclk then
+ if not self.stateful.moving and self.stateful.rghtclk and not self.stateful.dragging then
self.stateful.rghtclk.obj:configure()
end
self.stateful.panning = nil
self.stateful.moving = nil
self.stateful.rghtclk = nil
self.stateful.rghtbutton = nil
+ if not self.stateful.leftbutton then self.stateful.dragging = nil end
+ end
+ end
+ end,
+
+ wheel_cb = function (self, delta, x, y, status)
+ local obj = self:findobj(x, y)
+
+ if obj then
+ if iup.isshift(status) then
+ obj:change_curoutput(delta)
+ elseif iup.isalt(status) then
+ obj:change_quicksetting(delta)
+ else
+ obj:change_curinput(delta)
end
end
+
+ self:draw()
end,
create = function (tab)
@@ -140,7 +233,7 @@ dalosp.canvas = {
tab.expand = "Yes"
tab.shrink = "Yes"
tab.minsize = "1x1"
- tab.size = "1x1"
+ tab.rastersize = "1x1"
local r = iup.canvas(tab)
r.action = iupep.dbuffer.action
r.draw = dalosp.canvas.draw
@@ -150,11 +243,15 @@ dalosp.canvas = {
r.focus_cb = dalosp.canvas.focus_cb
r.motion_cb = dalosp.canvas.motion_cb
r.button_cb = dalosp.canvas.button_cb
+ r.wheel_cb = dalosp.canvas.wheel_cb
r.addobj = dalosp.canvas.addobj
r.findobj = dalosp.canvas.findobj
r.moveobj = dalosp.canvas.moveobj
r.setobjplace = dalosp.canvas.setobjplace
r.panning = dalosp.canvas.panning
+ r.setstatus = dalosp.canvas.setstatus
+ r.createlink = dalosp.canvas.createlink
+ r.drawlink = dalosp.canvas.drawlink
r.origin = { x = 0, y = 0 }
r.stateful = {}
@@ -200,32 +297,64 @@ dalosp.object = {
cv:Foreground(self.color)
cv:Box(x1, x2, y1, y2)
cv:Foreground(cd.BLACK)
- cv:TextAlignment(cd.SOUTH) cv:Text(cx, y2, self.name)
- cv:TextAlignment(cd.NORTH_EAST) cv:Text(x1, y1, self.inputs)
- cv:TextAlignment(cd.NORTH_WEST) cv:Text(x2, y1, self.outputs)
- cv:TextAlignment(cd.NORTH) cv:Text(cx, y1, self.quicksetting)
+ cv:TextAlignment(cd.SOUTH)
+ cv:Text(cx, y2, self.name)
+ cv:TextAlignment(cd.NORTH)
+ if self.ninputs >= 1 then
+ cv:Text(x1, y1, self.curinput .. "/" .. self.ninputs)
+ end
+ if self.noutputs >= 1 then
+ cv:Text(x2, y1, self.curoutput .. "/" .. self.noutputs)
+ end
+ if self.quicksetting then
+ cv:Text(cx, y1, self.quicksetting)
+ end
end,
default_activate = function (self)
print "activate"
end,
- default_configure = function(self)
+ default_configure = function (self)
print "configure"
end,
+ change_curinput = function (self, delta)
+ if self.ninputs <= 1 then return end
+ self.curinput = self.curinput + delta
+ if self.curinput > self.ninputs then self.curinput = self.ninputs end
+ if self.curinput <= 0 then self.curinput = 1 end
+ end,
+
+ change_curoutput = function (self, delta)
+ if self.ouputs <= 1 then return end
+ self.curoutput = self.curoutput + delta
+ if self.curoutput > self.noutputs then self.curoutput = self.noutputs end
+ if self.curoutput <= 0 then self.curoutput = 1 end
+ end,
+
+ default_change_quicksetting = function (self, delta)
+ end,
+
create = function (dcanvas, tab, extra)
if not tab then tab = {} end
local obj = {
draw = dalosp.object.default_draw,
name = tab.name or "NoName",
color = tab.color or cd.GRAY,
- inputs = tab.inpus or 0,
- outputs = tab.outputs or 0,
- quicksetting = 0,
+ ninputs = tab.ninputs or 0,
+ noutputs = tab.noutputs or 0,
+ inputs = {},
+ outputs = {},
+ curinput = 1,
+ curoutput = 1,
+ quicksetting = nil,
otype = tab.otype or dalos.objtype.DUMMY,
activate = tab.activate or dalosp.object.default_activate,
configure = tab.configure or dalosp.object.default_configure,
+ change_curinput = dalosp.object.change_curinput,
+ change_curoutput = dalosp.object.change_curoutput,
+ change_quicksetting = tab.change_quicksetting or dalosp.object.default_change_quicksetting,
extra = extra,
}
@@ -260,8 +389,8 @@ dalos.objtype = {
d = dalos.canvas {}
m = dalos.menu {}
-o = dalos.object(d)
-o2 = dalos.object(d)
+o = dalos.object(d, { y = 30, x = 10, noutputs = 1, name = "An output" })
+o2 = dalos.object(d, { y = 30, x = 100, ninputs = 1, name = "An input" })
dlg = iup.dialog { d, title = "Dalos", menu = m }