0
\$\begingroup\$

Well, I'm trying to code a gui for my engine, but I've got some problems. I know how to make a UI overlay but buttons are still black magic for me. Anything I tried was to compilcated ( if it goes big ). To Example I tried to look if the mouse position is the same as the Pixel that is showing the button. But If I use some bigger areas it's getting to complicated. Now I'm searching for a Tutorial how to implement your own gui. I'm really confused about it. Well I hope you have/ know some good tutorials.


By the way, I took a look at the DXUTSample, but it's to big to get overview.

\$\endgroup\$
  • \$\begingroup\$ I know this question may have been asked couple times, but I'm too confused about it. So I'm searching for help here. \$\endgroup\$ – LaVolpe Dec 14 '12 at 21:07
1
\$\begingroup\$

So what I'm understanding is that you are looking for a quick way to test for GUI button presses?

The way I check is to have each button contain its own rectangle and check for a collision between the mouse and the button rectangle.

#include <windows.h>

class Button
{
 public:
     //The 'rect' variable is the 'Hotspot' for the button
     //Whether that is the whole button or just a select piece of it.
     RECT rect;

     //The constructor sets the 'rect' var accordingly.
     Button(RECT rect){this->rect = rect;};

     //Checks for collision between two RECTs
     bool RectCollision(RECT rectA, RECT rectB)
     {
         if(rectA.left > rectB.right)
             return false;
         if(rectA.right < rectB.left)
             return false;
         if(rectA.top > rectB.bottom)
             return false;
         if(rectA.bottom < rectB.top)
             return false;

         return true;
     }

     //This function is ran if the mouse button is pressed
     //which is checked in your 'WindowProc' function.
     bool CheckForButtonPress()
     {
          POINT mousePos;
          if(GetCursorPos(&mousePos)) //Gets the mousePos on the screen
          {
              if(ScreenToClient(hwnd, &mousePos)) //Sets the mousePos relative to the window
              {
                  RECT mouseRect = {mousePos.x, mousePos.y, 1, 1};
                  if(RectCollision(rect, mouseRect))
                      return true;
                  return false;
              }
          }

          return false;
     }
}

Obviously you can do some optimizations and changes. But I hope you get the gist of it.

Sincerely -MasterBaldwin

\$\endgroup\$
  • \$\begingroup\$ Yeah, that's what I was searching for thumbs up Thank you \$\endgroup\$ – LaVolpe Dec 14 '12 at 22:19
  • \$\begingroup\$ Your very welcome. Glad I could help :) \$\endgroup\$ – Mike Baldwin Dec 14 '12 at 22:24
  • \$\begingroup\$ I was just confused how to get collision test. Googled whole Google trough. Didn't find anything. German Google is crap. \$\endgroup\$ – LaVolpe Dec 14 '12 at 22:28
  • \$\begingroup\$ Just a note if you're going to be using RECT, there is a PtInRect function, as well as a couple of others \$\endgroup\$ – bobobobo Dec 14 '12 at 23:33
  • \$\begingroup\$ Thank you, but saw it also now on MSDN. \$\endgroup\$ – LaVolpe Jan 30 '13 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.