Files
Plugin/Source/SenseGloveEditor/Private/SGEditor/SGContentBrowserExtension.cpp
T

314 lines
9.8 KiB
C++

/**
* @file
*
* @author Mamadou Babaei <mamadou@senseglove.com>
*
* @section LICENSE
*
* (The MIT License)
*
* Copyright (c) 2020 - 2024 SenseGlove
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @section DESCRIPTION
*
*
*/
#include "SGEditor/SGContentBrowserExtension.h"
#include "Animation/Skeleton.h"
#include "AssetRegistry/AssetData.h"
#include "Containers/UnrealString.h"
#include "ContentBrowserDelegates.h"
#include "ContentBrowserModule.h"
#include "Editor.h"
#include "Engine/SkeletalMesh.h"
#include "Framework/Commands/UIAction.h"
#include "Framework/MultiBox/MultiBoxBuilder.h"
#include "Framework/MultiBox/MultiBoxExtender.h"
#include "GenericPlatform/GenericPlatformMisc.h"
#include "Internationalization/Text.h"
#include "Math/Transform.h"
#include "Misc/MessageDialog.h"
#include "ReferenceSkeleton.h"
#include "UObject/Object.h"
#include "SGEditor/SGAssetUtils.h"
#include "SGEditor/SGPluginStyle.h"
#define LOCTEXT_NAMESPACE "SGContentBrowserExtension"
struct FSGContentBrowserExtension::FImpl
{
/************************
* Static methods
************************/
static void TryAddSenseGloveSockets(const TArray<FAssetData>& SelectedAssets);
static void TryClearExistingSockets(const TArray<FAssetData>& SelectedAssets);
};
void FSGContentBrowserExtension::FImpl::TryAddSenseGloveSockets(const TArray<FAssetData>& SelectedAssets)
{
const FText ConfirmationDialogText{
LOCTEXT("SenseGloveAddSocketsConfirmation",
"Do you want to add the SenseGlove sockets?")
};
const EAppReturnType::Type DialogResult{
FMessageDialog::Open(EAppMsgType::YesNo, ConfirmationDialogText)
};
if (DialogResult != EAppReturnType::Yes)
{
return;
}
bool bAllAssetsSucceeded = true;
for (const FAssetData& Asset: SelectedAssets)
{
USkeletalMesh* SkeletalMesh{Cast<USkeletalMesh>(Asset.GetAsset())};
USkeleton* Skeleton{Cast<USkeleton>(Asset.GetAsset())};
if (!IsValid(SkeletalMesh) && !IsValid(Skeleton))
{
continue;
}
bool bAddedSenseGloveSockets = false;
if (IsValid(SkeletalMesh))
{
bAddedSenseGloveSockets = FSGAssetUtils::AddSenseGloveSockets(SkeletalMesh);
}
else if (IsValid(Skeleton))
{
bAddedSenseGloveSockets = FSGAssetUtils::AddSenseGloveSockets(Skeleton);
}
if (!bAddedSenseGloveSockets)
{
bAllAssetsSucceeded = false;
const FText ResultDialogText{
FText::Format(
LOCTEXT("SenseGloveAddSocketsResultFailedOn",
"Failed to add the SenseGlove sockets to '{0}'!"),
FText::FromString(Asset.GetSoftObjectPath().ToString()))
};
FMessageDialog::Open(EAppMsgType::Ok, ResultDialogText);
}
if (!bAddedSenseGloveSockets)
{
break;
}
}
if (bAllAssetsSucceeded)
{
const FText ResultDialogText{
LOCTEXT("SenseGloveAddSocketsResultSuccess",
"Successfully added the SenseGlove grab and touch sockets. If you wish the changes to persist, save"
" the modified asset(s) now!")
};
FMessageDialog::Open(EAppMsgType::Ok, ResultDialogText);
}
}
void FSGContentBrowserExtension::FImpl::TryClearExistingSockets(const TArray<FAssetData>& SelectedAssets)
{
const FText ConfirmationDialogText{
LOCTEXT("SenseGloveClearExistingSocketsConfirmation",
"Are you sure you want to clear all the existing sockets? This cannot be undone!")
};
const EAppReturnType::Type DialogResult{
FMessageDialog::Open(EAppMsgType::YesNo, ConfirmationDialogText)
};
if (DialogResult != EAppReturnType::Yes)
{
return;
}
bool bAllAssetsSucceeded = true;
for (const FAssetData& Asset: SelectedAssets)
{
USkeletalMesh* SkeletalMesh{Cast<USkeletalMesh>(Asset.GetAsset())};
USkeleton* Skeleton{Cast<USkeleton>(Asset.GetAsset())};
if (!IsValid(SkeletalMesh) && !IsValid(Skeleton))
{
continue;
}
bool bClearedExistingSockets = false;
if (IsValid(SkeletalMesh))
{
bClearedExistingSockets = FSGAssetUtils::ClearExistingSockets(SkeletalMesh);
}
else if (IsValid(Skeleton))
{
bClearedExistingSockets = FSGAssetUtils::ClearExistingSockets(Skeleton);
}
if (!bClearedExistingSockets)
{
bAllAssetsSucceeded = false;
const FText ResultDialogText{
FText::Format(
LOCTEXT("SenseGloveClearExistingSocketsResultFailedOn",
"Failed to clear all the existing sockets on '{0}'!"),
FText::FromString(Asset.GetSoftObjectPath().ToString()))
};
FMessageDialog::Open(EAppMsgType::Ok, ResultDialogText);
}
if (!bClearedExistingSockets)
{
break;
}
}
if (bAllAssetsSucceeded)
{
const FText ResultDialogText{
LOCTEXT("SenseGloveClearExistingSocketsResultSuccess",
"Successfully cleared all the existing sockets! If you wish the changes to persist, save the"
" modified asset(s) now!")
};
FMessageDialog::Open(EAppMsgType::Ok, ResultDialogText);
}
}
void FSGContentBrowserExtension::Register()
{
FContentBrowserModule& ContentBrowserModule{
FModuleManager::LoadModuleChecked<FContentBrowserModule>(TEXT("ContentBrowser"))
};
TArray<FContentBrowserMenuExtender_SelectedAssets>& ContextMenuDelegates{
ContentBrowserModule.GetAllAssetViewContextMenuExtenders()
};
ContextMenuDelegates.Add(FContentBrowserMenuExtender_SelectedAssets::CreateStatic(&OnExtendAssetSelectionMenu));
}
TSharedRef<FExtender> FSGContentBrowserExtension::OnExtendAssetSelectionMenu(
const TArray<FAssetData>& SelectedAssets)
{
TSharedRef<FExtender> Extender{MakeShared<FExtender>()};
Extender->AddMenuExtension(
"CommonAssetActions",
EExtensionHook::After,
nullptr,
FMenuExtensionDelegate::CreateStatic(&AssetExtenderCallback, SelectedAssets)
);
return MoveTemp(Extender);
}
void FSGContentBrowserExtension::AssetExtenderCallback(
FMenuBuilder& MenuBuilder, const TArray<FAssetData> SelectedAssets)
{
bool bShouldExtendTheMenu = true;
bool bAnySkeletalMeshSelected = false;
bool bAnySkeletonSelected = false;
for (const FAssetData& Asset: SelectedAssets)
{
if (Asset.IsRedirector()
|| FName{*Asset.AssetClassPath.ToString()} == NAME_Class
|| (Asset.PackageFlags & PKG_FilterEditorOnly))
{
bShouldExtendTheMenu = false;
break;
}
const bool bIsChildOfUSkeletalMesh = Asset.GetClass()->IsChildOf(USkeletalMesh::StaticClass());
const bool bIsChildOfUSkeleton = Asset.GetClass()->IsChildOf(USkeleton::StaticClass());
if (!bIsChildOfUSkeletalMesh && !bIsChildOfUSkeleton)
{
bShouldExtendTheMenu = false;
break;
}
if (bIsChildOfUSkeletalMesh)
{
bAnySkeletalMeshSelected = true;
}
if (bIsChildOfUSkeleton)
{
bAnySkeletonSelected = true;
}
if (bAnySkeletalMeshSelected && bAnySkeletonSelected)
{
bShouldExtendTheMenu = false;
break;
}
}
if (!bShouldExtendTheMenu)
{
return;
}
MenuBuilder.BeginSection(FName{TEXT("SenseGloveSection")}, LOCTEXT("SenseGloveHeading", "SenseGlove"));
MenuBuilder.AddMenuEntry(
LOCTEXT("SenseGloveAddSocketsLabel", "Add SenseGlove Sockets"),
LOCTEXT("SenseGloveAddSocketsToolTip", "Adds and sets up the SenseGlove grab and touch sockets"),
FSGPluginStyle::GetStyleSetIcon(),
FUIAction{
FExecuteAction::CreateLambda([SelectedAssets]()-> void
{
FImpl::TryAddSenseGloveSockets(SelectedAssets);
})
},
NAME_None,
EUserInterfaceActionType::Button,
NAME_None);
MenuBuilder.AddMenuEntry(
LOCTEXT("SenseGloveClearExistingSocketsLabel", "Clear Existing Sockets"),
LOCTEXT("SenseGloveClearExistingSocketsToolTip",
"Clears all the existing sockets; SenseGlove or otherwise"),
FSGPluginStyle::GetStyleSetIcon(),
FUIAction{
FExecuteAction::CreateLambda([SelectedAssets]()-> void
{
FImpl::TryClearExistingSockets(SelectedAssets);
})
},
NAME_None,
EUserInterfaceActionType::Button,
NAME_None);
MenuBuilder.EndSection();
}
#undef LOCTEXT_NAMESPACE