- Home /
Help in GUI issue - Can't lose focus of a dropdown list when mouse clicks somewhere else?
I'm working on an inventory system. When the player right-clicks an item, it displays some dropdown list of options, "Use", "Equip", etc. Currently the inventory is a grid of buttons. When I right click on a button, the dropdown list appears and I start to use labels instead of actual buttons so that it appears as if the buttons are disabled. Now the issue is that, I want the dropdown to disappear when the player clicks somewhere else other than the list's buttons. Here's the dropdown list code - they're just a bunch of buttons wrapped with a box:
public int Draw()
{
if (entries == null)
{
Debug.Log("Something wrong with entries...");
return -1;
}
float totalHeight = GetTotalEntriesHeight();
float x_offset = spaceBetweenButtons * 4;
float y_offset = spaceBetweenButtons * 4;
GUI.Box(new Rect(position.x, position.y, entryDimensions.x + x_offset, totalHeight + y_offset), "", defaultMenuBackgoundStyle);
int entryIndex = -1;
for (int i = 0; i < entries.Count; i++)
{
// offsetting x by spaceBetweenButtons and y by the height of the entry * i + the space between buttons
bool entryButtonPressed = GUI.Button(new Rect(position.x + x_offset / 2, position.y + i * entryDimensions.y + y_offset / 2, entryDimensions.x, entryDimensions.y), entries[i], defaultEntryStyle);
if (entryButtonPressed)
{
entryIndex = i;
shouldBeShown = false;
break;
}
}
return entryIndex;
}
I tried something like this:
//...
if(Input.GetMouseButtonDown(0) // and even GetMouseButton
{
shouldBeShown = false;
if (entryButtonPressed)
{
entryIndex = i;
break;
}
//...
Now this does hide the list when the player clicks on somewhere else, but for some reason the entryButtonPressed is always false and I get some strange behavior when the player clicks on a button that's directly above the item. So this is definitely not working, also tried repositioning this check in more than one place, all showed the same issue...
I recently knew there's something called a GUIUtility but I don't know how to use it, that is, if I'm supposed to use it. Any help would be appreciated, thanks in advance
Answer by vexe · Jun 25, 2013 at 01:49 AM
OK I solved the issue my self - just had to check if the mouse is within the list box area, if not, check for left/right clicks, if there was any, just hide the list, easy enough!
// draw stuff...
if (!IsMouseContainedWithinBoxArea())
{
if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
{
shouldBeShown = false;
}
}
// ...
private bool IsMouseContainedWithinBoxArea()
{
Vector2 mousePosition_InGUI = GUIUtility.ScreenToGUIPoint(new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y));
bool contained_horizontally = mousePosition_InGUI.x > boxRect.x && mousePosition_InGUI.x < boxRect.x + boxRect.width;
bool contained_verticaally = mousePosition_InGUI.y > boxRect.y && mousePosition_InGUI.y < boxRect.y + boxRect.height;
return contained_horizontally && contained_verticaally;
}