Merge remote-tracking branch 'origin/master' into 1.7

# Conflicts:
#	Flax.flaxproj
This commit is contained in:
Wojtek Figat
2023-10-09 12:40:47 +02:00
86 changed files with 1406 additions and 686 deletions

View File

@@ -37,11 +37,6 @@ namespace FlaxEditor.Modules
/// </summary>
public event Action<Prefab, Actor> PrefabApplied;
/// <summary>
/// Locally cached actor for prefab creation.
/// </summary>
private Actor _prefabCreationActor;
internal PrefabsModule(Editor editor)
: base(editor)
{
@@ -65,13 +60,14 @@ namespace FlaxEditor.Modules
/// To create prefab manually (from code) use <see cref="PrefabManager.CreatePrefab"/> method.
/// </remarks>
/// <param name="selection">The scene selection to use.</param>
public void CreatePrefab(List<SceneGraphNode> selection)
/// <param name="prefabWindow">The prefab window that creates it.</param>
public void CreatePrefab(List<SceneGraphNode> selection, Windows.Assets.PrefabWindow prefabWindow = null)
{
if (selection == null)
selection = Editor.SceneEditing.Selection;
if (selection.Count == 1 && selection[0] is ActorNode actorNode && actorNode.CanCreatePrefab)
{
CreatePrefab(actorNode.Actor);
CreatePrefab(actorNode.Actor, true, prefabWindow);
}
}
@@ -92,7 +88,8 @@ namespace FlaxEditor.Modules
/// </summary>
/// <param name="actor">The root prefab actor.</param>
/// <param name="rename">Allow renaming or not</param>
public void CreatePrefab(Actor actor, bool rename)
/// <param name="prefabWindow">The prefab window that creates it.</param>
public void CreatePrefab(Actor actor, bool rename, Windows.Assets.PrefabWindow prefabWindow = null)
{
// Skip in invalid states
if (!Editor.StateMachine.CurrentState.CanEditContent)
@@ -105,42 +102,47 @@ namespace FlaxEditor.Modules
PrefabCreating?.Invoke(actor);
var proxy = Editor.ContentDatabase.GetProxy<Prefab>();
_prefabCreationActor = actor;
Editor.Windows.ContentWin.NewItem(proxy, actor, OnPrefabCreated, actor.Name, rename);
Editor.Windows.ContentWin.NewItem(proxy, actor, contentItem => OnPrefabCreated(contentItem, actor, prefabWindow), actor.Name, rename);
}
private void OnPrefabCreated(ContentItem contentItem)
private void OnPrefabCreated(ContentItem contentItem, Actor actor, Windows.Assets.PrefabWindow prefabWindow)
{
if (contentItem is PrefabItem prefabItem)
{
PrefabCreated?.Invoke(prefabItem);
}
// Skip in invalid states
if (!Editor.StateMachine.CurrentState.CanEditScene)
return;
Undo undo = null;
if (prefabWindow != null)
{
prefabWindow.MarkAsEdited();
undo = prefabWindow.Undo;
}
else
{
// Skip in invalid states
if (!Editor.StateMachine.CurrentState.CanEditScene)
return;
undo = Editor.Undo;
}
// Record undo for prefab creating (backend links the target instance with the prefab)
if (Editor.Undo.Enabled)
if (undo.Enabled)
{
if (!_prefabCreationActor)
if (!actor)
return;
var actorsList = new List<Actor>();
Utilities.Utils.GetActorsTree(actorsList, _prefabCreationActor);
Utilities.Utils.GetActorsTree(actorsList, actor);
var actions = new IUndoAction[actorsList.Count];
for (int i = 0; i < actorsList.Count; i++)
{
var action = BreakPrefabLinkAction.Linked(actorsList[i]);
actions[i] = action;
}
Undo.AddAction(new MultiUndoAction(actions));
_prefabCreationActor = null;
actions[i] = BreakPrefabLinkAction.Linked(actorsList[i]);
undo.AddAction(new MultiUndoAction(actions));
}
Editor.Instance.Windows.PropertiesWin.Presenter.BuildLayout();
Editor.Windows.PropertiesWin.Presenter.BuildLayout();
if (prefabWindow != null)
prefabWindow.Presenter.BuildLayout();
}
/// <summary>

View File

@@ -2,8 +2,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using FlaxEditor.Options;
using FlaxEditor.Scripting;
using FlaxEngine;
@@ -330,6 +332,78 @@ namespace FlaxEditor.Modules.SourceCodeEditing
Editor.Instance.CodeEditing.SelectedEditor = editor;
}
/// <summary>
/// Starts creating a new module
/// </summary>
internal void CreateModule(string path, string moduleName, bool editorModule, bool cpp)
{
if (string.IsNullOrEmpty(moduleName) || string.IsNullOrEmpty(path))
{
Editor.LogWarning("Failed to create module due to no name");
return;
}
// Create folder
var moduleFolderPath = Path.Combine(path, moduleName);
Directory.CreateDirectory(moduleFolderPath);
// Create module
var moduleText = "using Flax.Build;\n" +
"using Flax.Build.NativeCpp;\n" +
$"\npublic class {moduleName} : Game{(editorModule ? "Editor" : "")}Module\n" +
"{\n " +
"/// <inheritdoc />\n" +
" public override void Init()\n" +
" {\n" +
" base.Init();\n" +
"\n" +
" // C#-only scripting if false\n" +
$" BuildNativeCode = {(cpp ? "true" : "false")};\n" +
" }\n" +
"\n" +
" /// <inheritdoc />\n" +
" public override void Setup(BuildOptions options)\n" +
" {" +
"\n" +
" base.Setup(options);\n" +
"\n" +
" options.ScriptingAPI.IgnoreMissingDocumentationWarnings = true;\n" +
"\n" +
" // Here you can modify the build options for your game module\n" +
" // To reference another module use: options.PublicDependencies.Add(\"Audio\");\n" +
" // To add C++ define use: options.PublicDefinitions.Add(\"COMPILE_WITH_FLAX\");\n" +
" // To learn more see scripting documentation.\n" +
" }\n" +
"}";
moduleText = Encoding.UTF8.GetString(Encoding.Default.GetBytes(moduleText));
var modulePath = Path.Combine(moduleFolderPath, $"{moduleName}.Build.cs");
File.WriteAllText(modulePath, moduleText);
Editor.Log($"Module created at {modulePath}");
// Get editor target and target files and add module
var files = Directory.GetFiles(path);
var targetModuleText = $"Modules.Add(\"{moduleName}\");\n ";
foreach (var file in files)
{
if (!file.Contains(".Build.cs", StringComparison.OrdinalIgnoreCase))
continue;
var targetText = File.ReadAllText(file);
// Skip game project if it is suppose to be an editor module
if (editorModule && targetText.Contains("GameProjectTarget", StringComparison.Ordinal))
continue;
// TODO: Handle edge case when there are no modules in a target
var index = targetText.IndexOf("Modules.Add");
if (index != -1)
{
var newText = targetText.Insert(index, targetModuleText);
File.WriteAllText(file, newText);
Editor.Log($"Module added to Target: {file}");
}
}
}
/// <inheritdoc />
public override void OnUpdate()
{