Skip to content

Commit

Permalink
Merge pull request #6018 from EVAST9919/simplify-picker
Browse files Browse the repository at this point in the history
Simplify `SaturationValueSelector`
  • Loading branch information
smoogipoo authored Oct 4, 2023
2 parents 5600aba + d0867a1 commit 725f671
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@
#nullable disable

using System;
using System.Runtime.InteropServices;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Rendering;
using osu.Framework.Graphics.Shaders;
using osu.Framework.Graphics.Shaders.Types;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Input.Events;
using osu.Framework.Utils;
using osuTK;
using osuTK.Graphics;

namespace osu.Framework.Graphics.UserInterface
{
Expand Down Expand Up @@ -60,7 +58,10 @@ protected SaturationValueSelector()
SelectionArea = new Container
{
RelativeSizeAxes = Axes.Both,
Child = box = new SaturationBox()
Child = box = new SaturationBox
{
Colour = ColourInfo.GradientHorizontal(Color4.White, Color4.Red)
}
},
marker = CreateMarker().With(d =>
{
Expand Down Expand Up @@ -148,7 +149,7 @@ private bool shouldUpdateHue(float newHue)

private void hueChanged()
{
box.Hue = Hue.Value;
box.Colour = ColourInfo.GradientHorizontal(Color4.White, Colour4.FromHSV(Hue.Value, 1f, 1f));
updateCurrent();
}

Expand Down Expand Up @@ -209,24 +210,8 @@ protected abstract partial class Marker : CompositeDrawable
public IBindable<Colour4> Current { get; } = new Bindable<Colour4>();
}

private partial class SaturationBox : Box, ITexturedShaderDrawable
private partial class SaturationBox : Box
{
public new IShader TextureShader { get; private set; }

private float hue;

public float Hue
{
get => hue;
set
{
if (hue == value) return;

hue = value;
Invalidate(Invalidation.DrawNode);
}
}

public SaturationBox()
{
RelativeSizeAxes = Axes.Both;
Expand All @@ -237,51 +222,6 @@ private void load(ShaderManager shaders)
{
TextureShader = shaders.Load(VertexShaderDescriptor.TEXTURE_2, "SaturationSelectorBackground");
}

protected override DrawNode CreateDrawNode() => new SaturationBoxDrawNode(this);

private class SaturationBoxDrawNode : SpriteDrawNode
{
public new SaturationBox Source => (SaturationBox)base.Source;

public SaturationBoxDrawNode(SaturationBox source)
: base(source)
{
}

private float hue;

public override void ApplyState()
{
base.ApplyState();
hue = Source.hue;
}

private IUniformBuffer<HueData> hueDataBuffer;

protected override void BindUniformResources(IShader shader, IRenderer renderer)
{
base.BindUniformResources(shader, renderer);

hueDataBuffer ??= renderer.CreateUniformBuffer<HueData>();
hueDataBuffer.Data = hueDataBuffer.Data with { Hue = hue };

shader.BindUniformBlock("m_HueData", hueDataBuffer);
}

protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
hueDataBuffer?.Dispose();
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
private record struct HueData
{
public UniformFloat Hue;
private readonly UniformPadding12 pad1;
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,13 @@

layout(location = 2) in highp vec2 v_TexCoord;

layout(std140, set = 0, binding = 0) uniform m_HueData
{
mediump float hue;
};

layout(location = 0) out vec4 o_Colour;

void main(void)
{
highp vec2 resolution = v_TexRect.zw - v_TexRect.xy;
highp vec2 pixelPos = v_TexCoord / resolution;
o_Colour = getRoundedColor(hsv2rgb(vec4(hue, pixelPos.x, 1.0 - pixelPos.y, 1.0)), v_TexCoord);
o_Colour = getRoundedColor(vec4(vec3(1.0 - pixelPos.y), 1.0), v_TexCoord);
}

#endif

0 comments on commit 725f671

Please sign in to comment.