views:

339

answers:

3

How can I get the x,y coordinate of a mouse click, to see if it is over my menu button drawn by directx? Currently, my codebase has the following mouse-related class that doesn't seem to be able to give me this..I'm not sure how this might work.

InputMouse::InputMouse() :
    m_LastX(-1),
    m_LastY(-1)
{
    m_MouseActionEvent.clear();
}

InputMouse::~InputMouse()
{

}

void InputMouse::PostUpdate()
{
    m_CurrentAction.clear();
}

bool InputMouse::IsEventTriggered(int eventNumber)
{
    for (unsigned int i = 0; i < m_CurrentAction.size(); i++)
    {
        if (m_MouseActionEvent.size() > 0 && m_MouseActionEvent[m_CurrentAction[i]] == eventNumber)
        {
            return true;
        }
    }
    return false;
}

void InputMouse::AddInputEvent(int action, int eventNumber)
{
    m_MouseActionEvent[action] = eventNumber;
}

void InputMouse::SetMouseMouse(int x, int y)
{
    if (m_LastX != -1)
    {
        if (x > m_LastX)
        {
            m_CurrentAction.push_back(MOUSE_RIGHT);
        }
        else if (x < m_LastX)
        {
            m_CurrentAction.push_back(MOUSE_LEFT);
        }

        if (y > m_LastY)
        {
            m_CurrentAction.push_back(MOUSE_UP);
        }
        else if (y < m_LastY)
        {
            m_CurrentAction.push_back(MOUSE_DOWN);
        }
    }

    m_LastX = x; 
    m_LastY = y;
}
+1  A: 

DirectX or not, GetCursorPos is going to retrieve the position of the mouse in screen co-ordinates. ScreenToClient will map the screen relative point to a point relative to the client area of your window/directX surface.

Chris Becke
+1  A: 

If your menu buttons are 2D, this should be as simple as remembering the screen co-ordinates used for your buttons.

If you're trying to determine if a click lands on a 3D object that's been rendered, then the technique you are looking for is called Picking.

A simple Google for "directx picking" comes up with some good results:

Basically, the technique involves converting the mouse click into a ray into the scene. For your menu items, a simple bounding box will probably suffice for determining a 'hit'.

Adam Luchjenbroers
A: 

Once an object is drawn, the system has no knowledge of which pixels on the screen it changed, nor do those pixels know which object or objects changed it (nor if those objects even still exist). Therefore, if you need to know where something is on-screen, you have to track it yourself. For buttons and other GUI elements this usually means keeping your GUI in memory along with the rectangles that define the boundaries of each element. Then you can compare your mouse position to the boundary of each element to see which one it is pointing at.

Kylotan