// Copyright (c) Wojciech Figat. All rights reserved. using System; using FlaxEditor.Scripting; using FlaxEngine; using FlaxEngine.GUI; using Object = FlaxEngine.Object; namespace FlaxEditor.Content.Create { /// /// Prefab asset creating handler. Allows to specify base actor to use as the root. /// /// public class PrefabCreateEntry : CreateFileEntry { /// public override bool CanBeCreated => _options.RootActorType != null; /// /// The create options. /// public class Options { /// /// The root actor. /// [TypeReference(typeof(FlaxEngine.Actor), nameof(IsValid))] [Tooltip("The actor type of the root of the new Prefab.")] public Type RootActorType = typeof(EmptyActor); 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 PrefabCreateEntry(string resultUrl) : base("Settings", resultUrl) { } /// public override bool Create() { var actorType = new ScriptType(_options.RootActorType ?? typeof(EmptyActor)); Actor actor; try { actor = actorType.CreateInstance() as Actor; Object.Destroy(actor, 20.0f); } catch (Exception ex) { Editor.LogError("Failed to create prefab with root actor type: " + actorType.Name); Editor.LogWarning(ex); return true; } return PrefabManager.CreatePrefab(actor, ResultUrl, true); } } /// /// Widget asset creating handler. Allows to specify base UIControl to use as the root. /// /// public class WidgetCreateEntry : CreateFileEntry { /// public override bool CanBeCreated => _options.RootControlType != null; /// /// 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.Control; 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(Button); private static bool IsValid(Type type) { var controlTypes = Editor.Instance.CodeEditing.Controls.Get(); return (type.IsPublic || type.IsNestedPublic) && !type.IsAbstract && !type.IsGenericType && controlTypes.Contains(new ScriptType(type)); } } 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) { var controlType = new ScriptType(_options.RootControlType ?? typeof(Control)); Control control; 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; } actor = new UIControl { Control = control, Name = controlType.Name }; } 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); } } }