1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-13 12:02:54 +08:00

Rewrite LineBadge.cs

+ update all its references
This commit is contained in:
HoutarouOreki 2018-07-23 17:49:42 +02:00
parent 4cc5a657f3
commit e27292fef8
6 changed files with 160 additions and 63 deletions

View File

@ -0,0 +1,51 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// 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);
}
}
}

View File

@ -0,0 +1,84 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// 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;
/// <summary>
/// Automatically sets the RelativeSizeAxes and switches X and Y components when changed.
/// </summary>
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;
}
}
/// <summary>
/// A simple rounded expandable line. Set its <see cref="Anchor"/>
/// property to the center of the edge it's meant stick with. By default,
/// takes up the full parent's axis defined by <see cref="IsHorizontal"/>.
/// </summary>
/// <param name="startCollapsed">Whether to initialize with the
/// <see cref="CollapsedSize"/> or the <see cref="UncollapsedSize"/></param>
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);
}
}
}

View File

@ -1,44 +0,0 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// 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,
};
}
}
}

View File

@ -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.</param>
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();
}

View File

@ -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);
}

View File

@ -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<SpriteText> 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,
},
};
}
/// <param name="withoutFiringUpdates">In case we don't want to
/// fire the <see cref="Selected"/> event.</param>
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();