fix: only consider nodes in the scene itself.
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
#if TOOLS
|
#if TOOLS
|
||||||
using System;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using Godot;
|
using Godot;
|
||||||
@@ -21,8 +20,6 @@ public partial class Plugin : EditorPlugin
|
|||||||
private ScopeInspectorPlugin? _plugin;
|
private ScopeInspectorPlugin? _plugin;
|
||||||
private SceneTreeTimer? _sceneTreeTimer;
|
private SceneTreeTimer? _sceneTreeTimer;
|
||||||
|
|
||||||
private readonly object _sceneTreeTimerLock = new();
|
|
||||||
|
|
||||||
private bool _treeChangesRegistered;
|
private bool _treeChangesRegistered;
|
||||||
|
|
||||||
public override void _EnterTree()
|
public override void _EnterTree()
|
||||||
@@ -56,7 +53,7 @@ public partial class Plugin : EditorPlugin
|
|||||||
OnSceneChanged(root);
|
OnSceneChanged(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async void OnSceneChanged(Node? sceneRoot)
|
private void OnSceneChanged(Node? sceneRoot)
|
||||||
{
|
{
|
||||||
// register to tree changes, in case the registration wasn't possible before
|
// register to tree changes, in case the registration wasn't possible before
|
||||||
RegisterTreeChanges();
|
RegisterTreeChanges();
|
||||||
@@ -65,38 +62,28 @@ public partial class Plugin : EditorPlugin
|
|||||||
if (sceneRoot == null) return;
|
if (sceneRoot == null) return;
|
||||||
|
|
||||||
// apply debounce
|
// apply debounce
|
||||||
var editedSceneRoot = EditorInterface.Singleton.GetEditedSceneRoot();
|
|
||||||
|
|
||||||
if (!editedSceneRoot.IsInsideTree()) await ToSignal(editedSceneRoot, Node.SignalName.TreeEntered);
|
|
||||||
|
|
||||||
var tree = editedSceneRoot.GetTree();
|
|
||||||
if (tree == null)
|
|
||||||
throw new InvalidOperationException("editedSceneRoot.GetTree() was null even after TreeEntered");
|
|
||||||
|
|
||||||
lock (_sceneTreeTimerLock)
|
|
||||||
{
|
|
||||||
if (_sceneTreeTimer != null)
|
if (_sceneTreeTimer != null)
|
||||||
{
|
{
|
||||||
_sceneTreeTimer.SetTimeLeft(DebounceDelay);
|
_sceneTreeTimer.SetTimeLeft(DebounceDelay);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
_sceneTreeTimer = tree.CreateTimer(DebounceDelay);
|
_sceneTreeTimer = GetTree().CreateTimer(DebounceDelay);
|
||||||
_sceneTreeTimer.Timeout += () => ActualOnSceneChanged(sceneRoot);
|
_sceneTreeTimer.Timeout += () => ActualOnSceneChanged(sceneRoot);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private void ActualOnSceneChanged(Node sceneRoot)
|
private void ActualOnSceneChanged(Node sceneRoot)
|
||||||
{
|
{
|
||||||
// mark debounce as completed
|
// mark debounce as completed
|
||||||
_sceneTreeTimer = null;
|
_sceneTreeTimer = null;
|
||||||
|
|
||||||
var sceneNodes = sceneRoot.FindChildren("*");
|
var sceneNodes = sceneRoot.FindChildren("*").Where(node => node.Owner == sceneRoot).Append(sceneRoot).ToList();
|
||||||
GD.Print($"Updating injection for root {sceneRoot.Name}");
|
GD.Print($"Updating injection for root {sceneRoot.Name}");
|
||||||
|
|
||||||
// look for the service source
|
// look for the service source
|
||||||
var serviceSource = sceneNodes.Where(child => child.IsServiceSource())
|
var serviceSource = sceneNodes.Where(child => child.IsServiceSource())
|
||||||
.Single(out var failureReason);
|
.Single(out var failureReason);
|
||||||
|
|
||||||
switch (failureReason)
|
switch (failureReason)
|
||||||
{
|
{
|
||||||
case EnumerableExtensions.SingleFailureReason.LessThanOne:
|
case EnumerableExtensions.SingleFailureReason.LessThanOne:
|
||||||
@@ -107,7 +94,7 @@ public partial class Plugin : EditorPlugin
|
|||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
// find all nodes that need injection
|
// find all nodes that need injection
|
||||||
var nodesNeedingInjection = sceneNodes.Append(sceneRoot).Where(node =>
|
var nodesNeedingInjection = sceneNodes.Where(node =>
|
||||||
{
|
{
|
||||||
var type = node.GetCSharpScriptType();
|
var type = node.GetCSharpScriptType();
|
||||||
return type != null && (
|
return type != null && (
|
||||||
@@ -126,7 +113,7 @@ public partial class Plugin : EditorPlugin
|
|||||||
{
|
{
|
||||||
newArray ??= new Array(injectedNodes);
|
newArray ??= new Array(injectedNodes);
|
||||||
newArray.Add(node);
|
newArray.Add(node);
|
||||||
GD.Print($"Added node {node.Name} to injection.");
|
GD.Print($"Added node {node.Name} to scope {serviceSource.GetPath()}.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove all nodes that are no longer needing injection
|
// remove all nodes that are no longer needing injection
|
||||||
@@ -137,7 +124,7 @@ public partial class Plugin : EditorPlugin
|
|||||||
if (node == null) continue;
|
if (node == null) continue;
|
||||||
newArray ??= new Array(injectedNodes);
|
newArray ??= new Array(injectedNodes);
|
||||||
newArray.Remove(node);
|
newArray.Remove(node);
|
||||||
GD.Print($"Removed node {node?.Name} from injection.");
|
GD.Print($"Removed node {node.Name} to scope {serviceSource.GetPath()}.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// apply changes to the scene if necessary
|
// apply changes to the scene if necessary
|
||||||
|
|||||||
Reference in New Issue
Block a user