Allow users to choose what type of widget they want to create.

This commit is contained in:
Menotdan
2024-04-11 00:03:04 -04:00
parent fe6c254a24
commit 019230f6d9
2 changed files with 112 additions and 13 deletions

View File

@@ -3,12 +3,13 @@
using System;
using FlaxEditor.Scripting;
using FlaxEngine;
using FlaxEngine.GUI;
using Object = FlaxEngine.Object;
namespace FlaxEditor.Content.Create
{
/// <summary>
/// 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.
/// </summary>
/// <seealso cref="FlaxEditor.Content.Create.CreateFileEntry" />
public class PrefabCreateEntry : CreateFileEntry
@@ -69,4 +70,112 @@ namespace FlaxEditor.Content.Create
return PrefabManager.CreatePrefab(actor, ResultUrl, true);
}
}
/// <summary>
/// Widget asset creating handler. Allows to specify base UIControl to use as the root.
/// </summary>
/// <seealso cref="FlaxEditor.Content.Create.CreateFileEntry" />
public class WidgetCreateEntry : CreateFileEntry
{
/// <summary>
/// The create options.
/// </summary>
public class Options
{
/// <summary>
/// Which mode is used to initialize this widget.
/// </summary>
public enum WidgetMode
{
/// <summary>
/// Initialize the widget with a UICanvas.
/// </summary>
Canvas,
/// <summary>
/// Initialize the widget with a UIControl.
/// </summary>
Control
}
/// <summary>
/// The mode used to initialize the widget.
/// </summary>
[Tooltip("Whether to initialize the widget with a canvas or a control.")]
public WidgetMode WidgetInitializationMode = WidgetMode.Canvas;
bool ShowRoot => WidgetInitializationMode == WidgetMode.Control;
/// <summary>
/// The root control.
/// </summary>
[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();
/// <inheritdoc />
public override object Settings => _options;
/// <summary>
/// Initializes a new instance of the <see cref="WidgetCreateEntry"/> class.
/// </summary>
/// <param name="resultUrl">The result file url.</param>
public WidgetCreateEntry(string resultUrl)
: base("Settings", resultUrl)
{
}
/// <inheritdoc />
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);
}
}
}

View File

@@ -233,18 +233,8 @@ namespace FlaxEditor.Content
/// <inheritdoc />
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;
}
}
}