Recommended Posts

I''m working on making my GUI and I can''t get the selection code down. I''m not using Windows-style GUI (where everything is a window) so I''m having problems. I have code that checks to see if the mouse is over it:

if((Mouse.x > Gui.x) && (Mouse.x < (Gui.x + Width)) && (Mouse.y > Gui.y) && (Mouse.y < (Gui.y + Height)))
{
if(TopMostControl)
{
// The mouse is over the control and its the visible control
}
// The mouse is over the control, but it''s not visible
}

But this didn''t work right. I would try and move it, but it wouldn''t. My code that I have now works right except I can click anywhere and move it. The second problem is that when I do move it, the control moves so that the mouse is pointing at the upper left corner. I tried to do an offset, but it didn''t work. Thanks in advance.
/*
I use DirectX 9 and C++ (Microsoft Visual C++ 6.0 Professional edition)
*/

Share this post

Link to post

Share on other sites

I can't tell you what's wrog with your code (since the problem is probably in the movement code, not the selection code), but here's how dragging should work:

1. When mouse button is pressed down:. 1.1. Find the front-most control that the mouse is over it.. 1.2. Set dragged-control variable to that control.. 1.3. Remember the absolute mouse coordinates.. 1.4. Optionally, if the control is not the top-most control, make it the top-most one.2. When the mouse moves, if dragged-control variable is not null:. 2.1. Find the new absolute mouse coordinates.. 2.2. Subtract old coords from new coords and remember deltas.. 2.3. Remember the new coords instead of the old ones.. 2.4. Add the deltas to the control's coordinates - the result are the control's new coordinates.3. When the mouse button is released:. 3.1. Set the dragged-control variable to null.

EDIT:You don't relly have to use the absolute mouse coordinates, as long as you aslways take them relative to the same point. And there is an alternative method, which is the same except:1.3. Calculate and remember the offset of the mouse cursor from top-left cordner of the control (assuming your corrdinates increase down and right).2.2.-2.3. (not needed) 2.4. Subtract the offset from the new mouse coordinates - the result is the new control coordinate.

Share this post

Link to post

Share on other sites

Go through the algorithm I posted step by step, and make sure each step is working before moving on to the next. The algorithm is not complex. Here's how you should do this:

1. Make sure that a single control is being selected properly. To test that, draw it differently when the mouse is above it. The code you posted above is the correct code for that, with the following conditions:. a. "Gui" is you control.. b. All the coordinates in the if statement are relative to the same point.. c. Your control coordinates are a 1:1 match to your mouse coordinates (which probably means they should be a 1:1 match to you pixel coordinates).. d. The comparisons should be >= and < (no, not <=, unless you're doing x+width-1), rather than > and <.2. once a single control is being selected correctly, make sure the same works with several controls.3. Make sure selection works even if the controls are overlapping.4. Adjust your code to only select the front-most control under the mouse.5. Make sure you can correctly move your controls programmatically (without actually dragging them with the mouse).6. Make sure you know how to calculate offsets correctly, if you intend to use them. They are not necessary in the original version of the algorithm that I posted above.7. Now you should be able to implement the dragging algorithm, and it should work.

If you want more specific help, you will have to give a more specific description of what goes wrong and under which circumstances.

Share this post

Link to post

Share on other sites

Sorry, I tend not to be very clear. I got that part working (mostly), but the rest of it isn''t working now. I''ve decided to change to a static GUI, that way I don''t have to worry about some stuff that I''ve been having problems with. Thanks for your help though!