osu/osu.Game.Rulesets.Osu/UI/Cursor/CursorTrail.cs

352 lines
12 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 09:19:50 +00:00
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using osu.Framework.Allocation;
using osu.Framework.Extensions.EnumExtensions;
2018-04-13 09:19:50 +00:00
using osu.Framework.Graphics;
2019-06-10 11:29:01 +00:00
using osu.Framework.Graphics.Batches;
2018-04-13 09:19:50 +00:00
using osu.Framework.Graphics.OpenGL.Vertices;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.Shaders;
using osu.Framework.Graphics.Textures;
using osu.Framework.Input;
2018-10-02 03:02:47 +00:00
using osu.Framework.Input.Events;
2020-02-24 11:52:15 +00:00
using osu.Framework.Layout;
2018-04-13 09:19:50 +00:00
using osu.Framework.Timing;
2018-11-20 07:51:59 +00:00
using osuTK;
using osuTK.Graphics;
using osuTK.Graphics.ES30;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Rulesets.Osu.UI.Cursor
{
public class CursorTrail : Drawable, IRequireHighFrequencyMousePosition
2018-04-13 09:19:50 +00:00
{
2019-09-09 03:35:15 +00:00
private const int max_sprites = 2048;
2018-04-13 09:19:50 +00:00
2021-08-13 01:27:26 +00:00
/// <summary>
/// An exponentiating factor to ease the trail fade.
/// </summary>
protected virtual float FadeExponent => 1.7f;
2019-09-09 03:35:15 +00:00
private readonly TrailPart[] parts = new TrailPart[max_sprites];
private int currentIndex;
2021-08-13 01:27:26 +00:00
private IShader shader;
2018-04-13 09:19:50 +00:00
private double timeOffset;
private float time;
2021-04-15 14:37:05 +00:00
private Anchor trailOrigin = Anchor.Centre;
protected Anchor TrailOrigin
{
get => trailOrigin;
set
{
trailOrigin = value;
Invalidate(Invalidation.DrawNode);
}
}
2018-04-13 09:19:50 +00:00
public CursorTrail()
{
// as we are currently very dependent on having a running clock, let's make our own clock for the time being.
Clock = new FramedClock();
RelativeSizeAxes = Axes.Both;
for (int i = 0; i < max_sprites; i++)
{
// -1 signals that the part is unusable, and should not be drawn
parts[i].InvalidationID = -1;
2018-04-13 09:19:50 +00:00
}
2020-02-24 11:52:15 +00:00
AddLayout(partSizeCache);
2018-04-13 09:19:50 +00:00
}
[BackgroundDependencyLoader]
private void load(ShaderManager shaders)
2018-04-13 09:19:50 +00:00
{
2021-08-13 01:27:26 +00:00
shader = shaders.Load(@"CursorTrail", FragmentShaderDescriptor.TEXTURE);
2018-04-13 09:19:50 +00:00
}
protected override void LoadComplete()
{
base.LoadComplete();
resetTime();
}
2019-09-09 10:00:42 +00:00
private Texture texture = Texture.WhitePixel;
public Texture Texture
{
get => texture;
set
{
if (texture == value)
return;
texture = value;
Invalidate(Invalidation.DrawNode);
}
}
2020-02-24 11:52:15 +00:00
private readonly LayoutValue<Vector2> partSizeCache = new LayoutValue<Vector2>(Invalidation.DrawInfo | Invalidation.RequiredParentSizeToFit | Invalidation.Presence);
private Vector2 partSize => partSizeCache.IsValid
? partSizeCache.Value
: (partSizeCache.Value = new Vector2(Texture.DisplayWidth, Texture.DisplayHeight) * DrawInfo.Matrix.ExtractScale().Xy);
/// <summary>
/// The amount of time to fade the cursor trail pieces.
/// </summary>
protected virtual double FadeDuration => 300;
2019-09-09 03:35:15 +00:00
public override bool IsPresent => true;
2018-04-13 09:19:50 +00:00
protected override void Update()
{
base.Update();
2020-02-25 02:30:33 +00:00
Invalidate(Invalidation.DrawNode);
2018-04-13 09:19:50 +00:00
const int fade_clock_reset_threshold = 1000000;
time = (float)((Time.Current - timeOffset) / FadeDuration);
2018-04-13 09:19:50 +00:00
if (time > fade_clock_reset_threshold)
resetTime();
}
private void resetTime()
{
for (int i = 0; i < parts.Length; ++i)
{
parts[i].Time -= time;
if (parts[i].InvalidationID != -1)
++parts[i].InvalidationID;
2018-04-13 09:19:50 +00:00
}
time = 0;
timeOffset = Time.Current;
}
/// <summary>
/// Whether to interpolate mouse movements and add trail pieces at intermediate points.
/// </summary>
protected virtual bool InterpolateMovements => true;
protected virtual float IntervalMultiplier => 1.0f;
2021-08-16 19:52:19 +00:00
protected virtual bool AvoidDrawingNearCursor => false;
2019-09-09 03:35:15 +00:00
private Vector2? lastPosition;
private readonly InputResampler resampler = new InputResampler();
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => true;
2018-10-02 03:02:47 +00:00
protected override bool OnMouseMove(MouseMoveEvent e)
2018-04-13 09:19:50 +00:00
{
AddTrail(e.ScreenSpaceMousePosition);
return base.OnMouseMove(e);
}
protected void AddTrail(Vector2 position)
{
if (InterpolateMovements)
{
if (!lastPosition.HasValue)
{
lastPosition = position;
resampler.AddPosition(lastPosition.Value);
return;
}
foreach (Vector2 pos2 in resampler.AddPosition(position))
2018-04-13 09:19:50 +00:00
{
Trace.Assert(lastPosition.HasValue);
Vector2 pos1 = lastPosition.Value;
Vector2 diff = pos2 - pos1;
float distance = diff.Length;
Vector2 direction = diff / distance;
2020-10-02 16:34:49 +00:00
float interval = partSize.X / 2.5f * IntervalMultiplier;
2021-08-16 19:52:19 +00:00
float stopAt = distance - (AvoidDrawingNearCursor ? interval : 0);
2021-08-16 19:52:19 +00:00
for (float d = interval; d < stopAt; d += interval)
{
lastPosition = pos1 + direction * d;
addPart(lastPosition.Value);
}
}
2018-04-13 09:19:50 +00:00
}
else
{
lastPosition = position;
addPart(lastPosition.Value);
}
2018-04-13 09:19:50 +00:00
}
private void addPart(Vector2 screenSpacePosition)
{
parts[currentIndex].Position = screenSpacePosition;
parts[currentIndex].Time = time + 1;
++parts[currentIndex].InvalidationID;
currentIndex = (currentIndex + 1) % max_sprites;
}
2019-09-09 03:35:15 +00:00
protected override DrawNode CreateDrawNode() => new TrailDrawNode(this);
2018-04-13 09:19:50 +00:00
private struct TrailPart
{
public Vector2 Position;
public float Time;
public long InvalidationID;
}
private class TrailDrawNode : DrawNode
{
2019-04-02 02:56:22 +00:00
protected new CursorTrail Source => (CursorTrail)base.Source;
2018-04-13 09:19:50 +00:00
2019-04-02 02:56:22 +00:00
private IShader shader;
private Texture texture;
private float time;
2021-08-13 01:27:26 +00:00
private float fadeExponent;
2018-04-13 09:19:50 +00:00
2019-04-02 02:56:22 +00:00
private readonly TrailPart[] parts = new TrailPart[max_sprites];
private Vector2 size;
private Vector2 originPosition;
private readonly QuadBatch<TexturedTrailVertex> vertexBatch = new QuadBatch<TexturedTrailVertex>(max_sprites, 1);
2019-04-02 02:56:22 +00:00
public TrailDrawNode(CursorTrail source)
: base(source)
2018-04-13 09:19:50 +00:00
{
2019-04-02 02:56:22 +00:00
}
public override void ApplyState()
{
base.ApplyState();
2021-08-13 01:27:26 +00:00
shader = Source.shader;
2019-04-02 02:56:22 +00:00
texture = Source.texture;
size = Source.partSize;
2019-04-02 02:56:22 +00:00
time = Source.time;
2021-08-13 01:27:26 +00:00
fadeExponent = Source.FadeExponent;
2019-04-02 02:56:22 +00:00
originPosition = Vector2.Zero;
if (Source.TrailOrigin.HasFlagFast(Anchor.x1))
originPosition.X = 0.5f;
else if (Source.TrailOrigin.HasFlagFast(Anchor.x2))
originPosition.X = 1f;
if (Source.TrailOrigin.HasFlagFast(Anchor.y1))
originPosition.Y = 0.5f;
else if (Source.TrailOrigin.HasFlagFast(Anchor.y2))
originPosition.Y = 1f;
Source.parts.CopyTo(parts, 0);
2018-04-13 09:19:50 +00:00
}
public override void Draw(Action<TexturedVertex2D> vertexAction)
{
2019-06-10 11:29:01 +00:00
base.Draw(vertexAction);
2018-04-13 09:19:50 +00:00
2019-06-10 11:29:01 +00:00
shader.Bind();
shader.GetUniform<float>("g_FadeClock").UpdateValue(ref time);
2021-08-13 01:27:26 +00:00
shader.GetUniform<float>("g_FadeExponent").UpdateValue(ref fadeExponent);
2019-04-01 03:16:05 +00:00
texture.TextureGL.Bind();
RectangleF textureRect = texture.GetTextureRect();
foreach (var part in parts)
2018-04-13 09:19:50 +00:00
{
if (part.InvalidationID == -1)
continue;
if (time - part.Time >= 1)
continue;
vertexBatch.Add(new TexturedTrailVertex
{
Position = new Vector2(part.Position.X - size.X * originPosition.X, part.Position.Y + size.Y * (1 - originPosition.Y)),
TexturePosition = textureRect.BottomLeft,
2020-06-02 14:47:18 +00:00
TextureRect = new Vector4(0, 0, 1, 1),
Colour = DrawColourInfo.Colour.BottomLeft.Linear,
Time = part.Time
});
vertexBatch.Add(new TexturedTrailVertex
{
Position = new Vector2(part.Position.X + size.X * (1 - originPosition.X), part.Position.Y + size.Y * (1 - originPosition.Y)),
TexturePosition = textureRect.BottomRight,
2020-06-02 14:47:18 +00:00
TextureRect = new Vector4(0, 0, 1, 1),
Colour = DrawColourInfo.Colour.BottomRight.Linear,
Time = part.Time
});
2019-06-10 11:29:01 +00:00
vertexBatch.Add(new TexturedTrailVertex
{
Position = new Vector2(part.Position.X + size.X * (1 - originPosition.X), part.Position.Y - size.Y * originPosition.Y),
TexturePosition = textureRect.TopRight,
2020-06-02 14:47:18 +00:00
TextureRect = new Vector4(0, 0, 1, 1),
Colour = DrawColourInfo.Colour.TopRight.Linear,
Time = part.Time
});
2019-07-16 08:50:03 +00:00
vertexBatch.Add(new TexturedTrailVertex
{
Position = new Vector2(part.Position.X - size.X * originPosition.X, part.Position.Y - size.Y * originPosition.Y),
TexturePosition = textureRect.TopLeft,
2020-06-02 14:47:18 +00:00
TextureRect = new Vector4(0, 0, 1, 1),
Colour = DrawColourInfo.Colour.TopLeft.Linear,
Time = part.Time
});
2018-04-13 09:19:50 +00:00
}
vertexBatch.Draw();
2019-04-02 02:56:22 +00:00
shader.Unbind();
2018-04-13 09:19:50 +00:00
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
2019-06-10 11:29:01 +00:00
vertexBatch.Dispose();
}
2018-04-13 09:19:50 +00:00
}
[StructLayout(LayoutKind.Sequential)]
public struct TexturedTrailVertex : IEquatable<TexturedTrailVertex>, IVertex
{
[VertexMember(2, VertexAttribPointerType.Float)]
public Vector2 Position;
2019-02-28 04:31:40 +00:00
2018-04-13 09:19:50 +00:00
[VertexMember(4, VertexAttribPointerType.Float)]
public Color4 Colour;
2019-02-28 04:31:40 +00:00
2018-04-13 09:19:50 +00:00
[VertexMember(2, VertexAttribPointerType.Float)]
public Vector2 TexturePosition;
2019-02-28 04:31:40 +00:00
2020-06-02 14:47:18 +00:00
[VertexMember(4, VertexAttribPointerType.Float)]
public Vector4 TextureRect;
2018-04-13 09:19:50 +00:00
[VertexMember(1, VertexAttribPointerType.Float)]
public float Time;
public bool Equals(TexturedTrailVertex other)
{
return Position.Equals(other.Position)
&& TexturePosition.Equals(other.TexturePosition)
&& Colour.Equals(other.Colour)
&& Time.Equals(other.Time);
}
}
}
}