]> Shamusworld >> Repos - ttedit/commitdiff
First stab at showing effect of added points on a given poly.
authorShamus Hammons <jlhamm@acm.org>
Wed, 9 Mar 2016 04:18:08 +0000 (22:18 -0600)
committerShamus Hammons <jlhamm@acm.org>
Wed, 9 Mar 2016 04:18:08 +0000 (22:18 -0600)
Right now, it only works with the AddPoly tool, and not 100% well. More
work needs to be done to really polish it so it fits in with the rest of
the tools.

src/editwindow.cpp
src/editwindow.h
src/glyphpoints.cpp
src/painter.cpp
src/painter.h

index 4a3d43f50fc8def35436ee526b222036a837cda0..e4f7d7b81dd47f2e691ff2548042861845573cbf 100644 (file)
@@ -269,46 +269,68 @@ void EditWindow::DrawGlyph(Painter & p, GlyphPoints & glyph)
 }
 
 
+/*
+So, to make it draw the point the pointer is pointing at, we need to do something. Either patch the GlyphPoints to handle it, or insert the point into the GlyphPoints and delete it if the user changes tools. Either way, need
+to change the color of the line(s) drawn to the point to signal to the user
+that it isn't finalized until they click the button.
+*/
 void EditWindow::DrawGlyphPoly(Painter & p, GlyphPoints & glyph, uint16_t poly)
 {
        // Sanity check
        if (glyph.GetNumPoints(poly) < 3)
                return;
 
-       // Initial move: If our start point is on curve, then go to it. Otherwise,
-       // check previous point. If it's on curve, go to it otherwise go the
-       // midpoint between start point and previous (since it's between two curve
-       // control points).
-       IPoint pt = (glyph.GetOnCurve(poly, 0)
-               ? glyph.GetPoint(poly, 0) : (glyph.GetPrevOnCurve(poly, 0)
-                       ? glyph.GetPrevPoint(poly, 0) : glyph.GetMidpointToPrev(poly, 0)));
+       IPoint p1 = glyph.GetPrevPoint(poly, 0);
+       IPoint p2 = glyph.GetPoint(poly, 0);
+
+       // Inject the new poly point into the current polygon
+       if ((tool == TOOLAddPoly) && (poly == (glyph.GetNumPolys() - 1)))
+       {
+               p1 = IPoint(addPoint.x, addPoint.y, addPointOnCurve);
+       }
 
        for(int i=0; i<glyph.GetNumPoints(poly); i++)
        {
-               // If this point and then next are both on curve, we have a line...
-               if (glyph.GetOnCurve(poly, i) && glyph.GetNextOnCurve(poly, i))
-               {
-                       IPoint pt2 = glyph.GetNextPoint(poly, i);
-                       p.DrawLine(pt.x, pt.y, pt2.x, pt2.y);
-                       pt = pt2;
-               }
-               else
+               IPoint p3 = glyph.GetNextPoint(poly, i);
+
+               if ((tool == TOOLAddPoly) && (poly == (glyph.GetNumPolys() - 1))
+                       && (i == (glyph.GetNumPoints(poly) - 1)))
                {
-                       // Skip point if it's on curve (start of curve--it's already
-                       // been plotted so we don't need to handle it...)
-                       if (glyph.GetOnCurve(poly, i))
-                               continue;
-
-                       // We are now guaranteed that we are sitting on a curve control
-                       // point (off curve). Figure the extent of the curve: If the
-                       // following is a curve control point, then use the midpoint to it
-                       // otherwise go to the next point since it's on curve.
-                       IPoint pt2 = (glyph.GetNextOnCurve(poly, i)
-                               ? glyph.GetNextPoint(poly, i) : glyph.GetMidpointToNext(poly, i));
-
-                       p.DrawBezier(pt, glyph.GetPoint(poly, i), pt2);
-                       pt = pt2;
+                       p3 = IPoint(addPoint.x, addPoint.y, addPointOnCurve);
+                       p.SetPen(QPen(Qt::green, 1.0, Qt::SolidLine));
+                       DrawGlyphSegment(p, p1, p2, p3);
+                       p1 = p2;
+                       p2 = p3;
+                       p3 = glyph.GetNextPoint(poly, i);
                }
+
+               DrawGlyphSegment(p, p1, p2, p3);
+
+               p1 = p2;
+               p2 = p3;
+       }
+}
+
+
+//
+// Draw a glyph segment given 3 points
+//
+void EditWindow::DrawGlyphSegment(Painter & p, IPoint p1, IPoint p2, IPoint p3)
+{
+       if (p2.onCurve)
+       {
+               // Skip drawing if the middle point is on curve and the last is off
+               if (p3.onCurve)
+                       p.DrawLine(p2, p3);
+       }
+       else
+       {
+               // The middle point is off curve, and so we need to draw a Bezier curve.
+               // Also, depending on whether or not the previous or follow points are
+               // off curve, we need to draw to the midpoints if so.
+               IPoint mid12 = IPoint((p1.x + p2.x) / 2, (p1.y + p2.y) / 2);
+               IPoint mid23 = IPoint((p2.x + p3.x) / 2, (p2.y + p3.y) / 2);
+               p.DrawBezier((p1.onCurve ? p1 : mid12), p2, (p3.onCurve ? p3 : mid23));
        }
 }
 
@@ -499,7 +521,16 @@ void EditWindow::mouseMoveEvent(QMouseEvent * event)
 
 //                     QPoint pt2 = GetAdjustedMousePosition(event);
                        Vector pt2 = Painter::QtToCartesianCoords(Vector(event->x(), event->y()));
-                       pts.SetXY(ptHighlight, pt2.x, pt2.y);
+
+                       if (tool != TOOLSelect)
+                       {
+                               addPoint = pt2;
+                               // Prolly should move this to the key handlers below...
+                               addPointOnCurve = ((event->modifiers() == Qt::ShiftModifier) || (event->modifiers() == Qt::ControlModifier) ? false : true);
+                       }
+                       else
+                               pts.SetXY(ptHighlight, pt2.x, pt2.y);
+
                        update();
                }
                else if (tool == TOOLPolySelect)
@@ -667,6 +698,11 @@ void EditWindow::mouseMoveEvent(QMouseEvent * event)
                }
 
                ptPrevious = Vector(event->x(), event->y());
+               addPoint = Painter::QtToCartesianCoords(Vector(event->x(), event->y()));
+//             addPointOnCurve = ((event->modifiers() == Qt::ShiftModifier) || (event->modifiers() == Qt::ControlModifier) ? false : true);
+
+               if (tool == TOOLAddPoly)
+                       update();
        }
 
        event->accept();
@@ -753,17 +789,29 @@ void EditWindow::keyPressEvent(QKeyEvent * event)
                pts.SetXY(ptHighlight, pts.GetX(ptHighlight) + 1, pts.GetY(ptHighlight));
        else if (event->key() == Qt::Key_Left)
                pts.SetXY(ptHighlight, pts.GetX(ptHighlight) - 1, pts.GetY(ptHighlight));
+       else if ((event->key() == Qt::Key_Shift) || (event->key() == Qt::Key_Control))
+       {
+               addPointOnCurve = false;
+       }
        else
                return;
 
-       event->accept();
+//     event->accept();
        update();
        ((TTEdit *)qApp)->charWnd->MakePathFromPoints(&pts);
        ((TTEdit *)qApp)->charWnd->update();
 }
 
 
-void EditWindow::keyReleaseEvent(QKeyEvent * /*event*/)
+void EditWindow::keyReleaseEvent(QKeyEvent * event)
 {
+       if ((event->key() == Qt::Key_Shift) || (event->key() == Qt::Key_Control))
+       {
+               addPointOnCurve = true;
+       }
+       else
+               return;
+
+       update();
 }
 
index f1518e9a4e27c1bd5fb5ebce24a75caa3128ec6b..77104bd4d442287b704108ee3ce44492da24da01 100644 (file)
@@ -36,11 +36,13 @@ class EditWindow: public QWidget
                void CreateCursors(void);
                void DrawGlyph(Painter & p, GlyphPoints & glyph);
                void DrawGlyphPoly(Painter & p, GlyphPoints & glyph, uint16_t poly);
+               void DrawGlyphSegment(Painter & p, IPoint p1, IPoint p2, IPoint p3);
                void ClearSelection(void);
 
        public:
                QImage image;
-               Vector pt, ptOffset, ptPrevious;
+               Vector pt, ptOffset, ptPrevious, addPoint;
+               bool addPointOnCurve;
                ToolType tool;                                          // Current tool
                GlyphPoints pts;                                        // Glyph point structure
                int32_t ptHighlight, oldPtHighlight, ptNextHighlight, oldPtNextHighlight;
index 4ae01e5d5d8630c0d9dcec1cfd0d6cb0689db0f4..856b85964e5d1c5764f1d7020d6fa0cc9baa9e0c 100644 (file)
@@ -74,19 +74,6 @@ WriteLogMsg("GlyphPoints: Copy constructor. %u points, %u polys.\n", numPoints,
 GlyphPoints::~GlyphPoints()
 {
        FreeAllocatedMemory();
-#if 0
-       if (x)
-               delete[] x;
-
-       if (y)
-               delete[] y;
-
-       if (onCurve)
-               delete[] onCurve;
-
-       if (polyEnd)
-               delete[] polyEnd;
-#endif
 }
 
 
@@ -751,7 +738,7 @@ IPoint GlyphPoints::GetPrevPoint(uint16_t poly, uint16_t pt)
 {
        uint16_t prevPt = GetPrev(poly, pt);
 
-       return IPoint(GetX(poly, prevPt), GetY(poly, prevPt));
+       return IPoint(GetX(poly, prevPt), GetY(poly, prevPt), GetOnCurve(poly, prevPt));
 }
 
 
@@ -759,7 +746,7 @@ IPoint GlyphPoints::GetNextPoint(uint16_t poly, uint16_t pt)
 {
        uint16_t nextPt = GetNext(poly, pt);
 
-       return IPoint(GetX(poly, nextPt), GetY(poly, nextPt));
+       return IPoint(GetX(poly, nextPt), GetY(poly, nextPt), GetOnCurve(poly, nextPt));
 }
 
 
index 16268e70b38535c9ca42090e9f6e17a9a11c4b8d..e2a457a5499a3cff2ba5732e9cc399080c57986a 100644 (file)
@@ -279,6 +279,17 @@ void Painter::DrawLine(int x1, int y1, int x2, int y2)
 }
 
 
+void Painter::DrawLine(IPoint p1, IPoint p2)
+{
+       if (!painter)
+               return;
+
+       Vector v1 = CartesianToQtCoords(Vector(p1.x, p1.y));
+       Vector v2 = CartesianToQtCoords(Vector(p2.x, p2.y));
+       painter->drawLine(v1.x, v1.y, v2.x, v2.y);
+}
+
+
 void Painter::DrawLine(Vector v1, Vector v2)
 {
        if (!painter)
index c95b84adba32083b52f6fc2c229d847f28d943be..d136f23c967f26c2f8ef341f1561451fd7aba5fb 100644 (file)
@@ -30,6 +30,7 @@ class Painter
                void DrawArrowHandle(Vector, double);
                void DrawArrowToLineHandle(Vector, double);
                void DrawLine(int, int, int, int);
+               void DrawLine(IPoint, IPoint);
                void DrawLine(Vector, Vector);
                void DrawPoint(int, int);
                void DrawRoundedRect(QRectF, double, double);