1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 01:43:20 +08:00
osu-lazer/osu.Game.Rulesets.Osu/UI/Cursor/CursorTrail.cs

273 lines
8.9 KiB
C#
Raw Normal View History

2018-01-05 19:21:19 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2016-11-26 18:22:56 +08:00
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
2016-11-26 18:22:56 +08:00
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.OpenGL.Buffers;
using osu.Framework.Graphics.OpenGL.Vertices;
using osu.Framework.Graphics.Primitives;
2016-11-26 18:22:56 +08:00
using osu.Framework.Graphics.Shaders;
using osu.Framework.Graphics.Textures;
using osu.Framework.Input;
using osu.Framework.Timing;
2016-11-26 18:22:56 +08:00
using OpenTK;
using OpenTK.Graphics;
2016-11-26 21:08:43 +08:00
using OpenTK.Graphics.ES30;
2016-11-26 18:22:56 +08:00
namespace osu.Game.Rulesets.Osu.UI.Cursor
2016-11-26 18:22:56 +08:00
{
internal class CursorTrail : Drawable, IRequireHighFrequencyMousePosition
2016-11-26 18:22:56 +08:00
{
private int currentIndex;
2016-11-26 18:22:56 +08:00
private Shader shader;
private Texture texture;
private Vector2 size => texture.Size * Scale;
2016-11-26 18:22:56 +08:00
private double timeOffset;
private float time;
public override bool IsPresent => true;
private readonly TrailDrawNodeSharedData trailDrawNodeSharedData = new TrailDrawNodeSharedData();
2017-02-07 15:15:45 +08:00
private const int max_sprites = 2048;
2016-11-26 18:22:56 +08:00
private readonly TrailPart[] parts = new TrailPart[max_sprites];
private Vector2? lastPosition;
2016-11-26 18:25:58 +08:00
2017-05-04 00:48:17 +08:00
private readonly InputResampler resampler = new InputResampler();
2017-05-04 00:33:42 +08:00
2016-11-26 18:22:56 +08:00
protected override DrawNode CreateDrawNode() => new TrailDrawNode();
protected override void ApplyDrawNode(DrawNode node)
{
base.ApplyDrawNode(node);
2017-03-07 09:59:19 +08:00
TrailDrawNode tNode = (TrailDrawNode)node;
2016-11-26 18:22:56 +08:00
tNode.Shader = shader;
tNode.Texture = texture;
tNode.Size = size;
2016-11-26 18:22:56 +08:00
tNode.Time = time;
2016-11-26 21:08:43 +08:00
tNode.Shared = trailDrawNodeSharedData;
for (int i = 0; i < parts.Length; ++i)
if (parts[i].InvalidationID > tNode.Parts[i].InvalidationID)
tNode.Parts[i] = parts[i];
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();
2016-11-26 18:22:56 +08:00
RelativeSizeAxes = Axes.Both;
2017-02-07 15:15:45 +08:00
for (int i = 0; i < max_sprites; i++)
{
parts[i].InvalidationID = 0;
parts[i].WasUpdated = true;
}
2016-11-26 18:22:56 +08:00
}
public override bool ReceiveMouseInputAt(Vector2 screenSpacePos) => true;
2017-06-24 00:02:24 +08:00
2016-11-26 18:22:56 +08:00
[BackgroundDependencyLoader]
private void load(ShaderManager shaders, TextureStore textures)
2016-11-26 18:22:56 +08:00
{
2017-05-09 20:00:40 +08:00
shader = shaders?.Load(@"CursorTrail", FragmentShaderDescriptor.TEXTURE);
texture = textures.Get(@"Cursor/cursortrail");
Scale = new Vector2(1 / texture.ScaleAdjust);
2016-11-26 18:22:56 +08:00
}
protected override void LoadComplete()
{
base.LoadComplete();
resetTime();
}
2016-11-26 18:22:56 +08:00
protected override void Update()
{
base.Update();
2016-11-26 21:08:43 +08:00
Invalidate(Invalidation.DrawNode, shallPropagate: false);
2016-11-26 18:22:56 +08:00
2017-03-23 12:52:38 +08:00
const int fade_clock_reset_threshold = 1000000;
2016-11-26 18:22:56 +08:00
time = (float)(Time.Current - timeOffset) / 300f;
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
}
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;
++parts[i].InvalidationID;
}
2016-11-26 18:22:56 +08:00
time = 0;
timeOffset = Time.Current;
}
2016-11-26 18:25:58 +08:00
protected override bool OnMouseMove(InputState state)
{
Vector2 pos = state.Mouse.NativeState.Position;
2016-11-26 18:25:58 +08:00
if (lastPosition == null)
{
lastPosition = pos;
2017-05-04 00:33:42 +08:00
resampler.AddPosition(lastPosition.Value);
2016-11-26 18:25:58 +08:00
return base.OnMouseMove(state);
}
foreach (Vector2 pos2 in resampler.AddPosition(pos))
2017-05-04 00:33:42 +08:00
{
2017-05-08 11:05:48 +08:00
Trace.Assert(lastPosition.HasValue);
Vector2 pos1 = lastPosition.Value;
2017-05-04 00:33:42 +08:00
Vector2 diff = pos2 - pos1;
float distance = diff.Length;
Vector2 direction = diff / distance;
2016-11-26 18:25:58 +08:00
2017-05-04 00:33:42 +08:00
float interval = size.X / 2 * 0.9f;
2016-11-26 18:25:58 +08:00
2017-05-04 00:33:42 +08:00
for (float d = interval; d < distance; d += interval)
{
lastPosition = pos1 + direction * d;
addPosition(lastPosition.Value);
}
2016-11-26 18:25:58 +08:00
}
return base.OnMouseMove(state);
}
private void addPosition(Vector2 pos)
{
parts[currentIndex].Position = pos;
parts[currentIndex].Time = time;
++parts[currentIndex].InvalidationID;
2016-11-26 18:25:58 +08:00
2017-02-07 15:15:45 +08:00
currentIndex = (currentIndex + 1) % max_sprites;
2016-11-26 18:25:58 +08:00
}
2017-03-07 09:59:19 +08:00
private struct TrailPart
{
public Vector2 Position;
public float Time;
public long InvalidationID;
public bool WasUpdated;
}
2017-03-07 09:59:19 +08:00
private class TrailDrawNodeSharedData
2016-11-26 21:08:43 +08:00
{
public VertexBuffer<TexturedTrailVertex> VertexBuffer;
2016-11-26 21:08:43 +08:00
}
2017-03-07 09:59:19 +08:00
private class TrailDrawNode : DrawNode
2016-11-26 18:22:56 +08:00
{
public Shader Shader;
public Texture Texture;
2016-11-26 18:22:56 +08:00
public float Time;
2016-11-26 21:08:43 +08:00
public TrailDrawNodeSharedData Shared;
2016-11-26 18:22:56 +08:00
public readonly TrailPart[] Parts = new TrailPart[max_sprites];
public Vector2 Size;
public TrailDrawNode()
{
2017-02-07 15:15:45 +08:00
for (int i = 0; i < max_sprites; i++)
{
Parts[i].InvalidationID = 0;
Parts[i].WasUpdated = false;
}
}
2016-11-26 21:08:43 +08:00
public override void Draw(Action<TexturedVertex2D> vertexAction)
2016-11-26 18:22:56 +08:00
{
2016-11-26 21:08:43 +08:00
if (Shared.VertexBuffer == null)
Shared.VertexBuffer = new QuadVertexBuffer<TexturedTrailVertex>(max_sprites, BufferUsageHint.DynamicDraw);
2016-11-26 21:08:43 +08:00
2016-11-26 18:22:56 +08:00
Shader.GetUniform<float>("g_FadeClock").Value = Time;
2016-11-26 21:08:43 +08:00
int updateStart = -1, updateEnd = 0;
for (int i = 0; i < Parts.Length; ++i)
2016-11-26 21:08:43 +08:00
{
if (Parts[i].WasUpdated)
2016-11-26 21:08:43 +08:00
{
if (updateStart == -1)
updateStart = i;
updateEnd = i + 1;
int start = i * 4;
int end = start;
Vector2 pos = Parts[i].Position;
float time = Parts[i].Time;
2016-12-02 02:12:35 +08:00
Texture.DrawQuad(
new Quad(pos.X - Size.X / 2, pos.Y - Size.Y / 2, Size.X, Size.Y),
DrawInfo.Colour,
null,
v => Shared.VertexBuffer.Vertices[end++] = new TexturedTrailVertex
{
Position = v.Position,
TexturePosition = v.TexturePosition,
Time = time + 1,
Colour = v.Colour,
});
Parts[i].WasUpdated = false;
2016-11-26 21:08:43 +08:00
}
else if (updateStart != -1)
{
Shared.VertexBuffer.UpdateRange(updateStart * 4, updateEnd * 4);
updateStart = -1;
}
}
// Update all remaining vertices that have been changed.
if (updateStart != -1)
Shared.VertexBuffer.UpdateRange(updateStart * 4, updateEnd * 4);
base.Draw(vertexAction);
Shader.Bind();
2016-11-26 21:08:43 +08:00
Texture.TextureGL.Bind();
2016-11-26 21:08:43 +08:00
Shared.VertexBuffer.Draw();
Shader.Unbind();
2016-11-26 18:22:56 +08:00
}
}
[StructLayout(LayoutKind.Sequential)]
public struct TexturedTrailVertex : IEquatable<TexturedTrailVertex>, IVertex
{
[VertexMember(2, VertexAttribPointerType.Float)]
public Vector2 Position;
[VertexMember(4, VertexAttribPointerType.Float)]
public Color4 Colour;
[VertexMember(2, VertexAttribPointerType.Float)]
public Vector2 TexturePosition;
[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);
}
}
2016-11-26 18:22:56 +08:00
}
}