1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-25 16:52:55 +08:00
osu-lazer/osu.Game/Screens/Edit/Components/PlaybackControl.cs

185 lines
6.1 KiB
C#
Raw Normal View History

// 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.
2018-04-13 17:19:50 +08:00
using System.Linq;
2018-11-20 15:51:59 +08:00
using osuTK;
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Bindables;
2018-04-13 17:19:50 +08:00
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics.UserInterface;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
2018-04-13 17:19:50 +08:00
using osu.Framework.Timing;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
2018-11-30 13:41:17 +08:00
using osuTK.Input;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Screens.Edit.Components
{
public class PlaybackControl : BottomBarContainer
{
private IconButton playButton;
2020-02-14 21:14:00 +08:00
[Resolved]
private IAdjustableClock adjustableClock { get; set; }
2018-04-13 17:19:50 +08:00
private readonly BindableNumber<double> tempo = new BindableDouble(1);
2018-04-13 17:19:50 +08:00
[BackgroundDependencyLoader]
2020-02-14 21:14:00 +08:00
private void load()
2018-04-13 17:19:50 +08:00
{
Children = new Drawable[]
{
playButton = new IconButton
{
Anchor = Anchor.CentreLeft,
2019-06-16 02:05:58 +08:00
Origin = Anchor.CentreLeft,
2018-04-13 17:19:50 +08:00
Scale = new Vector2(1.4f),
IconScale = new Vector2(1.4f),
2019-04-02 18:55:24 +08:00
Icon = FontAwesome.Regular.PlayCircle,
2018-04-13 17:19:50 +08:00
Action = togglePause,
},
new OsuSpriteText
{
Origin = Anchor.BottomLeft,
2018-09-15 22:30:11 +08:00
Text = "Playback speed",
2018-04-13 17:19:50 +08:00
RelativePositionAxes = Axes.Y,
Y = 0.5f,
Padding = new MarginPadding { Left = 45 }
},
new Container
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
RelativeSizeAxes = Axes.Both,
Height = 0.5f,
Padding = new MarginPadding { Left = 45 },
Child = new PlaybackTabControl { Current = tempo },
2018-04-13 17:19:50 +08:00
}
};
Track?.AddAdjustment(AdjustableProperty.Tempo, tempo);
}
protected override void Dispose(bool isDisposing)
{
Track?.RemoveAdjustment(AdjustableProperty.Tempo, tempo);
base.Dispose(isDisposing);
2018-04-13 17:19:50 +08:00
}
2018-11-30 13:41:17 +08:00
protected override bool OnKeyDown(KeyDownEvent e)
{
switch (e.Key)
{
case Key.Space:
togglePause();
return true;
}
return base.OnKeyDown(e);
}
2018-04-13 17:19:50 +08:00
private void togglePause()
{
if (adjustableClock.IsRunning)
adjustableClock.Stop();
else
adjustableClock.Start();
}
protected override void Update()
{
base.Update();
2019-04-02 18:55:24 +08:00
playButton.Icon = adjustableClock.IsRunning ? FontAwesome.Regular.PauseCircle : FontAwesome.Regular.PlayCircle;
2018-04-13 17:19:50 +08:00
}
private class PlaybackTabControl : OsuTabControl<double>
{
private static readonly double[] tempo_values = { 0.5, 0.75, 1 };
protected override TabItem<double> CreateTabItem(double value) => new PlaybackTabItem(value);
protected override Dropdown<double> CreateDropdown() => null;
public PlaybackTabControl()
{
RelativeSizeAxes = Axes.Both;
TabContainer.Spacing = Vector2.Zero;
tempo_values.ForEach(AddItem);
Current.Value = tempo_values.Last();
2018-04-13 17:19:50 +08:00
}
public class PlaybackTabItem : TabItem<double>
{
private const float fade_duration = 200;
private readonly OsuSpriteText text;
private readonly OsuSpriteText textBold;
2019-02-28 12:31:40 +08:00
public PlaybackTabItem(double value)
: base(value)
2018-04-13 17:19:50 +08:00
{
RelativeSizeAxes = Axes.Both;
Width = 1f / tempo_values.Length;
Children = new Drawable[]
{
text = new OsuSpriteText
{
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
Text = $"{value:0%}",
Font = OsuFont.GetFont(size: 14)
2018-04-13 17:19:50 +08:00
},
textBold = new OsuSpriteText
{
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
Text = $"{value:0%}",
Font = OsuFont.GetFont(size: 14, weight: FontWeight.Bold),
2018-04-13 17:19:50 +08:00
Alpha = 0,
},
};
}
private Color4 hoveredColour;
private Color4 normalColour;
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
text.Colour = normalColour = colours.YellowDarker;
textBold.Colour = hoveredColour = colours.Yellow;
}
2018-10-02 11:02:47 +08:00
protected override bool OnHover(HoverEvent e)
2018-04-13 17:19:50 +08:00
{
updateState();
return true;
}
2018-10-02 11:02:47 +08:00
protected override void OnHoverLost(HoverLostEvent e) => updateState();
2018-04-13 17:19:50 +08:00
protected override void OnActivated() => updateState();
protected override void OnDeactivated() => updateState();
private void updateState()
{
2019-02-21 17:56:34 +08:00
text.FadeColour(Active.Value || IsHovered ? hoveredColour : normalColour, fade_duration, Easing.OutQuint);
text.FadeTo(Active.Value ? 0 : 1, fade_duration, Easing.OutQuint);
textBold.FadeTo(Active.Value ? 1 : 0, fade_duration, Easing.OutQuint);
2018-04-13 17:19:50 +08:00
}
}
}
}
}