2019-01-24 08:43:03 +00: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 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using osu.Framework.Allocation;
|
2021-04-14 05:20:18 +00:00
|
|
|
|
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
|
|
|
|
|
{
|
2019-09-09 08:53:51 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-14 05:20:18 +00:00
|
|
|
|
|
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++)
|
|
|
|
|
{
|
2019-09-25 07:25:15 +00:00
|
|
|
|
// -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]
|
2020-10-02 16:34:06 +00:00
|
|
|
|
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);
|
2019-09-09 10:30:31 +00:00
|
|
|
|
|
|
|
|
|
private Vector2 partSize => partSizeCache.IsValid
|
|
|
|
|
? partSizeCache.Value
|
|
|
|
|
: (partSizeCache.Value = new Vector2(Texture.DisplayWidth, Texture.DisplayHeight) * DrawInfo.Matrix.ExtractScale().Xy);
|
|
|
|
|
|
2019-09-09 10:02:10 +00:00
|
|
|
|
/// <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;
|
|
|
|
|
|
2019-09-09 10:02:10 +00:00
|
|
|
|
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;
|
2019-09-25 07:25:15 +00:00
|
|
|
|
|
|
|
|
|
if (parts[i].InvalidationID != -1)
|
|
|
|
|
++parts[i].InvalidationID;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
time = 0;
|
|
|
|
|
timeOffset = Time.Current;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 10:22:27 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether to interpolate mouse movements and add trail pieces at intermediate points.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual bool InterpolateMovements => true;
|
|
|
|
|
|
2020-10-02 16:34:06 +00:00
|
|
|
|
protected virtual float IntervalMultiplier => 1.0f;
|
2021-08-16 19:52:19 +00:00
|
|
|
|
protected virtual bool AvoidDrawingNearCursor => false;
|
2020-10-02 16:34:06 +00:00
|
|
|
|
|
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
|
|
|
|
{
|
2021-08-13 03:10:33 +00:00
|
|
|
|
AddTrail(e.ScreenSpaceMousePosition);
|
2021-08-12 14:13:03 +00:00
|
|
|
|
return base.OnMouseMove(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void AddTrail(Vector2 position)
|
|
|
|
|
{
|
|
|
|
|
if (InterpolateMovements)
|
|
|
|
|
{
|
2021-08-13 03:10:33 +00:00
|
|
|
|
if (!lastPosition.HasValue)
|
|
|
|
|
{
|
|
|
|
|
lastPosition = position;
|
|
|
|
|
resampler.AddPosition(lastPosition.Value);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 14:13:03 +00:00
|
|
|
|
foreach (Vector2 pos2 in resampler.AddPosition(position))
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2021-08-12 14:13:03 +00:00
|
|
|
|
Trace.Assert(lastPosition.HasValue);
|
|
|
|
|
|
2019-09-09 10:22:27 +00:00
|
|
|
|
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);
|
2019-09-09 10:22:27 +00:00
|
|
|
|
|
2021-08-16 19:52:19 +00:00
|
|
|
|
for (float d = interval; d < stopAt; d += interval)
|
2019-09-09 10:22:27 +00:00
|
|
|
|
{
|
|
|
|
|
lastPosition = pos1 + direction * d;
|
|
|
|
|
addPart(lastPosition.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
2021-08-12 14:13:03 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
lastPosition = position;
|
|
|
|
|
addPart(lastPosition.Value);
|
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 10:22:27 +00:00
|
|
|
|
private void addPart(Vector2 screenSpacePosition)
|
|
|
|
|
{
|
|
|
|
|
parts[currentIndex].Position = screenSpacePosition;
|
2019-12-16 06:15:26 +00:00
|
|
|
|
parts[currentIndex].Time = time + 1;
|
2019-09-09 10:22:27 +00:00
|
|
|
|
++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;
|
2021-04-14 05:20:18 +00:00
|
|
|
|
private Vector2 originPosition;
|
|
|
|
|
|
2019-12-16 06:15:26 +00:00
|
|
|
|
private readonly QuadBatch<TexturedTrailVertex> vertexBatch = new QuadBatch<TexturedTrailVertex>(max_sprites, 1);
|
2019-03-01 04:01:52 +00:00
|
|
|
|
|
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;
|
2019-09-09 10:30:31 +00:00
|
|
|
|
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
|
|
|
|
|
2021-04-14 05:20:18 +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;
|
|
|
|
|
|
2019-09-25 07:25:15 +00:00
|
|
|
|
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
|
|
|
|
|
2019-12-16 09:10:44 +00:00
|
|
|
|
texture.TextureGL.Bind();
|
|
|
|
|
|
2019-12-16 06:15:26 +00:00
|
|
|
|
RectangleF textureRect = texture.GetTextureRect();
|
|
|
|
|
|
|
|
|
|
foreach (var part in parts)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2019-12-16 06:15:26 +00:00
|
|
|
|
if (part.InvalidationID == -1)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (time - part.Time >= 1)
|
2019-09-25 07:25:15 +00:00
|
|
|
|
continue;
|
|
|
|
|
|
2019-12-16 06:15:26 +00:00
|
|
|
|
vertexBatch.Add(new TexturedTrailVertex
|
|
|
|
|
{
|
2021-04-14 05:20:18 +00:00
|
|
|
|
Position = new Vector2(part.Position.X - size.X * originPosition.X, part.Position.Y + size.Y * (1 - originPosition.Y)),
|
2019-12-16 06:15:26 +00:00
|
|
|
|
TexturePosition = textureRect.BottomLeft,
|
2020-06-02 14:47:18 +00:00
|
|
|
|
TextureRect = new Vector4(0, 0, 1, 1),
|
2019-12-16 06:15:26 +00:00
|
|
|
|
Colour = DrawColourInfo.Colour.BottomLeft.Linear,
|
|
|
|
|
Time = part.Time
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
vertexBatch.Add(new TexturedTrailVertex
|
|
|
|
|
{
|
2021-04-14 05:20:18 +00:00
|
|
|
|
Position = new Vector2(part.Position.X + size.X * (1 - originPosition.X), part.Position.Y + size.Y * (1 - originPosition.Y)),
|
2019-12-16 06:15:26 +00:00
|
|
|
|
TexturePosition = textureRect.BottomRight,
|
2020-06-02 14:47:18 +00:00
|
|
|
|
TextureRect = new Vector4(0, 0, 1, 1),
|
2019-12-16 06:15:26 +00:00
|
|
|
|
Colour = DrawColourInfo.Colour.BottomRight.Linear,
|
|
|
|
|
Time = part.Time
|
|
|
|
|
});
|
2019-06-10 11:29:01 +00:00
|
|
|
|
|
2019-12-16 06:15:26 +00:00
|
|
|
|
vertexBatch.Add(new TexturedTrailVertex
|
|
|
|
|
{
|
2021-04-14 05:20:18 +00:00
|
|
|
|
Position = new Vector2(part.Position.X + size.X * (1 - originPosition.X), part.Position.Y - size.Y * originPosition.Y),
|
2019-12-16 06:15:26 +00:00
|
|
|
|
TexturePosition = textureRect.TopRight,
|
2020-06-02 14:47:18 +00:00
|
|
|
|
TextureRect = new Vector4(0, 0, 1, 1),
|
2019-12-16 06:15:26 +00:00
|
|
|
|
Colour = DrawColourInfo.Colour.TopRight.Linear,
|
|
|
|
|
Time = part.Time
|
|
|
|
|
});
|
2019-07-16 08:50:03 +00:00
|
|
|
|
|
2019-12-16 06:15:26 +00:00
|
|
|
|
vertexBatch.Add(new TexturedTrailVertex
|
|
|
|
|
{
|
2021-04-14 05:20:18 +00:00
|
|
|
|
Position = new Vector2(part.Position.X - size.X * originPosition.X, part.Position.Y - size.Y * originPosition.Y),
|
2019-12-16 06:15:26 +00:00
|
|
|
|
TexturePosition = textureRect.TopLeft,
|
2020-06-02 14:47:18 +00:00
|
|
|
|
TextureRect = new Vector4(0, 0, 1, 1),
|
2019-12-16 06:15:26 +00:00
|
|
|
|
Colour = DrawColourInfo.Colour.TopLeft.Linear,
|
|
|
|
|
Time = part.Time
|
|
|
|
|
});
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-16 06:15:26 +00:00
|
|
|
|
vertexBatch.Draw();
|
2019-04-02 02:56:22 +00:00
|
|
|
|
shader.Unbind();
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
2019-03-01 04:01:52 +00:00
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
|
2019-06-10 11:29:01 +00:00
|
|
|
|
vertexBatch.Dispose();
|
2019-03-01 04:01:52 +00:00
|
|
|
|
}
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|