Clicking on an image

BlitzMax Forums/BlitzMax Programming/Clicking on an image

zambani(Posted 2008) [#1]
What's the best way to detect if a user has clicked on an TImage or Pixmap that has been rendered to the screen with the draw command?


Thanks in advance


tonyg(Posted 2008) [#2]
It's going to depend.
If it is a rotated, and/or scaled and/or oddly shaped image with masking and you want pixel perfect collision then ImagesCollide2 command.
If you simply want to check a rect than either imagescollide or colliderect.
Finally, you might want to do your own check. If mousehit(1) is positive then check with a 1*1 pixel rect at the mousex(), mousey() versus a rect with Image_x,image_y,image_x+imagewidth(image),image_y+imageheight(image).
There's lots of example of this in these forums or code arhives.


zambani(Posted 2008) [#3]
Thanks tonyg


Schragnasher(Posted 2008) [#4]
if you are using a .png, i use this statement.

If ReadPixel(hitmap, MouseX() - x, MouseY() - y) & $FF000000 Then


basically it reads a pixmap(aka hitmap) at the x and y mouse location relative to itsposition on screen and checks for a fully alpha pixel.


zambani(Posted 2008) [#5]
I don't get it. How does that tell you which image was clicked on?


Dreamora(Posted 2008) [#6]
collideimage and colliderect, the later with an 1x1 large rect.

if you put the timage handle in the collideimage call then you get an object back that you can cast to TImage again and know which one it was.


plash(Posted 2008) [#7]
I don't get it. How does that tell you which image was clicked on?

It doesn't, and readpixel is a lil slow.


Schragnasher(Posted 2008) [#8]
the image will have to tell you it was clicked on. And yeah im assuming readpixel is slow, but for testing alpha channeled images i don't know any other method. and masks dont get the nice quality of alphas.

but as i said the image should check if its clicked itself. and tell your program somehow. basically you have to update the button when the user hits the mouse button. It will check if it was hit in whatever way you choose and somehow tell the program.


Schragnasher(Posted 2008) [#9]
some pseudo type code.

i basically update every visible button each loop of the game. if the mouse if not over it, it does nothing and moves on, if it is and the mouse button is being pressed, you check for pixel collision somehow, if thats true, then it was hit. you could say it was clicked but i usually wait for the mouse button to be released while still over top the image before calling it clicked.


tonyg(Posted 2008) [#10]
Schragnasher, I think you'll need to provide some example code as your method doesn't make much sense to me and seems to be built around problems that I don't think exist.