1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-26 06:28:35 +01:00

give access to all of the element callbacks in the ui system

This commit is contained in:
Ellpeck 2019-09-13 13:57:25 +02:00
parent 03923f91f6
commit 07b6853e0c
3 changed files with 26 additions and 15 deletions

View file

@ -298,7 +298,7 @@ namespace MLEM.Ui.Elements {
} }
this.area = new Rectangle(pos, actualSize); this.area = new Rectangle(pos, actualSize);
this.OnAreaUpdated?.Invoke(this); this.System.OnElementAreaUpdated?.Invoke(this);
foreach (var child in this.Children) foreach (var child in this.Children)
child.ForceUpdateArea(); child.ForceUpdateArea();

View file

@ -48,11 +48,11 @@ namespace MLEM.Ui {
var mousedNow = this.GetElementUnderPos(this.Input.MousePosition); var mousedNow = this.GetElementUnderPos(this.Input.MousePosition);
if (mousedNow != this.MousedElement) { if (mousedNow != this.MousedElement) {
if (this.MousedElement != null) if (this.MousedElement != null)
this.MousedElement.OnMouseExit?.Invoke(this.MousedElement); this.System.OnElementMouseExit?.Invoke(this.MousedElement);
if (mousedNow != null) if (mousedNow != null)
mousedNow.OnMouseEnter?.Invoke(mousedNow); this.System.OnElementMouseEnter?.Invoke(mousedNow);
this.MousedElement = mousedNow; this.MousedElement = mousedNow;
this.System.ApplyToAll(e => e.OnMousedElementChanged?.Invoke(e, mousedNow)); this.System.OnMousedElementChanged?.Invoke(mousedNow);
} }
if (this.Input.IsMouseButtonPressed(MouseButton.Left)) { if (this.Input.IsMouseButtonPressed(MouseButton.Left)) {
@ -60,11 +60,11 @@ namespace MLEM.Ui {
var selectedNow = mousedNow != null && mousedNow.CanBeSelected ? mousedNow : null; var selectedNow = mousedNow != null && mousedNow.CanBeSelected ? mousedNow : null;
this.ActiveRoot.SelectElement(selectedNow); this.ActiveRoot.SelectElement(selectedNow);
if (mousedNow != null) if (mousedNow != null)
mousedNow.OnPressed?.Invoke(mousedNow); this.System.OnElementPressed?.Invoke(mousedNow);
} else if (this.Input.IsMouseButtonPressed(MouseButton.Right)) { } else if (this.Input.IsMouseButtonPressed(MouseButton.Right)) {
this.IsAutoNavMode = false; this.IsAutoNavMode = false;
if (mousedNow != null) if (mousedNow != null)
mousedNow.OnSecondaryPressed?.Invoke(mousedNow); this.System.OnElementSecondaryPressed?.Invoke(mousedNow);
} }
// KEYBOARD INPUT // KEYBOARD INPUT
@ -73,10 +73,10 @@ namespace MLEM.Ui {
if (this.SelectedElement?.Root != null) { if (this.SelectedElement?.Root != null) {
if (this.Input.IsModifierKeyDown(ModifierKey.Shift)) { if (this.Input.IsModifierKeyDown(ModifierKey.Shift)) {
// secondary action on element using space or enter // secondary action on element using space or enter
this.SelectedElement.OnSecondaryPressed?.Invoke(this.SelectedElement); this.System.OnElementSecondaryPressed?.Invoke(this.SelectedElement);
} else { } else {
// first action on element using space or enter // 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)) { } else if (this.Input.IsKeyPressed(Keys.Tab)) {
@ -95,24 +95,24 @@ namespace MLEM.Ui {
var tapped = this.GetElementUnderPos(tap.Position.ToPoint()); var tapped = this.GetElementUnderPos(tap.Position.ToPoint());
this.ActiveRoot.SelectElement(tapped); this.ActiveRoot.SelectElement(tapped);
if (tapped != null) if (tapped != null)
tapped.OnPressed?.Invoke(tapped); this.System.OnElementPressed?.Invoke(tapped);
} else if (this.Input.GetGesture(GestureType.Hold, out var hold)) { } else if (this.Input.GetGesture(GestureType.Hold, out var hold)) {
this.IsAutoNavMode = false; this.IsAutoNavMode = false;
var held = this.GetElementUnderPos(hold.Position.ToPoint()); var held = this.GetElementUnderPos(hold.Position.ToPoint());
this.ActiveRoot.SelectElement(held); this.ActiveRoot.SelectElement(held);
if (held != null) if (held != null)
held.OnSecondaryPressed?.Invoke(held); this.System.OnElementSecondaryPressed?.Invoke(held);
} }
// GAMEPAD INPUT // GAMEPAD INPUT
else if (this.GamepadButtons.Any(b => this.IsGamepadPressed(b))) { else if (this.GamepadButtons.Any(b => this.IsGamepadPressed(b))) {
this.IsAutoNavMode = true; this.IsAutoNavMode = true;
if (this.SelectedElement?.Root != null) 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))) { } else if (this.SecondaryGamepadButtons.Any(b => this.IsGamepadPressed(b))) {
this.IsAutoNavMode = true; this.IsAutoNavMode = true;
if (this.SelectedElement?.Root != null) if (this.SelectedElement?.Root != null)
this.SelectedElement.OnSecondaryPressed?.Invoke(this.SelectedElement); this.System.OnElementSecondaryPressed?.Invoke(this.SelectedElement);
} else if (this.DownButtons.Any(this.IsGamepadPressed)) { } else if (this.DownButtons.Any(this.IsGamepadPressed)) {
this.HandleGamepadNextElement(Direction2.Down); this.HandleGamepadNextElement(Direction2.Down);
} else if (this.LeftButtons.Any(this.IsGamepadPressed)) { } else if (this.LeftButtons.Any(this.IsGamepadPressed)) {

View file

@ -54,6 +54,15 @@ namespace MLEM.Ui {
public Element.DrawCallback OnElementDrawn; public Element.DrawCallback OnElementDrawn;
public Element.DrawCallback OnSelectedElementDrawn; 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) { public UiSystem(GameWindow window, GraphicsDevice device, UiStyle style, InputHandler inputHandler = null) {
this.Controls = new UiControls(this, inputHandler); this.Controls = new UiControls(this, inputHandler);
@ -74,6 +83,8 @@ namespace MLEM.Ui {
root.Element.AndChildren(e => e.OnTextInput?.Invoke(e, key, character)); 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) => { this.OnSelectedElementDrawn = (element, time, batch, alpha) => {
if (this.Controls.IsAutoNavMode && element.SelectionIndicator != null) { if (this.Controls.IsAutoNavMode && element.SelectionIndicator != null) {
batch.Draw(element.SelectionIndicator, element.DisplayArea, Color.White * alpha, element.Scale / 2); batch.Draw(element.SelectionIndicator, element.DisplayArea, Color.White * alpha, element.Scale / 2);
@ -190,11 +201,11 @@ namespace MLEM.Ui {
return; return;
if (this.SelectedElement != null) if (this.SelectedElement != null)
this.SelectedElement.OnDeselected?.Invoke(this.SelectedElement); this.System.OnElementDeselected?.Invoke(this.SelectedElement);
if (element != null) if (element != null)
element.OnSelected?.Invoke(element); this.System.OnElementSelected?.Invoke(element);
this.SelectedElement = element; this.SelectedElement = element;
this.System.ApplyToAll(e => e.OnSelectedElementChanged?.Invoke(e, element)); this.System.OnSelectedElementChanged?.Invoke(element);
} }
public void MoveToFront() { public void MoveToFront() {