ADG Lua 

ADG Lua Commit Details

Date:2013-03-18 22:08:31 (6 years 5 months ago)
Author:Nicola Fontana
Branch:master
Commit:e31d2ccaaee25f85baba37562c48d979fbeabd77
Parents: b865851a2a8daafbe91055884acc1b4a4c0df5e0
Message:adg-demo: added bare editing capabilities

Handling the edit dialog and updating the drawing on request. There are
still some rough edges but the program is almost equivalent (but a lot
shorter and neater) to the original adg-demo.c.
Changes:
Madg-demo.lua.in (2 diffs)
Mpiston.lua (2 diffs)

File differences

adg-demo.lua.in
9494
9595
9696
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
97177
98178
99179
......
170250
171251
172252
173
253
174254
175255
176256
end
-- Edit dialog
local widgets = {
'A', 'B', 'C', 'DHOLE', 'LHOLE', 'GROOVE', 'ZGROOVE', 'DGROOVE', 'LGROOVE',
'D1', 'D2', 'LD2', 'D3', 'LD3', 'D4', 'D5', 'LD5', 'D6', 'LD6', 'D7',
'TITLE', 'DRAWING', 'AUTHOR', 'DATE',
}
for _, name in pairs(widgets) do
local widget = ui['edit' .. name]
if widget then
-- Synchronize GtkSpinButton to GtkAdjustment, that is
-- initialize the widget to its default value
if widget._name == 'Gtk.SpinButton' then
local adj = widget:get_adjustment()
adj:value_changed()
end
-- Unlock the UI on every widget change
local signal = widget._name == 'Gtk.ToggleButton' and 'toggled' or 'changed'
widget['on_' .. signal] = function ()
ui.btnApply:set_sensitive(true)
ui.btnReset:set_sensitive(true)
end
piston[name] = widget:get_value()
end
end
local function view_to_model()
end
ui.mainEdit.on_clicked = function ()
local dialog = ui.wndEdit
dialog:set_position(Gtk.WindowPosition.MOUSE);
local function lock_ui()
ui.btnApply:set_sensitive(false)
ui.btnReset:set_sensitive(false)
end
ui.btnApply.on_clicked = function ()
-- Update the piston data using the widgets
for _, name in pairs(widgets) do
local widget = ui['edit' .. name]
if widget then
piston[name] = widget:get_value()
end
end
-- Regenerate all the models
for _, model in pairs(piston.model) do
model:reset()
model:regenerate(piston)
model:changed()
end
-- Refresh the GTK+ interface
ui.mainCanvas:queue_draw()
lock_ui()
end
ui.btnReset.on_clicked = function ()
-- Update the widgets using the piston data
for _, name in pairs(widgets) do
local widget = ui['edit' .. name]
if widget then
widget:set_value(piston[name])
end
end
lock_ui()
end
dialog.on_response = Adg.gtk_window_hide_here
dialog:run()
end
-- Save as dialog
ui.mainSaveAs.on_clicked = function ()
local dialog = ui.wndSaveAs
canvas:set_global_map(old_map)
end
local result, err = operation:run(Gtk.PrintOperationAction.PRINT_DIALOG, window)
local result, err = operation:run(Gtk.PrintOperationAction.PRINT_DIALOG, ui.wndMain)
if result == Gtk.PrintOperationResult.APPLY then
settings = operation:get_print_settings()
end
piston.lua
4848
4949
5050
51
5152
52
53
54
53
54
55
56
57
58
5559
56
57
60
61
62
63
64
5865
5966
6067
......
182189
183190
184191
185
186
192
193
194
195
196
197
187198
188199
189
190
191
192
200
201
202
193203
194
195
204
196205
197206
198207
208
199209
200210
201211
-----------------------------------------------------------------
local model = {}
local constructor = {}
function model.body(piston)
local pair = Cpml.Pair {}
local path = Adg.Path {}
-- Inject the regenerate method into Adg.Model
rawset(Adg.Model, 'regenerate', function (model, part)
-- Call the original constructor of model, registered during the first call
-- of the same constructor, to regenerate it with the data stored in part.
constructor[model](part, model)
end)
pair.x = 0
pair.y = piston.D1 / 2
function model.body(piston, path)
path = path or Adg.Path {}
constructor[path] = model.body
local pair = Cpml.Pair { x = 0, y = piston.D1 / 2 }
path:move_to(pair)
path:set_named_pair('D1I', pair)
return path
end
function model.edges(piston)
return Adg.Edges { source = piston.model.body }
function model.edges(piston, edges)
edges = edges or Adg.Edges {}
constructor[edges] = model.edges
edges:set_source(piston.model.body)
return edges
end
function model.hole(piston)
local pair = Cpml.Pair {}
local tmp = Cpml.Pair {}
local path = Adg.Path {}
function model.hole(piston, path)
path = path or Adg.Path {}
constructor[path] = model.hole
pair.x = piston.LHOLE
pair.y = 0
local pair = Cpml.Pair {x = piston.LHOLE, y = 0 }
path:move_to(pair)
path:set_named_pair('LHOLE', pair)
local tmp = Cpml.Pair {}
tmp.y = piston.DHOLE / 2
tmp.x = pair.x - tmp.y / SQRT3
path:line_to(tmp)

Archive Download the corresponding diff file

Branches

Tags