18 #include "brushtool.h"
26 #include "beziercurve.h"
27 #include "vectorimage.h"
28 #include "layervector.h"
30 #include "colormanager.h"
31 #include "strokemanager.h"
32 #include "layermanager.h"
33 #include "viewmanager.h"
34 #include "selectionmanager.h"
35 #include "scribblearea.h"
37 #include "pointerevent.h"
44 ToolType BrushTool::type()
49 void BrushTool::loadSettings()
51 mPropertyEnabled[WIDTH] =
true;
52 mPropertyEnabled[FEATHER] =
true;
53 mPropertyEnabled[PRESSURE] =
true;
54 mPropertyEnabled[INVISIBILITY] =
true;
55 mPropertyEnabled[STABILIZATION] =
true;
59 properties.width = settings.value(
"brushWidth", 24.0).toDouble();
60 properties.feather = settings.value(
"brushFeather", 48.0).toDouble();
61 properties.pressure = settings.value(
"brushPressure",
true).toBool();
62 properties.invisibility = settings.value(
"brushInvisibility",
false).toBool();
63 properties.preserveAlpha = OFF;
64 properties.stabilizerLevel = settings.value(
"brushLineStabilization", StabilizationLevel::STRONG).toInt();
65 properties.useAA = DISABLED;
67 if (properties.width <= 0) { setWidth(15); }
68 if (std::isnan(properties.feather)) { setFeather(15); }
74 void BrushTool::resetToDefault()
78 setStabilizerLevel(StabilizationLevel::STRONG);
81 void BrushTool::setWidth(
const qreal width)
84 properties.width = width;
88 settings.setValue(
"brushWidth", width);
92 void BrushTool::setFeather(
const qreal feather)
95 properties.feather = feather;
99 settings.setValue(
"brushFeather", feather);
103 void BrushTool::setInvisibility(
const bool invisibility)
106 properties.invisibility = invisibility;
109 settings.setValue(
"brushInvisibility", invisibility);
113 void BrushTool::setPressure(
const bool pressure)
116 properties.pressure = pressure;
120 settings.setValue(
"brushPressure", pressure);
124 void BrushTool::setStabilizerLevel(
const int level)
126 properties.stabilizerLevel = level;
129 settings.setValue(
"brushLineStabilization", level);
135 if (mEditor->preference()->isOn(SETTING::TOOL_CURSOR))
144 mScribbleArea->setAllDirty();
145 mMouseDownPoint = getCurrentPoint();
146 mLastBrushPoint = getCurrentPoint();
148 startStroke(event->inputType());
155 mCurrentPressure = strokeManager()->getPressure();
157 if (properties.stabilizerLevel != strokeManager()->getStabilizerLevel())
158 strokeManager()->setStabilizerLevel(properties.stabilizerLevel);
162 void BrushTool::pointerReleaseEvent(
PointerEvent *event)
164 if (event->inputType() != mCurrentInputType)
return;
166 Layer* layer = mEditor->layers()->currentLayer();
167 mEditor->backup(typeName());
169 qreal distance =
QLineF(getCurrentPoint(), mMouseDownPoint).
length();
172 paintAt(mMouseDownPoint);
179 if (layer->type() == Layer::BITMAP)
181 else if (layer->type() == Layer::VECTOR)
188 void BrushTool::paintAt(
QPointF point)
191 Layer* layer = mEditor->layers()->currentLayer();
192 if (layer->type() == Layer::BITMAP)
194 qreal pressure = (properties.pressure) ? mCurrentPressure : 1.0;
195 qreal opacity = (properties.pressure) ? (mCurrentPressure * 0.5) : 1.0;
196 qreal brushWidth = properties.width * pressure;
197 mCurrentWidth = brushWidth;
200 mScribbleArea->drawBrush(point,
203 mEditor->color()->frontColor(),
207 int rad = qRound(brushWidth) / 2 + 2;
208 mScribbleArea->refreshBitmap(rect, rad);
212 void BrushTool::drawStroke()
214 StrokeTool::drawStroke();
217 Layer* layer = mEditor->layers()->currentLayer();
219 if (layer->type() == Layer::BITMAP)
221 for (
int i = 0; i < p.
size(); i++)
223 p[i] = mEditor->view()->mapScreenToCanvas(p[i]);
226 qreal pressure = (properties.pressure) ? mCurrentPressure : 1.0;
227 qreal opacity = (properties.pressure) ? (mCurrentPressure * 0.5) : 1.0;
228 qreal brushWidth = properties.width * pressure;
229 mCurrentWidth = brushWidth;
231 qreal brushStep = (0.5 * brushWidth);
232 brushStep = qMax(1.0, brushStep);
240 int steps = qRound(distance / brushStep);
242 for (
int i = 0; i < steps; i++)
244 QPointF point = mLastBrushPoint + (i + 1) * brushStep * (getCurrentPoint() - mLastBrushPoint) / distance;
247 mScribbleArea->drawBrush(point,
250 mEditor->color()->frontColor(),
253 if (i == (steps - 1))
255 mLastBrushPoint = getCurrentPoint();
259 int rad = qRound(brushWidth / 2 + 2);
261 mScribbleArea->paintBitmapBufferRect(rect);
262 mScribbleArea->refreshBitmap(rect, rad);
280 else if (layer->type() == Layer::VECTOR)
282 qreal pressure = (properties.pressure) ? mCurrentPressure : 1;
283 qreal brushWidth = properties.width * pressure;
285 int rad = qRound((brushWidth / 2 + 2) * mEditor->view()->scaling());
287 QPen pen(mEditor->color()->frontColor(),
288 brushWidth * mEditor->view()->scaling(),
296 path.cubicTo(p[1], p[2], p[3]);
299 mScribbleArea->refreshVector(path.boundingRect().toRect(), rad);
304 void BrushTool::paintBitmapStroke()
306 mScribbleArea->paintBitmapBuffer();
307 mScribbleArea->setAllDirty();
308 mScribbleArea->clearBitmapBuffer();
313 void BrushTool::paintVectorStroke()
315 if (mStrokePoints.
empty())
318 Layer* layer = mEditor->layers()->currentLayer();
320 if (layer->type() == Layer::VECTOR && mStrokePoints.
size() > -1)
323 mScribbleArea->clearBitmapBuffer();
324 qreal tol = mScribbleArea->getCurveSmoothing() / mEditor->view()->scaling();
326 BezierCurve curve(mStrokePoints, mStrokePressures, tol);
327 curve.setWidth(properties.width);
328 curve.setFeather(properties.feather);
329 curve.setFilled(
false);
330 curve.setInvisibility(properties.invisibility);
331 curve.setVariableWidth(properties.pressure);
332 curve.setColorNumber(mEditor->color()->frontColorNumber());
334 VectorImage* vectorImage =
static_cast<VectorImage*
>(layer->getLastKeyFrameAtPosition(mEditor->currentFrame()));
335 vectorImage->
addCurve(curve, mEditor->view()->scaling(),
false);
339 mEditor->deselectAll();
344 mScribbleArea->setModified(mEditor->layers()->currentLayerIndex(), mEditor->currentFrame());
345 mScribbleArea->setAllDirty();
QHash::iterator insert(const Key &key, const T &value)
qreal length() const const
bool isAnyCurveSelected()
VectorImage::isAnyCurveSelected.
void addCurve(BezierCurve &newCurve, qreal factor, bool interacts=true)
VectorImage::addCurve.
Qt::MouseButtons buttons() const
Returns Qt::MouseButtons()
QPoint toPoint() const const
int getLastCurveNumber()
VectorImage::getLastCurveNumber.
void setSelected(int curveNumber, bool YesOrNo)
VectorImage::setSelected.