Pencil2D Animation
Download Community News Docs Contribute
  • Overview
  • Articles
  • Code
  •  
  • Class List
  • Class Index
  • Class Hierarchy
  • Class Members
  • File List
Loading...
Searching...
No Matches
  • core_lib
  • src
  • tool
penciltool.cpp
1/*
2
3Pencil2D - Traditional Animation Software
4Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5Copyright (C) 2012-2020 Matthew Chiawen Chang
6
7This program is free software; you can redistribute it and/or
8modify it under the terms of the GNU General Public License
9as published by the Free Software Foundation; version 2 of the License.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16*/
17#include "penciltool.h"
18
19#include <QSettings>
20#include <QPixmap>
21#include "pointerevent.h"
22
23#include "layermanager.h"
24#include "colormanager.h"
25#include "viewmanager.h"
26#include "preferencemanager.h"
27#include "selectionmanager.h"
28#include "undoredomanager.h"
29
30#include "editor.h"
31#include "scribblearea.h"
32#include "layervector.h"
33#include "vectorimage.h"
34
35
36PencilTool::PencilTool(QObject* parent) : StrokeTool(parent)
37{
38}
39
40void PencilTool::loadSettings()
41{
42 StrokeTool::loadSettings();
43
44 mPropertyEnabled[WIDTH] = true;
45 mPropertyEnabled[PRESSURE] = true;
46 mPropertyEnabled[VECTORMERGE] = false;
47 mPropertyEnabled[STABILIZATION] = true;
48 mPropertyEnabled[FILLCONTOUR] = true;
49
50 QSettings settings(PENCIL2D, PENCIL2D);
51 properties.width = settings.value("pencilWidth", 4).toDouble();
52 properties.feather = 50;
53 properties.pressure = settings.value("pencilPressure", true).toBool();
54 properties.stabilizerLevel = settings.value("pencilLineStabilization", StabilizationLevel::STRONG).toInt();
55 properties.useAA = DISABLED;
56 properties.useFillContour = false;
57
58 mQuickSizingProperties.insert(Qt::ShiftModifier, WIDTH);
59}
60
61void PencilTool::saveSettings()
62{
63 QSettings settings(PENCIL2D, PENCIL2D);
64
65 settings.setValue("pencilWidth", properties.width);
66 settings.setValue("pencilPressure", properties.pressure);
67 settings.setValue("brushUseFeather", properties.useFeather);
68 settings.setValue("pencilLineStabilization", properties.stabilizerLevel);
69 settings.setValue("FillContour", properties.useFillContour);
70
71 settings.sync();
72}
73
74void PencilTool::resetToDefault()
75{
76 setWidth(4.0);
77 setFeather(50);
78 setUseFeather(false);
79 setStabilizerLevel(StabilizationLevel::STRONG);
80}
81
82void PencilTool::setWidth(const qreal width)
83{
84 // Set current property
85 properties.width = width;
86}
87
88void PencilTool::setFeather(const qreal feather)
89{
90 properties.feather = feather;
91}
92
93void PencilTool::setUseFeather(const bool usingFeather)
94{
95 // Set current property
96 properties.useFeather = usingFeather;
97
98}
99
100void PencilTool::setInvisibility(const bool)
101{
102 // force value
103 properties.invisibility = 1;
104}
105
106void PencilTool::setPressure(const bool pressure)
107{
108 // Set current property
109 properties.pressure = pressure;
110}
111
112void PencilTool::setPreserveAlpha(const bool preserveAlpha)
113{
114 // force value
115 Q_UNUSED( preserveAlpha )
116 properties.preserveAlpha = 0;
117}
118
119void PencilTool::setStabilizerLevel(const int level)
120{
121 properties.stabilizerLevel = level;
122}
123
124void PencilTool::setUseFillContour(const bool useFillContour)
125{
126 properties.useFillContour = useFillContour;
127}
128
129QCursor PencilTool::cursor()
130{
131 if (mEditor->preference()->isOn(SETTING::TOOL_CURSOR))
132 {
133 return QCursor(QPixmap(":icons/general/cursor-pencil.svg"), 4, 14);
134 }
135 return QCursor(QPixmap(":icons/general/cross.png"), 10, 10);
136}
137
138void PencilTool::pointerPressEvent(PointerEvent *event)
139{
140 mInterpolator.pointerPressEvent(event);
141 if (handleQuickSizing(event)) {
142 return;
143 }
144
145 mMouseDownPoint = getCurrentPoint();
146 mLastBrushPoint = getCurrentPoint();
147
148 startStroke(event->inputType());
149
150 // note: why are we doing this on device press event?
151 if (mEditor->layers()->currentLayer()->type() == Layer::VECTOR && !mEditor->preference()->isOn(SETTING::INVISIBLE_LINES))
152 {
153 mScribbleArea->toggleThinLines();
154 }
155
156 StrokeTool::pointerPressEvent(event);
157}
158
159void PencilTool::pointerMoveEvent(PointerEvent* event)
160{
161 mInterpolator.pointerMoveEvent(event);
162 if (handleQuickSizing(event)) {
163 return;
164 }
165
166 if (event->buttons() & Qt::LeftButton && event->inputType() == mCurrentInputType)
167 {
168 mCurrentPressure = mInterpolator.getPressure();
169 drawStroke();
170 if (properties.stabilizerLevel != mInterpolator.getStabilizerLevel())
171 {
172 mInterpolator.setStabilizerLevel(properties.stabilizerLevel);
173 }
174 }
175 StrokeTool::pointerMoveEvent(event);
176}
177
178void PencilTool::pointerReleaseEvent(PointerEvent *event)
179{
180 mInterpolator.pointerReleaseEvent(event);
181 if (handleQuickSizing(event)) {
182 return;
183 }
184
185 if (event->inputType() != mCurrentInputType) return;
186
187 mEditor->backup(typeName());
188 qreal distance = QLineF(getCurrentPoint(), mMouseDownPoint).length();
189 if (distance < 1)
190 {
191 paintAt(mMouseDownPoint);
192 }
193 else
194 {
195 drawStroke();
196 }
197
198 Layer* layer = mEditor->layers()->currentLayer();
199 if (layer->type() == Layer::VECTOR) {
200 paintVectorStroke(layer);
201 }
202 endStroke();
203
204 StrokeTool::pointerReleaseEvent(event);
205}
206
207// draw a single paint dab at the given location
208void PencilTool::paintAt(QPointF point)
209{
210 //qDebug() << "Made a single dab at " << point;
211 Layer* layer = mEditor->layers()->currentLayer();
212 if (layer->type() == Layer::BITMAP)
213 {
214 qreal opacity = (properties.pressure) ? (mCurrentPressure * 0.5) : 1.0;
215 qreal pressure = (properties.pressure) ? mCurrentPressure : 1.0;
216 qreal brushWidth = properties.width * pressure;
217 qreal fixedBrushFeather = properties.feather;
218
219 mCurrentWidth = brushWidth;
220 mScribbleArea->drawPencil(point,
221 brushWidth,
222 fixedBrushFeather,
223 mEditor->color()->frontColor(),
224 opacity);
225 }
226}
227
228
229void PencilTool::drawStroke()
230{
231 StrokeTool::drawStroke();
232 QList<QPointF> p = mInterpolator.interpolateStroke();
233
234 Layer* layer = mEditor->layers()->currentLayer();
235
236 if (layer->type() == Layer::BITMAP)
237 {
238 qreal pressure = (properties.pressure) ? mCurrentPressure : 1.0;
239 qreal opacity = (properties.pressure) ? (mCurrentPressure * 0.5) : 1.0;
240 qreal brushWidth = properties.width * pressure;
241 mCurrentWidth = brushWidth;
242
243 qreal fixedBrushFeather = properties.feather;
244 qreal brushStep = qMax(1.0, (0.5 * brushWidth));
245
246 QPointF a = mLastBrushPoint;
247 QPointF b = getCurrentPoint();
248
249 qreal distance = 4 * QLineF(b, a).length();
250 int steps = qRound(distance / brushStep);
251
252 for (int i = 0; i < steps; i++)
253 {
254 QPointF point = mLastBrushPoint + (i + 1) * brushStep * (getCurrentPoint() - mLastBrushPoint) / distance;
255 mScribbleArea->drawPencil(point,
256 brushWidth,
257 fixedBrushFeather,
258 mEditor->color()->frontColor(),
259 opacity);
260
261 if (i == (steps - 1))
262 {
263 mLastBrushPoint = getCurrentPoint();
264 }
265 }
266 }
267 else if (layer->type() == Layer::VECTOR)
268 {
269 mCurrentWidth = 0; // FIXME: WTF?
270 QPen pen(mEditor->color()->frontColor(),
271 1,
272 Qt::DotLine,
273 Qt::RoundCap,
274 Qt::RoundJoin);
275
276 if (p.size() == 4)
277 {
278 QPainterPath path(p[0]);
279 path.cubicTo(p[1],
280 p[2],
281 p[3]);
282 mScribbleArea->drawPath(path, pen, Qt::NoBrush, QPainter::CompositionMode_Source);
283 }
284 }
285}
286
287void PencilTool::paintVectorStroke(Layer* layer)
288{
289 if (mStrokePoints.empty())
290 return;
291
292 // Clear the temporary pixel path
293 mScribbleArea->clearDrawingBuffer();
294 qreal tol = mScribbleArea->getCurveSmoothing() / mEditor->view()->scaling();
295
296 BezierCurve curve(mStrokePoints, mStrokePressures, tol);
297 curve.setWidth(0);
298 curve.setFeather(0);
299 curve.setFilled(false);
300 curve.setInvisibility(true);
301 curve.setVariableWidth(false);
302 curve.setColorNumber(mEditor->color()->frontColorNumber());
303 VectorImage* vectorImage = static_cast<LayerVector*>(layer)->getLastVectorImageAtFrame(mEditor->currentFrame(), 0);
304 if (vectorImage == nullptr) { return; } // Can happen if the first frame is deleted while drawing
305 vectorImage->addCurve(curve, qAbs(mEditor->view()->scaling()), properties.vectorMergeEnabled);
306
307 if (properties.useFillContour)
308 {
309 vectorImage->fillContour(mStrokePoints,
310 mEditor->color()->frontColorNumber());
311 }
312
313 if (vectorImage->isAnyCurveSelected() || mEditor->select()->somethingSelected())
314 {
315 mEditor->deselectAll();
316 }
317
318 // select last/newest curve
319 vectorImage->setSelected(vectorImage->getLastCurveNumber(), true);
320
321 // TODO: selection doesn't apply on enter
322
323 mEditor->setModified(mEditor->layers()->currentLayerIndex(), mEditor->currentFrame());
324}
BezierCurve
Definition: beziercurve.h:34
ColorManager::frontColor
QColor frontColor(bool useIndexedColor=true)
frontColor
Definition: colormanager.cpp:61
Layer
Definition: layer.h:33
LayerVector
Definition: layervector.h:26
PointerEvent
Definition: pointerevent.h:8
StrokeTool
Definition: stroketool.h:34
StrokeTool::loadSettings
void loadSettings() override
Definition: stroketool.cpp:58
VectorImage
Definition: vectorimage.h:32
VectorImage::setSelected
void setSelected(int curveNumber, bool YesOrNo)
VectorImage::setSelected.
Definition: vectorimage.cpp:616
VectorImage::getLastCurveNumber
int getLastCurveNumber()
VectorImage::getLastCurveNumber.
Definition: vectorimage.cpp:2264
VectorImage::addCurve
void addCurve(BezierCurve &newCurve, qreal factor, bool interacts=true)
VectorImage::addCurve.
Definition: vectorimage.cpp:341
VectorImage::fillContour
void fillContour(QList< QPointF > contourPath, int color)
VectorImage::fillContour.
Definition: vectorimage.cpp:1829
VectorImage::isAnyCurveSelected
bool isAnyCurveSelected()
VectorImage::isAnyCurveSelected.
Definition: vectorimage.cpp:826
QCursor
QHash::insert
QHash::iterator insert(const Key &key, const T &value)
QLineF
QLineF::length
qreal length() const const
QList
QList::empty
bool empty() const const
QList::size
int size() const const
QObject
QObject::event
virtual bool event(QEvent *e)
QPainter::CompositionMode_Source
CompositionMode_Source
QPainterPath
QPen
QPixmap
QPointF
QSettings
Qt::NoBrush
NoBrush
Qt::ShiftModifier
ShiftModifier
Qt::LeftButton
LeftButton
Qt::RoundCap
RoundCap
Qt::RoundJoin
RoundJoin
Qt::DotLine
DotLine
Generated on Thu May 8 2025 04:47:53 for Pencil2D by doxygen 1.9.6 based on revision 4513250b1d5b1a3676ec0e67b06b7a885ceaae39