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

178 lines
6.2 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_SkeletalMesh.h"
#include "AssetRegistry/AssetData.h"
#include "AssetRegistry/AssetRegistryModule.h"
#include "ContentBrowserDelegates.h"
#include "ContentBrowserModule.h"
#include "Editor.h"
#include "Engine/SkeletalMeshSocket.h"
#include "Framework/MultiBox/MultiBoxBuilder.h"
#include "Framework/MultiBox/MultiBoxExtender.h"
#include "Math/Transform.h"
#include "ReferenceSkeleton.h"
#include "SGEditor/SGPluginStyle.h"
#include "SGLog/SGLog.h"
#define LOCTEXT_NAMESPACE "SGContentBrowserExtension_SkeletalMesh"
void FSGContentBrowserExtension_SkeletalMesh::Register()
{
FContentBrowserModule& ContentBrowserModule{
FModuleManager::LoadModuleChecked<FContentBrowserModule>(TEXT("ContentBrowser"))
};
TArray<FContentBrowserMenuExtender_SelectedAssets>& ContextMenuDelegates{
ContentBrowserModule.GetAllAssetViewContextMenuExtenders()
};
ContextMenuDelegates.Add(FContentBrowserMenuExtender_SelectedAssets::CreateStatic(&OnExtendAssetSelectionMenu));
}
TSharedRef<FExtender> FSGContentBrowserExtension_SkeletalMesh::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_SkeletalMesh::AssetExtenderCallback(
FMenuBuilder& MenuBuilder, const TArray<FAssetData> SelectedAssets)
{
MenuBuilder.BeginSection(FName{TEXT("SenseGloveSection")}, LOCTEXT("SenseGloveHeading", "SenseGlove"));
{
for (const FAssetData& Asset: SelectedAssets)
{
if (!Asset.IsRedirector()
&& FName{*Asset.AssetClassPath.ToString()} != NAME_Class
&& !(Asset.PackageFlags & PKG_FilterEditorOnly))
{
if (Asset.GetClass()->IsChildOf(USkeletalMesh::StaticClass()))
{
MenuBuilder.AddMenuEntry(
LOCTEXT("AddSenseGloveSocketsLabel", "Add SenseGlove Sockets"),
LOCTEXT(
"AddSenseGloveSocketsToolTip", "Adds and sets up the SenseGlove grab and touch sockets"),
FSGPluginStyle::GetStyleSetIcon(),
FUIAction{
FExecuteAction::CreateLambda([SelectedAssets]()-> void
{
for (const FAssetData& Asset: SelectedAssets)
{
USkeletalMesh* SkeletalMesh{Cast<USkeletalMesh>(Asset.GetAsset())};
if (!IsValid(SkeletalMesh))
{
continue;
}
AddSenseGloveSockets(SkeletalMesh);
}
})
},
NAME_None,
EUserInterfaceActionType::Button,
NAME_None);
}
}
}
}
MenuBuilder.EndSection();
}
void FSGContentBrowserExtension_SkeletalMesh::AddSenseGloveSockets(USkeletalMesh* SkeletalMesh)
{
if (!ensureAlwaysMsgf(IsValid(SkeletalMesh), TEXT("Invalid SkeletalMesh!")))
{
return;
}
const USkeleton* Skeleton{SkeletalMesh->GetSkeleton()};
if (!ensureAlwaysMsgf(IsValid(Skeleton), TEXT("Invalid Skeleton!")))
{
return;
}
USkeletalMeshSocket* GrabSocket{NewObject<USkeletalMeshSocket>()};
if (!IsValid(GrabSocket))
{
// show proper messages
SGLOG("Failed to create new socket!");
}
GrabSocket->SocketName = FName(TEXT("CodeGrabSocket"));
GrabSocket->BoneName = FName(TEXT("hand_l"));
GrabSocket->RelativeLocation = FVector(0.0f, 0.0f, 0.0f);
GrabSocket->RelativeRotation = FRotator(0.0f, 0.0f, 0.0f);
GrabSocket->RelativeScale = FVector(1.0f, 1.0f, 1.0f);
SkeletalMesh->AddSocket(GrabSocket, true);
const bool bMarkedSkeletalMeshPackageDirty = SkeletalMesh->MarkPackageDirty();
const bool bMarkedSkeletonPackageDirty = Skeleton->MarkPackageDirty();
SGLOG("Socket added to hand_l?", bMarkedSkeletalMeshPackageDirty, bMarkedSkeletonPackageDirty);
if (bMarkedSkeletalMeshPackageDirty && bMarkedSkeletonPackageDirty)
{
FAssetRegistryModule::AssetCreated(GrabSocket);
FEditorDelegates::RefreshEditor.Broadcast();
// Log success
SGLOG("Create new socket!");
}
else
{
// Log errors
SGLOG("Failed to create new socket 2!");
}
// Maybe we try to add them to this?
FReferenceSkeleton ReferenceSkeleton{Skeleton->GetReferenceSkeleton()};
FReferenceSkeletonModifier SkeletonModifier{ReferenceSkeleton, Skeleton};
}
#undef LOCTEXT_NAMESPACE