From 019230f6d9077ec025a5c6a84cc5bf4d8a9013f3 Mon Sep 17 00:00:00 2001 From: Menotdan <32620310+Menotdan@users.noreply.github.com> Date: Thu, 11 Apr 2024 00:03:04 -0400 Subject: [PATCH] Allow users to choose what type of widget they want to create. --- .../Content/Create/PrefabCreateEntry.cs | 111 +++++++++++++++++- Source/Editor/Content/Proxy/PrefabProxy.cs | 14 +-- 2 files changed, 112 insertions(+), 13 deletions(-) diff --git a/Source/Editor/Content/Create/PrefabCreateEntry.cs b/Source/Editor/Content/Create/PrefabCreateEntry.cs index b307f09dd..e207343a5 100644 --- a/Source/Editor/Content/Create/PrefabCreateEntry.cs +++ b/Source/Editor/Content/Create/PrefabCreateEntry.cs @@ -3,12 +3,13 @@ using System; using FlaxEditor.Scripting; using FlaxEngine; +using FlaxEngine.GUI; using Object = FlaxEngine.Object; namespace FlaxEditor.Content.Create { /// - /// Visual Script asset creating handler. Allows to specify base class to inherit from. + /// Prefab asset creating handler. Allows to specify base actor to use as the root. /// /// public class PrefabCreateEntry : CreateFileEntry @@ -69,4 +70,112 @@ namespace FlaxEditor.Content.Create return PrefabManager.CreatePrefab(actor, ResultUrl, true); } } + + /// + /// Widget asset creating handler. Allows to specify base UIControl to use as the root. + /// + /// + public class WidgetCreateEntry : CreateFileEntry + { + /// + /// The create options. + /// + public class Options + { + /// + /// Which mode is used to initialize this widget. + /// + public enum WidgetMode + { + /// + /// Initialize the widget with a UICanvas. + /// + Canvas, + + /// + /// Initialize the widget with a UIControl. + /// + Control + } + + /// + /// The mode used to initialize the widget. + /// + [Tooltip("Whether to initialize the widget with a canvas or a control.")] + public WidgetMode WidgetInitializationMode = WidgetMode.Canvas; + + bool ShowRoot => WidgetInitializationMode == WidgetMode.Control; + + /// + /// The root control. + /// + [TypeReference(typeof(Control), nameof(IsValid))] + [Tooltip("The control type of the root of the new Widget's root control."), VisibleIf(nameof(ShowRoot))] + public Type RootControlType = typeof(Panel); + + private static bool IsValid(Type type) + { + return (type.IsPublic || type.IsNestedPublic) && !type.IsAbstract && !type.IsGenericType; + } + } + + private readonly Options _options = new Options(); + + /// + public override object Settings => _options; + + /// + /// Initializes a new instance of the class. + /// + /// The result file url. + public WidgetCreateEntry(string resultUrl) + : base("Settings", resultUrl) + { + } + + /// + public override bool Create() + { + Actor actor = null; + + if (_options.WidgetInitializationMode == Options.WidgetMode.Control) + { + if (_options.RootControlType == null) + _options.RootControlType = typeof(Control); + + ScriptType controlType = new ScriptType(_options.RootControlType); + + Control control = null; + try + { + control = controlType.CreateInstance() as Control; + } + catch (Exception ex) + { + Editor.LogError("Failed to create widget with root control type: " + controlType.Name); + Editor.LogWarning(ex); + return true; + } + + UIControl newControl = new UIControl(); + newControl.Control = control; + + actor = newControl; + } + else if (_options.WidgetInitializationMode == Options.WidgetMode.Canvas) + { + actor = new UICanvas(); + } + + if (actor == null) + { + Editor.LogError("Failed to create widget. Final actor was null."); + return true; + } + + Object.Destroy(actor, 20.0f); + + return PrefabManager.CreatePrefab(actor, ResultUrl, true); + } + } } diff --git a/Source/Editor/Content/Proxy/PrefabProxy.cs b/Source/Editor/Content/Proxy/PrefabProxy.cs index a97b6232f..fd4eefdf2 100644 --- a/Source/Editor/Content/Proxy/PrefabProxy.cs +++ b/Source/Editor/Content/Proxy/PrefabProxy.cs @@ -233,18 +233,8 @@ namespace FlaxEditor.Content /// public override void Create(string outputPath, object arg) { - // Create prefab with UI Control - var actor = new UIControl - { - Name = Path.GetFileNameWithoutExtension(outputPath), - StaticFlags = StaticFlags.None, - }; - actor.Control = new Button - { - Text = "Button", - }; - PrefabManager.CreatePrefab(actor, outputPath, false); - Object.Destroy(actor, 20.0f); + Editor.Instance.ContentImporting.Create(new WidgetCreateEntry(outputPath)); + return; } } }