1 using System.Collections;
2 using System.Collections.Generic;
7 namespace AVR.UEditor.Core {
39 public abstract class AVR_HookableWizard<T> : EditorWindow where T : AVR_HookableWizard<T>
41 private static List<AVR_WizardHook<T>> registered_hooks =
new List<AVR_WizardHook<T>>();
45 public static void ShowWindow(GameObject _targetObject,
string windowName =
"Wizard")
47 registered_hooks.Clear();
48 targetObject = _targetObject;
51 List<System.Type> hook_classes = System.AppDomain.CurrentDomain.GetAssemblies().SelectMany(assembly => assembly.GetTypes()).Where(type => type.IsSubclassOf(typeof(AVR_WizardHook<T>))).ToList();
53 foreach (System.Type hook in hook_classes) {
55 if(hook.IsAbstract)
continue;
57 AVR_WizardHook<T> o = (AVR_WizardHook<T>)System.Activator.CreateInstance(hook);
59 registered_hooks.Add(o);
60 o.on_create_wizard(targetObject);
63 registered_hooks.Sort((a, b) => a.category.CompareTo(b.category));
65 EditorWindow.GetWindow(typeof(T),
true, windowName);
70 EditorGUILayout.BeginVertical();
72 foreach(AVR_WizardHook<T> o
in registered_hooks) {
73 EditorGUILayout.BeginVertical();
77 EditorGUILayout.EndVertical();
78 EditorGUILayout.Space();
81 EditorGUILayout.EndVertical();
83 if(GUILayout.Button(
"Update")) {
84 foreach (AVR_WizardHook<T> o
in registered_hooks) {
85 o.on_submit(targetObject);
95 public abstract class AVR_WizardHook<Wiz> where Wiz : AVR_HookableWizard<Wiz>
98 protected static Dictionary<string, bool> flags =
new Dictionary<string, bool>();
100 public virtual int category
112 EditorGUILayout.LabelField(
"Unnamed Module");
117 PrefabUtility.UnpackPrefabInstance(targetObject, PrefabUnpackMode.OutermostRoot, InteractionMode.AutomatedAction);
122 GameObject.DestroyImmediate(toDestroy);
124 catch(System.InvalidOperationException) {
125 PrefabUtility.UnpackPrefabInstance(wizardTarget, PrefabUnpackMode.OutermostRoot, InteractionMode.AutomatedAction);
126 GameObject.DestroyImmediate(toDestroy);
131 public abstract class AVR_WizardHook_SimpleToggle<Wiz, Mod> : AVR_WizardHook<Wiz> where Wiz : AVR_HookableWizard<Wiz> where Mod : MonoBehaviour
133 protected abstract string moduleName {
get; }
135 protected abstract string prefabPathSettingsToken {
get; }
137 protected virtual string[] dependencies =>
new string[] {};
145 _module = targetObject.GetComponentsInChildren<Mod>();
146 module = _module.Length > 0;
147 flags[moduleName] = module;
152 module = EditorGUILayout.BeginToggleGroup(moduleName, module);
154 EditorGUILayout.EndToggleGroup();
157 string[] missing_d = dependencies.Where((s) => !flags[s]).ToArray();
159 if(module && missing_d.Length > 0) {
160 EditorGUILayout.HelpBox(moduleName +
" needs " + string.Join(
", ", missing_d), MessageType.Error);
164 flags[moduleName] = module;
169 if (module && _module.Length < 1)
171 AVR_EditorUtility.InstantiatePrefabAsChild(targetObject.transform, prefabPathSettingsToken);
173 else if (!module && _module.Length > 0)
175 foreach (Mod c
in _module) safeDestroyImmediate(c.gameObject, targetObject);
180 public abstract class AVR_WizardHook_SimpleFilteredToggle<Wiz, Mod> : AVR_WizardHook_SimpleToggle<Wiz, Mod> where Wiz : AVR_HookableWizard<Wiz> where Mod : MonoBehaviour
182 protected abstract System.Func<Mod,
bool> filter {
get; }
186 _module = targetObject.GetComponentsInChildren<Mod>().Where(filter).ToArray();
187 module = _module.Length > 0;
188 flags[moduleName] = module;
192 public abstract class AVR_WizardHook_DropdownChoiceToggle<Wiz, Mod> : AVR_WizardHook<Wiz> where Wiz : AVR_HookableWizard<Wiz> where Mod : MonoBehaviour
194 protected abstract string moduleName {
get; }
196 protected abstract DDChoice[] options {
get; }
198 protected virtual string[] dependencies =>
new string[] { };
200 protected struct DDChoice {
201 public DDChoice(
string choiceName,
string prefabPathSettingsToken, System.Func<Mod,
bool> filter) {
202 this.choiceName = choiceName;
203 this.prefabPathSettingsToken = prefabPathSettingsToken;
204 this.filter = filter;
223 _module = targetObject.GetComponentsInChildren<Mod>();
224 moduleTypeList = options.Select(t => t.choiceName).ToArray();
228 foreach(var entry
in options) {
229 if(_module.Any(entry.filter)) {
230 _prevselected = _selected = entry;
236 flags[moduleName] = module;
241 module = EditorGUILayout.BeginToggleGroup(moduleName, module);
243 int index = Mathf.Max(0, System.Array.FindIndex(moduleTypeList, w => w == _selected.choiceName));
244 index = EditorGUILayout.Popup(index, moduleTypeList);
245 _selected = options[index];
247 EditorGUILayout.EndToggleGroup();
250 string[] missing_d = dependencies.Where((s) => !flags[s]).ToArray();
252 if (module && missing_d.Length > 0)
254 EditorGUILayout.HelpBox(moduleName +
" needs " + string.Join(
", ", missing_d), MessageType.Error);
258 flags[moduleName] = module;
263 if (module && _module.Length < 1)
265 AVR_EditorUtility.InstantiatePrefabAsChild(targetObject.transform, _selected.prefabPathSettingsToken);
267 else if (module && _module.Length > 0)
269 Mod first = _module[0];
270 if (!_selected.filter.Invoke(first)) {
271 safeDestroyImmediate(first.gameObject, targetObject);
272 AVR_EditorUtility.InstantiatePrefabAsChild(targetObject.transform, _selected.prefabPathSettingsToken);
275 else if (!module && _module.Length > 0)
277 foreach (Mod c
in _module) safeDestroyImmediate(c.gameObject, targetObject);
System.Func< Mod, bool > filter
virtual void on_submit(GameObject targetObject)
override void on_submit(GameObject targetObject)
override void on_create_wizard(GameObject targetObject)
static GameObject targetObject
virtual void on_create_wizard(GameObject targetObject)
string prefabPathSettingsToken
static void ShowWindow(GameObject _targetObject, string windowName="Wizard")
DDChoice(string choiceName, string prefabPathSettingsToken, System.Func< Mod, bool > filter)
override void embed_GUI()
override void on_create_wizard(GameObject targetObject)
override void on_create_wizard(GameObject targetObject)
override void on_submit(GameObject targetObject)
virtual void safeDestroyImmediate(GameObject toDestroy, GameObject wizardTarget)
override void embed_GUI()