-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathVolumeEditorWidget.lua
554 lines (413 loc) · 18.5 KB
/
VolumeEditorWidget.lua
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
return function()
local WidgetUtils = require("WidgetUtils");
local widget = new("osgUI::Dialog");
widget.Name = "VolumeSettingsDialog";
widget.load = function(widget, filename)
newVolumeSettings = readObjectFile(filename);
if (newVolumeSettings) then
print("Loaded VolumeSettings file "..filename);
if (widget.VolumeSettings) then
vs = widget.VolumeSettings;
vs.Technique = newVolumeSettings.Technique;
vs.ShadingModel = newVolumeSettings.ShadingModel;
vs.SampleRatio = newVolumeSettings.SampleRatio;
vs.SampleRatioWhenMoving = newVolumeSettings.SampleRatioWhenMoving;
vs.Cutoff = newVolumeSettings.Cutoff;
vs.Transparency = newVolumeSettings.Transparency;
else
widget.VolumeSettings = newVolumeSettings;
end
widget.VolumeSettings.Filename = filename;
widget.WidgetsUpated = false;
widget:updateWidgets();
end
end
widget.save = function(widget, filename)
vs = widget.VolumeSettings;
if (filename == nil) then filename = vs.Filename; end
if (filename == nil) then filename = "VolumeSettings.lua"; end
vs.Filename = "";
vs.Name = "";
print("Saving VolumeSettings to file:"..filename);
writeFile(vs, filename);
vs.Filename = filename;
vs.Name = "VolumeSettings";
end
widget.getWidget = function(widget, name)
local numChildren = widget:getNumChildren();
for i=0, numChildren-1 do
local node = widget:getChild(i);
if (node.Name==name) then return node; end
end
return nil;
end
widget.updateLineEdit = function(widget, name, value)
editWidget = widget:getWidget(name);
if (not editWidget) then return end;
if (type(value)=="table") then
if (value.ModifiedCount ~= editWidget.ModifiedCount) then
editWidget.ModifiedCount = value.ModifiedCount;
editWidget.Text = value.Value;
end
else
editWidget.Text = value;
end
end
widget.updateComboBox = function(widget, name, value)
cbWidget = widget:getWidget(name);
if (not cbWidget) then return end;
-- print("Updating combobox", cbWidget);
for i=0, cbWidget.Items:size()-1 do
print(" checking item ", i, cbWidget.Items[i].Text);
if (cbWidget.Items[i].Text==value) then
cbWidget.CurrentIndex = i;
print("Set current item",i,value);
end
end
end
widget.updateWidgets = function(widget)
if (not widget.VolumeSettings) then return; end;
widget.WidgetsUpated = true;
vs = widget.VolumeSettings;
--print("vs.ModifiedCount=", vs.ModifiedCount, " widget.ModifiedCount=",widget.ModifiedCount);
if (widget.ModifiedCount~=vs.ModifiedCount) then
print("Need to update FilenameEdit etc.");
widget:updateLineEdit("FilenameEdit", vs.Filename);
widget:updateComboBox("TechniqueComboBox", vs.Technique);
widget:updateComboBox("ShadingModelComboBox", vs.ShadingModel);
end
widget:updateLineEdit("SampleRatioEdit", vs.SampleRatioProperty);
widget:updateLineEdit("SampleRatioWhenMovingEdit", vs.SampleRatioWhenMovingProperty);
widget:updateLineEdit("CutoffEdit", vs.CutoffProperty);
widget:updateLineEdit("TransparencyEdit", vs.TransparencyProperty);
widget.ModifiedCount = vs.ModifiedCount;
end
widget.getEditValue = function(widget, name)
editWidget = widget:getWidget(name);
if (editWidget) then return editWidget.Text end;
return nil;
end
widget.copyEditValueToProperty = function(widget, name, property)
editWidget = widget:getWidget(name);
if (editWidget) then
property.Value = editWidget.Text;
editWidget.ModifiedCount = property.ModifiedCount;
end;
return nil;
end
widget.getComboBoxValue = function(widget, name)
cbWidget = widget:getWidget(name);
if (cbWidget) then return cbWidget.Items[cbWidget.CurrentIndex].Text end;
return nil;
end
widget.updateVolumeSettings = function(widget)
if (not(widget.VolumeSettingsUpdated)) then return; end
print("------ widget.updateVolumeSettings -------");
widget.VolumeSettingsUpdated = false;
if (not widget.VolumeSettings) then
widget.VolumeSettings = new("osgVolume:VolumeSettings");
end;
local vs = widget.VolumeSettings;
vs.Filename = widget:getEditValue("FilenameEdit");
vs.Technique = widget:getComboBoxValue("TechniqueComboBox");
vs.ShadingModel = widget:getComboBoxValue("ShadingModelComboBox");
widget:copyEditValueToProperty("SampleRatioEdit", vs.SampleRatioProperty);
widget:copyEditValueToProperty("SampleRatioWhenMovingEdit", vs.SampleRatioWhenMovingProperty);
widget:copyEditValueToProperty("CutoffEdit", vs.CutoffProperty);
widget:copyEditValueToProperty("CutoffEdit", vs.IsoSurfaceProperty);
widget:copyEditValueToProperty("TransparencyEdit", vs.TransparencyProperty);
widget.ModifiedCount = vs.ModifiedCount;
end
widget.createGraphics = function(widget)
print("Running widget.createGraphics");
local function _createLabel(name, text, x, y, width, height)
local label = new("osgUI::Label");
label.Name = name;
label.Extents = {xMin=x, yMin=y, zMin=0, xMax=x+width, yMax=y+height, zMax=0};
label.Text = text;
label.TextSettings = ts;
label.AlignmentSettings = as;
return label;
end
local function _createComboBox(name, x, y, width, height, ...)
local cb = new("osgUI::ComboBox");
cb.Name = name;
cb.Extents = {xMin=x, yMin=y, zMin=0, xMax=x+width, yMax=y+height, zMax=0};
cb.TextSettings = ts;
cb.FrameSettings = raisedFs;
local as = new("osgUI::AlignmentSettings");
as.Alignment = "CENTER_CENTER";
cb.AlignmentSettings = as;
for i,v in ipairs({...}) do
if (type(v)=="function") then
print("\n\n******* FOUND FUNCTION FOR COMBOBOX *********");
cb.currentIndexChanged = v;
else
item = new("osgUI::Item");
item.Text=v;
-- item.Color = {r=1.0,g=1.0,b=0.0,a=0.0};
cb.Items:add(item);
end
end
return cb;
end
local VolumeSettingsWidgetUpdated = function(widget, value)
local dialog = GetNamedNodeFromParentalChain(widget, "VolumeSettingsDialog");
if (dialog) then
dialog.VolumeSettingsUpdated = value;
end
end
local ComboBoxIndexChanged = function(widget, value)
print("Lua function callback : ComboBoxIndexChanged");
VolumeSettingsWidgetUpdated(widget, true);
widget:currentIndexChangedImplementation(value);
end
local function _createButton(name, x, y, width, height, text, pressedCallback)
local pb = new("osgUI::PushButton");
pb.Name = name;
pb.Extents = {xMin=x, yMin=y, zMin=0, xMax=x+width, yMax=y+height, zMax=0};
pb.TextSettings = ts;
pb.AlignmentSettings = centerAlignment;
pb.FrameSettings = raisedFs;
pb.Text = text;
pb.released = pressedCallback;
return pb;
end
local LineEditLeave = function(widget)
print("Lua function callback : LineEditLeave");
widget:leaveImplementation();
if (widget.changed) then
VolumeSettingsWidgetUpdated(widget, true);
widget.changed = false;
end
end
local LineEditUpdated = function(widget)
print("Lua function callback : LineEditUpdated");
if (widget.changed) then
VolumeSettingsWidgetUpdated(widget, true);
widget.changed = false;
end
end
local LineEditChanged = function(widget)
print("Lua function callback : LineEditChanged");
-- VolumeSettingsWidgetUpdated(widget, true);
widget.changed = true;
end
local function _createLineEdit(name, x, y, width, height, text, minValue, maxValue, numDecimals)
local le = new("osgUI::LineEdit");
le.Name = name;
le.Extents = {xMin=x, yMin=y, zMin=0, xMax=x+width, yMax=y+height, zMax=0};
le.TextSettings = ts;
le.AlignmentSettings = as;
le.FrameSettings = fs;
le.Text = text;
if (minValue and maxValue) then
le.Validator = new("osgUI::DoubleValidator");
le.Validator.Bottom = minValue;
le.Validator.Top = maxValue;
if (numDecimals) then
le.Validator.Decimals = numDecimals;
end
end
le.returnPressed = LineEditUpdated;
le.textChanged = LineEditChanged;
le.leave = LineEditLeave;
return le;
end
GetVolumeSettingsFromParentalChain = function(node)
if (node.VolumeSettings) then return node.VolumeSettings; end
if (node:getNumParents()>0) then
for i=0, node:getNumParents()-1 do
local vs = GetVolumeSettingsFromParentalChain(node:getParent(i));
if (vs) then return vs; end
end
else
return nil;
end
end
GetNamedNodeFromParentalChain = function(node, name)
if (node.Name==name) then return node; end
if (node:getNumParents()>0) then
for i=0, node:getNumParents()-1 do
local matchedNode = GetNamedNodeFromParentalChain(node:getParent(i), name);
if (matchedNode) then return matchedNode; end
end
else
return nil;
end
end
local FilePressed = function(widget)
print("file Pressed", widget);
widget:releasedImplementation();
end
local LoadPressed = function(widget)
print("load Pressed", widget);
local dialog = GetNamedNodeFromParentalChain(widget, "VolumeSettingsDialog");
if (dialog) then
local filenameWidget = dialog:getWidget("FilenameEdit");
if (filenameWidget) then
local filename = filenameWidget.Text;
dialog:load(filename);
end
else
print("Could not find dialog widget");
end
widget:releasedImplementation();
end
local SavePressed = function(widget)
print("save Pressed", widget);
local dialog = GetNamedNodeFromParentalChain(widget, "VolumeSettingsDialog");
if (dialog) then
local filenameWidget = dialog:getWidget("FilenameEdit");
local filename = filenameWidget.Text;
dialog:save(filename);
else
print("Could not find dialog widget");
end
widget:releasedImplementation();
end
local cs = 5.0;
local lg = cs*1.5;
local labelWidth = cs*13.0;
local labelHeight = cs*1.3;
local editWidth = cs*7.0;
local editHeight = labelHeight;
local dialogHeight = lg*8.4;
local rightCurrentX = cs+labelWidth+lg
local dialogWidth = rightCurrentX+editWidth+cs;
local tagWidgetTop = dialogHeight-1;
ts = new("osgUI::TextSettings");
ts.Font = "times.ttf";
ts.CharacterSize = cs;
as = new("osgUI::AlignmentSettings");
as.Alignment = "LEFT_CENTER";
centerAlignment = new("osgUI::AlignmentSettings");
centerAlignment.Alignment = "CENTER_CENTER";
fs = new("osgUI::FrameSettings");
fs.LineWidth = 0.5;
fs.Shape = "BOX";
fs.Shadow = "SUNKEN";
editFs = new("osgUI::FrameSettings");
editFs.LineWidth = 0.5;
editFs.Shape = "PANEL";
editFs.Shadow = "SUNKEN";
raisedFs = new("osgUI::FrameSettings");
raisedFs.LineWidth = 0.5;
raisedFs.Shape = "BOX";
raisedFs.Shadow = "RAISED";
dfs = new("osgUI::FrameSettings");
dfs.LineWidth = 1.0;
dfs.Shape = "BOX";
dfs.Shadow = "RAISED";
widget.Title = "Volume Settings Editor";
widget.Extents = {xMin=0, yMin=0, zMin=0, xMax=dialogWidth, yMax=dialogHeight, zMax=0};
widget.TextSettings = ts;
widget.FrameSettings = dfs;
tabWidget = new ("osgUI::TabWidget");
tabWidget.TextSettings = ts;
tabWidget.Extents = {xMin=2, yMin=2, zMin=0, xMax=dialogWidth-2, yMax=tagWidgetTop, zMax=0};
widget:addChild(tabWidget);
-- File Tab
local currentX = cs;
local currentY = tagWidgetTop-20;
fileGroup = new("osgUI::Widget");
fileGroup.Extents = {xMin=2, yMin=2, zMin=0, xMax=dialogWidth-2, yMax=tagWidgetTop, zMax=0};
fileTab = new ("osgUI::Tab");
fileTab.Text = "File"
fileTab.Widget = fileGroup;
tabWidget.Tabs:add(fileTab);
local margin = cs*0.35;
local filenameLabelWidth = cs*4.25;
local filenameEditX = currentX+filenameLabelWidth+margin;
local filenameEditWidth = cs*9.0;
local filenameButtonX = filenameEditX+filenameEditWidth+margin;
local filenameButtonWidth = cs*1.0;
local loadButtonX = filenameButtonX + filenameButtonWidth+margin;
local loadButtonWidth = cs*3.0;
local saveButtonX = loadButtonX + loadButtonWidth+margin;
local saveButtonWidth = cs*3.0;
fileGroup:addChild(WidgetUtils.createLabel("VS-FilenameLabel", "Filename", currentX, currentY, filenameLabelWidth, labelHeight));
fileGroup:addChild(WidgetUtils.createLineEdit("VS-FilenameEdit", filenameEditX, currentY, filenameEditWidth, editHeight, "filename"));
fileGroup:addChild(WidgetUtils.createButton("VS-FilenameButton", filenameButtonX, currentY, filenameButtonWidth, editHeight, "...", FilePressed));
fileGroup:addChild(WidgetUtils.createButton("VS-LoadButton", loadButtonX, currentY, loadButtonWidth, editHeight, "Load", LoadPressed));
fileGroup:addChild(WidgetUtils.createButton("VS-SaveButton", saveButtonX, currentY, saveButtonWidth, editHeight, "Save", SavePressed));
currentY = currentY-lg;
fileGroup:addChild(WidgetUtils.createLabel("TF-FilenameLabel", "Transfer Function File", currentX, currentY, filenameLabelWidth, labelHeight));
fileGroup:addChild(WidgetUtils.createLineEdit("TF-FilenameEdit", filenameEditX, currentY, filenameEditWidth, editHeight, "filename"));
fileGroup:addChild(WidgetUtils.createButton("TF-FilenameButton", filenameButtonX, currentY, filenameButtonWidth, editHeight, "...", FilePressed));
fileGroup:addChild(WidgetUtils.createButton("TF-LoadButton", loadButtonX, currentY, loadButtonWidth, editHeight, "Load", LoadPressed));
fileGroup:addChild(WidgetUtils.createButton("TF-SaveButton", saveButtonX, currentY, saveButtonWidth, editHeight, "Save", SavePressed));
currentY = currentY-lg;
-- Settings Tab
currentX = cs;
currentY = tagWidgetTop-20;
settingsGroup = new("osgUI::Widget");
settingsGroup.Extents = {xMin=2, yMin=2, zMin=0, xMax=dialogWidth-2, yMax=dialogHeight-2, zMax=0};
settingsTab = new ("osgUI::Tab");
settingsTab.Text = "Settings"
settingsTab.Widget = settingsGroup;
tabWidget.Tabs:add(settingsTab);
settingsGroup:addChild(WidgetUtils.createLabel("TechniqueLabel", "Technique", currentX, currentY, labelWidth, labelHeight));
settingsGroup:addChild(createComboBox("TechniqueComboBox", rightCurrentX, currentY, editWidth, editHeight, ComboBoxIndexChanged, "FixedFunction", "RayTraced", "MultiPass"));
currentY = currentY-lg;
settingsGroup:addChild(WidgetUtils.createLabel("ShadingModelLabel", "Shading Model", currentX, currentY, labelWidth, labelHeight));
settingsGroup:addChild(createComboBox("ShadingModelComboBox", rightCurrentX, currentY, editWidth, editHeight, ComboBoxIndexChanged, "Standard", "Light", "Isosurface", "MaximumIntensityProjection"));
currentY = currentY-lg;
settingsGroup:addChild(WidgetUtils.createLabel("SampleRatioLabel", "Sample Ratio", currentX, currentY, labelWidth, labelHeight));
settingsGroup:addChild(WidgetUtils.createLineEdit("SampleRatioEdit", rightCurrentX, currentY, editWidth, editHeight, 1.0, 0.0, 1.0, 5));
currentY = currentY-lg;
settingsGroup:addChild(WidgetUtils.createLabel("SampleRatioWhenMovingLabel", "Sample Ratio When Moving", currentX, currentY, labelWidth, labelHeight));
settingsGroup:addChild(WidgetUtils.createLineEdit("SampleRatioWhenMovingEdit", rightCurrentX, currentY, editWidth, editHeight, 1.0, 0.0, 1.0, 5));
currentY = currentY-lg;
settingsGroup:addChild(WidgetUtils.createLabel("CutoffLabel", "Cutoff", currentX, currentY, labelWidth, labelHeight));
settingsGroup:addChild(WidgetUtils.createLineEdit("CutoffEdit", rightCurrentX, currentY, editWidth, editHeight, 0.0, 0.0, 1.0, 5));
currentY = currentY-lg;
settingsGroup:addChild(WidgetUtils.createLabel("TransparencyLabel", "Transparency", currentX, currentY, labelWidth, labelHeight));
settingsGroup:addChild(WidgetUtils.createLineEdit("TransparencyEdit", rightCurrentX, currentY, editWidth, editHeight, 1.0, 0.0, 1.0, 5));
currentY = currentY-lg;
-- Transfer Function Tab
currentX = cs;
currentY = tagWidgetTop-20;
transferFunctionGroup = new("osgUI::Widget");
transferFunctionGroup.Extents = {xMin=2, yMin=2, zMin=0, xMax=dialogWidth-2, yMax=dialogHeight-2, zMax=0};
transferFunctionTab = new ("osgUI::Tab");
transferFunctionTab.Text = "Colour Map"
transferFunctionTab.Widget = transferFunctionGroup;
tabWidget.Tabs:add(transferFunctionTab);
-- local transferFunctionWidgetFunc = loadfile("TransferFunctionWidget.lua");
local createTransferFunctionWidget = require("TransferFunctionWidget");
if (createTransferFunctionWidget) then
local extents = { xMin = transferFunctionGroup.Extents.xMin+margin,
yMin = transferFunctionGroup.Extents.yMin+margin,
zMin = transferFunctionGroup.Extents.zMin,
xMax = transferFunctionGroup.Extents.xMax-margin,
yMax = transferFunctionGroup.Extents.yMax-margin-9,
zMax = transferFunctionGroup.Extents.zMax };
transferFunctionGroup:addChild(createTransferFunctionWidget(extents));
else
print("************* Have NOT found TransferFunctionWidget.lua ****************");
end
widget.WidgetsUpated = false;
widget:updateWidgets();
widget:createGraphicsImplementation();
end
widget.traverse = function (widget, visitor)
-- print("widget.traverse ", visitor);
if (widget.ModifiedCount~=widget.VolumeSettings.ModifiedCount) then
print("\nWidget has been modified\n");
--widget.ModifiedCount = widget.VolumeSettings.ModifiedCount;
end
local needToSyncWidgets = (visitor.VisitorType == "EVENT_VISITOR");
if (needToSyncWidgets) then
widget:updateWidgets();
end
widget:traverseImplementation(visitor);
if (needToSyncWidgets) then
widget:updateVolumeSettings();
end
end
widget:createGraphics();
widget.VolumeSettings = new("osgVolume::VolumeSettings");
--widget:load("vs.osgt");
return widget;
end