1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 18:53:51 +08:00
osu-lazer/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 17:58:33 +08:00
using System;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
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;
2016-10-27 17:58:33 +08:00
namespace osu.Game.Overlays
{
public class DragBar : Container
{
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;
public bool IsEnabled
{
get { return enabled; }
set
{
enabled = value;
if (!enabled)
Fill.Width = 0;
}
}
2016-10-27 17:58:33 +08:00
public DragBar()
{
RelativeSizeAxes = Axes.X;
Children = new Drawable[]
{
Fill = new Container
2016-10-27 17:58:33 +08:00
{
Name = "FillContainer",
Origin = Anchor.BottomLeft,
Anchor = Anchor.BottomLeft,
2016-10-27 17:58:33 +08:00
RelativeSizeAxes = Axes.Both,
Width = 0,
Children = new Drawable[]
{
2017-04-07 14:20:39 +08:00
new Box
{
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;
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-04-14 18:20:07 +08:00
Fill.TransformTo(() => Fill.Width, 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-04-07 14:20:20 +08:00
private class TransformSeek : TransformFloat
2017-04-07 08:57:34 +08:00
{
public override void Apply(Drawable d)
{
base.Apply(d);
d.Width = CurrentValue;
}
}
2016-10-27 17:58:33 +08:00
}
}