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

better implementation of the touch mousing from yesterday

This commit is contained in:
Ellpeck 2020-06-04 20:52:21 +02:00
parent 81ed3d3bee
commit 6b1e5f8dd9
3 changed files with 79 additions and 11 deletions

View file

@ -259,7 +259,7 @@ namespace MLEM.Ui.Elements {
public float DrawAlpha = 1; public float DrawAlpha = 1;
/// <summary> /// <summary>
/// Stores whether this element is currently being moused over. /// Stores whether this element is currently being moused over or touched.
/// </summary> /// </summary>
public bool IsMouseOver { get; protected set; } public bool IsMouseOver { get; protected set; }
/// <summary> /// <summary>
@ -300,6 +300,14 @@ namespace MLEM.Ui.Elements {
/// </summary> /// </summary>
public GenericCallback OnMouseExit; public GenericCallback OnMouseExit;
/// <summary> /// <summary>
/// Event that is called when this element starts being touched
/// </summary>
public GenericCallback OnTouchEnter;
/// <summary>
/// Event that is called when this element stops being touched
/// </summary>
public GenericCallback OnTouchExit;
/// <summary>
/// Event that is called when text input is made. /// Event that is called when text input is made.
/// Note that this event is called for every element, even if it is not selected. /// Note that this event is called for every element, even if it is not selected.
/// Also note that if <see cref="TextInputWrapper.RequiresOnScreenKeyboard"/> is true, this event is never called. /// Also note that if <see cref="TextInputWrapper.RequiresOnScreenKeyboard"/> is true, this event is never called.
@ -315,6 +323,11 @@ namespace MLEM.Ui.Elements {
/// </summary> /// </summary>
public OtherElementCallback OnMousedElementChanged; public OtherElementCallback OnMousedElementChanged;
/// <summary> /// <summary>
/// Event that is called when the element that is currently being touched changes within the ui system.
/// Note that the event fired doesn't necessarily correlate to this specific element.
/// </summary>
public OtherElementCallback OnTouchedElementChanged;
/// <summary>
/// Event that is called when the element that is currently selected changes within the ui system. /// Event that is called when the element that is currently selected changes within the ui system.
/// Note that the event fired doesn't necessarily correlate to this specific element. /// Note that the event fired doesn't necessarily correlate to this specific element.
/// </summary> /// </summary>
@ -362,6 +375,8 @@ namespace MLEM.Ui.Elements {
this.OnMouseEnter += element => this.IsMouseOver = true; this.OnMouseEnter += element => this.IsMouseOver = true;
this.OnMouseExit += element => this.IsMouseOver = false; this.OnMouseExit += element => this.IsMouseOver = false;
this.OnTouchEnter += element => this.IsMouseOver = true;
this.OnTouchExit += element => this.IsMouseOver = false;
this.OnSelected += element => this.IsSelected = true; this.OnSelected += element => this.IsSelected = true;
this.OnDeselected += element => this.IsSelected = false; this.OnDeselected += element => this.IsSelected = false;
this.GetTabNextElement += (backward, next) => next; this.GetTabNextElement += (backward, next) => next;

View file

@ -35,11 +35,15 @@ namespace MLEM.Ui {
/// The <see cref="RootElement"/> that is currently active. /// The <see cref="RootElement"/> that is currently active.
/// The active root element is the one with the highest <see cref="RootElement.Priority"/> that whose <see cref="RootElement.CanSelectContent"/> property is true. /// The active root element is the one with the highest <see cref="RootElement.Priority"/> that whose <see cref="RootElement.CanSelectContent"/> property is true.
/// </summary> /// </summary>
public RootElement ActiveRoot { get; private set; } public RootElement ActiveRoot { get; protected set; }
/// <summary> /// <summary>
/// The <see cref="Element"/> that the mouse is currently over. /// The <see cref="Element"/> that the mouse is currently over.
/// </summary> /// </summary>
public Element MousedElement { get; private set; } public Element MousedElement { get; protected set; }
/// <summary>
/// The <see cref="Element"/> that is currently touched.
/// </summary>
public Element TouchedElement { get; protected set; }
private readonly Dictionary<string, Element> selectedElements = new Dictionary<string, Element>(); private readonly Dictionary<string, Element> selectedElements = new Dictionary<string, Element>();
/// <summary> /// <summary>
/// The element that is currently selected. /// The element that is currently selected.
@ -141,14 +145,7 @@ namespace MLEM.Ui {
// MOUSE INPUT // MOUSE INPUT
if (this.HandleMouse) { if (this.HandleMouse) {
var mousedNow = this.GetElementUnderPos(this.Input.MousePosition.ToVector2()); var mousedNow = this.GetElementUnderPos(this.Input.MousePosition.ToVector2());
if (mousedNow != this.MousedElement) { this.SetMousedElement(mousedNow);
if (this.MousedElement != null)
this.System.OnElementMouseExit?.Invoke(this.MousedElement);
if (mousedNow != null)
this.System.OnElementMouseEnter?.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;
@ -200,6 +197,19 @@ namespace MLEM.Ui {
this.SelectElement(this.ActiveRoot, held); this.SelectElement(this.ActiveRoot, held);
if (held != null && held.CanBePressed) if (held != null && held.CanBePressed)
this.System.OnElementSecondaryPressed?.Invoke(held); this.System.OnElementSecondaryPressed?.Invoke(held);
} else if (this.Input.TouchState.Count <= 0) {
this.SetTouchedElement(null);
} else {
foreach (var location in this.Input.TouchState) {
var element = this.GetElementUnderPos(location.Position);
if (location.State == TouchLocationState.Pressed) {
// start touching an element if we just touched down on it
this.SetTouchedElement(element);
} else if (element != this.TouchedElement) {
// if we moved off of the touched element, we stop touching
this.SetTouchedElement(null);
}
}
} }
} }
@ -269,6 +279,36 @@ namespace MLEM.Ui {
this.IsAutoNavMode = autoNav.Value; this.IsAutoNavMode = autoNav.Value;
} }
/// <summary>
/// Sets the <see cref="MousedElement"/> to the given value, calling the appropriate events.
/// </summary>
/// <param name="element">The element to set as moused</param>
public void SetMousedElement(Element element) {
if (element != this.MousedElement) {
if (this.MousedElement != null)
this.System.OnElementMouseExit?.Invoke(this.MousedElement);
if (element != null)
this.System.OnElementMouseEnter?.Invoke(element);
this.MousedElement = element;
this.System.OnMousedElementChanged?.Invoke(element);
}
}
/// <summary>
/// Sets the <see cref="TouchedElement"/> to the given value, calling the appropriate events.
/// </summary>
/// <param name="element">The element to set as touched</param>
public void SetTouchedElement(Element element) {
if (element != this.TouchedElement) {
if (this.TouchedElement != null)
this.System.OnElementTouchExit?.Invoke(this.TouchedElement);
if (element != null)
this.System.OnElementTouchEnter?.Invoke(element);
this.TouchedElement = element;
this.System.OnTouchedElementChanged?.Invoke(element);
}
}
/// <summary> /// <summary>
/// Returns the selected element for the given root element. /// Returns the selected element for the given root element.
/// A property equivalent to this method is <see cref="RootElement.SelectedElement"/>. /// A property equivalent to this method is <see cref="RootElement.SelectedElement"/>.

View file

@ -141,6 +141,14 @@ namespace MLEM.Ui {
/// </summary> /// </summary>
public Element.GenericCallback OnElementMouseExit = e => e.OnMouseExit?.Invoke(e); public Element.GenericCallback OnElementMouseExit = e => e.OnMouseExit?.Invoke(e);
/// <summary> /// <summary>
/// Event that is invoked when an <see cref="Element"/> starts being touched
/// </summary>
public Element.GenericCallback OnElementTouchEnter = e => e.OnTouchEnter?.Invoke(e);
/// <summary>
/// Event that is invoked when an <see cref="Element"/> stops being touched
/// </summary>
public Element.GenericCallback OnElementTouchExit = e => e.OnTouchExit?.Invoke(e);
/// <summary>
/// Event that is invoked when an <see cref="Element"/>'s display area changes /// Event that is invoked when an <see cref="Element"/>'s display area changes
/// </summary> /// </summary>
public Element.GenericCallback OnElementAreaUpdated = e => e.OnAreaUpdated?.Invoke(e); public Element.GenericCallback OnElementAreaUpdated = e => e.OnAreaUpdated?.Invoke(e);
@ -149,6 +157,10 @@ namespace MLEM.Ui {
/// </summary> /// </summary>
public Element.GenericCallback OnMousedElementChanged; public Element.GenericCallback OnMousedElementChanged;
/// <summary> /// <summary>
/// Event that is invoked when the <see cref="Element"/> that is being touched changes
/// </summary>
public Element.GenericCallback OnTouchedElementChanged;
/// <summary>
/// Event that is invoked when the selected <see cref="Element"/> changes, either through automatic navigation, or by pressing on an element with the mouse /// Event that is invoked when the selected <see cref="Element"/> changes, either through automatic navigation, or by pressing on an element with the mouse
/// </summary> /// </summary>
public Element.GenericCallback OnSelectedElementChanged; public Element.GenericCallback OnSelectedElementChanged;
@ -184,6 +196,7 @@ namespace MLEM.Ui {
TextInputWrapper.Current.AddListener(window, (sender, key, character) => this.ApplyToAll(e => e.OnTextInput?.Invoke(e, key, character))); TextInputWrapper.Current.AddListener(window, (sender, key, character) => this.ApplyToAll(e => e.OnTextInput?.Invoke(e, key, character)));
this.OnMousedElementChanged = e => this.ApplyToAll(t => t.OnMousedElementChanged?.Invoke(t, e)); this.OnMousedElementChanged = e => this.ApplyToAll(t => t.OnMousedElementChanged?.Invoke(t, e));
this.OnTouchedElementChanged = e => this.ApplyToAll(t => t.OnTouchedElementChanged?.Invoke(t, e));
this.OnSelectedElementChanged = e => this.ApplyToAll(t => t.OnSelectedElementChanged?.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.HasValue()) { if (this.Controls.IsAutoNavMode && element.SelectionIndicator.HasValue()) {