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

Author Topic: Detect mouse button hold?  (Read 13739 times)

0 Members and 1 Guest are viewing this topic.

magneonx

  • Full Member
  • ***
  • Posts: 141
    • MSN Messenger - magnumneon04@hotmail.com
    • View Profile
Detect mouse button hold?
« on: October 29, 2013, 04:18:53 pm »
How can I detect mouse button hold without doing a time interval thing??? Is that even possible? I am trying to distinguish between a single click against single click and hold. How can I do this in SFML?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Detect mouse button hold?
« Reply #1 on: October 29, 2013, 04:33:58 pm »
You can simply track the KeyPressed / KeyReleased event (see the tutorial for events), but since "hold" is really not an actual input and everyone can have a different opinion on what point a "click" turns into a hold, you'll have to use some time interval. If the key is pressed for longer than x milliseconds you can count it as being hold or similar.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Detect mouse button hold?
« Reply #2 on: October 29, 2013, 04:37:58 pm »
Because it is kind of ambiguous what a "click" is or what the difference between a "click" and "press and hold for a short time" is, it really is up to you to program in this functionality yourself. I know some people who are really slow with the mouse and take more than a second to perform a "click". Does this constitute a click? Or is it already holding? You can program in a way of differentiating between them using a timer that the user will set according to their own preferences. Don't assume the user will use the program in the same way as you do.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

magneonx

  • Full Member
  • ***
  • Posts: 141
    • MSN Messenger - magnumneon04@hotmail.com
    • View Profile
Re: Detect mouse button hold?
« Reply #3 on: October 29, 2013, 04:47:03 pm »
I was thinking the same thing... but oh well... I think I have to stick to my guns and redesigning my code once again. I am actually designing a multiple selection scheme for an RTS game wherein you can select multiple units all at once. I need a left-click hold then mouse move to perform this. Currently I am thinking of just making multiple selection on top of everything and just differentiate whether or not I am just selecting a single unit.

magneonx

  • Full Member
  • ***
  • Posts: 141
    • MSN Messenger - magnumneon04@hotmail.com
    • View Profile
Re: Detect mouse button hold?
« Reply #4 on: October 29, 2013, 05:04:49 pm »
Alright, I've done it! as I have said I put multiple selection on top of every selection in the game. So there is no longer need for a single click. Every time I press click, I draw a very small box on size ( 1, 1) and never renders it if bounding box is on this size. But each entity detects collision on this very small box. At the same time if I move the mouse while holding the left click I am able to draw the bounding box and select multiple unit at once.

This actually works and of course it is not the best solution but it gets the job done. Thank you guys for giving me tips. Actually, as I have posted it, I am asking about doing this not using interval time. But thanks for verifying my first thought it seems to be very legit solution.

Here is the snippet on how I have done it:

My SelectionHighlighter.cpp
    void SelectionHighligher::calculateHighlight( sf::RenderWindow * window )
    {
        mousePosition = sf::Mouse::getPosition( *window );

        float width  = window->mapPixelToCoords( mousePosition ).x;
        float height = window->mapPixelToCoords( mousePosition ).y;

        width  = width  - mousePositionOnDrag.x + 1.0f;
        height = height - mousePositionOnDrag.y + 1.0f;

        highlight.setPosition( sf::Vector2f( mousePositionOnDrag ) );
        highlight.setSize( sf::Vector2f( width , height ) );
    }

    void SelectionHighligher::drawHighlight( sf::RenderWindow * window )
    {
        if( highlight.getSize().x != 1.0f && highlight.getSize().y != 1.0f )
            window->draw( highlight );
    }
 

From the main application class

if( sf::Mouse::isButtonPressed( sf::Mouse::Button::Left ) )
{
      highlight.calculateHighlight( &window );
      selection.detectMultipleObjectSelection( highlight.getHighlightBounds() , &window );
      selection.selectMultiple();
}
 

I just have to refactor this. Thank you very much!

~Cheers!
Neon Warge

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Detect mouse button hold?
« Reply #5 on: October 29, 2013, 06:13:48 pm »
I think it's quite common to do it that way. You could also just check whether the move moved WHILE you were holding the button and only then draw a rectangle.
You could then even add a minimum move distance, so if the mouse moves 3px it doesn't matter, but everything above gets counted as "selection".
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Detect mouse button hold?
« Reply #6 on: October 29, 2013, 07:03:12 pm »
If you wanted to measure how long someone had the mouse button pressed surely you could do something like use sf::time right? I've been interested in this sort of thing myself because it makes games a bit more interactive.

I've seen fable 3 infamously manage to force you to hold down a button every time you want to make a decision and that even does a little circular progress bar to show how long it's being held down for so it must be possible.
« Last Edit: October 29, 2013, 07:06:42 pm by Lethn »

 

anything