A game framework written with osu! in mind.
at master 109 lines 3.6 kB view raw
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. 2// See the LICENCE file in the repository root for full licence text. 3 4using osu.Framework.Allocation; 5using osu.Framework.Bindables; 6using osu.Framework.Graphics.Containers; 7using osu.Framework.Graphics.Shaders; 8using osu.Framework.Graphics.Shapes; 9using osu.Framework.Input.Events; 10using osuTK; 11 12namespace osu.Framework.Graphics.UserInterface 13{ 14 public abstract partial class HSVColourPicker 15 { 16 public abstract class HueSelector : CompositeDrawable 17 { 18 public Bindable<float> Hue { get; } = new BindableFloat 19 { 20 MinValue = 0, 21 MaxValue = 1 22 }; 23 24 /// <summary> 25 /// The body of the hue slider. 26 /// </summary> 27 protected readonly Container SliderBar; 28 29 private readonly Drawable nub; 30 31 protected HueSelector() 32 { 33 AutoSizeAxes = Axes.Y; 34 RelativeSizeAxes = Axes.X; 35 36 InternalChildren = new[] 37 { 38 SliderBar = new Container 39 { 40 Height = 30, 41 RelativeSizeAxes = Axes.X, 42 Child = new HueSelectorBackground 43 { 44 RelativeSizeAxes = Axes.Both 45 } 46 }, 47 nub = CreateSliderNub().With(d => 48 { 49 d.RelativeSizeAxes = Axes.Y; 50 d.RelativePositionAxes = Axes.X; 51 }) 52 }; 53 } 54 55 /// <summary> 56 /// Creates the nub which will be used for the hue slider. 57 /// </summary> 58 protected abstract Drawable CreateSliderNub(); 59 60 protected override void LoadComplete() 61 { 62 base.LoadComplete(); 63 64 Hue.BindValueChanged(_ => Scheduler.AddOnce(updateNubPosition), true); 65 } 66 67 private void updateNubPosition() 68 { 69 nub.Position = new Vector2(Hue.Value, 0); 70 } 71 72 protected override bool OnMouseDown(MouseDownEvent e) 73 { 74 handleMouseInput(e.ScreenSpaceMousePosition); 75 return true; 76 } 77 78 protected override bool OnDragStart(DragStartEvent e) 79 { 80 handleMouseInput(e.ScreenSpaceMousePosition); 81 return true; 82 } 83 84 protected override void OnDrag(DragEvent e) 85 { 86 handleMouseInput(e.ScreenSpaceMousePosition); 87 } 88 89 private void handleMouseInput(Vector2 mousePosition) 90 { 91 var localSpacePosition = ToLocalSpace(mousePosition); 92 Hue.Value = localSpacePosition.X / DrawWidth; 93 } 94 95 private class HueSelectorBackground : Box, ITexturedShaderDrawable 96 { 97 public new IShader TextureShader { get; private set; } 98 public new IShader RoundedTextureShader { get; private set; } 99 100 [BackgroundDependencyLoader] 101 private void load(ShaderManager shaders) 102 { 103 TextureShader = shaders.Load(VertexShaderDescriptor.TEXTURE_2, "HueSelectorBackground"); 104 RoundedTextureShader = shaders.Load(VertexShaderDescriptor.TEXTURE_2, "HueSelectorBackgroundRounded"); 105 } 106 } 107 } 108 } 109}