-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathManipulator.lua
322 lines (279 loc) · 9.32 KB
/
Manipulator.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
--[[
Title: Manipulator Base
Author(s): LiXizhi@yeah.net
Date: 2015/8/10
Desc: Manipulator is the base class used for creating user-defined manipulators.
A manipulator can be connected to a depend node instead of updating a node attribute directly
call AddValue() in constructor if one wants to define a custom manipulator property(plug)
that can be easily binded with depedent node's plug.
Virtual functions:
mousePressEvent(event)
mouseMoveEvent
mouseReleaseEvent
draw
use the lib:
------------------------------------------------------------
NPL.load("(gl)script/ide/System/Scene/Manipulators/Manipulator.lua");
local Manipulator = commonlib.gettable("System.Scene.Manipulators.Manipulator");
------------------------------------------------------------
]]
NPL.load("(gl)script/ide/System/Scene/Overlays/Overlay.lua");
NPL.load("(gl)script/ide/System/Scene/Overlays/ShapesDrawer.lua");
NPL.load("(gl)script/ide/System/Scene/Cameras/Cameras.lua");
NPL.load("(gl)script/ide/System/Windows/Screen.lua");
NPL.load("(gl)script/ide/System/Core/AttributeObject.lua");
NPL.load("(gl)script/ide/STL.lua");
local Screen = commonlib.gettable("System.Windows.Screen");
local Matrix4 = commonlib.gettable("mathlib.Matrix4");
local math3d = commonlib.gettable("mathlib.math3d");
local Cameras = commonlib.gettable("System.Scene.Cameras");
local ShapesDrawer = commonlib.gettable("System.Scene.Overlays.ShapesDrawer");
local Manipulator = commonlib.inherit(commonlib.gettable("System.Scene.Overlays.Overlay"), commonlib.gettable("System.Scene.Manipulators.Manipulator"));
Manipulator:Property("Name", "Manipulator");
Manipulator:Property({"PenWidth", 0.02});
Manipulator:Property({"mainColor", "#000000"});
Manipulator:Property({"dimmedColor", "#000000"});
Manipulator:Property({"selectedColor", "#ffff00"});
Manipulator:Property({"hoverColor", "#ffff00"});
Manipulator:Property({"labelColor", "#000000"});
Manipulator:Property({"labelBackgroundColor", "#ffffff"});
Manipulator:Property({"lineColor", "#808080"});
Manipulator:Property({"gridColor", "#000000"});
Manipulator:Property({"xColor", "#ff0000"});
Manipulator:Property({"yColor", "#0000ff"});
Manipulator:Property({"zColor", "#00ff00"});
Manipulator:Property({"textScale", 0.01});
Manipulator:Signal("valueChanged", function() end);
-- connect this to depedent node to support undo/redo operation.
Manipulator:Signal("modifyBegun");
Manipulator:Signal("modifyEnded");
Manipulator.valueFields = commonlib.ArrayMap:new();
function Manipulator:ctor()
self.pen = {width=self.PenWidth, color="#ff0000"}
self.valueFields = commonlib.ArrayMap:new();
end
function Manipulator:init(parent)
return Manipulator._super.init(self, parent);
end
function Manipulator:Destroy()
-- just in case we forget to call end modify
self:EndModify();
Manipulator._super.Destroy(self);
end
function Manipulator:SetSceneContext(context)
self.scene_context = scene_context;
end
function Manipulator:GetSceneContext()
return self.scene_context;
end
function Manipulator:BeginModify()
if(not self.m_bIsBeginModify) then
self.m_bIsBeginModify = true;
self:modifyBegun();
end
end
function Manipulator:EndModify()
if(self.m_bIsBeginModify) then
self.m_bIsBeginModify = nil;
self:modifyEnded();
end
end
-- virtual function callback, called whenever the values is modified. Both SetField and SetFieldInternal will call this
function Manipulator:OnValueChange(name, value)
end
function Manipulator:GetPen()
return self.pen;
end
-- virtual:
function Manipulator:mousePressEvent(mouse_event)
end
-- virtual:
function Manipulator:mouseMoveEvent(mouse_event)
end
-- virtual:
function Manipulator:mouseReleaseEvent(mouse_event)
end
-- virtual: actually means key stroke.
function Manipulator:keyPressEvent(key_event)
end
-- transform a list of local space vectors to view space. mostly used for calculating mouse positions.
-- @param vecList: input|output: a list of vector3d like {{0,1,0}}, which will be transformed
function Manipulator:TransformVectorsInViewSpace(vecList)
local worldMat = self:CalculateWorldMatrix(nil, true);
local viewMat = Cameras:GetCurrent():GetViewMatrix();
local worldViewMat = worldMat*viewMat;
for _, vec in ipairs(vecList) do
math3d.VectorMultiplyMatrix(vec, vec, worldViewMat);
end
return vecList;
end
function Manipulator:TransformVectorInViewSpace(vec)
local worldMat = self:CalculateWorldMatrix(nil, true);
local viewMat = Cameras:GetCurrent():GetViewMatrix();
local worldViewMat = worldMat*viewMat;
math3d.VectorMultiplyMatrix(vec, vec, worldViewMat);
return vec;
end
-- transform 3d vectors to screen space (projection space / w).
-- only v[1], v[2] should be used in returned value
function Manipulator:TransformVectorsInScreenSpace(vecList)
local worldMat = self:CalculateWorldMatrix(nil, true);
local viewMat = Cameras:GetCurrent():GetViewMatrix();
local projMat = Cameras:GetCurrent():GetProjMatrix();
local finalMat = worldMat*viewMat*projMat;
local screenWidth = Screen:GetWidth();
local screenHeight = Screen:GetHeight();
for _, vec in ipairs(vecList) do
math3d.Vector4MultiplyMatrix(vec, vec, finalMat);
vec:MulByFloat(1/vec[4]);
vec[1] = ((vec[1]+1)*0.5) * screenWidth;
vec[2] = ((1-vec[2])*0.5) * screenHeight;
vec[3] = 0;
vec[4] = nil;
end
return vecList;
end
-- @param mouse_x, mouse_y : if nil, it is the current mouse position.
-- @param plane: ShapePlane in view space.
-- @param point: if not nil, it will receive the intersection point in view space
-- @return: int, distance: interaction type, and distance.
-- 1:intersected
-- -1:The plane is parallel to the ray;
-- -3:The intersection occurs behind the ray's origin.
function Manipulator:MouseRayIntersectPlane(mouse_x, mouse_y, plane, point)
local mouseRay = Cameras:GetCurrent():GetMouseRay(mouse_x, mouse_y);
local result, t = mouseRay:IntersectPlane(plane, point);
if(result == -2) then
local result2, t2 = mouseRay:IntersectPlane(plane:clone():inverse(), point);
if( result2 == 1 ) then
return result2, t2
end
end
return result, t;
end
-- when a group of changes takes place, such as during recording,
-- we can put change inside BeginUpdate() and EndUpdate() pairs, so that
-- only one keyChanged() event will be emitted.
function Manipulator:BeginUpdate()
if(not self.isBeginUpdate) then
self.isBeginUpdate = 0;
self.isValueChanged = false;
else
self.isBeginUpdate = self.isBeginUpdate + 1;
end
end
function Manipulator:EndUpdate()
if(self.isBeginUpdate) then
if(self.isBeginUpdate <= 0) then
self.isBeginUpdate = false;
if(self.isValueChanged) then
self.isValueChanged = false;
self:SetModified();
end
else
self.isBeginUpdate = self.isBeginUpdate - 1;
end
end
end
function Manipulator:SetModified()
if(self.isBeginUpdate) then
self.isValueChanged = true;
else
self:valueChanged();
end
end
-------------------------------------
-- reimplement Attribute Fields interface:
-- 1. support dyanamically AddValue,
-- 2. automatically emit valueChanged signal
-- 3. support getting previous value in addition to current value.
-------------------------------------
-- @return attribute plug
function Manipulator:AddValue(name, defaultValue)
local fieldType;
local valueType = type(defaultValue);
if(valueType == "table") then
local nSize = #defaultValue;
if(nSize == 3) then
fieldType = "vector3";
elseif(nSize == 2) then
fieldType = "vector2";
elseif(nSize == 4) then
fieldType = "vector4";
elseif(nSize == 16) then
fieldType = "Matrix4";
else
fieldType = "";
end
elseif(valueType == "bool") then
fieldType = "bool";
elseif(valueType == "string") then
fieldType = "string";
else
fieldType = "double";
end
self.valueFields:add(name, {name=name, value=defaultValue, preValue=defaultValue, type=fieldType})
return self:findPlug(name);
end
function Manipulator:GetFieldNum()
return self.valueFields:size();
end
function Manipulator:GetFieldName(valueIndex)
local field = self.valueFields:at(valueIndex);
if(field) then
return field.name;
end
end
function Manipulator:GetFieldIndex(sFieldname)
return self.valueFields:getIndex(sFieldname) or -1;
end
function Manipulator:GetFieldType(nIndex)
local field = self.valueFields:at(valueIndex);
if(field) then
return field.type;
end
end
function Manipulator:SetField(name, value)
local field = self.valueFields:get(name);
if(field) then
if(not commonlib.partialcompare(value, field.value)) then
field.preValue = field.value;
field.value = value;
self:OnValueChange(name, value);
self:SetModified();
end
end
end
-- without sending the valueChanged signal like SetField.
-- usually used for updating manipulator values from plugs
function Manipulator:SetFieldInternal(name, value)
local field = self.valueFields:get(name);
if(field) then
if(not commonlib.partialcompare(value, field.value)) then
field.preValue = field.value;
field.value = value;
self:OnValueChange(name, value);
end
end
end
function Manipulator:GetField(name, defaultValue)
local field = self.valueFields:get(name);
if(field) then
return field.value;
else
return defaultValue;
end
end
-- similar to SetField
function Manipulator:SetValue(name, value)
self:SetField(name, value);
end
-- return the value similar to GetField
function Manipulator:GetValue(name, defaultValue, bPreviousValue)
if(not bPreviousValue) then
return self:GetField(name, defaultValue);
else
local field = self.valueFields:get(name);
return field.preValue;
end
end