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

224 lines
7.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/SGSocketsEditorCommands.h"
#include "Animation/Skeleton.h"
#include "Containers/UnrealString.h"
#include "Engine/SkeletalMesh.h"
#include "Framework/Commands/UIAction.h"
#include "GenericPlatform/GenericPlatformMisc.h"
#include "Internationalization/Text.h"
#include "Misc/MessageDialog.h"
#include "UObject/Object.h"
#include "SGEditor/SGAssetUtils.h"
#include "SGEditor/SGPluginStyle.h"
#define LOCTEXT_NAMESPACE "SocketsEditorCommands"
struct FSGSocketsEditorCommands::FImpl
{
/************************
* Static methods
************************/
static void TryAddSenseGloveSockets();
static void TryClearExistingSockets();
};
void FSGSocketsEditorCommands::FImpl::TryAddSenseGloveSockets()
{
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;
}
UObject* EditedAsset{FSGAssetUtils::GetEditedAsset()};
USkeletalMesh* EditedSkeletalMesh{Cast<USkeletalMesh>(EditedAsset)};
USkeleton* EditedSkeleton{Cast<USkeleton>(EditedAsset)};
bool bAddedSenseGloveSockets = false;
if (IsValid(EditedSkeletalMesh))
{
bAddedSenseGloveSockets = FSGAssetUtils::AddSenseGloveSockets(EditedSkeletalMesh);
}
else if (IsValid(EditedSkeleton))
{
bAddedSenseGloveSockets = FSGAssetUtils::AddSenseGloveSockets(EditedSkeleton);
}
if (bAddedSenseGloveSockets)
{
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);
}
else
{
const FText ResultDialogText{
LOCTEXT("SenseGloveAddSocketsResultSuccessFailed",
"Failed to add the SenseGlove sockets!")
};
FMessageDialog::Open(EAppMsgType::Ok, ResultDialogText);
}
}
void FSGSocketsEditorCommands::FImpl::TryClearExistingSockets()
{
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;
}
UObject* EditedAsset{FSGAssetUtils::GetEditedAsset()};
USkeletalMesh* EditedSkeletalMesh{Cast<USkeletalMesh>(EditedAsset)};
USkeleton* EditedSkeleton{Cast<USkeleton>(EditedAsset)};
bool bClearedExistingSockets = false;
if (IsValid(EditedSkeletalMesh))
{
bClearedExistingSockets = FSGAssetUtils::ClearExistingSockets(EditedSkeletalMesh);
}
else if (IsValid(EditedSkeleton))
{
bClearedExistingSockets = FSGAssetUtils::ClearExistingSockets(EditedSkeleton);
}
if (bClearedExistingSockets)
{
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);
}
else
{
const FText ResultDialogText{
LOCTEXT("SenseGloveClearExistingSocketsResultSuccessFailed",
"Failed to clear all the existing sockets!")
};
FMessageDialog::Open(EAppMsgType::Ok, ResultDialogText);
}
}
FSGSocketsEditorCommands::FSGSocketsEditorCommands()
: TCommands<FSGSocketsEditorCommands>{
TEXT("SGSocketsEditor"),
NSLOCTEXT("Contexts", "SGSocketsEditor", "SenseGlove Sockets Editor"),
NAME_None,
FSGPluginStyle::GetInstance()->GetStyleSetName()
}
{
}
FSGSocketsEditorCommands::~FSGSocketsEditorCommands() = default;
void FSGSocketsEditorCommands::RegisterCommands()
{
if (CommandList.IsValid())
{
CommandList.Reset();
}
CommandList = MakeShared<FUICommandList>();
if (SenseGloveAddSockets.IsValid())
{
SenseGloveAddSockets.Reset();
}
if (SenseGloveClearExistingSockets.IsValid())
{
SenseGloveClearExistingSockets.Reset();
}
UI_COMMAND(SenseGloveAddSockets,
"Add SenseGlove Sockets",
"Adds and sets up the SenseGlove grab and touch sockets",
EUserInterfaceActionType::Button,
FInputChord{});
UI_COMMAND(SenseGloveClearExistingSockets,
"Clear Existing Sockets",
"Clears all the existing sockets; SenseGlove or otherwise",
EUserInterfaceActionType::Button,
FInputChord{});
CommandList->MapAction(SenseGloveAddSockets,
FExecuteAction::CreateLambda([&]()-> void
{
FImpl::TryAddSenseGloveSockets();
}),
FCanExecuteAction::CreateLambda([&]()-> bool
{
return true;
}),
EUIActionRepeatMode::RepeatDisabled);
CommandList->MapAction(SenseGloveClearExistingSockets,
FExecuteAction::CreateLambda([&]()-> void
{
FImpl::TryClearExistingSockets();
}),
FCanExecuteAction::CreateLambda([&]()-> bool
{
return true;
}),
EUIActionRepeatMode::RepeatDisabled);
}
#undef LOCTEXT_NAMESPACE