osu/osu.Game/Overlays/DragBar.cs

111 lines
3.0 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-10-27 09:58:33 +00:00
using System;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
2017-04-07 00:57:34 +00:00
using osu.Framework.Graphics.Transforms;
2016-10-27 09:58:33 +00:00
using osu.Framework.Input;
2017-04-07 02:02:37 +00:00
using OpenTK;
2016-10-27 09:58:33 +00:00
namespace osu.Game.Overlays
{
public class DragBar : Container
{
protected readonly Container Fill;
2016-10-27 09:58:33 +00:00
public Action<float> SeekRequested;
2017-04-07 06:20:20 +00:00
public bool IsSeeking { get; private set; }
2016-10-27 09:58:33 +00:00
2017-04-07 02:02:37 +00:00
private bool enabled = true;
public bool IsEnabled
{
get { return enabled; }
set
{
enabled = value;
if (!enabled)
Fill.Width = 0;
}
}
2016-10-27 09:58:33 +00:00
public DragBar()
{
RelativeSizeAxes = Axes.X;
Children = new Drawable[]
{
Fill = new Container
2016-10-27 09:58:33 +00:00
{
Name = "FillContainer",
Origin = Anchor.BottomLeft,
Anchor = Anchor.BottomLeft,
2016-10-27 09:58:33 +00:00
RelativeSizeAxes = Axes.Both,
Width = 0,
Children = new Drawable[]
{
2017-04-07 06:20:39 +00:00
new Box
{
RelativeSizeAxes = Axes.Both
}
}
2016-10-27 09:58:33 +00:00
}
};
}
public void UpdatePosition(float position)
{
2017-04-07 06:20:20 +00:00
if (IsSeeking || !IsEnabled) return;
2016-10-27 09:58:33 +00:00
2017-04-14 10:20:07 +00:00
updatePosition(position, false);
2016-10-27 09:58:33 +00:00
}
private void seek(InputState state)
{
float seekLocation = state.Mouse.Position.X / DrawWidth;
if (!IsEnabled) return;
2016-10-27 09:58:33 +00:00
SeekRequested?.Invoke(seekLocation);
2017-04-07 00:57:34 +00:00
updatePosition(seekLocation);
}
2017-04-14 10:20:07 +00:00
private void updatePosition(float position, bool easing = true)
2017-04-07 00:57:34 +00:00
{
2017-04-07 02:02:37 +00:00
position = MathHelper.Clamp(position, 0, 1);
2017-04-14 10:20:07 +00:00
Fill.TransformTo(() => Fill.Width, position, easing ? 200 : 0, EasingTypes.OutQuint, new TransformSeek());
2016-10-27 09:58:33 +00:00
}
protected override bool OnMouseDown(InputState state, MouseDownEventArgs args)
{
seek(state);
return true;
}
protected override bool OnDrag(InputState state)
{
seek(state);
return true;
}
2017-04-07 06:20:20 +00:00
protected override bool OnDragStart(InputState state) => IsSeeking = true;
2016-10-27 09:58:33 +00:00
protected override bool OnDragEnd(InputState state)
{
2017-04-07 06:20:20 +00:00
IsSeeking = false;
2016-10-27 09:58:33 +00:00
return true;
}
2017-04-07 00:57:34 +00:00
2017-04-07 06:20:20 +00:00
private class TransformSeek : TransformFloat
2017-04-07 00:57:34 +00:00
{
public override void Apply(Drawable d)
{
base.Apply(d);
d.Width = CurrentValue;
}
}
2016-10-27 09:58:33 +00:00
}
}