osu/osu.Game/Graphics/Cursor/MenuCursor.cs

198 lines
7.1 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
2018-11-20 07:51:59 +00:00
using osuTK;
2018-04-13 09:19:50 +00:00
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Sprites;
using osu.Game.Configuration;
using System;
using JetBrains.Annotations;
2022-01-28 09:13:51 +00:00
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
2019-02-21 10:04:31 +00:00
using osu.Framework.Bindables;
2018-04-13 09:19:50 +00:00
using osu.Framework.Graphics.Textures;
2018-10-02 03:02:47 +00:00
using osu.Framework.Input.Events;
using osu.Framework.Utils;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Graphics.Cursor
{
public class MenuCursor : CursorContainer
{
private readonly IBindable<bool> screenshotCursorVisibility = new Bindable<bool>(true);
public override bool IsPresent => screenshotCursorVisibility.Value && base.IsPresent;
2018-07-06 08:45:39 +00:00
protected override Drawable CreateCursor() => activeCursor = new Cursor();
private Cursor activeCursor;
2018-04-13 09:19:50 +00:00
private Bindable<bool> cursorRotate;
2018-07-02 07:07:52 +00:00
private DragRotationState dragRotationState;
private Vector2 positionMouseDown;
2018-04-13 09:19:50 +00:00
2022-01-28 09:13:51 +00:00
private Sample tapSample;
[BackgroundDependencyLoader(true)]
2022-01-28 09:13:51 +00:00
private void load([NotNull] OsuConfigManager config, [CanBeNull] ScreenshotManager screenshotManager, AudioManager audio)
{
cursorRotate = config.GetBindable<bool>(OsuSetting.CursorRotation);
if (screenshotManager != null)
screenshotCursorVisibility.BindTo(screenshotManager.CursorVisibility);
2022-01-28 09:13:51 +00:00
tapSample = audio.Samples.Get(@"UI/cursor-tap");
}
2018-10-02 03:02:47 +00:00
protected override bool OnMouseMove(MouseMoveEvent e)
2018-04-13 09:19:50 +00:00
{
2018-07-02 07:07:52 +00:00
if (dragRotationState != DragRotationState.NotDragging)
2018-04-13 09:19:50 +00:00
{
var position = e.MousePosition;
float distance = Vector2Extensions.Distance(position, positionMouseDown);
2019-04-01 03:16:05 +00:00
2018-04-13 09:19:50 +00:00
// don't start rotating until we're moved a minimum distance away from the mouse down location,
// else it can have an annoying effect.
2018-07-02 07:07:52 +00:00
if (dragRotationState == DragRotationState.DragStarted && distance > 30)
dragRotationState = DragRotationState.Rotating;
2019-04-01 03:16:05 +00:00
2018-07-02 07:09:33 +00:00
// don't rotate when distance is zero to avoid NaN
2018-07-02 07:07:52 +00:00
if (dragRotationState == DragRotationState.Rotating && distance > 0)
2018-04-13 09:19:50 +00:00
{
Vector2 offset = e.MousePosition - positionMouseDown;
float degrees = MathUtils.RadiansToDegrees(MathF.Atan2(-offset.X, offset.Y)) + 24.3f;
2018-04-13 09:19:50 +00:00
// Always rotate in the direction of least distance
2018-07-06 08:45:39 +00:00
float diff = (degrees - activeCursor.Rotation) % 360;
2018-04-13 09:19:50 +00:00
if (diff < -180) diff += 360;
if (diff > 180) diff -= 360;
2018-07-06 08:45:39 +00:00
degrees = activeCursor.Rotation + diff;
2018-04-13 09:19:50 +00:00
2018-07-06 08:45:39 +00:00
activeCursor.RotateTo(degrees, 600, Easing.OutQuint);
2018-04-13 09:19:50 +00:00
}
}
2018-10-02 03:02:47 +00:00
return base.OnMouseMove(e);
2018-04-13 09:19:50 +00:00
}
2018-07-02 07:20:44 +00:00
2018-10-02 03:02:47 +00:00
protected override bool OnMouseDown(MouseDownEvent e)
2018-04-13 09:19:50 +00:00
{
if (State.Value == Visibility.Visible)
{
// only trigger animation for main mouse buttons
activeCursor.Scale = new Vector2(1);
activeCursor.ScaleTo(0.90f, 800, Easing.OutQuint);
2018-04-13 09:19:50 +00:00
activeCursor.AdditiveLayer.Alpha = 0;
activeCursor.AdditiveLayer.FadeInFromZero(800, Easing.OutQuint);
2018-07-02 07:07:52 +00:00
if (cursorRotate.Value && dragRotationState != DragRotationState.Rotating)
{
// if cursor is already rotating don't reset its rotate origin
dragRotationState = DragRotationState.DragStarted;
positionMouseDown = e.MousePosition;
}
playTapSample();
2018-07-02 07:07:52 +00:00
}
2019-02-28 04:31:40 +00:00
2018-10-02 03:02:47 +00:00
return base.OnMouseDown(e);
2018-04-13 09:19:50 +00:00
}
protected override void OnMouseUp(MouseUpEvent e)
2018-04-13 09:19:50 +00:00
{
if (!e.HasAnyButtonPressed)
2018-04-13 09:19:50 +00:00
{
2018-07-06 08:45:39 +00:00
activeCursor.AdditiveLayer.FadeOutFromOne(500, Easing.OutQuint);
activeCursor.ScaleTo(1, 500, Easing.OutElastic);
2018-04-13 09:19:50 +00:00
if (dragRotationState != DragRotationState.NotDragging)
{
2018-07-06 08:45:39 +00:00
activeCursor.RotateTo(0, 600 * (1 + Math.Abs(activeCursor.Rotation / 720)), Easing.OutElasticHalf);
dragRotationState = DragRotationState.NotDragging;
}
if (State.Value == Visibility.Visible)
playTapSample(0.8);
2018-07-02 07:07:52 +00:00
}
2019-02-28 04:31:40 +00:00
base.OnMouseUp(e);
2018-04-13 09:19:50 +00:00
}
protected override void PopIn()
{
2018-07-06 08:45:39 +00:00
activeCursor.FadeTo(1, 250, Easing.OutQuint);
activeCursor.ScaleTo(1, 400, Easing.OutQuint);
2018-04-13 09:19:50 +00:00
}
protected override void PopOut()
{
2018-07-06 08:45:39 +00:00
activeCursor.FadeTo(0, 250, Easing.OutQuint);
activeCursor.ScaleTo(0.6f, 250, Easing.In);
2018-04-13 09:19:50 +00:00
}
private void playTapSample(double baseFrequency = 1f)
{
const float random_range = 0.02f;
SampleChannel channel = tapSample.GetChannel();
// Scale to [-0.75, 0.75] so that the sample isn't fully panned left or right (sounds weird)
channel.Balance.Value = ((activeCursor.X / DrawWidth) * 2 - 1) * 0.75;
channel.Frequency.Value = baseFrequency - (random_range / 2f) + RNG.NextDouble(random_range);
channel.Play();
}
2018-04-13 09:19:50 +00:00
public class Cursor : Container
{
private Container cursorContainer;
2019-09-02 22:28:51 +00:00
private Bindable<float> cursorScale;
2018-04-13 09:19:50 +00:00
private const float base_scale = 0.15f;
public Sprite AdditiveLayer;
public Cursor()
{
AutoSizeAxes = Axes.Both;
}
[BackgroundDependencyLoader]
2018-08-30 22:04:40 +00:00
private void load(OsuConfigManager config, TextureStore textures, OsuColour colour)
2018-04-13 09:19:50 +00:00
{
Children = new Drawable[]
{
cursorContainer = new Container
{
AutoSizeAxes = Axes.Both,
Children = new Drawable[]
{
new Sprite
{
2018-08-30 22:04:40 +00:00
Texture = textures.Get(@"Cursor/menu-cursor"),
2018-04-13 09:19:50 +00:00
},
AdditiveLayer = new Sprite
{
2019-08-21 04:29:50 +00:00
Blending = BlendingParameters.Additive,
2018-04-13 09:19:50 +00:00
Colour = colour.Pink,
Alpha = 0,
2018-08-30 22:04:40 +00:00
Texture = textures.Get(@"Cursor/menu-cursor-additive"),
2018-04-13 09:19:50 +00:00
},
}
}
};
2019-09-02 22:28:51 +00:00
cursorScale = config.GetBindable<float>(OsuSetting.MenuCursorSize);
cursorScale.BindValueChanged(scale => cursorContainer.Scale = new Vector2(scale.NewValue * base_scale), true);
2018-04-13 09:19:50 +00:00
}
}
2018-07-02 07:07:52 +00:00
private enum DragRotationState
{
NotDragging,
DragStarted,
Rotating,
}
2018-04-13 09:19:50 +00:00
}
}