-
Notifications
You must be signed in to change notification settings - Fork 30
/
FabricSpliceToolContext.cpp
453 lines (366 loc) · 12.8 KB
/
FabricSpliceToolContext.cpp
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
#include <QtGui/QWidget>
#include <QtGui/QKeyEvent>
#include <QtGui/QMouseEvent>
#include <QtGui/QWheelEvent>
#include "FabricSpliceToolContext.h"
#include "FabricSpliceBaseInterface.h"
#include "FabricSpliceRenderCallback.h"
#include <FabricSplice.h>
#include <maya/MCursor.h>
#include <maya/MDagPath.h>
#include <maya/MFnCamera.h>
#include <maya/MAnimControl.h>
#include <maya/MTime.h>
#include <map>
/////////////////////////////////////////////////////
// FabricSpliceManipulationCmd
FabricCore::RTVal FabricSpliceManipulationCmd::s_rtval_commands;
FabricSpliceManipulationCmd::FabricSpliceManipulationCmd()
{
m_rtval_commands = s_rtval_commands;
}
FabricSpliceManipulationCmd::~FabricSpliceManipulationCmd()
{
}
void* FabricSpliceManipulationCmd::creator()
{
return new FabricSpliceManipulationCmd;
}
MStatus FabricSpliceManipulationCmd::doIt(const MArgList &args)
{
return MStatus::kSuccess;
}
MStatus FabricSpliceManipulationCmd::redoIt()
{
try
{
if(m_rtval_commands.isValid()){
for(int i=0; i<m_rtval_commands.getArraySize(); i++){
m_rtval_commands.getArrayElement(i).callMethod("", "doAction", 0, 0);
}
}
M3dView view = M3dView::active3dView();
view.refresh(true, true);
return MStatus::kSuccess;
}
catch (FabricCore::Exception e)
{
mayaLogErrorFunc(e.getDesc_cstr());
return MStatus::kFailure;
}
}
MStatus FabricSpliceManipulationCmd::undoIt()
{
try
{
if(m_rtval_commands.isValid()){
for(int i=0; i<m_rtval_commands.getArraySize(); i++){
m_rtval_commands.getArrayElement(i).callMethod("", "undoAction", 0, 0);
}
}
M3dView view = M3dView::active3dView();
view.refresh(true, true);
return MStatus::kSuccess;
}
catch (FabricCore::Exception e)
{
mayaLogErrorFunc(e.getDesc_cstr());
return MStatus::kFailure;
}
}
bool FabricSpliceManipulationCmd::isUndoable() const
{
return true;
}
/////////////////////////////////////////////////////
// FabricSpliceManipulationCmd
FabricSpliceToolCmd::FabricSpliceToolCmd()
{
setCommandString("FabricSpliceToolCmd");
}
FabricSpliceToolCmd::~FabricSpliceToolCmd()
{
}
void* FabricSpliceToolCmd::creator()
{
return new FabricSpliceToolCmd;
}
MStatus FabricSpliceToolCmd::doIt(const MArgList &args)
{
return redoIt();
}
MStatus FabricSpliceToolCmd::redoIt()
{
// we don't do anything during the tool really
return MStatus::kSuccess;
}
MStatus FabricSpliceToolCmd::undoIt()
{
return MStatus::kSuccess;
}
bool FabricSpliceToolCmd::isUndoable() const
{
return false;
}
/////////////////////////////////////////////////////
// FabricSpliceToolContext
class EventFilterObject : public QObject
{
public:
FabricSpliceToolContext *tool;
bool eventFilter(QObject *object, QEvent *event);
};
static EventFilterObject sEventFilterObject;
const char helpString[] = "Click and drag to interact with Fabric:Splice.";
FabricSpliceToolContext::FabricSpliceToolContext()
{
}
void FabricSpliceToolContext::getClassName( MString & name ) const
{
name.set("FabricSpliceTool");
}
void FabricSpliceToolContext::toolOnSetup(MEvent &)
{
M3dView view = M3dView::active3dView();
const FabricCore::Client * client = NULL;
FECS_DGGraph_getClient(&client);
if(!client){
mayaLogFunc("Fabric Client not constructed yet. A Splice Node must be created before the manipulation tool can be activated.");
return;
}
try{
mManipulationHandle = FabricSplice::constructObjectRTVal("ManipulationHandle");
if(mManipulationHandle.isValid()){
mManipulationHandle.callMethod("", "activateManipulation", 0, 0);
view.refresh(true, true);
}
}
catch(FabricCore::Exception e) {
mayaLogErrorFunc(e.getDesc_cstr());
return;
}
catch(FabricSplice::Exception e){
mayaLogErrorFunc(e.what());
return;
}
sEventFilterObject.tool = this;
view.widget()->installEventFilter(&sEventFilterObject);
view.widget()->setFocus();
setCursor( MCursor::editCursor);
setHelpString(helpString);
setTitleString("FabricSplice Tool");
view.refresh(true, true);
}
void FabricSpliceToolContext::toolOffCleanup()
{
try
{
M3dView view = M3dView::active3dView();
view.widget()->removeEventFilter(&sEventFilterObject);
view.widget()->clearFocus();
if(mManipulationHandle.isValid()){
// By deactivating the manipulation, we enable the manipulators to perform
// cleanup, such as hiding paint brushes/gizmos.
mManipulationHandle.callMethod("", "deactivateManipulation", 0, 0);
view.refresh(true, true);
mManipulationHandle.invalidate();
}
view.refresh(true, true);
}
catch (FabricCore::Exception e)
{
mayaLogErrorFunc(e.getDesc_cstr());
}
}
MStatus FabricSpliceToolContext::doPress(MEvent & event)
{
return MS::kSuccess;
}
MStatus FabricSpliceToolContext::doDrag(MEvent & event)
{
return MS::kSuccess;
}
MStatus FabricSpliceToolContext::doRelease( MEvent & event)
{
return MS::kSuccess;
}
MStatus FabricSpliceToolContext::doEnterRegion( MEvent & event)
{
return setHelpString( helpString );
}
MPxContext* FabricSpliceToolContextCmd::makeObj()
{
return new FabricSpliceToolContext;
}
void* FabricSpliceToolContextCmd::creator()
{
return new FabricSpliceToolContextCmd;
}
bool EventFilterObject::eventFilter(QObject *object, QEvent *event)
{
return tool->onEvent(event);
}
bool FabricSpliceToolContext::onEvent(QEvent *event)
{
if(!mManipulationHandle.isValid()){
mayaLogFunc("Fabric Client not constructed yet.");
return false;
}
// Now we translate the Qt events to FabricEngine events..
M3dView view = M3dView::active3dView();
FabricCore::RTVal klevent;
if(event->type() == QEvent::Enter){
klevent = FabricSplice::constructObjectRTVal("MouseEvent");
}
else if(event->type() == QEvent::Leave){
klevent = FabricSplice::constructObjectRTVal("MouseEvent");
}
else if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
try{
klevent = FabricSplice::constructObjectRTVal("KeyEvent");
}
catch(FabricCore::Exception e) {
mayaLogErrorFunc(e.getDesc_cstr());
return false;
}
catch(FabricSplice::Exception e){
mayaLogErrorFunc(e.what());
return false;
}
klevent.setMember("key", FabricSplice::constructUInt32RTVal(keyEvent->key()));
klevent.setMember("count", FabricSplice::constructUInt32RTVal(keyEvent->count()));
klevent.setMember("isAutoRepeat", FabricSplice::constructBooleanRTVal(keyEvent->isAutoRepeat()));
}
else if ( event->type() == QEvent::MouseMove ||
event->type() == QEvent::MouseButtonDblClick ||
event->type() == QEvent::MouseButtonPress ||
event->type() == QEvent::MouseButtonRelease
) {
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
klevent = FabricSplice::constructObjectRTVal("MouseEvent");
FabricCore::RTVal klpos = FabricSplice::constructRTVal("Vec2");
klpos.setMember("x", FabricSplice::constructFloat32RTVal(mouseEvent->pos().x()));
klpos.setMember("y", FabricSplice::constructFloat32RTVal(mouseEvent->pos().y()));
klevent.setMember("button", FabricSplice::constructUInt32RTVal(mouseEvent->button()));
klevent.setMember("buttons", FabricSplice::constructUInt32RTVal(mouseEvent->buttons()));
klevent.setMember("pos", klpos);
}
else if (event->type() == QEvent::Wheel) {
QWheelEvent *mouseWheelEvent = static_cast<QWheelEvent *>(event);
try{
klevent = FabricSplice::constructObjectRTVal("MouseWheelEvent");
}
catch(FabricCore::Exception e) {
mayaLogErrorFunc(e.getDesc_cstr());
return false;
}
catch(FabricSplice::Exception e){
mayaLogErrorFunc(e.what());
return false;
}
FabricCore::RTVal klpos = FabricSplice::constructRTVal("Vec2");
klpos.setMember("x", FabricSplice::constructFloat32RTVal(mouseWheelEvent->pos().x()));
klpos.setMember("y", FabricSplice::constructFloat32RTVal(mouseWheelEvent->pos().y()));
klevent.setMember("buttons", FabricSplice::constructUInt32RTVal(mouseWheelEvent->buttons()));
klevent.setMember("delta", FabricSplice::constructSInt32RTVal(mouseWheelEvent->delta()));
klevent.setMember("pos", klpos);
}
if(klevent.isValid()){
int eventType = int(event->type());
klevent.setMember("eventType", FabricSplice::constructUInt32RTVal(eventType));
QInputEvent *inputEvent = static_cast<QInputEvent *>(event);
klevent.setMember("modifiers", FabricSplice::constructUInt32RTVal(inputEvent->modifiers()));
//////////////////////////
// Setup the Camera
{
//////////////////////////
// Setup the viewport
FabricCore::RTVal inlineViewport = FabricSplice::constructObjectRTVal("InlineViewport");
{
FabricCore::RTVal viewportDim = FabricSplice::constructRTVal("Vec2");
viewportDim.setMember("x", FabricSplice::constructFloat64RTVal(view.portWidth()));
viewportDim.setMember("y", FabricSplice::constructFloat64RTVal(view.portHeight()));
inlineViewport.setMember("dimensions", viewportDim);
}
{
FabricCore::RTVal inlineCamera = FabricSplice::constructObjectRTVal("InlineCamera");
MDagPath cameraDag;
view.getCamera(cameraDag);
MFnCamera camera(cameraDag);
inlineCamera.setMember("isOrthographic", FabricSplice::constructBooleanRTVal(camera.isOrtho()));
double fovX, fovY;
camera.getPortFieldOfView(view.portWidth(), view.portHeight(), fovX, fovY);
inlineCamera.setMember("fovY", FabricSplice::constructFloat64RTVal(fovY));
inlineCamera.setMember("nearDistance", FabricSplice::constructFloat64RTVal(camera.nearClippingPlane()));
inlineCamera.setMember("farDistance", FabricSplice::constructFloat64RTVal(camera.farClippingPlane()));
MMatrix mayaCameraMatrix = cameraDag.inclusiveMatrix();
FabricCore::RTVal cameraMat = inlineCamera.maybeGetMember("mat44");
try
{
FabricCore::RTVal cameraMatData = cameraMat.callMethod("Data", "data", 0, 0);
float * cameraMatFloats = (float*)cameraMatData.getData();
if(cameraMat) {
cameraMatFloats[0] = (float)mayaCameraMatrix[0][0];
cameraMatFloats[1] = (float)mayaCameraMatrix[1][0];
cameraMatFloats[2] = (float)mayaCameraMatrix[2][0];
cameraMatFloats[3] = (float)mayaCameraMatrix[3][0];
cameraMatFloats[4] = (float)mayaCameraMatrix[0][1];
cameraMatFloats[5] = (float)mayaCameraMatrix[1][1];
cameraMatFloats[6] = (float)mayaCameraMatrix[2][1];
cameraMatFloats[7] = (float)mayaCameraMatrix[3][1];
cameraMatFloats[8] = (float)mayaCameraMatrix[0][2];
cameraMatFloats[9] = (float)mayaCameraMatrix[1][2];
cameraMatFloats[10] = (float)mayaCameraMatrix[2][2];
cameraMatFloats[11] = (float)mayaCameraMatrix[3][2];
cameraMatFloats[12] = (float)mayaCameraMatrix[0][3];
cameraMatFloats[13] = (float)mayaCameraMatrix[1][3];
cameraMatFloats[14] = (float)mayaCameraMatrix[2][3];
cameraMatFloats[15] = (float)mayaCameraMatrix[3][3];
inlineCamera.setMember("mat44", cameraMat);
}
}
catch (FabricCore::Exception e)
{
mayaLogErrorFunc(e.getDesc_cstr());
}
inlineViewport.setMember("camera", inlineCamera);
}
klevent.setMember("viewport", inlineViewport);
}
//////////////////////////
// Setup the Host
// We cannot set an interface value via RTVals.
FabricCore::RTVal host = FabricSplice::constructObjectRTVal("Host");
host.setMember("hostName", FabricSplice::constructStringRTVal("Maya"));
klevent.setMember("host", host);
//////////////////////////
// Invoke the event...
try{
mManipulationHandle.callMethod("Boolean", "onEvent", 1, &klevent);
bool result = klevent.callMethod("Boolean", "isAccepted", 0, 0).getBoolean();
if(result)
event->accept();
if(host.maybeGetMember("redrawRequested").getBoolean())
view.refresh(true, true);
if(host.callMethod("Boolean", "undoRedoCommandsAdded", 0, 0).getBoolean()){
// Cache the rtvals in a static variable that the command will then stor in the undo stack.
FabricSpliceManipulationCmd::s_rtval_commands = host.callMethod("UndoRedoCommand[]", "getUndoRedoCommands", 0, 0);
bool displayEnabled = true;
MGlobal::executeCommandOnIdle(MString("fabricSpliceManipulation"), displayEnabled);
}
klevent.invalidate();
return result;
}
catch(FabricCore::Exception e) {
mayaLogErrorFunc(e.getDesc_cstr());
return false;
}
catch(FabricSplice::Exception e){
mayaLogErrorFunc(e.what());
return false;
}
}
// the event was not handled by FabricEngine manipulation system.
return false;
}