Cleanup code

#3359
This commit is contained in:
Wojtek Figat
2025-04-13 21:46:04 +02:00
parent b3ab1b7a4a
commit d351c96a40

View File

@@ -8,6 +8,7 @@ using Real = System.Single;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using FlaxEditor.Content; using FlaxEditor.Content;
using FlaxEditor.GUI.ContextMenu; using FlaxEditor.GUI.ContextMenu;
using FlaxEditor.Windows; using FlaxEditor.Windows;
@@ -29,22 +30,20 @@ namespace FlaxEditor.SceneGraph.Actors
private Model _selectionPointsModel; private Model _selectionPointsModel;
/// <summary> /// <summary>
/// Wether the model of the static model is one of the primitive models. /// Whether the model of the static model is one of the primitive models (box/sphere/capsule/etc.).
/// </summary> /// </summary>
public bool IsPrimitive public bool IsPrimitive
{ {
get get
{ {
Model model = ((StaticModel)Actor).Model; Model model = ((StaticModel)Actor).Model;
if (!model) if (!model)
return false; return false;
string path = model.Path;
string modelPath = model.Path; return path.EndsWith("/Primitives/Cube.flax", StringComparison.Ordinal) ||
return modelPath.EndsWith("/Primitives/Cube.flax", StringComparison.Ordinal) || path.EndsWith("/Primitives/Sphere.flax", StringComparison.Ordinal) ||
modelPath.EndsWith("/Primitives/Sphere.flax", StringComparison.Ordinal) || path.EndsWith("/Primitives/Plane.flax", StringComparison.Ordinal) ||
modelPath.EndsWith("/Primitives/Plane.flax", StringComparison.Ordinal) || path.EndsWith("/Primitives/Capsule.flax", StringComparison.Ordinal);
modelPath.EndsWith("/Primitives/Capsule.flax", StringComparison.Ordinal);
} }
} }
@@ -121,21 +120,12 @@ namespace FlaxEditor.SceneGraph.Actors
{ {
base.OnContextMenu(contextMenu, window); base.OnContextMenu(contextMenu, window);
// Check that every selected node is a primitive... // Check if every selected node is a primitive
var selection = Array.Empty<SceneGraphNode>(); var selection = GetSelection(window);
if (window is SceneTreeWindow)
selection = Editor.Instance.SceneEditing.Selection.ToArray();
else if (window is PrefabWindow prefabWindow)
selection = prefabWindow.Selection.ToArray();
bool autoOptionEnabled = true; bool autoOptionEnabled = true;
foreach (var node in selection) foreach (var node in selection)
{ {
if (node is not StaticModelNode staticModelNode) if (node is StaticModelNode staticModelNode && !staticModelNode.IsPrimitive)
continue;
if (!((StaticModel)staticModelNode.Actor).Model)
continue;
if (!staticModelNode.IsPrimitive)
{ {
autoOptionEnabled = false; autoOptionEnabled = false;
break; break;
@@ -146,7 +136,6 @@ namespace FlaxEditor.SceneGraph.Actors
menu.Enabled = ((StaticModel)Actor).Model != null; menu.Enabled = ((StaticModel)Actor).Model != null;
var b = menu.ContextMenu.AddButton("Auto", () => OnAddCollider(window, CreateAuto)); var b = menu.ContextMenu.AddButton("Auto", () => OnAddCollider(window, CreateAuto));
b.TooltipText = "Add the best fitting collider to every model that uses an in-built Editor primitive."; b.TooltipText = "Add the best fitting collider to every model that uses an in-built Editor primitive.";
// ... and if not, disable the "Auto" option
b.Enabled = autoOptionEnabled; b.Enabled = autoOptionEnabled;
b = menu.ContextMenu.AddButton("Box", () => OnAddCollider(window, CreateBox)); b = menu.ContextMenu.AddButton("Box", () => OnAddCollider(window, CreateBox));
b.TooltipText = "Add a box collider to every selected model that will auto resize based on the model bounds."; b.TooltipText = "Add a box collider to every selected model that will auto resize based on the model bounds.";
@@ -198,8 +187,18 @@ namespace FlaxEditor.SceneGraph.Actors
} }
private delegate void Spawner(Collider collider); private delegate void Spawner(Collider collider);
private delegate void CreateCollider(StaticModel actor, Spawner spawner, bool singleNode); private delegate void CreateCollider(StaticModel actor, Spawner spawner, bool singleNode);
private IEnumerable<SceneGraphNode> GetSelection(EditorWindow window)
{
if (window is SceneTreeWindow)
return Editor.Instance.SceneEditing.Selection;
if (window is PrefabWindow prefabWindow)
return prefabWindow.Selection;
return Array.Empty<SceneGraphNode>();
}
private void CreateAuto(StaticModel actor, Spawner spawner, bool singleNode) private void CreateAuto(StaticModel actor, Spawner spawner, bool singleNode)
{ {
// Special case for in-built Editor models that can use analytical collision // Special case for in-built Editor models that can use analytical collision
@@ -296,13 +295,8 @@ namespace FlaxEditor.SceneGraph.Actors
private void OnAddCollider(EditorWindow window, CreateCollider createCollider) private void OnAddCollider(EditorWindow window, CreateCollider createCollider)
{ {
// Allow collider to be added to evey static model selection // Allow collider to be added to every static model selection
var selection = Array.Empty<SceneGraphNode>(); var selection = GetSelection(window).ToArray();
if (window is SceneTreeWindow)
selection = Editor.Instance.SceneEditing.Selection.ToArray();
else if (window is PrefabWindow prefabWindow)
selection = prefabWindow.Selection.ToArray();
var createdNodes = new List<SceneGraphNode>(); var createdNodes = new List<SceneGraphNode>();
foreach (var node in selection) foreach (var node in selection)
{ {