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

optimize the active root

This commit is contained in:
Ellpeck 2019-09-09 17:18:44 +02:00
parent 672e5eb548
commit 42a0ef172b

View file

@ -15,8 +15,9 @@ namespace MLEM.Ui {
private readonly bool isInputOurs; private readonly bool isInputOurs;
private readonly UiSystem system; private readonly UiSystem system;
public RootElement ActiveRoot { get; private set; }
public Element MousedElement { get; private set; } public Element MousedElement { get; private set; }
public Element SelectedElement => this.GetActiveRoot()?.SelectedElement; public Element SelectedElement => this.ActiveRoot.SelectedElement;
public Buttons[] GamepadButtons = {Buttons.A}; public Buttons[] GamepadButtons = {Buttons.A};
public Buttons[] SecondaryGamepadButtons = {Buttons.X}; public Buttons[] SecondaryGamepadButtons = {Buttons.X};
@ -36,6 +37,7 @@ namespace MLEM.Ui {
public void Update() { public void Update() {
if (this.isInputOurs) if (this.isInputOurs)
this.Input.Update(); this.Input.Update();
this.ActiveRoot = this.system.GetRootElements().FirstOrDefault(root => root.CanSelectContent);
// MOUSE INPUT // MOUSE INPUT
var mousedNow = this.GetElementUnderPos(this.Input.MousePosition); var mousedNow = this.GetElementUnderPos(this.Input.MousePosition);
@ -51,7 +53,7 @@ namespace MLEM.Ui {
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.GetActiveRoot().SelectElement(selectedNow); this.ActiveRoot.SelectElement(selectedNow);
if (mousedNow != null) if (mousedNow != null)
mousedNow.OnPressed?.Invoke(mousedNow); mousedNow.OnPressed?.Invoke(mousedNow);
} else if (this.Input.IsMouseButtonPressed(MouseButton.Right)) { } else if (this.Input.IsMouseButtonPressed(MouseButton.Right)) {
@ -79,20 +81,20 @@ namespace MLEM.Ui {
var next = this.GetTabNextElement(backward); var next = this.GetTabNextElement(backward);
if (this.SelectedElement?.Root != null) if (this.SelectedElement?.Root != null)
next = this.SelectedElement.GetTabNextElement(backward, next); next = this.SelectedElement.GetTabNextElement(backward, next);
this.GetActiveRoot().SelectElement(next); this.ActiveRoot.SelectElement(next);
} }
// TOUCH INPUT // TOUCH INPUT
else if (this.Input.GetGesture(GestureType.Tap, out var tap)) { else if (this.Input.GetGesture(GestureType.Tap, out var tap)) {
this.IsAutoNavMode = false; this.IsAutoNavMode = false;
var tapped = this.GetElementUnderPos(tap.Position.ToPoint()); var tapped = this.GetElementUnderPos(tap.Position.ToPoint());
this.GetActiveRoot().SelectElement(tapped); this.ActiveRoot.SelectElement(tapped);
if (tapped != null) if (tapped != null)
tapped.OnPressed?.Invoke(tapped); tapped.OnPressed?.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.GetActiveRoot().SelectElement(held); this.ActiveRoot.SelectElement(held);
if (held != null) if (held != null)
held.OnSecondaryPressed?.Invoke(held); held.OnSecondaryPressed?.Invoke(held);
} }
@ -128,11 +130,10 @@ namespace MLEM.Ui {
} }
private Element GetTabNextElement(bool backward) { private Element GetTabNextElement(bool backward) {
var currRoot = this.GetActiveRoot(); if (this.ActiveRoot == null)
if (currRoot == null)
return null; return null;
var children = currRoot.Element.GetChildren(regardChildrensChildren: true).Append(currRoot.Element); var children = this.ActiveRoot.Element.GetChildren(regardChildrensChildren: true).Append(this.ActiveRoot.Element);
if (this.SelectedElement?.Root != currRoot) { if (this.SelectedElement?.Root != this.ActiveRoot) {
return backward ? children.LastOrDefault(c => c.CanBeSelected) : children.FirstOrDefault(c => c.CanBeSelected); return backward ? children.LastOrDefault(c => c.CanBeSelected) : children.FirstOrDefault(c => c.CanBeSelected);
} else { } else {
var foundCurr = false; var foundCurr = false;
@ -183,15 +184,14 @@ namespace MLEM.Ui {
if (this.SelectedElement != null) if (this.SelectedElement != null)
next = this.SelectedElement.GetGamepadNextElement(dir, next); next = this.SelectedElement.GetGamepadNextElement(dir, next);
if (next != null) if (next != null)
this.GetActiveRoot().SelectElement(next); this.ActiveRoot.SelectElement(next);
} }
private Element GetGamepadNextElement(Rectangle searchArea) { private Element GetGamepadNextElement(Rectangle searchArea) {
var currRoot = this.GetActiveRoot(); if (this.ActiveRoot == null)
if (currRoot == null)
return null; return null;
var children = currRoot.Element.GetChildren(regardChildrensChildren: true).Append(currRoot.Element); var children = this.ActiveRoot.Element.GetChildren(regardChildrensChildren: true).Append(this.ActiveRoot.Element);
if (this.SelectedElement?.Root != currRoot) { if (this.SelectedElement?.Root != this.ActiveRoot) {
return children.FirstOrDefault(c => c.CanBeSelected); return children.FirstOrDefault(c => c.CanBeSelected);
} else { } else {
Element closest = null; Element closest = null;
@ -209,9 +209,5 @@ namespace MLEM.Ui {
} }
} }
public RootElement GetActiveRoot() {
return this.system.GetRootElements().FirstOrDefault(root => root.CanSelectContent);
}
} }
} }