summaryrefslogtreecommitdiff
path: root/dalos.lua
blob: b7f58a0103818d640064720db134d6f92236ef3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
loadmodule "luaiup"
loadmodule "luahandle"
loadmodule "lualibs"

load "iupe-dbuffer.lua"
load "iupe-hexview.lua"
load "iupe-hexview-toolbox.lua"
load "iupe-tview.lua"

if not dalosp then dalosp = {} end
if not dalos then dalos = {} end

dalosp.canvas = {
    draw = function (self)
        local cvdb = self.cvdb
        if not cvdb then return iup.DEFAULT end
        cvdb:Clear()
        
        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)
        end
        
        cvdb:Flush()
        
        return iup.DEFAULT
    end,
    
    addobj = function (self, obj, x, y, w, h)
        table.insert(self.objects, { obj = obj, x = x, y = y, w = w, h = h })
        self:draw()
    end,
    
    findobj = function (self, x, y)
        local obj, ind
        
        x = x + 0
        y = y + 0
        
        for k, v in ipairs(self.objects) do
            if v.x <= x and (v.x + v.w) >= x and v.y <= y and (v.y + v.h) >= y then
                obj, ind = v, k
            end
        end
        
        return obj, ind
    end,
    
    moveobj = function (self, obj, dx, dy)
        obj.x, obj.y = obj.x + dx, obj.y + dy
        self:draw()
    end,
    
    setobjplace = function (self, obj, x, y)
        obj.x, obj.y = x, y
        self:draw()
    end,
    
    panning = function (self, dx, dy)
        self.origin.x = self.origin.x + dx
        self.origin.y = self.origin.y + dy
        self:draw()
    end,
    
    focus_cb = function (self, focus)
    end,
    
    motion_cb = function (self, x, y, status)
        if self.stateful.panning then
            print "Panning..."
            local ox, oy = self.ox, self.oy
            local dx, dy = x - ox, y - oy
            self:panning(dx, dy)
            self.ox, self.oy = x, y
        elseif iup.isbutton1(status) 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...
            else
                -- start selection square ?
            end
        elseif iup.isbutton3(status) then
            if self.stateful.rghtclk and not self.stateful.moving then self.stateful.moving = self.stateful.rghtclk end
            local moving = self.stateful.moving
            if moving then
                local ox, oy = self.ox, self.oy
                local dx, dy = x - ox, y - oy
                self:moveobj(moving, dx, dy)
                self.ox, self.oy = x, y
            else
                -- show X menu
            end
        end
    end,
    
    button_cb = function (self, button, pressed, x, y, status)
        if button == iup.BUTTON1 or not button then
            if pressed == 1 then
                self.stateful.leftbutton = true
                if self.stateful.rghtclk then
                    self.stateful.panning = true
                else
                    self.stateful.leftclk = self:findobj(x,y)
                    self.ox, self.oy = x, y
                    self.bx, self.by = x, y
                end
            else
                if not self.stateful.linking and self.stateful.leftclk then
                    self.stateful.leftclk.obj:activate()
                end
                self.stateful.panning = nil
                self.stateful.linking = nil
                self.stateful.leftclk = nil
                self.stateful.leftbutton = nil
            end
        end
        
        if button == iup.BUTTON3 or not button then
            if pressed == 1 then
                self.stateful.rghtbutton = true
                if self.stateful.leftclk then
                    self.stateful.panning = true
                else
                    self.stateful.rghtclk = self:findobj(x, y)
                    self.ox, self.oy = x, y
                end
            else
                if not self.stateful.moving and self.stateful.rghtclk then
                    self.stateful.rghtclk.obj:configure()
                end
                self.stateful.panning = nil
                self.stateful.moving = nil
                self.stateful.rghtclk = nil
                self.stateful.rghtbutton = nil
            end
        end
    end,
    
    create = function (tab)
        tab.border = "No"
        tab.expand = "Yes"
        tab.shrink = "Yes"
        tab.minsize = "1x1"
        tab.size = "1x1"
        local r = iup.canvas(tab)
        r.action = iupep.dbuffer.action
        r.draw = dalosp.canvas.draw
        r.resize_cb = iupep.dbuffer.resize_cb
        r.map_cb = iupep.dbuffer.map_cb
        r.unmap_cb = iupep.dbuffer.unmap_cb
        r.focus_cb = dalosp.canvas.focus_cb
        r.motion_cb = dalosp.canvas.motion_cb
        r.button_cb = dalosp.canvas.button_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.origin = { x = 0, y = 0 }
        r.stateful = {}
        
        r.objects = {}
        
        return r
    end,
}

dalosp.menu = {
    action_exit = function (self)
        return iup.CLOSE
    end,
    
    action_about = function (self)
        local dlg = iup.messagedlg {
            DialogType = "Information",
            ButtonDefault = "1",
            Buttons = "OK",
            Title = "About",
            Value = 'DALOS (c) 2009-2010 Nicolas "Pixel" Noble.\nThis is free software with ABSOLUTELY NO WARRANTY.\nPlease look at the COPYRIGHT file for details.',
        }
        dlg:popup()
        return iup.DEFAULT
    end,
    
    create = function (tab)
        local item_exit = iup.item { title = "Exit" }
        item_exit.action = dalosp.menu.action_exit
        local item_about = iup.item { title = "About" }
        item_about.action = dalosp.menu.action_about
        local menu_file = iup.submenu { iup.menu { item_exit }, title = "File" }
        local menu_help = iup.submenu { iup.menu { item_about }, title = "Help" }
        return iup.menu { menu_file, menu_help }
    end,
}

dalosp.object = {
    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)
        local cx, cy = (x1 + x2) / 2, (y1 + y2) / 2
        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)
    end,
    
    default_activate = function (self)
        print "activate"
    end,
    
    default_configure = function(self)
        print "configure"
    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,
            otype = tab.otype or dalos.objtype.DUMMY,
            activate = tab.activate or dalosp.object.default_activate,
            configure = tab.configure or dalosp.object.default_configure,
            extra = extra,
        }
        
        if tab.otype and not tab.c then
            if tab.otype == dalos.objtype.DUMMY then
                obj.color = cd.GRAY
            elseif tab.otype == dalos.objtype.HANDLE then
                obj.color = cd.CYAN
            elseif tab.otype == dalos.objtype.LUA_FILTER then
                obj.color = cd.YELLOW
            else
                obj.color = cd.DARK_RED
            end
        end
        
        dcanvas:addobj(obj, tab.x or 0, tab.y or 0, tab.w or 50, tab.h or 50)
    end,
}

dalos.canvas = dalosp.canvas.create
dalos.menu = dalosp.menu.create
dalos.object = dalosp.object.create

dalos.objtype = {
    UNKNOWN = 0,
    DUMMY = 1,
    HANDLE = 2,
    LUA_FILTER = 3,
}

----------------

d = dalos.canvas {}
m = dalos.menu {}
o = dalos.object(d)
o2 = dalos.object(d)

dlg = iup.dialog { d, title = "Dalos", menu = m }

dlg:show()
iup.MainLoop()
dlg:hide()