1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 09:32:55 +08:00
osu-lazer/osu.Game.Rulesets.Osu/Objects/Drawables/Pieces/SliderBody.cs

140 lines
4.1 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
2018-04-13 17:19:50 +08:00
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System.Collections.Generic;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Lines;
using osu.Framework.Graphics.Primitives;
2018-11-20 15:51:59 +08:00
using osuTK;
using osuTK.Graphics;
using osuTK.Graphics.ES30;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Rulesets.Osu.Objects.Drawables.Pieces
{
public abstract class SliderBody : CompositeDrawable
2018-04-13 17:19:50 +08:00
{
2018-10-05 14:45:45 +08:00
private readonly SliderPath path;
protected Path Path => path;
2018-04-13 17:19:50 +08:00
private readonly BufferedContainer container;
public float PathWidth
{
2018-10-05 14:45:45 +08:00
get => path.PathWidth;
set => path.PathWidth = 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;
2018-10-05 14:45:45 +08:00
path.AccentColour = value;
2018-04-13 17:19:50 +08:00
2018-10-05 14:45:45 +08:00
container.ForceRedraw();
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;
2018-10-05 14:45:45 +08:00
path.BorderColour = value;
2018-04-13 17:19:50 +08:00
2018-10-05 14:45:45 +08:00
container.ForceRedraw();
2018-04-13 17:19:50 +08:00
}
}
public Quad PathDrawQuad => container.ScreenSpaceDrawQuad;
protected SliderBody()
2018-04-13 17:19:50 +08:00
{
InternalChild = container = new BufferedContainer
2018-04-13 17:19:50 +08:00
{
RelativeSizeAxes = Axes.Both,
CacheDrawnFrameBuffer = true,
Child = path = new SliderPath { Blending = BlendingMode.None }
2018-04-13 17:19:50 +08:00
};
container.Attach(RenderbufferInternalFormat.DepthComponent16);
}
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)
2018-04-13 17:19:50 +08:00
{
path.Vertices = vertices;
container.ForceRedraw();
2018-04-13 17:19:50 +08:00
}
2018-10-05 14:45:45 +08:00
private class SliderPath : SmoothPath
{
private const float border_portion = 0.128f;
private const float gradient_portion = 1 - border_portion;
private const float opacity_at_centre = 0.3f;
private const float opacity_at_edge = 0.8f;
private Color4 borderColour = Color4.White;
public Color4 BorderColour
{
get => borderColour;
set
{
if (borderColour == value)
return;
borderColour = value;
InvalidateTexture();
}
}
private Color4 accentColour = Color4.White;
public Color4 AccentColour
{
get => accentColour;
set
{
if (accentColour == value)
return;
accentColour = value;
InvalidateTexture();
}
}
protected override Color4 ColourAt(float position)
{
if (position <= border_portion)
return BorderColour;
position -= border_portion;
return new Color4(AccentColour.R, AccentColour.G, AccentColour.B, (opacity_at_edge - (opacity_at_edge - opacity_at_centre) * position / gradient_portion) * AccentColour.A);
}
}
2018-04-13 17:19:50 +08:00
}
}