1 /* ***** BEGIN LICENSE BLOCK *****
2  * This file is part of openfx-supportext <https://github.com/devernay/openfx-supportext>,
3  * Copyright (C) 2013-2016 INRIA
4  *
5  * openfx-supportext is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * openfx-supportext is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with openfx-supportext.  If not, see <http://www.gnu.org/licenses/gpl-2.0.html>
17  * ***** END LICENSE BLOCK ***** */
18 
19 /*
20  * OFX TransformInteractCustom.
21  */
22 
23 #ifndef openfx_supportext_ofxsTransformInteractCustom_h
24 #define openfx_supportext_ofxsTransformInteractCustom_h
25 
26 #include <cmath>
27 
28 #include "ofxsImageEffect.h"
29 #include "ofxsMacros.h"
30 
31 #define kParamTransformTranslate "transformTranslate"
32 #define kParamTransformTranslateLabel "Translate"
33 #define kParamTransformRotate "transformRotate"
34 #define kParamTransformRotateLabel "Rotate"
35 #define kParamTransformScale "transformScale"
36 #define kParamTransformScaleLabel "Scale"
37 #define kParamTransformScaleUniform "transformScaleUniform"
38 #define kParamTransformScaleUniformLabel "Uniform"
39 #define kParamTransformScaleUniformHint "Use the X scale for both directions"
40 #define kParamTransformCenter "transformCenter"
41 #define kParamTransformCenterLabel "Center"
42 #define kParamTransformResetCenter "transformResetCenter"
43 #define kParamTransformResetCenterLabel "Reset Center"
44 #define kParamTransformResetCenterHint "Reset the position of the center to the center of the input region of definition"
45 #define kParamTransformInteractCustomOpen "TransformInteractCustomOpen"
46 #define kParamTransformInteractCustomOpenLabel "Show Interact"
47 #define kParamTransformInteractCustomOpenHint "If checked, the transform interact is displayed over the image."
48 #define kParamTransformInteractCustomive "TransformInteractCustomive"
49 #define kParamTransformInteractCustomiveLabel "Interactive Update"
50 #define kParamTransformInteractCustomiveHint "If checked, update the parameter values during interaction with the image viewer, else update the values when pen is released."
51 
52 // old parameter names (Transform DirBlur and GodRays only)
53 #define kParamTransformTranslateOld "translate"
54 #define kParamTransformRotateOld "rotate"
55 #define kParamTransformScaleOld "scale"
56 #define kParamTransformScaleUniformOld "uniform"
57 #define kParamTransformCenterOld "center"
58 #define kParamTransformResetCenterOld "resetCenter"
59 #define kParamTransformInteractCustomiveOld "interactive"
60 
61 namespace OFX {
62 inline void
ofxsTransformGetScale(const OfxPointD & scaleParam,bool scaleUniform,OfxPointD * scale)63 ofxsTransformGetScale(const OfxPointD &scaleParam,
64                       bool scaleUniform,
65                       OfxPointD* scale)
66 {
67     const double SCALE_MIN = 0.0001;
68 
69     scale->x = scaleParam.x;
70     if (std::fabs(scale->x) < SCALE_MIN) {
71         scale->x = (scale->x >= 0) ? SCALE_MIN : -SCALE_MIN;
72     }
73     if (scaleUniform) {
74         scale->y = scaleParam.x;
75     } else {
76         scale->y = scaleParam.y;
77     }
78     if (std::fabs(scale->y) < SCALE_MIN) {
79         scale->y = (scale->y >= 0) ? SCALE_MIN : -SCALE_MIN;
80     }
81 }
82 
83 /// add Transform params. page and group are optional
84 void ofxsTransformDescribeParams(OFX::ImageEffectDescriptor &desc, OFX::PageParamDescriptor *page, OFX::GroupParamDescriptor *group, bool isOpen, bool oldParams, bool noTranslate = false, bool uniform = false, double rotateDefault = 0);
85 
86 class TransformInteractCustomHelper
87     : private OFX::InteractAbstract
88 {
89 protected:
90     enum DrawStateEnum
91     {
92         eInActive = 0, //< nothing happening
93         eCircleHovered, //< the scale circle is hovered
94         eLeftPointHovered, //< the left point of the circle is hovered
95         eRightPointHovered, //< the right point of the circle is hovered
96         eBottomPointHovered, //< the bottom point of the circle is hovered
97         eTopPointHovered, //< the top point of the circle is hovered
98         eCenterPointHovered, //< the center point of the circle is hovered
99         eRotationBarHovered, //< the rotation bar is hovered
100     };
101 
102     enum MouseStateEnum
103     {
104         eReleased = 0,
105         eDraggingCircle,
106         eDraggingLeftPoint,
107         eDraggingRightPoint,
108         eDraggingTopPoint,
109         eDraggingBottomPoint,
110         eDraggingTranslation,
111         eDraggingCenter,
112         eDraggingRotationBar,
113     };
114 
115     enum OrientationEnum
116     {
117         eOrientationAllDirections = 0,
118         eOrientationNotSet,
119         eOrientationHorizontal,
120         eOrientationVertical
121     };
122 
123     DrawStateEnum _drawState;
124     MouseStateEnum _mouseState;
125     int _modifierStateCtrl;
126     int _modifierStateShift;
127     OrientationEnum _orientation;
128     ImageEffect* _effect;
129     Interact* _interact;
130     OfxPointD _lastMousePos;
131     OfxPointD _centerDrag;
132     OfxPointD _translateDrag;
133     OfxPointD _scaleParamDrag;
134     bool _scaleUniformDrag;
135     double _rotateDrag;
136     bool _invertedDrag;
137     bool _interactiveDrag;
138 
139 public:
140     TransformInteractCustomHelper(OFX::ImageEffect* effect, OFX::Interact* interact, bool oldParams = false);
141 
142     /** @brief virtual destructor */
~TransformInteractCustomHelper()143     virtual ~TransformInteractCustomHelper()
144     {
145         // fetched clips and params are owned and deleted by the ImageEffect and its ParamSet
146     }
147 
148     // overridden functions from OFX::Interact to do things
149     virtual bool draw(const OFX::DrawArgs &args) OVERRIDE;
150     virtual bool penMotion(const OFX::PenArgs &args) OVERRIDE;
151     virtual bool penDown(const OFX::PenArgs &args) OVERRIDE;
152     virtual bool penUp(const OFX::PenArgs &args) OVERRIDE;
153     virtual bool keyDown(const OFX::KeyArgs &args) OVERRIDE;
154     virtual bool keyUp(const OFX::KeyArgs &args) OVERRIDE;
keyRepeat(const KeyArgs &)155     virtual bool keyRepeat(const KeyArgs & /*args*/) OVERRIDE { return false; }
156 
gainFocus(const FocusArgs &)157     virtual void gainFocus(const FocusArgs & /*args*/) OVERRIDE {}
158 
159     virtual void loseFocus(const FocusArgs &args) OVERRIDE;
160 
161 private:
162     // NON-GENERIC
163     OFX::Double2DParam* _translate;
164     OFX::DoubleParam* _rotate;
165     OFX::Double2DParam* _scale;
166     OFX::BooleanParam* _scaleUniform;
167     OFX::Double2DParam* _center;
168     OFX::BooleanParam* _invert;
169     OFX::BooleanParam* _interactOpen;
170     OFX::BooleanParam* _interactive;
171 };
172 
173 typedef OverlayInteractFromHelper<TransformInteractCustomHelper> TransformInteractCustom;
174 
175 class TransformOverlayDescriptor
176     : public DefaultEffectOverlayDescriptor<TransformOverlayDescriptor, TransformInteractCustom>
177 {
178 };
179 
180 class TransformInteractCustomHelperOldParams
181     : public TransformInteractCustomHelper
182 {
183 public:
TransformInteractCustomHelperOldParams(OFX::ImageEffect * effect,OFX::Interact * interact)184     TransformInteractCustomHelperOldParams(OFX::ImageEffect* effect,
185                                      OFX::Interact* interact)
186         : TransformInteractCustomHelper(effect, interact, true) {}
187 };
188 
189 typedef OverlayInteractFromHelper<TransformInteractCustomHelperOldParams> TransformInteractCustomOldParams;
190 
191 class TransformOverlayDescriptorOldParams
192     : public DefaultEffectOverlayDescriptor<TransformOverlayDescriptorOldParams, TransformInteractCustomOldParams>
193 {
194 };
195 } // namespace OFX
196 #endif /* defined(openfx_supportext_ofxsTransformInteractCustom_h) */
197