From: Shamus Hammons Date: Fri, 30 Nov 2012 02:20:18 +0000 (-0600) Subject: Added individual polygon rotation tool. X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;ds=sidebyside;h=360d040a4727383cc8e78d0b8a3c8151c27d26fc;p=ttedit Added individual polygon rotation tool. --- diff --git a/src/editwindow.cpp b/src/editwindow.cpp index 89a4ef8..242d237 100755 --- a/src/editwindow.cpp +++ b/src/editwindow.cpp @@ -163,8 +163,6 @@ void EditWindow::paintEvent(QPaintEvent * /*event*/) { if (i == ptHighlight) { -// dc.SetPen(*(wxThePenList->FindOrCreatePen(wxColour(0xFF, 0x00, 0x00), 1, wxSOLID))); -//// SelectObject(hdc, hRedPen1); p.setPen(QPen(Qt::red, 1.0, Qt::SolidLine)); if (pts.GetOnCurve(i)) @@ -180,8 +178,6 @@ void EditWindow::paintEvent(QPaintEvent * /*event*/) } else if ((i == ptHighlight || i == ptNextHighlight) && tool == TOOLAddPt) { -// dc.SetPen(*(wxThePenList->FindOrCreatePen(wxColour(0x00, 0xAF, 0x00), 1, wxSOLID))); -//// SelectObject(hdc, hGreenPen1); p.setPen(QPen(Qt::green, 1.0, Qt::SolidLine)); if (pts.GetOnCurve(i)) @@ -197,8 +193,6 @@ void EditWindow::paintEvent(QPaintEvent * /*event*/) } else { -// dc.SetPen(*(wxThePenList->FindOrCreatePen(wxColour(0x00, 0x00, 0x00), 1, wxSOLID))); -//// SelectObject(hdc, hBlackPen1); p.setPen(QPen(Qt::black, 1.0, Qt::SolidLine)); if (pts.GetOnCurve(i)) @@ -209,129 +203,32 @@ void EditWindow::paintEvent(QPaintEvent * /*event*/) if (tool == TOOLDelPt && i == ptHighlight) { -#if 0 - dc.SetPen(*(wxThePenList->FindOrCreatePen(wxColour(0xFF, 0x00, 0x00), 1, wxSOLID))); -// SelectObject(hdc, hRedPen1); -// MoveToEx(hdc, pts.GetX(i) - 5, pts.GetY(i) - 5, NULL); -// LineTo(hdc, pts.GetX(i) + 5, pts.GetY(i) + 5); -// LineTo(hdc, pts.GetX(i) - 5, pts.GetY(i) - 5);//Lameness! -// MoveToEx(hdc, pts.GetX(i) - 5, pts.GetY(i) + 5, NULL); -// LineTo(hdc, pts.GetX(i) + 5, pts.GetY(i) - 5); -// LineTo(hdc, pts.GetX(i) - 5, pts.GetY(i) + 5);//More lameness!! -#endif p.setPen(QPen(Qt::red, 1.0, Qt::SolidLine)); p.drawLine(pts.GetX(i) - 5, pts.GetY(i) - 5, pts.GetX(i) + 5, pts.GetY(i) + 5); p.drawLine(pts.GetX(i) + 5, pts.GetY(i) - 5, pts.GetX(i) - 5, pts.GetY(i) + 5); } } -//// SelectObject(hdc, hBlackPen1); -// dc.SetPen(*(wxThePenList->FindOrCreatePen(wxColour(0x00, 0x00, 0x00), 1, wxSOLID))); - p.setPen(QPen(Qt::black, 1.0, Qt::SolidLine)); - // Draw curve formed by points - for(int poly=0; poly 2) - { - // Initial move... - // If it's not on curve, then move to it, otherwise move to last point... - - int x, y; - - if (pts.GetOnCurve(poly, pts.GetNumPoints(poly) - 1)) - x = (int)pts.GetX(poly, pts.GetNumPoints(poly) - 1), y = (int)pts.GetY(poly, pts.GetNumPoints(poly) - 1); - else - x = (int)pts.GetX(poly, 0), y = (int)pts.GetY(poly, 0); - - for(int i=0; i 2) - { - // Initial move... - // If it's not on curve, then move to it, otherwise move to last point... - - int x, y; - if (rotated.GetOnCurve(poly, rotated.GetNumPoints(poly) - 1)) - x = (int)rotated.GetX(poly, rotated.GetNumPoints(poly) - 1), y = (int)rotated.GetY(poly, rotated.GetNumPoints(poly) - 1); - else - x = (int)rotated.GetX(poly, 0), y = (int)rotated.GetY(poly, 0); - - for(int i=0; i [# polys: %u, # points: %u]\n", pts.GetNumPolys(), pts.GetNumP rotationAngle = 0; update(); } + else if (tool == TOOLRotatePoly) + { + IPoint centroid = pts.GetPolyCentroid(pts.GetPolyForPointNumber(ptHighlight)); + rotationCenter = QPoint(centroid.x, centroid.y); + showRotationCenter = true; + pt = GetAdjustedClientPosition(pts.GetX(ptHighlight), pts.GetY(ptHighlight)); + QCursor::setPos(mapToGlobal(pt)); + rotationZeroPoint = QPoint(pts.GetX(ptHighlight), pts.GetY(ptHighlight)); + haveZeroPoint = true; + rotationAngle = 0; + update(); + } } event->accept(); @@ -529,7 +438,7 @@ void EditWindow::mouseMoveEvent(QMouseEvent * event) update(); } } - else if (tool == TOOLRotate) + else if (tool == TOOLRotate || tool == TOOLRotatePoly) { if (pts.GetNumPoints() > 0) { @@ -563,7 +472,7 @@ void EditWindow::mouseMoveEvent(QMouseEvent * event) { // Moving, not dragging... if (tool == TOOLSelect || tool == TOOLDelPt || tool == TOOLAddPt - || tool == TOOLPolySelect)// || tool == TOOLAddPoly) + || tool == TOOLPolySelect || tool == TOOLRotatePoly) { QPoint pt2 = GetAdjustedMousePosition(event); double closest = 1.0e+99; @@ -694,7 +603,15 @@ void EditWindow::mouseReleaseEvent(QMouseEvent * event) { showRotationCenter = false; haveZeroPoint = false; - pts.RotatePoints(rotationAngle, IPoint(rotationCenter.x(), rotationCenter.y())); + + if (tool == TOOLRotate) + pts.RotatePoints(rotationAngle, IPoint(rotationCenter.x(), rotationCenter.y())); + else + { + uint16 poly = pts.GetPolyForPointNumber(ptHighlight); + pts.RotatePolyAroundCentroid(poly, rotationAngle); + } + update(); ((TTEdit *)qApp)->mainWindow->statusBar()->showMessage(""); } diff --git a/src/glyphpoints.cpp b/src/glyphpoints.cpp index e6dc52c..05edc82 100755 --- a/src/glyphpoints.cpp +++ b/src/glyphpoints.cpp @@ -145,19 +145,6 @@ GlyphPoints& GlyphPoints::operator=(const GlyphPoints &c) if (this == &c) return *this; // Take care of self-assignment -#if 0 - if (x) - delete[] x; - - if (y) - delete[] y; - - if (onCurve) - delete[] onCurve; - - if (polyEnd) - delete[] polyEnd; -#endif FreeAllocatedMemory(); AllocateAndCopy(c.numPoints, c.numPolys, c.x, c.y, c.onCurve, c.polyEnd); @@ -454,6 +441,15 @@ IPoint GlyphPoints::GetPoint(uint16 poly, uint16 pt) } +IPoint GlyphPoints::GetPoint(uint16 pointNumber) +{ + if (pointNumber > numPoints) + throw GP_OUT_OF_RANGE; + + return IPoint(x[pointNumber], y[pointNumber]); +} + + bool GlyphPoints::GetOnCurve(uint16 poly, uint16 pt) { if (pt >= GetNumPoints(poly)) @@ -655,6 +651,7 @@ uint16 GlyphPoints::GetNext(uint16 poly, uint16 pt) } +#warning "!!! This function returns incorrect results !!!" uint16 GlyphPoints::GetPoly(uint16 pt) { if (pt >= numPoints) @@ -730,6 +727,40 @@ IPoint GlyphPoints::GetNextPoint(uint16 poly, uint16 pt) } +uint16 GlyphPoints::GetPolyForPoint(IPoint point) +{ + uint16 poly = 0; + + for(uint16 i=0; i polyEnd[poly]) + poly++; + + if (IPoint(x[i], y[i]) == point) + return poly; + } + + return 0xFFFF; +} + + +uint16 GlyphPoints::GetPolyForPointNumber(uint16 pointNumber) +{ + // If there's only one poly, we know where the point is... + if (numPolys <= 1) + return 0; + + // Otherwise, do a linear search through the polys to find the right one + for(uint16 i=0; i= GetPolyStart(i) && pointNumber <= polyEnd[i]) + return i; + } + + return 0xFFFF; +} + + // // Rotate a point by "angle" around point "center" // diff --git a/src/glyphpoints.h b/src/glyphpoints.h index 09b422c..fc0dd2b 100755 --- a/src/glyphpoints.h +++ b/src/glyphpoints.h @@ -21,6 +21,7 @@ struct IPoint bool onCurve; IPoint(int32 xx=0, int32 yy=0, bool oc=true): x(xx), y(yy), onCurve(oc) {} + bool operator==(const IPoint & p) { return (p.x == x && p.y == y ? true: false); }; }; // Throws the following exceptions: @@ -63,6 +64,7 @@ class GlyphPoints int GetY(uint16, uint16); int GetNextY(uint16, uint16); IPoint GetPoint(uint16, uint16); + IPoint GetPoint(uint16); bool GetOnCurve(uint16, uint16); bool GetPrevOnCurve(uint16, uint16); bool GetNextOnCurve(uint16, uint16); @@ -83,6 +85,8 @@ class GlyphPoints IPoint GetMidpointToNext(uint16, uint16); IPoint GetPrevPoint(uint16, uint16); IPoint GetNextPoint(uint16, uint16); + uint16 GetPolyForPoint(IPoint point); + uint16 GetPolyForPointNumber(uint16 pointNumber); IPoint RotatePoint(const double angle, const IPoint point, const IPoint center); void RotatePoints(const double angle, const IPoint point); diff --git a/src/toolwindow.h b/src/toolwindow.h index 323b563..f9134bc 100755 --- a/src/toolwindow.h +++ b/src/toolwindow.h @@ -23,7 +23,7 @@ enum ToolType { TOOLDelPt, // Delete point tool TOOLDelPoly, // Delete polygon tool TOOLRotate, // Rotate tool - TOOLRotatePoly // ROtate polygon around centroid tool + TOOLRotatePoly // Rotate polygon around centroid tool }; class ToolWindow: public QWidget