mirror of
https://github.com/ppy/osu.git
synced 2025-02-13 18:32:55 +08:00
Encapsulate base graph further
This commit is contained in:
parent
2b0bdd1db5
commit
9edddbaf46
@ -39,27 +39,29 @@ namespace osu.Game.Overlays.Profile.Header.Components
|
||||
|
||||
private void updateStatistics(UserStatistics statistics)
|
||||
{
|
||||
placeholder.FadeIn(FADE_DURATION, Easing.Out);
|
||||
int[] userRanks = statistics?.RankHistory?.Data;
|
||||
|
||||
if (statistics?.Ranks.Global == null)
|
||||
if (userRanks == null)
|
||||
{
|
||||
Graph.FadeOut(FADE_DURATION, Easing.Out);
|
||||
Data = null;
|
||||
return;
|
||||
}
|
||||
|
||||
int[] userRanks = statistics.RankHistory?.Data ?? new[] { statistics.Ranks.Global.Value };
|
||||
Data = userRanks.Select((x, index) => new KeyValuePair<int, int>(index, x)).Where(x => x.Value != 0).ToArray();
|
||||
|
||||
if (Data.Length > 1)
|
||||
{
|
||||
placeholder.FadeOut(FADE_DURATION, Easing.Out);
|
||||
|
||||
Graph.DefaultValueCount = Data.Length;
|
||||
Graph.Values = Data.Select(x => -MathF.Log(x.Value));
|
||||
}
|
||||
|
||||
Graph.FadeTo(Data.Length > 1 ? 1 : 0, FADE_DURATION, Easing.Out);
|
||||
protected override float GetDataPointHeight(int rank) => -MathF.Log(rank);
|
||||
|
||||
protected override void ShowGraph()
|
||||
{
|
||||
base.ShowGraph();
|
||||
placeholder.FadeOut(FADE_DURATION, Easing.Out);
|
||||
}
|
||||
|
||||
protected override void HideGraph()
|
||||
{
|
||||
base.HideGraph();
|
||||
placeholder.FadeIn(FADE_DURATION, Easing.Out);
|
||||
}
|
||||
|
||||
protected override object GetTooltipContent(int index, int rank)
|
||||
|
@ -4,43 +4,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using osu.Framework.Graphics;
|
||||
using static osu.Game.Users.User;
|
||||
|
||||
namespace osu.Game.Overlays.Profile.Sections.Historical
|
||||
{
|
||||
public abstract class UserHistoryGraph : UserGraph<DateTime, long>
|
||||
{
|
||||
private UserHistoryCount[] values;
|
||||
|
||||
public UserHistoryCount[] Values
|
||||
{
|
||||
get => values;
|
||||
set
|
||||
{
|
||||
values = value;
|
||||
updateValues(value);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateValues(UserHistoryCount[] values)
|
||||
if (value == null)
|
||||
{
|
||||
if (values == null || !values.Any())
|
||||
{
|
||||
Graph.FadeOut(FADE_DURATION, Easing.Out);
|
||||
Data = null;
|
||||
return;
|
||||
}
|
||||
|
||||
Data = values.Select(v => new KeyValuePair<DateTime, long>(v.Date, v.Count)).ToArray();
|
||||
Data = value.Select(v => new KeyValuePair<DateTime, long>(v.Date, v.Count)).ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
if (values.Length > 1)
|
||||
{
|
||||
Graph.DefaultValueCount = Data.Length;
|
||||
Graph.Values = Data.Select(x => (float)x.Value);
|
||||
Graph.FadeIn(FADE_DURATION, Easing.Out);
|
||||
}
|
||||
}
|
||||
protected override float GetDataPointHeight(long playCount) => playCount;
|
||||
|
||||
protected override object GetTooltipContent(DateTime date, long playCount)
|
||||
{
|
||||
|
@ -21,54 +21,83 @@ namespace osu.Game.Overlays.Profile
|
||||
{
|
||||
protected const float FADE_DURATION = 150;
|
||||
|
||||
protected readonly UserLineGraph Graph;
|
||||
protected KeyValuePair<TKey, TValue>[] Data;
|
||||
private readonly UserLineGraph graph;
|
||||
private KeyValuePair<TKey, TValue>[] data;
|
||||
private int dataIndex;
|
||||
|
||||
protected UserGraph()
|
||||
{
|
||||
Add(Graph = new UserLineGraph
|
||||
data = Array.Empty<KeyValuePair<TKey, TValue>>();
|
||||
|
||||
Add(graph = new UserLineGraph
|
||||
{
|
||||
RelativeSizeAxes = Axes.Both,
|
||||
Alpha = 0
|
||||
});
|
||||
|
||||
Graph.OnBallMove += i => dataIndex = i;
|
||||
graph.OnBallMove += i => dataIndex = i;
|
||||
}
|
||||
|
||||
[BackgroundDependencyLoader]
|
||||
private void load(OsuColour colours)
|
||||
{
|
||||
Graph.LineColour = colours.Yellow;
|
||||
graph.LineColour = colours.Yellow;
|
||||
}
|
||||
|
||||
protected override bool OnHover(HoverEvent e)
|
||||
{
|
||||
if (Data?.Length > 1)
|
||||
{
|
||||
Graph.UpdateBallPosition(e.MousePosition.X);
|
||||
Graph.ShowBar();
|
||||
}
|
||||
if (data.Length <= 1)
|
||||
return base.OnHover(e);
|
||||
|
||||
graph.UpdateBallPosition(e.MousePosition.X);
|
||||
graph.ShowBar();
|
||||
|
||||
return base.OnHover(e);
|
||||
}
|
||||
|
||||
protected override bool OnMouseMove(MouseMoveEvent e)
|
||||
{
|
||||
if (Data?.Length > 1)
|
||||
Graph.UpdateBallPosition(e.MousePosition.X);
|
||||
if (data.Length > 1)
|
||||
graph.UpdateBallPosition(e.MousePosition.X);
|
||||
|
||||
return base.OnMouseMove(e);
|
||||
}
|
||||
|
||||
protected override void OnHoverLost(HoverLostEvent e)
|
||||
{
|
||||
if (Data?.Length > 1)
|
||||
Graph.HideBar();
|
||||
if (data.Length > 1)
|
||||
graph.HideBar();
|
||||
|
||||
base.OnHoverLost(e);
|
||||
}
|
||||
|
||||
protected KeyValuePair<TKey, TValue>[] Data
|
||||
{
|
||||
set
|
||||
{
|
||||
value ??= Array.Empty<KeyValuePair<TKey, TValue>>();
|
||||
data = value;
|
||||
redrawGraph();
|
||||
}
|
||||
}
|
||||
|
||||
private void redrawGraph()
|
||||
{
|
||||
if (data.Length == 0)
|
||||
{
|
||||
HideGraph();
|
||||
return;
|
||||
}
|
||||
|
||||
graph.DefaultValueCount = data.Length;
|
||||
graph.Values = data.Select(pair => GetDataPointHeight(pair.Value)).ToArray();
|
||||
ShowGraph();
|
||||
}
|
||||
|
||||
protected abstract float GetDataPointHeight(TValue value);
|
||||
protected virtual void ShowGraph() => graph.FadeIn(FADE_DURATION, Easing.Out);
|
||||
protected virtual void HideGraph() => graph.FadeOut(FADE_DURATION, Easing.Out);
|
||||
|
||||
public ITooltip GetCustomTooltip() => GetTooltip();
|
||||
|
||||
protected abstract UserGraphTooltip GetTooltip();
|
||||
@ -77,10 +106,10 @@ namespace osu.Game.Overlays.Profile
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Data == null || Data.Length == 0)
|
||||
if (data.Length == 0)
|
||||
return null;
|
||||
|
||||
var (key, value) = Data[dataIndex];
|
||||
var (key, value) = data[dataIndex];
|
||||
return GetTooltipContent(key, value);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user