osu/osu.Game.Rulesets.Osu/UI/Cursor/GameplayCursor.cs

223 lines
8.0 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2018-04-13 09:19:50 +00:00
using osu.Framework.Allocation;
2019-02-21 10:04:31 +00:00
using osu.Framework.Bindables;
2018-04-13 09:19:50 +00:00
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input.Bindings;
using osu.Game.Beatmaps;
using osu.Game.Configuration;
using osu.Game.Skinning;
2018-11-20 07:51:59 +00:00
using osuTK;
using osuTK.Graphics;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Rulesets.Osu.UI.Cursor
{
public class GameplayCursor : CursorContainer, IKeyBindingHandler<OsuAction>
{
protected override Drawable CreateCursor() => new OsuCursor();
protected override Container<Drawable> Content => fadeContainer;
private readonly Container<Drawable> fadeContainer;
public GameplayCursor()
{
2019-01-10 05:20:44 +00:00
InternalChild = fadeContainer = new Container
2018-04-13 09:19:50 +00:00
{
2019-01-10 05:20:44 +00:00
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
2018-04-13 09:19:50 +00:00
{
2019-01-10 05:20:44 +00:00
new CursorTrail { Depth = 1 }
}
2018-04-13 09:19:50 +00:00
};
}
private int downCount;
private void updateExpandedState()
{
if (downCount > 0)
(ActiveCursor as OsuCursor)?.Expand();
else
(ActiveCursor as OsuCursor)?.Contract();
}
2018-04-13 09:19:50 +00:00
public bool OnPressed(OsuAction action)
{
2018-12-10 23:38:03 +00:00
switch (action)
2018-12-11 20:02:12 +00:00
{
case OsuAction.LeftButton:
case OsuAction.RightButton:
downCount++;
updateExpandedState();
2018-12-11 20:02:12 +00:00
break;
}
2018-04-13 09:19:50 +00:00
return false;
}
public bool OnReleased(OsuAction action)
{
2018-12-10 23:38:03 +00:00
switch (action)
{
case OsuAction.LeftButton:
case OsuAction.RightButton:
if (--downCount == 0)
updateExpandedState();
2018-12-10 23:38:03 +00:00
break;
}
2018-04-13 09:19:50 +00:00
return false;
}
public override bool HandlePositionalInput => true; // OverlayContainer will set this false when we go hidden, but we always want to receive input.
2018-04-13 09:19:50 +00:00
protected override void PopIn()
{
fadeContainer.FadeTo(1, 300, Easing.OutQuint);
ActiveCursor.ScaleTo(1, 400, Easing.OutQuint);
2018-04-13 09:19:50 +00:00
}
protected override void PopOut()
{
fadeContainer.FadeTo(0.05f, 450, Easing.OutQuint);
ActiveCursor.ScaleTo(0.8f, 450, Easing.OutQuint);
2018-04-13 09:19:50 +00:00
}
public class OsuCursor : SkinReloadableDrawable
2018-04-13 09:19:50 +00:00
{
private bool cursorExpand;
2018-04-13 09:19:50 +00:00
private Bindable<double> cursorScale;
private Bindable<bool> autoCursorScale;
private readonly IBindable<WorkingBeatmap> beatmap = new Bindable<WorkingBeatmap>();
2018-04-13 09:19:50 +00:00
private Container expandTarget;
private Drawable scaleTarget;
2018-04-13 09:19:50 +00:00
public OsuCursor()
{
Origin = Anchor.Centre;
Size = new Vector2(42);
}
protected override void SkinChanged(ISkinSource skin, bool allowFallback)
{
2019-01-07 11:12:39 +00:00
cursorExpand = skin.GetValue<SkinConfiguration, bool>(s => s.CursorExpand ?? true);
}
2018-04-13 09:19:50 +00:00
[BackgroundDependencyLoader]
2019-02-01 06:42:15 +00:00
private void load(OsuConfigManager config, IBindable<WorkingBeatmap> beatmap)
2018-04-13 09:19:50 +00:00
{
InternalChild = expandTarget = new Container
2018-04-13 09:19:50 +00:00
{
RelativeSizeAxes = Axes.Both,
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Child = scaleTarget = new SkinnableDrawable("cursor", _ => new CircularContainer
2018-04-13 09:19:50 +00:00
{
RelativeSizeAxes = Axes.Both,
Masking = true,
BorderThickness = Size.X / 6,
BorderColour = Color4.White,
EdgeEffect = new EdgeEffectParameters
2018-04-13 09:19:50 +00:00
{
Type = EdgeEffectType.Shadow,
Colour = Color4.Pink.Opacity(0.5f),
Radius = 5,
2018-04-13 09:19:50 +00:00
},
Children = new Drawable[]
2018-04-13 09:19:50 +00:00
{
new Box
{
RelativeSizeAxes = Axes.Both,
Alpha = 0,
AlwaysPresent = true,
},
new CircularContainer
2018-04-13 09:19:50 +00:00
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
RelativeSizeAxes = Axes.Both,
Masking = true,
BorderThickness = Size.X / 3,
BorderColour = Color4.White.Opacity(0.5f),
Children = new Drawable[]
2018-04-13 09:19:50 +00:00
{
new Box
{
RelativeSizeAxes = Axes.Both,
Alpha = 0,
AlwaysPresent = true,
},
2018-04-13 09:19:50 +00:00
},
},
new CircularContainer
2018-04-13 09:19:50 +00:00
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
RelativeSizeAxes = Axes.Both,
Scale = new Vector2(0.1f),
Masking = true,
Children = new Drawable[]
2018-04-13 09:19:50 +00:00
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.White,
},
2018-04-13 09:19:50 +00:00
},
},
}
}, restrictSize: false)
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
RelativeSizeAxes = Axes.Both,
2018-04-13 09:19:50 +00:00
}
};
this.beatmap.BindTo(beatmap);
this.beatmap.ValueChanged += _ => calculateScale();
2018-04-13 09:19:50 +00:00
cursorScale = config.GetBindable<double>(OsuSetting.GameplayCursorSize);
cursorScale.ValueChanged += _ => calculateScale();
2018-04-13 09:19:50 +00:00
autoCursorScale = config.GetBindable<bool>(OsuSetting.AutoCursorSize);
autoCursorScale.ValueChanged += _ => calculateScale();
2018-04-13 09:19:50 +00:00
calculateScale();
}
private void calculateScale()
{
float scale = (float)cursorScale.Value;
2019-02-21 09:56:34 +00:00
if (autoCursorScale.Value && beatmap.Value != null)
2018-04-13 09:19:50 +00:00
{
// if we have a beatmap available, let's get its circle size to figure out an automatic cursor scale modifier.
scale *= (float)(1 - 0.7 * (1 + beatmap.Value.BeatmapInfo.BaseDifficulty.CircleSize - BeatmapDifficulty.DEFAULT_DIFFICULTY) / BeatmapDifficulty.DEFAULT_DIFFICULTY);
}
scaleTarget.Scale = new Vector2(scale);
2018-04-13 09:19:50 +00:00
}
2018-12-11 17:06:41 +00:00
private const float pressed_scale = 1.2f;
private const float released_scale = 1f;
2018-12-12 10:17:09 +00:00
public void Expand()
2018-12-12 10:17:09 +00:00
{
if (!cursorExpand) return;
expandTarget.ScaleTo(released_scale).ScaleTo(pressed_scale, 100, Easing.OutQuad);
2018-12-12 10:17:09 +00:00
}
public void Contract() => expandTarget.ScaleTo(released_scale, 100, Easing.OutQuad);
2018-12-11 17:06:41 +00:00
}
}
2018-04-13 09:19:50 +00:00
}