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

Author Topic: How can I create a drag and drop function?  (Read 4854 times)

0 Members and 1 Guest are viewing this topic.

GNRlova23

  • Newbie
  • *
  • Posts: 3
    • View Profile
How can I create a drag and drop function?
« on: March 09, 2013, 11:02:47 am »
So yeah, what I want is to draw a rectangle, or any shape for that matter, and then have the user be able to drag that object and drop it off wherever he feels like. So far the only thing I've really managed to handle is setting the position of the shape whenever the user presses the left mouse button, the mouse doesn't even have to be over the rectangle, and even if it is, the rectangle will automatically adjust so that the mouse is holding it by the middle.

Now I understand why these problems exist, I just need to know how to have the program detect when the mouse is hovering over the rectangle and to have the origin of the rectangle set to where ever the mouse is (when the mouse is hovering over the rectangle). At least, that's what I think needs to be done. I'm gonna leave the code of the entire program here, if anybody would be so kind as to help me out, I'd very greatly appreciate it. :)

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

//Glboal variables, functions, classes

//C++ program entry point
int main()
{
        //Creating the window
        sf::RenderWindow window(sf::VideoMode(1280, 800), "SFML Game");

        //Settign the framerate limit to 60 FPS
        window.setFramerateLimit(60);

        window.setKeyRepeatEnabled(false);

        //Variable that keeps the game loop running
        bool play = true;

        //Event object holding all events
        sf::Event event;

        //States for button/events
        bool mouseClicked = false;
        bool mouseInsideRect = false;

        //Variables
        int mouseX = 0;
        int mouseY = 0;

        //Images

        //Render shapes
        //Rectangle Shape
        sf::RectangleShape rect;
        rect.setSize(sf::Vector2f(200, 200));
        rect.setPosition(640, 400);
        rect.setFillColor(sf::Color::White);
        rect.setOrigin(100, 100);

        //Font

        //Text


        //Game loop
        while (play == true)
        {
                //EVENTS
                while (window.pollEvent(event))
                {
                        //LMB Pressed
                        if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left)
                        {
                                mouseClicked = true;
                        }

                        //LMB released
                        if (event.type == sf::Event::MouseButtonReleased && event.mouseButton.button == sf::Mouse::Left)
                        {
                                mouseClicked = false;
                        }

                        //Mouse Moved in window
                        if (event.type == sf::Event::MouseMoved)
                        {
                                mouseX = event.mouseMove.x;
                                mouseY = event.mouseMove.y;
                        }

                        //Event type is window closed
                        if (event.type == sf::Event::Closed)
                        {
                                //Set play to false in order to stop the game loop
                                play = false;
                        }
                }

                //LOGIC

                if (mouseClicked == true)
                {
                        rect.setPosition(mouseX, mouseY);
                }

                //RENDERING
                window.clear();

                window.draw(rect);

                window.display();
        }
        ///////////

        //Clean up and close the window
        window.close();

        //Close the program
        return 0;
}

cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: How can I create a drag and drop function?
« Reply #1 on: March 09, 2013, 11:33:10 am »
#include <SFML/Graphics.hpp>
#include <iostream>
#include <SFML/Audio.hpp>

//Glboal variables, functions, classes

//C++ program entry point
int main()
{
    //Creating the window
    sf::RenderWindow window(sf::VideoMode(1280, 800), "SFML Game");

    //Settign the framerate limit to 60 FPS
    window.setFramerateLimit(60);

    window.setKeyRepeatEnabled(false);

    //Variable that keeps the game loop running
    bool play = true;

    //Event object holding all events
    sf::Event event;

    //States for button/events
    bool mouseClicked = false;
    bool mouseInsideRect = false;
    bool dragging = false ;

    sf::Vector2f mouseRectOffset ;

    //Variables
    int mouseX = 0;
    int mouseY = 0;

    //Images

    //Render shapes
    //Rectangle Shape
    sf::RectangleShape rect;
    rect.setSize(sf::Vector2f(200, 200));
    rect.setPosition(640, 400);
    rect.setFillColor(sf::Color::White);
    rect.setOrigin(100, 100);

    //Font

    //Text


    //Game loop
    while (play == true)
    {
        //EVENTS
        while (window.pollEvent(event))
        {
            //LMB Pressed
            if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left)
            {
                mouseClicked = true;

                if ( rect.getGlobalBounds().contains(event.mouseButton.x, event.mouseButton.y) )
                {
                    dragging = true ;
                    mouseRectOffset.x = event.mouseButton.x - rect.getGlobalBounds().left - rect.getOrigin().x ;
                    mouseRectOffset.y = event.mouseButton.y - rect.getGlobalBounds().top - rect.getOrigin().y ;
                }
            }

            //LMB released
            if (event.type == sf::Event::MouseButtonReleased && event.mouseButton.button == sf::Mouse::Left)
            {
                mouseClicked = false;
                dragging = false ;
            }

            //Mouse Moved in window
            if (event.type == sf::Event::MouseMoved)
            {
                mouseX = event.mouseMove.x;
                mouseY = event.mouseMove.y;
            }

            //Event type is window closed
            if (event.type == sf::Event::Closed)
            {
                //Set play to false in order to stop the game loop
                play = false;
            }
        }

        //LOGIC

        if (dragging == true)
        {
            rect.setPosition(mouseX - mouseRectOffset.x, mouseY - mouseRectOffset.y);
        }

        //RENDERING
        window.clear();

        window.draw(rect);

        window.display();
    }
    ///////////

    //Clean up and close the window
    window.close();

    //Close the program
    return 0;
}

Bryston

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: How can I create a drag and drop function?
« Reply #2 on: March 15, 2013, 04:49:03 pm »
I was just trying something similar but with hover/moving effects.

I did it with 3 images, normal, hover_effect, moving effect. The effects were basically the normal image with green or red glows in the background.

I had to put some logic in my rendering section though to display the proper image based on a the Bools for hover, moving and buttonDown.

I'm sure there is a more elgeant way to do this inside the SFML library, any suggestions?

// Rendering
                gamewindow.clear();
                gamewindow.draw(background);
               
                if(hover == true && buttonDown == false)
                {
                        gamewindow.draw(highlightChar);
                        hover = false;
                }
                else if(moving == true && buttonDown == true)
                {
                        gamewindow.draw(movingChar);
                        hover = false;
                }
                else
                {
                        gamewindow.draw(normalChar);
                }
                gamewindow.draw(cursor);
                gamewindow.display();