Archived

This topic is now archived and is closed to further replies.

Kaezin

Knowing when a button is clicked...

Recommended Posts

Alright this is probably something really easy, but how do I know when someone clicks on a certain area or button of mine? (Using C++ and DX). Would I have to loop through every button''s area coordinates to figure out if it was clicked (this seems a bit redundent), or please tell me if there is an easier way? Tell me if you don''t understand what I mean; I usually forget to say a lot of things =P Thanks in advance! By the way, I''m not so much looking for code, but rather just a way to do it. But any code helps a lot =D

Share this post


Link to post
Share on other sites
That''s what I was going to do before, so do I have to loop through every single button in order to find out which was pressed?

Upon looking back on my question, it looks really stupid to me and I don''t think this is such a big deal anymore =P

Share this post


Link to post
Share on other sites
well, if you only have a few buttons, it won''t take very long at all... but if you like, you can divide the screen into rectangular sections, and figure out which buttons are in which sections... then when the mouse moves, find out what section it is in, and then only test buttons in that section...

Share this post


Link to post
Share on other sites
Just iterate through a list of buttons and check them. The time to check will increase as you add more buttons but it won't get noticeable unless you're using a LOT of buttons (And how many are you really going to need on screen at any time? Is this really going to be a speed bottleneck in your program?).

If you really want a method that doesn't increase as you add buttons, store a two dimensional array of ints that has the same dimensions as your window/viewing area. Set all those ints so that they correspond to the index of the appropriate button in an array/vector of buttons. When the user clicks the screen at coordinates (x,y), check the value of the two dimensional array at (x,y) and this will tell you the index of the button that was pressed.


      
int buttonIndices[800][600];
button buttons[100];

button[0].x1 = button[0].y1 = 100;
button[0].x2 = button[0].y2 = 200;

for (int i=button[0].x1; i < button[0].x2; i++)
for (int j=button[0].y1; i < button[0].y2; i++)
buttonIndex[i][j] = 0;

button & whichButton(int x, int y)
{
return buttons[buttonIndicies[x][y]];
}


That's pretty naive code of course, I'm just trying to illustrate what I mean.

Edit: I suck at teh C++

[edited by - Dobbs on July 27, 2002 4:46:01 PM]

[edited by - Dobbs on July 27, 2002 4:47:05 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Now that we''re at it, why don''t we invent some kind of atom fusion system or a cure for cancer...

Even if you have a thousand buttons, checking each one every frame will not cost much, even on a prehistoric machine. So you could compare every button one by one at will, either by using some linked list or an array with the data you need (namely top, bottom, left and right coords). You''d probably use function pointers if you want an absoltuly generic data container, or simply use a bool flag which you''ll then consult on a later ocasion in the pipe line, if you want to keep the function simple (or you could just expand it out and write every single line of code the computer could ever imaginably run, mind you it might be a good idea to break or return as soon as you''ve found your target).

If you still want to do some kind of "sorting", then a simple quad-tree based thing will probably do all you want, cut the screen in 4 which then either point to 4 more rectangles or a array of triangle, as krez points out. Mind you, no matter how many compares you save, this will make you waste time writing and proofing more code.

No matter what your solution, don''t loose to much time on such a frivolous thing, concentrate on what you want the butttons to do...

If you want an introduction to testing a cursor click in 3d space, don''t forget to see the pick example in the DX sdk.

Share this post


Link to post
Share on other sites
quote:
Original post by krez
that''d be:
800 x 600 = 480,000 ints
480,000 x 4 = 1,920,000 bytes
almost 2 megs!


I never said it was a good idea, just that it has constant running time (in other words it was a joke). I''m well aware of how stupid it is, but you can use similar techniques in other situations to solve seemingly very complex problems.

Share this post


Link to post
Share on other sites
If you are very worried about speed then I'd just divide the screen into separate pieces. I'm doing this for my 2d game for collision detection on a map. You can divide it up into nice even rectangles, and when the time comes, find out which rectangle the mouse resides in. From there on you only need to test for the buttons that reside in that rectangle.


edit: I see this method has been mentioned already.. erhm, well it still works well for collision detection

[edited by - blueEbola on July 28, 2002 4:09:17 PM]

Share this post


Link to post
Share on other sites