So I am trying to implement a mouse drag box where as long as the mouse left-click is held, a box is drawn positioned at the point of the original click and drawn out from that original click to the mouse cursor's current position, like you would use in an operating system's file explorer to select multiple files or in a RTS game to select multiple units.
My implementation below somewhat works with this problem: it draws a rectangle positioned at (0,0), not the starting click position, and draws out to where the mouse is currently, however, it doesn't fully draw it, it just flashes briefly (and doesn't even fully draw, it draws like a very random portion of it) and disappears when I'm no longer dragging the mouse.
The latter I'm guessing is because I'm using 'sf::Event::MouseMoved', but any other implementations I tried involving 'sf::Event::MouseButtonPressed' or 'sf::Event::MouseButtonReleased' using if, for, or while loops didn't even draw the rectangle at all, so I am at a loss. There are a few discussions I've seen in the forum, and I've tried implementing similarly or even exactly, and it doesn't produce any better of a result, if anything at all.
class MouseEvents
{
//will probably make all of these variables and methods static later
private:
sf::Vector2i mousePosOnClick;
sf::Vector2i mousePosOnHold;
public:
sf::RectangleShape mouseDrawnBox;
void mouseDrawBox(sf::Event &event)
{
if( (event.type == sf::Event::MouseButtonPressed) && (event.mouseButton.button == sf::Mouse::Left) )
{
mousePosOnClick.x = event.mouseButton.x;
mousePosOnClick.y = event.mouseButton.y;
}
if( (event.type == sf::Event::MouseMoved) && (sf::Mouse::isButtonPressed(sf::Mouse::Left)) )
{
mousePosOnHold.x = event.mouseMove.x;
mousePosOnHold.y = event.mouseMove.y;
//I will add logic for negative values in 'size' later, just want something to work at all first
sf::Vector2f size( (mousePosOnHold.x - mousePosOnClick.x), (mousePosOnHold.y - mousePosOnClick.y) );
mouseDrawnBox.setPosition(mousePosOnClick.x, mousePosOnClick.y);
mouseDrawnBox.setSize(size);
mouseDrawnBox.setFillColor( sf::Color(0,0,255) );
}
}
};
int main()
{
while (window.isOpen())
{
sf::Event event;
MouseEvents mouseEvent;
while (window.pollEvent(event))
{
// Close window : exit
if (event.type == sf::Event::Closed) {
window.close();
}
// Espace pressed : exit
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
window.close();
}
mouseEvent.mouseDrawBox(event);
}
window.clear();
window.draw(mouseEvent.mouseDrawnBox);
window.display();
}
return EXIT_SUCCESS;
}