-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlugin.cs
More file actions
88 lines (72 loc) · 2.91 KB
/
Plugin.cs
File metadata and controls
88 lines (72 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Originally written by wmigor
// Edited by Atlinx to recursively search for files.
// Edited by bls220 to update for Godot 4.0
// wmigor's Public Repo: https://github.com/wmigor/godot-mono-custom-resource-register
using System.Collections.Generic;
using Godot;
using GodotCustomTypes.Attributes;
using GodotCustomTypes.Metadata;
namespace GodotCustomTypes
{
#if TOOLS
[Tool]
[MuteTypeRegistration]
public partial class Plugin : EditorPlugin
{
private readonly ITypesLoader _typesLoader = new SortedTypesLoaderDecorator(new TypesLoader());
// We're not going to hijack the Mono Build button since it actually takes time to build
// and we can't be sure how long that is. I guess we have to leave refreshing to the user for now.
// There isn't any automation we can do to fix that.
// private Button MonoBuildButton => GetNode<Button>("/root/EditorNode/@@580/@@581/@@589/@@590/Button");
private readonly List<string> customTypes = new List<string>();
private Button? refreshButton;
public override void _EnterTree()
{
refreshButton = new Button();
refreshButton.Text = "CCR";
AddControlToContainer(CustomControlContainer.Toolbar, refreshButton);
refreshButton.Icon = EditorInterface.Singleton.GetBaseControl().GetThemeIcon("Reload", "EditorIcons");
refreshButton.Pressed += OnRefreshPressed;
Settings.Init();
RefreshCustomClasses();
GD.PushWarning("You may change any setting for MonoCustomResourceRegistry in Project -> ProjectSettings -> General -> MonoCustomResourceRegistry");
}
public override void _ExitTree()
{
UnregisterCustomClasses();
RemoveControlFromContainer(CustomControlContainer.Toolbar, refreshButton);
refreshButton?.QueueFree();
}
public void RefreshCustomClasses()
{
GD.Print("Refreshing Registered Resources...");
UnregisterCustomClasses();
RegisterCustomClasses();
}
private void RegisterCustomClasses()
{
customTypes.Clear();
var types = _typesLoader.LoadCustomTypes();
foreach (var type in types)
{
AddCustomType(type.TypeName, type.BaseTypeName, type.Script, type.Icon);
customTypes.Add(type.TypeName);
GD.Print($"Loaded custom type: {type.TypeName} -> {type.Script.GetPath()}");
}
}
private void UnregisterCustomClasses()
{
foreach (var script in customTypes)
{
RemoveCustomType(script);
GD.Print($"Unregister custom resource: {script}");
}
customTypes.Clear();
}
private void OnRefreshPressed()
{
RefreshCustomClasses();
}
}
#endif
}