Mouse click on image

BlitzMax Forums/BlitzMax Beginners Area/Mouse click on image

Qweeg(Posted 2005) [#1]
Could someone please tell me the best way to determine whether a user has clicked the mouse when the pointer is on a certain image? In this case the image is a circle.

Thanks in advance


Scott Shaver(Posted 2005) [#2]
there is probably an easier way to do this but to see if a point is inside of a circle.

Equation of a Circle: (parametric coordinates)
for a circle with origin (j, k) and radius r:
x(t) = r cos(t) + j y(t) = r sin(t) + k

tes to see if the distance from the center of the circle to the point is longer than the radius of the circle.


SoggyP(Posted 2005) [#3]
Hello.

Set up collisions and check to see if mouse pressed?

Goodbye.


Scott Shaver(Posted 2005) [#4]
Or I suppose you could test for a particular color being at the location clicked inside of the image. So if the image is 50x50 and drawn at location 10,10 and the mouse is clicked at 25,25 then check the pixel in the image at location 15,15 to see if it is not black (clicked). (assuming the background of the image is black)


SoggyP(Posted 2005) [#5]
Hello.

Set up collisions and check to see if mouse pressed?

:o)

Goodbye.


EOF(Posted 2005) [#6]
This example uses a 'dummy' single-pixel image for testing collisions under the mouse.

There is no need to move or draw the dummy image. Just tell ImagesCollide() where the cursor is in relation to the image to test against:

' Mouse over image

Graphics 640,480,0,SOFTSYNC

' create a circle test image
Local image=CreateImage(64,64,1,DYNAMICIMAGE|MASKEDIMAGE)
Cls ; DrawOval 0,0,64,64 ; GrabImage image,0,0

' create a single pixel dummy image
dummyCursorImage=CreateImage(1,1,1,DYNAMICIMAGE|MASKEDIMAGE)
Plot 0,0 ; GrabImage dummyCursorImage,0,0


Repeat
	Cls
	mx=MouseX() ; my=MouseY()
	' draw the circle image
	DrawImage image,200,100
	' test for collision against dummy image
	result=ImagesCollide(image,200,100,0 , dummyCursorImage,mx,my,0)
	DrawText "Collision = "+Mid$("NO YES",result*4,3) , 10,10
	Flip
	FlushMem
Until KeyHit(KEY_ESCAPE)

End
 



Qweeg(Posted 2005) [#7]
Thanks everyone for your tips. Just wondering though, the example using the imagesCollide calls for a dummy image. It looks like I could maybe also do this using collideImage instead without creating a dummy image -something like:

CollideImage arrBalls[0].imgBall,500,600,0,0,1
If MouseDown(1) And CollideImage(arrBalls[0].imgBall,MouseX(),MouseY(),0,1,0)


So would this way of things be much slower? The collideImage function seems to do pixel perfect collision so is presumably slower, but there is no need to plot a point and grab it to an image.


Sarge(Posted 2005) [#8]
I use this for my gui,

Function MouseCollision( X, Y, Width, Height, Value:Int )
	
	Select Value
			
		Case 1 'Mouse Down
	
			If MouseX()>=x And MouseY()>=y And MouseX()<=x+width And MouseY()<=y+height And MouseDown(1)
				Return True
			End If
			
		Case 2 'Mouse Over
	
			If MouseX()>=X And MouseY()>=Y And MouseX()<=X+Width And MouseY()<=Y+Height
				Return True
			End If
			
	End Select

End Function


This wont work for a circle though.