Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to move a rectangle with two clicks in SFML?  (Read 10233 times)

0 Members and 2 Guests are viewing this topic.

zed118

  • Newbie
  • *
  • Posts: 2
    • View Profile
How to move a rectangle with two clicks in SFML?
« on: August 30, 2021, 04:52:15 pm »
I want to move a rectangle in such a way that the first click selects it and the second click will change its position to wherever the current mouse position is. I've written the code for actually changing the position of the rectangle. The only problem is actually triggering these events because when I try to, it detects more than one click. What would be your approach?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to move a rectangle with two clicks in SFML?
« Reply #1 on: August 30, 2021, 05:19:05 pm »
Flags.

Use a flag (boolean value) to represent when something has been selected.

When you click:
if the isSelected boolean is not set, set it, if it's on the shape.
if the isSelected boolean is already set, unset it and move the shape.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

zed118

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: How to move a rectangle with two clicks in SFML?
« Reply #2 on: August 30, 2021, 05:34:55 pm »
Flags.

Use a flag (boolean value) to represent when something has been selected.

When you click:
if the isSelected boolean is not set, set it, if it's on the shape.
if the isSelected boolean is already set, unset it and move the shape.

This is exactly what I've done, but it still does not work:

void Board::get_click(sf::RenderWindow &window, sf::Event &event)
{
        // get first click
        if (event.type == sf::Event::MouseButtonPressed && mouse_pressed == false)
        {
                if (event.mouseButton.button == sf::Mouse::Left)
                {
                        sf::Vector2i mouse_pos = sf::Mouse::getPosition(window);
                        mouse_pressed = true;

                        for (int i = 0; i < board.size(); i++)
                        {
                                // get position bounds of tile
                                Tile tile = board[i];
                                // check if mouse is within bounds
                                if (mouse_pos.x > tile.top_left().x && mouse_pos.x < tile.top_right().x) {
                                        if (mouse_pos.y > tile.top_left().y && mouse_pos.y < tile.down_left().y) {
                                                // get piece at tile
                                                if (tile.getOccupied() == true)
                                                {
                                                        std::cout << "Valid click!\n";
                                                        piece_index = i;
                                                        break;
                                                }
                                                else
                                                {
                                                        mouse_pressed = false;
                                                        break;
                                                }
                                        }
                                }
                        }
                }
        }

        if (sf::Mouse::isButtonPressed(sf::Mouse::Left) && mouse_pressed == true)
        {
                // move piece
                sf::Vector2i mouse_pos = sf::Mouse::getPosition(window);

                // create new piece with mouse_pos
                Piece moved_piece = board[piece_index].getTilePiece();
                moved_piece.set_position(mouse_pos.x, mouse_pos.y);

                mouse_pressed = false;

                std::cout << "Piece moved!\n";
        }
}

This is the output after 1 CLICK:
Valid click!
Piece moved!

It should be triggering only the first sentence, and the last sentence should be triggered once I make a second click.
« Last Edit: August 31, 2021, 07:48:55 am by eXpl0it3r »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: How to move a rectangle with two clicks in SFML?
« Reply #3 on: August 30, 2021, 05:45:03 pm »
If you set mouse_pressed in your first if, and the condition of your second if is "mouse_pressed == true" then of course the condition will be verified immediately, use an "else if" ;)

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to move a rectangle with two clicks in SFML?
« Reply #4 on: August 30, 2021, 05:45:56 pm »
EDIT: what G. said.  ;D
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*