Straight line tool on raster brush tool (#44)

This commit is contained in:
Jeremy Bullock 2020-06-04 20:50:21 -06:00 committed by GitHub
parent 49c375bb1d
commit bc9b68e15e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 2 deletions

View file

@ -308,6 +308,12 @@ void FullColorBrushTool::leftButtonDown(const TPointD &pos,
if (!ri) return; 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 /* update color here since the current style might be switched with numpad
* shortcut keys */ * shortcut keys */
updateCurrentStyle(); updateCurrentStyle();
@ -355,6 +361,47 @@ void FullColorBrushTool::leftButtonDown(const TPointD &pos,
void FullColorBrushTool::leftButtonDrag(const TPointD &pos, void FullColorBrushTool::leftButtonDrag(const TPointD &pos,
const TMouseEvent &e) { 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; TPointD previousBrushPos = m_brushPos;
m_brushPos = m_mousePos = pos; m_brushPos = m_mousePos = pos;
m_mouseEvent = e; m_mouseEvent = e;
@ -380,7 +427,7 @@ void FullColorBrushTool::leftButtonDrag(const TPointD &pos,
ras->extract(updateRect)->copy(m_workRaster->extract(updateRect)); ras->extract(updateRect)->copy(m_workRaster->extract(updateRect));
TPointD thickOffset(m_maxCursorThick * 0.5, m_maxCursorThick * 0.5); 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(m_brushPos - thickOffset, m_brushPos + thickOffset);
invalidateRect += invalidateRect +=
TRectD(previousBrushPos - thickOffset, previousBrushPos + thickOffset); TRectD(previousBrushPos - thickOffset, previousBrushPos + thickOffset);
@ -401,7 +448,11 @@ void FullColorBrushTool::leftButtonUp(const TPointD &pos,
TRasterP ras = ri->getRaster(); TRasterP ras = ri->getRaster();
TPointD rasCenter = ras->getCenterD(); 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; double pressure;
if (getApplication()->getCurrentLevelStyle()->getTagId() == if (getApplication()->getCurrentLevelStyle()->getTagId() ==
4001) // mypaint brush case 4001) // mypaint brush case
@ -446,6 +497,7 @@ void FullColorBrushTool::leftButtonUp(const TPointD &pos,
notifyImageChanged(); notifyImageChanged();
m_strokeRect.empty(); m_strokeRect.empty();
m_mousePressed = false; m_mousePressed = false;
m_isStraight = false;
} }
//--------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------
@ -513,6 +565,10 @@ void FullColorBrushTool::mouseMove(const TPointD &pos, const TMouseEvent &e) {
void FullColorBrushTool::draw() { void FullColorBrushTool::draw() {
if (TRasterImageP ri = TRasterImageP(getImage(false))) { if (TRasterImageP ri = TRasterImageP(getImage(false))) {
if (m_isStraight) {
tglDrawSegment(m_firstPoint, m_lastPoint);
}
// If toggled off, don't draw brush outline // If toggled off, don't draw brush outline
if (!Preferences::instance()->isCursorOutlineEnabled()) return; if (!Preferences::instance()->isCursorOutlineEnabled()) return;

View file

@ -123,6 +123,10 @@ protected:
bool m_mousePressed = false; bool m_mousePressed = false;
TMouseEvent m_mouseEvent; TMouseEvent m_mouseEvent;
bool m_isStraight = false;
TPointD m_firstPoint;
TPointD m_lastPoint;
bool m_propertyUpdating = false; bool m_propertyUpdating = false;
}; };