Is there some problem with undoActions that prevents my two functions( redo and undo from working properly)

70 Views Asked by At

So I've been trying to add a back and forward functions to my paint by numbers project, so that if the user accidently paints the wrong square the function back redraws the window to the state it was before the paint. And this function works fine, but my other function that does the opposite returns only the last undone square even if there were multiple undone squares in a row. here are theese functions:

void UndoLastPaint(HWND hWnd)
{
    if (!paintedCoordinates.empty())
    {
        // Retrieve the coordinates of the last painted square
        int x = paintedCoordinates.back().first;
        int y = paintedCoordinates.back().second;

        // Check if the square was previously painted
        bool wasPainted = false;
        COLORREF color = RGB(255, 255, 255); // Default color if the square was not painted
        for (int i = paintedCoordinates.size() - 2; i >= 0; --i) {
            if (paintedCoordinates[i].first == x && paintedCoordinates[i].second == y) {
                wasPainted = true;
                color = paintedColors[i]; // Store color from the loop
                // Store the undone action
                PaintAction undoneAction;
                undoneAction.x = x;
                undoneAction.y = y;
                undoneAction.color = color;
                undoneActions.push(undoneAction);
                break;
            }
        }

        if (wasPainted) {
            // Restore the previous color of the square
            RECT squareRect = { x * squareSize, y * squareSize, (x + 1) * squareSize, (y + 1) * squareSize };
            HDC hdc = GetDC(hWnd);
            HBRUSH brush = CreateSolidBrush(color);
            FillRect(hdc, &squareRect, brush);
            ReleaseDC(hWnd, hdc);
        }
        else {
            // Calculate the rectangle occupied by the last painted square
            RECT squareRect = { x * squareSize, y * squareSize, (x + 1) * squareSize, (y + 1) * squareSize };

            // Remove the last painted square from the screen
            InvalidateRect(hWnd, &squareRect, TRUE);
            UpdateWindow(hWnd);
        }

        // Remove the last painted square from the data collection
        paintedCoordinates.pop_back();
        paintedColors.pop_back();
    }
}


void MoveForward(HWND hWnd)
{
    if (!undoneActions.empty())
    {
        // Retrieve the coordinates and color of the last undone action
        int x = undoneActions.top().x;
        int y = undoneActions.top().y;
        COLORREF color = undoneActions.top().color;

        // Remove the next undone action from the stack
        undoneActions.pop();

        // Paint the square at the next undone action
        PaintSquare(hWnd, x, y);

        // Store the repainted square in the data collection
        paintedCoordinates.push_back({ x, y });
        paintedColors.push_back(color);
    }
}

that's why I think that stack undoneActions isn't functioning properly, but i can't figure out what is the exact problem

0

There are 0 best solutions below