Skip to content

Commit

Permalink
Merge pull request #4680 from justburner/StraightLine
Browse files Browse the repository at this point in the history
Straight line mode on brush tool
  • Loading branch information
RodneyBaker committed Dec 31, 2022
2 parents 8f4ee6f + e250b76 commit 7f6dea6
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 32 deletions.
7 changes: 4 additions & 3 deletions toonz/sources/include/toonz/rasterstrokegenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class DVAPI RasterStrokeGenerator {
void add(const TThickPoint &p);

// Disegna il tratto interamente
void generateStroke(bool isPencil) const;
void generateStroke(bool isPencil, bool isStraight = false) const;

// Ritorna m_points
std::vector<TThickPoint> getPointsSequence() { return m_points; }
Expand All @@ -79,7 +79,8 @@ class DVAPI RasterStrokeGenerator {
// raggio "points.thick" +2 pixel a bordo
TRect getBBox(const std::vector<TThickPoint> &points) const;

TRect getLastRect() const;
TRect getLastRect(bool isStraight = false) const;

TRect generateLastPieceOfStroke(bool isPencil, bool closeStroke = false);
TRect generateLastPieceOfStroke(bool isPencil, bool closeStroke = false,
bool isStraight = false);
};
69 changes: 67 additions & 2 deletions toonz/sources/tnztools/fullcolorbrushtool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,13 @@ void FullColorBrushTool::leftButtonDown(const TPointD &pos,

if (!ri) return;

// Modifier to do straight line
if (e.isShiftPressed() || e.isCtrlPressed()) {
m_isStraight = true;
m_firstPoint = pos;
m_lastPoint = pos;
}

/* update color here since the current style might be switched with numpad
* shortcut keys */
updateCurrentStyle();
Expand Down Expand Up @@ -364,6 +371,9 @@ void FullColorBrushTool::leftButtonDown(const TPointD &pos,

void FullColorBrushTool::leftButtonDrag(const TPointD &pos,
const TMouseEvent &e) {
TRectD invalidateRect;
m_lastPoint = pos;

TPointD previousBrushPos = m_brushPos;
m_brushPos = m_mousePos = pos;
m_mouseEvent = e;
Expand All @@ -382,14 +392,54 @@ void FullColorBrushTool::leftButtonDrag(const TPointD &pos,
else
pressure = m_enabledPressure ? e.m_pressure : 1.0;

if (m_maxPressure < pressure) m_maxPressure = pressure;

if (m_isStraight) {
invalidateRect = TRectD(m_firstPoint, m_lastPoint).enlarge(2);
if (e.isCtrlPressed()) {
double distance = (m_brushPos.x - m_maxCursorThick + 1) * 0.5;
TRectD brushRect =
TRectD(TPointD(m_brushPos.x - distance, m_brushPos.y - distance),
TPointD(m_brushPos.x + distance, m_brushPos.y + distance));
invalidateRect += (brushRect);
double denominator = m_lastPoint.x - m_firstPoint.x;
if (denominator == 0) denominator == 0.001;
double slope = ((m_lastPoint.y - m_firstPoint.y) / denominator);
double angle = std::atan(slope) * (180 / 3.14159);
if (abs(angle) > 67.5)
m_lastPoint.x = m_firstPoint.x;
else if (abs(angle) < 22.5)
m_lastPoint.y = m_firstPoint.y;
else {
double xDistance = m_lastPoint.x - m_firstPoint.x;
double yDistance = m_lastPoint.y - m_firstPoint.y;
if (abs(xDistance) > abs(yDistance)) {
if (abs(yDistance) == yDistance)
m_lastPoint.y = m_firstPoint.y + abs(xDistance);
else
m_lastPoint.y = m_firstPoint.y - abs(xDistance);
} else {
if (abs(xDistance) == xDistance)
m_lastPoint.x = m_firstPoint.x + abs(yDistance);
else
m_lastPoint.x = m_firstPoint.x - abs(yDistance);
}
}
}
m_mousePos = pos;
m_brushPos = pos;
invalidate(invalidateRect);
return;
}

m_strokeSegmentRect.empty();
m_toonz_brush->strokeTo(point, pressure, restartBrushTimer());
TRect updateRect = m_strokeSegmentRect * ras->getBounds();
if (!updateRect.isEmpty())
ras->extract(updateRect)->copy(m_workRaster->extract(updateRect));

TPointD thickOffset(m_maxCursorThick * 0.5, m_maxCursorThick * 0.5);
TRectD invalidateRect = convert(m_strokeSegmentRect) - rasCenter;
invalidateRect = convert(m_strokeSegmentRect) - rasCenter;
invalidateRect += TRectD(m_brushPos - thickOffset, m_brushPos + thickOffset);
invalidateRect +=
TRectD(previousBrushPos - thickOffset, previousBrushPos + thickOffset);
Expand All @@ -410,14 +460,22 @@ void FullColorBrushTool::leftButtonUp(const TPointD &pos,

TRasterP ras = ri->getRaster();
TPointD rasCenter = ras->getCenterD();
TPointD point(pos + rasCenter);
TPointD point;
if (m_isStraight)
point = TPointD(m_lastPoint + rasCenter);
else
point = TPointD(pos + rasCenter);
double pressure;
if (getApplication()->getCurrentLevelStyle()->getTagId() ==
4001) // mypaint brush case
pressure = m_enabledPressure && e.isTablet() ? e.m_pressure : 0.5;
else
pressure = m_enabledPressure ? e.m_pressure : 1.0;

if (m_isStraight && m_maxPressure > 0.0) {
pressure = m_maxPressure;
}

m_strokeSegmentRect.empty();
m_toonz_brush->strokeTo(point, pressure, restartBrushTimer());
m_toonz_brush->endStroke();
Expand Down Expand Up @@ -455,6 +513,8 @@ void FullColorBrushTool::leftButtonUp(const TPointD &pos,
notifyImageChanged();
m_strokeRect.empty();
m_mousePressed = false;
m_isStraight = false;
m_maxPressure = -1.0;
}

//---------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -522,6 +582,11 @@ void FullColorBrushTool::mouseMove(const TPointD &pos, const TMouseEvent &e) {

void FullColorBrushTool::draw() {
if (TRasterImageP ri = TRasterImageP(getImage(false))) {
// Draw line segment on straight line mode
if (m_isStraight) {
tglDrawSegment(m_firstPoint, m_lastPoint);
}

// If toggled off, don't draw brush outline
if (!Preferences::instance()->isCursorOutlineEnabled()) return;

Expand Down
5 changes: 5 additions & 0 deletions toonz/sources/tnztools/fullcolorbrushtool.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ class FullColorBrushTool final : public TTool, public RasterController {
bool m_mousePressed = false;
TMouseEvent m_mouseEvent;

bool m_isStraight = false;
TPointD m_firstPoint;
TPointD m_lastPoint;
double m_maxPressure = -1.0;

bool m_propertyUpdating = false;
};

Expand Down
Loading

0 comments on commit 7f6dea6

Please sign in to comment.