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