From bc9b68e15e61b805286558d8aeff68580585ce45 Mon Sep 17 00:00:00 2001 From: Jeremy Bullock Date: Thu, 4 Jun 2020 20:50:21 -0600 Subject: [PATCH] Straight line tool on raster brush tool (#44) --- toonz/sources/tnztools/fullcolorbrushtool.cpp | 60 ++++++++++++++++++- toonz/sources/tnztools/fullcolorbrushtool.h | 4 ++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/toonz/sources/tnztools/fullcolorbrushtool.cpp b/toonz/sources/tnztools/fullcolorbrushtool.cpp index f16c6909..240da5d4 100644 --- a/toonz/sources/tnztools/fullcolorbrushtool.cpp +++ b/toonz/sources/tnztools/fullcolorbrushtool.cpp @@ -308,6 +308,12 @@ void FullColorBrushTool::leftButtonDown(const TPointD &pos, if (!ri) return; + 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(); @@ -355,6 +361,47 @@ void FullColorBrushTool::leftButtonDown(const TPointD &pos, void FullColorBrushTool::leftButtonDrag(const TPointD &pos, const TMouseEvent &e) { + TRectD invalidateRect; + if (m_isStraight) { + invalidateRect = TRectD(m_firstPoint, m_lastPoint).enlarge(2); + m_lastPoint = pos; + 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); + int blah = 0; + 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; + } + TPointD previousBrushPos = m_brushPos; m_brushPos = m_mousePos = pos; m_mouseEvent = e; @@ -380,7 +427,7 @@ void FullColorBrushTool::leftButtonDrag(const TPointD &pos, 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); @@ -401,7 +448,11 @@ void FullColorBrushTool::leftButtonUp(const TPointD &pos, TRasterP ras = ri->getRaster(); TPointD rasCenter = ras->getCenterD(); - TPointD point(pos + rasCenter); + TPointD point; + if (!e.isCtrlPressed()) + point = TPointD(pos + rasCenter); + else + point = TPointD(m_lastPoint + rasCenter); double pressure; if (getApplication()->getCurrentLevelStyle()->getTagId() == 4001) // mypaint brush case @@ -446,6 +497,7 @@ void FullColorBrushTool::leftButtonUp(const TPointD &pos, notifyImageChanged(); m_strokeRect.empty(); m_mousePressed = false; + m_isStraight = false; } //--------------------------------------------------------------------------------------------------------------- @@ -513,6 +565,10 @@ void FullColorBrushTool::mouseMove(const TPointD &pos, const TMouseEvent &e) { void FullColorBrushTool::draw() { if (TRasterImageP ri = TRasterImageP(getImage(false))) { + if (m_isStraight) { + tglDrawSegment(m_firstPoint, m_lastPoint); + } + // If toggled off, don't draw brush outline if (!Preferences::instance()->isCursorOutlineEnabled()) return; diff --git a/toonz/sources/tnztools/fullcolorbrushtool.h b/toonz/sources/tnztools/fullcolorbrushtool.h index 414aff9c..71df3e17 100644 --- a/toonz/sources/tnztools/fullcolorbrushtool.h +++ b/toonz/sources/tnztools/fullcolorbrushtool.h @@ -123,6 +123,10 @@ protected: bool m_mousePressed = false; TMouseEvent m_mouseEvent; + bool m_isStraight = false; + TPointD m_firstPoint; + TPointD m_lastPoint; + bool m_propertyUpdating = false; };