-
Notifications
You must be signed in to change notification settings - Fork 14
/
ofxsRectangleInteract.cpp
682 lines (624 loc) · 24.1 KB
/
ofxsRectangleInteract.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
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; -*- */
/* ***** BEGIN LICENSE BLOCK *****
* This file is part of openfx-supportext <https://github.com/NatronGitHub/openfx-supportext>,
* (C) 2018-2021 The Natron Developers
* (C) 2013-2018 INRIA
*
* openfx-supportext is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* openfx-supportext is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with openfx-supportext. If not, see <http://www.gnu.org/licenses/gpl-2.0.html>
* ***** END LICENSE BLOCK ***** */
/*
* OFX generic rectangle interact with 4 corner points + center point and 4 mid-points.
* You can use it to define any rectangle in an image resizable by the user.
*/
#include "ofxsRectangleInteract.h"
#include <cmath>
#ifdef __APPLE__
#ifndef GL_SILENCE_DEPRECATION
#define GL_SILENCE_DEPRECATION // Yes, we are still doing OpenGL 2.1
#endif
#include <OpenGL/gl.h>
#else
#include <GL/gl.h>
#endif
#define POINT_SIZE 5
#define POINT_TOLERANCE 6
#define CROSS_SIZE 7
#define HANDLE_SIZE 6
using namespace OFX;
using OFX::RectangleInteract;
static bool
isNearby(const OfxPointD & p,
double x,
double y,
double tolerance,
const OfxPointD & pscale)
{
return std::fabs(p.x - x) <= tolerance * pscale.x && std::fabs(p.y - y) <= tolerance * pscale.y;
}
// round to the closest int, 1/10 int, etc
// this make parameter editing easier
// pscale is args.pixelScale.x / args.renderScale.x;
// pscale10 is the power of 10 below pscale
static double
fround(double val,
double pscale)
{
if (pscale == 0) {
return val;
}
double pscale10 = std::pow( 10., std::floor( std::log10(pscale) ) );
return pscale10 * std::floor(val / pscale10 + 0.5);
}
static void
drawPoint(const OfxRGBColourD &color,
bool draw,
double x,
double y,
RectangleInteract::DrawStateEnum id,
RectangleInteract::DrawStateEnum ds,
bool keepAR,
int l)
{
if (draw) {
if (ds == id) {
if (keepAR) {
glColor3f(1.f * l, 0.f * l, 0.f * l);
} else {
glColor3f(0.f * l, 1.f * l, 0.f * l);
}
} else {
glColor3f( (float)color.r * l, (float)color.g * l, (float)color.b * l );
}
glVertex2d(x, y);
}
}
bool
RectangleInteract::draw(const DrawArgs &args)
{
if ( _btmLeft->getIsSecret() || _size->getIsSecret() ||
!_btmLeft->getIsEnable() || !_size->getIsEnable() ||
( _enable && !_enable->getValueAtTime(args.time) ) ) {
return false;
}
bool hiDPI = _hiDPI ? _hiDPI->getValue() : false;
double screenPixelRatio = hiDPI ? 2 : 1;
#ifdef OFX_EXTENSIONS_NATRON
screenPixelRatio *= args.screenPixelRatio;
//hiDPI |= args.screenPixelRatio > 1;
#endif
//TextRenderer::Font font = hiDPI ? TextRenderer::FONT_TIMES_ROMAN_24 : TextRenderer::FONT_HELVETICA_12;
OfxRGBColourD color = { 0.8, 0.8, 0.8 };
getSuggestedColour(color);
const OfxPointD& pscale = args.pixelScale;
GLdouble projection[16];
glGetDoublev( GL_PROJECTION_MATRIX, projection);
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
OfxPointD shadow; // how much to translate GL_PROJECTION to get exactly one pixel on screen
shadow.x = 2. / (projection[0] * viewport[2]);
shadow.y = 2. / (projection[5] * viewport[3]);
double x1, y1, w, h;
if (_mouseState != eMouseStateIdle) {
x1 = _btmLeftDragPos.x;
y1 = _btmLeftDragPos.y;
w = _sizeDrag.x;
h = _sizeDrag.y;
} else {
_btmLeft->getValueAtTime(args.time, x1, y1);
_size->getValueAtTime(args.time, w, h);
}
double x2 = x1 + w;
double y2 = y1 + h;
double xc = x1 + w / 2;
double yc = y1 + h / 2;
const bool keepAR = _modifierStateShift > 0;
const bool centered = _modifierStateCtrl > 0;
//glPushAttrib(GL_ALL_ATTRIB_BITS); // caller is responsible for protecting attribs
aboutToCheckInteractivity(args.time);
glDisable(GL_LINE_STIPPLE);
glEnable(GL_LINE_SMOOTH);
glDisable(GL_POINT_SMOOTH);
glEnable(GL_BLEND);
glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
glLineWidth(1.5f * screenPixelRatio);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Draw everything twice
// l = 0: shadow
// l = 1: drawing
for (int l = 0; l < 2; ++l) {
// shadow (uses GL_PROJECTION)
glMatrixMode(GL_PROJECTION);
int direction = (l == 0) ? 1 : -1;
// translate (1,-1) pixels
glTranslated(direction * shadow.x, -direction * shadow.y, 0);
glMatrixMode(GL_MODELVIEW); // Modelview should be used on Nuke
glColor3f( (float)color.r * l, (float)color.g * l, (float)color.b * l );
glBegin(GL_LINE_LOOP);
glVertex2d(x1, y1);
glVertex2d(x1, y2);
glVertex2d(x2, y2);
glVertex2d(x2, y1);
glEnd();
glPointSize(POINT_SIZE * screenPixelRatio);
glBegin(GL_POINTS);
drawPoint(color, allowBtmLeftInteraction(), x1, y1, eDrawStateHoveringBtmLeft, _drawState, keepAR, l);
drawPoint(color, allowMidLeftInteraction(), x1, yc, eDrawStateHoveringMidLeft, _drawState, false, l);
drawPoint(color, allowTopLeftInteraction(), x1, y2, eDrawStateHoveringTopLeft, _drawState, keepAR, l);
drawPoint(color, allowBtmMidInteraction(), xc, y1, eDrawStateHoveringBtmMid, _drawState, false, l);
drawPoint(color, allowCenterInteraction(), xc, yc, eDrawStateHoveringCenter, _drawState, false, l);
drawPoint(color, allowTopMidInteraction(), xc, y2, eDrawStateHoveringTopMid, _drawState, false, l);
drawPoint(color, allowBtmRightInteraction(), x2, y1, eDrawStateHoveringBtmRight, _drawState, keepAR, l);
drawPoint(color, allowMidRightInteraction(), x2, yc, eDrawStateHoveringMidRight, _drawState, false, l);
drawPoint(color, allowTopRightInteraction(), x2, y2, eDrawStateHoveringTopRight, _drawState, keepAR, l);
glEnd();
glPointSize(1);
///draw center cross hair
glBegin(GL_LINES);
if ( (_drawState == eDrawStateHoveringCenter) || ( centered && (_drawState != eDrawStateInactive) ) ) {
glColor3f(0.f * l, 1.f * l, 0.f * l);
} else if ( !allowCenterInteraction() ) {
glColor3f( (float)(color.r / 2) * l, (float)(color.g / 2) * l, (float)(color.b / 2) * l );
} else {
glColor3f( (float)color.r * l, (float)color.g * l, (float)color.b * l );
}
glVertex2d(xc - CROSS_SIZE * pscale.x, yc);
glVertex2d(xc + CROSS_SIZE * pscale.x, yc);
glVertex2d(xc, yc - CROSS_SIZE * pscale.y);
glVertex2d(xc, yc + CROSS_SIZE * pscale.y);
glEnd();
}
//glPopAttrib();
return true;
} // draw
bool
RectangleInteract::penMotion(const PenArgs &args)
{
if ( _btmLeft->getIsSecret() || _size->getIsSecret() ||
!_btmLeft->getIsEnable() || !_size->getIsEnable() ||
( _enable && !_enable->getValueAtTime(args.time) ) ) {
return false;
}
const OfxPointD& pscale = args.pixelScale;
double x1, y1, w, h;
if (_mouseState != eMouseStateIdle) {
x1 = _btmLeftDragPos.x;
y1 = _btmLeftDragPos.y;
w = _sizeDrag.x;
h = _sizeDrag.y;
} else {
_btmLeft->getValueAtTime(args.time, x1, y1);
_size->getValueAtTime(args.time, w, h);
}
double x2 = x1 + w;
double y2 = y1 + h;
double xc = x1 + w / 2;
double yc = y1 + h / 2;
bool valuesChanged = false;
OfxPointD delta;
delta.x = args.penPosition.x - _lastMousePos.x;
delta.y = args.penPosition.y - _lastMousePos.y;
aboutToCheckInteractivity(args.time);
DrawStateEnum newState = _drawState;
// test center first
if ( isNearby(args.penPosition, xc, yc, POINT_TOLERANCE, pscale) && allowCenterInteraction() ) {
newState = eDrawStateHoveringCenter;
} else if ( isNearby(args.penPosition, x1, y1, POINT_TOLERANCE, pscale) && allowBtmLeftInteraction() ) {
newState = eDrawStateHoveringBtmLeft;
} else if ( isNearby(args.penPosition, x2, y1, POINT_TOLERANCE, pscale) && allowBtmRightInteraction() ) {
newState = eDrawStateHoveringBtmRight;
} else if ( isNearby(args.penPosition, x1, y2, POINT_TOLERANCE, pscale) && allowTopLeftInteraction() ) {
newState = eDrawStateHoveringTopLeft;
} else if ( isNearby(args.penPosition, x2, y2, POINT_TOLERANCE, pscale) && allowTopRightInteraction() ) {
newState = eDrawStateHoveringTopRight;
} else if ( isNearby(args.penPosition, xc, y1, POINT_TOLERANCE, pscale) && allowBtmMidInteraction() ) {
newState = eDrawStateHoveringBtmMid;
} else if ( isNearby(args.penPosition, xc, y2, POINT_TOLERANCE, pscale) && allowTopMidInteraction() ) {
newState = eDrawStateHoveringTopMid;
} else if ( isNearby(args.penPosition, x1, yc, POINT_TOLERANCE, pscale) && allowMidLeftInteraction() ) {
newState = eDrawStateHoveringMidLeft;
} else if ( isNearby(args.penPosition, x2, yc, POINT_TOLERANCE, pscale) && allowMidRightInteraction() ) {
newState = eDrawStateHoveringMidRight;
} else {
newState = eDrawStateInactive;
}
bool redraw = _drawState != newState;
_drawState = newState;
const bool keepAR = _modifierStateShift > 0;
const bool centered = _modifierStateCtrl > 0;
if ( keepAR && (_sizeDrag.x > 0.) && (_sizeDrag.y > 0.) &&
( ( _mouseState == eMouseStateDraggingTopLeft) ||
( _mouseState == eMouseStateDraggingTopRight) ||
( _mouseState == eMouseStateDraggingBtmLeft) ||
( _mouseState == eMouseStateDraggingBtmRight) ) ) {
double r2 = _sizeDrag.x * _sizeDrag.x + _sizeDrag.y * _sizeDrag.y;
if ( (_mouseState == eMouseStateDraggingTopRight) ||
( _mouseState == eMouseStateDraggingBtmLeft) ) {
double dotprod = (delta.x * _sizeDrag.y + delta.y * _sizeDrag.x) / r2;
delta.x = _sizeDrag.x * dotprod;
delta.y = _sizeDrag.y * dotprod;
} else {
double dotprod = (delta.x * _sizeDrag.y - delta.y * _sizeDrag.x) / r2;
delta.x = _sizeDrag.x * dotprod;
delta.y = -_sizeDrag.y * dotprod;
}
}
if (_mouseState == eMouseStateDraggingBtmLeft) {
_drawState = eDrawStateHoveringBtmLeft;
OfxPointD topRight;
topRight.x = _btmLeftDragPos.x + _sizeDrag.x;
topRight.y = _btmLeftDragPos.y + _sizeDrag.y;
_btmLeftDragPos.x += delta.x;
_btmLeftDragPos.y += delta.y;
_sizeDrag.x = topRight.x - _btmLeftDragPos.x;
_sizeDrag.y = topRight.y - _btmLeftDragPos.y;
if (centered) {
_sizeDrag.x -= delta.x;
_sizeDrag.y -= delta.y;
}
valuesChanged = true;
} else if (_mouseState == eMouseStateDraggingTopLeft) {
_drawState = eDrawStateHoveringTopLeft;
OfxPointD btmRight;
btmRight.x = _btmLeftDragPos.x + _sizeDrag.x;
btmRight.y = _btmLeftDragPos.y;
_btmLeftDragPos.x += delta.x;
_sizeDrag.y += delta.y;
_sizeDrag.x = btmRight.x - _btmLeftDragPos.x;
if (centered) {
_sizeDrag.x -= delta.x;
_sizeDrag.y += delta.y;
_btmLeftDragPos.y -= delta.y;
}
valuesChanged = true;
} else if (_mouseState == eMouseStateDraggingTopRight) {
_drawState = eDrawStateHoveringTopRight;
_sizeDrag.x += delta.x;
_sizeDrag.y += delta.y;
if (centered) {
_sizeDrag.x += delta.x;
_btmLeftDragPos.x -= delta.x;
_sizeDrag.y += delta.y;
_btmLeftDragPos.y -= delta.y;
}
valuesChanged = true;
} else if (_mouseState == eMouseStateDraggingBtmRight) {
_drawState = eDrawStateHoveringBtmRight;
OfxPointD topLeft;
topLeft.x = _btmLeftDragPos.x;
topLeft.y = _btmLeftDragPos.y + _sizeDrag.y;
_sizeDrag.x += delta.x;
_btmLeftDragPos.y += delta.y;
_sizeDrag.y = topLeft.y - _btmLeftDragPos.y;
if (centered) {
_sizeDrag.x += delta.x;
_btmLeftDragPos.x -= delta.x;
_sizeDrag.y -= delta.y;
}
valuesChanged = true;
} else if (_mouseState == eMouseStateDraggingTopMid) {
_drawState = eDrawStateHoveringTopMid;
_sizeDrag.y += delta.y;
if (centered) {
_sizeDrag.y += delta.y;
_btmLeftDragPos.y -= delta.y;
}
valuesChanged = true;
} else if (_mouseState == eMouseStateDraggingMidRight) {
_drawState = eDrawStateHoveringMidRight;
_sizeDrag.x += delta.x;
if (centered) {
_sizeDrag.x += delta.x;
_btmLeftDragPos.x -= delta.x;
}
valuesChanged = true;
} else if (_mouseState == eMouseStateDraggingBtmMid) {
_drawState = eDrawStateHoveringBtmMid;
double top = _btmLeftDragPos.y + _sizeDrag.y;
_btmLeftDragPos.y += delta.y;
_sizeDrag.y = top - _btmLeftDragPos.y;
if (centered) {
_sizeDrag.y -= delta.y;
}
valuesChanged = true;
} else if (_mouseState == eMouseStateDraggingMidLeft) {
_drawState = eDrawStateHoveringMidLeft;
double right = _btmLeftDragPos.x + _sizeDrag.x;
_btmLeftDragPos.x += delta.x;
_sizeDrag.x = right - _btmLeftDragPos.x;
if (centered) {
_sizeDrag.x -= delta.x;
}
valuesChanged = true;
} else if (_mouseState == eMouseStateDraggingCenter) {
_drawState = eDrawStateHoveringCenter;
_btmLeftDragPos.x += delta.x;
_btmLeftDragPos.y += delta.y;
valuesChanged = true;
}
//if size is negative shift bottom left
if (_sizeDrag.x < 0) {
if (_mouseState == eMouseStateDraggingBtmLeft) {
_mouseState = eMouseStateDraggingBtmRight;
} else if (_mouseState == eMouseStateDraggingMidLeft) {
_mouseState = eMouseStateDraggingMidRight;
} else if (_mouseState == eMouseStateDraggingTopLeft) {
_mouseState = eMouseStateDraggingTopRight;
} else if (_mouseState == eMouseStateDraggingBtmRight) {
_mouseState = eMouseStateDraggingBtmLeft;
} else if (_mouseState == eMouseStateDraggingMidRight) {
_mouseState = eMouseStateDraggingMidLeft;
} else if (_mouseState == eMouseStateDraggingTopRight) {
_mouseState = eMouseStateDraggingTopLeft;
}
_btmLeftDragPos.x += _sizeDrag.x;
_sizeDrag.x = -_sizeDrag.x;
valuesChanged = true;
}
if (_sizeDrag.y < 0) {
if (_mouseState == eMouseStateDraggingTopLeft) {
_mouseState = eMouseStateDraggingBtmLeft;
} else if (_mouseState == eMouseStateDraggingTopMid) {
_mouseState = eMouseStateDraggingBtmMid;
} else if (_mouseState == eMouseStateDraggingTopRight) {
_mouseState = eMouseStateDraggingBtmRight;
} else if (_mouseState == eMouseStateDraggingBtmLeft) {
_mouseState = eMouseStateDraggingTopLeft;
} else if (_mouseState == eMouseStateDraggingBtmMid) {
_mouseState = eMouseStateDraggingTopMid;
} else if (_mouseState == eMouseStateDraggingBtmRight) {
_mouseState = eMouseStateDraggingTopRight;
}
_btmLeftDragPos.y += _sizeDrag.y;
_sizeDrag.y = -_sizeDrag.y;
valuesChanged = true;
}
///forbid 0 pixels wide crop rectangles
if (_sizeDrag.x < 1) {
_sizeDrag.x = 1;
valuesChanged = true;
}
if (_sizeDrag.y < 1) {
_sizeDrag.y = 1;
valuesChanged = true;
}
if ( (_mouseState != eMouseStateIdle) && _interactiveDrag && valuesChanged ) {
setValue(_btmLeftDragPos, _sizeDrag, args.pixelScale);
// no need to redraw overlay since it is slave to the paramaters
} else if (redraw || valuesChanged) {
requestRedraw();
}
_lastMousePos = args.penPosition;
return valuesChanged;
} // penMotion
bool
RectangleInteract::penDown(const PenArgs &args)
{
if ( _btmLeft->getIsSecret() || _size->getIsSecret() ||
!_btmLeft->getIsEnable() || !_size->getIsEnable() ||
( _enable && !_enable->getValueAtTime(args.time) ) ) {
return false;
}
const OfxPointD& pscale = args.pixelScale;
double x1, y1, w, h;
if (_mouseState != eMouseStateIdle) {
x1 = _btmLeftDragPos.x;
y1 = _btmLeftDragPos.y;
w = _sizeDrag.x;
h = _sizeDrag.y;
} else {
_btmLeft->getValueAtTime(args.time, x1, y1);
_size->getValueAtTime(args.time, w, h);
if ( _interactive && _interactive->getIsEnable() ) {
_interactive->getValueAtTime(args.time, _interactiveDrag);
} else {
_interactiveDrag = false;
}
}
double x2 = x1 + w;
double y2 = y1 + h;
double xc = x1 + w / 2;
double yc = y1 + h / 2;
bool didSomething = false;
aboutToCheckInteractivity(args.time);
// test center first
MouseStateEnum newState = _mouseState;
if ( isNearby(args.penPosition, xc, yc, POINT_TOLERANCE, pscale) && allowCenterInteraction() ) {
newState = eMouseStateDraggingCenter;
} else if ( isNearby(args.penPosition, x1, y1, POINT_TOLERANCE, pscale) && allowBtmLeftInteraction() ) {
newState = eMouseStateDraggingBtmLeft;
} else if ( isNearby(args.penPosition, x2, y1, POINT_TOLERANCE, pscale) && allowBtmRightInteraction() ) {
newState = eMouseStateDraggingBtmRight;
} else if ( isNearby(args.penPosition, x1, y2, POINT_TOLERANCE, pscale) && allowTopLeftInteraction() ) {
newState = eMouseStateDraggingTopLeft;
} else if ( isNearby(args.penPosition, x2, y2, POINT_TOLERANCE, pscale) && allowTopRightInteraction() ) {
newState = eMouseStateDraggingTopRight;
} else if ( isNearby(args.penPosition, xc, y1, POINT_TOLERANCE, pscale) && allowBtmMidInteraction() ) {
newState = eMouseStateDraggingBtmMid;
} else if ( isNearby(args.penPosition, xc, y2, POINT_TOLERANCE, pscale) && allowTopMidInteraction() ) {
newState = eMouseStateDraggingTopMid;
} else if ( isNearby(args.penPosition, x1, yc, POINT_TOLERANCE, pscale) && allowMidLeftInteraction() ) {
newState = eMouseStateDraggingMidLeft;
} else if ( isNearby(args.penPosition, x2, yc, POINT_TOLERANCE, pscale) && allowMidRightInteraction() ) {
newState = eMouseStateDraggingMidRight;
} else {
newState = eMouseStateIdle;
}
didSomething = _mouseState != newState;
_mouseState = newState;
_btmLeftDragPos.x = x1;
_btmLeftDragPos.y = y1;
_sizeDrag.x = w;
_sizeDrag.y = h;
_lastMousePos = args.penPosition;
if (didSomething) {
requestRedraw();
}
return didSomething;
} // penDown
bool
RectangleInteract::penUp(const PenArgs &args)
{
if ( _btmLeft->getIsSecret() || _size->getIsSecret() ||
!_btmLeft->getIsEnable() || !_size->getIsEnable() ||
( _enable && !_enable->getValueAtTime(args.time) ) ) {
return false;
}
bool didSmthing = false;
if ( !_interactiveDrag && (_mouseState != eMouseStateIdle) ) {
// no need to redraw overlay since it is slave to the paramaters
setValue(_btmLeftDragPos, _sizeDrag, args.pixelScale);
didSmthing = true;
} else if (_mouseState != eMouseStateIdle) {
requestRedraw();
}
_mouseState = eMouseStateIdle;
return didSmthing;
} // penUp
// keyDown just updates the modifier state
bool
RectangleInteract::keyDown(const KeyArgs &args)
{
if ( _btmLeft->getIsSecret() || _size->getIsSecret() ||
!_btmLeft->getIsEnable() || !_size->getIsEnable() ||
( _enable && !_enable->getValueAtTime(args.time) ) ) {
return false;
}
// Note that on the Mac:
// cmd/apple/cloverleaf is kOfxKey_Control_L
// ctrl is kOfxKey_Meta_L
// alt/option is kOfxKey_Alt_L
bool mustRedraw = false;
// the two control keys may be pressed consecutively, be aware about this
if ( (args.keySymbol == kOfxKey_Control_L) || (args.keySymbol == kOfxKey_Control_R) ) {
mustRedraw = _modifierStateCtrl == 0;
++_modifierStateCtrl;
}
if ( (args.keySymbol == kOfxKey_Shift_L) || (args.keySymbol == kOfxKey_Shift_R) ) {
mustRedraw = _modifierStateShift == 0;
++_modifierStateShift;
}
if (mustRedraw) {
requestRedraw();
}
//std::cout << std::hex << args.keySymbol << std::endl;
// modifiers are not "caught"
return false;
}
// keyUp just updates the modifier state
bool
RectangleInteract::keyUp(const KeyArgs &args)
{
if ( _btmLeft->getIsSecret() || _size->getIsSecret() ||
!_btmLeft->getIsEnable() || !_size->getIsEnable() ||
( _enable && !_enable->getValueAtTime(args.time) ) ) {
return false;
}
bool mustRedraw = false;
if ( (args.keySymbol == kOfxKey_Control_L) || (args.keySymbol == kOfxKey_Control_R) ) {
// we may have missed a keypress
if (_modifierStateCtrl > 0) {
--_modifierStateCtrl;
mustRedraw = _modifierStateCtrl == 0;
}
}
if ( (args.keySymbol == kOfxKey_Shift_L) || (args.keySymbol == kOfxKey_Shift_R) ) {
if (_modifierStateShift > 0) {
--_modifierStateShift;
mustRedraw = _modifierStateShift == 0;
}
}
if (mustRedraw) {
requestRedraw();
}
// modifiers are not "caught"
return false;
}
/** @brief Called when the interact is loses input focus */
void
RectangleInteract::loseFocus(const FocusArgs & /*args*/)
{
// reset the modifiers state
_modifierStateCtrl = 0;
_modifierStateShift = 0;
_interactiveDrag = false;
}
OfxPointD
RectangleInteract::getBtmLeft(OfxTime time) const
{
OfxPointD ret;
_btmLeft->getValueAtTime(time, ret.x, ret.y);
return ret;
}
void
RectangleInteract::setValue(OfxPointD btmLeft,
OfxPointD size,
const OfxPointD &pscale)
{
bool setBtmLeft = false;
bool setSize = false;
// round newx/y to the closest int, 1/10 int, etc
// this make parameter editing easier
switch (_mouseState) {
case eMouseStateIdle:
break;
case eMouseStateDraggingTopLeft:
btmLeft.x = fround(btmLeft.x, pscale.x);
size.x = fround(size.x, pscale.x);
size.y = fround(size.y, pscale.y);
setBtmLeft = setSize = true;
break;
case eMouseStateDraggingTopRight:
size.x = fround(size.x, pscale.x);
size.y = fround(size.y, pscale.y);
setSize = true;
break;
case eMouseStateDraggingBtmLeft:
btmLeft.x = fround(btmLeft.x, pscale.x);
btmLeft.y = fround(btmLeft.y, pscale.y);
size.x = fround(size.x, pscale.x);
size.y = fround(size.y, pscale.y);
setBtmLeft = setSize = true;
break;
case eMouseStateDraggingBtmRight:
size.x = fround(size.x, pscale.x);
size.y = fround(size.y, pscale.y);
btmLeft.y = fround(btmLeft.y, pscale.y);
setBtmLeft = setSize = true;
break;
case eMouseStateDraggingCenter:
btmLeft.x = fround(btmLeft.x, pscale.x);
btmLeft.y = fround(btmLeft.y, pscale.y);
setBtmLeft = true;
break;
case eMouseStateDraggingTopMid:
size.y = fround(size.y, pscale.y);
setSize = true;
break;
case eMouseStateDraggingMidRight:
size.x = fround(size.x, pscale.x);
setSize = true;
break;
case eMouseStateDraggingBtmMid:
btmLeft.y = fround(btmLeft.y, pscale.y);
setBtmLeft = true;
break;
case eMouseStateDraggingMidLeft:
btmLeft.x = fround(btmLeft.x, pscale.x);
setBtmLeft = true;
break;
}
ImageEffect::EditBlock eb(*_effect, "setRectangle", setBtmLeft + setSize > 1);
_btmLeft->setValue(btmLeft.x, btmLeft.y);
_size->setValue(size.x, size.y);
} // penDown