1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-17 05:52:54 +08:00
osu-lazer/osu.Game.Rulesets.Osu/UI/Cursor/OsuCursorContainer.cs

172 lines
5.6 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 17:19:50 +08:00
using System;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Textures;
2018-04-13 17:19:50 +08:00
using osu.Framework.Input.Bindings;
using osu.Game.Beatmaps;
using osu.Game.Configuration;
using osu.Game.Rulesets.Osu.Configuration;
using osu.Game.Rulesets.UI;
using osu.Game.Screens.Play;
using osu.Game.Skinning;
using osuTK;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Rulesets.Osu.UI.Cursor
{
public class OsuCursorContainer : GameplayCursorContainer, IKeyBindingHandler<OsuAction>
2018-04-13 17:19:50 +08:00
{
protected override Drawable CreateCursor() => new OsuCursor();
protected override Container<Drawable> Content => fadeContainer;
private readonly Container<Drawable> fadeContainer;
private readonly Bindable<bool> showTrail = new Bindable<bool>(true);
private readonly Drawable cursorTrail;
public IBindable<float> CursorScale => cursorScale;
private readonly Bindable<float> cursorScale = new BindableFloat(1);
private Bindable<float> userCursorScale;
private Bindable<bool> autoCursorScale;
public OsuCursorContainer()
2018-04-13 17:19:50 +08:00
{
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,
2019-09-09 18:22:44 +08:00
Child = cursorTrail = new SkinnableDrawable(new OsuSkinComponent(OsuSkinComponents.CursorTrail), _ => new DefaultCursorTrail(), confineMode: ConfineMode.NoScaling)
2018-04-13 17:19:50 +08:00
};
}
[Resolved(canBeNull: true)]
private GameplayBeatmap beatmap { get; set; }
[Resolved]
private OsuConfigManager config { get; set; }
[BackgroundDependencyLoader(true)]
private void load(OsuConfigManager config, OsuRulesetConfigManager rulesetConfig)
{
rulesetConfig?.BindWith(OsuRulesetSetting.ShowCursorTrail, showTrail);
}
protected override void LoadComplete()
{
base.LoadComplete();
showTrail.BindValueChanged(v => cursorTrail.FadeTo(v.NewValue ? 1 : 0, 200), true);
userCursorScale = config.GetBindable<float>(OsuSetting.GameplayCursorSize);
userCursorScale.ValueChanged += _ => calculateScale();
autoCursorScale = config.GetBindable<bool>(OsuSetting.AutoCursorSize);
autoCursorScale.ValueChanged += _ => calculateScale();
CursorScale.BindValueChanged(e =>
{
var newScale = new Vector2(e.NewValue);
ActiveCursor.Scale = newScale;
cursorTrail.Scale = newScale;
}, true);
calculateScale();
}
/// <summary>
/// Get the scale applicable to the ActiveCursor based on a beatmap's circle size.
/// </summary>
public static float GetScaleForCircleSize(float circleSize) =>
1f - 0.7f * (1f + circleSize - BeatmapDifficulty.DEFAULT_DIFFICULTY) / BeatmapDifficulty.DEFAULT_DIFFICULTY;
private void calculateScale()
{
float scale = userCursorScale.Value;
if (autoCursorScale.Value && beatmap != null)
{
// if we have a beatmap available, let's get its circle size to figure out an automatic cursor scale modifier.
scale *= GetScaleForCircleSize(beatmap.BeatmapInfo.BaseDifficulty.CircleSize);
}
cursorScale.Value = scale;
var newScale = new Vector2(scale);
ActiveCursor.ScaleTo(newScale, 400, Easing.OutQuint);
cursorTrail.Scale = newScale;
}
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 void OnReleased(OsuAction action)
2018-04-13 17:19:50 +08:00
{
2018-12-11 07:38:03 +08:00
switch (action)
{
case OsuAction.LeftButton:
case OsuAction.RightButton:
// Todo: Math.Max() is required as a temporary measure to address https://github.com/ppy/osu-framework/issues/2576
downCount = Math.Max(0, downCount - 1);
if (downCount == 0)
updateExpandedState();
2018-12-11 07:38:03 +08:00
break;
}
2018-04-13 17:19:50 +08:00
}
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(CursorScale.Value, 400, Easing.OutQuint);
2018-04-13 17:19:50 +08:00
}
protected override void PopOut()
{
fadeContainer.FadeTo(0.05f, 450, Easing.OutQuint);
ActiveCursor.ScaleTo(CursorScale.Value * 0.8f, 450, Easing.OutQuint);
2018-04-13 17:19:50 +08:00
}
private class DefaultCursorTrail : CursorTrail
{
[BackgroundDependencyLoader]
private void load(TextureStore textures)
{
Texture = textures.Get(@"Cursor/cursortrail");
Scale = new Vector2(1 / Texture.ScaleAdjust);
}
}
2018-12-12 01:06:41 +08:00
}
2018-04-13 17:19:50 +08:00
}