Whats the best way to check mouseclick locations in window

Just starting out? Need help? Post your questions and find answers here.
Harbinger
User
User
Posts: 10
Joined: Mon Mar 18, 2019 6:40 pm

Whats the best way to check mouseclick locations in window

Post by Harbinger »

I'm developing a new game in PB which uses a table of card slots, so that when you have selected a card to place, you can then click on a card slot to move that card there. I'm simply using image placement on an image window. No 2D or 3D commands.

The problem is there are 100 possible rectangles where the player can place the selected card. They are not in a grid. I need to know what method is the best for having the application check the mouse click so as to determine the location to place the selected card?

Should I use a hundred buttons, or an array of rectangle coordinates to compare the current mouseclick, or some other method? I don't want there to be a delay between the mouseclick and highlighting the selected rectangle (with my drawing routine).
PurePilish
User
User
Posts: 10
Joined: Mon Oct 07, 2024 9:15 am

Re: Whats the best way to check mouseclick locations in window

Post by PurePilish »

With today's GHz processors, 100 is a small number, so I would guess that it is not a problem (and simple) to test the mouseclick coordinates against an array of rectangle coordinates. Of course that can be made more sophisticated, but a simple loop over all 100 possibilities is not unreasonable. I would suggest doing that at first - it could always be optimized later, but I doubt that will be necessary. If you do the four tests for each rectangle (against its coordinates Xmin, Xmax, Ymin, Ymax) separately instead of in one IF statement then it will take much less than 400 comparisons on average, because often the first test will fail, or the second, etc. Plus there's another factor of 2 in your favor because on average you'll only have to test ~50 rectangles before finding the right one. So maybe 60 or 70 comparisons on average instead of 400.
User avatar
mk-soft
Always Here
Always Here
Posts: 6270
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Whats the best way to check mouseclick locations in window

Post by mk-soft »

One CanvasGadget and an array of Type rect with for:next run ...
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Harbinger
User
User
Posts: 10
Joined: Mon Mar 18, 2019 6:40 pm

Re: Whats the best way to check mouseclick locations in window

Post by Harbinger »

OK I'll first try a loop to test for the mouseclick within rects.
I can probly optimize it by deductive testing, such as "is mouseX greater than firstrowX?" and winnow it down that way.

Thanks for your suggestions guys
miso
Enthusiast
Enthusiast
Posts: 484
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: Whats the best way to check mouseclick locations in window

Post by miso »

A hundred rectangle check is so quick, You won't need optimization.
Post Reply