diff options
| -rw-r--r-- | dalos.lua | 119 | 
1 files changed, 101 insertions, 18 deletions
| @@ -11,22 +11,78 @@ if not dalosp then dalosp = {} end  if not dalos then dalos = {} end  dalosp.canvas = { +    DARK_WHITE = cd.EncodeColor(224, 224, 224), +    BEZIER_CTRL_LEN = 40, +          drawlink = function (self, src, dst, si, di) -        local cvdb = self.cvdb +        local cv = self.cvdb +        local iy = function (y) return cv:InvertYAxis(y) end +        local ctrl_len = dalosp.canvas.BEZIER_CTRL_LEN -        cvdb:Foreground(cd.BLACK) +        cv: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)) +        x1, x2, y1, y2 = math.floor(x1 + 0.5), math.floor(x2 + 0.5), math.floor(y1 + 0.5), math.floor(y2 + 0.5) +        cv:Begin(cd.BEZIER) +        cv:Vertex(x1, iy(y1)) +        cv:Vertex(x1 + ctrl_len, iy(y1)) +        cv:Vertex(x2 - ctrl_len, iy(y2)) +        cv:Vertex(x2, iy(y2)) +        cv:End() +    end, +     +    XMENUWIDTH = 100, +     +    drawxmenu = function (self, x, y) +        local cv = self.cvdb +        local width = dalosp.canvas.XMENUWIDTH +        local x1, x2, y1, y2 = x - width / 2, x + width / 2, y - width / 2, y + width / 2 +        x1, x2, y1, y2 = math.floor(x1 + 0.5), math.floor(x2 + 0.5), math.floor(y1 + 0.5), math.floor(y2 + 0.5) +        local iy = function (y) return cv:InvertYAxis(y) end +        local cx1, cx2, cy1, cy2 = x - 5, x + 5, y - 5, y + 5 +         +        cv:Foreground(dalosp.canvas.DARK_WHITE) +        cv:Box(x1, x2, iy(y2), iy(y1)) +        cv:Foreground(cd.GRAY) +        cv:Line(x1, iy(y1), x2, iy(y2)) +        cv:Line(x1, iy(y2), x2, iy(y1)) +        cv:Line(x1 + 1, iy(y1), x2, iy(y2 - 1)) +        cv:Line(x1, iy(y2 - 1), x2 - 1, iy(y1)) +        cv:Foreground(cd.WHITE) +        cv:Line(x1, iy(y1 + 1), x2 - 1, iy(y2)) +        cv:Line(x1 + 1, iy(y2), x2, iy(y1 + 1)) +        cv:Line(x1, iy(y1 + 2), x2 - 2, iy(y2)) +        cv:Line(x1 + 2, iy(y2), x2, iy(y1 + 2)) +        cv:Foreground(dalosp.canvas.DARK_WHITE) +        cv:Sector(x, iy(y), 10, 10, 0, 360) +        cv:Foreground(cd.GRAY) +        cv:Arc(x, iy(y), 10, 10, 0, 360) +        cv:Foreground(cd.WHITE) +        cv:Arc(x + 1, iy(y + 1), 10, 10, 0, 360) +        cv:Foreground(cd.GRAY) +        cv:Line(x1, iy(y2), x2, iy(y2)) +        cv:Line(x1 + 1, iy(y2 - 1), x2 - 1, iy(y2 - 1)) +        cv:Line(x1 + 2, iy(y2 - 2), x2 - 2, iy(y2 - 2)) +        cv:Line(x2, iy(y1), x2, iy(y2)) +        cv:Line(x2 - 1, iy(y1 + 1), x2 - 1, iy(y2 - 1)) +        cv:Line(x2 - 2, iy(y1 + 2), x2 - 2, iy(y2 - 2)) +        cv:Foreground(cd.WHITE) +        cv:Line(x1, iy(y1), x1, iy(y2)) +        cv:Line(x1 + 1, iy(y1 + 1), x1 + 1, iy(y2 - 1)) +        cv:Line(x1 + 2, iy(y1 + 2), x1 + 2, iy(y2 - 2)) +        cv:Line(x1, iy(y1), x2, iy(y1)) +        cv:Line(x1 + 1, iy(y1 + 1), x2 - 1, iy(y1 + 1)) +        cv:Line(x1 + 2, iy(y1 + 2), x2 - 2, iy(y1 + 2))      end,      draw = function (self)          local cvdb = self.cvdb          if not cvdb then return iup.DEFAULT end +        cvdb:Background(dalosp.canvas.DARK_WHITE)          cvdb:Clear()          for k, v in ipairs(self.objects) do @@ -44,6 +100,8 @@ dalosp.canvas = {              cvdb:Line(self.bx, cvdb:InvertYAxis(self.by), self.ox, cvdb:InvertYAxis(self.oy))          end +        if self.menu.x then self:drawxmenu(self.menu.x, self.menu.y) end +                  cvdb:Flush()          return iup.DEFAULT @@ -137,8 +195,9 @@ dalosp.canvas = {                  local dx, dy = x - ox, y - oy                  self:moveobj(moving, dx, dy)                  self.ox, self.oy = x, y -            else -                -- todo: show X menu +            elseif not self.menu.x then +                self.menu.x, self.menu.y = x, y +                self:draw()              end          end      end, @@ -160,6 +219,17 @@ dalosp.canvas = {          dst.obj:input_change(dst.obj.curinput)      end, +    destroylink = function (self, src) +        local oldsrc = src.obj.outputs[src.obj.curoutput] +        if oldsrc then +            src.obj.outputs[src.obj.curoutput] = nil +            src.obj:output_change(src.obj.curoutput) + +            oldsrc.obj.obj.inputs[oldsrc.ind] = nil +            oldsrc.obj.obj:input_change(oldsrc.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 @@ -171,12 +241,14 @@ dalosp.canvas = {                      self.stateful.panning = true                  else                      local obj, ind = self:findobj(x, y) -                    self.stateful.leftclk = obj -                    table.remove(self.objects, ind) -                    table.insert(self.objects, obj) -                    self.ox, self.oy = x, y -                    self.bx, self.by = x, y -                    self:draw() +                    if obj then +                        self.stateful.leftclk = obj +                        table.remove(self.objects, ind) +                        table.insert(self.objects, obj) +                        self.ox, self.oy = x, y +                        self.bx, self.by = x, y +                        self:draw() +                    end                  end              else                  if not self.stateful.linking and self.stateful.leftclk and not self.stateful.dragging then @@ -184,7 +256,7 @@ dalosp.canvas = {                  elseif self.stateful.linking then                      local dest = self:findobj(x,y)                      if not dest then -                        -- just do nothing, huh... +                        self:destroylink(self.stateful.linking)                      elseif 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 @@ -210,16 +282,24 @@ dalosp.canvas = {                      self.stateful.panning = true                  else                      local obj, ind = self:findobj(x, y) -                    self.stateful.rghtclk = obj -                    table.remove(self.objects, ind) -                    table.insert(self.objects, obj) -                    self.ox, self.oy = x, y -                    self.bx, self.by = x, y -                    self:draw() +                    if obj then +                        self.stateful.rghtclk = obj +                        table.remove(self.objects, ind) +                        table.insert(self.objects, obj) +                        self.ox, self.oy = x, y +                        self.bx, self.by = x, y +                        self:draw() +                    end                  end              else                  if not self.stateful.moving and self.stateful.rghtclk and not self.stateful.dragging then                      self.stateful.rghtclk.obj:configure() +                    self:draw() +                elseif self.menu.x then +                    -- activate X menu operation +                    self.menu.x = nil +                    self.menu.y = nil +                    self:draw()                  end                  self.stateful.panning = nil                  self.stateful.moving = nil @@ -269,8 +349,11 @@ dalosp.canvas = {          r.panning = dalosp.canvas.panning          r.setstatus = dalosp.canvas.setstatus          r.createlink = dalosp.canvas.createlink +        r.destroylink = dalosp.canvas.destroylink          r.drawlink = dalosp.canvas.drawlink +        r.drawxmenu = dalosp.canvas.drawxmenu          r.origin = { x = 0, y = 0 } +        r.menu = { }          r.stateful = {}          r.objects = {} | 
