1 #pragma once
2 
3 #ifndef VECTORSELECTIONTOOL_H
4 #define VECTORSELECTIONTOOL_H
5 
6 #include "selectiontool.h"
7 
8 // TnzTools includes
9 #include "tools/strokeselection.h"
10 #include "tools/levelselection.h"
11 #include "tools/cursors.h"
12 
13 // TnzCore includes
14 #include "tstroke.h"
15 #include "tregion.h"
16 
17 // Qt includes
18 #include <QSet>
19 
20 //============================================================
21 
22 //    Forward declarations
23 
24 class VectorSelectionTool;
25 
26 //============================================================
27 
28 //=============================================================================
29 // Constants / Defines
30 //-----------------------------------------------------------------------------
31 
32 enum SelectionTarget         //! Possible selection targets in a SelectionTool.
33 { NORMAL_TYPE_IDX,           //!< Normal selection (strokes at current frame).
34   SELECTED_FRAMES_TYPE_IDX,  //!< Selection of whole frames.
35   ALL_LEVEL_TYPE_IDX,        //!< Selection of an entire level.
36   SAME_STYLE_TYPE_IDX,       //!< Selected styles at current frame.
37   STYLE_SELECTED_FRAMES_TYPE_IDX,  //!< Selected styles at selected frames.
38   STYLE_LEVEL_TYPE_IDX,  //!< Selected styles at current frame on the whole
39                          //! level.
40   BOUNDARY_TYPE_IDX,     //!< Boundary strokes on current frame.
41   BOUNDARY_SELECTED_FRAMES_TYPE_IDX,  //!< Boundary strokes on selected frames.
42   BOUNDARY_LEVEL_TYPE_IDX,            //!< Boundary strokes on the whole level.
43 };
44 
45 #define NORMAL_TYPE L"Standard"
46 #define SELECTED_FRAMES_TYPE L"Selected Frames"
47 #define ALL_LEVEL_TYPE L"Whole Level"
48 #define SAME_STYLE_TYPE L"Same Style"
49 #define STYLE_SELECTED_FRAMES_TYPE L"Same Style on Selected Frames"
50 #define STYLE_LEVEL_TYPE L"Same Style on Whole Level"
51 #define BOUNDARY_TYPE L"Boundary Strokes"
52 #define BOUNDARY_SELECTED_FRAMES_TYPE L"Boundaries on Selected Frames"
53 #define BOUNDARY_LEVEL_TYPE L"Boundaries on Whole Level"
54 
55 #define ROUNDC_WSTR L"round_cap"
56 #define BUTT_WSTR L"butt_cap"
57 #define PROJECTING_WSTR L"projecting_cap"
58 #define ROUNDJ_WSTR L"round_join"
59 #define BEVEL_WSTR L"bevel_join"
60 #define MITER_WSTR L"miter_join"
61 
62 //=============================================================================
63 // VectorFreeDeformer
64 //-----------------------------------------------------------------------------
65 
66 class VectorFreeDeformer final : public FreeDeformer {
67   TVectorImageP m_vi;
68   std::set<int> m_strokeIndexes;
69   std::vector<TStroke *> m_originalStrokes;
70 
71   bool m_preserveThickness, m_computeRegion, m_flip;
72 
73   TThickPoint deform(TThickPoint point);
74 
75 public:
76   VectorFreeDeformer(TVectorImageP vi, std::set<int> strokeIndexes);
77   ~VectorFreeDeformer();
78 
79   void setPreserveThickness(bool preserveThickness);
80   void setComputeRegion(bool computeRegion);
81   void setFlip(bool flip);
82 
83   /*! Return \b index point, with index from 0 to 3. */
getPoint(int index)84   TPointD getPoint(int index) const { return m_newPoints[index]; }
85   /*! Set \b index point to \b p, with index from 0 to 3. */
86   void setPoint(int index, const TPointD &p) override;
87   /*! Helper function. */
88   void setPoints(const TPointD &p0, const TPointD &p1, const TPointD &p2,
89                  const TPointD &p3) override;
90   /*! Helper function. */
getDeformedImage()91   TVectorImage *getDeformedImage() const { return m_vi.getPointer(); }
92 
93   void deformRegions();
94   void deformImage() override;
95 };
96 
97 //=============================================================================
98 // DragSelectionTool
99 //-----------------------------------------------------------------------------
100 
101 namespace DragSelectionTool {
102 
103 //=============================================================================
104 // UndoChangeStrokes
105 //-----------------------------------------------------------------------------
106 
107 class UndoChangeStrokes final : public ToolUtils::TToolUndo {
108 public:
109   UndoChangeStrokes(TXshSimpleLevel *level, const TFrameId &frameId,
110                     VectorSelectionTool *tool,
111                     const StrokeSelection &selection);
112   UndoChangeStrokes(TXshSimpleLevel *level, const TFrameId &frameId,
113                     VectorSelectionTool *tool, const LevelSelection &selection);
114   ~UndoChangeStrokes();
115 
116   void registerStrokes(bool beforeModify = false);
setFlip(bool value)117   void setFlip(bool value) { m_flip = value; }
118   void transform(const std::vector<TStroke *> &strokes, FourPoints bbox,
119                  TPointD center, DeformValues deformValue) const;
120   void restoreRegions() const;
121   void undo() const override;
122   void redo() const override;
123   int getSize() const override;
124 
125 private:
126   VectorSelectionTool *m_tool;
127 
128   std::vector<TStroke *> m_oldStrokes, m_newStrokes;
129 
130   std::vector<int> m_indexes;  //!< Selected indexes at current frame.
131   std::vector<TFilledRegionInf> m_regionsData;
132 
133   int m_selectionCount;
134 
135   FourPoints m_oldBBox, m_newBBox;
136 
137   TPointD m_oldCenter, m_newCenter;
138 
139   DeformValues m_oldDeformValues, m_newDeformValues;
140 
141   bool m_flip;
142 };
143 
144 //=============================================================================
145 // VectorDeformTool
146 //-----------------------------------------------------------------------------
147 
148 class VectorDeformTool : public DeformTool {
149 public:
150   VectorDeformTool(VectorSelectionTool *tool);
151   ~VectorDeformTool();
152 
153   void applyTransform(FourPoints bbox) override;
154   void addTransformUndo() override;
155 
156   /*! Trasform whole level and add undo. */
157   void transformWholeLevel();
158   bool isFlip();
159 
160 protected:
161   std::unique_ptr<UndoChangeStrokes> m_undo;
162 
163 protected:
leftButtonDrag(const TPointD & pos,const TMouseEvent & e)164   void leftButtonDrag(const TPointD &pos, const TMouseEvent &e) override {}
165 
166   void leftButtonUp(const TPointD &pos, const TMouseEvent &e) override;
167 
draw()168   void draw() override {}
169 
170 private:
171   struct VFDScopedBlock;
172   std::unique_ptr<VFDScopedBlock> m_vfdScopedBlock;
173 };
174 
175 //=============================================================================
176 // VectorRotationTool
177 //-----------------------------------------------------------------------------
178 
179 class VectorRotationTool final : public VectorDeformTool {
180   std::unique_ptr<Rotation> m_rotation;
181 
182 public:
183   VectorRotationTool(VectorSelectionTool *tool);
184 
185   void transform(TAffine aff, double angle) override;
186   void leftButtonDrag(const TPointD &pos, const TMouseEvent &e) override;
187   void draw() override;
188 };
189 
190 //=============================================================================
191 // VectorFreeDeformTool
192 //-----------------------------------------------------------------------------
193 
194 class VectorFreeDeformTool final : public VectorDeformTool {
195   std::unique_ptr<FreeDeform> m_freeDeform;
196 
197 public:
198   VectorFreeDeformTool(VectorSelectionTool *tool);
199 
200   void leftButtonDrag(const TPointD &pos, const TMouseEvent &e) override;
201 };
202 
203 //=============================================================================
204 // VectorMoveSelectionTool
205 //-----------------------------------------------------------------------------
206 
207 class VectorMoveSelectionTool final : public VectorDeformTool {
208   std::unique_ptr<MoveSelection> m_moveSelection;
209 
210 public:
211   VectorMoveSelectionTool(VectorSelectionTool *tool);
212 
213   void transform(TAffine aff) override;
214   void leftButtonDown(const TPointD &pos, const TMouseEvent &e) override;
215   void leftButtonDrag(const TPointD &pos, const TMouseEvent &e) override;
216 };
217 
218 //=============================================================================
219 // VectorScaleTool
220 //-----------------------------------------------------------------------------
221 
222 class VectorScaleTool final : public VectorDeformTool {
223   std::unique_ptr<Scale> m_scale;
224 
225 public:
226   VectorScaleTool(VectorSelectionTool *tool, ScaleType type);
227 
228   TPointD transform(int index,
229                     TPointD newPos) override;  //!< Returns scale value.
230 
231   void leftButtonDown(const TPointD &pos, const TMouseEvent &e) override;
232   void leftButtonDrag(const TPointD &pos, const TMouseEvent &e) override;
233 };
234 
235 //=============================================================================
236 // VectorChangeThicknessTool
237 //-----------------------------------------------------------------------------
238 
239 class VectorChangeThicknessTool final : public DragTool {
240   TPointD m_curPos, m_firstPos;
241   std::map<int, std::vector<double>> m_strokesThickness;
242   double m_thicknessChange;
243 
244   std::unique_ptr<UndoChangeStrokes> m_undo;
245 
246 public:
247   VectorChangeThicknessTool(VectorSelectionTool *tool);
248   ~VectorChangeThicknessTool();
249 
250   void setStrokesThickness(TVectorImage &vi);
setThicknessChange(double value)251   void setThicknessChange(double value) { m_thicknessChange = value; }
252 
253   void changeImageThickness(TVectorImage &vi, double newThickness);
254   void addUndo();
255 
256   void leftButtonDown(const TPointD &pos, const TMouseEvent &e) override;
257   void leftButtonDrag(const TPointD &pos, const TMouseEvent &e) override;
258   void leftButtonUp(const TPointD &pos, const TMouseEvent &e) override;
draw()259   void draw() override {}
260 };
261 
262 }  // namespace DragSelectionTool
263 
264 //=============================================================================
265 // VectorSelectionTool
266 //-----------------------------------------------------------------------------
267 
268 /*!
269   \brief    Selection tool for Toonz's vector images.
270 */
271 
272 class VectorSelectionTool final : public SelectionTool {
273   Q_DECLARE_TR_FUNCTIONS(VectorSelectionTool)
274 
275 public:
276   VectorSelectionTool(int targetType);
277 
278   void setNewFreeDeformer() override;
279 
setCanEnterGroup(bool value)280   void setCanEnterGroup(bool value) { m_canEnterGroup = value; }
281 
isConstantThickness()282   bool isConstantThickness() const override {
283     return m_constantThickness.getValue();
284   }
285   bool isLevelType() const override;
286   bool isSelectedFramesType() const override;
287   bool isSameStyleType() const override;
288   bool isModifiableSelectionType() const override;
289 
selectedStyles()290   const std::set<int> &selectedStyles() const {
291     return m_levelSelection.styles();
292   }
selectedStyles()293   std::set<int> &selectedStyles() { return m_levelSelection.styles(); }
294 
getSelectionCount()295   int getSelectionCount() const { return m_selectionCount; }
296 
297   void selectionOutlineStyle(int &capStyle, int &joinStyle);
298 
strokeSelection()299   const StrokeSelection &strokeSelection() const { return m_strokeSelection; }
300 
levelSelection()301   const LevelSelection &levelSelection() const { return m_levelSelection; }
302 
303   TSelection *getSelection() override;
304   bool isSelectionEmpty() override;
305 
306   void computeBBox() override;
307 
308   TPropertyGroup *getProperties(int targetType) override;
309 
310   bool m_resetCenter;
311 
setResetCenter(bool update)312   void setResetCenter(bool update) { m_resetCenter = update; }
canResetCenter()313   bool canResetCenter() { return m_resetCenter; }
314 
isSelectionEditable()315   bool isSelectionEditable() override { return m_strokeSelection.isEditable(); }
316 
317 protected:
318   void onActivate() override;
319   void onDeactivate() override;
320 
321   void leftButtonDrag(const TPointD &pos, const TMouseEvent &) override;
322   void leftButtonDown(const TPointD &pos, const TMouseEvent &) override;
323   void leftButtonUp(const TPointD &pos, const TMouseEvent &) override;
324   void leftButtonDoubleClick(const TPointD &, const TMouseEvent &e) override;
325   void addContextMenuItems(QMenu *menu) override;
326 
327   void draw() override;
328 
329   void updateAction(TPointD pos, const TMouseEvent &e) override;
330   void onSelectedFramesChanged() override;
331 
332   bool onPropertyChanged(std::string propertyName) override;
333   void onImageChanged() override;
334 
getCursorId()335   int getCursorId() const override {
336     if (m_viewer && m_viewer->getGuidedStrokePickerMode())
337       return m_viewer->getGuidedStrokePickerCursor();
338     return m_cursorId;
339   }
340 
341   bool isDragging() const override;
342 
343 private:
344   class AttachedLevelSelection final : public LevelSelection {
345     StrokeSelection &m_strokeSelection;  //!< Selection of strokes to be seen at
346                                          //! current frame.
347 
348   public:
AttachedLevelSelection(StrokeSelection & strokeSelection)349     AttachedLevelSelection(StrokeSelection &strokeSelection)
350         : m_strokeSelection(strokeSelection) {}
351 
selectNone()352     void selectNone() override {
353       LevelSelection::selectNone(), m_strokeSelection.selectNone();
354     }
enableCommands()355     void enableCommands() override { m_strokeSelection.enableCommands(); }
356   };
357 
358 private:
359   TEnumProperty m_selectionTarget;  //!< Selected target content (strokes, whole
360                                     //! image, styles...).
361   TBoolProperty m_includeIntersection;
362   TBoolProperty m_constantThickness;
363 
364   StrokeSelection m_strokeSelection;        //!< Standard selection of a set of
365                                             //! strokes in current image.
366   AttachedLevelSelection m_levelSelection;  //!< Selection across multiple
367                                             //! frames and specific styles.
368 
369   TEnumProperty m_capStyle;       //!< Style of stroke caps.
370   TEnumProperty m_joinStyle;      //!< Style of stroke joins.
371   TIntProperty m_miterJoinLimit;  //!< Stroke joins threshold value.
372 
373   TPropertyGroup m_outlineProps;
374 
375   int m_selectionCount;  //!< \deprecated  This is \a not related to stroke
376                          //! selections;
377   //!               but rather to bboxes count - and even that is deprecated.
378   bool m_canEnterGroup;  //!< \deprecated  Used in Manga and Kids
379 
380 private:
381   void modifySelectionOnClick(TImageP image, const TPointD &pos,
382                               const TMouseEvent &e) override;
383   bool selectStroke(int index, bool toggle);
384 
385   void doOnActivate() override;
386   void doOnDeactivate() override;
387 
388   void selectRegionVectorImage(bool includeIntersect);
389 
390   void updateTranslation() override;
391 
392   /*! \details  The updateSelectionTarget() function reads the selection
393           target (styles, frames, etc) selected by the user, and ensures
394           that the associated internal selection is configured and
395           \a current.
396 
397           The selected strokes set is \a may be cleared appropriately
398           as a result of a change in target.                                  */
399 
400   void updateSelectionTarget();  //!< Reads widget fields and updates the
401                                  //! selection target accordingly.
402   void
403   clearSelectedStrokes();    //!< Clears strokes visible in current selection.
404   void finalizeSelection();  //!< Makes selection consistent, and invokes all
405                              //! related cached data
406   //!  updates needed for visualization.
407   void drawInLevelType(const TVectorImage &vi);
408   void drawSelectedStrokes(const TVectorImage &vi);
409   void drawGroup(const TVectorImage &vi);
410 };
411 
412 #endif  // VECTORSELECTIONTOOL_H
413