osu/osu.Game/Overlays/Options/SliderOption.cs

160 lines
5.4 KiB
C#
Raw Normal View History

2016-12-14 19:59:23 +00:00
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
2016-12-07 20:39:21 +00:00
using OpenTK;
2016-12-08 22:12:12 +00:00
using OpenTK.Input;
using OpenTK.Graphics;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
using osu.Framework.Configuration;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.Sprites;
2016-12-07 20:39:21 +00:00
using osu.Framework.Graphics.Transformations;
using osu.Framework.Graphics.UserInterface;
2016-12-08 22:12:12 +00:00
using osu.Framework.Input;
2016-12-07 20:39:21 +00:00
namespace osu.Game.Overlays.Options
{
2016-12-16 03:50:23 +00:00
public class SliderOption<T> : FlowContainer where T : struct
2016-12-07 20:39:21 +00:00
{
private SliderBar<T> slider;
private SpriteText text;
public string LabelText
{
get { return text.Text; }
set
{
text.Text = value;
text.Alpha = string.IsNullOrEmpty(value) ? 0 : 1;
}
}
public BindableNumber<T> Bindable
{
get { return slider.Bindable; }
set { slider.Bindable = value; }
}
public SliderOption()
{
Direction = FlowDirection.VerticalOnly;
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
Children = new Drawable[]
{
text = new SpriteText { Alpha = 0 },
slider = new OsuSliderBar<T>
{
Margin = new MarginPadding { Top = 5 },
RelativeSizeAxes = Axes.X,
}
};
}
2016-12-16 03:50:23 +00:00
private class OsuSliderBar<U> : SliderBar<U> where U : struct
2016-12-07 20:39:21 +00:00
{
private AudioSample sample;
2016-12-08 22:12:12 +00:00
private double lastSample;
private Container nub;
private Box leftBox, rightBox;
2016-12-07 20:39:21 +00:00
public OsuSliderBar()
{
Height = 22;
2016-12-08 22:12:12 +00:00
Children = new Drawable[]
2016-12-07 20:39:21 +00:00
{
2016-12-08 22:12:12 +00:00
leftBox = new Box
{
Height = 2,
RelativeSizeAxes = Axes.None,
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Colour = new Color4(255, 102, 170, 255),
},
rightBox = new Box
2016-12-07 20:39:21 +00:00
{
2016-12-08 22:12:12 +00:00
Height = 2,
RelativeSizeAxes = Axes.None,
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
Colour = new Color4(255, 102, 170, 255),
Alpha = 0.5f,
},
nub = new Container
{
Width = Height,
Height = Height,
CornerRadius = Height / 2,
Origin = Anchor.TopCentre,
AutoSizeAxes = Axes.None,
RelativeSizeAxes = Axes.None,
Masking = true,
BorderColour = new Color4(255, 102, 170, 255),
BorderThickness = 3,
Children = new[]
{
new Box { Colour = Color4.Transparent, RelativeSizeAxes = Axes.Both }
}
},
};
2016-12-07 20:39:21 +00:00
}
[BackgroundDependencyLoader]
private void load(AudioManager audio)
{
sample = audio.Sample.Get(@"Sliderbar/sliderbar");
}
2016-12-08 22:12:12 +00:00
private void playSample()
{
if (Clock == null)
return;
if (Clock.CurrentTime - lastSample > 50)
{
lastSample = Clock.CurrentTime;
sample.Frequency.Value = 1 + NormalizedValue * 0.2f;
sample.Play();
}
}
protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
{
if (args.Key == Key.Left || args.Key == Key.Right)
playSample();
return base.OnKeyDown(state, args);
}
protected override bool OnClick(InputState state)
{
playSample();
return base.OnClick(state);
}
protected override bool OnDrag(InputState state)
{
playSample();
return base.OnDrag(state);
}
protected override void Update()
{
2016-12-15 05:11:37 +00:00
base.Update();
leftBox.Scale = new Vector2(MathHelper.Clamp(
nub.DrawPosition.X - nub.DrawSize.X / 2 + 2, 0, DrawWidth), 1);
rightBox.Scale = new Vector2(MathHelper.Clamp(
DrawWidth - nub.DrawPosition.X - nub.DrawSize.X / 2 + 2, 0, DrawWidth), 1);
}
2016-12-08 22:12:12 +00:00
2016-12-15 05:11:37 +00:00
protected override void UpdateValue(float value)
2016-12-07 20:39:21 +00:00
{
2016-12-15 05:11:37 +00:00
nub.Position = new Vector2(DrawWidth * value, nub.Position.Y);
2016-12-07 20:39:21 +00:00
}
}
}
2016-12-09 13:18:58 +00:00
}