1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-19 03:12:54 +08:00
osu-lazer/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 17:19:50 +08:00
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using osu.Framework.Allocation;
using osu.Framework.Extensions.EnumExtensions;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
2019-06-10 19:29:01 +08:00
using osu.Framework.Graphics.Batches;
2018-04-13 17:19:50 +08: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 11:02:47 +08:00
using osu.Framework.Input.Events;
2020-02-24 19:52:15 +08:00
using osu.Framework.Layout;
2018-04-13 17:19:50 +08:00
using osu.Framework.Timing;
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.UI.Cursor
{
public class CursorTrail : Drawable, IRequireHighFrequencyMousePosition
2018-04-13 17:19:50 +08:00
{
2019-09-09 11:35:15 +08:00
private const int max_sprites = 2048;
2018-04-13 17:19:50 +08:00
2021-08-13 09:27:26 +08:00
/// <summary>
/// An exponentiating factor to ease the trail fade.
/// </summary>
protected virtual float FadeExponent => 1.7f;
2019-09-09 11:35:15 +08:00
private readonly TrailPart[] parts = new TrailPart[max_sprites];
private int currentIndex;
2021-08-13 09:27:26 +08:00
private IShader shader;
2018-04-13 17:19:50 +08:00
private double timeOffset;
private float time;
2021-04-15 22:37:05 +08:00
private Anchor trailOrigin = Anchor.Centre;
protected Anchor TrailOrigin
{
get => trailOrigin;
set
{
trailOrigin = value;
Invalidate(Invalidation.DrawNode);
}
}
2018-04-13 17:19:50 +08: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 17:19:50 +08:00
}
2020-02-24 19:52:15 +08:00
AddLayout(partSizeCache);
2018-04-13 17:19:50 +08:00
}
[BackgroundDependencyLoader]
private void load(ShaderManager shaders)
2018-04-13 17:19:50 +08:00
{
2021-08-13 09:27:26 +08:00
shader = shaders.Load(@"CursorTrail", FragmentShaderDescriptor.TEXTURE);
2018-04-13 17:19:50 +08:00
}
protected override void LoadComplete()
{
base.LoadComplete();
resetTime();
}
2019-09-09 18:00:42 +08:00
private Texture texture = Texture.WhitePixel;
public Texture Texture
{
get => texture;
set
{
if (texture == value)
return;
texture = value;
Invalidate(Invalidation.DrawNode);
}
}
2020-02-24 19:52:15 +08: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 11:35:15 +08:00
public override bool IsPresent => true;
2018-04-13 17:19:50 +08:00
protected override void Update()
{
base.Update();
2020-02-25 10:30:33 +08:00
Invalidate(Invalidation.DrawNode);
2018-04-13 17:19:50 +08:00
const int fade_clock_reset_threshold = 1000000;
time = (float)((Time.Current - timeOffset) / FadeDuration);
2018-04-13 17:19:50 +08: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 17:19:50 +08: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-17 03:52:19 +08:00
protected virtual bool AvoidDrawingNearCursor => false;
2019-09-09 11:35:15 +08:00
private Vector2? lastPosition;
private readonly InputResampler resampler = new InputResampler();
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => true;
2018-10-02 11:02:47 +08:00
protected override bool OnMouseMove(MouseMoveEvent e)
2018-04-13 17:19:50 +08: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 17:19:50 +08:00
{
Trace.Assert(lastPosition.HasValue);
Vector2 pos1 = lastPosition.Value;
Vector2 diff = pos2 - pos1;
float distance = diff.Length;
Vector2 direction = diff / distance;
2020-10-03 00:34:49 +08:00
float interval = partSize.X / 2.5f * IntervalMultiplier;
2021-08-17 03:52:19 +08:00
float stopAt = distance - (AvoidDrawingNearCursor ? interval : 0);
2021-08-17 03:52:19 +08:00
for (float d = interval; d < stopAt; d += interval)
{
lastPosition = pos1 + direction * d;
addPart(lastPosition.Value);
}
}
2018-04-13 17:19:50 +08:00
}
else
{
lastPosition = position;
addPart(lastPosition.Value);
}
2018-04-13 17:19:50 +08: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 11:35:15 +08:00
protected override DrawNode CreateDrawNode() => new TrailDrawNode(this);
2018-04-13 17:19:50 +08:00
private struct TrailPart
{
public Vector2 Position;
public float Time;
public long InvalidationID;
}
private class TrailDrawNode : DrawNode
{
2019-04-02 10:56:22 +08:00
protected new CursorTrail Source => (CursorTrail)base.Source;
2018-04-13 17:19:50 +08:00
2019-04-02 10:56:22 +08:00
private IShader shader;
private Texture texture;
private float time;
2021-08-13 09:27:26 +08:00
private float fadeExponent;
2018-04-13 17:19:50 +08:00
2019-04-02 10:56:22 +08: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 10:56:22 +08:00
public TrailDrawNode(CursorTrail source)
: base(source)
2018-04-13 17:19:50 +08:00
{
2019-04-02 10:56:22 +08:00
}
public override void ApplyState()
{
base.ApplyState();
2021-08-13 09:27:26 +08:00
shader = Source.shader;
2019-04-02 10:56:22 +08:00
texture = Source.texture;
size = Source.partSize;
2019-04-02 10:56:22 +08:00
time = Source.time;
2021-08-13 09:27:26 +08:00
fadeExponent = Source.FadeExponent;
2019-04-02 10:56:22 +08: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 17:19:50 +08:00
}
public override void Draw(Action<TexturedVertex2D> vertexAction)
{
2019-06-10 19:29:01 +08:00
base.Draw(vertexAction);
2018-04-13 17:19:50 +08:00
2019-06-10 19:29:01 +08:00
shader.Bind();
shader.GetUniform<float>("g_FadeClock").UpdateValue(ref time);
2021-08-13 09:27:26 +08:00
shader.GetUniform<float>("g_FadeExponent").UpdateValue(ref fadeExponent);
2019-04-01 11:16:05 +08:00
texture.TextureGL.Bind();
RectangleF textureRect = texture.GetTextureRect();
foreach (var part in parts)
2018-04-13 17:19:50 +08: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 22:47:18 +08: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 22:47:18 +08:00
TextureRect = new Vector4(0, 0, 1, 1),
Colour = DrawColourInfo.Colour.BottomRight.Linear,
Time = part.Time
});
2019-06-10 19:29:01 +08: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 22:47:18 +08:00
TextureRect = new Vector4(0, 0, 1, 1),
Colour = DrawColourInfo.Colour.TopRight.Linear,
Time = part.Time
});
2019-07-16 16:50:03 +08: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 22:47:18 +08:00
TextureRect = new Vector4(0, 0, 1, 1),
Colour = DrawColourInfo.Colour.TopLeft.Linear,
Time = part.Time
});
2018-04-13 17:19:50 +08:00
}
vertexBatch.Draw();
2019-04-02 10:56:22 +08:00
shader.Unbind();
2018-04-13 17:19:50 +08:00
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
2019-06-10 19:29:01 +08:00
vertexBatch.Dispose();
}
2018-04-13 17:19:50 +08:00
}
[StructLayout(LayoutKind.Sequential)]
public struct TexturedTrailVertex : IEquatable<TexturedTrailVertex>, IVertex
{
[VertexMember(2, VertexAttribPointerType.Float)]
public Vector2 Position;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
[VertexMember(4, VertexAttribPointerType.Float)]
public Color4 Colour;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
[VertexMember(2, VertexAttribPointerType.Float)]
public Vector2 TexturePosition;
2019-02-28 12:31:40 +08:00
2020-06-02 22:47:18 +08:00
[VertexMember(4, VertexAttribPointerType.Float)]
public Vector4 TextureRect;
2018-04-13 17:19:50 +08: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);
}
}
}
}