1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-23 01:27:35 +08:00
osu-lazer/osu.Game/Overlays/DragBar.cs

109 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 FillContainer;
protected readonly Box 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)
2017-03-02 21:15:53 +08:00
FillContainer.Width = 0;
}
}
2016-10-27 17:58:33 +08:00
public DragBar()
{
RelativeSizeAxes = Axes.X;
Children = new Drawable[]
{
2017-03-02 21:15:53 +08:00
FillContainer = new Container
2016-10-27 17:58:33 +08:00
{
Origin = Anchor.BottomLeft,
Anchor = Anchor.BottomLeft,
2016-10-27 17:58:33 +08:00
RelativeSizeAxes = Axes.Both,
Width = 0,
Children = new Drawable[]
{
2017-03-02 21:15:53 +08:00
Fill = 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-07 08:57:34 +08:00
updatePosition(position);
2016-10-27 17:58:33 +08:00
}
private void seek(InputState state)
{
if (!IsEnabled) return;
2016-10-27 17:58:33 +08:00
float seekLocation = state.Mouse.Position.X / DrawWidth;
SeekRequested?.Invoke(seekLocation);
2017-04-07 08:57:34 +08:00
updatePosition(seekLocation);
}
private void updatePosition(float position)
{
2017-04-07 10:02:37 +08:00
position = MathHelper.Clamp(position, 0, 1);
2017-04-07 14:20:20 +08:00
fill.TransformTo(fill.Width, position, 200, 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
}
}