1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 20:58:34 +01:00

made a bunch of stuff in UiControls virtual so that changing more complex aspects is easier

This commit is contained in:
Ellpeck 2019-09-11 18:44:05 +02:00
parent b8bf5c28d9
commit 3796cc5ef0

View file

@ -12,8 +12,8 @@ namespace MLEM.Ui {
public class UiControls { public class UiControls {
public readonly InputHandler Input; public readonly InputHandler Input;
private readonly bool isInputOurs; protected readonly bool IsInputOurs;
private readonly UiSystem system; protected readonly UiSystem System;
public RootElement ActiveRoot { get; private set; } public RootElement ActiveRoot { get; private set; }
public Element MousedElement { get; private set; } public Element MousedElement { get; private set; }
@ -31,18 +31,18 @@ namespace MLEM.Ui {
public bool IsAutoNavMode { get; private set; } public bool IsAutoNavMode { get; private set; }
public UiControls(UiSystem system, InputHandler inputHandler = null) { public UiControls(UiSystem system, InputHandler inputHandler = null) {
this.system = system; this.System = system;
this.Input = inputHandler ?? new InputHandler(); this.Input = inputHandler ?? new InputHandler();
this.isInputOurs = inputHandler == null; this.IsInputOurs = inputHandler == null;
// enable all required gestures // enable all required gestures
InputHandler.EnableGestures(GestureType.Tap, GestureType.Hold); InputHandler.EnableGestures(GestureType.Tap, GestureType.Hold);
} }
public void Update() { public virtual void Update() {
if (this.isInputOurs) if (this.IsInputOurs)
this.Input.Update(); this.Input.Update();
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); var mousedNow = this.GetElementUnderPos(this.Input.MousePosition);
@ -52,7 +52,7 @@ namespace MLEM.Ui {
if (mousedNow != null) if (mousedNow != null)
mousedNow.OnMouseEnter?.Invoke(mousedNow); mousedNow.OnMouseEnter?.Invoke(mousedNow);
this.MousedElement = mousedNow; this.MousedElement = mousedNow;
this.system.ApplyToAll(e => e.OnMousedElementChanged?.Invoke(e, mousedNow)); this.System.ApplyToAll(e => e.OnMousedElementChanged?.Invoke(e, mousedNow));
} }
if (this.Input.IsMouseButtonPressed(MouseButton.Left)) { if (this.Input.IsMouseButtonPressed(MouseButton.Left)) {
@ -124,8 +124,8 @@ namespace MLEM.Ui {
} }
} }
public Element GetElementUnderPos(Point position, bool transform = true) { public virtual Element GetElementUnderPos(Point position, bool transform = true) {
foreach (var root in this.system.GetRootElements()) { foreach (var root in this.System.GetRootElements()) {
var pos = transform ? position.Transform(root.InvTransform) : position; var pos = transform ? position.Transform(root.InvTransform) : position;
var moused = root.Element.GetElementUnderPos(pos); var moused = root.Element.GetElementUnderPos(pos);
if (moused != null) if (moused != null)
@ -134,7 +134,7 @@ namespace MLEM.Ui {
return null; return null;
} }
private Element GetTabNextElement(bool backward) { protected virtual Element GetTabNextElement(bool backward) {
if (this.ActiveRoot == null) if (this.ActiveRoot == null)
return null; return null;
var children = this.ActiveRoot.Element.GetChildren(regardChildrensChildren: true).Append(this.ActiveRoot.Element); var children = this.ActiveRoot.Element.GetChildren(regardChildrensChildren: true).Append(this.ActiveRoot.Element);
@ -162,37 +162,7 @@ namespace MLEM.Ui {
} }
} }
private void HandleGamepadNextElement(Direction2 dir) { protected virtual Element GetGamepadNextElement(Rectangle searchArea) {
this.IsAutoNavMode = true;
Rectangle searchArea = default;
if (this.SelectedElement?.Root != null) {
searchArea = this.SelectedElement.Area;
var (_, _, width, height) = this.system.Viewport;
switch (dir) {
case Direction2.Down:
searchArea.Height += height;
break;
case Direction2.Left:
searchArea.X -= width;
searchArea.Width += width;
break;
case Direction2.Right:
searchArea.Width += width;
break;
case Direction2.Up:
searchArea.Y -= height;
searchArea.Height += height;
break;
}
}
var next = this.GetGamepadNextElement(searchArea);
if (this.SelectedElement != null)
next = this.SelectedElement.GetGamepadNextElement(dir, next);
if (next != null)
this.ActiveRoot.SelectElement(next);
}
private Element GetGamepadNextElement(Rectangle searchArea) {
if (this.ActiveRoot == null) if (this.ActiveRoot == null)
return null; return null;
var children = this.ActiveRoot.Element.GetChildren(regardChildrensChildren: true).Append(this.ActiveRoot.Element); var children = this.ActiveRoot.Element.GetChildren(regardChildrensChildren: true).Append(this.ActiveRoot.Element);
@ -218,5 +188,35 @@ namespace MLEM.Ui {
return this.Input.IsPressed(button, this.GamepadIndex); return this.Input.IsPressed(button, this.GamepadIndex);
} }
private void HandleGamepadNextElement(Direction2 dir) {
this.IsAutoNavMode = true;
Rectangle searchArea = default;
if (this.SelectedElement?.Root != null) {
searchArea = this.SelectedElement.Area;
var (_, _, width, height) = this.System.Viewport;
switch (dir) {
case Direction2.Down:
searchArea.Height += height;
break;
case Direction2.Left:
searchArea.X -= width;
searchArea.Width += width;
break;
case Direction2.Right:
searchArea.Width += width;
break;
case Direction2.Up:
searchArea.Y -= height;
searchArea.Height += height;
break;
}
}
var next = this.GetGamepadNextElement(searchArea);
if (this.SelectedElement != null)
next = this.SelectedElement.GetGamepadNextElement(dir, next);
if (next != null)
this.ActiveRoot.SelectElement(next);
}
} }
} }