2019-01-24 16:43:03 +08:00
|
|
|
|
// 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;
|
2019-09-09 18:30:31 +08:00
|
|
|
|
using osu.Framework.Caching;
|
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;
|
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
|
|
|
|
|
{
|
2019-09-09 16:53:51 +08:00
|
|
|
|
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
|
|
|
|
|
2019-09-09 11:35:15 +08:00
|
|
|
|
private readonly TrailPart[] parts = new TrailPart[max_sprites];
|
|
|
|
|
private int currentIndex;
|
2019-03-07 17:30:18 +08:00
|
|
|
|
private IShader shader;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
private double timeOffset;
|
|
|
|
|
private float time;
|
|
|
|
|
|
|
|
|
|
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++)
|
|
|
|
|
{
|
2019-06-07 09:36:36 +08:00
|
|
|
|
// InvalidationID 1 forces an update of each part of the cursor trail the first time ApplyState is run on the draw node
|
|
|
|
|
// This is to prevent garbage data from being sent to the vertex shader, resulting in visual issues on some platforms
|
2019-06-06 17:07:19 +08:00
|
|
|
|
parts[i].InvalidationID = 1;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
2019-09-09 16:53:51 +08:00
|
|
|
|
private void load(ShaderManager shaders)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2019-03-07 17:30:18 +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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 18:30:31 +08:00
|
|
|
|
private readonly Cached<Vector2> partSizeCache = new Cached<Vector2>();
|
|
|
|
|
|
|
|
|
|
private Vector2 partSize => partSizeCache.IsValid
|
|
|
|
|
? partSizeCache.Value
|
|
|
|
|
: (partSizeCache.Value = new Vector2(Texture.DisplayWidth, Texture.DisplayHeight) * DrawInfo.Matrix.ExtractScale().Xy);
|
|
|
|
|
|
|
|
|
|
public override bool Invalidate(Invalidation invalidation = Invalidation.All, Drawable source = null, bool shallPropagate = true)
|
|
|
|
|
{
|
|
|
|
|
if ((invalidation & (Invalidation.DrawInfo | Invalidation.RequiredParentSizeToFit | Invalidation.Presence)) > 0)
|
|
|
|
|
partSizeCache.Invalidate();
|
|
|
|
|
|
|
|
|
|
return base.Invalidate(invalidation, source, shallPropagate);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 18:02:10 +08:00
|
|
|
|
/// <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();
|
|
|
|
|
|
|
|
|
|
Invalidate(Invalidation.DrawNode, shallPropagate: false);
|
|
|
|
|
|
|
|
|
|
const int fade_clock_reset_threshold = 1000000;
|
|
|
|
|
|
2019-09-09 18:02:10 +08:00
|
|
|
|
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;
|
|
|
|
|
++parts[i].InvalidationID;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
time = 0;
|
|
|
|
|
timeOffset = Time.Current;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 18:22:27 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether to interpolate mouse movements and add trail pieces at intermediate points.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual bool InterpolateMovements => true;
|
|
|
|
|
|
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
|
|
|
|
{
|
2018-09-19 19:52:57 +08:00
|
|
|
|
Vector2 pos = e.ScreenSpaceMousePosition;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
if (lastPosition == null)
|
|
|
|
|
{
|
|
|
|
|
lastPosition = pos;
|
|
|
|
|
resampler.AddPosition(lastPosition.Value);
|
2018-10-02 11:02:47 +08:00
|
|
|
|
return base.OnMouseMove(e);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (Vector2 pos2 in resampler.AddPosition(pos))
|
|
|
|
|
{
|
|
|
|
|
Trace.Assert(lastPosition.HasValue);
|
|
|
|
|
|
2019-09-09 18:22:27 +08:00
|
|
|
|
if (InterpolateMovements)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2019-09-09 18:22:27 +08:00
|
|
|
|
// ReSharper disable once PossibleInvalidOperationException
|
|
|
|
|
Vector2 pos1 = lastPosition.Value;
|
|
|
|
|
Vector2 diff = pos2 - pos1;
|
|
|
|
|
float distance = diff.Length;
|
|
|
|
|
Vector2 direction = diff / distance;
|
|
|
|
|
|
2019-09-11 12:28:46 +08:00
|
|
|
|
float interval = partSize.X / 2.5f;
|
2019-09-09 18:22:27 +08:00
|
|
|
|
|
|
|
|
|
for (float d = interval; d < distance; d += interval)
|
|
|
|
|
{
|
|
|
|
|
lastPosition = pos1 + direction * d;
|
|
|
|
|
addPart(lastPosition.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
lastPosition = pos2;
|
|
|
|
|
addPart(lastPosition.Value);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 11:02:47 +08:00
|
|
|
|
return base.OnMouseMove(e);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 18:22:27 +08:00
|
|
|
|
private void addPart(Vector2 screenSpacePosition)
|
|
|
|
|
{
|
|
|
|
|
parts[currentIndex].Position = screenSpacePosition;
|
|
|
|
|
parts[currentIndex].Time = time;
|
|
|
|
|
++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;
|
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;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-07-16 16:50:03 +08:00
|
|
|
|
private readonly TrailBatch vertexBatch = new TrailBatch(max_sprites, 1);
|
2019-03-01 12:01:52 +08:00
|
|
|
|
|
2019-04-02 10:56:22 +08:00
|
|
|
|
public TrailDrawNode(CursorTrail source)
|
|
|
|
|
: base(source)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < max_sprites; i++)
|
2019-04-02 10:56:22 +08:00
|
|
|
|
parts[i].InvalidationID = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void ApplyState()
|
|
|
|
|
{
|
|
|
|
|
base.ApplyState();
|
|
|
|
|
|
|
|
|
|
shader = Source.shader;
|
|
|
|
|
texture = Source.texture;
|
2019-09-09 18:30:31 +08:00
|
|
|
|
size = Source.partSize;
|
2019-04-02 10:56:22 +08:00
|
|
|
|
time = Source.time;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < Source.parts.Length; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (Source.parts[i].InvalidationID > parts[i].InvalidationID)
|
|
|
|
|
parts[i] = Source.parts[i];
|
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);
|
2019-04-01 11:16:05 +08:00
|
|
|
|
|
2019-04-02 10:56:22 +08:00
|
|
|
|
for (int i = 0; i < parts.Length; ++i)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2019-07-16 16:33:14 +08:00
|
|
|
|
vertexBatch.DrawTime = parts[i].Time;
|
2019-06-10 19:29:01 +08:00
|
|
|
|
|
2019-07-16 16:50:03 +08:00
|
|
|
|
Vector2 pos = parts[i].Position;
|
|
|
|
|
|
2019-06-10 19:29:01 +08:00
|
|
|
|
DrawQuad(
|
|
|
|
|
texture,
|
|
|
|
|
new Quad(pos.X - size.X / 2, pos.Y - size.Y / 2, size.X, size.Y),
|
|
|
|
|
DrawColourInfo.Colour,
|
|
|
|
|
null,
|
2019-07-16 16:33:14 +08:00
|
|
|
|
vertexBatch.AddAction);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-02 10:56:22 +08:00
|
|
|
|
shader.Unbind();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
2019-03-01 12:01:52 +08:00
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
|
2019-06-10 19:29:01 +08:00
|
|
|
|
vertexBatch.Dispose();
|
2019-03-01 12:01:52 +08:00
|
|
|
|
}
|
2019-07-16 16:33:14 +08:00
|
|
|
|
|
2019-07-16 16:50:03 +08:00
|
|
|
|
// Todo: This shouldn't exist, but is currently used to reduce allocations by caching variable-capturing closures.
|
|
|
|
|
private class TrailBatch : QuadBatch<TexturedTrailVertex>
|
2019-07-16 16:33:14 +08:00
|
|
|
|
{
|
|
|
|
|
public new readonly Action<TexturedVertex2D> AddAction;
|
|
|
|
|
public float DrawTime;
|
|
|
|
|
|
2019-07-16 16:50:03 +08:00
|
|
|
|
public TrailBatch(int size, int maxBuffers)
|
2019-07-16 16:33:14 +08:00
|
|
|
|
: base(size, maxBuffers)
|
|
|
|
|
{
|
2019-07-16 16:50:03 +08:00
|
|
|
|
AddAction = v => Add(new TexturedTrailVertex
|
|
|
|
|
{
|
|
|
|
|
Position = v.Position,
|
|
|
|
|
TexturePosition = v.TexturePosition,
|
|
|
|
|
Time = DrawTime + 1,
|
|
|
|
|
Colour = v.Colour,
|
|
|
|
|
});
|
2019-07-16 16:33:14 +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
|
|
|
|
|
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
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|