更新 Source/FLESHEditor/Private/FLESHEditorModule.cpp
This commit is contained in:
@@ -10,63 +10,126 @@
|
|||||||
|
|
||||||
void FFLESHEditorModule::StartupModule()
|
void FFLESHEditorModule::StartupModule()
|
||||||
{
|
{
|
||||||
// This code will execute after your module is loaded into memory
|
// This code will execute after your module is loaded into memory
|
||||||
// The exact timing is specified in the .uplugin file per-module
|
// The exact timing is specified in the .uplugin file per-module
|
||||||
|
|
||||||
// Initialize style
|
// Add try-catch block to prevent crashes during startup
|
||||||
FFLESHEditorStyle::Initialize();
|
try
|
||||||
FFLESHEditorStyle::ReloadTextures();
|
{
|
||||||
|
// Initialize style
|
||||||
|
FFLESHEditorStyle::Initialize();
|
||||||
|
FFLESHEditorStyle::ReloadTextures();
|
||||||
|
|
||||||
// Register commands
|
// Register commands
|
||||||
FFLESHEditorCommands::Register();
|
FFLESHEditorCommands::Register();
|
||||||
|
|
||||||
// Map commands
|
// Map commands
|
||||||
PluginCommands = MakeShareable(new FUICommandList);
|
PluginCommands = MakeShareable(new FUICommandList);
|
||||||
PluginCommands->MapAction(
|
PluginCommands->MapAction(
|
||||||
FFLESHEditorCommands::Get().OpenFLESHEditor,
|
FFLESHEditorCommands::Get().OpenFLESHEditor,
|
||||||
FExecuteAction::CreateRaw(this, &FFLESHEditorModule::OpenFLESHEditor),
|
FExecuteAction::CreateRaw(this, &FFLESHEditorModule::OpenFLESHEditorCommand),
|
||||||
FCanExecuteAction());
|
FCanExecuteAction());
|
||||||
|
|
||||||
// Register editor menus
|
// Register editor menus
|
||||||
UToolMenus::RegisterStartupCallback(FSimpleMulticastDelegate::FDelegate::CreateLambda([this]()
|
UToolMenus::RegisterStartupCallback(FSimpleMulticastDelegate::FDelegate::CreateLambda([this]()
|
||||||
{
|
{
|
||||||
// Add menu items
|
// Add menu items
|
||||||
FToolMenuOwnerScoped OwnerScoped(this);
|
FToolMenuOwnerScoped OwnerScoped(this);
|
||||||
|
|
||||||
// Add to main menu
|
// Add to main menu
|
||||||
{
|
{
|
||||||
UToolMenu* Menu = UToolMenus::Get()->ExtendMenu("LevelEditor.MainMenu.Tools");
|
UToolMenu* Menu = UToolMenus::Get()->ExtendMenu("LevelEditor.MainMenu.Tools");
|
||||||
FToolMenuSection& Section = Menu->FindOrAddSection("Tools");
|
FToolMenuSection& Section = Menu->FindOrAddSection("Tools");
|
||||||
Section.AddMenuEntryWithCommandList(FFLESHEditorCommands::Get().OpenFLESHEditor, PluginCommands);
|
Section.AddMenuEntryWithCommandList(FFLESHEditorCommands::Get().OpenFLESHEditor, PluginCommands);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add to toolbar
|
// Add to toolbar
|
||||||
{
|
{
|
||||||
UToolMenu* ToolbarMenu = UToolMenus::Get()->ExtendMenu("LevelEditor.LevelEditorToolBar");
|
UToolMenu* ToolbarMenu = UToolMenus::Get()->ExtendMenu("LevelEditor.LevelEditorToolBar");
|
||||||
FToolMenuSection& Section = ToolbarMenu->FindOrAddSection("Settings");
|
FToolMenuSection& Section = ToolbarMenu->FindOrAddSection("Settings");
|
||||||
FToolMenuEntry& Entry = Section.AddEntry(FToolMenuEntry::InitToolBarButton(FFLESHEditorCommands::Get().OpenFLESHEditor));
|
FToolMenuEntry& Entry = Section.AddEntry(FToolMenuEntry::InitToolBarButton(FFLESHEditorCommands::Get().OpenFLESHEditor));
|
||||||
Entry.SetCommandList(PluginCommands);
|
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()
|
void FFLESHEditorModule::ShutdownModule()
|
||||||
{
|
{
|
||||||
// This function may be called during shutdown to clean up your module
|
// 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
|
// For modules that support dynamic reloading, we call this function before unloading the module
|
||||||
|
|
||||||
// Unregister style
|
// Unregister style
|
||||||
FFLESHEditorStyle::Shutdown();
|
FFLESHEditorStyle::Shutdown();
|
||||||
|
|
||||||
// Unregister editor menus
|
// Unregister editor menus
|
||||||
UToolMenus::UnRegisterStartupCallback(this);
|
UToolMenus::UnRegisterStartupCallback(this);
|
||||||
UToolMenus::UnregisterOwner(this);
|
UToolMenus::UnregisterOwner(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void FFLESHEditorModule::OpenFLESHEditor()
|
void FFLESHEditorModule::OpenFLESHEditorCommand()
|
||||||
{
|
{
|
||||||
// Open FLESH editor
|
// Open the editor with default parameters
|
||||||
FFLESHEditor::OpenEditor();
|
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
|
#undef LOCTEXT_NAMESPACE
|
||||||
|
Reference in New Issue
Block a user