[bb] Mouse Include by Curtastic [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:41

Previous topic - Next topic

BlitzBot

Title : Mouse Include
Author : Curtastic
Posted : 1+ years ago

Description : Stores mousespeed, mousehit, mousedown, mousex
Tells if the user doubleclicked. Tells if a button was released this loop.
Each button also remembers where it was when the click started.

Just include it, and in your loop call mouseupdate()

I am using this for my RTS and my GUI.
Here is an example
;Mouse Example
;doubleclick to exit
;right click and drag to make lines


Include "mouseinclude.bb"

Graphics 800,600,0,2
SetBuffer BackBuffer()

mousecontrolled=1
HidePointer

;background image
image=CreateImage(800,600)

;main loop
Repeat
mouseupdate()

;right click to make lines
If mousereleased Then
SetBuffer ImageBuffer(image)
Line mousex,mousey,mouserstartx,mouserstarty
SetBuffer BackBuffer()
EndIf

DrawBlock image,0,0


If mouserdown Then
Text 1,1,"right button is down"
EndIf


;double click test
If mouselhitdouble Then
End
EndIf

;right click and drag shows the line
If mouserdown Then
Line mousex,mousey,mouserstartx,mouserstarty
EndIf


;draw mouse cursor
Rect mousex,mousey,5,5

Flip


If KeyHit(1) Then End
Forever


.
.
.
.
Here is the include file: [/i]

Code :
Code (blitzbasic) Select
;do not use Mouse_Speed(). Use mouseuttonspeed
;do not use MouseHit(). Use mouseuttonhit
;if mousecrontrolled then
;do not use MouseX() Or MouseY(). Use mousex and mousey
;do not use MoveMouse(). use mousex=


Type Mouse
;button left
Field bl.mousebutton
;button right
Field br.mousebutton
;button middle
Field bm.mousebutton
;the position of the mouse
Field x,y
;stores mousexspeed(),mouseyspeed()
Field speedx,speedy,speedz
;this tells if the mouse pointer is hidden and kept at the center and you draw your own pointer
Field controlled
;how many millisecs are allowed for a double click
Field hitdoublespeed
End Type
Global mouse.mouse=New mouse

mousehitdoublespeed=300


mousel=New mousebutton
mouselindex=1
mouser=New mousebutton
mouserindex=2
mousem=New mousebutton
mousemindex=3


Type MouseButton
;what number blitz uses for this button
Field index

;where the mouse was when the click started
Field startx,starty
;if the button whas hit this loop
Field hit
;tells if the button was let go this loop
Field released
;tells if the button is down this loop
Field down
;tells if the button was doubleclicked this loop
Field hitdouble
;when it was let go
Field releasedtime
;internal: makes sure no clicks were missed
Field hitcheck
End Type










;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
Function MouseUpdate()
Local b.mousebutton


mousespeedz=MouseZSpeed()
mousespeedx=MouseXSpeed()
mousespeedy=MouseYSpeed()
If mousecontrolled Then
mousex=mousex+mousespeedx
mousey=mousey+mousespeedy
MoveMouse GraphicsWidth()/2,GraphicsHeight()/2

;done after movemouse to fix mouse problems on some computers
MouseXSpeed()
MouseYSpeed()

mousex=keepin(mousex,0,GraphicsWidth()-1)
mousey=keepin(mousey,0,GraphicsHeight()-1)

Else
mousex=MouseX()
mousey=MouseY()
EndIf


For b=Each mousebutton
beleased=0
bhitdouble=0

bhit=MouseHit(bindex)>0
bdown=MouseDown(bindex)

If bhitcheck=0 Then bhitcheck=bhit
If bhitcheck=1 And bdown=0 Then
beleased=1
bhitcheck=0
EndIf

If bhit=1 Then
bstartx=mousex
bstarty=mousey

If MilliSecs()-beleasedtime<mousehitdoublespeed Then
bhitdouble=1
EndIf
beleasedtime=MilliSecs()
EndIf
Next

End Function


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;pass Null if you want to flush all buttons
Function MouseFlush(mb.mousebutton,speeds=1)
Local mb2.mousebutton


For mb2=Each mousebutton
If mb2=mb Or mb=Null Then

mb2hit=0
mb2startx=0
mb2starty=0
MouseHit(mb2index)
mb2hitcheck=0

EndIf
Next

If speeds=1 Then
MouseZSpeed()
MouseXSpeed()
MouseYSpeed()
mousespeedz=0
mousespeedx=0
mousespeedy=0
EndIf
End Function



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;keeps num in bounds
;returns the same number if its already in bounds.
Function KeepIn#(num#,min#,max#)
If num<min Then
Return min
ElseIf num>max
Return max
EndIf
Return num
End Function


Comments :


churchaxe(Posted 1+ years ago)

 wow, this is exactly what I needed for my GUI, cool stuff!