blitzmax mouse behaviour

Started by Ashmoor, March 24, 2018, 01:09:39

Previous topic - Next topic

Ashmoor

Is there a way to differentiate between when the mouse button is down and when it is released? I want to implement two actions, one for when the LMouse Button is held down and dragged and one for when a click happens (press then release or at least releasing the mouse button in a specific spot). Any ideas on how to do that? MouseHit(1) is acting in the same way as MouseDown(1).

GW

You'll need to hold the previous mousedown value in a variable from the previous frame. then compare the current value to the one from last frame.

Henri

Hi,

perhaps you could implement a timer to recognize drag event:

Code (blitzmax) Select

Graphics 640,480

Const MOUSE_DOWN:Int = 500  'half a second
Local start:Int

While Not KeyHit(KEY_ESCAPE)
Cls
If MouseHit(1) Then start = MilliSecs()

If MouseDown(1)
If ( MilliSecs()-start ) > MOUSE_DOWN
DrawRect 0,0,200,200
Else
DrawRect 200,0,200,200
EndIf
EndIf
Flip
Wend


-Henri
- Got 01100011 problems, but the bit ain't 00000001

IanMartin

MouseHit() is how many times it's been clicked since the last call to MouseHit().
MouseDown() is "is it held down right now?"

So you would do something like:
mousepressed=0 
mousereleased=0
mouseheld=0

Graphics 800,600
Repeat
If MouseDown(1)=1 Then
mousepressed=1
mousereleased=0
mouseheld=mouseheld+1
EndIf

If mousepressed=1 And MouseDown(1)=0 Then
mousepressed=0
mousereleased=1
mouseheld=0
EndIf

Cls
DrawText "mousepressed=" + mousepressed, 0, 0
DrawText "mousereleased=" + mousereleased, 0, 20
DrawText "mouseheld=" + mouseheld, 0, 40
Flip
Until KeyHit (KEY_ESCAPE)



And increment a variable like mouseheld so you can count how long the mouse button had been held.  This way you can make a list go faster if the user is holding down the mouse button for a while. 
Platfinity (made with BlitzMax) on Steam:
http://store.steampowered.com/app/365440/Platfinity/

col

Maybe something like this?



Strict

Graphics 800,600

Global isCapturing:Int
Global mouseCaptureX:Int
Global mouseCaptureY:Int
Global mouseReleaseX:Int
Global mouseReleaseY:Int

While Not AppTerminate() And Not KeyDown(KEY_ESCAPE)
Cls

If MouseDown(1)
If Not isCapturing
mouseCaptureX = MouseX()
mouseCaptureY = MouseY()
EndIf
isCapturing = True
EndIf

If MouseDown(1) = False
If isCapturing
mouseReleaseX = MouseX()
mouseReleaseY = MouseY()
EndIf
isCapturing = False
EndIf

If isCapturing
DrawText "isCapturing: True", 0, 0
DrawText "MouseX start: " + mouseCaptureX, 0, 12
DrawText "MouseX start: " + mouseCaptureY, 0, 24
DrawText "MouseX end  : " + MouseX(), 0, 36
DrawText "MouseY end  : " + MouseY(), 0, 48

DrawLine mouseCaptureX, mouseCaptureY, MouseX(), mouseCaptureY
DrawLine MouseX(), mouseCaptureY, MouseX(), MouseY()
DrawLine MouseX(), MouseY(), mouseCaptureX, MouseY()
DrawLine mouseCaptureX, MouseY(), mouseCaptureX, mouseCaptureY
Else
DrawText "isCapturing: False", 0, 0

DrawText "Mouse clicked at: " + mouseReleaseX + ", " + mouseReleaseY, 0, 72
EndIf

Flip
Wend
End
https://github.com/davecamp

"When you observe the world through social media, you lose your faith in it."

Ashmoor

Thank you very much guys. Your examples clarified the difference between MouseDown and MouseHit, MouseHit actually tracks how many times a button switched state from up to down while MouseDown just checks if the button is currently down.

Ashmoor

I came up with this code that acts as "on release" :

Graphics 640, 480

Local mhit1:Int
Local mpressed1:Int


While Not KeyHit(KEY_ESCAPE)
        Cls
       
If MouseDown(1) = False And MouseHit(1) = True Then
mhit1 = 1
Else
mhit1 = 0
End If

If MouseDown(1)
mpressed1 = 1
Else
mpressed1 = 0
End If

     
If mhit1 = 1 Then
        DrawRect 0, 0, 200, 200
EndIf

If mpressed1 = 1 Then
DrawRect 200, 0, 200, 200
EndIf

DrawText ("mh:" + mhit1, 10, 10)

DrawText ("mp:" + mpressed1, 10, 30)

        Flip
Wend

TomToad

Example of drag-n-drop :)
Code (blitzmax) Select
SuperStrict
Graphics 800,600

Local RectX:Float = 350 'upper left corner of square
Local RectY:Float = 250
Local MX:Int, MY:Int 'To storew mouse position
Local Drag:Int = False 'flag for if we are dragging or not
Local OffX:Float, OffY:Float 'position of mouse relative to square
SetColor 0,0,255 'blue square

While Not KeyHit(KEY_ESCAPE) And Not AppTerminate()
'draw the square
Cls
DrawRect RectX, RectY, 100,100
Flip

'store mouse position
MX = MouseX()
MY = MouseY()

If MouseDown(1) 'Check if mouse button is pressed
If Drag 'are we currently dragging the mouse?
RectX = MX - OffX 'update the square's coordinates
RectY = MY - OffY
Else 'Mouse has just been clicked
If MX >= RectX And MY >= RectY And MX < RectX+100 And MY < RectY+100 'clicking on square?
Drag = True 'start the dragging
OffX = MX - RectX 'set the offset
OffY = MY - RectY
End If
End If
Else If Drag 'Was the mouse just released?
Drag = False 'end drag mose
'...Place any code here you need upon release of mouse
End If
Wend

------------------------------------------------
8 rabbits equals 1 rabbyte.

Ashmoor

@TomToad your code example is very clear. Thanks.

Derron

Do not forget that "DND" involves more than just dragging something around.

- drop on target: does the target allow drop? if not, veto and stop drop
- drop successful: does the dropped item swap with a potentially dragged one..
- if an item is not interested in dropping, does the underlaying item accept it ...
- ...

Dragndrop isn't that easy (imho).


bye
Ron

TomToad

Depend on what you are doing with it. Making an editor whee you drag components around? Easy peasy. Making a checkers game? Then check if move is valid. Dragging between applications? More difficult.

My example was just to show a way to move an icon around with the mouse. What you do with it beyond that is up to you.
------------------------------------------------
8 rabbits equals 1 rabbyte.