1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-16 22:22:54 +08:00
osu-lazer/osu.Game.Rulesets.Osu/UI/Cursor/GameplayCursor.cs

223 lines
8.0 KiB
C#
Raw Normal View History

2018-04-13 17:19:50 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Framework.Allocation;
using osu.Framework.Configuration;
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 15:51:59 +08:00
using osuTK;
using osuTK.Graphics;
2018-04-13 17:19:50 +08: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 13:20:44 +08:00
InternalChild = fadeContainer = new Container
2018-04-13 17:19:50 +08:00
{
2019-01-10 13:20:44 +08:00
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
2018-04-13 17:19:50 +08:00
{
2019-01-10 13:20:44 +08:00
new CursorTrail { Depth = 1 }
}
2018-04-13 17:19:50 +08:00
};
}
private int downCount;
private void updateExpandedState()
{
if (downCount > 0)
(ActiveCursor as OsuCursor)?.Expand();
else
(ActiveCursor as OsuCursor)?.Contract();
}
2018-04-13 17:19:50 +08:00
public bool OnPressed(OsuAction action)
{
2018-12-11 07:38:03 +08:00
switch (action)
2018-12-12 04:02:12 +08:00
{
case OsuAction.LeftButton:
case OsuAction.RightButton:
downCount++;
updateExpandedState();
2018-12-12 04:02:12 +08:00
break;
}
2018-04-13 17:19:50 +08:00
return false;
}
public bool OnReleased(OsuAction action)
{
2018-12-11 07:38:03 +08:00
switch (action)
{
case OsuAction.LeftButton:
case OsuAction.RightButton:
if (--downCount == 0)
updateExpandedState();
2018-12-11 07:38:03 +08:00
break;
}
2018-04-13 17:19:50 +08: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 17:19:50 +08:00
protected override void PopIn()
{
fadeContainer.FadeTo(1, 300, Easing.OutQuint);
ActiveCursor.ScaleTo(1, 400, Easing.OutQuint);
2018-04-13 17:19:50 +08:00
}
protected override void PopOut()
{
fadeContainer.FadeTo(0.05f, 450, Easing.OutQuint);
ActiveCursor.ScaleTo(0.8f, 450, Easing.OutQuint);
2018-04-13 17:19:50 +08:00
}
public class OsuCursor : SkinReloadableDrawable
2018-04-13 17:19:50 +08:00
{
private bool cursorExpand;
2018-04-13 17:19:50 +08:00
private Bindable<double> cursorScale;
private Bindable<bool> autoCursorScale;
private readonly IBindable<WorkingBeatmap> beatmap = new Bindable<WorkingBeatmap>();
2018-04-13 17:19:50 +08:00
private Container expandTarget;
private Drawable scaleTarget;
2018-04-13 17:19:50 +08:00
public OsuCursor()
{
Origin = Anchor.Centre;
Size = new Vector2(42);
}
protected override void SkinChanged(ISkinSource skin, bool allowFallback)
{
2019-01-07 19:12:39 +08:00
cursorExpand = skin.GetValue<SkinConfiguration, bool>(s => s.CursorExpand ?? true);
}
2018-04-13 17:19:50 +08:00
[BackgroundDependencyLoader]
2018-06-06 19:16:20 +08:00
private void load(OsuConfigManager config, IBindableBeatmap beatmap)
2018-04-13 17:19:50 +08:00
{
InternalChild = expandTarget = new Container
2018-04-13 17:19:50 +08:00
{
RelativeSizeAxes = Axes.Both,
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Child = scaleTarget = new SkinnableDrawable("cursor", _ => new CircularContainer
2018-04-13 17:19:50 +08:00
{
RelativeSizeAxes = Axes.Both,
Masking = true,
BorderThickness = Size.X / 6,
BorderColour = Color4.White,
EdgeEffect = new EdgeEffectParameters
2018-04-13 17:19:50 +08:00
{
Type = EdgeEffectType.Shadow,
Colour = Color4.Pink.Opacity(0.5f),
Radius = 5,
2018-04-13 17:19:50 +08:00
},
Children = new Drawable[]
2018-04-13 17:19:50 +08:00
{
new Box
{
RelativeSizeAxes = Axes.Both,
Alpha = 0,
AlwaysPresent = true,
},
new CircularContainer
2018-04-13 17:19:50 +08: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 17:19:50 +08:00
{
new Box
{
RelativeSizeAxes = Axes.Both,
Alpha = 0,
AlwaysPresent = true,
},
2018-04-13 17:19:50 +08:00
},
},
new CircularContainer
2018-04-13 17:19:50 +08:00
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
RelativeSizeAxes = Axes.Both,
Scale = new Vector2(0.1f),
Masking = true,
Children = new Drawable[]
2018-04-13 17:19:50 +08:00
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.White,
},
2018-04-13 17:19:50 +08:00
},
},
}
}, restrictSize: false)
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
RelativeSizeAxes = Axes.Both,
2018-04-13 17:19:50 +08:00
}
};
this.beatmap.BindTo(beatmap);
this.beatmap.ValueChanged += v => calculateScale();
2018-04-13 17:19:50 +08:00
cursorScale = config.GetBindable<double>(OsuSetting.GameplayCursorSize);
cursorScale.ValueChanged += v => calculateScale();
autoCursorScale = config.GetBindable<bool>(OsuSetting.AutoCursorSize);
autoCursorScale.ValueChanged += v => calculateScale();
calculateScale();
}
private void calculateScale()
{
float scale = (float)cursorScale.Value;
if (autoCursorScale && beatmap.Value != null)
{
// 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 17:19:50 +08:00
}
2018-12-12 01:06:41 +08:00
private const float pressed_scale = 1.2f;
private const float released_scale = 1f;
2018-12-12 18:17:09 +08:00
public void Expand()
2018-12-12 18:17:09 +08:00
{
if (!cursorExpand) return;
expandTarget.ScaleTo(released_scale).ScaleTo(pressed_scale, 100, Easing.OutQuad);
2018-12-12 18:17:09 +08:00
}
public void Contract() => expandTarget.ScaleTo(released_scale, 100, Easing.OutQuad);
2018-12-12 01:06:41 +08:00
}
}
2018-04-13 17:19:50 +08:00
}