TGC Codebase Backup



Mouse states by Fluffy Paul

5th Feb 2010 16:52
Summary

A short subroutine which will let you detect separate mousedown, mousepress and mouse up events, as well as double clicks.



Description

This code features a subroutine which detects changes in the mouseclick() value, buffers them and sets up the following variables:

MOUSE_mousedown
MOUSE_mousepress
MOUSE_mouseup

These variables will contain the number of the mouse button which triggered them.

Anatomy of a mouseclick:

1. mouseup, mousepress and mousedown are zero.
2. User clicks button 1
3. mousedown and mousepress equal 1 but mouseup is still zero.
4. In the next cycle mousedown now equals zero, mousepress equals 1 and mouseup is also zero.
5. User releases mouse.
6. mousedown and mousepress are both zero. Mouseup is equal to 1.
7. In the next cycle, assuming no other mouse buttons are being pressed, all three variables are zero again.

Note that if mouse button two was pressed then all the 1's in the above example would actually be 2's.

This routine also detects double clicks. It records the time the user triggers a mouseup and compares it to the previous time the user moused up. If the same button moused up within the time specified by MOUSE_doubleClickSpeed then a double click is triggered.

A double click is represented by a negative mouseup value which is zero minus the number of the mouse button which double clicked.
e.g. User double clicks button 2. MOUSE_mouseup equals -2 for one cycle.

You may wish to alter the code so that double clicks are triggered by mousedowns or mousepresses.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    Rem Mouse States Component

Rem Variable to store the max time between clicks.
Rem If you click the same button twice within this time, you'll trigger a double click.
MOUSE_doubleClickSpeed = 750

Rem Call this sub every cycle
_get_mouse_state:
   rem differentiate mouse down and mouse up
   rem firstly, is the mouse being clicked?
   if mouseclick() 
      if MOUSE_mousepress=mouseclick()
         MOUSE_mousedown=0
      else
         MOUSE_mousedown = mouseclick()
      endif
      
      MOUSE_mousepress=mouseclick()
   else
      rem catch mouseup
      rem also resets mouseup to zero if there's no mouse action for a frame.
      MOUSE_mouseup=MOUSE_mousepress

      rem save the last mouseup to check for double click
      rem N.B. you could change this to use mousedown or mouse press instead, if it doesn't feel right to you
      if MOUSE_mouseup>0

         rem store the time this mouseup took place
         MOUSE_lastClickTime=MOUSE_thisClickTime
         MOUSE_thisClickTime=timer()
         MOUSE_clickSpeed = MOUSE_thisClickTime - MOUSE_lastClickTime

         rem check to see if there was another one recently
         rem check interval between clicks and that they were the same button
         rem double clicking is signified by the button number being negative
         rem e.g. mouseup=1 means button 1 was clicked. mouseup=-1 means button 1 was double clicked.
         if MOUSE_clickSpeed<=MOUSE_doubleClickSpeed and MOUSE_lastMouseUp=MOUSE_mouseup
            MOUSE_mouseup=0-MOUSE_mouseup
         endif
         MOUSE_lastMouseUp=MOUSE_mouseup
      endif
      MOUSE_mousedown=0
      MOUSE_mousepress=0
   endif
return