Ooops
January 26, 2021, 12:10:23 PM

Author Topic: [bmx] Mouse handling by Pineapple [ 1+ years ago ]  (Read 597 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] Mouse handling by Pineapple [ 1+ years ago ]
« on: June 29, 2017, 12:28:40 AM »
Title : Mouse handling
Author : Pineapple
Posted : 1+ years ago

Description : Generalized mouse update function tracks position, velocity, clicked/down/up/double-clicked, and other info. Check the huge rem block for details.

Code :
Code: BlitzMax
  1. '       --+-----------------------------------------------------------------------------------------+--
  2. '         |   This code was originally written by Sophie Kirschner (sophiek@pineapplemachine.com)   |  
  3. '         | It is released as public domain. Please don't interpret that as liberty to claim credit |  
  4. '         |   that isn't yours, or to sell this code when it could otherwise be obtained for free   |  
  5. '         |                because that would be a really shitty thing of you to do.                |
  6. '       --+-----------------------------------------------------------------------------------------+--
  7.  
  8.  
  9. SuperStrict
  10.  
  11. Import brl.polledinput
  12. Import brl.system
  13.  
  14. Rem
  15.        
  16.         Useful globals:
  17.        
  18.         mx : mouse X position
  19.         my : mouse Y position
  20.        
  21.         mxv : mouse X velocity (change since last update)
  22.         myv : mouse Y velocity
  23.        
  24.         lmb : left mouse button CLICKED (has just been pressed down)
  25.         rmb : right mouse button CLICKED
  26.         mmb : middle mouse button CLICKED
  27.        
  28.         lmbx : mouse X position at last left mouse button CLICKED
  29.         lmby : mouse Y position at last left mouse button CLICKED
  30.         rmbx : mouse X position at last right mouse button CLICKED
  31.         rmby : mouse Y position at last right mouse button CLICKED
  32.         mmbx : mouse X position at last middle mouse button CLICKED
  33.         mmby : mouse Y position at last middle mouse button CLICKED
  34.                
  35.         lmbd : left mouse button DOWN (is currently held down)
  36.         rmbd : right mouse button DOWN
  37.         mmbd : middle mouse button DOWN
  38.        
  39.         lmbu : left mouse button UP (has just been released)
  40.         rmbu : right mouse button UP
  41.         mmbu : middle mouse button UP
  42.        
  43.         lmbc : left mouse button DOUBLE-CLICK (has just been double-clicked)
  44.         rmbc : right mouse button DOUBLE-CLICK
  45.         mmbc : middle mouse button DOUBLE-CLICK
  46.        
  47.         lmbtime : the last time that the left mouse button was CLICKED (on the system millisecs clock)
  48.         rmbtime : the last time that the right mouse button was CLICKED
  49.         mmbtime : the last time that the middle mouse button was CLICKED
  50.        
  51.         doubleclickdelay : maximum number of milliseconds between two clicks in order to register as a double-click
  52.        
  53.         mz : change in mouse z position (since last update)
  54.         nowmz : current mouse z position
  55.        
  56. EndRem
  57.  
  58. Global mx%,my%
  59. Global mxv%=0,myv%=0
  60. Global lmb%,rmb%,mmb%   ' clicked
  61. Global lmbd%,rmbd%,mmbd%        ' down
  62. Global lmbu%,rmbu%,mmbu%        ' up
  63. Global lmbc%,rmbc%,mmbc%        ' double-click
  64. Global lmbx%,lmby%,rmbx%,rmby%,mmbx%,mmby%
  65. Global l_lmb%,l_rmb%,l_mmb%
  66. Global lmbtime%,rmbtime%,mmbtime%
  67. Global mz%,nowmz%,lastmz%
  68. Global doubleclickdelay%=320
  69.  
  70. Function UpdateMouse()
  71.         Local now%=MilliSecs()
  72.         Local nmx%=MouseX(),nmy%=MouseY()
  73.         mxv=nmx-mx;myv=nmy-my
  74.         mx=nmx;my=nmy
  75.         Local lastl%=lmbd,lastr%=rmbd,lastm%=mmbd
  76.         lmbd=MouseDown(1)
  77.         rmbd=MouseDown(2)
  78.         mmbd=MouseDown(3)
  79.         nowmz=MouseZ()
  80.         mz=lastmz-nowmz
  81.         lastmz=nowmz
  82.         lmb=0;rmb=0;mmb=0
  83.         If lastl>lmbd Then lmbu=1 Else lmbu=0
  84.         If lastr>rmbd Then rmbu=1 Else rmbu=0
  85.         If lastm>mmbd Then mmbu=1 Else mmbu=0
  86.         lmbc=0;rmbc=0;mmbc=0
  87.         If lastl<lmbd Then
  88.                 If now-l_lmb<=doubleclickdelay Then
  89.                         lmbc=1
  90.                 Else
  91.                         l_lmb=now
  92.                 EndIf
  93.                 lmb=1
  94.                 lmbx=mx;lmby=my
  95.                 lmbtime=MilliSecs()
  96.         EndIf
  97.         If lastr<rmbd Then
  98.                 If now-l_rmb<=doubleclickdelay Then
  99.                         rmbc=1
  100.                 Else
  101.                         l_rmb=now
  102.                 EndIf
  103.                 rmb=1
  104.                 rmbx=mx;rmby=my
  105.                 rmbtime=MilliSecs()
  106.         EndIf
  107.         If lastm<mmbd Then
  108.                 If now-l_mmb<=doubleclickdelay Then
  109.                         mmbc=1
  110.                 Else
  111.                         l_mmb=now
  112.                 EndIf
  113.                 mmb=1
  114.                 mmbx=mx;mmby=my
  115.                 mmbtime=MilliSecs()
  116.         EndIf
  117. End Function
  118.  
  119. ' Convenience function, returns boolean whether the mouse is inside a rectangular space
  120. Function MouseIn%(x%,y%,w%,h%)
  121.         If mx=>x And mx<x+w And my=>y And my<y+h Then Return 1
  122.         Return 0
  123. End Function
  124.  
  125.  
  126.  
  127. ' Example code
  128.  
  129. Rem
  130.  
  131. Graphics 320,320
  132.  
  133. Global vx%,vy%
  134.  
  135. Repeat
  136.         Cls
  137.         updatemouse()
  138.        
  139.         vx=12;vy=12
  140.        
  141.         val "mx",mx
  142.         val "my",my
  143.         val "mxv",mxv
  144.         val "myv",myv
  145.         val "lmb",lmb,1
  146.         val "rmb",rmb,1
  147.         val "mmb",mmb,1
  148.         val "lmbx",lmbx
  149.         val "lmby",lmby
  150.         val "rmbx",rmbx
  151.         val "rmby",rmby
  152.         val "mmbx",mmbx
  153.         val "mmby",mmby
  154.         val "lmbd",lmbd
  155.         val "rmbd",rmbd
  156.         val "mmbd",mmbd
  157.         val "lmbu",lmbu,1
  158.         val "rmbu",rmbu,1
  159.         val "mmbu",mmbu,1
  160.         val "lmbc",lmbc,1
  161.         val "rmbc",rmbc,1
  162.         val "mmbc",mmbc,1
  163.         val "lmbtime",lmbtime
  164.         val "rmbtime",rmbtime
  165.         val "mmbtime",mmbtime
  166.         val "mz",mz,1
  167.         val "nowmz",nowmz
  168.        
  169.         Flip
  170.         Delay 15
  171. Forever
  172.  
  173. Function val(t$,v%,highlight%=0)
  174.         If highlight And v SetColor 0,255,0 Else SetColor 255,255,255
  175.         DrawText t+": "+v,vx,vy
  176.         vy:+12
  177.         If vy>GraphicsHeight()-24 Then vy=12;vx:+128
  178. End Function
  179.  
  180. EndRem


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal