rds1983 / Myra

UI Library for MonoGame, FNA and Stride
MIT License
704 stars 93 forks source link

How do I prevent button clicks also triggering mouse clicks? #310

Closed mkelcb closed 3 years ago

mkelcb commented 3 years ago

I have a problem with a simple use case, I add a TextButton with a click event:

TextButton button = new TextButton{ GridColumn = 1, GridRow = 5,Text = "SUBMIT"};
button.Click += (s, a) =>{Console.WriteLine("clicked button");};

What is the preferred way to only get 1 click event on the button?

mkelcb commented 3 years ago

I managed to solve this problem with the following idea: when handling mouse clicks, check if the mouse is over any of the interface widgets by checking against their bounds. Maybe not the most elegant, but it works:

Vector2 mouseLoc = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);  
for (int i = 0; i < grid.Widgets.Count; i++)
                if (grid.Widgets[i].Bounds.Contains(mouseLoc)) return; // mouse is over the interface
gamedevcb commented 3 years ago

use _desktop.IsMouseOverGUI