1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 02:22:59 +08:00
osu-lazer/osu.Game/Screens/Select/BeatmapDetails.cs

375 lines
14 KiB
C#
Raw Normal View History

2019-02-26 15:10:06 +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.
2018-04-13 17:19:50 +08:00
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.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using System.Linq;
using JetBrains.Annotations;
2018-04-13 17:19:50 +08:00
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Framework.Threading;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Extensions.Color4Extensions;
using osu.Game.Screens.Select.Details;
using osu.Game.Beatmaps;
using osu.Game.Graphics;
2018-04-13 17:19:50 +08:00
using osu.Game.Graphics.Containers;
namespace osu.Game.Screens.Select
{
public class BeatmapDetails : Container
{
private const float spacing = 10;
private const float transition_duration = 250;
private readonly FillFlowContainer top, statsFlow;
private readonly AdvancedStats advanced;
private readonly DetailBox ratingsContainer;
private readonly UserRatings ratings;
private readonly ScrollContainer metadataScroll;
private readonly MetadataSection description, source, tags;
private readonly Container failRetryContainer;
private readonly FailRetryGraph failRetryGraph;
private readonly DimmedLoadingAnimation loading;
private APIAccess api;
private ScheduledDelegate pendingBeatmapSwitch;
private BeatmapInfo beatmap;
2018-04-13 17:19:50 +08:00
public BeatmapInfo Beatmap
{
get => beatmap;
2018-04-13 17:19:50 +08:00
set
{
if (value == beatmap) return;
2018-04-13 17:19:50 +08:00
beatmap = value;
pendingBeatmapSwitch?.Cancel();
pendingBeatmapSwitch = Schedule(updateStatistics);
}
}
public BeatmapDetails()
{
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.Black.Opacity(0.5f),
},
new Container
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Horizontal = spacing },
Children = new Drawable[]
{
top = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Horizontal,
Children = new Drawable[]
{
statsFlow = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Width = 0.5f,
Spacing = new Vector2(spacing),
Padding = new MarginPadding { Right = spacing / 2 },
Children = new[]
{
new DetailBox
{
Child = advanced = new AdvancedStats
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding { Horizontal = spacing, Top = spacing * 2, Bottom = spacing },
},
},
ratingsContainer = new DetailBox
{
Child = ratings = new UserRatings
{
RelativeSizeAxes = Axes.X,
Height = 134,
Padding = new MarginPadding { Horizontal = spacing, Top = spacing },
},
},
},
},
metadataScroll = new ScrollContainer
{
RelativeSizeAxes = Axes.X,
Width = 0.5f,
ScrollbarVisible = false,
Padding = new MarginPadding { Left = spacing / 2 },
Child = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
LayoutDuration = transition_duration,
2019-02-26 15:09:45 +08:00
LayoutEasing = Easing.OutQuad,
2018-04-13 17:19:50 +08:00
Spacing = new Vector2(spacing * 2),
Margin = new MarginPadding { Top = spacing * 2 },
Children = new[]
{
description = new MetadataSection("Description"),
2018-06-01 01:21:22 +08:00
source = new MetadataSection("Source"),
2018-04-13 17:19:50 +08:00
tags = new MetadataSection("Tags"),
},
},
},
},
},
failRetryContainer = new Container
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
RelativeSizeAxes = Axes.X,
Children = new Drawable[]
{
new OsuSpriteText
{
Text = "Points of Failure",
Font = OsuFont.GetFont(weight: FontWeight.Bold, size: 14),
2018-04-13 17:19:50 +08:00
},
failRetryGraph = new FailRetryGraph
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Top = 14 + spacing / 2 },
},
},
},
},
},
loading = new DimmedLoadingAnimation
{
RelativeSizeAxes = Axes.Both,
},
};
}
[BackgroundDependencyLoader]
private void load(APIAccess api)
2018-04-13 17:19:50 +08:00
{
this.api = api;
}
protected override void UpdateAfterChildren()
{
base.UpdateAfterChildren();
metadataScroll.Height = statsFlow.DrawHeight;
failRetryContainer.Height = DrawHeight - Padding.TotalVertical - (top.DrawHeight + spacing / 2);
}
private void updateStatistics()
{
advanced.Beatmap = Beatmap;
description.Text = Beatmap?.Version;
source.Text = Beatmap?.Metadata?.Source;
tags.Text = Beatmap?.Metadata?.Tags;
// metrics may have been previously fetched
if (Beatmap?.Metrics != null)
2018-04-13 17:19:50 +08:00
{
updateMetrics(Beatmap.Metrics);
2018-04-13 17:19:50 +08:00
return;
}
// metrics may not be fetched but can be
if (Beatmap?.OnlineBeatmapID != null)
2018-04-13 17:19:50 +08:00
{
var requestedBeatmap = Beatmap;
2018-04-13 17:19:50 +08:00
var lookup = new GetBeatmapDetailsRequest(requestedBeatmap);
lookup.Success += res =>
{
if (beatmap != requestedBeatmap)
//the beatmap has been changed since we started the lookup.
return;
requestedBeatmap.Metrics = res;
Schedule(() => updateMetrics(res));
2018-04-13 17:19:50 +08:00
};
lookup.Failure += e => Schedule(() => updateMetrics(null));
2018-04-13 17:19:50 +08:00
api.Queue(lookup);
loading.Show();
return;
2018-04-13 17:19:50 +08:00
}
updateMetrics(null);
2018-04-13 17:19:50 +08:00
}
private void updateMetrics([CanBeNull] BeatmapMetrics metrics)
2018-04-13 17:19:50 +08:00
{
var hasRatings = metrics?.Ratings?.Any() ?? false;
var hasRetriesFails = (metrics?.Retries?.Any() ?? false) && (metrics.Fails?.Any() ?? false);
if (hasRatings)
{
ratings.Metrics = metrics;
ratingsContainer.FadeIn(transition_duration);
2018-04-13 17:19:50 +08:00
}
else
2018-04-13 17:19:50 +08:00
{
ratings.Metrics = new BeatmapMetrics
{
Ratings = new int[10],
};
ratingsContainer.FadeTo(0.25f, transition_duration);
2018-04-13 17:19:50 +08:00
}
if (hasRetriesFails)
{
failRetryGraph.Metrics = metrics;
failRetryContainer.FadeIn(transition_duration);
}
else
2018-04-13 17:19:50 +08:00
{
failRetryGraph.Metrics = new BeatmapMetrics
{
Fails = new int[100],
Retries = new int[100],
};
failRetryContainer.FadeOut(transition_duration);
2018-04-13 17:19:50 +08:00
}
loading.Hide();
}
private class DetailBox : Container
{
private readonly Container content;
protected override Container<Drawable> Content => content;
public DetailBox()
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
InternalChildren = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.Black.Opacity(0.5f),
},
content = new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
},
};
}
}
private class MetadataSection : Container
{
private readonly FillFlowContainer textContainer;
private TextFlowContainer textFlow;
public MetadataSection(string title)
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
InternalChild = textContainer = new FillFlowContainer
{
Alpha = 0,
2018-04-13 17:19:50 +08:00
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Spacing = new Vector2(spacing / 2),
Children = new Drawable[]
{
new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Child = new OsuSpriteText
{
Text = title,
Font = OsuFont.GetFont(weight: FontWeight.Bold, size: 14),
2018-04-13 17:19:50 +08:00
},
},
},
};
}
public string Text
{
set
{
if (string.IsNullOrEmpty(value))
{
2019-02-26 15:10:06 +08:00
this.FadeOut(transition_duration);
2018-04-13 17:19:50 +08:00
return;
}
2019-02-26 14:48:34 +08:00
2019-02-26 15:10:06 +08:00
this.FadeIn(transition_duration);
2018-04-13 17:19:50 +08:00
setTextAsync(value);
}
}
private void setTextAsync(string text)
{
LoadComponentAsync(new OsuTextFlowContainer(s => s.Font = s.Font.With(size: 14))
2018-04-13 17:19:50 +08:00
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
2018-06-04 08:41:34 +08:00
Colour = Color4.White.Opacity(0.75f),
2018-04-13 17:19:50 +08:00
Text = text
}, loaded =>
{
textFlow?.Expire();
textContainer.Add(textFlow = loaded);
// fade in if we haven't yet.
textContainer.FadeIn(transition_duration);
2018-04-13 17:19:50 +08:00
});
}
}
private class DimmedLoadingAnimation : VisibilityContainer
{
private readonly LoadingAnimation loading;
public DimmedLoadingAnimation()
{
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.Black.Opacity(0.5f),
},
loading = new LoadingAnimation(),
};
}
protected override void PopIn()
{
this.FadeIn(transition_duration, Easing.OutQuint);
loading.State = Visibility.Visible;
}
protected override void PopOut()
{
this.FadeOut(transition_duration, Easing.OutQuint);
loading.State = Visibility.Hidden;
}
}
}
}