1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-14 01:07:42 +08:00
osu-lazer/osu.Game.Rulesets.Osu/UI/Cursor/CursorTrail.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

357 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
2022-06-17 15:37:17 +08:00
#nullable disable
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
2016-11-26 18:22:56 +08:00
using osu.Framework.Allocation;
using osu.Framework.Extensions.EnumExtensions;
2016-11-26 18:22:56 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Primitives;
2022-07-29 21:33:34 +08:00
using osu.Framework.Graphics.Rendering;
2022-08-05 19:36:28 +08:00
using osu.Framework.Graphics.Rendering.Vertices;
2016-11-26 18:22:56 +08:00
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;
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
2016-11-26 18:22:56 +08:00
{
public partial class CursorTrail : Drawable, IRequireHighFrequencyMousePosition
2016-11-26 18:22:56 +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;
2016-11-26 18:22:56 +08:00
private double timeOffset;
private float time;
2018-04-13 17:19:50 +08:00
2021-04-15 22:37:05 +08:00
private Anchor trailOrigin = Anchor.Centre;
protected Anchor TrailOrigin
{
get => trailOrigin;
set
{
trailOrigin = value;
Invalidate(Invalidation.DrawNode);
}
}
2016-11-26 18:22:56 +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();
2018-04-13 17:19:50 +08:00
2016-11-26 18:22:56 +08:00
RelativeSizeAxes = Axes.Both;
2018-04-13 17:19:50 +08:00
2017-02-07 15:15:45 +08:00
for (int i = 0; i < max_sprites; i++)
{
// -1 signals that the part is unusable, and should not be drawn
parts[i].InvalidationID = -1;
}
2020-02-24 19:52:15 +08:00
AddLayout(partSizeCache);
2016-11-26 18:22:56 +08:00
}
2018-04-13 17:19:50 +08:00
2016-11-26 18:22:56 +08:00
[BackgroundDependencyLoader]
2022-08-02 18:50:57 +08:00
private void load(IRenderer renderer, ShaderManager shaders)
2016-11-26 18:22:56 +08:00
{
2022-08-02 18:50:57 +08:00
texture ??= renderer.WhitePixel;
2021-08-13 09:27:26 +08:00
shader = shaders.Load(@"CursorTrail", FragmentShaderDescriptor.TEXTURE);
2016-11-26 18:22:56 +08:00
}
2018-04-13 17:19:50 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
resetTime();
}
2018-04-13 17:19:50 +08:00
2022-08-02 18:50:57 +08:00
private Texture texture;
2019-09-09 18:00:42 +08:00
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;
2016-11-26 18:22:56 +08:00
protected override void Update()
{
base.Update();
2018-04-13 17:19:50 +08:00
2020-02-25 10:30:33 +08:00
Invalidate(Invalidation.DrawNode);
2018-04-13 17:19:50 +08:00
2017-03-23 12:52:38 +08:00
const int fade_clock_reset_threshold = 1000000;
2018-04-13 17:19:50 +08:00
time = (float)((Time.Current - timeOffset) / FadeDuration);
2017-03-23 12:52:38 +08:00
if (time > fade_clock_reset_threshold)
2017-02-07 15:15:45 +08:00
resetTime();
2016-11-26 18:22:56 +08:00
}
2018-04-13 17:19:50 +08:00
2017-02-07 15:15:45 +08:00
private void resetTime()
2016-11-26 18:22:56 +08:00
{
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
2016-11-26 18:22:56 +08:00
time = 0;
timeOffset = Time.Current;
}
2018-04-13 17:19:50 +08:00
/// <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)
2016-11-26 18:25:58 +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))
2017-05-04 00:33:42 +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);
}
}
2016-11-26 18:25:58 +08:00
}
else
{
lastPosition = position;
addPart(lastPosition.Value);
}
2016-11-26 18:25:58 +08:00
}
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
2017-03-07 09:59:19 +08:00
private struct TrailPart
{
public Vector2 Position;
public float Time;
public long InvalidationID;
}
2018-04-13 17:19:50 +08:00
2017-03-07 09:59:19 +08:00
private class TrailDrawNode : DrawNode
2016-11-26 18:22:56 +08:00
{
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;
2022-07-29 22:32:06 +08:00
private IVertexBatch<TexturedTrailVertex> vertexBatch;
2019-04-02 10:56:22 +08:00
public TrailDrawNode(CursorTrail source)
: base(source)
{
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
2022-07-29 21:33:34 +08:00
public override void Draw(IRenderer renderer)
2016-11-26 18:22:56 +08:00
{
2022-07-29 21:33:34 +08:00
base.Draw(renderer);
2018-04-13 17:19:50 +08:00
2022-07-29 22:32:06 +08:00
vertexBatch ??= renderer.CreateQuadBatch<TexturedTrailVertex>(max_sprites, 1);
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
2022-08-02 18:50:57 +08:00
texture.Bind();
RectangleF textureRect = texture.GetTextureRect();
foreach (var part in parts)
2016-11-26 21:08:43 +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
});
2016-11-26 21:08:43 +08:00
}
2018-04-13 17:19:50 +08:00
vertexBatch.Draw();
2019-04-02 10:56:22 +08:00
shader.Unbind();
2016-11-26 18:22:56 +08:00
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
2022-07-29 22:32:06 +08:00
vertexBatch?.Dispose();
}
2016-11-26 18:22:56 +08:00
}
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
[VertexMember(4, VertexAttribPointerType.Float)]
public Color4 Colour;
2019-02-28 12:31:40 +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;
[VertexMember(1, VertexAttribPointerType.Float)]
public float Time;
2018-04-13 17:19:50 +08:00
public bool Equals(TexturedTrailVertex other)
{
return Position.Equals(other.Position)
&& TexturePosition.Equals(other.TexturePosition)
&& Colour.Equals(other.Colour)
&& Time.Equals(other.Time);
}
}
2016-11-26 18:22:56 +08:00
}
}