129 lines
4.7 KiB
C++
129 lines
4.7 KiB
C++
#include "FLESHEditorModule.h"
|
|
#include "Modules/ModuleManager.h"
|
|
#include "ToolMenus.h"
|
|
#include "FLESHEditorStyle.h"
|
|
#include "FLESHEditorCommands.h"
|
|
#include "FLESHEditor.h"
|
|
#include "LevelEditor.h"
|
|
|
|
#define LOCTEXT_NAMESPACE "FFLESHEditorModule"
|
|
|
|
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
|
|
|
|
// 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);
|
|
}
|
|
|
|
void FFLESHEditorModule::OpenFLESHEditorCommand()
|
|
{
|
|
// Open the editor with default parameters
|
|
OpenFLESHEditor(EToolkitMode::Standalone, nullptr, nullptr);
|
|
}
|
|
|
|
void FFLESHEditorModule::OpenFLESHEditor(const EToolkitMode::Type Mode, const TSharedPtr<IToolkitHost>& InitToolkitHost, TObjectPtr<UObject> ObjectToEdit)
|
|
{
|
|
// Add try-catch block to prevent crashes when opening the editor
|
|
try
|
|
{
|
|
// Check if FLESH module is loaded
|
|
if (!FModuleManager::Get().IsModuleLoaded("FLESH"))
|
|
{
|
|
UE_LOG(LogTemp, Error, TEXT("Failed to open FLESH Editor: FLESH module not loaded"));
|
|
return;
|
|
}
|
|
|
|
// If no object is provided, create a default object
|
|
if (ObjectToEdit == nullptr)
|
|
{
|
|
// Create a basic UObject
|
|
ObjectToEdit = NewObject<UObject>(GetTransientPackage(), UObject::StaticClass(), TEXT("DefaultFLESHObject"));
|
|
if (ObjectToEdit == nullptr)
|
|
{
|
|
UE_LOG(LogTemp, Error, TEXT("Failed to create default object, cannot open FLESH Editor"));
|
|
return;
|
|
}
|
|
}
|
|
|
|
// Create a new FLESH editor
|
|
TSharedRef<FFLESHEditor> NewFLESHEditor(new FFLESHEditor());
|
|
NewFLESHEditor->InitFLESHEditor(Mode, InitToolkitHost, ObjectToEdit);
|
|
|
|
UE_LOG(LogTemp, Log, TEXT("FLESH Editor successfully opened"));
|
|
}
|
|
catch (const std::exception& e)
|
|
{
|
|
UE_LOG(LogTemp, Error, TEXT("Failed to open FLESH Editor: %s"), UTF8_TO_TCHAR(e.what()));
|
|
FMessageDialog::Open(EAppMsgType::Ok, FText::Format(LOCTEXT("OpenEditorError", "Failed to open FLESH Editor: {0}"), FText::FromString(UTF8_TO_TCHAR(e.what()))));
|
|
}
|
|
catch (...)
|
|
{
|
|
UE_LOG(LogTemp, Error, TEXT("Unknown exception occurred when opening FLESH Editor"));
|
|
FMessageDialog::Open(EAppMsgType::Ok, LOCTEXT("OpenEditorUnknownError", "Unknown exception occurred when opening FLESH Editor"));
|
|
}
|
|
}
|
|
|
|
#undef LOCTEXT_NAMESPACE
|
|
|
|
IMPLEMENT_MODULE(FFLESHEditorModule, FLESHEditor)
|