1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-23 20:07:26 +08:00
osu-lazer/osu.Game/Graphics/Cursor/CursorTrail.cs

243 lines
7.9 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2017 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 osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Shaders;
using osu.Framework.Graphics.Textures;
using osu.Framework.Input;
using OpenTK;
2016-11-26 21:08:43 +08:00
using System;
using osu.Framework.Graphics.OpenGL;
using osu.Framework.Graphics.OpenGL.Buffers;
using OpenTK.Graphics.ES30;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.Colour;
using osu.Framework.Timing;
2016-11-26 18:22:56 +08:00
namespace osu.Game.Graphics.Cursor
{
2017-03-07 09:59:19 +08:00
internal class CursorTrail : Drawable
2016-11-26 18:22:56 +08:00
{
public override bool HandleInput => true;
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;
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:33:42 +08:00
private InputResampler resampler = new InputResampler();
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();
AlwaysReceiveInput = true;
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
}
[BackgroundDependencyLoader]
private void load(ShaderManager shaders, TextureStore textures)
2016-11-26 18:22:56 +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) / 500f;
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)
{
if (lastPosition == null)
{
lastPosition = state.Mouse.NativeState.Position;
2017-05-04 00:33:42 +08:00
resampler.AddPosition(lastPosition.Value);
2016-11-26 18:25:58 +08:00
return base.OnMouseMove(state);
}
Vector2 pos1 = lastPosition.Value;
2017-05-04 00:33:42 +08:00
foreach (Vector2 pos2 in resampler.AddPosition(state.Mouse.NativeState.Position))
{
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<TexturedVertex2D> VertexBuffer;
}
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)
2017-02-07 15:15:45 +08:00
Shared.VertexBuffer = new QuadVertexBuffer<TexturedVertex2D>(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;
ColourInfo colour = DrawInfo.Colour;
colour.TopLeft.Linear.A = Parts[i].Time + colour.TopLeft.Linear.A;
colour.TopRight.Linear.A = Parts[i].Time + colour.TopRight.Linear.A;
colour.BottomLeft.Linear.A = Parts[i].Time + colour.BottomLeft.Linear.A;
colour.BottomRight.Linear.A = Parts[i].Time + colour.BottomRight.Linear.A;
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),
colour,
null,
v => Shared.VertexBuffer.Vertices[end++] = v);
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
}
}
}
}