diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs index 86e8615..49ac891 100644 --- a/MLEM.Ui/Elements/Element.cs +++ b/MLEM.Ui/Elements/Element.cs @@ -90,7 +90,6 @@ namespace MLEM.Ui.Elements { /// The size of this element, where X represents the width and Y represents the height. /// If the x or y value of the size is between 0 and 1, the size will be seen as a percentage of its parent's size rather than as an absolute value. /// If the x (or y) value of the size is negative, the width (or height) is seen as a percentage of the element's resulting height (or width). - /// If is true, this property's X value is ignored and overridden. If is true, this property's Y value is ignored and overridden. /// /// /// The following example combines both types of percentage-based sizing. diff --git a/MLEM.Ui/Elements/Panel.cs b/MLEM.Ui/Elements/Panel.cs index 101444f..8c1e27e 100644 --- a/MLEM.Ui/Elements/Panel.cs +++ b/MLEM.Ui/Elements/Panel.cs @@ -227,11 +227,13 @@ namespace MLEM.Ui.Elements { var targetArea = (Rectangle) this.GetRenderTargetArea(); if (targetArea.Width <= 0 || targetArea.Height <= 0) return; + #if !TEST if (this.renderTarget == null || targetArea.Width != this.renderTarget.Width || targetArea.Height != this.renderTarget.Height) { if (this.renderTarget != null) this.renderTarget.Dispose(); this.renderTarget = targetArea.IsEmpty ? null : new RenderTarget2D(this.System.Game.GraphicsDevice, targetArea.Width, targetArea.Height); } + #endif } } diff --git a/MLEM.Ui/Elements/TextField.cs b/MLEM.Ui/Elements/TextField.cs index 279a41b..de0c099 100644 --- a/MLEM.Ui/Elements/TextField.cs +++ b/MLEM.Ui/Elements/TextField.cs @@ -144,12 +144,14 @@ namespace MLEM.Ui.Elements { TextInputWrapper.EnsureExists(); if (TextInputWrapper.Current.RequiresOnScreenKeyboard()) { this.OnPressed += async e => { + #if !TEST if (!KeyboardInput.IsVisible) { var title = this.MobileTitle ?? this.PlaceholderText; var result = await KeyboardInput.Show(title, this.MobileDescription, this.Text); if (result != null) this.SetText(result.Replace('\n', ' '), true); } + #endif }; } this.OnTextInput += (element, key, character) => { diff --git a/MLEM/Input/InputHandler.cs b/MLEM/Input/InputHandler.cs index 3da5667..c05f5b5 100644 --- a/MLEM/Input/InputHandler.cs +++ b/MLEM/Input/InputHandler.cs @@ -453,6 +453,7 @@ namespace MLEM.Input { return true; } } + sample = default; return false; } @@ -540,8 +541,10 @@ namespace MLEM.Input { /// /// The gestures to enable public static void EnableGestures(params GestureType[] gestures) { + #if !TEST foreach (var gesture in gestures) TouchPanel.EnabledGestures |= gesture; + #endif } /// @@ -549,8 +552,10 @@ namespace MLEM.Input { /// /// The gestures to disable public static void DisableGestures(params GestureType[] gestures) { + #if !TEST foreach (var gesture in gestures) TouchPanel.EnabledGestures &= ~gesture; + #endif } /// diff --git a/MLEM/Textures/TextureRegion.cs b/MLEM/Textures/TextureRegion.cs index 534086a..d4a12a9 100644 --- a/MLEM/Textures/TextureRegion.cs +++ b/MLEM/Textures/TextureRegion.cs @@ -126,7 +126,11 @@ namespace MLEM.Textures { /// The top left corner of this area /// The size of this area public TextureRegion(TextureRegion region, Point uv, Point size) : + #if TEST this(region?.Texture, (region?.Position ?? Point.Zero) + uv, size) { + #else + this(region.Texture, region.Position + uv, size) { + #endif } } diff --git a/Tests/Stub/StubStyle.cs b/Tests/Stub/StubStyle.cs new file mode 100644 index 0000000..eb3ad42 --- /dev/null +++ b/Tests/Stub/StubStyle.cs @@ -0,0 +1,11 @@ +using MLEM.Ui.Style; + +namespace Tests.Stub { + public class StubStyle : UiStyle { + + public StubStyle() { + this.Font = new StubFont(); + } + + } +} \ No newline at end of file diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index f41e98c..c3683b0 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -1,16 +1,36 @@ netcoreapp3.1 + TEST nunit + false - - - + + + + MLEM\%(RecursiveDir)%(Filename)%(Extension) + + + + MLEM.Ui\%(RecursiveDir)%(Filename)%(Extension) + + + + MLEM.Data\%(RecursiveDir)%(Filename)%(Extension) + + + + + + + + + diff --git a/Tests/UiTests.cs b/Tests/UiTests.cs new file mode 100644 index 0000000..bf98ca3 --- /dev/null +++ b/Tests/UiTests.cs @@ -0,0 +1,66 @@ +using System; +using System.Linq; +using Microsoft.Xna.Framework; +using MLEM.Misc; +using MLEM.Ui; +using MLEM.Ui.Elements; +using MLEM.Ui.Style; +using NUnit.Framework; +using Tests.Stub; + +namespace Tests { + public class UiTests { + + [Test] + public void TestInvalidPanel() { + var invalidPanel = new Panel(Anchor.Center, Vector2.Zero, Vector2.Zero) { + SetWidthBasedOnChildren = true, + SetHeightBasedOnChildren = true + }; + invalidPanel.AddChild(new Paragraph(Anchor.AutoRight, 1, "This is some test text!", true)); + invalidPanel.AddChild(new VerticalSpace(1)); + Assert.Throws(() => AddAndUpdate(invalidPanel)); + } + + [Test] + public void TestComplexPanel() { + var group = new Group(Anchor.TopLeft, Vector2.One, false); + var panel = group.AddChild(new Panel(Anchor.Center, new Vector2(150, 150), Vector2.Zero, false, true, new Point(5, 10), false) { + ChildPadding = new Padding(5, 10, 5, 5) + }); + for (var i = 0; i < 5; i++) { + var button = panel.AddChild(new Button(Anchor.AutoLeft, new Vector2(1)) { + SetHeightBasedOnChildren = true, + Padding = new Padding(0, 0, 0, 1), + ChildPadding = new Vector2(3) + }); + button.AddChild(new Group(Anchor.AutoLeft, new Vector2(0.5F, 30), false) { + CanBeMoused = false + }); + } + AddAndUpdate(group); + + // group has 1 panel with 1 scroll bar, and the panel's 10 children + Assert.AreEqual(1, group.GetChildren().Count()); + Assert.AreEqual(12, group.GetChildren(regardGrandchildren: true).Count()); + + // panel 1 scroll bar and 5 buttons, each button has 1 group, so 11 grandchildren + Assert.AreEqual(6, panel.GetChildren().Count()); + Assert.AreEqual(11, panel.GetChildren(regardGrandchildren: true).Count()); + + var testBtn = panel.GetChildren