Mouse functions
Makes global variables to read the current mouse position and the state of the three mouse buttons (click, release, held state).
Detecting button release instead of push can be useful when clicking and holding should have different outcomes.
Setup
To enable mouse mode, use mouseon()
in _init() or after selecting the input mode. This activates the dev mode flag.
Getting mouse values
-
Run
updatemouse()
in _update() - Optional: Run
mousetime()
in _update() to fetch whether a mouse button is being held longer than a click
Global variables
These are the values produced by updatemouse() each tick.
-
mx
,my
: mouse position (0-127) -
m1
,m2
,m3
: mouse button is down -
m1p
,m2p
,m3p
: new press this tick m1r
,m2r
,m3r
: button just released
These are the values produced by mousetime() each tick.
-
m1h, m2h, m3h: button held down, not just a quick click
- m1t, m2t, m3t: how long the button has been held, in ticks
The timing of holds can be supplied as an argument to the mousetime function.
mousetime(10)
will make m1h
TRUE after 10 ticks (a third of a second)
The default time is 7 ticks (works well at 30fps, double it for 60fps)
Reducing the code/tokens
- If you only need to monitor e.g. left mouse clicks, remove m2 and m3 code.
- Remove the mwheel variable if not needed.
Drag and drop
In the demo tab I demonstrate how these variables are used to power drag and dropping objects, while also detecting clicks on the items only when they're not dragged.
Cursors and button icons
In the sprite section you'll find a number of mouse cursors and controller button sprites.
--mouse functions --by snjo function mouseon() poke(0x5f2d,0x1) end function updatemouse() --mouse button is down m1 = stat(34)&1>0 m2 = stat(34)&2>0 m3 = stat(34)&4>0 --new mouse clicks m1p = m1 and not m1old m2p = m2 and not m2old m3p = m3 and not m3old --mouse released m1r = m1old and not m1 m2r = m2old and not m2 m3r = m3old and not m3 m1old,m2old,m3old = m1,m2,m3 -- constrain to canvas mx = mid(0,stat(32),127) my = mid(0,stat(33),127) -- optional actual mouse coord, often unsafe --mxreal = stat(32) --myreal = stat(33) --mouse wheel moved? mwheel = stat(36) end function mousetime(holdtime) holdtime = holdtime or 7 m1t = m1 and m1t+1 or 0 m1h = m1t > holdtime --held down m2t = m2 and m2t+1 or 0 m2h = m2t > holdtime m3t = m3 and m3t+1 or 0 m3h = m3t > holdtime --held down end |
[Please log in to post a comment]