mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-29 15:58:33 +01:00
give access to all of the element callbacks in the ui system
This commit is contained in:
parent
03923f91f6
commit
07b6853e0c
3 changed files with 26 additions and 15 deletions
|
@ -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();
|
||||
|
|
|
@ -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)) {
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in a new issue