1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-21 12:33:03 +08:00
osu-lazer/osu.Game/Overlays/NowPlayingOverlay.cs

595 lines
22 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.
using System;
using System.Threading;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Configuration;
using osu.Framework.Extensions.Color4Extensions;
2023-07-30 15:17:04 +08:00
using osu.Framework.Extensions.ObjectExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Effects;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Textures;
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
using osu.Game.Beatmaps;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Localisation;
using osu.Game.Overlays.Music;
using osuTK;
using osuTK.Graphics;
namespace osu.Game.Overlays
{
2022-11-24 13:32:20 +08:00
public partial class NowPlayingOverlay : OsuFocusedOverlayContainer, INamedOverlayComponent
{
public IconUsage Icon => OsuIcon.Music;
public LocalisableString Title => NowPlayingStrings.HeaderTitle;
public LocalisableString Description => NowPlayingStrings.HeaderDescription;
2024-03-19 03:22:23 +08:00
private const float player_width = 400;
private const float player_height = 130;
private const float transition_length = 800;
private const float progress_height = 10;
private const float bottom_black_area_height = 55;
2022-09-25 02:52:16 +08:00
private const float margin = 10;
2023-07-30 15:17:04 +08:00
private Drawable background = null!;
private ProgressBar progressBar = null!;
2023-07-30 15:17:04 +08:00
private IconButton prevButton = null!;
private IconButton playButton = null!;
private IconButton nextButton = null!;
private MusicIconButton shuffleButton = null!;
2023-07-30 15:17:04 +08:00
private IconButton playlistButton = null!;
private ScrollingTextContainer title = null!, artist = null!;
2023-07-30 15:17:04 +08:00
private PlaylistOverlay? playlist;
2023-07-30 15:17:04 +08:00
private Container dragContainer = null!;
private Container playerContainer = null!;
private Container playlistContainer = null!;
protected override double PopInOutSampleBalance => OsuGameBase.SFX_STEREO_STRENGTH * 0.75f;
[Resolved]
2023-07-30 15:17:04 +08:00
private MusicController musicController { get; set; } = null!;
[Resolved]
2023-07-30 15:17:04 +08:00
private Bindable<WorkingBeatmap> beatmap { get; set; } = null!;
[Resolved]
2023-07-30 15:17:04 +08:00
private OsuColour colours { get; set; } = null!;
2023-07-30 20:28:16 +08:00
private Bindable<bool> allowTrackControl = null!;
2024-09-18 21:30:56 +08:00
private readonly BindableBool shuffle = new BindableBool(true);
public NowPlayingOverlay()
{
2024-03-19 03:22:23 +08:00
Width = player_width;
2022-09-25 02:52:16 +08:00
Margin = new MarginPadding(margin);
}
[BackgroundDependencyLoader]
private void load()
{
Children = new Drawable[]
{
dragContainer = new DragContainer
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.X,
Children = new Drawable[]
{
playerContainer = new Container
{
RelativeSizeAxes = Axes.X,
Height = player_height,
Masking = true,
CornerRadius = 5,
EdgeEffect = new EdgeEffectParameters
{
Type = EdgeEffectType.Shadow,
Colour = Color4.Black.Opacity(40),
Radius = 5,
},
Children = new[]
{
background = Empty(),
title = new ScrollingTextContainer
{
Origin = Anchor.BottomCentre,
Anchor = Anchor.TopCentre,
Position = new Vector2(0, 40),
Font = OsuFont.GetFont(size: 25, italics: true),
Colour = Color4.White,
Text = @"Nothing to play",
},
artist = new ScrollingTextContainer
{
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
Position = new Vector2(0, 45),
Font = OsuFont.GetFont(size: 15, weight: FontWeight.Bold, italics: true),
Colour = Color4.White,
Text = @"Nothing to play",
},
new Container
{
Padding = new MarginPadding { Bottom = progress_height },
Height = bottom_black_area_height,
RelativeSizeAxes = Axes.X,
Origin = Anchor.BottomCentre,
Anchor = Anchor.BottomCentre,
Children = new Drawable[]
{
new FillFlowContainer<IconButton>
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Spacing = new Vector2(5),
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Children = new[]
{
prevButton = new MusicIconButton
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
2019-10-24 12:10:17 +08:00
Action = () => musicController.PreviousTrack(),
Icon = FontAwesome.Solid.StepBackward,
},
playButton = new MusicIconButton
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Scale = new Vector2(1.4f),
IconScale = new Vector2(1.4f),
Action = () => musicController.TogglePause(),
Icon = FontAwesome.Regular.PlayCircle,
},
nextButton = new MusicIconButton
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Action = () => musicController.NextTrack(),
Icon = FontAwesome.Solid.StepForward,
},
}
},
2024-10-01 16:12:38 +08:00
shuffleButton = new MusicIconButton
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.Centre,
Position = new Vector2(bottom_black_area_height / 2, 0),
Action = shuffle.Toggle,
Icon = FontAwesome.Solid.Random,
},
playlistButton = new MusicIconButton
{
Origin = Anchor.Centre,
Anchor = Anchor.CentreRight,
Position = new Vector2(-bottom_black_area_height / 2, 0),
Icon = FontAwesome.Solid.Bars,
Action = togglePlaylist
},
}
},
2019-11-06 15:11:47 +08:00
progressBar = new HoverableProgressBar
{
Origin = Anchor.BottomCentre,
Anchor = Anchor.BottomCentre,
2019-11-06 15:11:47 +08:00
Height = progress_height / 2,
FillColour = colours.Yellow,
BackgroundColour = colours.YellowDarker.Opacity(0.5f),
OnSeek = musicController.SeekTo
}
},
},
playlistContainer = new Container
{
RelativeSizeAxes = Axes.X,
Y = player_height + margin,
}
}
},
};
}
private void togglePlaylist()
{
if (playlist == null)
{
LoadComponentAsync(playlist = new PlaylistOverlay
{
RelativeSizeAxes = Axes.Both,
}, _ =>
{
playlistContainer.Add(playlist);
playlist.State.BindValueChanged(s => playlistButton.FadeColour(s.NewValue == Visibility.Visible ? colours.Yellow : Color4.White, 200, Easing.OutQuint), true);
togglePlaylist();
});
return;
}
if (!beatmap.Disabled)
playlist.ToggleVisibility();
}
protected override void LoadComplete()
{
base.LoadComplete();
beatmap.BindDisabledChanged(_ => Scheduler.AddOnce(updateEnabledStates));
allowTrackControl = musicController.AllowTrackControl.GetBoundCopy();
allowTrackControl.BindValueChanged(_ => Scheduler.AddOnce(updateEnabledStates), true);
shuffle.BindTo(musicController.Shuffle);
shuffle.BindValueChanged(s => shuffleButton.FadeColour(s.NewValue ? colours.Yellow : Color4.White, 200, Easing.OutQuint), true);
musicController.TrackChanged += trackChanged;
trackChanged(beatmap.Value);
}
protected override void PopIn()
{
this.FadeIn(transition_length, Easing.OutQuint);
dragContainer.ScaleTo(1, transition_length, Easing.OutElastic);
}
protected override void PopOut()
{
base.PopOut();
this.FadeOut(transition_length, Easing.OutQuint);
dragContainer.ScaleTo(0.9f, transition_length, Easing.OutQuint);
}
protected override void UpdateAfterChildren()
{
base.UpdateAfterChildren();
playlistContainer.Height = MathF.Min(Parent!.DrawHeight - margin * 3 - player_height, PlaylistOverlay.PLAYLIST_HEIGHT);
float height = player_height;
if (playlist != null)
{
height += playlist.DrawHeight;
if (playlist.State.Value == Visibility.Visible)
height += margin;
}
Height = dragContainer.Height = height;
}
protected override void Update()
{
base.Update();
if (pendingBeatmapSwitch != null)
{
pendingBeatmapSwitch();
pendingBeatmapSwitch = null;
}
2020-08-11 12:53:23 +08:00
var track = musicController.CurrentTrack;
2020-08-05 20:10:38 +08:00
2020-08-11 23:48:38 +08:00
if (!track.IsDummyDevice)
{
2020-08-05 20:10:38 +08:00
progressBar.EndTime = track.Length;
progressBar.CurrentTime = track.CurrentTime;
2020-08-05 20:10:38 +08:00
playButton.Icon = track.IsRunning ? FontAwesome.Regular.PauseCircle : FontAwesome.Regular.PlayCircle;
}
else
{
progressBar.CurrentTime = 0;
progressBar.EndTime = 1;
playButton.Icon = FontAwesome.Regular.PlayCircle;
}
}
2023-07-30 15:17:04 +08:00
private Action? pendingBeatmapSwitch;
private CancellationTokenSource? backgroundLoadCancellation;
private WorkingBeatmap? currentBeatmap;
private void trackChanged(WorkingBeatmap beatmap, TrackChangeDirection direction = TrackChangeDirection.None)
{
currentBeatmap = beatmap;
// avoid using scheduler as our scheduler may not be run for a long time, holding references to beatmaps.
pendingBeatmapSwitch = delegate
{
BeatmapMetadata metadata = beatmap.Metadata;
title.Text = new RomanisableString(metadata.TitleUnicode, metadata.Title);
artist.Text = new RomanisableString(metadata.ArtistUnicode, metadata.Artist);
backgroundLoadCancellation?.Cancel();
LoadComponentAsync(new Background(beatmap) { Depth = float.MaxValue }, newBackground =>
{
if (beatmap != currentBeatmap)
{
newBackground.Dispose();
return;
}
switch (direction)
{
case TrackChangeDirection.Next:
2024-03-19 03:22:23 +08:00
newBackground.Position = new Vector2(player_width, 0);
newBackground.MoveToX(0, 500, Easing.OutCubic);
2024-03-19 03:22:23 +08:00
background.MoveToX(-player_width, 500, Easing.OutCubic);
break;
case TrackChangeDirection.Prev:
2024-03-19 03:22:23 +08:00
newBackground.Position = new Vector2(-player_width, 0);
newBackground.MoveToX(0, 500, Easing.OutCubic);
2024-03-19 03:22:23 +08:00
background.MoveToX(player_width, 500, Easing.OutCubic);
break;
}
background.Expire();
background = newBackground;
playerContainer.Add(newBackground);
}, (backgroundLoadCancellation = new CancellationTokenSource()).Token);
};
}
private void updateEnabledStates()
{
bool beatmapDisabled = beatmap.Disabled;
bool trackControlDisabled = !musicController.AllowTrackControl.Value;
if (beatmapDisabled || trackControlDisabled)
playlist?.Hide();
prevButton.Enabled.Value = !beatmapDisabled && !trackControlDisabled;
nextButton.Enabled.Value = !beatmapDisabled && !trackControlDisabled;
playlistButton.Enabled.Value = !beatmapDisabled && !trackControlDisabled;
playButton.Enabled.Value = !trackControlDisabled;
}
2019-08-13 14:04:04 +08:00
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
2023-07-30 15:17:04 +08:00
if (musicController.IsNotNull())
2019-08-13 14:04:04 +08:00
musicController.TrackChanged -= trackChanged;
}
2022-11-24 13:32:20 +08:00
private partial class MusicIconButton : IconButton
{
public MusicIconButton()
{
AutoSizeAxes = Axes.Both;
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
HoverColour = colours.YellowDark.Opacity(0.6f);
FlashColour = colours.Yellow;
}
protected override void LoadComplete()
{
base.LoadComplete();
// works with AutoSizeAxes above to make buttons autosize with the scale animation.
Content.AutoSizeAxes = Axes.None;
Content.Size = new Vector2(DEFAULT_BUTTON_SIZE);
}
}
2022-11-24 13:32:20 +08:00
private partial class Background : BufferedContainer
{
private readonly Sprite sprite;
private readonly WorkingBeatmap beatmap;
2023-07-30 15:17:04 +08:00
public Background(WorkingBeatmap beatmap)
: base(cachedFrameBuffer: true)
{
this.beatmap = beatmap;
Depth = float.MaxValue;
RelativeSizeAxes = Axes.Both;
Children = new Drawable[]
{
sprite = new Sprite
{
RelativeSizeAxes = Axes.Both,
Colour = OsuColour.Gray(150),
FillMode = FillMode.Fill,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
},
new Box
{
RelativeSizeAxes = Axes.X,
Height = bottom_black_area_height,
Origin = Anchor.BottomCentre,
Anchor = Anchor.BottomCentre,
Colour = Color4.Black.Opacity(0.5f)
}
};
}
[BackgroundDependencyLoader]
private void load(LargeTextureStore textures)
{
sprite.Texture = beatmap.GetBackground() ?? textures.Get(@"Backgrounds/bg2");
}
}
2022-11-24 13:32:20 +08:00
private partial class DragContainer : Container
{
protected override bool OnDragStart(DragStartEvent e)
{
return true;
}
protected override void OnDrag(DragEvent e)
{
Vector2 change = e.MousePosition - e.MouseDownPosition;
// Diminish the drag distance as we go further to simulate "rubber band" feeling.
change *= change.Length <= 0 ? 0 : MathF.Pow(change.Length, 0.7f) / change.Length;
this.MoveTo(change);
}
protected override void OnDragEnd(DragEndEvent e)
{
this.MoveTo(Vector2.Zero, 800, Easing.OutElastic);
base.OnDragEnd(e);
}
}
2019-11-06 15:11:47 +08:00
2022-11-24 13:32:20 +08:00
private partial class HoverableProgressBar : ProgressBar
2019-11-06 15:11:47 +08:00
{
public HoverableProgressBar()
: base(true)
{
}
2019-11-06 15:11:47 +08:00
protected override bool OnHover(HoverEvent e)
{
this.ResizeHeightTo(progress_height, 500, Easing.OutQuint);
return base.OnHover(e);
}
protected override void OnHoverLost(HoverLostEvent e)
{
this.ResizeHeightTo(progress_height / 2, 500, Easing.OutQuint);
base.OnHoverLost(e);
}
}
private partial class ScrollingTextContainer : CompositeDrawable
{
private const float initial_move_delay = 1000;
private const float pixels_per_second = 50;
private OsuSpriteText mainSpriteText = null!;
private OsuSpriteText fillerSpriteText = null!;
private Bindable<bool> showUnicode = null!;
[Resolved]
private FrameworkConfigManager frameworkConfig { get; set; } = null!;
2024-05-03 13:46:42 +08:00
private LocalisableString text;
public LocalisableString Text
{
get => text;
set
{
text = value;
2024-05-03 13:46:42 +08:00
if (IsLoaded)
updateText();
}
}
2024-05-03 13:46:42 +08:00
private FontUsage font = OsuFont.Default;
public FontUsage Font
{
2024-05-03 13:46:42 +08:00
get => font;
set
2024-05-03 08:05:12 +08:00
{
2024-05-03 13:46:42 +08:00
font = value;
2024-05-03 13:46:42 +08:00
if (IsLoaded)
updateFontAndText();
}
}
public ScrollingTextContainer()
{
AutoSizeAxes = Axes.Both;
}
[BackgroundDependencyLoader]
private void load()
{
InternalChild = new FillFlowContainer<OsuSpriteText>
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Children = new[]
{
mainSpriteText = new OsuSpriteText { Padding = new MarginPadding { Horizontal = margin } },
fillerSpriteText = new OsuSpriteText { Padding = new MarginPadding { Horizontal = margin }, Alpha = 0 },
}
};
}
2024-05-03 13:46:42 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
showUnicode = frameworkConfig.GetBindable<bool>(FrameworkSetting.ShowUnicode);
showUnicode.BindValueChanged(_ => updateText());
2024-05-03 13:46:42 +08:00
updateFontAndText();
}
private void updateFontAndText()
{
mainSpriteText.Font = font;
fillerSpriteText.Font = font;
updateText();
}
private void updateText()
{
mainSpriteText.Text = text;
fillerSpriteText.Alpha = 0;
ClearTransforms();
X = 0;
float textOverflowWidth = mainSpriteText.Width - player_width;
// apply half margin of tolerance on both sides before the text scrolls
if (textOverflowWidth > margin)
{
fillerSpriteText.Alpha = 1;
fillerSpriteText.Text = text;
2024-05-03 08:04:22 +08:00
float initialX = (textOverflowWidth + mainSpriteText.Width) / 2;
float targetX = (textOverflowWidth - mainSpriteText.Width) / 2;
this.MoveToX(initialX)
.Delay(initial_move_delay)
.MoveToX(targetX, mainSpriteText.Width * 1000 / pixels_per_second)
.Loop();
}
}
}
}
}