1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-09 09:37:42 +08:00
osu-lazer/osu.Game/Graphics/Cursor/MenuCursorContainer.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

82 lines
2.4 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 osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Input;
using osu.Framework.Input.StateChanges;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Graphics.Cursor
{
/// <summary>
/// A container which provides a <see cref="MenuCursor"/> which can be overridden by hovered <see cref="Drawable"/>s.
/// </summary>
public class MenuCursorContainer : Container, IProvideCursor
{
protected override Container<Drawable> Content => content;
private readonly Container content;
2018-04-13 17:19:50 +08:00
/// <summary>
/// Whether any cursors can be displayed.
/// </summary>
internal bool CanShowCursor = true;
2018-04-13 17:19:50 +08:00
2018-01-12 18:36:35 +08:00
public CursorContainer Cursor { get; }
public bool ProvidingUserCursor => true;
2018-04-13 17:19:50 +08:00
public MenuCursorContainer()
{
AddRangeInternal(new Drawable[]
{
Cursor = new MenuCursor { State = { Value = Visibility.Hidden } },
content = new Container { RelativeSizeAxes = Axes.Both }
});
}
2018-04-13 17:19:50 +08:00
private InputManager inputManager;
2018-04-13 17:19:50 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
inputManager = GetContainingInputManager();
}
2018-04-13 17:19:50 +08:00
2018-01-12 18:36:35 +08:00
private IProvideCursor currentTarget;
2019-02-28 12:31:40 +08:00
protected override void Update()
{
base.Update();
2018-04-13 17:19:50 +08:00
2020-09-07 14:31:05 +08:00
var lastMouseSource = inputManager.CurrentState.Mouse.LastSource;
bool hasValidInput = lastMouseSource != null && !(lastMouseSource is ISourcedFromTouch);
if (!hasValidInput || !CanShowCursor)
{
2018-01-12 18:36:35 +08:00
currentTarget?.Cursor?.Hide();
2019-01-23 19:52:00 +08:00
currentTarget = null;
return;
}
2018-04-13 17:19:50 +08:00
IProvideCursor newTarget = this;
foreach (var d in inputManager.HoveredDrawables)
{
2020-07-24 09:41:09 +08:00
if (d is IProvideCursor p && p.ProvidingUserCursor)
{
newTarget = p;
break;
}
}
2018-04-13 17:19:50 +08:00
if (currentTarget == newTarget)
return;
2018-04-13 17:19:50 +08:00
2018-01-12 18:36:35 +08:00
currentTarget?.Cursor?.Hide();
newTarget.Cursor?.Show();
2018-04-13 17:19:50 +08:00
currentTarget = newTarget;
}
}
}