mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-12-24 17:29:23 +01:00
improve ui root sorting
This commit is contained in:
parent
a0f0859ba2
commit
ac8b09e74f
2 changed files with 24 additions and 23 deletions
|
@ -180,7 +180,6 @@ namespace MLEM.Ui.Elements {
|
|||
if (this.Root != null)
|
||||
this.Root.OnElementRemoved(e);
|
||||
});
|
||||
this.SetSortedChildrenDirty();
|
||||
this.SetAreaDirty();
|
||||
}
|
||||
|
||||
|
|
|
@ -124,18 +124,10 @@ namespace MLEM.Ui {
|
|||
}
|
||||
|
||||
public RootElement Add(string name, Element element) {
|
||||
if (this.IndexOf(name) >= 0)
|
||||
return null;
|
||||
var root = new RootElement(name, element, this);
|
||||
return !this.Add(root, true) ? null : root;
|
||||
}
|
||||
|
||||
internal bool Add(RootElement root, bool notify, int index = -1) {
|
||||
if (this.IndexOf(root.Name) >= 0)
|
||||
return false;
|
||||
if (index < 0 || index > this.rootElements.Count)
|
||||
index = this.rootElements.Count;
|
||||
this.rootElements.Insert(index, root);
|
||||
if (!notify)
|
||||
return true;
|
||||
this.rootElements.Add(root);
|
||||
root.Element.AndChildren(e => {
|
||||
e.Root = root;
|
||||
e.System = this;
|
||||
|
@ -143,20 +135,15 @@ namespace MLEM.Ui {
|
|||
e.SetAreaDirty();
|
||||
});
|
||||
this.OnRootAdded?.Invoke(root);
|
||||
return true;
|
||||
this.SortRoots();
|
||||
return root;
|
||||
}
|
||||
|
||||
public void Remove(string name) {
|
||||
this.Remove(name, true);
|
||||
}
|
||||
|
||||
internal void Remove(string name, bool notify) {
|
||||
var root = this.Get(name);
|
||||
if (root == null)
|
||||
return;
|
||||
this.rootElements.Remove(root);
|
||||
if (!notify)
|
||||
return;
|
||||
root.SelectElement(null);
|
||||
root.Element.AndChildren(e => {
|
||||
e.Root = null;
|
||||
|
@ -176,6 +163,13 @@ namespace MLEM.Ui {
|
|||
return this.rootElements.FindIndex(element => element.Name == name);
|
||||
}
|
||||
|
||||
internal void SortRoots() {
|
||||
// Normal list sorting isn't stable, but ordering is
|
||||
var sorted = this.rootElements.OrderBy(root => root.Priority).ToArray();
|
||||
this.rootElements.Clear();
|
||||
this.rootElements.AddRange(sorted);
|
||||
}
|
||||
|
||||
public IEnumerable<RootElement> GetRootElements() {
|
||||
for (var i = this.rootElements.Count - 1; i >= 0; i--)
|
||||
yield return this.rootElements[i];
|
||||
|
@ -205,6 +199,14 @@ namespace MLEM.Ui {
|
|||
this.Element.ForceUpdateArea();
|
||||
}
|
||||
}
|
||||
private int priority;
|
||||
public int Priority {
|
||||
get => this.priority;
|
||||
set {
|
||||
this.priority = value;
|
||||
this.System.SortRoots();
|
||||
}
|
||||
}
|
||||
public float ActualScale => this.System.GlobalScale * this.Scale;
|
||||
|
||||
public Matrix Transform = Matrix.Identity;
|
||||
|
@ -243,14 +245,14 @@ namespace MLEM.Ui {
|
|||
this.System.OnSelectedElementChanged?.Invoke(element);
|
||||
}
|
||||
|
||||
[Obsolete("Use the Priority property for greater control")]
|
||||
public void MoveToFront() {
|
||||
this.System.Remove(this.Name, false);
|
||||
this.System.Add(this, false);
|
||||
this.Priority = 10000;
|
||||
}
|
||||
|
||||
[Obsolete("Use the Priority property for greater control")]
|
||||
public void MoveToBack() {
|
||||
this.System.Remove(this.Name, false);
|
||||
this.System.Add(this, false, 0);
|
||||
this.Priority = -10000;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue