1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-16 03:02:53 +08:00
osu-lazer/osu.Game/Graphics/Cursor/MenuCursorContainer.cs

70 lines
2.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 17:19:50 +08:00
using System.Linq;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Input;
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
2018-04-13 17:19:50 +08:00
{
protected override Container<Drawable> Content => content;
private readonly Container content;
/// <summary>
/// Whether any cursors can be displayed.
/// </summary>
internal bool CanShowCursor = true;
2018-04-13 17:19:50 +08:00
public CursorContainer Cursor { get; }
public bool ProvidingUserCursor => true;
public MenuCursorContainer()
2018-04-13 17:19:50 +08:00
{
AddRangeInternal(new Drawable[]
{
Cursor = new MenuCursor { State = Visibility.Hidden },
content = new Container { RelativeSizeAxes = Axes.Both }
});
}
private InputManager inputManager;
protected override void LoadComplete()
{
base.LoadComplete();
inputManager = GetContainingInputManager();
}
private IProvideCursor currentTarget;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
protected override void Update()
{
base.Update();
if (!CanShowCursor)
{
currentTarget?.Cursor?.Hide();
2019-01-23 19:52:00 +08:00
currentTarget = null;
2018-04-13 17:19:50 +08:00
return;
}
var newTarget = inputManager.HoveredDrawables.OfType<IProvideCursor>().FirstOrDefault(t => t.ProvidingUserCursor) ?? this;
if (currentTarget == newTarget)
return;
currentTarget?.Cursor?.Hide();
newTarget.Cursor?.Show();
currentTarget = newTarget;
}
}
}