1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-19 01:03:02 +08:00
osu-lazer/osu.Game/Graphics/Cursor/CursorOverrideContainer.cs

68 lines
2.0 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
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 CursorOverrideContainer : Container, IProvideCursor
{
protected override Container<Drawable> Content => content;
private readonly Container content;
/// <summary>
/// Whether any cursors can be displayed.
/// </summary>
public bool CanShowCursor;
2018-01-12 18:36:35 +08:00
public CursorContainer Cursor { get; }
public bool ProvidingUserCursor => true;
public CursorOverrideContainer()
{
AddRangeInternal(new Drawable[]
{
2018-01-12 18:36:35 +08:00
Cursor = new MenuCursor { State = Visibility.Hidden },
content = new Container { RelativeSizeAxes = Axes.Both }
});
}
private InputManager inputManager;
protected override void LoadComplete()
{
base.LoadComplete();
inputManager = GetContainingInputManager();
}
2018-01-12 18:36:35 +08:00
private IProvideCursor currentTarget;
protected override void Update()
{
base.Update();
if (!CanShowCursor)
{
2018-01-12 18:36:35 +08:00
currentTarget?.Cursor?.Hide();
return;
}
var newTarget = inputManager.HoveredDrawables.OfType<IProvideCursor>().FirstOrDefault(t => t.ProvidingUserCursor) ?? this;
if (currentTarget == newTarget)
return;
2018-01-12 18:36:35 +08:00
currentTarget?.Cursor?.Hide();
newTarget.Cursor?.Show();
currentTarget = newTarget;
}
}
}