Button mouse over detection, how?

BlitzPlus Forums/BlitzPlus Programming/Button mouse over detection, how?

Apollonius(Posted 2006) [#1]
How do I detect if the cursor is over gb_ship? If it's over I was replace it with gb_shipx and when it moves off it returns to gb_ship.
(Note: I don't wana use a custom image for my cursor)
Global gb_ship = LoadImage("button_ship.bmp")
Global gb_shipx = LoadImage("button_shipx.bmp")


It should be quite simple but I don't know how.


CS_TBL(Posted 2006) [#2]
If MouseX() >= playerleftborder and MouseX<> <= playerrightborder
    If MouseY() >= playerupperborder and MouseY<> <= playerbottomborder
; mouse is on player
  
EndIf
  
EndIf


something like this, haven't tested, just woke up, prolly full of bugs orso :P


Apollonius(Posted 2006) [#3]
WHat would you do if you wanted it to be more precise, like if it's a circle? Then you'd have little corners that shouldn't trigger the action in question...?


CS_TBL(Posted 2006) [#4]
use a map.

The actual button would be square or rect, when clicking on it, check the x,y coord, compare it with a map (for example an image of a white filled circle on a black background), if the result of the comparison is white, then you clicked on the circle.


Apollonius(Posted 2006) [#5]
Ok, I'm stupid, cause I don't understand. Let me try to cook up soemthing and get back to you.


Andres(Posted 2006) [#6]
ImagesOverlap(image%, x%, y%, pixel%, MouseX(), MouseY())


Apollonius(Posted 2006) [#7]
Andres, Nice! That'd be a time saver!

CS_TBL, I don't think the command above would work with draw Oval shapes,

What would be the simplest way to have that circle change color on mouse over.
Graphics 800,600,16,2
SetBuffer BackBuffer()

Global check_mouse$="off"
Global circle_x=50, circle_y=50, circle_width=50, circle_height=50 

; Create the timer to track speed 
frameTimer=CreateTimer(60)
While Not KeyHit(1)
WaitTimer(frameTimer)
Cls



If(check_mouse$="on") Then
	Oval circle_x,circle_y,circle_width,circle_height,1
	Color 0,255,0
EndIf

If(check_mouse$="off") Then
	Oval circle_x,circle_y,circle_width,circle_height,1
	Color 255,255,0
EndIf

Flip
Wend:End



WolRon(Posted 2006) [#8]
Graphics 800,600,16, 2
SetBuffer BackBuffer()

Global check_mouse$="off"
Global circle_x=50, circle_y=50, circle_width=50, circle_height=50 

; Create the timer to track speed 
frameTimer=CreateTimer(60)
While Not KeyHit(1)
WaitTimer(frameTimer)
Cls

If(check_mouse$="on") Then
	Color 0,255,0
	Oval circle_x,circle_y,circle_width,circle_height,1
EndIf

If(check_mouse$="off") Then
	Color 255,255,0
	Oval circle_x,circle_y,circle_width,circle_height,1
EndIf

pix = ReadPixel(MouseX(), MouseY())
pix = (pix Shl 8) Shr 8
If pix > 0
	check_mouse$ = "on"
Else
	check_mouse$ = "off"
EndIf

Flip
Wend:End



Apollonius(Posted 2006) [#9]
omfg, that's awesoem and it works! WolRon, now explain me this, please:

pix = ReadPixel(MouseX(), MouseY())
pix = (pix Shl 8) Shr 8
If pix > 0
	check_mouse$ = "on"
Else
	check_mouse$ = "off"
EndIf

Chinese! :P


Andres(Posted 2006) [#10]
If you want to use it only with an oval then you could use function GetDistance:

Graphics 300, 300, 16, 2

While Not KeyHit(1)
	If GetDistance#(MouseX(), MouseY(), GraphicsWidth() / 2, GraphicsHeight() / 2) < 25 Then End
	Cls
	Color 200, 200, 200
	Oval GraphicsWidth() / 2 - 25, GraphicsHeight() / 2 - 25, 50, 50
	Flip
Wend

Function GetDistance#(x1#, y1#, x2#, y2#)
	Return Sqr((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))
End Function



WolRon(Posted 2006) [#11]
ReadPixel command is self-explanatory.

Shl (shift left) and Shr (shift right) are there to shift the bits of the colors value 8 bits left and then 8 bits right. This is to erase the alpha transparency value in the color.

example:
01234567 01234567 01234567 01234567
 alpha      red     green    blue

If any value was in the alpha part, it will be lost with the Shl. Then I shift the data 8 bits right again to put it back where it should be.
Basically, it's just a very fast way to erase the alpha part without effecting the color parts.