1
General / 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.
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;
}
#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;
}