2022-03-27 04:33:52 +08:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
#nullable disable
|
|
|
|
|
2022-08-02 04:04:14 +08:00
|
|
|
using System.Linq;
|
2022-03-27 04:33:52 +08:00
|
|
|
using NUnit.Framework;
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Graphics;
|
2024-05-16 09:49:33 +08:00
|
|
|
using osu.Framework.Graphics.Containers;
|
2022-08-02 04:04:14 +08:00
|
|
|
using osu.Framework.Graphics.Sprites;
|
|
|
|
using osu.Framework.Testing;
|
|
|
|
using osu.Framework.Utils;
|
2022-03-27 04:33:52 +08:00
|
|
|
using osu.Game.Graphics.UserInterface;
|
|
|
|
using osu.Game.Overlays;
|
2024-05-16 09:49:33 +08:00
|
|
|
using osuTK;
|
2022-03-27 04:33:52 +08:00
|
|
|
using osuTK.Input;
|
|
|
|
|
|
|
|
namespace osu.Game.Tests.Visual.UserInterface
|
|
|
|
{
|
|
|
|
[TestFixture]
|
2022-11-24 13:32:20 +08:00
|
|
|
public partial class TestSceneShearedButtons : OsuManualInputManagerTestScene
|
2022-03-27 04:33:52 +08:00
|
|
|
{
|
|
|
|
[Cached]
|
|
|
|
private OverlayColourProvider colourProvider = new OverlayColourProvider(OverlayColourScheme.Green);
|
|
|
|
|
2022-04-26 17:14:57 +08:00
|
|
|
[TestCase(false)]
|
|
|
|
[TestCase(true)]
|
|
|
|
public void TestShearedButton(bool bigButton)
|
2022-04-26 16:32:02 +08:00
|
|
|
{
|
|
|
|
ShearedButton button = null;
|
|
|
|
bool actionFired = false;
|
|
|
|
|
|
|
|
AddStep("create button", () =>
|
|
|
|
{
|
|
|
|
actionFired = false;
|
|
|
|
|
2022-04-26 17:14:57 +08:00
|
|
|
if (bigButton)
|
2022-04-26 16:32:02 +08:00
|
|
|
{
|
2024-05-16 09:49:33 +08:00
|
|
|
Child = button = new ShearedButton(400, 80)
|
2022-04-26 17:14:57 +08:00
|
|
|
{
|
|
|
|
LighterColour = Colour4.FromHex("#FFFFFF"),
|
|
|
|
DarkerColour = Colour4.FromHex("#FFCC22"),
|
|
|
|
TextColour = Colour4.Black,
|
|
|
|
TextSize = 36,
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
Text = "Let's GO!",
|
|
|
|
Action = () => actionFired = true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-05-16 09:49:33 +08:00
|
|
|
Child = button = new ShearedButton(200, 80)
|
2022-04-26 17:14:57 +08:00
|
|
|
{
|
|
|
|
LighterColour = Colour4.FromHex("#FF86DD"),
|
|
|
|
DarkerColour = Colour4.FromHex("#DE31AE"),
|
|
|
|
TextColour = Colour4.White,
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
Text = "Press me",
|
|
|
|
Action = () => actionFired = true,
|
|
|
|
};
|
|
|
|
}
|
2022-04-26 16:32:02 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
AddStep("set disabled", () => button.Enabled.Value = false);
|
|
|
|
AddStep("press button", () => button.TriggerClick());
|
|
|
|
AddAssert("action not fired", () => !actionFired);
|
|
|
|
|
|
|
|
AddStep("set enabled", () => button.Enabled.Value = true);
|
|
|
|
AddStep("press button", () => button.TriggerClick());
|
|
|
|
AddAssert("action fired", () => actionFired);
|
|
|
|
}
|
|
|
|
|
2022-03-27 04:33:52 +08:00
|
|
|
[Test]
|
|
|
|
public void TestShearedToggleButton()
|
|
|
|
{
|
|
|
|
ShearedToggleButton button = null;
|
|
|
|
|
|
|
|
AddStep("create button", () =>
|
|
|
|
{
|
2022-04-01 04:19:08 +08:00
|
|
|
Child = button = new ShearedToggleButton(200)
|
2022-03-27 04:33:52 +08:00
|
|
|
{
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
Text = "Toggle me",
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
AddToggleStep("toggle button", active => button.Active.Value = active);
|
|
|
|
AddToggleStep("toggle disabled", disabled => button.Active.Disabled = disabled);
|
|
|
|
}
|
|
|
|
|
2022-04-01 04:19:08 +08:00
|
|
|
[Test]
|
|
|
|
public void TestSizing()
|
|
|
|
{
|
|
|
|
ShearedToggleButton toggleButton = null;
|
|
|
|
|
|
|
|
AddStep("create fixed width button", () => Child = toggleButton = new ShearedToggleButton(200)
|
|
|
|
{
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
Text = "Fixed width"
|
|
|
|
});
|
2022-08-02 04:04:14 +08:00
|
|
|
AddAssert("draw width is 200", () => toggleButton.DrawWidth, () => Is.EqualTo(200).Within(Precision.FLOAT_EPSILON));
|
|
|
|
|
2022-04-01 04:19:08 +08:00
|
|
|
AddStep("change text", () => toggleButton.Text = "New text");
|
2022-08-02 04:04:14 +08:00
|
|
|
AddAssert("draw width is 200", () => toggleButton.DrawWidth, () => Is.EqualTo(200).Within(Precision.FLOAT_EPSILON));
|
2022-04-01 04:19:08 +08:00
|
|
|
|
|
|
|
AddStep("create auto-sizing button", () => Child = toggleButton = new ShearedToggleButton
|
|
|
|
{
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
Text = "This button autosizes to its text!"
|
|
|
|
});
|
2022-08-02 04:04:14 +08:00
|
|
|
AddAssert("button is wider than text", () => toggleButton.DrawWidth, () => Is.GreaterThan(toggleButton.ChildrenOfType<SpriteText>().Single().DrawWidth));
|
|
|
|
|
|
|
|
float originalDrawWidth = 0;
|
|
|
|
AddStep("store button width", () => originalDrawWidth = toggleButton.DrawWidth);
|
|
|
|
|
2022-04-01 04:19:08 +08:00
|
|
|
AddStep("change text", () => toggleButton.Text = "New text");
|
2022-08-02 04:04:14 +08:00
|
|
|
AddAssert("button is wider than text", () => toggleButton.DrawWidth, () => Is.GreaterThan(toggleButton.ChildrenOfType<SpriteText>().Single().DrawWidth));
|
|
|
|
AddAssert("button width decreased", () => toggleButton.DrawWidth, () => Is.LessThan(originalDrawWidth));
|
2022-04-01 04:19:08 +08:00
|
|
|
}
|
|
|
|
|
2022-03-27 04:33:52 +08:00
|
|
|
[Test]
|
|
|
|
public void TestDisabledState()
|
|
|
|
{
|
|
|
|
ShearedToggleButton button = null;
|
|
|
|
|
|
|
|
AddStep("create button", () =>
|
|
|
|
{
|
2022-04-01 09:53:14 +08:00
|
|
|
Child = button = new ShearedToggleButton(200)
|
2022-03-27 04:33:52 +08:00
|
|
|
{
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
Text = "Toggle me",
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
clickToggle();
|
|
|
|
assertToggleState(true);
|
|
|
|
|
|
|
|
clickToggle();
|
|
|
|
assertToggleState(false);
|
|
|
|
|
|
|
|
setToggleDisabledState(true);
|
|
|
|
|
|
|
|
assertToggleState(false);
|
|
|
|
clickToggle();
|
|
|
|
assertToggleState(false);
|
|
|
|
|
|
|
|
setToggleDisabledState(false);
|
|
|
|
assertToggleState(false);
|
|
|
|
clickToggle();
|
|
|
|
assertToggleState(true);
|
|
|
|
|
|
|
|
setToggleDisabledState(true);
|
|
|
|
assertToggleState(true);
|
|
|
|
clickToggle();
|
|
|
|
assertToggleState(true);
|
|
|
|
|
|
|
|
void clickToggle() => AddStep("click toggle", () =>
|
|
|
|
{
|
|
|
|
InputManager.MoveMouseTo(button);
|
|
|
|
InputManager.Click(MouseButton.Left);
|
|
|
|
});
|
|
|
|
|
|
|
|
void assertToggleState(bool active) => AddAssert($"toggle is {(active ? "" : "not ")}active", () => button.Active.Value == active);
|
|
|
|
|
|
|
|
void setToggleDisabledState(bool disabled) => AddStep($"{(disabled ? "disable" : "enable")} toggle", () => button.Active.Disabled = disabled);
|
|
|
|
}
|
2024-05-16 09:49:33 +08:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestButtons()
|
|
|
|
{
|
|
|
|
AddStep("create buttons", () => Children = new[]
|
|
|
|
{
|
|
|
|
new FillFlowContainer
|
|
|
|
{
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
Direction = FillDirection.Horizontal,
|
|
|
|
AutoSizeAxes = Axes.Both,
|
|
|
|
Scale = new Vector2(2.5f),
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
new ShearedButton(120)
|
|
|
|
{
|
|
|
|
Anchor = Anchor.BottomLeft,
|
|
|
|
Origin = Anchor.BottomLeft,
|
|
|
|
Text = "Test",
|
|
|
|
Action = () => { },
|
|
|
|
Padding = new MarginPadding(),
|
|
|
|
},
|
|
|
|
new ShearedButton(120, 40)
|
|
|
|
{
|
|
|
|
Anchor = Anchor.BottomLeft,
|
|
|
|
Origin = Anchor.BottomLeft,
|
|
|
|
Text = "Test",
|
|
|
|
Action = () => { },
|
|
|
|
Padding = new MarginPadding { Left = -1f },
|
|
|
|
},
|
|
|
|
new ShearedButton(120, 70)
|
|
|
|
{
|
|
|
|
Anchor = Anchor.BottomLeft,
|
|
|
|
Origin = Anchor.BottomLeft,
|
|
|
|
Text = "Test",
|
|
|
|
Action = () => { },
|
|
|
|
Padding = new MarginPadding { Left = 3f },
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-03-27 04:33:52 +08:00
|
|
|
}
|
|
|
|
}
|