Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add attribute to hide arbitrary drawables from draw visualiser #6411

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions osu.Framework/Graphics/Visualisation/DrawVisualiser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ protected override void Update()
overlay.Target = Searching ? cursorTarget : inputManager.HoveredDrawables.OfType<VisualisedDrawable>().FirstOrDefault()?.Target;
}

private static readonly Dictionary<Type, bool> is_type_valid_target_cache = new Dictionary<Type, bool>();

private void updateCursorTarget()
{
Drawable drawableTarget = null;
Expand Down Expand Up @@ -268,30 +270,32 @@ void findTarget(Drawable drawable)
if (!validForTarget(drawable))
return;

// Special case for full-screen overlays that act as input receptors, but don't display anything
if (!hasCustomDrawNode(drawable))
return;

drawableTarget = drawable;
}
}

// Valid if the drawable contains the mouse position and the position wouldn't be masked by the parent
bool validForTarget(Drawable drawable)
=> drawable.ScreenSpaceDrawQuad.Contains(inputManager.CurrentState.Mouse.Position)
&& maskingQuad?.Contains(inputManager.CurrentState.Mouse.Position) != false;
}
{
if (!drawable.ScreenSpaceDrawQuad.Contains(inputManager.CurrentState.Mouse.Position)
|| maskingQuad?.Contains(inputManager.CurrentState.Mouse.Position) == false)
{
return false;
}

private static readonly Dictionary<Type, bool> has_custom_drawnode_cache = new Dictionary<Type, bool>();
Type type = drawable.GetType();

private bool hasCustomDrawNode(Drawable drawable)
{
var type = drawable.GetType();
if (is_type_valid_target_cache.TryGetValue(type, out bool valid))
return valid;

if (has_custom_drawnode_cache.TryGetValue(type, out bool existing))
return existing;
// Exclude "overlay" objects (Component/etc) that don't draw anything and don't override CreateDrawNode().
valid = type.GetMethod(nameof(CreateDrawNode), BindingFlags.Instance | BindingFlags.NonPublic)?.DeclaringType != typeof(Drawable);

return has_custom_drawnode_cache[type] = type.GetMethod(nameof(CreateDrawNode), BindingFlags.Instance | BindingFlags.NonPublic)?.DeclaringType != typeof(Drawable);
// Exclude objects that specify they should be hidden anyway.
valid &= !type.GetCustomAttributes<DrawVisualiserHiddenAttribute>(true).Any();

return is_type_valid_target_cache[type] = valid;
}
}

public bool Searching { get; private set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;

namespace osu.Framework.Graphics.Visualisation
{
/// <summary>
/// Indicates that instances of this type or any subtype should not be valid targets for the draw visualiser.
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public class DrawVisualiserHiddenAttribute : Attribute;
}
Loading