update forge
This commit is contained in:
@@ -13,28 +13,22 @@ public partial class CueHandlerInspectorPlugin : EditorInspectorPlugin
|
||||
public override bool _CanHandle(GodotObject @object)
|
||||
{
|
||||
// Find out if its an implementation of CueHandler without having to add [Tool] attribute to them.
|
||||
try
|
||||
if (@object?.GetScript().As<CSharpScript>() is CSharpScript script)
|
||||
{
|
||||
if (@object?.GetScript().As<CSharpScript>() is null)
|
||||
return false;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return false;
|
||||
StringName className = script.GetGlobalName();
|
||||
|
||||
Type baseType = typeof(ForgeCueHandler);
|
||||
System.Reflection.Assembly assembly = baseType.Assembly;
|
||||
|
||||
Type? implementationType =
|
||||
Array.Find(assembly.GetTypes(), x =>
|
||||
x.Name == className &&
|
||||
baseType.IsAssignableFrom(x));
|
||||
|
||||
return implementationType is not null;
|
||||
}
|
||||
|
||||
var script = @object?.GetScript().As<CSharpScript>();
|
||||
StringName className = script.GetGlobalName();
|
||||
|
||||
Type baseType = typeof(ForgeCueHandler);
|
||||
System.Reflection.Assembly assembly = baseType.Assembly;
|
||||
|
||||
Type? implementationType =
|
||||
Array.Find(assembly.GetTypes(), x =>
|
||||
x.Name == className &&
|
||||
baseType.IsAssignableFrom(x));
|
||||
|
||||
return implementationType is not null;
|
||||
return false;
|
||||
}
|
||||
|
||||
public override bool _ParseProperty(
|
||||
|
||||
@@ -9,12 +9,15 @@ using Godot;
|
||||
namespace Gamesmiths.Forge.Godot.Editor.Cues;
|
||||
|
||||
[Tool]
|
||||
public partial class CueKeyEditorProperty : EditorProperty
|
||||
public partial class CueKeyEditorProperty : EditorProperty, ISerializationListener
|
||||
{
|
||||
private const int ButtonSize = 26;
|
||||
private const int PopupSize = 300;
|
||||
|
||||
private Label _label = null!;
|
||||
private Label? _label;
|
||||
private Button? _button;
|
||||
private Popup? _popup;
|
||||
private Tree? _tree;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
@@ -24,72 +27,63 @@ public partial class CueKeyEditorProperty : EditorProperty
|
||||
|
||||
var hbox = new HBoxContainer();
|
||||
_label = new Label { Text = "None", SizeFlagsHorizontal = SizeFlags.ExpandFill };
|
||||
var button = new Button { Icon = dropdownIcon, CustomMinimumSize = new Vector2(ButtonSize, 0) };
|
||||
_button = new Button { Icon = dropdownIcon, CustomMinimumSize = new Vector2(ButtonSize, 0) };
|
||||
|
||||
hbox.AddChild(_label);
|
||||
hbox.AddChild(button);
|
||||
hbox.AddChild(_button);
|
||||
AddChild(hbox);
|
||||
|
||||
var popup = new Popup { Size = new Vector2I(PopupSize, PopupSize) };
|
||||
var tree = new Tree
|
||||
_popup = new Popup { Size = new Vector2I(PopupSize, PopupSize) };
|
||||
_tree = new Tree
|
||||
{
|
||||
HideRoot = true,
|
||||
AnchorRight = 1,
|
||||
AnchorBottom = 1,
|
||||
};
|
||||
popup.AddChild(tree);
|
||||
_popup.AddChild(_tree);
|
||||
|
||||
var backgroundStyle = new StyleBoxFlat
|
||||
{
|
||||
BgColor = EditorInterface.Singleton.GetEditorTheme().GetColor("base_color", "Editor"),
|
||||
};
|
||||
tree.AddThemeStyleboxOverride("panel", backgroundStyle);
|
||||
_tree.AddThemeStyleboxOverride("panel", backgroundStyle);
|
||||
|
||||
AddChild(popup);
|
||||
AddChild(_popup);
|
||||
|
||||
ForgeData pluginData = ResourceLoader.Load<ForgeData>(ForgeData.ForgeDataResourcePath);
|
||||
var tagsManager = new TagsManager([.. pluginData.RegisteredTags]);
|
||||
TreeItem root = tree.CreateItem();
|
||||
BuildTreeRecursively(tree, root, tagsManager.RootNode);
|
||||
TreeItem root = _tree.CreateItem();
|
||||
BuildTreeRecursively(_tree, root, tagsManager.RootNode);
|
||||
|
||||
button.Pressed += () =>
|
||||
{
|
||||
Window win = GetWindow();
|
||||
popup.Position = (Vector2I)button.GlobalPosition
|
||||
+ win.Position
|
||||
- new Vector2I(PopupSize - ButtonSize, -30);
|
||||
popup.Popup();
|
||||
};
|
||||
|
||||
tree.ItemActivated += () =>
|
||||
{
|
||||
TreeItem item = tree.GetSelected();
|
||||
if (item is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Build full path from root.
|
||||
var segments = new List<string>();
|
||||
TreeItem current = item;
|
||||
while (current.GetParent() is not null)
|
||||
{
|
||||
segments.Insert(0, current.GetText(0));
|
||||
current = current.GetParent();
|
||||
}
|
||||
|
||||
var fullPath = string.Join(".", segments);
|
||||
|
||||
_label.Text = fullPath;
|
||||
EmitChanged(GetEditedProperty(), fullPath);
|
||||
popup.Hide();
|
||||
};
|
||||
_button.Pressed += OnButtonPressed;
|
||||
_tree.ItemActivated += OnTreeItemActivated;
|
||||
}
|
||||
|
||||
public override void _UpdateProperty()
|
||||
{
|
||||
var property = GetEditedObject().Get(GetEditedProperty()).AsString();
|
||||
_label.Text = string.IsNullOrEmpty(property) ? "None" : property;
|
||||
string property = GetEditedObject().Get(GetEditedProperty()).AsString();
|
||||
|
||||
if (_label is not null && IsInstanceValid(_label))
|
||||
{
|
||||
_label.Text = string.IsNullOrEmpty(property) ? "None" : property;
|
||||
}
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
ReleaseUiState();
|
||||
FreeAllChildren();
|
||||
base._ExitTree();
|
||||
}
|
||||
|
||||
public void OnBeforeSerialize()
|
||||
{
|
||||
ReleaseUiState();
|
||||
FreeAllChildren();
|
||||
}
|
||||
|
||||
public void OnAfterDeserialize()
|
||||
{
|
||||
}
|
||||
|
||||
private static void BuildTreeRecursively(Tree tree, TreeItem currentTreeItem, TagNode currentNode)
|
||||
@@ -102,5 +96,76 @@ public partial class CueKeyEditorProperty : EditorProperty
|
||||
BuildTreeRecursively(tree, childTreeNode, childTagNode);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnButtonPressed()
|
||||
{
|
||||
if (_button is null || _popup is null || !IsInstanceValid(_button) || !IsInstanceValid(_popup))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Window win = GetWindow();
|
||||
_popup.Position = (Vector2I)_button.GlobalPosition
|
||||
+ win.Position
|
||||
- new Vector2I(PopupSize - ButtonSize, -30);
|
||||
_popup.Popup();
|
||||
}
|
||||
|
||||
private void OnTreeItemActivated()
|
||||
{
|
||||
if (_tree is null || _popup is null || _label is null
|
||||
|| !IsInstanceValid(_tree) || !IsInstanceValid(_popup) || !IsInstanceValid(_label))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
TreeItem item = _tree.GetSelected();
|
||||
if (item is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var segments = new List<string>();
|
||||
TreeItem current = item;
|
||||
while (current.GetParent() is not null)
|
||||
{
|
||||
segments.Insert(0, current.GetText(0));
|
||||
current = current.GetParent();
|
||||
}
|
||||
|
||||
string fullPath = string.Join(".", segments);
|
||||
|
||||
_label.Text = fullPath;
|
||||
EmitChanged(GetEditedProperty(), fullPath);
|
||||
_popup.Hide();
|
||||
}
|
||||
|
||||
private void ReleaseUiState()
|
||||
{
|
||||
if (_button is not null && IsInstanceValid(_button))
|
||||
{
|
||||
_button.Pressed -= OnButtonPressed;
|
||||
}
|
||||
|
||||
if (_tree is not null && IsInstanceValid(_tree))
|
||||
{
|
||||
_tree.ItemActivated -= OnTreeItemActivated;
|
||||
}
|
||||
|
||||
_label = null;
|
||||
_button = null;
|
||||
_popup = null;
|
||||
_tree = null;
|
||||
}
|
||||
|
||||
private void FreeAllChildren()
|
||||
{
|
||||
for (int i = GetChildCount() - 1; i >= 0; i--)
|
||||
{
|
||||
Node child = GetChild(i);
|
||||
RemoveChild(child);
|
||||
child.Free();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user