Skip to content

Commit

Permalink
Merge pull request #6411 from smoogipoo/drawvis-hidden-attribute
Browse files Browse the repository at this point in the history
Add attribute to hide arbitrary drawables from draw visualiser
  • Loading branch information
peppy authored Nov 5, 2024
2 parents 451f8b2 + 0303aa6 commit 6a1a7a6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
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;
}

0 comments on commit 6a1a7a6

Please sign in to comment.