I'm trying to write an undo feature for a drawing program. I have an array of CPoint's called ShapePts[100]. I made another array of CPoint's Undo[100]. When the program deletes a point it finds the closest point to the mouse. So, I was thinking just store the point in another array before it gets deleted. /* This is the MainFrame.h declaration */ CPoint Undo[100]; int nUndo;
void CMainFrame::OnLButtonDown (UINT nFlags, CPoint MousePt) { int ShortDist; if (nFlags & MK_SHIFT) { // Shift key down. So delete closest point. if (nPts > 0) // - only if have one { // Step: Find point closest to MousePt Pos = ClosestPoint (MousePt,ShapePts,nPts, ShortDist); <-- returns int Undo[nUndo] = ShapePts[nPts]; // nPts and nUndo were initialized to // zero in MainFrame contructor // Step: Shift points down one array component index for (int i = Pos + 1; i < nPts; i++) { ShapePts[i-1] = ShapePts[i]; } nPts--; // adjust count } // end if } else { if (nPts >= 99) // array full return; // do nothing else { ShapePts[nPts] = MousePt; nPts++; CString s; s.Format("Last pt:(%d, %d)",MousePt.x, MousePt.y); myStatusBar.SetText (s,1,SBT_NOBORDERS); } } Invalidate(); }
void CMainFrame::OnRButtonDown (UINT nFlags, CPoint MousePt) { CPoint ScreenPt; int ShortestDist, i; // Step 1: Find position of closest pt in array Pos = ClosestPoint (MousePt, ShapePts, nPts, ShortestDist); if(nFlags & MK_SHIFT) { ShapePts[nPts] = Undo[nUndo]; nPts++; for(i = 0; i < nUndo; i++) { Undo[i-1] = Undo[i]; } nUndo--; } else { SetCapture(); // Step 2: Move the cursor to that position ScreenPt = ShapePts[Pos]; ClientToScreen (&ScreenPt); SetCursorPos (ScreenPt.x, ScreenPt.y); } }
Where am I going wrong. If you need more info please let me know. -CDudd