|
| 1 | +// Copyright Gradess Games. All Rights Reserved. |
| 2 | + |
| 3 | +#include "ActorLockerCommandManager.h" |
| 4 | +#include "ActorLockerCommands.h" |
| 5 | +#include "ActorLockerManager.h" |
| 6 | +#include "EngineUtils.h" |
| 7 | +#include "LevelEditor.h" |
| 8 | +#include "Selection.h" |
| 9 | + |
| 10 | +TSharedRef<FUICommandList> UActorLockerCommandManager::RegisterCommands() |
| 11 | +{ |
| 12 | + FActorLockerCommands::Register(); |
| 13 | + |
| 14 | + const auto& LevelEditorModule = FModuleManager::Get().LoadModuleChecked<FLevelEditorModule>("LevelEditor"); |
| 15 | + |
| 16 | + const auto& Commands = FActorLockerCommands::Get(); |
| 17 | + const auto CommandList = LevelEditorModule.GetGlobalLevelEditorActions(); |
| 18 | + |
| 19 | + CommandList->MapAction( |
| 20 | + Commands.LockObject, |
| 21 | + FExecuteAction::CreateStatic(&UActorLockerCommandManager::LockObject), |
| 22 | + FCanExecuteAction::CreateStatic(&UActorLockerCommandManager::CanLockObject) |
| 23 | + ); |
| 24 | + |
| 25 | + CommandList->MapAction( |
| 26 | + Commands.UnlockObject, |
| 27 | + FExecuteAction::CreateStatic(&UActorLockerCommandManager::UnlockObject), |
| 28 | + FCanExecuteAction::CreateStatic(&UActorLockerCommandManager::CanUnlockObject) |
| 29 | + ); |
| 30 | + |
| 31 | + CommandList->MapAction( |
| 32 | + Commands.LockAllObjects, |
| 33 | + FExecuteAction::CreateStatic(&UActorLockerCommandManager::LockAllObjects), |
| 34 | + FCanExecuteAction::CreateStatic(&UActorLockerCommandManager::CanLockAllObjects) |
| 35 | + ); |
| 36 | + |
| 37 | + CommandList->MapAction( |
| 38 | + Commands.UnlockAllObjects, |
| 39 | + FExecuteAction::CreateStatic(&UActorLockerCommandManager::UnlockAllObjects), |
| 40 | + FCanExecuteAction::CreateStatic(&UActorLockerCommandManager::CanUnlockAllObjects) |
| 41 | + ); |
| 42 | + |
| 43 | + CommandList->MapAction( |
| 44 | + Commands.ToggleLockedObjects, |
| 45 | + FExecuteAction::CreateStatic(&UActorLockerCommandManager::ToggleLockedObjects), |
| 46 | + FCanExecuteAction::CreateStatic(&UActorLockerCommandManager::CanToggleLockedObjects) |
| 47 | + ); |
| 48 | + |
| 49 | + return CommandList; |
| 50 | +} |
| 51 | + |
| 52 | +void UActorLockerCommandManager::UnregisterCommands() |
| 53 | +{ |
| 54 | + FActorLockerCommands::Unregister(); |
| 55 | +} |
| 56 | + |
| 57 | +void UActorLockerCommandManager::LockObject() |
| 58 | +{ |
| 59 | + GEditor->BeginTransaction(FText::FromString(TEXT("Lock object"))); |
| 60 | + SetLockActors(true); |
| 61 | + GEditor->EndTransaction(); |
| 62 | +} |
| 63 | + |
| 64 | +void UActorLockerCommandManager::UnlockObject() |
| 65 | +{ |
| 66 | + GEditor->BeginTransaction(FText::FromString(TEXT("Unlock object"))); |
| 67 | + SetLockActors(false); |
| 68 | + GEditor->EndTransaction(); |
| 69 | +} |
| 70 | + |
| 71 | +void UActorLockerCommandManager::LockAllObjects() |
| 72 | +{ |
| 73 | + GEditor->BeginTransaction(FText::FromString(TEXT("Lock all objects"))); |
| 74 | + SetLockAllActors(true); |
| 75 | + GEditor->EndTransaction(); |
| 76 | +} |
| 77 | + |
| 78 | +void UActorLockerCommandManager::UnlockAllObjects() |
| 79 | +{ |
| 80 | + GEditor->BeginTransaction(FText::FromString(TEXT("Unlock all objects"))); |
| 81 | + SetLockAllActors(false); |
| 82 | + GEditor->EndTransaction(); |
| 83 | +} |
| 84 | + |
| 85 | +void UActorLockerCommandManager::ToggleLockedObjects() |
| 86 | +{ |
| 87 | + GEditor->BeginTransaction(FText::FromString(TEXT("Toggle locked objects"))); |
| 88 | + |
| 89 | + const auto ActorLockerManager = UActorLockerManager::GetActorLockerManager(); |
| 90 | + |
| 91 | + SaveToTransactionBuffer(ActorLockerManager, false); |
| 92 | + ActorLockerManager->ToggleLockedActors(); |
| 93 | + |
| 94 | + GEditor->EndTransaction(); |
| 95 | +} |
| 96 | + |
| 97 | +bool UActorLockerCommandManager::CanLockObject() |
| 98 | +{ |
| 99 | + return IsValidActorLockerManager(); |
| 100 | +} |
| 101 | + |
| 102 | +bool UActorLockerCommandManager::CanUnlockObject() |
| 103 | +{ |
| 104 | + return IsValidActorLockerManager(); |
| 105 | +} |
| 106 | + |
| 107 | +bool UActorLockerCommandManager::CanLockAllObjects() |
| 108 | +{ |
| 109 | + return IsValidActorLockerManager() && IsValidEditorWorld(); |
| 110 | +} |
| 111 | + |
| 112 | +bool UActorLockerCommandManager::CanUnlockAllObjects() |
| 113 | +{ |
| 114 | + return IsValidActorLockerManager() && IsValidEditorWorld(); |
| 115 | +} |
| 116 | + |
| 117 | +bool UActorLockerCommandManager::CanToggleLockedObjects() |
| 118 | +{ |
| 119 | + return IsValidActorLockerManager(); |
| 120 | +} |
| 121 | + |
| 122 | +void UActorLockerCommandManager::SetLockActors(const bool bInLock) |
| 123 | +{ |
| 124 | + auto ActorLockerManager = UActorLockerManager::GetActorLockerManager(); |
| 125 | + SaveToTransactionBuffer(ActorLockerManager, false); |
| 126 | + |
| 127 | + if (const auto Selection = GEditor->GetSelectedActors()) |
| 128 | + { |
| 129 | + TArray<AActor*> SelectedActors; |
| 130 | + Selection->GetSelectedObjects<AActor>(SelectedActors); |
| 131 | + |
| 132 | + for (const auto SelectedActor : SelectedActors) |
| 133 | + { |
| 134 | + ActorLockerManager->SetLockActor(SelectedActor, bInLock); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +void UActorLockerCommandManager::SetLockAllActors(const bool bInLock) |
| 140 | +{ |
| 141 | + const auto ActorLockerManager = UActorLockerManager::GetActorLockerManager(); |
| 142 | + const auto World = GEditor->GetEditorWorldContext().World(); |
| 143 | + |
| 144 | + SaveToTransactionBuffer(ActorLockerManager, false); |
| 145 | + |
| 146 | + for (TActorIterator<AActor> ActorIterator(World); ActorIterator; ++ActorIterator) |
| 147 | + { |
| 148 | + ActorLockerManager->SetLockActor(*ActorIterator, bInLock); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +bool UActorLockerCommandManager::IsValidActorLockerManager() |
| 153 | +{ |
| 154 | + const auto ActorLockerManager = UActorLockerManager::GetActorLockerManager(); |
| 155 | + return IsValid(ActorLockerManager); |
| 156 | +} |
| 157 | + |
| 158 | +bool UActorLockerCommandManager::IsValidEditorWorld() |
| 159 | +{ |
| 160 | + const auto World = GEditor->GetEditorWorldContext().World(); |
| 161 | + return IsValid(World); |
| 162 | +} |
0 commit comments