Ooops
October 20, 2021, 10:31:38

Author Topic: blitzmax mouse behaviour  (Read 2244 times)

Offline Ashmoor

  • Full Member
  • ***
  • Posts: 115
blitzmax mouse behaviour
« on: March 24, 2018, 01:09:39 »
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).

Offline GW

  • Full Member
  • ***
  • Posts: 222
Re: blitzmax mouse behaviour
« Reply #1 on: March 24, 2018, 03:24:47 »
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.

Offline Henri

  • Sr. Member
  • ****
  • Posts: 355
Re: blitzmax mouse behaviour
« Reply #2 on: March 24, 2018, 08:39:58 »
Hi,

perhaps you could implement a timer to recognize drag event:

Code: BlitzMax
  1. Graphics 640,480
  2.  
  3. Const MOUSE_DOWN:Int = 500  'half a second
  4. Local start:Int
  5.  
  6. While Not KeyHit(KEY_ESCAPE)
  7.         Cls
  8.         If MouseHit(1) Then start = MilliSecs()
  9.        
  10.         If MouseDown(1)
  11.                 If ( MilliSecs()-start ) > MOUSE_DOWN
  12.                         DrawRect 0,0,200,200
  13.                 Else
  14.                         DrawRect 200,0,200,200
  15.                 EndIf
  16.         EndIf
  17.         Flip
  18. Wend

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

Offline IanMartin

  • Full Member
  • ***
  • Posts: 122
Re: blitzmax mouse behaviour
« Reply #3 on: March 24, 2018, 18:35:34 »
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:
Code: [Select]
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/

Offline col

  • Hero Member
  • *****
  • Posts: 599
Re: blitzmax mouse behaviour
« Reply #4 on: March 24, 2018, 22:06:26 »
Maybe something like this?

Code: [Select]

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

Offline Ashmoor

  • Full Member
  • ***
  • Posts: 115
Re: blitzmax mouse behaviour
« Reply #5 on: March 25, 2018, 02:11:11 »
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.

Offline Ashmoor

  • Full Member
  • ***
  • Posts: 115
Re: blitzmax mouse behaviour
« Reply #6 on: March 25, 2018, 02:38:23 »
I came up with this code that acts as "on release" :

Code: [Select]
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

Offline TomToad

  • Hero Member
  • *****
  • Posts: 556
Re: blitzmax mouse behaviour
« Reply #7 on: March 26, 2018, 12:48:48 »
Example of drag-n-drop :)
Code: BlitzMax
  1. SuperStrict
  2. Graphics 800,600
  3.  
  4. Local RectX:Float = 350 'upper left corner of square
  5. Local RectY:Float = 250
  6. Local MX:Int, MY:Int 'To storew mouse position
  7. Local Drag:Int = False 'flag for if we are dragging or not
  8. Local OffX:Float, OffY:Float 'position of mouse relative to square
  9. SetColor 0,0,255 'blue square
  10.  
  11. While Not KeyHit(KEY_ESCAPE) And Not AppTerminate()
  12.         'draw the square
  13.         Cls
  14.         DrawRect RectX, RectY, 100,100
  15.         Flip
  16.        
  17.         'store mouse position
  18.         MX = MouseX()
  19.         MY = MouseY()
  20.        
  21.         If MouseDown(1) 'Check if mouse button is pressed
  22.                 If Drag 'are we currently dragging the mouse?
  23.                         RectX = MX - OffX 'update the square's coordinates
  24.                         RectY = MY - OffY
  25.                 Else 'Mouse has just been clicked
  26.                         If MX >= RectX And MY >= RectY And MX < RectX+100 And MY < RectY+100 'clicking on square?
  27.                                 Drag = True 'start the dragging
  28.                                 OffX = MX - RectX 'set the offset
  29.                                 OffY = MY - RectY
  30.                         End If
  31.                 End If
  32.         Else If Drag 'Was the mouse just released?
  33.                 Drag = False 'end drag mose
  34.                 '...Place any code here you need upon release of mouse
  35.         End If
  36. Wend
  37.  
  38.  
------------------------------------------------
8 rabbits equals 1 rabbyte.

Offline Ashmoor

  • Full Member
  • ***
  • Posts: 115
Re: blitzmax mouse behaviour
« Reply #8 on: March 26, 2018, 19:17:22 »
@TomToad your code example is very clear. Thanks.

Offline Derron

  • Hero Member
  • *****
  • Posts: 3667
Re: blitzmax mouse behaviour
« Reply #9 on: March 26, 2018, 20:58:42 »
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

Offline TomToad

  • Hero Member
  • *****
  • Posts: 556
Re: blitzmax mouse behaviour
« Reply #10 on: March 26, 2018, 21:29:50 »
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.

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal