更新 Source/FLESHEditor/Private/FLESHEditorModule.cpp

This commit is contained in:
2025-04-21 18:20:22 +08:00
parent fe71618b83
commit e235c3ca6e

View File

@@ -10,63 +10,126 @@
void FFLESHEditorModule::StartupModule()
{
// This code will execute after your module is loaded into memory
// The exact timing is specified in the .uplugin file per-module
// Initialize style
FFLESHEditorStyle::Initialize();
FFLESHEditorStyle::ReloadTextures();
// Register commands
FFLESHEditorCommands::Register();
// Map commands
PluginCommands = MakeShareable(new FUICommandList);
PluginCommands->MapAction(
FFLESHEditorCommands::Get().OpenFLESHEditor,
FExecuteAction::CreateRaw(this, &FFLESHEditorModule::OpenFLESHEditor),
FCanExecuteAction());
// Register editor menus
UToolMenus::RegisterStartupCallback(FSimpleMulticastDelegate::FDelegate::CreateLambda([this]()
{
// Add menu items
FToolMenuOwnerScoped OwnerScoped(this);
// Add to main menu
{
UToolMenu* Menu = UToolMenus::Get()->ExtendMenu("LevelEditor.MainMenu.Tools");
FToolMenuSection& Section = Menu->FindOrAddSection("Tools");
Section.AddMenuEntryWithCommandList(FFLESHEditorCommands::Get().OpenFLESHEditor, PluginCommands);
}
// Add to toolbar
{
UToolMenu* ToolbarMenu = UToolMenus::Get()->ExtendMenu("LevelEditor.LevelEditorToolBar");
FToolMenuSection& Section = ToolbarMenu->FindOrAddSection("Settings");
FToolMenuEntry& Entry = Section.AddEntry(FToolMenuEntry::InitToolBarButton(FFLESHEditorCommands::Get().OpenFLESHEditor));
Entry.SetCommandList(PluginCommands);
}
}));
// This code will execute after your module is loaded into memory
// The exact timing is specified in the .uplugin file per-module
// Add try-catch block to prevent crashes during startup
try
{
// Initialize style
FFLESHEditorStyle::Initialize();
FFLESHEditorStyle::ReloadTextures();
// Register commands
FFLESHEditorCommands::Register();
// Map commands
PluginCommands = MakeShareable(new FUICommandList);
PluginCommands->MapAction(
FFLESHEditorCommands::Get().OpenFLESHEditor,
FExecuteAction::CreateRaw(this, &FFLESHEditorModule::OpenFLESHEditorCommand),
FCanExecuteAction());
// Register editor menus
UToolMenus::RegisterStartupCallback(FSimpleMulticastDelegate::FDelegate::CreateLambda([this]()
{
// Add menu items
FToolMenuOwnerScoped OwnerScoped(this);
// Add to main menu
{
UToolMenu* Menu = UToolMenus::Get()->ExtendMenu("LevelEditor.MainMenu.Tools");
FToolMenuSection& Section = Menu->FindOrAddSection("Tools");
Section.AddMenuEntryWithCommandList(FFLESHEditorCommands::Get().OpenFLESHEditor, PluginCommands);
}
// Add to toolbar
{
UToolMenu* ToolbarMenu = UToolMenus::Get()->ExtendMenu("LevelEditor.LevelEditorToolBar");
FToolMenuSection& Section = ToolbarMenu->FindOrAddSection("Settings");
FToolMenuEntry& Entry = Section.AddEntry(FToolMenuEntry::InitToolBarButton(FFLESHEditorCommands::Get().OpenFLESHEditor));
Entry.SetCommandList(PluginCommands);
}
}));
}
catch (const std::exception& e)
{
UE_LOG(LogTemp, Error, TEXT("FLESH Editor module startup failed: %s"), UTF8_TO_TCHAR(e.what()));
}
catch (...)
{
UE_LOG(LogTemp, Error, TEXT("FLESH Editor module startup failed with unknown exception"));
}
}
void FFLESHEditorModule::ShutdownModule()
{
// This function may be called during shutdown to clean up your module
// For modules that support dynamic reloading, we call this function before unloading the module
// Unregister style
FFLESHEditorStyle::Shutdown();
// Unregister editor menus
UToolMenus::UnRegisterStartupCallback(this);
UToolMenus::UnregisterOwner(this);
// This function may be called during shutdown to clean up your module
// For modules that support dynamic reloading, we call this function before unloading the module
// Unregister style
FFLESHEditorStyle::Shutdown();
// Unregister editor menus
UToolMenus::UnRegisterStartupCallback(this);
UToolMenus::UnregisterOwner(this);
}
void FFLESHEditorModule::OpenFLESHEditor()
void FFLESHEditorModule::OpenFLESHEditorCommand()
{
// Open FLESH editor
FFLESHEditor::OpenEditor();
// Open the editor with default parameters
OpenFLESHEditor(EToolkitMode::Standalone, nullptr, nullptr);
}
void FFLESHEditorModule::OpenFLESHEditor(const EToolkitMode::Type Mode, const TSharedPtr<IToolkitHost>& InitToolkitHost, UObject* ObjectToEdit)
{
// Add try-catch block to prevent crashes when opening editor
try
{
// Check if FLESH module is loaded
if (!FModuleManager::Get().IsModuleLoaded("FLESH"))
{
UE_LOG(LogTemp, Error, TEXT("Cannot open FLESH Editor: FLESH module is not loaded"));
return;
}
// If no object is provided, create a default object to edit
if (ObjectToEdit == nullptr)
{
// Try to find or create a dismemberment graph asset to edit
UClass* DismembermentGraphClass = FindObject<UClass>(nullptr, TEXT("/Script/FLESH.DismembermentGraphAsset"));
if (DismembermentGraphClass)
{
// Try to find an existing asset first
ObjectToEdit = FindObject<UObject>(nullptr, TEXT("DefaultDismembermentGraph"));
// If not found, create a temporary object
if (ObjectToEdit == nullptr)
{
ObjectToEdit = NewObject<UObject>(GetTransientPackage(), DismembermentGraphClass, TEXT("DefaultDismembermentGraph"));
ObjectToEdit->AddToRoot(); // Prevent garbage collection
}
}
else
{
// If we can't find the class, create a basic UObject
ObjectToEdit = NewObject<UObject>(GetTransientPackage(), UObject::StaticClass(), TEXT("DefaultFLESHObject"));
ObjectToEdit->AddToRoot(); // Prevent garbage collection
}
}
// Create a new FLESH editor
TSharedRef<FFLESHEditor> NewFLESHEditor(new FFLESHEditor());
NewFLESHEditor->InitFLESHEditor(Mode, InitToolkitHost, ObjectToEdit);
}
catch (const std::exception& e)
{
UE_LOG(LogTemp, Error, TEXT("Failed to open FLESH Editor: %s"), UTF8_TO_TCHAR(e.what()));
}
catch (...)
{
UE_LOG(LogTemp, Error, TEXT("Failed to open FLESH Editor with unknown exception"));
}
}
#undef LOCTEXT_NAMESPACE