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

Author Topic: Mouse Pressed and Released Method for Single Mouse Click?  (Read 21639 times)

0 Members and 1 Guest are viewing this topic.

BlazeCrate

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Mouse Pressed and Released Method for Single Mouse Click?
« on: May 18, 2014, 11:34:27 pm »
Hello,

I have been using SFML for a little while now and I am wondering if there is a way to register a single mouse click and not just call it when the mouse is down.

Something like this:
if (sf::Mouse::isButtonPressed(sf::Mouse::Button))
{

}
 

I know that this exists but it should be if the mouse is down and, pressed should be if the mouse is pressed and released.

I think that I might just need to write my own input class to handle all the input but if anyone knows another way of doing things that would be great.

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Mouse Pressed and Released Method for Single Mouse Click?
« Reply #1 on: May 18, 2014, 11:43:06 pm »
What's wrong with MouseButtonPressed and MouseButtonReleased events?

BlazeCrate

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Mouse Pressed and Released Method for Single Mouse Click?
« Reply #2 on: May 18, 2014, 11:52:44 pm »
      while (window.pollEvent(event))
      {
         if (currentGameState == OptionsScreen)
         {
            sf::IntRect backButtonRect(151, 700, 299, 79);

            if (mouseRect.intersects(backButtonRect) && event.type == sf::Event::MouseButtonPressed)
            {
                currentGameState = MainMenu;
            }
        }
      }
 

This is what I tryed with the events but it doesn't work most of the times that I press on the button. It isn't consistent at all. I don't know if I am doing it wrong or what?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Mouse Pressed and Released Method for Single Mouse Click?
« Reply #3 on: May 18, 2014, 11:55:29 pm »
Typically, you check for the event type before reacting to it, i.e.
while (window.pollEvent(event))
{
    if (event.type == sf::Event::MouseButtonPressed)
    {
        ...
    }
}

Furthermore, you obviously have to access the event's members to get more information about the current mouse position. Please read the tutorial and documentation carefully, there are detailed descriptions and examples.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Mouse Pressed and Released Method for Single Mouse Click?
« Reply #4 on: May 18, 2014, 11:58:58 pm »
Don't know what mouseRect is, but it's probably wrong.
Moreover as you can see in the events tutorial, the MouseButtonPressed event also contains the position on the mouse so you could check if the event type is MouseButtonPressed and if the mouse position is inside the backButtonRect (backButtonRect.contains(...))

BlazeCrate

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Mouse Pressed and Released Method for Single Mouse Click?
« Reply #5 on: May 19, 2014, 05:46:35 am »
I have modified the code a little bit; playing around with the advice you guys gave.

            if (event.type == sf::Event::MouseButtonPressed)
            {
                if (event.mouseButton.button == sf::Mouse::Left)
                {
                    if (quitButtonRect.contains(event.mouseButton.x, event.mouseButton.y))
                    {
                        currentGameState = Quit;
                    }
                }
            }

I came up with this and it doesn't work the way that I want to. I have it in a update method and then is inside a while(window.pollEvent(event)) loop.

The problem is that it works on very little occasions and it is very inconsistent.

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Mouse Pressed and Released Method for Single Mouse Click?
« Reply #6 on: May 19, 2014, 11:14:14 am »
Of course this code has to be inside the pollEvent loop.
You only have one event loop, right?

If your world coordinates (the button) and your window relative coordinates are different, you should use mapPixelToCoords.

Obviously you're doing something wrong. :p
#include <iostream>
#include <SFML/Graphics.hpp>


int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "");

    sf::RectangleShape button;
    button.setSize(sf::Vector2f(200.0f, 80.0f));
    button.setPosition(300.0f, 260.0f);

    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                window.close();
            }
            if (event.type == sf::Event::MouseButtonPressed) {
                if (event.mouseButton.button == sf::Mouse::Left) {
                    if (button.getGlobalBounds().contains(window.mapPixelToCoords(sf::Vector2i(event.mouseButton.x, event.mouseButton.y)))) {
                        std::cout << "Button pressed" << std::endl;
                    }
                }
            }
        }

        window.clear(sf::Color::Black);
        window.draw(button);
        window.display();
    }

    return EXIT_SUCCESS;
}
« Last Edit: May 19, 2014, 12:30:43 pm by G. »

BlazeCrate

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Mouse Pressed and Released Method for Single Mouse Click?
« Reply #7 on: May 19, 2014, 05:25:45 pm »
You can view all of my BEGINNER, learning code here on github: https://github.com/Ancell/Tractus

P.s: This is in no way the best way of doing things I just wanted to try to make a game with what I know.

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Mouse Pressed and Released Method for Single Mouse Click?
« Reply #8 on: May 19, 2014, 06:36:20 pm »
Events HAVE to be used inside the pollEvent loop or the values they contain won't be correctly defined.
And that's not what you're doing in your code.