-
Notifications
You must be signed in to change notification settings - Fork 281
/
Copy pathtoolmanager.cpp
425 lines (361 loc) · 11 KB
/
toolmanager.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
/*
Pencil2D - Traditional Animation Software
Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
Copyright (C) 2012-2020 Matthew Chiawen Chang
This program 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; version 2 of the License.
This program 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.
*/
#include "toolmanager.h"
#include <cmath>
#include "pentool.h"
#include "penciltool.h"
#include "brushtool.h"
#include "buckettool.h"
#include "erasertool.h"
#include "eyedroppertool.h"
#include "handtool.h"
#include "movetool.h"
#include "polylinetool.h"
#include "selecttool.h"
#include "smudgetool.h"
#include "cameratool.h"
#include "editor.h"
ToolManager::ToolManager(Editor* editor) : BaseManager(editor, __FUNCTION__)
{
}
bool ToolManager::init()
{
mToolSetHash.insert(PEN, new PenTool(this));
mToolSetHash.insert(PENCIL, new PencilTool(this));
mToolSetHash.insert(BRUSH, new BrushTool(this));
mToolSetHash.insert(ERASER, new EraserTool(this));
mToolSetHash.insert(BUCKET, new BucketTool(this));
mToolSetHash.insert(EYEDROPPER, new EyedropperTool(this));
mToolSetHash.insert(HAND, new HandTool(this));
mToolSetHash.insert(MOVE, new MoveTool(this));
mToolSetHash.insert(POLYLINE, new PolylineTool(this));
mToolSetHash.insert(SELECT, new SelectTool(this));
mToolSetHash.insert(SMUDGE, new SmudgeTool(this));
mToolSetHash.insert(CAMERA, new CameraTool(this));
foreach(BaseTool* pTool, mToolSetHash.values())
{
pTool->initialize(editor());
}
setDefaultTool();
return true;
}
Status ToolManager::load(Object*)
{
setDefaultTool();
return Status::OK;
}
Status ToolManager::save(Object*)
{
return Status::OK;
}
BaseTool* ToolManager::currentTool() const
{
if (mTemporaryTool != nullptr)
{
return mTemporaryTool;
}
else if (mTabletEraserTool != nullptr)
{
return mTabletEraserTool;
}
return mCurrentTool;
}
BaseTool* ToolManager::getTool(ToolType eToolType)
{
return mToolSetHash[eToolType];
}
void ToolManager::setDefaultTool()
{
// Set default tool
// (called by the main window init)
ToolType defaultToolType = PENCIL;
setCurrentTool(defaultToolType);
mTabletEraserTool = nullptr;
mTemporaryTool = nullptr;
}
void ToolManager::setCurrentTool(ToolType eToolType)
{
// We're already using this tool
if (mCurrentTool == getTool(eToolType)) { return; }
if (mCurrentTool != nullptr)
{
mCurrentTool->leavingThisTool();
}
mCurrentTool = getTool(eToolType);
if (mTemporaryTool == nullptr && mTabletEraserTool == nullptr)
{
emit toolChanged(eToolType);
}
}
bool ToolManager::leavingThisTool()
{
return currentTool()->leavingThisTool();
}
void ToolManager::cleanupAllToolsData()
{
foreach(BaseTool* tool, mToolSetHash)
{
tool->clearToolData();
}
}
void ToolManager::resetAllTools()
{
// Reset can be useful to solve some pencil settings problems.
// Beta-testers should be recommended to reset before sending tool related issues.
// This can prevent from users to stop working on their project.
foreach(BaseTool* tool, mToolSetHash)
{
tool->resetToDefault();
}
qDebug("tools restored to default settings");
}
void ToolManager::setWidth(float newWidth)
{
if (std::isnan(newWidth) || newWidth < 0)
{
newWidth = 1.f;
}
currentTool()->setWidth(static_cast<qreal>(newWidth));
emit penWidthValueChanged(newWidth);
emit toolPropertyChanged(currentTool()->type(), WIDTH);
}
void ToolManager::setFeather(float newFeather)
{
if (std::isnan(newFeather) || newFeather < 0)
{
newFeather = 0.f;
}
currentTool()->setFeather(static_cast<qreal>(newFeather));
emit penFeatherValueChanged(newFeather);
emit toolPropertyChanged(currentTool()->type(), FEATHER);
}
void ToolManager::setUseFeather(bool usingFeather)
{
int usingAA = currentTool()->properties.useAA;
int value = propertySwitch(usingFeather, usingAA);
currentTool()->setAA(value);
currentTool()->setUseFeather(usingFeather);
emit toolPropertyChanged(currentTool()->type(), USEFEATHER);
emit toolPropertyChanged(currentTool()->type(), ANTI_ALIASING);
}
void ToolManager::setInvisibility(bool isInvisible)
{
currentTool()->setInvisibility(isInvisible);
emit toolPropertyChanged(currentTool()->type(), INVISIBILITY);
}
void ToolManager::setPreserveAlpha(bool isPreserveAlpha)
{
currentTool()->setPreserveAlpha(isPreserveAlpha);
emit toolPropertyChanged(currentTool()->type(), PRESERVEALPHA);
}
void ToolManager::setVectorMergeEnabled(bool isVectorMergeEnabled)
{
currentTool()->setVectorMergeEnabled(isVectorMergeEnabled);
emit toolPropertyChanged(currentTool()->type(), VECTORMERGE);
}
void ToolManager::setBezier(bool isBezierOn)
{
currentTool()->setBezier(isBezierOn);
emit toolPropertyChanged(currentTool()->type(), BEZIER);
}
void ToolManager::setPressure(bool isPressureOn)
{
currentTool()->setPressure(isPressureOn);
emit toolPropertyChanged(currentTool()->type(), PRESSURE);
}
void ToolManager::setAA(int usingAA)
{
currentTool()->setAA(usingAA);
emit toolPropertyChanged(currentTool()->type(), ANTI_ALIASING);
}
void ToolManager::setFillMode(int mode)
{
currentTool()->setFillMode(mode);
emit toolPropertyChanged(currentTool()->type(), FILL_MODE);
}
void ToolManager::setStabilizerLevel(int level)
{
currentTool()->setStabilizerLevel(level);
emit toolPropertyChanged(currentTool()->type(), STABILIZATION);
}
void ToolManager::setTolerance(int newTolerance)
{
newTolerance = qMax(0, newTolerance);
currentTool()->setTolerance(newTolerance);
emit toleranceValueChanged(newTolerance);
emit toolPropertyChanged(currentTool()->type(), TOLERANCE);
}
void ToolManager::setBucketColorToleranceEnabled(bool enabled)
{
currentTool()->setToleranceEnabled(enabled);
emit toolPropertyChanged(currentTool()->type(), USETOLERANCE);
}
void ToolManager::setBucketFillExpandEnabled(bool expandValue)
{
currentTool()->setFillExpandEnabled(expandValue);
emit toolPropertyChanged(currentTool()->type(), USEBUCKETFILLEXPAND);
}
void ToolManager::setBucketFillExpand(int expandValue)
{
currentTool()->setFillExpand(expandValue);
emit toolPropertyChanged(currentTool()->type(), BUCKETFILLEXPAND);
}
void ToolManager::setBucketFillReferenceMode(int referenceMode)
{
currentTool()->setFillReferenceMode(referenceMode);
emit toolPropertyChanged(currentTool()->type(), BUCKETFILLLAYERREFERENCEMODE);
}
void ToolManager::setUseFillContour(bool useFillContour)
{
currentTool()->setUseFillContour(useFillContour);
emit toolPropertyChanged(currentTool()->type(), FILLCONTOUR);
}
void ToolManager::setAutoSwitchTool(bool autoSwitch)
{
currentTool()->setAutoSwitchTool(autoSwitch);
}
void ToolManager::setShowSelectionInfo(bool b)
{
currentTool()->setShowSelectionInfo(b);
}
void ToolManager::setShowCameraPath(bool enabled)
{
CameraTool* cameraTool = static_cast<CameraTool*>(getTool(CAMERA));
cameraTool->setShowCameraPath(enabled);
emit toolPropertyChanged(cameraTool->type(), CAMERAPATH);
}
void ToolManager::resetCameraPath()
{
CameraTool* cameraTool = static_cast<CameraTool*>(getTool(CAMERA));
cameraTool->resetCameraPath();
emit toolPropertyChanged(cameraTool->type(), CAMERAPATH);
}
void ToolManager::resetCameraTransform(CameraFieldOption option)
{
CameraTool* cameraTool = static_cast<CameraTool*>(getTool(CAMERA));
cameraTool->resetTransform(option);
}
void ToolManager::setCameraPathDotColor(int dotColorNum)
{
CameraTool* cameraTool = static_cast<CameraTool*>(getTool(CAMERA));
cameraTool->setPathDotColorType(static_cast<DotColorType>(dotColorNum));
emit toolPropertyChanged(cameraTool->type(), CAMERAPATH);
}
bool ToolManager::bucketReferenceModeIsCurrentLayer(int referenceMode) const
{
return referenceMode == 0;
}
// Switches on/off two actions
// eg. if x = true, then y = false
int ToolManager::propertySwitch(bool condition, int tool)
{
int value = 0;
int newValue = 0;
if (condition == true)
{
value = -1;
newValue = mOldValue;
mOldValue = tool;
}
else if (condition == false)
{
value = (newValue == 1) ? 1 : mOldValue;
}
return value;
}
void ToolManager::tabletSwitchToEraser()
{
mTabletEraserTool = getTool(ERASER);
// We should only notify a tool change if we're positive that the state has changed and it should only happen once
// if the user for some reason is using another temporary tool at the same time, that takes first priority
if (mTemporaryTool == nullptr)
{
emit toolChanged(ERASER);
}
}
void ToolManager::tabletRestorePrevTool()
{
if (mTemporaryTool == nullptr && mTabletEraserTool != nullptr)
{
mTabletEraserTool = nullptr;
emit toolChanged(currentTool()->type());
}
}
bool ToolManager::setTemporaryTool(ToolType eToolType, QFlags<Qt::Key> keys, Qt::KeyboardModifiers modifiers)
{
if (mTemporaryTool != nullptr) return false;
mTemporaryTriggerKeys = keys;
mTemporaryTriggerModifiers = modifiers;
mTemporaryTriggerMouseButtons = Qt::NoButton;
setTemporaryTool(eToolType);
return true;
}
bool ToolManager::setTemporaryTool(ToolType eToolType, Qt::MouseButtons buttons)
{
if (mTemporaryTool != nullptr) return false;
mTemporaryTriggerKeys = {};
mTemporaryTriggerModifiers = Qt::NoModifier;
mTemporaryTriggerMouseButtons = buttons;
setTemporaryTool(eToolType);
return true;
}
bool ToolManager::tryClearTemporaryTool(Qt::Key key)
{
Qt::KeyboardModifier modifier = Qt::NoModifier;
switch(key)
{
case Qt::Key_Control:
modifier = Qt::ControlModifier;
break;
case Qt::Key_Shift:
modifier = Qt::ShiftModifier;
break;
case Qt::Key_Alt:
modifier = Qt::AltModifier;
break;
case Qt::Key_Meta:
modifier = Qt::MetaModifier;
break;
default:
break;
}
if (mTemporaryTriggerKeys.testFlag(key) ||
mTemporaryTriggerModifiers.testFlag(modifier))
{
clearTemporaryTool();
return true;
}
return false;
}
bool ToolManager::tryClearTemporaryTool(Qt::MouseButton button)
{
if (mTemporaryTriggerMouseButtons != Qt::NoButton && mTemporaryTriggerMouseButtons.testFlag(button))
{
clearTemporaryTool();
return true;
}
return false;
}
void ToolManager::setTemporaryTool(ToolType eToolType)
{
mTemporaryTool = getTool(eToolType);
emit toolChanged(eToolType);
}
void ToolManager::clearTemporaryTool()
{
mTemporaryTool = nullptr;
mTemporaryTriggerKeys = {};
mTemporaryTriggerModifiers = Qt::NoModifier;
mTemporaryTriggerMouseButtons = Qt::NoButton;
emit toolChanged(currentTool()->type());
}