diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs index 30e42dd..47f5ca9 100644 --- a/MLEM.Ui/Elements/Element.cs +++ b/MLEM.Ui/Elements/Element.cs @@ -298,7 +298,7 @@ namespace MLEM.Ui.Elements { } this.area = new Rectangle(pos, actualSize); - this.OnAreaUpdated?.Invoke(this); + this.System.OnElementAreaUpdated?.Invoke(this); foreach (var child in this.Children) child.ForceUpdateArea(); diff --git a/MLEM.Ui/UiControls.cs b/MLEM.Ui/UiControls.cs index f06d423..faeb306 100644 --- a/MLEM.Ui/UiControls.cs +++ b/MLEM.Ui/UiControls.cs @@ -48,11 +48,11 @@ namespace MLEM.Ui { var mousedNow = this.GetElementUnderPos(this.Input.MousePosition); if (mousedNow != this.MousedElement) { if (this.MousedElement != null) - this.MousedElement.OnMouseExit?.Invoke(this.MousedElement); + this.System.OnElementMouseExit?.Invoke(this.MousedElement); if (mousedNow != null) - mousedNow.OnMouseEnter?.Invoke(mousedNow); + this.System.OnElementMouseEnter?.Invoke(mousedNow); this.MousedElement = mousedNow; - this.System.ApplyToAll(e => e.OnMousedElementChanged?.Invoke(e, mousedNow)); + this.System.OnMousedElementChanged?.Invoke(mousedNow); } if (this.Input.IsMouseButtonPressed(MouseButton.Left)) { @@ -60,11 +60,11 @@ namespace MLEM.Ui { var selectedNow = mousedNow != null && mousedNow.CanBeSelected ? mousedNow : null; this.ActiveRoot.SelectElement(selectedNow); if (mousedNow != null) - mousedNow.OnPressed?.Invoke(mousedNow); + this.System.OnElementPressed?.Invoke(mousedNow); } else if (this.Input.IsMouseButtonPressed(MouseButton.Right)) { this.IsAutoNavMode = false; if (mousedNow != null) - mousedNow.OnSecondaryPressed?.Invoke(mousedNow); + this.System.OnElementSecondaryPressed?.Invoke(mousedNow); } // KEYBOARD INPUT @@ -73,10 +73,10 @@ namespace MLEM.Ui { if (this.SelectedElement?.Root != null) { if (this.Input.IsModifierKeyDown(ModifierKey.Shift)) { // secondary action on element using space or enter - this.SelectedElement.OnSecondaryPressed?.Invoke(this.SelectedElement); + this.System.OnElementSecondaryPressed?.Invoke(this.SelectedElement); } else { // first action on element using space or enter - this.SelectedElement.OnPressed?.Invoke(this.SelectedElement); + this.System.OnElementPressed?.Invoke(this.SelectedElement); } } } else if (this.Input.IsKeyPressed(Keys.Tab)) { @@ -95,24 +95,24 @@ namespace MLEM.Ui { var tapped = this.GetElementUnderPos(tap.Position.ToPoint()); this.ActiveRoot.SelectElement(tapped); if (tapped != null) - tapped.OnPressed?.Invoke(tapped); + this.System.OnElementPressed?.Invoke(tapped); } else if (this.Input.GetGesture(GestureType.Hold, out var hold)) { this.IsAutoNavMode = false; var held = this.GetElementUnderPos(hold.Position.ToPoint()); this.ActiveRoot.SelectElement(held); if (held != null) - held.OnSecondaryPressed?.Invoke(held); + this.System.OnElementSecondaryPressed?.Invoke(held); } // GAMEPAD INPUT else if (this.GamepadButtons.Any(b => this.IsGamepadPressed(b))) { this.IsAutoNavMode = true; if (this.SelectedElement?.Root != null) - this.SelectedElement.OnPressed?.Invoke(this.SelectedElement); + this.System.OnElementPressed?.Invoke(this.SelectedElement); } else if (this.SecondaryGamepadButtons.Any(b => this.IsGamepadPressed(b))) { this.IsAutoNavMode = true; if (this.SelectedElement?.Root != null) - this.SelectedElement.OnSecondaryPressed?.Invoke(this.SelectedElement); + this.System.OnElementSecondaryPressed?.Invoke(this.SelectedElement); } else if (this.DownButtons.Any(this.IsGamepadPressed)) { this.HandleGamepadNextElement(Direction2.Down); } else if (this.LeftButtons.Any(this.IsGamepadPressed)) { diff --git a/MLEM.Ui/UiSystem.cs b/MLEM.Ui/UiSystem.cs index 9ffe3fc..5ada373 100644 --- a/MLEM.Ui/UiSystem.cs +++ b/MLEM.Ui/UiSystem.cs @@ -54,6 +54,15 @@ namespace MLEM.Ui { public Element.DrawCallback OnElementDrawn; public Element.DrawCallback OnSelectedElementDrawn; + public Element.GenericCallback OnElementPressed = e => e.OnPressed?.Invoke(e); + public Element.GenericCallback OnElementSecondaryPressed = e => e.OnSecondaryPressed?.Invoke(e); + public Element.GenericCallback OnElementSelected = e => e.OnSelected?.Invoke(e); + public Element.GenericCallback OnElementDeselected = e => e.OnDeselected?.Invoke(e); + public Element.GenericCallback OnElementMouseEnter = e => e.OnMouseEnter?.Invoke(e); + public Element.GenericCallback OnElementMouseExit = e => e.OnMouseExit?.Invoke(e); + public Element.GenericCallback OnElementAreaUpdated = e => e.OnAreaUpdated?.Invoke(e); + public Element.GenericCallback OnMousedElementChanged; + public Element.GenericCallback OnSelectedElementChanged; public UiSystem(GameWindow window, GraphicsDevice device, UiStyle style, InputHandler inputHandler = null) { this.Controls = new UiControls(this, inputHandler); @@ -74,6 +83,8 @@ namespace MLEM.Ui { root.Element.AndChildren(e => e.OnTextInput?.Invoke(e, key, character)); }); + this.OnMousedElementChanged = e => this.ApplyToAll(t => t.OnMousedElementChanged?.Invoke(t, e)); + this.OnSelectedElementChanged = e => this.ApplyToAll(t => t.OnSelectedElementChanged?.Invoke(t, e)); this.OnSelectedElementDrawn = (element, time, batch, alpha) => { if (this.Controls.IsAutoNavMode && element.SelectionIndicator != null) { batch.Draw(element.SelectionIndicator, element.DisplayArea, Color.White * alpha, element.Scale / 2); @@ -190,11 +201,11 @@ namespace MLEM.Ui { return; if (this.SelectedElement != null) - this.SelectedElement.OnDeselected?.Invoke(this.SelectedElement); + this.System.OnElementDeselected?.Invoke(this.SelectedElement); if (element != null) - element.OnSelected?.Invoke(element); + this.System.OnElementSelected?.Invoke(element); this.SelectedElement = element; - this.System.ApplyToAll(e => e.OnSelectedElementChanged?.Invoke(e, element)); + this.System.OnSelectedElementChanged?.Invoke(element); } public void MoveToFront() {