- // We only change the tool if a new one was actually selected. Otherwise, we do nothing.
- if (newTool != TOOLNone)
- {
- tool = newTool;
-
- if (tool == TOOLScroll || tool == TOOLZoom || tool == TOOLAddPoly
- || tool == TOOLDelPoly)
- ptHighlight = -1;
-
- if (tool == TOOLAddPoly)
- polyFirstPoint = true;
- }
-
- ReleaseMouse();
- app.toolPalette->Show(false);
- SetCursor(*(app.cur[tool]));
- }
- else if (e.LeftDown())
- {
- if (tool == TOOLScroll || tool == TOOLZoom)
- CaptureMouse(); // Make sure we capture the mouse when in scroll/zoom mode
- else if (tool == TOOLAddPt) // "Add Point" tool
- {
- if (pts.GetNumPoints() > 0)
- {
- wxPoint pt = GetAdjustedMousePosition(e);
- pts.InsertPoint(pts.GetNext(ptHighlight), pt.x, pt.y, (e.ShiftDown() | e.ControlDown() ? false : true));
- ptHighlight = ptNextHighlight;
- Refresh();
- }
- }
- else if (tool == TOOLAddPoly) // "Add Poly" tool
- {
-#ifdef DEBUGFOO
-WriteLogMsg("Adding point... # polys: %u, # points: %u", pts.GetNumPolys(), pts.GetNumPoints());
-#endif
- if (polyFirstPoint)
- {
- polyFirstPoint = false;
- pts.AddNewPolyAtEnd();
- }
-
- wxPoint pt = GetAdjustedMousePosition(e);
- // Append a point to the end of the structure
- pts += IPoint(pt.x, pt.y, (e.ShiftDown() | e.ControlDown() ? false : true));
- ptHighlight = pts.GetNumPoints() - 1;
- Refresh();
-#ifdef DEBUGFOO
-WriteLogMsg(" --> [# polys: %u, # points: %u]\n", pts.GetNumPolys(), pts.GetNumPoints());
-#endif
- }
- else if (tool == TOOLSelect || tool == TOOLPolySelect)
- {
- if (pts.GetNumPoints() > 0)
- {
- pt = GetAdjustedClientPosition(pts.GetX(ptHighlight), pts.GetY(ptHighlight));
- WarpPointer(pt.x, pt.y);
-
- if (e.ShiftDown() | e.ControlDown())
- pts.SetOnCurve(ptHighlight, !pts.GetOnCurve(ptHighlight));
- }
- }
- else if (tool == TOOLDelPt)
- {
- if (pts.GetNumPoints() > 0)
-//Or could use:
-// if (ptHighlight != -1)
- {
-//This assumes that WM_MOUSEMOVE happens before this!
-//The above commented out line should take care of this contingency... !!! FIX !!!
- pts.DeletePoint(ptHighlight);
- Refresh();
- }
- }
- }
- else if (e.LeftUp())
- {
- if (tool == TOOLScroll || tool == TOOLZoom)
- ReleaseMouse();
- }
- else if (e.MiddleDown())
- {
- SetCursor(*(app.cur[2])); // Scrolling cursor
- }
- else if (e.MiddleUp())
- {
- SetCursor(*(app.cur[tool])); // Restore previous cursor
- }
- else if (e.Dragging())
- {
- if (e.RightIsDown())
- {
- ToolType newTool = app.toolPalette->FindSelectedTool();
-
- if (newTool != app.toolPalette->prevTool)
- {
- app.toolPalette->prevTool = newTool;
- app.toolPalette->Refresh(false);
- }
-
-// return;
- }
- else if (e.MiddleIsDown())
- {
- // Calc offset from previous point
- pt = e.GetPosition();
- ptOffset.x = pt.x - ptPrevious.x,
- ptOffset.y = pt.y - ptPrevious.y;
-
-// Then multiply it by the scaling factor. Whee!
- // This looks wacky because we're using screen coords for the offset...
- // Otherwise, we would subtract both offsets!
- offsetX -= ptOffset.x, offsetY += ptOffset.y;
- Refresh();
-// ptPrevious = pt;
-
-// return;
- }
-
- else if (e.LeftIsDown())
- {
-#if 0
- if (tool == TOOLScroll)
- {
- // Extract current point from lParam/calc offset from previous point
-
- pt = e.GetPosition();
- ptOffset.x = pt.x - ptPrevious.x,
- ptOffset.y = pt.y - ptPrevious.y;
-
- // NOTE: OffsetViewportOrg operates in DEVICE UNITS...
-
-//Seems there's no equivalent for this in wxWidgets...!
-//!!! FIX !!!
-// hdc = GetDC(hWnd);
-// OffsetViewportOrgEx(hdc, ptOffset.x, ptOffset.y, NULL);
-// ReleaseDC(hWnd, hdc);
-
-// this shows that it works, so the logic above must be faulty...
-// And it is. It should convert the coords first, then do the subtraction to figure the offset...
-// Above: DONE
-// Then multiply it by the scaling factor. Whee!
- // This looks wacky because we're using screen coords for the offset...
- // Otherwise, we would subtract both offsets!
- offsetX -= ptOffset.x, offsetY += ptOffset.y;
- Refresh();
- }
- else
-#endif
- if (tool == TOOLAddPt || tool == TOOLAddPoly || tool == TOOLSelect)
- {
- if (tool != TOOLAddPt || pts.GetNumPoints() > 0)//yecch.
- {
-//temporary, for testing. BTW, Select drag bug is here...!
-#if 1
- wxPoint pt2 = GetAdjustedMousePosition(e);
- pts.SetXY(ptHighlight, pt2.x, pt2.y);
- Refresh();
-#endif
- }
- }
- else if (tool == TOOLPolySelect)
- {
- if (pts.GetNumPoints() > 0)
- {
- wxPoint pt2 = GetAdjustedMousePosition(e);
- // Should also set onCurve here as well, depending on keystate
-//Or should we?
- pts.OffsetPoly(pts.GetPoly(ptHighlight), pt2.x - pts.GetX(ptHighlight), pt2.y - pts.GetY(ptHighlight));
- Refresh();
- }
- }
- }
-
- ptPrevious = pt;
- }
- else if (e.Moving())
- {
-// else // Moving, not dragging...
-// {
- if (tool == TOOLSelect || tool == TOOLDelPt || tool == TOOLAddPt
- || tool == TOOLPolySelect)// || tool == TOOLAddPoly)
- {
- wxPoint pt2 = GetAdjustedMousePosition(e);
- double closest = 1.0e+99;
-
- for(int i=0; i<pts.GetNumPoints(); i++)
- {
- double dist = ((pt2.x - pts.GetX(i)) * (pt2.x - pts.GetX(i)))
- + ((pt2.y - pts.GetY(i)) * (pt2.y - pts.GetY(i)));
-
- if (dist < closest)
- closest = dist, ptHighlight = i;
- }
-
- if (ptHighlight != oldPtHighlight)
- {
- oldPtHighlight = ptHighlight;
- Refresh();
- }
-
- // What follows here looks like voodoo, but is really simple. What we do is
- // check to see if the mouse point has a perpendicular intersection with any of
- // the line segments. If it does, calculate the length of the perpendicular
- // and choose the smallest length. If there is no perpendicular, then choose the
- // length of line connecting the closer of either the first endpoint or the
- // second and choose the smallest of those.
-
- // There is one bit of math that looks like voodoo to me ATM--will explain once
- // I understand it better (the calculation of the length of the perpendicular).
-
- if (pts.GetNumPoints() > 1 && tool == TOOLAddPt)
- {
- double smallest = 1.0e+99;
-
- for(int i=0; i<pts.GetNumPoints(); i++)
- {
- int32 p1x = pts.GetX(i), p1y = pts.GetY(i),
- p2x = pts.GetX(pts.GetNext(i)), p2y = pts.GetY(pts.GetNext(i));
-
- vector ls(p2x, p2y, 0, p1x, p1y, 0), v1(pt2.x, pt2.y, 0, p1x, p1y, 0),
- v2(pt2.x, pt2.y, 0, p2x, p2y, 0);
- double pp = ls.dot(v1) / ls.length(), dist;
-// Geometric interpretation:
-// pp is the paremeterized point on the vector ls where the perpendicular intersects ls.
-// If pp < 0, then the perpendicular lies beyond the 1st endpoint. If pp > length of ls,
-// then the perpendicular lies beyond the 2nd endpoint.
-
- if (pp < 0.0)
- dist = v1.length();
- else if (pp > ls.length())
- dist = v2.length();
- else // distance = ?Det?(ls, v1) / |ls|
- dist = fabs((ls.x * v1.y - v1.x * ls.y) / ls.length());
-
-//The answer to the above looks like it might be found here:
-//
-//If the segment endpoints are s and e, and the point is p, then the test for the perpendicular
-//intercepting the segment is equivalent to insisting that the two dot products {s-e}.{s-p} and
-//{e-s}.{e-p} are both non-negative. Perpendicular distance from the point to the segment is
-//computed by first computing the area of the triangle the three points form, then dividing by the
-//length of the segment. Distances are done just by the Pythagorean theorem. Twice the area of the
-//triangle formed by three points is the determinant of the following matrix:
-//
-//sx sy 1
-//ex ey 1
-//px py 1
-//
-//(???) By translating the start point to the origin, this can be rewritten as:
-//By subtracting row 1 from all rows, you get the following:
-//
-//0 0 0
-//(ex - sx) (ey - sy) 0
-//(px - sx) (py - sy) 0
-//
-//which greatly simplifies the calculation of the determinant.
-
- if (dist < smallest)
- smallest = dist, ptNextHighlight = pts.GetNext(i), ptHighlight = i;
- }
-
- if (ptNextHighlight != oldPtNextHighlight)
- {
- oldPtNextHighlight = ptNextHighlight;
- Refresh();
- }
- }
- }
-// }
-
- ptPrevious = e.GetPosition();
- }
-}
-
-wxPoint TTEditWindow::GetAdjustedMousePosition(wxMouseEvent &e)
-{
- wxCoord width, height;
- wxClientDC dc(this);
-
- dc.GetSize(&width, &height);
- dc.SetDeviceOrigin(-offsetX, height - (-offsetY));
- dc.SetAxisOrientation(true, true);
-
-#if 0
-wxStatusBar * sb = ((wxFrame *)GetParent())->GetStatusBar();
-wxString s;
-s.Printf("Logical mouse pos: %d, %d (%d, %d)", pt.x, pt.y, width, height);
-sb->SetStatusText(s);
-#endif
-
- return e.GetLogicalPosition(dc);
-}
-
-wxPoint TTEditWindow::GetAdjustedClientPosition(wxCoord x, wxCoord y)
-{
- wxCoord width, height;
- wxClientDC dc(this);
-
- dc.GetSize(&width, &height);
- dc.SetDeviceOrigin(-offsetX, height - (-offsetY));
- dc.SetAxisOrientation(true, true);
-
- return wxPoint(dc.LogicalToDeviceX(x), dc.LogicalToDeviceY(y));
-}
-
-
-#if 0
-
-!!! OLD STUFF !!!
-
-
-LRESULT CALLBACK WndProc(HWND hWnd, UINT msgID, WPARAM wParam, LPARAM lParam)
-{
- RECT rc1, rc2;
- HDC hdc;
- POINT pt, ptOffset;
- SIZE sz;
- PAINTSTRUCT ps;
-
- switch (msgID)
- {
- case WM_CREATE:
-
- MiscCenterWnd(hWnd, GetDesktopWindow());
- InitCommonControls();
- hStatusBar = CreateStatusWindow(WS_CHILD | WS_VISIBLE, statusBarTxt, hWnd, ID_STATUSBAR);
-
- if (!hStatusBar)
- return -1;
-
-// clean this crap up!
-// well, the only crappy thing here is using a POINT as an int array, but otherwise, this is OK
- wsprintf(strBuf, zoom, 1000);
- hdc = GetDC(hWnd);
- GetTextExtentPoint32(hdc, strBuf, lstrlen(strBuf), &sz);
- ReleaseDC(hWnd, hdc);
- zoomWndWidth = sz.cx;
- wsprintf(strBuf, zoom, 100);
-
- GetClientRect(hWnd, &rc1);
- pt.x = rc1.right - zoomWndWidth, pt.y = -1;
- SendMessage(hStatusBar, SB_SETPARTS, 2, (LPARAM)&pt);
- SendMessage(hStatusBar, SB_SETTEXT, (0 | SBT_NOBORDERS), (LPARAM)statusBarTxt);
- SendMessage(hStatusBar, SB_SETTEXT, 1, (LPARAM)strBuf);
-
- hToolBar = CreateToolbarEx(hWnd, WS_CHILD | WS_BORDER | WS_VISIBLE | TBSTYLE_TOOLTIPS,
- IDR_TOOLBAR1, 3, hInst, IDB_TOOLBAR1, tbButtons, 4, 16, 16, 16, 16, sizeof(TBBUTTON));
-
- if (!hToolBar)
- return -1;
-
- CreateNewDoc();
-
-// The following can only be done because we use a private DC (using "CS_OWNDC")
-// (Is that true???)
-// Set the mapping to draw the character so it fits in the viewport...
- hdc = GetDC(hWnd);
- GetClientRect(hWnd, &rc1);
- GetClientRect(hStatusBar, &rc2);
- rc1.bottom -= rc2.bottom;
- SetMapMode(hdc, MM_ISOTROPIC);
- SetWindowExtEx(hdc, rc1.right, rc1.bottom, NULL);
- SetViewportExtEx(hdc, rc1.right, -rc1.bottom, NULL);
- SetViewportOrgEx(hdc, 0, rc1.bottom, NULL);
- ReleaseDC(hWnd, hdc);
- break;
-
- case WM_CLOSE:
-
- if (SaveChanges())
- {
- wpM.length = wpC.length = sizeof(WINDOWPLACEMENT);
- GetWindowPlacement(hMainWnd, &wpM);
- GetWindowPlacement(hCharWnd, &wpC);
-
- if (!IsWindowVisible(hCharWnd)) // Needed because Windows lies about visibility
- wpC.showCmd = SW_HIDE;
-
- hdc = GetDC(hWnd);
- GetViewportOrgEx(hdc, &ptVPM);
- ReleaseDC(hWnd, hdc);
-
- DestroyWindow(hWnd);
- }
-
- break;
-
- case WM_DESTROY:
-
- PostQuitMessage(0);
- break;
-
- case WM_NCLBUTTONDOWN:
-
- if (wParam == HTCAPTION)
- {
- NCMouseDown = true;
- GetWindowRect(hMainWnd, &rc1);
- GetWindowRect(hCharWnd, &rc2);
- ptWinOffset.x = rc2.left - rc1.left;
- ptWinOffset.y = rc2.top - rc1.top;
- }
-
- // Let Windows do its thing with this msg, or weird things will happen...
-
- DefWindowProc(hWnd, msgID, wParam, lParam);
- NCMouseDown = false;
- break;
-
- case WM_WINDOWPOSCHANGING:
-
- if (NCMouseDown)
- {
- WINDOWPOS * wp = (WINDOWPOS *)lParam;
-
- if (wp->hwnd == hMainWnd && !(wp->flags & SWP_NOMOVE))
- SetWindowPos(hCharWnd, 0, wp->x + ptWinOffset.x, wp->y + ptWinOffset.y,
- 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
- }
-
- return DefWindowProc(hWnd, msgID, wParam, lParam); // Seems this is needed... Bleah!
-
- case WM_PAINT:
- {
- hdc = BeginPaint(hWnd, &ps);
-
-// Scrolling can be done by using OffsetViewportOrgEx
-// Scaling can be done by adjusting SetWindowExtEx (it's denominator of txform)
-// you'd use: % = ViewportExt / WindowExt
-// But it makes the window look like crap: fuggetuboutit.
-// Instead, we have to scale EVERYTHING by hand. Crap!
-
- // Apparently, you *must* save the individual object types (pen, brush, etc.)
-
- HGDIOBJ oldPen = SelectObject(hdc, hBluePen1),
- oldBrush = SelectObject(hdc, hNullBrush);
-
- // Draw coordinate axes
-
- MoveToEx(hdc, 0, -32000, NULL);
- LineTo(hdc, 0, 32000);
- MoveToEx(hdc, -32000, 0, NULL);
- LineTo(hdc, 32000, 0);
-
- // Draw points
-
- for(int i=0; i<pts.GetNumPoints(); i++)
- {
- if (i == ptHighlight)
- {
- SelectObject(hdc, hRedPen1);
-
- if (pts.GetOnCurve(i))
- {
- DrawSquareDotN(hdc, pts.GetX(i), pts.GetY(i), 7);
- DrawSquareDotN(hdc, pts.GetX(i), pts.GetY(i), 9);
- }
- else
- {
- DrawRoundDotN(hdc, pts.GetX(i), pts.GetY(i), 7);
- DrawRoundDotN(hdc, pts.GetX(i), pts.GetY(i), 9);
- }
- }
- else if ((i == ptHighlight || i == ptNextHighlight) && currentTool == TOOLAddPt)
- {
- SelectObject(hdc, hGreenPen1);
-
- if (pts.GetOnCurve(i))
- {
- DrawSquareDotN(hdc, pts.GetX(i), pts.GetY(i), 7);
- DrawSquareDotN(hdc, pts.GetX(i), pts.GetY(i), 9);
- }
- else
- {
- DrawRoundDotN(hdc, pts.GetX(i), pts.GetY(i), 7);
- DrawRoundDotN(hdc, pts.GetX(i), pts.GetY(i), 9);
- }
- }
- else
- {
- SelectObject(hdc, hBlackPen1);
-
- if (pts.GetOnCurve(i))
- DrawSquareDot(hdc, pts.GetX(i), pts.GetY(i));
- else
- DrawRoundDot(hdc, pts.GetX(i), pts.GetY(i));
- }
-
- if (currentTool == TOOLDelPt && i == ptHighlight)
- {
- 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!!
- }
- }
-
- SelectObject(hdc, hBlackPen1);
-
- // Draw curve formed by points
-
- for(int poly=0; poly<pts.GetNumPolys(); poly++)
- {
- if (pts.GetNumPoints(poly) > 2)
- {
- // Initial move...
- // If it's not on curve, then move to it, otherwise move to last point...
-
- if (pts.GetOnCurve(poly, pts.GetNumPoints(poly) - 1))
- MoveToEx(hdc, pts.GetX(poly, pts.GetNumPoints(poly) - 1), pts.GetY(poly, pts.GetNumPoints(poly) - 1), NULL);
- else
- MoveToEx(hdc, pts.GetX(poly, 0), pts.GetY(poly, 0), NULL);
-
- for(int i=0; i<pts.GetNumPoints(poly); i++)
- {
- if (pts.GetOnCurve(poly, i))
- LineTo(hdc, pts.GetX(poly, i), pts.GetY(poly, i));
- else
- {
- uint32 prev = pts.GetPrev(poly, i), next = pts.GetNext(poly, i);
- float px = pts.GetX(poly, prev), py = pts.GetY(poly, prev),
- nx = pts.GetX(poly, next), ny = pts.GetY(poly, next);
-
- if (!pts.GetOnCurve(poly, prev))
- px = (px + pts.GetX(poly, i)) / 2.0f,
- py = (py + pts.GetY(poly, i)) / 2.0f;
-
- if (!pts.GetOnCurve(poly, next))
- nx = (nx + pts.GetX(poly, i)) / 2.0f,
- ny = (ny + pts.GetY(poly, i)) / 2.0f;
-
- Bezier(hdc, point(px, py), point(pts.GetX(poly, i), pts.GetY(poly, i)), point(nx, ny));
-
- if (pts.GetOnCurve(poly, next))
- i++; // Following point is on curve, so move past it
- }
- }
- }
- }
-
- SelectObject(hdc, oldPen); // Restore the stuff we disrupted...
- SelectObject(hdc, oldBrush);
- EndPaint(hWnd, &ps);
- break;
- }
- case WM_SIZE:
-
- // Apparently this is needed since these windows don't update themselves.
- SendMessage(hStatusBar, msgID, wParam, lParam);
- SendMessage(hToolBar, msgID, wParam, lParam);
-
- // This is needed to make the 2nd status pane visible
- GetClientRect(hWnd, &rc1);
- pt.x = rc1.right - zoomWndWidth, pt.y = -1;
- SendMessage(hStatusBar, SB_SETPARTS, 2, (LPARAM)&pt);
- break;
-
- case WM_RBUTTONDOWN:
-
- GetCursorPos(&pt);
- SetWindowPos(hToolPalWnd, 0, pt.x, pt.y, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_SHOWWINDOW);
- SetFocus(hToolPalWnd);
- SetCapture(hToolPalWnd); // Ensure tool palette gets RButtonUp
- SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW))); // Tool pallete has "regular" cursor
- break;
-
- case WM_LBUTTONDOWN:
-
- mouseDown = true;
-
- if (currentTool == TOOLScroll || currentTool == TOOLZoom)
- SetCapture(hWnd); // Make sure we capture the mouse when in scroll/zoom mode
- else if (currentTool == TOOLAddPt) // "Add Point" tool
- {
- if (pts.GetNumPoints() > 0)
- {
-//Do we really need to put a cap on this???
-//Maybe...
-// if (pts.GetNumPoints() < 16)
-// {
- pt.x = lParam & 0xFFFF, pt.y = lParam >> 16;
- hdc = GetDC(hWnd);
- DPtoLP(hdc, &pt, 1);
- pts.InsertPoint(pts.GetNext(ptHighlight), pt.x, pt.y, (wParam & (MK_SHIFT | MK_CONTROL) ? false : true));
- ptHighlight = ptNextHighlight;
- ReleaseDC(hWnd, hdc);
- InvalidateRect(hWnd, NULL, TRUE);
-// }
- }
- }
- else if (currentTool == TOOLAddPoly) // "Add Poly" tool
- {
-#ifdef DEBUGFOO
-wsprintf(strBuf, "Adding point... # polys: %u, # points: %u", pts.GetNumPolys(), pts.GetNumPoints());
-WriteLogMsg(strBuf);
-#endif
- if (polyFirstPoint)
- {
- polyFirstPoint = false;
- pts.AddNewPolyAtEnd();
- }
-
-//Do we really need to put a cap on this???
-//Maybe...
-// if (pts.GetNumPoints() < 16)
-// {
- pt.x = lParam & 0xFFFF, pt.y = lParam >> 16;
- hdc = GetDC(hWnd);
- DPtoLP(hdc, &pt, 1);
- ReleaseDC(hWnd, hdc);
- // Append a point to the end of the structure
- pts += IPoint(pt.x, pt.y, (wParam & (MK_SHIFT | MK_CONTROL) ? false : true));
-ptHighlight = pts.GetNumPoints() - 1;
- InvalidateRect(hWnd, NULL, TRUE);
-// }
-#ifdef DEBUGFOO
-wsprintf(strBuf, " --> [# polys: %u, # points: %u]\xD\xA", pts.GetNumPolys(), pts.GetNumPoints());
-WriteLogMsg(strBuf);
-#endif
- }
- else if (currentTool == TOOLSelect || currentTool == TOOLPolySelect)
- {
- if (pts.GetNumPoints() > 0)
- {
- pt.x = pts.GetX(ptHighlight), pt.y = pts.GetY(ptHighlight);
- hdc = GetDC(hWnd);
- LPtoDP(hdc, &pt, 1);
- ClientToScreen(hWnd, &pt);
- SetCursorPos(pt.x, pt.y);
- ReleaseDC(hWnd, hdc);
-
- if (wParam & (MK_SHIFT | MK_CONTROL))
- pts.SetOnCurve(ptHighlight, !pts.GetOnCurve(ptHighlight));
- }
- }
- else if (currentTool == TOOLDelPt)
- {
- if (pts.GetNumPoints() > 0)
-//Or could use:
-// if (ptHighlight != -1)
- {
-//This assumes that WM_MOUSEMOVE happens before this!
-//The above commented out line should take care of this contingency... !!! FIX !!!
- pts.DeletePoint(ptHighlight);
- InvalidateRect(hWnd, NULL, TRUE);
- }
- }
-
- break;
-
- case WM_LBUTTONUP:
-
- mouseDown = false;
-
- if (currentTool == TOOLScroll || currentTool == TOOLZoom)
- ReleaseCapture();
-
- break;
-
- case WM_MOUSEMOVE:
-
- SetCursor(hCur[currentTool]);
-
- // Extract current point from lParam/calc offset from previous point
-
- pt.x = lParam & 0xFFFF, pt.y = lParam >> 16;
- ptOffset.x = pt.x - ptPrevious.x,
- ptOffset.y = pt.y - ptPrevious.y;
-
- if (mouseDown)
- {
- if (currentTool == TOOLScroll)
- {
- // NOTE: OffsetViewportOrg operates in DEVICE UNITS...
-
- hdc = GetDC(hWnd);
- OffsetViewportOrgEx(hdc, ptOffset.x, ptOffset.y, NULL);
- ReleaseDC(hWnd, hdc);
-
-// this shows that it works, so the logic above must be faulty...
-// And it is. It should convert the coords first, then do the subtraction to figure the offset...
-// Above: DONE
-// Then multiply it by the scaling factor. Whee!
-
- InvalidateRect(hWnd, NULL, TRUE);
-// SendMessage(hWnd, WM_PAINT, NULL, NULL);
- }
- else if (currentTool == TOOLAddPt || currentTool == TOOLAddPoly || currentTool == TOOLSelect)
- {
- if (currentTool != TOOLAddPt || pts.GetNumPoints() > 0)//yecch.
- {
- POINT pt2;
- pt2.x = pt.x, pt2.y = pt.y;
- // Should also set onCurve here as well, depending on keystate
-//Or should we?
- hdc = GetDC(hWnd);
- DPtoLP(hdc, &pt2, 1);
- pts.SetXY(ptHighlight, pt2.x, pt2.y);
- ReleaseDC(hWnd, hdc);
- InvalidateRect(hWnd, NULL, TRUE);
- }
- }
- else if (currentTool == TOOLPolySelect)
- {
- if (pts.GetNumPoints() > 0)
- {
- POINT pt2;
- pt2.x = pt.x, pt2.y = pt.y;
- // Should also set onCurve here as well, depending on keystate
-//Or should we?
- hdc = GetDC(hWnd);
- DPtoLP(hdc, &pt2, 1);
- pts.OffsetPoly(pts.GetPoly(ptHighlight), pt2.x - pts.GetX(ptHighlight), pt2.y - pts.GetY(ptHighlight));
- ReleaseDC(hWnd, hdc);
- InvalidateRect(hWnd, NULL, TRUE);
- }
- }
- }
- else
- {
- if (currentTool == TOOLSelect || currentTool == TOOLDelPt || currentTool == TOOLAddPt
- || currentTool == TOOLPolySelect)// || currentTool == TOOLAddPoly)
- {
- POINT pt2;
- pt2.x = pt.x, pt2.y = pt.y;
- hdc = GetDC(hWnd);
- DPtoLP(hdc, &pt2, 1);
- ReleaseDC(hWnd, hdc);
-
- double closest = 1.0e+99;
-
- for(int i=0; i<pts.GetNumPoints(); i++)
- {
- double dist = ((pt2.x - pts.GetX(i)) * (pt2.x - pts.GetX(i)))
- + ((pt2.y - pts.GetY(i)) * (pt2.y - pts.GetY(i)));
-
- if (dist < closest)
- closest = dist, ptHighlight = i;
- }
-
- if (ptHighlight != oldPtHighlight)
- {
- oldPtHighlight = ptHighlight;
- InvalidateRect(hWnd, NULL, TRUE);
- }
-
- // What follows here looks like voodoo, but is really simple. What we do is
- // check to see if the mouse point has a perpendicular intersection with any of
- // the line segments. If it does, calculate the length of the perpendicular
- // and choose the smallest length. If there is no perpendicular, then choose the
- // length of line connecting the closer of either the first endpoint or the
- // second and choose the smallest of those.
-
- // There is one bit of math that looks like voodoo to me ATM--will explain once
- // I understand it better (the calculation of the length of the perpendicular).
-
- if (pts.GetNumPoints() > 1 && currentTool == TOOLAddPt)
- {
- double smallest = 1.0e+99;
-
- for(int i=0; i<pts.GetNumPoints(); i++)
- {
- int32 p1x = pts.GetX(i), p1y = pts.GetY(i),
- p2x = pts.GetX(pts.GetNext(i)), p2y = pts.GetY(pts.GetNext(i));
-
- vector ls(p2x, p2y, 0, p1x, p1y, 0), v1(pt2.x, pt2.y, 0, p1x, p1y, 0),
- v2(pt2.x, pt2.y, 0, p2x, p2y, 0);
- double pp = ls.dot(v1) / ls.length(), dist;
-// Geometric interpretation:
-// pp is the paremeterized point on the vector ls where the perpendicular intersects ls.
-// If pp < 0, then the perpendicular lies beyond the 1st endpoint. If pp > length of ls,
-// then the perpendicular lies beyond the 2nd endpoint.
-
- if (pp < 0.0)
- dist = v1.length();
- else if (pp > ls.length())
- dist = v2.length();
- else // distance = ?Det?(ls, v1) / |ls|
- dist = abs((ls.x * v1.y - v1.x * ls.y) / ls.length());
-
-//The answer to the above looks like it might be found here:
-//
-//If the segment endpoints are s and e, and the point is p, then the test for the perpendicular
-//intercepting the segment is equivalent to insisting that the two dot products {s-e}.{s-p} and
-//{e-s}.{e-p} are both non-negative. Perpendicular distance from the point to the segment is
-//computed by first computing the area of the triangle the three points form, then dividing by the
-//length of the segment. Distances are done just by the Pythagorean theorem. Twice the area of the
-//triangle formed by three points is the determinant of the following matrix:
-//
-//sx sy 1
-//ex ey 1
-//px py 1
-//
-//(???) By translating the start point to the origin, this can be rewritten as:
-//By subtracting row 1 from all rows, you get the following:
-//
-//0 0 0
-//(ex - sx) (ey - sy) 0
-//(px - sx) (py - sy) 0
-//
-//which greatly simplifies the calculation of the determinant.
-
- if (dist < smallest)
- smallest = dist, ptNextHighlight = pts.GetNext(i), ptHighlight = i;
- }
-
- if (ptNextHighlight != oldPtNextHighlight)
- {
- oldPtNextHighlight = ptNextHighlight;
- InvalidateRect(hWnd, NULL, TRUE);
- }
- }
- }
- }
-
- ptPrevious.x = pt.x, ptPrevious.y = pt.y;
-
- break;
-
- case WM_NOTIFY:
-
- if (((NMHDR *)lParam)->code == TTN_NEEDTEXT)
- {
- LoadString(hInst, ((TOOLTIPTEXT *)lParam)->hdr.idFrom + 0x80, toolTipTxt, 16);
- ((TOOLTIPTEXT *)lParam)->lpszText = toolTipTxt;
- }
-
- break;
-
- case WM_MENUSELECT:
- {
- statusBarTxt[0] = 0; // Clear status bar text
- uint16 flags = wParam >> 16; // Extract flags
-
- if (!(flags & MFT_SEPARATOR))
- {
- uint16 id = wParam & 0xFFFF;
-
- if (flags & MF_POPUP)
- {
- if (flags & MF_SYSMENU)
- id = IDS_SYSMENU;
- else
- id = IDM_FILEMENU + wParam;
- }
-
- LoadString(hInst, id, statusBarTxt, 64);
- }
-
- SendMessage(hStatusBar, SB_SETTEXT, 0 + SBT_NOBORDERS, (LPARAM)statusBarTxt);
- break;
- }
- case WM_COMMAND:
- {
- uint16 cmd = wParam & 0xFFFF;
-
- if (cmd == IDM_NEW)
- {
-// call CmdIDM_NEW
- }
- else if (cmd == IDM_OPEN)
- {
-// call SaveChanges
-// .IF (eax)
-// movmov ofn.hwndOwner, eax, hMainWnd
-// mov ofn.Flags, OFN_PATHMUSTEXIST + OFN_FILEMUSTEXIST
-// invoke GetOpenFileName, ADDR ofn
-// .IF (eax)
-////////
-//jmp @F
-//szDMsg1a BYTE "Could not open the file (GetOpenFileName)...", 0
-//szDMsg1b BYTE "Open error!", 0
-//szDMsg1c BYTE "About to attempt to open file...", 0
-//@@:
-////invoke MessageBox, hWnd, ADDR szDMsg1a, ADDR szDMsg1b, MB_ICONERROR or MB_OK
-//invoke MessageBox, hMainWnd, ADDR szDMsg1c, ADDR szFile, MB_ICONERROR or MB_OK
-// invoke LoadTTF, ADDR szFile
-//
-//////
-// // <<< FILE OPEN CODE HERE >>>
-// or fFileStatus, NAMEDbit
-// and fFileStatus, NOT CHANGEDbit
-// call NewWindowName
-// mov eax, TRUE // return TRUE
-// jmp Return
-// //��������������������������������������
-//OpenError: invoke GetLastError
-// // <<< FILE OPEN ERROR CODE HERE >>>
-// .ENDIF
-// zero eax // return FALSE
-// .ENDIF
- }
- else if (cmd == IDM_SAVEAS)
- {
-// and fFileStatus, NOT NAMEDbit
-// call CmdIDM_SAVE
- }
- else if (cmd == IDM_SAVE)
- {
-// call CmdIDM_SAVE
- }
- else if (cmd == IDM_ABOUT)
- DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_ABOUT), hMainWnd, AboutProc, NULL);
- else if (cmd == IDM_EXIT)
- SendMessage(hWnd, WM_CLOSE, 0, 0);
- else if (cmd == ID_TBCHARWIN)
- {
- ShowWindow(hCharWnd, (IsWindowVisible(hCharWnd) ? SW_HIDE : SW_SHOWNOACTIVATE));
-
-#ifdef DEBUGFOO
-wpC.length = sizeof(WINDOWPLACEMENT);
-GetWindowPlacement(hCharWnd, &wpC);
-wsprintf(strBuf, "Char window showCmd = %08X...\n", wpC.showCmd);
-WriteLogMsg(strBuf);
-#endif
- }
- else
- return DefWindowProc(hWnd, msgID, wParam, lParam);
-
- break;
- }
- default:
- return DefWindowProc(hWnd, msgID, wParam, lParam);
- }
-
- return 0;
-}
-
-//
-// Initialize TTF data
-//
-void CreateNewDoc(void)
-{
-}
-
-//
-// Save changes to document before quitting
-//
-bool SaveChanges(void)
-{
- return true;
-}
-
-
-
-//
-// ABOUT Dialog WndProc
-//
-BOOL CALLBACK AboutProc(HWND hDlg, UINT msgID, WPARAM wParam, LPARAM lParam)
-{
- switch (msgID)
- {
- case WM_INITDIALOG:
-
- MiscCenterWnd(hDlg, hMainWnd);
- break;
-
- case WM_COMMAND:
-
- if (wParam == IDOK || wParam == IDCANCEL)
- EndDialog(hDlg, TRUE);
-
- break;
-
- default:
- return FALSE;
- }
-
- return TRUE;
-}
-
-//
-// Character Window WndProc
-//
-WndProcCW PROC STDCALL, hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
-
- mov eax, uMsg // pickup our message
- .IF (eax == WM_PAINT)
- mov eax, 0 // Non-sense... (placeholder!)
-// Scan conversion etc. goes here...
- .ELSEIF (eax == WM_LBUTTONDOWN)
- invoke SetCapture, hCharWnd
- .ELSEIF (eax == WM_LBUTTONUP)
- invoke ReleaseCapture
- invoke SetFocus, hMainWnd // Make sure the main wnd keeps focus!
- .ELSEIF (eax == WM_NCLBUTTONDOWN)
- invoke DefWindowProc, hWnd, uMsg, wParam, lParam // Let it do its thing
- invoke SetFocus, hMainWnd // Make sure the main wnd keeps focus!
- .ELSE
-DefProc: invoke DefWindowProc, hWnd, uMsg, wParam, lParam
- .ENDIF
- ret
-
-WndProcCW ENDP