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

Author Topic: Can't figure out how to enable click and move  (Read 2803 times)

0 Members and 1 Guest are viewing this topic.

The Illusionist Mirage

  • Full Member
  • ***
  • Posts: 115
  • My Life = Linux, C++ & Computers
    • View Profile
    • fleptic
    • Email
Can't figure out how to enable click and move
« on: September 18, 2013, 12:05:47 pm »
Hello

I am trying to move a sf::RectangleShape only when the user clicks the mouse over it and then moves it until the mouse is pressed. I am having some problem with this.

Here's my code
#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
    sf::RenderWindow window(sf::VideoMode(640, 480, 32), "TEST");

    sf::RectangleShape shape(sf::Vector2f(30.0f, 30.0f));
    shape.setFillColor(sf::Color::Red);
    shape.setPosition(sf::Vector2f(100.0f, 200.0f));

    while(window.isOpen())
    {
        sf::Event event;
        while(window.pollEvent(event))
        {
            switch(event.type)
            {
                case sf::Event::Closed:
                    window.close();
                    break;

                case sf::Event::KeyPressed:
                    if(event.key.code == sf::Keyboard::Escape)
                        window.close();
                    break;
            }
        }

        sf::Vector2f mousePos = (sf::Vector2f)sf::Mouse::getPosition(window);
        sf::Vector2f shapePos = shape.getPosition();

        std::cout << mousePos.x << ", "<< mousePos.y << std::endl;

        if(mousePos.x >= shapePos.x && mousePos.x <= shapePos.x + shape.getGlobalBounds().width
           && mousePos.y >= shapePos.y && mousePos.y <= shapePos.y + shape.getGlobalBounds().height)
            if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
                shape.setPosition(mousePos.x, mousePos.y);

        window.clear();

        window.draw(shape);

        window.display();
    }

    return 0;
}
 

How do I rectify my code to make it work correctly?

Thanks

DarkYoung

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Can't figure out how to enable click and move
« Reply #1 on: September 18, 2013, 12:34:56 pm »
Something like that?


#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
    sf::RenderWindow window(sf::VideoMode(640, 480, 32), "TEST");

    sf::RectangleShape shape(sf::Vector2f(30.0f, 30.0f));
    shape.setFillColor(sf::Color::Red);
    shape.setPosition(sf::Vector2f(100.0f, 200.0f));
    shape.setOrigin(15, 15);

    while(window.isOpen())
    {
        sf::Event event;
        while(window.pollEvent(event))
        {
            switch(event.type)
            {
                case sf::Event::Closed:
                    window.close();
                    break;

                case sf::Event::KeyPressed:
                    if(event.key.code == sf::Keyboard::Escape)
                        window.close();
                    break;

                default:
                    break;

            }
        }

        if(sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
            sf::Vector2f pos = (sf::Vector2f)sf::Mouse::getPosition(window);
            if(shape.getGlobalBounds().contains(pos)) {
                shape.setPosition(pos);
            }
        }

        window.clear();

        window.draw(shape);

        window.display();
    }

    return 0;
}
 

The Illusionist Mirage

  • Full Member
  • ***
  • Posts: 115
  • My Life = Linux, C++ & Computers
    • View Profile
    • fleptic
    • Email
Re: Can't figure out how to enable click and move
« Reply #2 on: September 18, 2013, 12:52:06 pm »
Something like that?

Thanks but if I move my rectangle fast by holding it, then the desired effect is not produced.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Can't figure out how to enable click and move
« Reply #3 on: September 18, 2013, 12:56:27 pm »
The correct algorithm would be:

event loop
{
    on mouse press event:
        if mouse cursor in entity
            dragging = true
            offset = cursor - entity.getPosition()

    on mouse released event:
        dragging = false;
}

if (dragging)
    entity.setPosition(cursor - offset);
Laurent Gomila - SFML developer

The Illusionist Mirage

  • Full Member
  • ***
  • Posts: 115
  • My Life = Linux, C++ & Computers
    • View Profile
    • fleptic
    • Email
Re: Can't figure out how to enable click and move
« Reply #4 on: September 18, 2013, 01:11:15 pm »
The correct algorithm would be....

Thanks sir, it worked perfectly :D.

 

anything