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

Author Topic: Double click event  (Read 9030 times)

0 Members and 1 Guest are viewing this topic.

darekg11

  • Full Member
  • ***
  • Posts: 127
    • View Profile
Double click event
« on: July 04, 2011, 11:38:08 am »
Okay, it's killing me. I know that there is no built in double click event so I wanted to implement it by my own with usage of clock. Default Windows value is 500ms so I tried to implement it by measuring time between each click but I can't get it to work. Can anyone post code to it or some advice?x_x

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Double click event
« Reply #1 on: July 04, 2011, 12:03:35 pm »
Catch sf::Event::MouseButtonPressed events and reset the clock at each click, measuring elapsed time since the last click. Note that sf::Clock works now with milliseconds.

Otherwise, show your current code...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

darekg11

  • Full Member
  • ***
  • Posts: 127
    • View Profile
Double click event
« Reply #2 on: July 04, 2011, 01:10:46 pm »
Code: [Select]

int main()
{
sfClock* clock =  sfClock_Create();
sfWindowSettings Settings = {32, 8, 2};
     sfVideoMode Mode = {800, 600, 32};
     sfRenderWindow* App;
     sfEvent Event;

     /* Create the main window */
     App = sfRenderWindow_Create(Mode, "CSFML", sfClose, Settings);
     if (!App)
         return EXIT_FAILURE;
     
     /* Start the game loop */
     while (sfRenderWindow_IsOpened(App))
     {
         /* Process events */
         while (sfRenderWindow_GetEvent(App, &Event))
         {
             /* Close window : exit */
             if (Event.Type == sfEvtClosed)
                 sfRenderWindow_Close(App);
if (Event.MouseButton.Button == sfButtonLeft && Event.Type == sfEvtMouseButtonPressed)
{
if (sfClock_GetTime(clock) <= 0.5f)
std::cout << "DOUBLE\n";
else
std::cout << "SINGLE\n";
sfClock_Reset(zegar);

}
         }
         /* Clear the screen */
         sfRenderWindow_Clear(App, sfBlack);
 
         /* Update the window */
         sfRenderWindow_Display(App);
     }
 
     /* Cleanup resources */
sfClock_Destroy(clock);
clock = NULL;
     sfRenderWindow_Destroy(App);
 
     return EXIT_SUCCESS;
 }


Not working quite as I would like it to work.

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Double click event
« Reply #3 on: July 04, 2011, 10:29:32 pm »
Quote from: "darekg11"
Not working quite as I would like it to work.
Because the Clock gives time in milliseconds, not seconds.
I use the latest build of SFML2

darekg11

  • Full Member
  • ***
  • Posts: 127
    • View Profile
Double click event
« Reply #4 on: July 04, 2011, 10:43:33 pm »
No, it's CSFML 1.6

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Double click event
« Reply #5 on: July 04, 2011, 10:50:54 pm »
Code: [Select]
if (Event.MouseButton.Button == sfButtonLeft && Event.Type == sfEvtMouseButtonPressed) Swap the two conditions. You may not access Event.MouseButton.Button if the event is not of type MouseButtonPressed. So you can make use of &&'s shortcut semantics.

Quote from: "darekg11"
Not working quite as I would like it to work.
Can you be more exact? Have you already tried to watch the variable values with a debugger or additional output statements?

By the way, why do you use CSFML if you obviously work with C++?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

darekg11

  • Full Member
  • ***
  • Posts: 127
    • View Profile
Double click event
« Reply #6 on: July 04, 2011, 11:10:42 pm »
I work with C, it's just c++ compiler. (Yes I know, be next blaming me)

The thing is that when I'm doing double click I get SINGLE first so it looks like(I'm not sure if it's wrong or not"):

SINGLE
DOUBLE
SINGLE
DOUBLE

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Double click event
« Reply #7 on: July 04, 2011, 11:19:36 pm »
Yes, that is expected... Since the first click of a double click is a single click :D
Just try to reconstruct the program path when it comes to a mouse button event. There is an if and an else branch, on of them must be executed.

You use std::cout, so you can also use C++ SFML... But I don't want to hinder you from choosing the complicated way :P
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

darekg11

  • Full Member
  • ***
  • Posts: 127
    • View Profile
Double click event
« Reply #8 on: July 05, 2011, 10:10:11 am »
Okay, thanks. I guess I must also check if second click happend in the same place as first one right?

Haikarainen

  • Guest
Double click event
« Reply #9 on: July 05, 2011, 12:45:20 pm »
Quote from: "darekg11"
Okay, thanks. I guess I must also check if second click happend in the same place as first one right?


Probably a good idea, but leave a margin of like 2-4 pixels, sometimes you dont have the mousepointer on the exact same position both times you click. Either do this or code your other code with respect of this issue.

 

anything