Skip to content

Commit

Permalink
#323: ZIndex fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Shapiro committed Jul 17, 2021
1 parent 4aa2d68 commit 9505e52
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 8 deletions.
7 changes: 7 additions & 0 deletions samples/UI/zindex.xmmp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project>
<Panel>
<TextButton Text="Button 3" Left="20" />
<TextButton Text="Button 1" ZIndex="2" />
<TextButton Text="Button 2" Left="10" Top="10" ZIndex="4" />
</Panel>
</Project>
37 changes: 37 additions & 0 deletions src/Myra.Tests/CustomTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Myra.Graphics2D.TextureAtlases;
using Myra.Graphics2D.UI;
using Myra.Graphics2D.UI.Styles;
using Myra.Utility;
using NUnit.Framework;
using System;
using System.Collections.Generic;

namespace Myra.Tests
{
Expand Down Expand Up @@ -49,5 +51,40 @@ public void MyraEnvironmentReset()
Assert.AreNotEqual(oldStylesheet, newStylesheet);
Assert.AreNotEqual(oldTexture, newTexture);
}

[Test]
public void TestZIndexSort()
{
var widgets = new List<Widget>();
var button1 = new TextButton
{
ZIndex = 10
};
var button2 = new TextButton
{
ZIndex = 6
};
var button3 = new TextButton
{
ZIndex = 4
};
var button4 = new TextButton
{
ZIndex = 2
};

widgets.Add(button1);
widgets.Add(button2);
widgets.Add(button3);
widgets.Add(button4);

widgets.SortWidgetsByZIndex();

// The list should become reversed
Assert.AreEqual(widgets[0], button4);
Assert.AreEqual(widgets[1], button3);
Assert.AreEqual(widgets[2], button2);
Assert.AreEqual(widgets[3], button1);
}
}
}
2 changes: 1 addition & 1 deletion src/Myra/Graphics2D/UI/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private void UpdateWidgets()
_childrenCopy.Add(GetChild(i));
}

_childrenCopy.Sort((a, b) => Comparer<int>.Default.Compare(a.ZOrder, b.ZOrder));
_childrenCopy.SortWidgetsByZIndex();

_childrenDirty = false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Myra/Graphics2D/UI/Desktop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +1171,7 @@ private void UpdateWidgetsCopy()
_widgetsCopy.Clear();
_widgetsCopy.AddRange(Widgets);

_widgetsCopy.Sort((a, b) => Comparer<int>.Default.Compare(a.ZOrder, b.ZOrder));
_widgetsCopy.SortWidgetsByZIndex();

_widgetsDirty = false;
}
Expand Down
24 changes: 18 additions & 6 deletions src/Myra/Graphics2D/UI/Widget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ private enum LayoutState
private int _left, _top;
private int? _minWidth, _minHeight, _maxWidth, _maxHeight, _width, _height;
private int _gridColumn, _gridRow, _gridColumnSpan = 1, _gridRowSpan = 1;
private int _zIndex;
private HorizontalAlignment _horizontalAlignment = HorizontalAlignment.Left;
private VerticalAlignment _verticalAlignment = VerticalAlignment.Top;
private LayoutState _layoutState = LayoutState.Invalid;
Expand Down Expand Up @@ -531,15 +532,26 @@ public virtual bool Visible
[DefaultValue(DragDirection.None)]
public virtual DragDirection DragDirection { get; set; } = DragDirection.None;

[XmlIgnore]
[Browsable(false)]
internal bool IsDraggable { get => DragDirection != DragDirection.None; }

[Category("Behavior")]
[DefaultValue(0)]
public int ZOrder { get; set; }

public int ZIndex
{
get => _zIndex;
set
{
if (value == _zIndex)
{
return;
}

[Obsolete("Use DragDirection instead")]
[XmlIgnore]
[Browsable(false)]
public bool IsDraggable => DragDirection != DragDirection.None;
_zIndex = value;
InvalidateMeasure();
}
}

[XmlIgnore]
[Browsable(false)]
Expand Down
28 changes: 28 additions & 0 deletions src/Myra/Utility/UIUtils.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Myra.Graphics2D.UI;
using System;
using System.Collections.Generic;

namespace Myra.Utility
{
Expand Down Expand Up @@ -32,5 +33,32 @@ public static bool ProcessWidgets(this Widget root, Func<Widget, bool> operation

return true;
}

/// <summary>
/// Sorts widgets by ZIndex using bubble sort
/// </summary>
/// <param name="list"></param>
public static void SortWidgetsByZIndex(this List<Widget> list)
{
var n = list.Count;
do
{
var newN = 0;
for (var i = 1; i < n; ++i)
{
if (list[i - 1].ZIndex > list[i].ZIndex)
{
// Swap
var temp = list[i - 1];
list[i - 1] = list[i];
list[i] = temp;

newN = i;
}
}

n = newN;
} while (n > 1);
}
}
}

0 comments on commit 9505e52

Please sign in to comment.