1 
2 
3 // StrokeDeformation.cpp: implementation of the StrokeDeformation class.
4 //
5 //-----------------------------------------------------------------------------
6 #ifdef _DEBUG
7 #define _STLP_DEBUG 1
8 #endif
9 
10 #include "ext/StrokeDeformation.h"
11 #include "ext/SquarePotential.h"
12 #include "ext/StrokeParametricDeformer.h"
13 #include "ext/NotSymmetricBezierPotential.h"
14 #include "ext/SmoothDeformation.h"
15 #include "ext/ContextStatus.h"
16 #include "ext/Designer.h"
17 //#include "ext/TriParam.h"
18 
19 #include "ext/StraightCornerDeformation.h"
20 #include "ext/CornerDeformation.h"
21 
22 #include "ext/StrokeDeformationImpl.h"
23 #include "DeformationSelector.h"
24 
25 #include <tcurves.h>
26 #include <tstrokeutil.h>
27 #include <tmathutil.h>
28 
29 #include <algorithm>
30 #include <iterator>
31 #include <vector>
32 
33 #include "tthreadmessage.h"
34 
35 using namespace ToonzExt;
36 using namespace std;
37 
38 /**
39  * @brief Manage a mutable context.
40  */
41 static TThread::Mutex s_mutex;
42 
43 //-----------------------------------------------------------------------------
44 
StrokeDeformation()45 StrokeDeformation::StrokeDeformation() {
46   state_           = CREATED;
47   deformationImpl_ = SmoothDeformation::instance();
48 }
49 
50 //-----------------------------------------------------------------------------
51 
~StrokeDeformation()52 StrokeDeformation::~StrokeDeformation() {}
53 
54 //-----------------------------------------------------------------------------
55 
activate(const ContextStatus * status)56 void StrokeDeformation::activate(const ContextStatus *status) {
57   QMutexLocker sl(&s_mutex);
58 
59   assert(status && "Status is null!!!");
60   if (!status) return;
61 
62   if (state_ == RESETTED) delete this->deactivate();
63 
64   if (state_ == CREATED || state_ == DEACTIVE) {
65     deformationImpl_ = this->retrieveDeformator(status);
66   } else {
67     // if it is a not valid entry state_ recover
68     assert(deformationImpl_ && "Can not find a valid deformator!!!");
69     if (!deformationImpl_) return;
70     deformationImpl_->reset();
71     deformationImpl_ = DeformationSelector::instance()->getDeformation(status);
72   }
73 
74   assert(deformationImpl_ && "Deformation is null!!!");
75   if (!deformationImpl_) return;
76 
77   if (!deformationImpl_->activate_impl(status)) {
78     deformationImpl_->reset();
79     state_ = DEACTIVE;
80     return;
81   }
82 
83   state_ = ACTIVE;
84 }
85 
86 //-----------------------------------------------------------------------------
87 
update(const TPointD & delta)88 void StrokeDeformation::update(const TPointD &delta) {
89   QMutexLocker sl(&s_mutex);
90 
91   assert(deformationImpl_ && "Deformation is null!!!");
92   if (!deformationImpl_) {
93     state_ = DEACTIVE;
94     return;
95   }
96 
97   if (state_ != ACTIVE && state_ != UPDATING) {
98     deformationImpl_->reset();
99     state_ = ACTIVE;
100     return;
101   }
102 
103   deformationImpl_->update_impl(delta);
104   state_ = UPDATING;
105 }
106 
107 //-----------------------------------------------------------------------------
108 
deactivate()109 TStroke *StrokeDeformation::deactivate() {
110   QMutexLocker sl(&s_mutex);
111 
112   if (!deformationImpl_) {
113     state_ = DEACTIVE;
114     return 0;
115   }
116 
117   if (state_ != ACTIVE && state_ != UPDATING) {
118     state_ = DEACTIVE;
119     deformationImpl_->reset();
120     return 0;
121   }
122 
123   state_           = DEACTIVE;
124   TStroke *out     = deformationImpl_->deactivate_impl();
125   deformationImpl_ = 0;
126   return out;
127 }
128 
129 //-----------------------------------------------------------------------------
130 
reset()131 void StrokeDeformation::reset() {
132   QMutexLocker sl(&s_mutex);
133 
134   state_ = RESETTED;
135 }
136 
137 //-----------------------------------------------------------------------------
138 
draw(Designer * designer)139 void StrokeDeformation::draw(Designer *designer) {
140   QMutexLocker sl(&s_mutex);
141 
142   assert(designer && "Designer is null!!!");
143   if (!deformationImpl_ || !designer) return;
144 
145   // this is to draw the icon
146   deformationImpl_->draw(designer);
147 
148   // this is to draw stroke when change something
149   designer->draw(this);
150 }
151 
152 //-----------------------------------------------------------------------------
153 
check(const ContextStatus * status)154 void StrokeDeformation::check(const ContextStatus *status) {
155   QMutexLocker sl(&s_mutex);
156   if (state_ != UPDATING) deformationImpl_ = this->retrieveDeformator(status);
157 }
158 
159 //-----------------------------------------------------------------------------
160 
getCopiedStroke() const161 const TStroke *StrokeDeformation::getCopiedStroke() const {
162   QMutexLocker sl(&s_mutex);
163   if (deformationImpl_) return deformationImpl_->getCopiedStroke();
164   return 0;
165 }
166 
167 //-----------------------------------------------------------------------------
168 
getStroke() const169 const TStroke *StrokeDeformation::getStroke() const {
170   QMutexLocker sl(&s_mutex);
171   if (deformationImpl_) return deformationImpl_->getStrokeSelected();
172   return 0;
173 }
174 
175 //-----------------------------------------------------------------------------
176 
getTransformedStroke() const177 const TStroke *StrokeDeformation::getTransformedStroke() const {
178   QMutexLocker sl(&s_mutex);
179   if (deformationImpl_) return deformationImpl_->getTransformedStroke();
180   return 0;
181 }
182 
183 //-----------------------------------------------------------------------------
184 
recover()185 void StrokeDeformation::recover() {
186   QMutexLocker sl(&s_mutex);
187   assert(deformationImpl_ && "Deformation is null!!!");
188   if (!deformationImpl_) return;
189 
190   deformationImpl_->reset();
191   //  this->clearStatus();
192 
193   // deformationImpl_ = DeformationSelector::instance()->getDeformation(status);
194   // assert(deformationImpl_ &&  "Deformation is null!!!" );
195   // if( !deformationImpl_ )
196   //  return;
197   // deformationImpl_->activate_impl(status);
198 }
199 
200 //-----------------------------------------------------------------------------
201 
getStatus() const202 const ContextStatus *StrokeDeformation::getStatus() const {
203   QMutexLocker sl(&s_mutex);
204   if (deformationImpl_) return deformationImpl_->getImplStatus();
205   return 0;
206 }
207 
208 //-----------------------------------------------------------------------------
209 
getExtremes() const210 ToonzExt::Interval StrokeDeformation::getExtremes() const {
211   QMutexLocker sl(&s_mutex);
212   if (!deformationImpl_) return Interval(-1, -1);
213   return deformationImpl_->getExtremes();
214 }
215 
216 //-----------------------------------------------------------------------------
217 
retrieveDeformator(const ContextStatus * status)218 StrokeDeformationImpl *StrokeDeformation::retrieveDeformator(
219     const ContextStatus *status) {
220   QMutexLocker sl(&s_mutex);
221   return DeformationSelector::instance()->getDeformation(status);
222 }
223 
224 //-----------------------------------------------------------------------------
225 
getCursorId() const226 int StrokeDeformation::getCursorId() const {
227   QMutexLocker sl(&s_mutex);
228   if (!deformationImpl_) return -1;
229   return deformationImpl_->getCursorId();
230 }
231 
232 //-----------------------------------------------------------------------------
233 
234 #ifdef _DEBUG
235 
getPotential() const236 const Potential *StrokeDeformation::getPotential() const {
237   assert(deformationImpl_ && "Deformation is null!!!");
238   if (!deformationImpl_) return 0;
239   return deformationImpl_->getPotential();
240 }
241 
242 //-----------------------------------------------------------------------------
243 
getDeformationImpl() const244 const StrokeDeformationImpl *StrokeDeformation::getDeformationImpl() const {
245   return deformationImpl_;
246 }
247 #endif
248 
249 //-----------------------------------------------------------------------------
250 //  End Of File
251 //-----------------------------------------------------------------------------
252