Add Create collision data action to be performed for each model selected in the Content Window

This commit is contained in:
Wojtek Figat
2023-04-17 12:20:02 +02:00
parent 71b5b528fd
commit 2eea7abc27
3 changed files with 87 additions and 30 deletions

View File

@@ -80,7 +80,8 @@ namespace FlaxEditor.Content
/// </summary>
/// <param name="model">The associated model.</param>
/// <param name="created">The action to call once the collision data gets created (or reused from existing).</param>
public void CreateCollisionDataFromModel(Model model, Action<CollisionData> created = null)
/// <param name="withRenaming">True if start initial item renaming by user, or tru to skip it.</param>
public void CreateCollisionDataFromModel(Model model, Action<CollisionData> created = null, bool withRenaming = true)
{
// Check if there already is collision data for that model to reuse
var modelItem = (AssetItem)Editor.Instance.ContentDatabase.Find(model.ID);
@@ -140,7 +141,7 @@ namespace FlaxEditor.Content
});
};
var initialName = (modelItem?.ShortName ?? Path.GetFileNameWithoutExtension(model.Path)) + " Collision";
Editor.Instance.Windows.ContentWin.NewItem(this, null, create, initialName);
Editor.Instance.Windows.ContentWin.NewItem(this, null, create, initialName, withRenaming);
}
}
}

View File

@@ -47,9 +47,23 @@ namespace FlaxEditor.Content
menu.AddButton("Create collision data", () =>
{
var model = FlaxEngine.Content.LoadAsync<Model>(((ModelItem)item).ID);
var collisionDataProxy = (CollisionDataProxy)Editor.Instance.ContentDatabase.GetProxy<CollisionData>();
collisionDataProxy.CreateCollisionDataFromModel(model);
var selection = Editor.Instance.Windows.ContentWin.View.Selection;
if (selection.Count > 1)
{
// Batch action
var items = selection.ToArray(); // Clone to prevent issue when iterating over and content window changes the selection
foreach (var contentItem in items)
{
if (contentItem is ModelItem modelItem)
collisionDataProxy.CreateCollisionDataFromModel(FlaxEngine.Content.LoadAsync<Model>(modelItem.ID), null, false);
}
}
else
{
var model = FlaxEngine.Content.LoadAsync<Model>(((ModelItem)item).ID);
collisionDataProxy.CreateCollisionDataFromModel(model);
}
});
}

View File

@@ -319,6 +319,7 @@ namespace FlaxEditor.Windows
/// Shows popup dialog with UI to rename content item.
/// </summary>
/// <param name="item">The item to rename.</param>
/// <returns>The created renaming popup.</returns>
public void Rename(ContentItem item)
{
// Show element in the view
@@ -336,24 +337,7 @@ namespace FlaxEditor.Windows
popup.Tag = item;
popup.Validate += OnRenameValidate;
popup.Renamed += renamePopup => Rename((ContentItem)renamePopup.Tag, renamePopup.Text);
popup.Closed += renamePopup =>
{
// Restore scrolling in content view
if (_contentViewPanel.VScrollBar != null)
_contentViewPanel.VScrollBar.ThumbEnabled = true;
if (_contentViewPanel.HScrollBar != null)
_contentViewPanel.HScrollBar.ThumbEnabled = true;
ScrollingOnContentView(true);
// Check if was creating new element
if (_newElement != null)
{
// Destroy mock control
_newElement.ParentFolder = null;
_newElement.Dispose();
_newElement = null;
}
};
popup.Closed += OnRenameClosed;
// For new asset we want to mock the initial value so user can press just Enter to use default name
if (_newElement != null)
@@ -367,6 +351,25 @@ namespace FlaxEditor.Windows
return Editor.ContentEditing.IsValidAssetName((ContentItem)popup.Tag, value, out _);
}
private void OnRenameClosed(RenamePopup popup)
{
// Restore scrolling in content view
if (_contentViewPanel.VScrollBar != null)
_contentViewPanel.VScrollBar.ThumbEnabled = true;
if (_contentViewPanel.HScrollBar != null)
_contentViewPanel.HScrollBar.ThumbEnabled = true;
ScrollingOnContentView(true);
// Check if was creating new element
if (_newElement != null)
{
// Destroy mock control
_newElement.ParentFolder = null;
_newElement.Dispose();
_newElement = null;
}
}
/// <summary>
/// Renames the specified item.
/// </summary>
@@ -644,7 +647,8 @@ namespace FlaxEditor.Windows
/// <param name="argument">The argument passed to the proxy for the item creation. In most cases it is null.</param>
/// <param name="created">The event called when the item is crated by the user. The argument is the new item.</param>
/// <param name="initialName">The initial item name.</param>
public void NewItem(ContentProxy proxy, object argument = null, Action<ContentItem> created = null, string initialName = null)
/// <param name="withRenaming">True if start initial item renaming by user, or tru to skip it.</param>
public void NewItem(ContentProxy proxy, object argument = null, Action<ContentItem> created = null, string initialName = null, bool withRenaming = true)
{
Assert.IsNull(_newElement);
if (proxy == null)
@@ -666,14 +670,52 @@ namespace FlaxEditor.Windows
} while (parentFolder.FindChild(path) != null);
}
// Create new asset proxy, add to view and rename it
_newElement = new NewItem(path, proxy, argument)
if (withRenaming)
{
ParentFolder = parentFolder,
Tag = created,
};
RefreshView();
Rename(_newElement);
// Create new asset proxy, add to view and rename it
_newElement = new NewItem(path, proxy, argument)
{
ParentFolder = parentFolder,
Tag = created,
};
RefreshView();
Rename(_newElement);
}
else
{
// Create new asset
try
{
Editor.Log(string.Format("Creating asset {0} in {1}", proxy.Name, path));
proxy.Create(path, argument);
}
catch (Exception ex)
{
Editor.LogWarning(ex);
Editor.LogError("Failed to create asset.");
return;
}
// Focus content window
Focus();
RootWindow?.Focus();
// Refresh database and view now
Editor.ContentDatabase.RefreshFolder(parentFolder, false);
RefreshView();
var newItem = parentFolder.FindChild(path);
if (newItem == null)
{
Editor.LogWarning("Failed to find the created new item.");
return;
}
// Auto-select item
Select(newItem, true);
// Custom post-action
created?.Invoke(newItem);
}
}
private void ContentDatabaseOnItemRemoved(ContentItem contentItem)