1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-06-15 18:24:31 +02:00

added a way to tell ui controls to ignore certain input types

This commit is contained in:
Ellpeck 2019-12-04 01:17:16 +01:00
parent d224d57cf2
commit f1702ee5b2

View file

@ -27,6 +27,10 @@ namespace MLEM.Ui {
public object[] LeftButtons = {Buttons.DPadLeft, Buttons.LeftThumbstickLeft}; public object[] LeftButtons = {Buttons.DPadLeft, Buttons.LeftThumbstickLeft};
public object[] RightButtons = {Buttons.DPadRight, Buttons.LeftThumbstickRight}; public object[] RightButtons = {Buttons.DPadRight, Buttons.LeftThumbstickRight};
public int GamepadIndex = -1; public int GamepadIndex = -1;
public bool HandleMouse = true;
public bool HandleKeyboard = true;
public bool HandleTouch = true;
public bool HandleGamepad = true;
public bool IsAutoNavMode { get; private set; } public bool IsAutoNavMode { get; private set; }
@ -45,82 +49,90 @@ namespace MLEM.Ui {
this.ActiveRoot = this.System.GetRootElements().FirstOrDefault(root => root.CanSelectContent); this.ActiveRoot = this.System.GetRootElements().FirstOrDefault(root => root.CanSelectContent);
// MOUSE INPUT // MOUSE INPUT
var mousedNow = this.GetElementUnderPos(this.Input.MousePosition.ToVector2()); if (this.HandleMouse) {
if (mousedNow != this.MousedElement) { var mousedNow = this.GetElementUnderPos(this.Input.MousePosition.ToVector2());
if (this.MousedElement != null) if (mousedNow != this.MousedElement) {
this.System.OnElementMouseExit?.Invoke(this.MousedElement); if (this.MousedElement != null)
if (mousedNow != null) this.System.OnElementMouseExit?.Invoke(this.MousedElement);
this.System.OnElementMouseEnter?.Invoke(mousedNow); if (mousedNow != null)
this.MousedElement = mousedNow; this.System.OnElementMouseEnter?.Invoke(mousedNow);
this.System.OnMousedElementChanged?.Invoke(mousedNow); this.MousedElement = mousedNow;
} this.System.OnMousedElementChanged?.Invoke(mousedNow);
}
if (this.Input.IsMouseButtonPressed(MouseButton.Left)) { if (this.Input.IsMouseButtonPressed(MouseButton.Left)) {
this.IsAutoNavMode = false; this.IsAutoNavMode = false;
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)
this.System.OnElementPressed?.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)
this.System.OnElementSecondaryPressed?.Invoke(mousedNow); this.System.OnElementSecondaryPressed?.Invoke(mousedNow);
}
} }
// KEYBOARD INPUT // KEYBOARD INPUT
else if (this.KeyboardButtons.Any(this.Input.IsKeyPressed)) { if (this.HandleKeyboard) {
this.IsAutoNavMode = true; if (this.KeyboardButtons.Any(this.Input.IsKeyPressed)) {
if (this.SelectedElement?.Root != null) { this.IsAutoNavMode = true;
if (this.Input.IsModifierKeyDown(ModifierKey.Shift)) { if (this.SelectedElement?.Root != null) {
// secondary action on element using space or enter if (this.Input.IsModifierKeyDown(ModifierKey.Shift)) {
this.System.OnElementSecondaryPressed?.Invoke(this.SelectedElement); // secondary action on element using space or enter
} else { this.System.OnElementSecondaryPressed?.Invoke(this.SelectedElement);
// first action on element using space or enter } else {
this.System.OnElementPressed?.Invoke(this.SelectedElement); // first action on element using space or enter
this.System.OnElementPressed?.Invoke(this.SelectedElement);
}
} }
} else if (this.Input.IsKeyPressed(Keys.Tab)) {
this.IsAutoNavMode = true;
// tab or shift-tab to next or previous element
var backward = this.Input.IsModifierKeyDown(ModifierKey.Shift);
var next = this.GetTabNextElement(backward);
if (this.SelectedElement?.Root != null)
next = this.SelectedElement.GetTabNextElement(backward, next);
this.ActiveRoot?.SelectElement(next);
} }
} else if (this.Input.IsKeyPressed(Keys.Tab)) {
this.IsAutoNavMode = true;
// tab or shift-tab to next or previous element
var backward = this.Input.IsModifierKeyDown(ModifierKey.Shift);
var next = this.GetTabNextElement(backward);
if (this.SelectedElement?.Root != null)
next = this.SelectedElement.GetTabNextElement(backward, next);
this.ActiveRoot?.SelectElement(next);
} }
// TOUCH INPUT // TOUCH INPUT
else if (this.Input.GetGesture(GestureType.Tap, out var tap)) { if (this.HandleTouch) {
this.IsAutoNavMode = false; if (this.Input.GetGesture(GestureType.Tap, out var tap)) {
var tapped = this.GetElementUnderPos(tap.Position); this.IsAutoNavMode = false;
this.ActiveRoot?.SelectElement(tapped); var tapped = this.GetElementUnderPos(tap.Position);
if (tapped != null) this.ActiveRoot?.SelectElement(tapped);
this.System.OnElementPressed?.Invoke(tapped); if (tapped != null)
} else if (this.Input.GetGesture(GestureType.Hold, out var hold)) { this.System.OnElementPressed?.Invoke(tapped);
this.IsAutoNavMode = false; } else if (this.Input.GetGesture(GestureType.Hold, out var hold)) {
var held = this.GetElementUnderPos(hold.Position); this.IsAutoNavMode = false;
this.ActiveRoot?.SelectElement(held); var held = this.GetElementUnderPos(hold.Position);
if (held != null) this.ActiveRoot?.SelectElement(held);
this.System.OnElementSecondaryPressed?.Invoke(held); if (held != null)
this.System.OnElementSecondaryPressed?.Invoke(held);
}
} }
// GAMEPAD INPUT // GAMEPAD INPUT
else if (this.GamepadButtons.Any(b => this.IsGamepadPressed(b))) { if (this.HandleGamepad) {
this.IsAutoNavMode = true; if (this.GamepadButtons.Any(b => this.IsGamepadPressed(b))) {
if (this.SelectedElement?.Root != null) this.IsAutoNavMode = true;
this.System.OnElementPressed?.Invoke(this.SelectedElement); if (this.SelectedElement?.Root != null)
} else if (this.SecondaryGamepadButtons.Any(b => this.IsGamepadPressed(b))) { this.System.OnElementPressed?.Invoke(this.SelectedElement);
this.IsAutoNavMode = true; } else if (this.SecondaryGamepadButtons.Any(b => this.IsGamepadPressed(b))) {
if (this.SelectedElement?.Root != null) this.IsAutoNavMode = true;
this.System.OnElementSecondaryPressed?.Invoke(this.SelectedElement); if (this.SelectedElement?.Root != null)
} else if (this.DownButtons.Any(this.IsGamepadPressed)) { this.System.OnElementSecondaryPressed?.Invoke(this.SelectedElement);
this.HandleGamepadNextElement(Direction2.Down); } else if (this.DownButtons.Any(this.IsGamepadPressed)) {
} else if (this.LeftButtons.Any(this.IsGamepadPressed)) { this.HandleGamepadNextElement(Direction2.Down);
this.HandleGamepadNextElement(Direction2.Left); } else if (this.LeftButtons.Any(this.IsGamepadPressed)) {
} else if (this.RightButtons.Any(this.IsGamepadPressed)) { this.HandleGamepadNextElement(Direction2.Left);
this.HandleGamepadNextElement(Direction2.Right); } else if (this.RightButtons.Any(this.IsGamepadPressed)) {
} else if (this.UpButtons.Any(this.IsGamepadPressed)) { this.HandleGamepadNextElement(Direction2.Right);
this.HandleGamepadNextElement(Direction2.Up); } else if (this.UpButtons.Any(this.IsGamepadPressed)) {
this.HandleGamepadNextElement(Direction2.Up);
}
} }
} }