1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-23 00:07:24 +08:00
osu-lazer/osu.Game.Rulesets.Osu/Skinning/Default/SliderBody.cs

123 lines
3.8 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;
2018-04-13 17:19:50 +08:00
using System.Collections.Generic;
using osu.Framework.Graphics;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Lines;
2018-11-20 15:51:59 +08:00
using osuTK;
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
2020-12-04 19:21:53 +08:00
namespace osu.Game.Rulesets.Osu.Skinning.Default
2018-04-13 17:19:50 +08:00
{
public abstract class SliderBody : CompositeDrawable
2018-04-13 17:19:50 +08:00
{
private DrawableSliderPath path;
protected Path Path => path;
public virtual float PathRadius
2018-04-13 17:19:50 +08:00
{
2019-03-07 16:39:19 +08:00
get => path.PathRadius;
set => path.PathRadius = value;
2018-04-13 17:19:50 +08:00
}
/// <summary>
/// Offset in absolute coordinates from the start of the curve.
/// </summary>
public virtual Vector2 PathOffset => path.PositionInBoundingBox(path.Vertices[0]);
2018-04-13 17:19:50 +08:00
/// <summary>
/// Used to colour the path.
/// </summary>
public Color4 AccentColour
{
2018-10-05 14:45:45 +08:00
get => path.AccentColour;
2018-04-13 17:19:50 +08:00
set
{
2018-10-05 14:45:45 +08:00
if (path.AccentColour == value)
2018-04-13 17:19:50 +08:00
return;
2019-02-28 12:31:40 +08:00
2018-10-05 14:45:45 +08:00
path.AccentColour = value;
2018-04-13 17:19:50 +08:00
}
}
/// <summary>
/// Used to colour the path border.
/// </summary>
public new Color4 BorderColour
{
2018-10-05 14:45:45 +08:00
get => path.BorderColour;
2018-04-13 17:19:50 +08:00
set
{
2018-10-05 14:45:45 +08:00
if (path.BorderColour == value)
2018-04-13 17:19:50 +08:00
return;
2019-02-28 12:31:40 +08:00
2018-10-05 14:45:45 +08:00
path.BorderColour = value;
2018-04-13 17:19:50 +08:00
}
}
2019-03-15 03:57:39 +08:00
/// <summary>
/// Used to size the path border.
/// </summary>
public float BorderSize
{
2019-03-15 03:57:39 +08:00
get => path.BorderSize;
2019-05-12 21:53:12 +08:00
set
{
2019-03-15 03:57:39 +08:00
if (path.BorderSize == value)
return;
path.BorderSize = value;
}
}
protected SliderBody()
2018-04-13 17:19:50 +08:00
{
RecyclePath();
2018-04-13 17:19:50 +08:00
}
/// <summary>
/// Initialises a new <see cref="DrawableSliderPath"/>, releasing all resources retained by the old one.
/// </summary>
public virtual void RecyclePath()
{
InternalChild = path = CreateSliderPath().With(p =>
{
p.Position = path?.Position ?? Vector2.Zero;
p.PathRadius = path?.PathRadius ?? 10;
p.AccentColour = path?.AccentColour ?? Color4.White;
p.BorderColour = path?.BorderColour ?? Color4.White;
p.BorderSize = path?.BorderSize ?? 1;
p.Vertices = path?.Vertices ?? Array.Empty<Vector2>();
});
}
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => path.ReceivePositionalInputAt(screenSpacePos);
2018-04-13 17:19:50 +08:00
/// <summary>
/// Sets the vertices of the path which should be drawn by this <see cref="SliderBody"/>.
/// </summary>
/// <param name="vertices">The vertices</param>
protected void SetVertices(IReadOnlyList<Vector2> vertices) => path.Vertices = vertices;
2018-10-05 14:45:45 +08:00
protected virtual DrawableSliderPath CreateSliderPath() => new DefaultDrawableSliderPath();
2018-10-05 14:45:45 +08:00
private class DefaultDrawableSliderPath : DrawableSliderPath
{
2018-10-05 14:45:45 +08:00
private const float opacity_at_centre = 0.3f;
private const float opacity_at_edge = 0.8f;
protected override Color4 ColourAt(float position)
{
if (CalculatedBorderPortion != 0f && position <= CalculatedBorderPortion)
2018-10-05 14:45:45 +08:00
return BorderColour;
position -= CalculatedBorderPortion;
return new Color4(AccentColour.R, AccentColour.G, AccentColour.B, (opacity_at_edge - (opacity_at_edge - opacity_at_centre) * position / GRADIENT_PORTION) * AccentColour.A);
2018-10-05 14:45:45 +08:00
}
}
2018-04-13 17:19:50 +08:00
}
}