2020-02-08 04:26:35 +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.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
|
using osu.Framework.Graphics.Cursor;
|
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
|
|
|
|
using osu.Framework.Input.Events;
|
|
|
|
|
using osu.Game.Graphics;
|
|
|
|
|
using osu.Game.Graphics.Sprites;
|
|
|
|
|
using osu.Game.Graphics.UserInterface;
|
|
|
|
|
using osuTK;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays.Profile
|
|
|
|
|
{
|
2020-03-10 01:13:59 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Graph which is used in <see cref="UserProfileOverlay"/> to present changes in user statistics over time.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TKey">Type of data to be used for X-axis of the graph.</typeparam>
|
|
|
|
|
/// <typeparam name="TValue">Type of data to be used for Y-axis of the graph.</typeparam>
|
2020-02-08 04:26:35 +08:00
|
|
|
|
public abstract class UserGraph<TKey, TValue> : Container, IHasCustomTooltip
|
|
|
|
|
{
|
|
|
|
|
protected const float FADE_DURATION = 150;
|
|
|
|
|
|
2020-02-13 03:19:20 +08:00
|
|
|
|
private readonly UserLineGraph graph;
|
|
|
|
|
private KeyValuePair<TKey, TValue>[] data;
|
2020-02-13 02:15:37 +08:00
|
|
|
|
private int dataIndex;
|
2020-02-08 04:26:35 +08:00
|
|
|
|
|
|
|
|
|
protected UserGraph()
|
|
|
|
|
{
|
2020-02-13 03:19:20 +08:00
|
|
|
|
Add(graph = new UserLineGraph
|
2020-02-08 04:26:35 +08:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
2020-02-08 05:10:17 +08:00
|
|
|
|
Alpha = 0
|
2020-02-08 04:26:35 +08:00
|
|
|
|
});
|
|
|
|
|
|
2020-02-13 03:19:20 +08:00
|
|
|
|
graph.OnBallMove += i => dataIndex = i;
|
2020-02-08 04:26:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(OsuColour colours)
|
|
|
|
|
{
|
2020-02-13 03:19:20 +08:00
|
|
|
|
graph.LineColour = colours.Yellow;
|
2020-02-08 04:26:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool OnHover(HoverEvent e)
|
|
|
|
|
{
|
2020-03-10 00:42:35 +08:00
|
|
|
|
if (data?.Length > 1)
|
2020-03-10 00:22:03 +08:00
|
|
|
|
{
|
|
|
|
|
graph.UpdateBallPosition(e.MousePosition.X);
|
|
|
|
|
graph.ShowBar();
|
2020-02-13 03:19:20 +08:00
|
|
|
|
|
2020-03-10 00:22:03 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-02-08 04:26:35 +08:00
|
|
|
|
|
2020-03-10 00:22:03 +08:00
|
|
|
|
return base.OnHover(e);
|
2020-02-08 04:26:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool OnMouseMove(MouseMoveEvent e)
|
|
|
|
|
{
|
2020-03-10 00:42:35 +08:00
|
|
|
|
if (data?.Length > 1)
|
2020-02-13 03:19:20 +08:00
|
|
|
|
graph.UpdateBallPosition(e.MousePosition.X);
|
2020-02-08 04:26:35 +08:00
|
|
|
|
|
|
|
|
|
return base.OnMouseMove(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnHoverLost(HoverLostEvent e)
|
|
|
|
|
{
|
2020-03-10 00:42:35 +08:00
|
|
|
|
if (data?.Length > 1)
|
2020-02-13 03:19:20 +08:00
|
|
|
|
graph.HideBar();
|
2020-02-08 04:26:35 +08:00
|
|
|
|
|
|
|
|
|
base.OnHoverLost(e);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-13 03:19:20 +08:00
|
|
|
|
protected KeyValuePair<TKey, TValue>[] Data
|
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
value ??= Array.Empty<KeyValuePair<TKey, TValue>>();
|
|
|
|
|
data = value;
|
|
|
|
|
redrawGraph();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void redrawGraph()
|
|
|
|
|
{
|
2020-03-10 00:42:35 +08:00
|
|
|
|
if (!data?.Any() ?? true)
|
2020-02-13 03:19:20 +08:00
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
|
2020-02-08 04:26:35 +08:00
|
|
|
|
public ITooltip GetCustomTooltip() => GetTooltip();
|
|
|
|
|
|
|
|
|
|
protected abstract UserGraphTooltip GetTooltip();
|
|
|
|
|
|
2020-02-13 02:15:37 +08:00
|
|
|
|
public object TooltipContent
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2020-03-10 00:42:35 +08:00
|
|
|
|
if (!data?.Any() ?? true)
|
2020-02-13 02:15:37 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
2020-02-13 03:19:20 +08:00
|
|
|
|
var (key, value) = data[dataIndex];
|
2020-02-13 02:15:37 +08:00
|
|
|
|
return GetTooltipContent(key, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected abstract object GetTooltipContent(TKey key, TValue value);
|
2020-02-08 04:26:35 +08:00
|
|
|
|
|
2020-02-09 05:28:38 +08:00
|
|
|
|
protected class UserLineGraph : LineGraph
|
2020-02-08 04:26:35 +08:00
|
|
|
|
{
|
|
|
|
|
private readonly CircularContainer movingBall;
|
|
|
|
|
private readonly Container bar;
|
|
|
|
|
private readonly Box ballBg;
|
|
|
|
|
private readonly Box line;
|
|
|
|
|
|
|
|
|
|
public Action<int> OnBallMove;
|
|
|
|
|
|
2020-02-09 05:28:38 +08:00
|
|
|
|
public UserLineGraph()
|
2020-02-08 04:26:35 +08:00
|
|
|
|
{
|
|
|
|
|
Add(bar = new Container
|
|
|
|
|
{
|
|
|
|
|
Origin = Anchor.TopCentre,
|
|
|
|
|
RelativeSizeAxes = Axes.Y,
|
|
|
|
|
AutoSizeAxes = Axes.X,
|
|
|
|
|
Alpha = 0,
|
|
|
|
|
RelativePositionAxes = Axes.Both,
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
line = new Box
|
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
RelativeSizeAxes = Axes.Y,
|
2020-03-06 03:34:33 +08:00
|
|
|
|
Width = 2,
|
2020-02-08 04:26:35 +08:00
|
|
|
|
},
|
|
|
|
|
movingBall = new CircularContainer
|
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.TopCentre,
|
|
|
|
|
Origin = Anchor.Centre,
|
2020-03-06 03:34:33 +08:00
|
|
|
|
Size = new Vector2(20),
|
2020-02-08 04:26:35 +08:00
|
|
|
|
Masking = true,
|
|
|
|
|
BorderThickness = 4,
|
|
|
|
|
RelativePositionAxes = Axes.Y,
|
|
|
|
|
Child = ballBg = new Box { RelativeSizeAxes = Axes.Both }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(OverlayColourProvider colourProvider, OsuColour colours)
|
|
|
|
|
{
|
|
|
|
|
ballBg.Colour = colourProvider.Background5;
|
|
|
|
|
movingBall.BorderColour = line.Colour = colours.Yellow;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateBallPosition(float mouseXPosition)
|
|
|
|
|
{
|
|
|
|
|
const int duration = 200;
|
|
|
|
|
int index = calculateIndex(mouseXPosition);
|
|
|
|
|
Vector2 position = calculateBallPosition(index);
|
|
|
|
|
movingBall.MoveToY(position.Y, duration, Easing.OutQuint);
|
|
|
|
|
bar.MoveToX(position.X, duration, Easing.OutQuint);
|
|
|
|
|
OnBallMove.Invoke(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ShowBar() => bar.FadeIn(FADE_DURATION);
|
|
|
|
|
|
|
|
|
|
public void HideBar() => bar.FadeOut(FADE_DURATION);
|
|
|
|
|
|
|
|
|
|
private int calculateIndex(float mouseXPosition) => (int)MathF.Round(mouseXPosition / DrawWidth * (DefaultValueCount - 1));
|
|
|
|
|
|
|
|
|
|
private Vector2 calculateBallPosition(int index)
|
|
|
|
|
{
|
|
|
|
|
float y = GetYPosition(Values.ElementAt(index));
|
|
|
|
|
return new Vector2(index / (float)(DefaultValueCount - 1), y);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected abstract class UserGraphTooltip : VisibilityContainer, ITooltip
|
|
|
|
|
{
|
|
|
|
|
protected readonly OsuSpriteText Counter, BottomText;
|
|
|
|
|
private readonly Box background;
|
|
|
|
|
|
2020-03-10 01:13:59 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Text which will be shown near the <see cref="Counter"/>.
|
|
|
|
|
/// </summary>
|
2020-02-09 05:36:41 +08:00
|
|
|
|
protected abstract string TooltipCounterName { get; }
|
|
|
|
|
|
|
|
|
|
protected UserGraphTooltip()
|
2020-02-08 04:26:35 +08:00
|
|
|
|
{
|
|
|
|
|
AutoSizeAxes = Axes.Both;
|
|
|
|
|
Masking = true;
|
|
|
|
|
CornerRadius = 10;
|
|
|
|
|
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
background = new Box
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both
|
|
|
|
|
},
|
|
|
|
|
new FillFlowContainer
|
|
|
|
|
{
|
|
|
|
|
AutoSizeAxes = Axes.Both,
|
|
|
|
|
Direction = FillDirection.Vertical,
|
|
|
|
|
Padding = new MarginPadding(10),
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
new FillFlowContainer
|
|
|
|
|
{
|
|
|
|
|
AutoSizeAxes = Axes.Both,
|
|
|
|
|
Direction = FillDirection.Horizontal,
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
new OsuSpriteText
|
|
|
|
|
{
|
|
|
|
|
Font = OsuFont.GetFont(size: 12, weight: FontWeight.Bold),
|
2020-02-09 05:36:41 +08:00
|
|
|
|
Text = $"{TooltipCounterName} "
|
2020-02-08 04:26:35 +08:00
|
|
|
|
},
|
|
|
|
|
Counter = new OsuSpriteText
|
|
|
|
|
{
|
|
|
|
|
Font = OsuFont.GetFont(size: 12, weight: FontWeight.Regular),
|
|
|
|
|
Anchor = Anchor.BottomLeft,
|
|
|
|
|
Origin = Anchor.BottomLeft,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
BottomText = new OsuSpriteText
|
|
|
|
|
{
|
|
|
|
|
Font = OsuFont.GetFont(size: 12, weight: FontWeight.Regular),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(OsuColour colours)
|
|
|
|
|
{
|
|
|
|
|
// Temporary colour since it's currently impossible to change it without bugs (see https://github.com/ppy/osu-framework/issues/3231)
|
|
|
|
|
// If above is fixed, this should use OverlayColourProvider
|
|
|
|
|
background.Colour = colours.Gray1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public abstract bool SetContent(object content);
|
|
|
|
|
|
|
|
|
|
private bool instantMove = true;
|
|
|
|
|
|
|
|
|
|
public void Move(Vector2 pos)
|
|
|
|
|
{
|
|
|
|
|
if (instantMove)
|
|
|
|
|
{
|
|
|
|
|
Position = pos;
|
|
|
|
|
instantMove = false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
this.MoveTo(pos, 200, Easing.OutQuint);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void PopIn()
|
|
|
|
|
{
|
|
|
|
|
instantMove |= !IsPresent;
|
|
|
|
|
this.FadeIn(200, Easing.OutQuint);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void PopOut() => this.FadeOut(200, Easing.OutQuint);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|