diff --git a/osu.Game.Tests/Visual/TestCaseLineBadge.cs b/osu.Game.Tests/Visual/TestCaseLineBadge.cs new file mode 100644 index 0000000000..304b0f9518 --- /dev/null +++ b/osu.Game.Tests/Visual/TestCaseLineBadge.cs @@ -0,0 +1,51 @@ +// Copyright (c) 2007-2018 ppy Pty Ltd . +// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using OpenTK.Graphics; +using osu.Framework.Graphics; +using osu.Framework.Graphics.Containers; +using osu.Framework.Graphics.Shapes; +using osu.Game.Graphics.UserInterface; + +namespace osu.Game.Tests.Visual +{ + public class TestCaseLineBadge : OsuTestCase + { + public TestCaseLineBadge() + { + Container containerHorizontal; + LineBadge lineBadge; + + Add(containerHorizontal = new Container + { + Anchor = Anchor.Centre, + Origin = Anchor.Centre, + Children = new Drawable[] + { + new Box + { + Colour = Color4.Gray, + Alpha = 0.5f, + RelativeSizeAxes = Axes.Both, + }, + lineBadge = new LineBadge + { + Anchor = Anchor.Centre, + UncollapsedSize = 10, + CollapsedSize = 2, + Colour = Color4.DeepSkyBlue, + } + } + }); + + AddStep(@"", () => { }); + AddStep(@"Collapse", () => lineBadge.Collapse()); + AddStep(@"Uncollapse", () => lineBadge.Uncollapse()); + AddSliderStep(@"Resize container", 1, 300, 150, value => containerHorizontal.ResizeTo(value)); + AddStep(@"Horizontal", () => lineBadge.IsHorizontal = true); + AddStep(@"Anchor top", () => lineBadge.Anchor = Anchor.TopCentre); + AddStep(@"Vertical", () => lineBadge.IsHorizontal = false); + AddStep(@"Anchor left", () => lineBadge.Anchor = Anchor.CentreLeft); + } + } +} diff --git a/osu.Game/Graphics/UserInterface/LineBadge.cs b/osu.Game/Graphics/UserInterface/LineBadge.cs new file mode 100644 index 0000000000..91dd2add1f --- /dev/null +++ b/osu.Game/Graphics/UserInterface/LineBadge.cs @@ -0,0 +1,84 @@ +// Copyright (c) 2007-2018 ppy Pty Ltd . +// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using osu.Framework.Graphics; +using osu.Framework.Graphics.Shapes; + +namespace osu.Game.Graphics.UserInterface +{ + public class LineBadge : Circle + { + public float UncollapsedSize; + public float CollapsedSize; + + public bool IsCollapsed { get; private set; } + private bool isHorizontal; + + /// + /// Automatically sets the RelativeSizeAxes and switches X and Y components when changed. + /// + public bool IsHorizontal + { + get { return isHorizontal; } + set + { + if (value == isHorizontal) + return; + if (IsLoaded) + { + FinishTransforms(); + var height = Height; + var width = Width; + RelativeSizeAxes = value ? Axes.X : Axes.Y; + Width = height; + Height = width; + } + else + RelativeSizeAxes = value ? Axes.X : Axes.Y; + isHorizontal = value; + } + } + + /// + /// A simple rounded expandable line. Set its + /// property to the center of the edge it's meant stick with. By default, + /// takes up the full parent's axis defined by . + /// + /// Whether to initialize with the + /// or the + public LineBadge(bool startCollapsed = true) + { + IsCollapsed = startCollapsed; + RelativeSizeAxes = Axes.X; + isHorizontal = true; + Origin = Anchor.Centre; + } + + protected override void LoadComplete() + { + if (isHorizontal) + Height = IsCollapsed ? CollapsedSize : UncollapsedSize; + else + Width = IsCollapsed ? CollapsedSize : UncollapsedSize; + base.LoadComplete(); + } + + public void Collapse(float transitionDuration = 400, Easing easing = Easing.Out) + { + IsCollapsed = true; + if (IsHorizontal) + this.ResizeHeightTo(CollapsedSize, transitionDuration, easing); + else + this.ResizeWidthTo(CollapsedSize, transitionDuration, easing); + } + + public void Uncollapse(float transitionDuration = 400, Easing easing = Easing.OutElastic) + { + IsCollapsed = false; + if (IsHorizontal) + this.ResizeHeightTo(UncollapsedSize, transitionDuration, easing); + else + this.ResizeWidthTo(UncollapsedSize, transitionDuration, easing); + } + } +} diff --git a/osu.Game/Overlays/Changelog/Header/LineBadge.cs b/osu.Game/Overlays/Changelog/Header/LineBadge.cs deleted file mode 100644 index 93eca528c5..0000000000 --- a/osu.Game/Overlays/Changelog/Header/LineBadge.cs +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) 2007-2018 ppy Pty Ltd . -// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE - -using osu.Framework.Graphics; -using osu.Framework.Graphics.Shapes; - -namespace osu.Game.Overlays.Changelog.Header -{ - public class LineBadge : Circle - { - public float TransitionDuration = 400; - public float UncollapsedHeight; - public float CollapsedHeight; - - private bool isCollapsed; - public bool IsCollapsed - { - get { return isCollapsed; } - set - { - isCollapsed = value; - this.ResizeHeightTo(value ? CollapsedHeight : UncollapsedHeight, - value ? TransitionDuration / 2f : TransitionDuration, - value ? Easing.Out : Easing.OutElastic); - } - } - - public LineBadge(bool startCollapsed = true, float collapsedHeight = 1, float uncollapsedHeight = 10) - { - Anchor = Anchor.BottomCentre; - Origin = Anchor.Centre; - CollapsedHeight = collapsedHeight; - UncollapsedHeight = uncollapsedHeight; - Height = startCollapsed ? CollapsedHeight : UncollapsedHeight; - - // this margin prevents jumps when changing text's font weight - Margin = new MarginPadding - { - Left = 10, - Right = 10, - }; - } - } -} diff --git a/osu.Game/Overlays/Changelog/Header/TextBadgePair.cs b/osu.Game/Overlays/Changelog/Header/TextBadgePair.cs index c01501c308..6ff945469c 100644 --- a/osu.Game/Overlays/Changelog/Header/TextBadgePair.cs +++ b/osu.Game/Overlays/Changelog/Header/TextBadgePair.cs @@ -9,6 +9,7 @@ using osu.Framework.Graphics.Colour; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Sprites; using osu.Framework.Input.States; +using osu.Game.Graphics.UserInterface; using System; namespace osu.Game.Overlays.Changelog.Header @@ -36,14 +37,14 @@ namespace osu.Game.Overlays.Changelog.Header public void HideText(double duration = 0, Easing easing = Easing.InOutCubic) { - LineBadge.IsCollapsed = true; + LineBadge.Collapse(); Text.MoveToY(20, duration, easing) .FadeOut(duration, easing); } public void ShowText(double duration = 0, string displayText = null, Easing easing = Easing.InOutCubic) { - LineBadge.IsCollapsed = false; + LineBadge.Uncollapse(); if (!string.IsNullOrEmpty(displayText)) Text.Text = displayText; Text.MoveToY(0, duration, easing) @@ -55,7 +56,7 @@ namespace osu.Game.Overlays.Changelog.Header /// Full change takes double this time. public void ChangeText(double duration = 0, string displayText = null, Easing easing = Easing.InOutCubic) { - LineBadge.IsCollapsed = true; + LineBadge.Collapse(); Text.MoveToY(20, duration, easing) .FadeOut(duration, easing) .Then() @@ -67,7 +68,7 @@ namespace osu.Game.Overlays.Changelog.Header Scheduler.AddDelayed(() => { if (!string.IsNullOrEmpty(displayText)) Text.Text = displayText; - LineBadge.IsCollapsed = false; + LineBadge.Uncollapse(); }, duration); } @@ -89,8 +90,10 @@ namespace osu.Game.Overlays.Changelog.Header }, LineBadge = new LineBadge(startCollapsed) { + CollapsedSize = 2, + UncollapsedSize = 10, Colour = badgeColour, - RelativeSizeAxes = Axes.X, + Anchor = Anchor.BottomCentre, } }; } @@ -98,14 +101,14 @@ namespace osu.Game.Overlays.Changelog.Header public virtual void Deactivate() { IsActivated = false; - LineBadge.IsCollapsed = true; + LineBadge.Collapse(); Text.Font = "Exo2.0-Regular"; } public virtual void Activate() { IsActivated = true; - LineBadge.IsCollapsed = false; + LineBadge.Uncollapse(); Text.Font = "Exo2.0-Bold"; SampleActivate?.Play(); } diff --git a/osu.Game/Overlays/Changelog/Header/TextBadgePairListing.cs b/osu.Game/Overlays/Changelog/Header/TextBadgePairListing.cs index 36db135675..2e9152b256 100644 --- a/osu.Game/Overlays/Changelog/Header/TextBadgePairListing.cs +++ b/osu.Game/Overlays/Changelog/Header/TextBadgePairListing.cs @@ -33,7 +33,7 @@ namespace osu.Game.Overlays.Changelog.Header public override void Activate() { IsActivated = true; - LineBadge.IsCollapsed = false; + LineBadge.Uncollapse(); Text.Font = "Exo2.0-Bold"; SetTextColour(Color4.White, 100); SampleActivate?.Play(); @@ -43,7 +43,7 @@ namespace osu.Game.Overlays.Changelog.Header public override void Deactivate() { IsActivated = false; - LineBadge.IsCollapsed = true; + LineBadge.Collapse(); Text.Font = "Exo2.0-Regular"; // commented out since it makes bad resize-jumping SetTextColour(badgeColour, 100); OnDeactivation?.Invoke(); @@ -57,14 +57,14 @@ namespace osu.Game.Overlays.Changelog.Header protected override bool OnHover(InputState state) { - LineBadge.ResizeHeightTo(LineBadge.UncollapsedHeight, LineBadge.TransitionDuration, Easing.OutElastic); + LineBadge.Uncollapse(); return base.OnHover(state); } protected override void OnHoverLost(InputState state) { if (!IsActivated) - LineBadge.ResizeHeightTo(1, LineBadge.TransitionDuration, Easing.Out); + LineBadge.Collapse(); base.OnHoverLost(state); } diff --git a/osu.Game/Overlays/Changelog/StreamBadge.cs b/osu.Game/Overlays/Changelog/StreamBadge.cs index 44fdeb80a7..59ce97bcc7 100644 --- a/osu.Game/Overlays/Changelog/StreamBadge.cs +++ b/osu.Game/Overlays/Changelog/StreamBadge.cs @@ -10,6 +10,7 @@ using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Sprites; using osu.Framework.Input.States; using osu.Game.Graphics; +using osu.Game.Graphics.UserInterface; using osu.Game.Online.API.Requests.Responses; using System; @@ -27,7 +28,7 @@ namespace osu.Game.Overlays.Changelog private bool isActivated; - private readonly Header.LineBadge lineBadge; + private readonly LineBadge lineBadge; private SampleChannel sampleHover; public readonly APIChangelog ChangelogEntry; private readonly FillFlowContainer text; @@ -75,21 +76,23 @@ namespace osu.Game.Overlays.Changelog }, } }, - lineBadge = new Header.LineBadge(false, 2, 4) + lineBadge = new LineBadge(false) { Anchor = Anchor.TopCentre, - Origin = Anchor.TopCentre, Colour = StreamColour.FromStreamName(ChangelogEntry.UpdateStream.Name), - RelativeSizeAxes = Axes.X, + UncollapsedSize = 4, + CollapsedSize = 2, }, }; } + /// In case we don't want to + /// fire the event. public void Activate(bool withoutFiringUpdates = true) { isActivated = true; this.FadeIn(transition_duration); - lineBadge.IsCollapsed = false; + lineBadge.Uncollapse(); if (!withoutFiringUpdates) Selected?.Invoke(this, EventArgs.Empty); } @@ -101,7 +104,7 @@ namespace osu.Game.Overlays.Changelog if (!IsHovered) { this.FadeTo(0.5f, transition_duration); - lineBadge.IsCollapsed = true; + lineBadge.Collapse(200); } } @@ -116,7 +119,7 @@ namespace osu.Game.Overlays.Changelog sampleHover?.Play(); DisableDim(); this.FadeIn(transition_duration); - lineBadge.IsCollapsed = false; + lineBadge.Uncollapse(); return base.OnHover(state); } @@ -125,7 +128,7 @@ namespace osu.Game.Overlays.Changelog if (!isActivated) { this.FadeTo(0.5f, transition_duration); - lineBadge.IsCollapsed = true; + lineBadge.Collapse(200); } else EnableDim();