2 // TTEDIT.CPP - The TrueType Editor
4 // (C) 2004 Underground Software
6 // JLH = James L. Hammons <jlhamm@acm.org>
9 // --- ---------- -------------------------------------------------------------
10 // JLH 08/28/2008 Created this file
11 // JLH 09/02/2008 Separated scrolling from dedicated tool to MMB drag
12 // JLH 03/13/2009 Converted from wxWidgets to Qt
21 // - Fix bug in Glyphpoints when dragging on an empty canvas or loading a font
22 // - Fix zooming, settings (ini)
23 // - Fix point adding bug 1: should be able to add points to empty canvas
24 // - Fix point adding bug 2: should be able to add point successfully to single
26 // - Add poly multi-select
27 // - Add point multi-select
31 // Uncomment this for debugging...
33 #define DEBUGFOO // Various tool debugging...
34 #define DEBUGTP // Toolpalette debugging...
36 #include "editwindow.h"
37 #include "charwindow.h"
39 #include "graphicprimitives.h"
40 #include "mainwindow.h"
45 EditWindow::EditWindow(QWidget * parent/*= NULL*/): QWidget(parent),
46 scale(1.0), offsetX(-10), offsetY(-10), tool(TOOLSelect),
47 ptHighlight(-1), oldPtHighlight(-1), ptNextHighlight(-1), oldPtNextHighlight(-1),
48 polyFirstPoint(true), showRotationCenter(false), haveZeroPoint(false)
50 setBackgroundRole(QPalette::Base);
51 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
53 toolPalette = new ToolWindow();
55 setCursor(cur[TOOLSelect]);
56 setMouseTracking(true);
60 QSize EditWindow::minimumSizeHint() const
66 QSize EditWindow::sizeHint() const
68 return QSize(400, 400);
72 void EditWindow::CreateCursors(void)
74 int hotx[11] = { 1, 1, 11, 15, 1, 1, 1, 1, 1, 1, 1 };
75 int hoty[11] = { 1, 1, 11, 13, 1, 1, 1, 1, 1, 1, 1 };
76 char cursorName[11][48] = { "select", "select-poly", "scroll", "zoom", "add-point",
77 "add-poly", "del-point", "del-poly", "rotate", "rotate", "select" };
79 for(int i=0; i<11; i++)
82 s.sprintf(":/res/cursor-%s.png", cursorName[i]);
84 cur[i] = QCursor(pmTmp, hotx[i], hoty[i]);
89 QPoint EditWindow::GetAdjustedMousePosition(QMouseEvent * event)
91 QSize winSize = size();
92 // This is undoing the transform, e.g. going from client coords to local coords.
93 // In essence, the height - y is height + (y * -1), the (y * -1) term doing the
94 // conversion of the y-axis from increasing bottom to top.
95 return QPoint(offsetX + event->x(), offsetY + (winSize.height() - event->y()));
99 QPoint EditWindow::GetAdjustedClientPosition(int x, int y)
101 QSize winSize = size();
103 // VOODOO ALERT (ON Y COMPONENT!!!!)
104 return QPoint(-offsetX + x, (winSize.height() - (-offsetY + y)) * +1.0);
110 o Different colors for polys on selected points
111 o Different colors for handles on non-selected polys
112 o Line of sight (dashed, dotted) for off-curve points
113 o Repaints for press/release of CTRL/SHIFT during point creation
115 void EditWindow::paintEvent(QPaintEvent * /*event*/)
118 //hm, causes lockup (or does it???)
119 p.setRenderHint(QPainter::Antialiasing);
121 QSize winSize = size();
123 p.translate(QPoint(-offsetX, winSize.height() - (-offsetY)));
126 // Scrolling can be done by using OffsetViewportOrgEx
127 // Scaling can be done by adjusting SetWindowExtEx (it's denominator of txform)
128 // you'd use: % = ViewportExt / WindowExt
129 // But it makes the window look like crap: fuggetuboutit.
130 // Instead, we have to scale EVERYTHING by hand. Crap!
131 // It's not *that* bad, but not as convenient either...
133 p.setPen(QPen(Qt::blue, 1.0, Qt::DotLine));
135 // Draw coordinate axes
137 p.drawLine(0, -16384, 0, 16384);
138 p.drawLine(-16384, 0, 16384, 0);
140 // Draw rotation center (if active)
142 if (showRotationCenter)
144 p.setPen(QPen(Qt::red, 2.0, Qt::SolidLine));
145 p.drawLine(rotationCenter.x() + 7, rotationCenter.y(), rotationCenter.x() - 7, rotationCenter.y());
146 p.drawLine(rotationCenter.x(), rotationCenter.y() + 7, rotationCenter.x(), rotationCenter.y() - 7);
151 for(int i=0; i<pts.GetNumPoints(); i++)
153 if (i == ptHighlight)
155 p.setPen(QPen(Qt::red, 1.0, Qt::SolidLine));
157 if (pts.GetOnCurve(i))
159 DrawSquareDotN(p, pts.GetX(i), pts.GetY(i), 7);
160 DrawSquareDotN(p, pts.GetX(i), pts.GetY(i), 9);
164 DrawRoundDotN(p, pts.GetX(i), pts.GetY(i), 7);
165 DrawRoundDotN(p, pts.GetX(i), pts.GetY(i), 9);
168 else if ((i == ptHighlight || i == ptNextHighlight) && tool == TOOLAddPt)
170 p.setPen(QPen(Qt::green, 1.0, Qt::SolidLine));
172 if (pts.GetOnCurve(i))
174 DrawSquareDotN(p, pts.GetX(i), pts.GetY(i), 7);
175 DrawSquareDotN(p, pts.GetX(i), pts.GetY(i), 9);
179 DrawRoundDotN(p, pts.GetX(i), pts.GetY(i), 7);
180 DrawRoundDotN(p, pts.GetX(i), pts.GetY(i), 9);
185 p.setPen(QPen(Qt::black, 1.0, Qt::SolidLine));
187 if (pts.GetOnCurve(i))
188 DrawSquareDot(p, pts.GetX(i), pts.GetY(i));
190 DrawRoundDot(p, pts.GetX(i), pts.GetY(i));
193 if (tool == TOOLDelPt && i == ptHighlight)
195 p.setPen(QPen(Qt::red, 1.0, Qt::SolidLine));
196 p.drawLine(pts.GetX(i) - 5, pts.GetY(i) - 5, pts.GetX(i) + 5, pts.GetY(i) + 5);
197 p.drawLine(pts.GetX(i) + 5, pts.GetY(i) - 5, pts.GetX(i) - 5, pts.GetY(i) + 5);
201 // Draw curve formed by points
203 p.setPen(QPen(Qt::black, 1.0, Qt::SolidLine));
209 GlyphPoints rotated = pts;
211 if (tool == TOOLRotate)
212 rotated.RotatePoints(rotationAngle, IPoint(rotationCenter.x(), rotationCenter.y()));
213 else if (tool == TOOLRotatePoly)
215 uint16 poly = rotated.GetPolyForPointNumber(ptHighlight);
216 rotated.RotatePolyAroundCentroid(poly, rotationAngle);
219 p.setPen(QPen(QColor(255, 0, 255), 1.0, Qt::SolidLine));
220 DrawGlyph(p, rotated);
225 void EditWindow::DrawGlyph(QPainter & p, GlyphPoints & glyph)
227 for(int poly=0; poly<glyph.GetNumPolys(); poly++)
229 if (glyph.GetNumPoints(poly) < 3)
232 // Initial move: If our start point is on curve, then go to it. Otherwise,
233 // check previous point. If it's on curve, go to it otherwise go the
234 // midpoint between start point and previous (since it's between two curve
236 IPoint pt = (glyph.GetOnCurve(poly, 0)
237 ? glyph.GetPoint(poly, 0) : (glyph.GetPrevOnCurve(poly, 0)
238 ? glyph.GetPrevPoint(poly, 0) : glyph.GetMidpointToPrev(poly, 0)));
240 // Need to add separate color handling here for polys that are being manipulated...
242 for(int i=0; i<glyph.GetNumPoints(poly); i++)
244 // If this point and then next are both on curve, we have a line...
245 if (glyph.GetOnCurve(poly, i) && glyph.GetNextOnCurve(poly, i))
247 IPoint pt2 = glyph.GetNextPoint(poly, i);
248 p.drawLine(pt.x, pt.y, pt2.x, pt2.y);
253 // Skip point if it's on curve (start of curve--it's already
254 // been plotted so we don't need to handle it...)
255 if (glyph.GetOnCurve(poly, i))
258 // We are now guaranteed that we are sitting on a curve control point
259 // (off curve). Figure the extent of the curve: If the following is a
260 // curve control point, then use the midpoint to it otherwise go to
261 // the next point since it's on curve.
262 IPoint pt2 = (glyph.GetNextOnCurve(poly, i)
263 ? glyph.GetNextPoint(poly, i) : glyph.GetMidpointToNext(poly, i));
265 Bezier(p, pt, glyph.GetPoint(poly, i), pt2);
273 void EditWindow::DrawGlyphPoly(QPainter & p, GlyphPoints & glyph, uint16 poly)
276 if (glyph.GetNumPoints(poly) < 3)
279 // Initial move: If our start point is on curve, then go to it. Otherwise,
280 // check previous point. If it's on curve, go to it otherwise go the
281 // midpoint between start point and previous (since it's between two curve
283 IPoint pt = (glyph.GetOnCurve(poly, 0)
284 ? glyph.GetPoint(poly, 0) : (glyph.GetPrevOnCurve(poly, 0)
285 ? glyph.GetPrevPoint(poly, 0) : glyph.GetMidpointToPrev(poly, 0)));
287 for(int i=0; i<glyph.GetNumPoints(poly); i++)
289 // If this point and then next are both on curve, we have a line...
290 if (glyph.GetOnCurve(poly, i) && glyph.GetNextOnCurve(poly, i))
292 IPoint pt2 = glyph.GetNextPoint(poly, i);
293 p.drawLine(pt.x, pt.y, pt2.x, pt2.y);
298 // Skip point if it's on curve (start of curve--it's already
299 // been plotted so we don't need to handle it...)
300 if (glyph.GetOnCurve(poly, i))
303 // We are now guaranteed that we are sitting on a curve control point
304 // (off curve). Figure the extent of the curve: If the following is a
305 // curve control point, then use the midpoint to it otherwise go to
306 // the next point since it's on curve.
307 IPoint pt2 = (glyph.GetNextOnCurve(poly, i)
308 ? glyph.GetNextPoint(poly, i) : glyph.GetMidpointToNext(poly, i));
310 Bezier(p, pt, glyph.GetPoint(poly, i), pt2);
317 void EditWindow::mousePressEvent(QMouseEvent * event)
319 if (event->button() == Qt::RightButton)
321 toolPalette->move(event->globalPos());
322 toolPalette->setVisible(true);
323 setCursor(cur[TOOLSelect]);
324 toolPalette->prevTool = TOOLSelect;
326 else if (event->button() == Qt::MidButton)
328 setCursor(cur[2]); // Scrolling cursor
330 else if (event->button() == Qt::LeftButton)
332 if (tool == TOOLScroll || tool == TOOLZoom)
333 ;//meh CaptureMouse(); // Make sure we capture the mouse when in scroll/zoom mode
334 else if (tool == TOOLAddPt) // "Add Point" tool
336 QPoint pt = GetAdjustedMousePosition(event);
337 IPoint pointToAdd(pt.x(), pt.y(), ((event->modifiers() == Qt::ShiftModifier || event->modifiers() == Qt::ControlModifier) ? false : true));
339 if (pts.GetNumPoints() < 2)
341 // pts += IPoint(pt.x(), pt.y(), ((event->modifiers() == Qt::ShiftModifier || event->modifiers() == Qt::ControlModifier) ? false : true));
343 ptHighlight = pts.GetNumPoints() - 1;
347 // QPoint pt = GetAdjustedMousePosition(event);
348 // pts.InsertPoint(pts.GetNext(ptHighlight), pt.x(), pt.y(), ((event->modifiers() == Qt::ShiftModifier || event->modifiers() == Qt::ControlModifier) ? false : true));
349 pts.InsertPoint(pts.GetNext(ptHighlight), pointToAdd);
350 ptHighlight = ptNextHighlight;
356 else if (tool == TOOLAddPoly) // "Add Poly" tool
359 WriteLogMsg("Adding point... # polys: %u, # points: %u", pts.GetNumPolys(), pts.GetNumPoints());
363 polyFirstPoint = false;
364 pts.AddNewPolyAtEnd();
367 QPoint pt = GetAdjustedMousePosition(event);
368 //printf("GetAdjustedMousePosition = %i, %i\n", pt.x(), pt.y());
369 // Append a point to the end of the structure
370 pts += IPoint(pt.x(), pt.y(), ((event->modifiers() == Qt::ShiftModifier || event->modifiers() == Qt::ControlModifier) ? false : true));
371 ptHighlight = pts.GetNumPoints() - 1;
374 WriteLogMsg(" --> [# polys: %u, # points: %u]\n", pts.GetNumPolys(), pts.GetNumPoints());
377 else if (tool == TOOLSelect || tool == TOOLPolySelect)
379 if (pts.GetNumPoints() > 0)
381 pt = GetAdjustedClientPosition(pts.GetX(ptHighlight), pts.GetY(ptHighlight));
382 //printf("GetAdjustedClientPosition = %i, %i\n", pt.x(), pt.y());
383 // WarpPointer(pt.x, pt.y);
384 QCursor::setPos(mapToGlobal(pt));
386 if (event->modifiers() == Qt::ShiftModifier || event->modifiers() == Qt::ControlModifier)
388 pts.SetOnCurve(ptHighlight, !pts.GetOnCurve(ptHighlight));
393 else if (tool == TOOLDelPt)
395 if (pts.GetNumPoints() > 0)
397 // if (ptHighlight != -1)
399 //This assumes that WM_MOUSEMOVE happens before this!
400 //The above commented out line should take care of this contingency... !!! FIX !!!
401 pts.DeletePoint(ptHighlight);
405 else if (tool == TOOLRotate)
407 // I think what's needed here is to keep the initial mouse click,
408 // paint the rotation center, then use the 1st mouse move event to establish
409 // the rotation "zero line", which becomes the line of reference to all
410 // subsequent mouse moves.
411 rotationCenter = GetAdjustedMousePosition(event);
412 showRotationCenter = true;
413 haveZeroPoint = false;
417 else if (tool == TOOLRotatePoly)
419 IPoint centroid = pts.GetPolyCentroid(pts.GetPolyForPointNumber(ptHighlight));
420 rotationCenter = QPoint(centroid.x, centroid.y);
421 showRotationCenter = true;
422 pt = GetAdjustedClientPosition(pts.GetX(ptHighlight), pts.GetY(ptHighlight));
423 QCursor::setPos(mapToGlobal(pt));
424 rotationZeroPoint = QPoint(pts.GetX(ptHighlight), pts.GetY(ptHighlight));
425 haveZeroPoint = true;
429 else if (tool == TOOLFlipWinding)
431 pts.InvertPolyDrawSequence(pts.GetPolyForPointNumber(ptHighlight));
432 pt = GetAdjustedClientPosition(pts.GetX(ptHighlight), pts.GetY(ptHighlight));
433 QCursor::setPos(mapToGlobal(pt));
442 void EditWindow::mouseMoveEvent(QMouseEvent * event)
444 if (event->buttons() == Qt::RightButton)
446 ToolType newTool = toolPalette->FindSelectedTool();
448 if (newTool != toolPalette->prevTool)
450 toolPalette->prevTool = newTool;
451 toolPalette->repaint();
454 else if (event->buttons() == Qt::MidButton)
456 // Calc offset from previous point
458 ptOffset = QPoint(pt.x() - ptPrevious.x(), pt.y() - ptPrevious.y());
460 // Then multiply it by the scaling factor. Whee!
461 // This looks wacky because we're using screen coords for the offset...
462 // Otherwise, we would subtract both offsets!
463 offsetX -= ptOffset.x(), offsetY += ptOffset.y();
467 else if (event->buttons() == Qt::LeftButton)
469 if (tool == TOOLAddPt || tool == TOOLAddPoly || tool == TOOLSelect)
471 // Bail out if we have the select tool and no points yet...
472 if (tool == TOOLSelect && pts.GetNumPoints() == 0)
475 QPoint pt2 = GetAdjustedMousePosition(event);
476 pts.SetXY(ptHighlight, pt2.x(), pt2.y());
479 else if (tool == TOOLPolySelect)
481 if (pts.GetNumPoints() > 0)
483 QPoint pt2 = GetAdjustedMousePosition(event);
484 // Should also set onCurve here as well, depending on keystate
486 //Would be nice, but we'd need to trap the keyPressEvent() as well, otherwise pressing/releasing
487 //the hotkey would show no change until the user moved their mouse.
488 pts.OffsetPoly(pts.GetPoly(ptHighlight), pt2.x() - pts.GetX(ptHighlight), pt2.y() - pts.GetY(ptHighlight));
492 else if (tool == TOOLRotate || tool == TOOLRotatePoly)
494 if (pts.GetNumPoints() > 0)
498 rotationZeroPoint = GetAdjustedMousePosition(event);
499 haveZeroPoint = true;
503 // Figure out the angle between the "zero" vector and the current one,
504 // then rotate all points relative to the "zero" vector (done by paint())
505 QPoint currentPoint = GetAdjustedMousePosition(event);
506 Vector v1(rotationZeroPoint.x(), rotationZeroPoint.y(), 0,
507 rotationCenter.x(), rotationCenter.y(), 0);
508 Vector v2(currentPoint.x(), currentPoint.y(), 0,
509 rotationCenter.x(), rotationCenter.y(), 0);
510 // rotationAngle = v1.Angle(v2);
511 rotationAngle = v2.Angle(v1);
514 s.sprintf("%.3f degrees", rotationAngle * 180.0 / 3.14159265358979323);
515 ((TTEdit *)qApp)->mainWindow->statusBar()->showMessage(s);
522 else if (event->buttons() == Qt::NoButton)
524 // Moving, not dragging...
525 if (tool == TOOLSelect || tool == TOOLDelPt || tool == TOOLAddPt
526 || tool == TOOLPolySelect || tool == TOOLRotatePoly || tool == TOOLFlipWinding)
528 QPoint pt2 = GetAdjustedMousePosition(event);
529 double closest = 1.0e+99;
531 for(int i=0; i<pts.GetNumPoints(); i++)
533 double dist = ((pt2.x() - pts.GetX(i)) * (pt2.x() - pts.GetX(i)))
534 + ((pt2.y() - pts.GetY(i)) * (pt2.y() - pts.GetY(i)));
537 closest = dist, ptHighlight = i;
540 if (ptHighlight != oldPtHighlight)
542 oldPtHighlight = ptHighlight;
546 // What follows here looks like voodoo, but is really simple. What we do is
547 // check to see if the mouse point has a perpendicular intersection with any of
548 // the line segments. If it does, calculate the length of the perpendicular
549 // and choose the smallest length. If there is no perpendicular, then choose the
550 // length of line connecting the closer of either the first endpoint or the
551 // second and choose the smallest of those.
553 // There is one bit of math that looks like voodoo to me ATM--will explain once
554 // I understand it better (the calculation of the length of the perpendicular).
556 if (pts.GetNumPoints() > 1 && tool == TOOLAddPt)
558 double smallest = 1.0e+99;
560 for(int i=0; i<pts.GetNumPoints(); i++)
562 int32 p1x = pts.GetX(i), p1y = pts.GetY(i),
563 p2x = pts.GetX(pts.GetNext(i)), p2y = pts.GetY(pts.GetNext(i));
565 Vector ls(p2x, p2y, 0, p1x, p1y, 0), v1(pt2.x(), pt2.y(), 0, p1x, p1y, 0),
566 v2(pt2.x(), pt2.y(), 0, p2x, p2y, 0);
567 double pp = ls.Dot(v1) / ls.Magnitude(), dist;
568 // Geometric interpretation:
569 // pp is the paremeterized point on the vector ls where the perpendicular intersects ls.
570 // If pp < 0, then the perpendicular lies beyond the 1st endpoint. If pp > length of ls,
571 // then the perpendicular lies beyond the 2nd endpoint.
574 dist = v1.Magnitude();
575 else if (pp > ls.Magnitude())
576 dist = v2.Magnitude();
577 else // distance = ?Det?(ls, v1) / |ls|
578 dist = fabs((ls.x * v1.y - v1.x * ls.y) / ls.Magnitude());
580 //The answer to the above looks like it might be found here:
582 //If the segment endpoints are s and e, and the point is p, then the test for the perpendicular
583 //intercepting the segment is equivalent to insisting that the two dot products {s-e}.{s-p} and
584 //{e-s}.{e-p} are both non-negative. Perpendicular distance from the point to the segment is
585 //computed by first computing the area of the triangle the three points form, then dividing by the
586 //length of the segment. Distances are done just by the Pythagorean theorem. Twice the area of the
587 //triangle formed by three points is the determinant of the following matrix:
593 //By translating the start point to the origin, this can be rewritten as:
594 //By subtracting row 1 from all rows, you get the following:
595 //[because sx = sy = 0. you could leave out the -sx/y terms below. because we subtracted
596 // row 1 from all rows (including row 1) row 1 turns out to be zero. duh!]
599 //(ex - sx) (ey - sy) 0
600 //(px - sx) (py - sy) 0
602 //which greatly simplifies the calculation of the determinant.
605 smallest = dist, ptNextHighlight = pts.GetNext(i), ptHighlight = i;
608 if (ptNextHighlight != oldPtNextHighlight)
610 oldPtNextHighlight = ptNextHighlight;
616 ptPrevious = event->pos();
623 void EditWindow::mouseReleaseEvent(QMouseEvent * event)
625 if (event->button() == Qt::RightButton)
627 ToolType newTool = toolPalette->FindSelectedTool();
629 // We only change the tool if a new one was actually selected. Otherwise, we do nothing.
630 if (newTool != TOOLNone)
634 if (tool == TOOLScroll || tool == TOOLZoom || tool == TOOLAddPoly
635 || tool == TOOLDelPoly)
638 if (tool == TOOLAddPoly)
639 polyFirstPoint = true;
642 toolPalette->setVisible(false);
643 setCursor(cur[tool]);
644 // Just in case we changed highlighting style with the new tool...
647 else if (event->button() == Qt::MidButton)
649 setCursor(cur[tool]); // Restore previous cursor
651 else if (event->button() == Qt::LeftButton)
653 if (showRotationCenter)
655 showRotationCenter = false;
656 haveZeroPoint = false;
658 if (tool == TOOLRotate)
659 pts.RotatePoints(rotationAngle, IPoint(rotationCenter.x(), rotationCenter.y()));
662 uint16 poly = pts.GetPolyForPointNumber(ptHighlight);
663 pts.RotatePolyAroundCentroid(poly, rotationAngle);
667 ((TTEdit *)qApp)->mainWindow->statusBar()->showMessage("");
670 // if (tool == TOOLScroll || tool == TOOLZoom)
672 //this is prolly too much
673 ((TTEdit *)qApp)->charWnd->MakePathFromPoints(&pts);
674 ((TTEdit *)qApp)->charWnd->update();