mirror of
https://github.com/osukey/osukey.git
synced 2025-08-07 00:23:59 +09:00
Make TestCaseWaveform use a custom drawnode instead of boxes
This commit is contained in:
@ -5,12 +5,19 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using OpenTK;
|
using OpenTK;
|
||||||
using OpenTK.Graphics;
|
using OpenTK.Graphics;
|
||||||
|
using OpenTK.Graphics.ES30;
|
||||||
using osu.Framework.Allocation;
|
using osu.Framework.Allocation;
|
||||||
using osu.Framework.Audio.Track;
|
using osu.Framework.Audio.Track;
|
||||||
using osu.Framework.Configuration;
|
using osu.Framework.Configuration;
|
||||||
using osu.Framework.Graphics;
|
using osu.Framework.Graphics;
|
||||||
|
using osu.Framework.Graphics.Batches;
|
||||||
|
using osu.Framework.Graphics.Colour;
|
||||||
using osu.Framework.Graphics.Containers;
|
using osu.Framework.Graphics.Containers;
|
||||||
|
using osu.Framework.Graphics.OpenGL.Vertices;
|
||||||
|
using osu.Framework.Graphics.Primitives;
|
||||||
|
using osu.Framework.Graphics.Shaders;
|
||||||
using osu.Framework.Graphics.Shapes;
|
using osu.Framework.Graphics.Shapes;
|
||||||
|
using osu.Framework.Graphics.Textures;
|
||||||
using osu.Game.Beatmaps;
|
using osu.Game.Beatmaps;
|
||||||
using osu.Game.Graphics.Sprites;
|
using osu.Game.Graphics.Sprites;
|
||||||
using osu.Game.Overlays;
|
using osu.Game.Overlays;
|
||||||
@ -92,51 +99,118 @@ namespace osu.Game.Tests.Visual
|
|||||||
|
|
||||||
private void processWaveform(Waveform waveform) => Schedule(() => displays.ForEach(d => d.Display(waveform)));
|
private void processWaveform(Waveform waveform) => Schedule(() => displays.ForEach(d => d.Display(waveform)));
|
||||||
|
|
||||||
private class WaveformDisplay : CompositeDrawable
|
private class WaveformDisplay : Drawable
|
||||||
{
|
{
|
||||||
|
private List<WaveformPoint> points;
|
||||||
|
private int channels;
|
||||||
|
|
||||||
|
private Shader shader;
|
||||||
|
private readonly Texture texture;
|
||||||
|
|
||||||
private readonly int resolution;
|
private readonly int resolution;
|
||||||
|
|
||||||
public WaveformDisplay(int resolution)
|
public WaveformDisplay(int resolution)
|
||||||
{
|
{
|
||||||
this.resolution = resolution;
|
this.resolution = resolution;
|
||||||
|
|
||||||
|
texture = Texture.WhitePixel;
|
||||||
|
}
|
||||||
|
|
||||||
|
[BackgroundDependencyLoader]
|
||||||
|
private void load(ShaderManager shaders)
|
||||||
|
{
|
||||||
|
shader = shaders?.Load(VertexShaderDescriptor.TEXTURE_2, FragmentShaderDescriptor.TEXTURE_ROUNDED);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Display(Waveform waveform)
|
public void Display(Waveform waveform)
|
||||||
{
|
{
|
||||||
ClearInternal();
|
points = waveform.Generate((int)MathHelper.Clamp(Math.Ceiling(DrawWidth), 0, waveform.TotalPoints) / resolution);
|
||||||
|
channels = waveform.Channels;
|
||||||
|
Invalidate(Invalidation.DrawNode);
|
||||||
|
}
|
||||||
|
|
||||||
var generated = waveform.Generate((int)MathHelper.Clamp(Math.Ceiling(DrawWidth), 0, waveform.TotalPoints) / resolution);
|
protected override DrawNode CreateDrawNode() => new WaveformDrawNode();
|
||||||
|
|
||||||
for (int i = 0; i < generated.Count; i++)
|
private readonly WaveformDrawNodeSharedData sharedData = new WaveformDrawNodeSharedData();
|
||||||
|
protected override void ApplyDrawNode(DrawNode node)
|
||||||
|
{
|
||||||
|
var n = (WaveformDrawNode)node;
|
||||||
|
|
||||||
|
n.Shader = shader;
|
||||||
|
n.Texture = texture;
|
||||||
|
n.Points = points;
|
||||||
|
n.Channels = channels;
|
||||||
|
n.Size = DrawSize;
|
||||||
|
n.Shared = sharedData;
|
||||||
|
|
||||||
|
|
||||||
|
base.ApplyDrawNode(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
private class WaveformDrawNodeSharedData
|
||||||
|
{
|
||||||
|
public readonly QuadBatch<TexturedVertex2D> VertexBatch = new QuadBatch<TexturedVertex2D>(1000, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
private class WaveformDrawNode : DrawNode
|
||||||
|
{
|
||||||
|
public Shader Shader;
|
||||||
|
public Texture Texture;
|
||||||
|
|
||||||
|
public WaveformDrawNodeSharedData Shared;
|
||||||
|
|
||||||
|
public List<WaveformPoint> Points;
|
||||||
|
public Vector2 Size;
|
||||||
|
public int Channels;
|
||||||
|
|
||||||
|
public override void Draw(Action<TexturedVertex2D> vertexAction)
|
||||||
{
|
{
|
||||||
var point = generated[i];
|
base.Draw(vertexAction);
|
||||||
|
|
||||||
// Left channel
|
if (Points == null || Points.Count == 0)
|
||||||
AddInternal(new NonInputBox
|
return;
|
||||||
{
|
|
||||||
Anchor = Anchor.CentreLeft,
|
|
||||||
Origin = Anchor.BottomLeft,
|
|
||||||
RelativePositionAxes = Axes.X,
|
|
||||||
RelativeSizeAxes = Axes.Both,
|
|
||||||
X = 1f / generated.Count * i,
|
|
||||||
Size = new Vector2(1f / generated.Count, point.Amplitude[0] / 2),
|
|
||||||
Colour = Color4.Red
|
|
||||||
});
|
|
||||||
|
|
||||||
if (waveform.Channels >= 2)
|
Shader.Bind();
|
||||||
|
Texture.TextureGL.Bind();
|
||||||
|
|
||||||
|
float separation = Size.X / (Points.Count - 1);
|
||||||
|
Vector2 localInflationAmount = new Vector2(0, 1) * DrawInfo.MatrixInverse.ExtractScale().Xy;
|
||||||
|
|
||||||
|
for (int i = 0; i < Points.Count - 1; i++)
|
||||||
{
|
{
|
||||||
// Right channel
|
ColourInfo colour = DrawInfo.Colour;
|
||||||
AddInternal(new NonInputBox
|
Quad quadToDraw;
|
||||||
|
|
||||||
|
switch (Channels)
|
||||||
{
|
{
|
||||||
Anchor = Anchor.CentreLeft,
|
default:
|
||||||
Origin = Anchor.TopLeft,
|
case 2:
|
||||||
RelativePositionAxes = Axes.X,
|
{
|
||||||
RelativeSizeAxes = Axes.Both,
|
float height = Size.Y / 2;
|
||||||
X = 1f / generated.Count * i,
|
quadToDraw = new Quad(
|
||||||
Size = new Vector2(1f / generated.Count, point.Amplitude[1] / 2),
|
new Vector2(i * separation, height - Points[i].Amplitude[0] * height),
|
||||||
Colour = Color4.Green
|
new Vector2((i + 1) * separation, height - Points[i + 1].Amplitude[0] * height),
|
||||||
});
|
new Vector2(i * separation, height + Points[i].Amplitude[1] * height),
|
||||||
|
new Vector2((i + 1) * separation, height + Points[i + 1].Amplitude[1] * height)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
{
|
||||||
|
quadToDraw = new Quad(
|
||||||
|
new Vector2(i * separation, Size.Y - Points[i].Amplitude[0] * Size.Y),
|
||||||
|
new Vector2((i + 1) * separation, Size.Y - Points[i + 1].Amplitude[0] * Size.Y),
|
||||||
|
new Vector2(i * separation, Size.Y),
|
||||||
|
new Vector2((i + 1) * separation, Size.Y)
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Texture.DrawQuad(quadToDraw * DrawInfo.Matrix, colour, null, Shared.VertexBatch.Add, Vector2.Divide(localInflationAmount, quadToDraw.Size));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Shader.Unbind();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user