Clicking on an image
BlitzMax Forums/BlitzMax Programming/Clicking on an image
| ||
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 |
| ||
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. |
| ||
Thanks tonyg |
| ||
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. |
| ||
I don't get it. How does that tell you which image was clicked on? |
| ||
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. |
| ||
I don't get it. How does that tell you which image was clicked on? It doesn't, and readpixel is a lil slow. |
| ||
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. |
| ||
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. |
| ||
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. |