1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 04:53:29 +01:00

added some useful querying functions

This commit is contained in:
Ellpeck 2019-08-18 18:32:34 +02:00
parent 91959d6c6e
commit ef677ae441
2 changed files with 39 additions and 2 deletions

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Coroutine;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
@ -132,6 +133,19 @@ namespace Demos {
var slider = new Slider(Anchor.AutoLeft, new Vector2(1, 10), 5, 1);
root.AddChild(new Paragraph(Anchor.AutoLeft, 1, paragraph => "Slider is at " + (slider.CurrentValue * 100).Floor() + "%") {PositionOffset = new Vector2(0, 1)});
root.AddChild(slider);
// Below are some querying examples that help you find certain elements easily
var textFields = root.GetChildren<TextField>();
Console.WriteLine($"The root has {textFields.Count()} text fields");
var paragraphs = root.GetChildren<Paragraph>();
var totalParagraphs = root.GetChildren<Paragraph>(regardChildrensChildren: true);
Console.WriteLine($"The root has {paragraphs.Count()} paragraphs, but there are {totalParagraphs.Count()} when regarding children's children");
var autoWidthChildren = root.GetChildren(e => e.Size.X == 1);
var autoWidthButtons = root.GetChildren<Button>(e => e.Size.X == 1);
Console.WriteLine($"The root has {autoWidthChildren.Count()} auto-width children, {autoWidthButtons.Count()} of which are buttons");
}
protected override void DoDraw(GameTime gameTime) {

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
@ -329,7 +330,7 @@ namespace MLEM.Ui.Elements {
return this.Area;
}
protected Element GetPreviousChild(bool hiddenAlso) {
public Element GetPreviousChild(bool hiddenAlso) {
if (this.Parent == null)
return null;
@ -344,7 +345,7 @@ namespace MLEM.Ui.Elements {
return lastChild;
}
protected IEnumerable<Element> GetSiblings(bool hiddenAlso) {
public IEnumerable<Element> GetSiblings(bool hiddenAlso) {
if (this.Parent == null)
yield break;
foreach (var child in this.Parent.Children) {
@ -355,6 +356,28 @@ namespace MLEM.Ui.Elements {
}
}
public IEnumerable<Element> GetChildren(Func<Element, bool> condition = null, bool regardChildrensChildren = false) {
foreach (var child in this.Children) {
if (condition == null || condition(child))
yield return child;
if (regardChildrensChildren) {
foreach (var cc in child.GetChildren(condition, true))
yield return cc;
}
}
}
public IEnumerable<T> GetChildren<T>(Func<T, bool> condition = null, bool regardChildrensChildren = false) where T : Element {
foreach (var child in this.Children) {
if (child is T t && (condition == null || condition(t)))
yield return t;
if (regardChildrensChildren) {
foreach (var cc in child.GetChildren(condition, true))
yield return cc;
}
}
}
public virtual void Update(GameTime time) {
foreach (var child in this.SortedChildren)
child.Update(time);