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 //dc.SetBackground(*wxWHITE_BRUSH);
123 // Due to the screwiness of wxWidgets coord system, the origin is ALWAYS
124 // the upper left corner--regardless of axis orientation, etc...
125 // int width, height;
126 // dc.GetSize(&width, &height);
127 QSize winSize = size();
129 // dc.SetDeviceOrigin(-offsetX, height - (-offsetY));
130 // dc.SetAxisOrientation(true, true);
131 p.translate(QPoint(-offsetX, winSize.height() - (-offsetY)));
134 // Scrolling can be done by using OffsetViewportOrgEx
135 // Scaling can be done by adjusting SetWindowExtEx (it's denominator of txform)
136 // you'd use: % = ViewportExt / WindowExt
137 // But it makes the window look like crap: fuggetuboutit.
138 // Instead, we have to scale EVERYTHING by hand. Crap!
139 // It's not *that* bad, but not as convenient either...
141 // dc.SetPen(*(wxThePenList->FindOrCreatePen(wxColour(0x00, 0x00, 0xFF), 1, wxDOT)));
142 //// dc.DrawLine(0, 0, 10, 10);
143 p.setPen(QPen(Qt::blue, 1.0, Qt::DotLine));
145 // Draw coordinate axes
147 // dc.CrossHair(0, 0);
148 p.drawLine(0, -16384, 0, 16384);
149 p.drawLine(-16384, 0, 16384, 0);
151 // Draw rotation center (if active)
153 if (showRotationCenter)
155 p.setPen(QPen(Qt::red, 2.0, Qt::SolidLine));
156 p.drawLine(rotationCenter.x() + 7, rotationCenter.y(), rotationCenter.x() - 7, rotationCenter.y());
157 p.drawLine(rotationCenter.x(), rotationCenter.y() + 7, rotationCenter.x(), rotationCenter.y() - 7);
162 for(int i=0; i<pts.GetNumPoints(); i++)
164 if (i == ptHighlight)
166 p.setPen(QPen(Qt::red, 1.0, Qt::SolidLine));
168 if (pts.GetOnCurve(i))
170 DrawSquareDotN(p, pts.GetX(i), pts.GetY(i), 7);
171 DrawSquareDotN(p, pts.GetX(i), pts.GetY(i), 9);
175 DrawRoundDotN(p, pts.GetX(i), pts.GetY(i), 7);
176 DrawRoundDotN(p, pts.GetX(i), pts.GetY(i), 9);
179 else if ((i == ptHighlight || i == ptNextHighlight) && tool == TOOLAddPt)
181 p.setPen(QPen(Qt::green, 1.0, Qt::SolidLine));
183 if (pts.GetOnCurve(i))
185 DrawSquareDotN(p, pts.GetX(i), pts.GetY(i), 7);
186 DrawSquareDotN(p, pts.GetX(i), pts.GetY(i), 9);
190 DrawRoundDotN(p, pts.GetX(i), pts.GetY(i), 7);
191 DrawRoundDotN(p, pts.GetX(i), pts.GetY(i), 9);
196 p.setPen(QPen(Qt::black, 1.0, Qt::SolidLine));
198 if (pts.GetOnCurve(i))
199 DrawSquareDot(p, pts.GetX(i), pts.GetY(i));
201 DrawRoundDot(p, pts.GetX(i), pts.GetY(i));
204 if (tool == TOOLDelPt && i == ptHighlight)
206 p.setPen(QPen(Qt::red, 1.0, Qt::SolidLine));
207 p.drawLine(pts.GetX(i) - 5, pts.GetY(i) - 5, pts.GetX(i) + 5, pts.GetY(i) + 5);
208 p.drawLine(pts.GetX(i) + 5, pts.GetY(i) - 5, pts.GetX(i) - 5, pts.GetY(i) + 5);
212 // Draw curve formed by points
214 p.setPen(QPen(Qt::black, 1.0, Qt::SolidLine));
220 GlyphPoints rotated = pts;
222 if (tool == TOOLRotate)
223 rotated.RotatePoints(rotationAngle, IPoint(rotationCenter.x(), rotationCenter.y()));
224 else if (tool == TOOLRotatePoly)
226 uint16 poly = rotated.GetPolyForPointNumber(ptHighlight);
227 rotated.RotatePolyAroundCentroid(poly, rotationAngle);
230 p.setPen(QPen(QColor(255, 0, 255), 1.0, Qt::SolidLine));
231 DrawGlyph(p, rotated);
236 void EditWindow::DrawGlyph(QPainter & p, GlyphPoints & glyph)
238 for(int poly=0; poly<glyph.GetNumPolys(); poly++)
240 if (glyph.GetNumPoints(poly) > 2)
243 // If it's not on curve, then move to it, otherwise move to last point...
247 if (glyph.GetOnCurve(poly, glyph.GetNumPoints(poly) - 1))
248 x = (int)glyph.GetX(poly, glyph.GetNumPoints(poly) - 1), y = (int)glyph.GetY(poly, glyph.GetNumPoints(poly) - 1);
250 x = (int)glyph.GetX(poly, 0), y = (int)glyph.GetY(poly, 0);
252 for(int i=0; i<glyph.GetNumPoints(poly); i++)
254 if (glyph.GetOnCurve(poly, i))
256 p.drawLine(x, y, glyph.GetX(poly, i), glyph.GetY(poly, i));
257 x = (int)glyph.GetX(poly, i), y = (int)glyph.GetY(poly, i);
261 uint32 prev = glyph.GetPrev(poly, i), next = glyph.GetNext(poly, i);
262 float px = glyph.GetX(poly, prev), py = glyph.GetY(poly, prev),
263 nx = glyph.GetX(poly, next), ny = glyph.GetY(poly, next);
265 if (!glyph.GetOnCurve(poly, prev))
266 px = (px + glyph.GetX(poly, i)) / 2.0f,
267 py = (py + glyph.GetY(poly, i)) / 2.0f;
269 if (!glyph.GetOnCurve(poly, next))
270 nx = (nx + glyph.GetX(poly, i)) / 2.0f,
271 ny = (ny + glyph.GetY(poly, i)) / 2.0f;
273 Bezier(p, point(px, py), point(glyph.GetX(poly, i), glyph.GetY(poly, i)), point(nx, ny));
274 x = (int)nx, y = (int)ny;
276 if (glyph.GetOnCurve(poly, next))
277 i++; // Following point is on curve, so move past it
285 void EditWindow::mousePressEvent(QMouseEvent * event)
287 if (event->button() == Qt::RightButton)
289 toolPalette->move(event->globalPos());
290 toolPalette->setVisible(true);
291 setCursor(cur[TOOLSelect]);
292 toolPalette->prevTool = TOOLSelect;
294 else if (event->button() == Qt::MidButton)
296 setCursor(cur[2]); // Scrolling cursor
298 else if (event->button() == Qt::LeftButton)
300 if (tool == TOOLScroll || tool == TOOLZoom)
301 ;//meh CaptureMouse(); // Make sure we capture the mouse when in scroll/zoom mode
302 else if (tool == TOOLAddPt) // "Add Point" tool
304 if (pts.GetNumPoints() > 0)
306 QPoint pt = GetAdjustedMousePosition(event);
307 pts.InsertPoint(pts.GetNext(ptHighlight), pt.x(), pt.y(), ((event->modifiers() == Qt::ShiftModifier || event->modifiers() == Qt::ControlModifier) ? false : true));
308 ptHighlight = ptNextHighlight;
312 else if (tool == TOOLAddPoly) // "Add Poly" tool
315 WriteLogMsg("Adding point... # polys: %u, # points: %u", pts.GetNumPolys(), pts.GetNumPoints());
319 polyFirstPoint = false;
320 pts.AddNewPolyAtEnd();
323 QPoint pt = GetAdjustedMousePosition(event);
324 //printf("GetAdjustedMousePosition = %i, %i\n", pt.x(), pt.y());
325 // Append a point to the end of the structure
326 pts += IPoint(pt.x(), pt.y(), ((event->modifiers() == Qt::ShiftModifier || event->modifiers() == Qt::ControlModifier) ? false : true));
327 ptHighlight = pts.GetNumPoints() - 1;
330 WriteLogMsg(" --> [# polys: %u, # points: %u]\n", pts.GetNumPolys(), pts.GetNumPoints());
333 else if (tool == TOOLSelect || tool == TOOLPolySelect)
335 if (pts.GetNumPoints() > 0)
337 pt = GetAdjustedClientPosition(pts.GetX(ptHighlight), pts.GetY(ptHighlight));
338 //printf("GetAdjustedClientPosition = %i, %i\n", pt.x(), pt.y());
339 // WarpPointer(pt.x, pt.y);
340 QCursor::setPos(mapToGlobal(pt));
342 if (event->modifiers() == Qt::ShiftModifier || event->modifiers() == Qt::ControlModifier)
344 pts.SetOnCurve(ptHighlight, !pts.GetOnCurve(ptHighlight));
349 else if (tool == TOOLDelPt)
351 if (pts.GetNumPoints() > 0)
353 // if (ptHighlight != -1)
355 //This assumes that WM_MOUSEMOVE happens before this!
356 //The above commented out line should take care of this contingency... !!! FIX !!!
357 pts.DeletePoint(ptHighlight);
361 else if (tool == TOOLRotate)
363 // I think what's needed here is to keep the initial mouse click,
364 // paint the rotation center, then use the 1st mouse move event to establish
365 // the rotation "zero line", which becomes the line of reference to all
366 // subsequent mouse moves.
367 rotationCenter = GetAdjustedMousePosition(event);
368 showRotationCenter = true;
369 haveZeroPoint = false;
373 else if (tool == TOOLRotatePoly)
375 IPoint centroid = pts.GetPolyCentroid(pts.GetPolyForPointNumber(ptHighlight));
376 rotationCenter = QPoint(centroid.x, centroid.y);
377 showRotationCenter = true;
378 pt = GetAdjustedClientPosition(pts.GetX(ptHighlight), pts.GetY(ptHighlight));
379 QCursor::setPos(mapToGlobal(pt));
380 rotationZeroPoint = QPoint(pts.GetX(ptHighlight), pts.GetY(ptHighlight));
381 haveZeroPoint = true;
385 else if (tool == TOOLFlipWinding)
387 // IPoint centroid = pts.GetPolyCentroid(pts.GetPolyForPointNumber(ptHighlight));
388 // rotationCenter = QPoint(centroid.x, centroid.y);
389 // showRotationCenter = true;
390 pts.InvertPolyDrawSequence(pts.GetPolyForPointNumber(ptHighlight));
391 pt = GetAdjustedClientPosition(pts.GetX(ptHighlight), pts.GetY(ptHighlight));
392 QCursor::setPos(mapToGlobal(pt));
393 // rotationZeroPoint = QPoint(pts.GetX(ptHighlight), pts.GetY(ptHighlight));
394 // haveZeroPoint = true;
395 // rotationAngle = 0;
397 // ((TTEdit *)qApp)->charWnd->MakePathFromPoints(&pts);
398 // ((TTEdit *)qApp)->charWnd->update();
406 void EditWindow::mouseMoveEvent(QMouseEvent * event)
408 if (event->buttons() == Qt::RightButton)
410 ToolType newTool = toolPalette->FindSelectedTool();
412 if (newTool != toolPalette->prevTool)
414 toolPalette->prevTool = newTool;
415 toolPalette->repaint();
418 else if (event->buttons() == Qt::MidButton)
420 // Calc offset from previous point
422 ptOffset = QPoint(pt.x() - ptPrevious.x(), pt.y() - ptPrevious.y());
424 // Then multiply it by the scaling factor. Whee!
425 // This looks wacky because we're using screen coords for the offset...
426 // Otherwise, we would subtract both offsets!
427 offsetX -= ptOffset.x(), offsetY += ptOffset.y();
431 else if (event->buttons() == Qt::LeftButton)
433 if (tool == TOOLAddPt || tool == TOOLAddPoly || tool == TOOLSelect)
435 if (tool != TOOLAddPt || pts.GetNumPoints() > 0)//yecch.
437 //temporary, for testing. BTW, Select drag bug is here...!
439 QPoint pt2 = GetAdjustedMousePosition(event);
440 pts.SetXY(ptHighlight, pt2.x(), pt2.y());
445 else if (tool == TOOLPolySelect)
447 if (pts.GetNumPoints() > 0)
449 QPoint pt2 = GetAdjustedMousePosition(event);
450 // Should also set onCurve here as well, depending on keystate
452 pts.OffsetPoly(pts.GetPoly(ptHighlight), pt2.x() - pts.GetX(ptHighlight), pt2.y() - pts.GetY(ptHighlight));
456 else if (tool == TOOLRotate || tool == TOOLRotatePoly)
458 if (pts.GetNumPoints() > 0)
462 rotationZeroPoint = GetAdjustedMousePosition(event);
463 haveZeroPoint = true;
467 // Figure out the angle between the "zero" vector and the current one,
468 // then rotate all points relative to the "zero" vector (done by paint())
469 QPoint currentPoint = GetAdjustedMousePosition(event);
470 Vector v1(rotationZeroPoint.x(), rotationZeroPoint.y(), 0,
471 rotationCenter.x(), rotationCenter.y(), 0);
472 Vector v2(currentPoint.x(), currentPoint.y(), 0,
473 rotationCenter.x(), rotationCenter.y(), 0);
474 // rotationAngle = v1.Angle(v2);
475 rotationAngle = v2.Angle(v1);
478 s.sprintf("%.3f degrees", rotationAngle * 180.0 / 3.14159265358979323);
479 ((TTEdit *)qApp)->mainWindow->statusBar()->showMessage(s);
486 else if (event->buttons() == Qt::NoButton)
488 // Moving, not dragging...
489 if (tool == TOOLSelect || tool == TOOLDelPt || tool == TOOLAddPt
490 || tool == TOOLPolySelect || tool == TOOLRotatePoly || tool == TOOLFlipWinding)
492 QPoint pt2 = GetAdjustedMousePosition(event);
493 double closest = 1.0e+99;
495 for(int i=0; i<pts.GetNumPoints(); i++)
497 double dist = ((pt2.x() - pts.GetX(i)) * (pt2.x() - pts.GetX(i)))
498 + ((pt2.y() - pts.GetY(i)) * (pt2.y() - pts.GetY(i)));
501 closest = dist, ptHighlight = i;
504 if (ptHighlight != oldPtHighlight)
506 oldPtHighlight = ptHighlight;
510 // What follows here looks like voodoo, but is really simple. What we do is
511 // check to see if the mouse point has a perpendicular intersection with any of
512 // the line segments. If it does, calculate the length of the perpendicular
513 // and choose the smallest length. If there is no perpendicular, then choose the
514 // length of line connecting the closer of either the first endpoint or the
515 // second and choose the smallest of those.
517 // There is one bit of math that looks like voodoo to me ATM--will explain once
518 // I understand it better (the calculation of the length of the perpendicular).
520 if (pts.GetNumPoints() > 1 && tool == TOOLAddPt)
522 double smallest = 1.0e+99;
524 for(int i=0; i<pts.GetNumPoints(); i++)
526 int32 p1x = pts.GetX(i), p1y = pts.GetY(i),
527 p2x = pts.GetX(pts.GetNext(i)), p2y = pts.GetY(pts.GetNext(i));
529 Vector ls(p2x, p2y, 0, p1x, p1y, 0), v1(pt2.x(), pt2.y(), 0, p1x, p1y, 0),
530 v2(pt2.x(), pt2.y(), 0, p2x, p2y, 0);
531 double pp = ls.Dot(v1) / ls.Magnitude(), dist;
532 // Geometric interpretation:
533 // pp is the paremeterized point on the vector ls where the perpendicular intersects ls.
534 // If pp < 0, then the perpendicular lies beyond the 1st endpoint. If pp > length of ls,
535 // then the perpendicular lies beyond the 2nd endpoint.
538 dist = v1.Magnitude();
539 else if (pp > ls.Magnitude())
540 dist = v2.Magnitude();
541 else // distance = ?Det?(ls, v1) / |ls|
542 dist = fabs((ls.x * v1.y - v1.x * ls.y) / ls.Magnitude());
544 //The answer to the above looks like it might be found here:
546 //If the segment endpoints are s and e, and the point is p, then the test for the perpendicular
547 //intercepting the segment is equivalent to insisting that the two dot products {s-e}.{s-p} and
548 //{e-s}.{e-p} are both non-negative. Perpendicular distance from the point to the segment is
549 //computed by first computing the area of the triangle the three points form, then dividing by the
550 //length of the segment. Distances are done just by the Pythagorean theorem. Twice the area of the
551 //triangle formed by three points is the determinant of the following matrix:
557 //By translating the start point to the origin, this can be rewritten as:
558 //By subtracting row 1 from all rows, you get the following:
559 //[because sx = sy = 0. you could leave out the -sx/y terms below. because we subtracted
560 // row 1 from all rows (including row 1) row 1 turns out to be zero. duh!]
563 //(ex - sx) (ey - sy) 0
564 //(px - sx) (py - sy) 0
566 //which greatly simplifies the calculation of the determinant.
569 smallest = dist, ptNextHighlight = pts.GetNext(i), ptHighlight = i;
572 if (ptNextHighlight != oldPtNextHighlight)
574 oldPtNextHighlight = ptNextHighlight;
580 ptPrevious = event->pos();
587 void EditWindow::mouseReleaseEvent(QMouseEvent * event)
589 if (event->button() == Qt::RightButton)
591 ToolType newTool = toolPalette->FindSelectedTool();
593 // We only change the tool if a new one was actually selected. Otherwise, we do nothing.
594 if (newTool != TOOLNone)
598 if (tool == TOOLScroll || tool == TOOLZoom || tool == TOOLAddPoly
599 || tool == TOOLDelPoly)
602 if (tool == TOOLAddPoly)
603 polyFirstPoint = true;
606 toolPalette->setVisible(false);
607 setCursor(cur[tool]);
608 // Just in case we changed highlighting style with the new tool...
611 else if (event->button() == Qt::MidButton)
613 setCursor(cur[tool]); // Restore previous cursor
615 else if (event->button() == Qt::LeftButton)
617 if (showRotationCenter)
619 showRotationCenter = false;
620 haveZeroPoint = false;
622 if (tool == TOOLRotate)
623 pts.RotatePoints(rotationAngle, IPoint(rotationCenter.x(), rotationCenter.y()));
626 uint16 poly = pts.GetPolyForPointNumber(ptHighlight);
627 pts.RotatePolyAroundCentroid(poly, rotationAngle);
631 ((TTEdit *)qApp)->mainWindow->statusBar()->showMessage("");
634 // if (tool == TOOLScroll || tool == TOOLZoom)
636 //this is prolly too much
637 ((TTEdit *)qApp)->charWnd->MakePathFromPoints(&pts);
638 ((TTEdit *)qApp)->charWnd->update();