|
| 1 | +// Copyright (c) Yevhenii Selivanov |
| 2 | + |
| 3 | +#include "CustomShapeButtonManager.h" |
| 4 | +//--- |
| 5 | +#include "CustomShapeButton.h" |
| 6 | +#include "SCustomShapeButton.h" |
| 7 | +//--- |
| 8 | +#include "Engine/Engine.h" |
| 9 | +#include "Input/Reply.h" |
| 10 | +//--- |
| 11 | +#include UE_INLINE_GENERATED_CPP_BY_NAME(CustomShapeButtonManager) |
| 12 | + |
| 13 | +// Returns the manager instance, or crash if can't be obtained |
| 14 | +UCustomShapeButtonManager& UCustomShapeButtonManager::Get() |
| 15 | +{ |
| 16 | + UCustomShapeButtonManager* CustomShapeButtonManager = GetCustomShapeButtonManager(); |
| 17 | + checkf(CustomShapeButtonManager, TEXT("ERROR: [%i] %hs:\n'CustomShapeButtonManager' is null!"), __LINE__, __FUNCTION__); |
| 18 | + return *CustomShapeButtonManager; |
| 19 | +} |
| 20 | + |
| 21 | +// Returns the manager instance, or nullptr if can't be obtained |
| 22 | +UCustomShapeButtonManager* UCustomShapeButtonManager::GetCustomShapeButtonManager() |
| 23 | +{ |
| 24 | + return GEngine ? GEngine->GetEngineSubsystem<UCustomShapeButtonManager>() : nullptr; |
| 25 | +} |
| 26 | + |
| 27 | +// Registers a button for event redirection |
| 28 | +void UCustomShapeButtonManager::RegisterButton(UCustomShapeButton* Button) |
| 29 | +{ |
| 30 | + const TSharedPtr<SCustomShapeButton> SButton = Button ? Button->GetSlateCustomShapeButton() : nullptr; |
| 31 | + if (!ensureMsgf(SButton, TEXT("ASSERT: [%i] %hs:\n'SButton' is not valid!"), __LINE__, __FUNCTION__) |
| 32 | + || !CanRegisterButton(Button)) |
| 33 | + { |
| 34 | + // Button can't handle events if the world is not playing or button itself if not valid |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + // Find insertion point to maintain descending order of OverlapOrder |
| 39 | + const int32 InsertIndex = RegisteredButtons.IndexOfByPredicate( |
| 40 | + [ZOrder = Button->OverlapOrder](const TSoftObjectPtr<UCustomShapeButton>& It) |
| 41 | + { |
| 42 | + const UCustomShapeButton* OtherButton = It.Get(); |
| 43 | + return OtherButton && ZOrder > OtherButton->OverlapOrder; |
| 44 | + }); |
| 45 | + |
| 46 | + if (InsertIndex != INDEX_NONE) |
| 47 | + { |
| 48 | + RegisteredButtons.Insert(Button, InsertIndex); |
| 49 | + } |
| 50 | + else |
| 51 | + { |
| 52 | + RegisteredButtons.Add(Button); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// Unregisters a button when it is destroyed |
| 57 | +void UCustomShapeButtonManager::UnregisterButton(UCustomShapeButton* Button) |
| 58 | +{ |
| 59 | + const int32 Index = Button ? RegisteredButtons.IndexOfByKey(Button) : INDEX_NONE; |
| 60 | + if (Index == INDEX_NONE) |
| 61 | + { |
| 62 | + // Is already removed or was not even registered |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + RegisteredButtons.RemoveAtSwap(Index); |
| 67 | +} |
| 68 | + |
| 69 | +// Returns true is button is initialized and ready to handle events |
| 70 | +bool UCustomShapeButtonManager::CanRegisterButton(const UCustomShapeButton* Button) |
| 71 | +{ |
| 72 | +#if !WITH_EDITOR |
| 73 | + // Manager is always ready to handle events in builds |
| 74 | + return true; |
| 75 | +#else |
| 76 | + // Don't register preview buttons in editor (not -game) |
| 77 | + const bool bIsMinusGame = !GIsEditor; |
| 78 | + const UWorld* World = Button ? Button->GetWorld() : nullptr; |
| 79 | + const bool bIsGameWorld = World && World->IsGameWorld(); |
| 80 | + return bIsMinusGame || bIsGameWorld; |
| 81 | +#endif // WITH_EDITOR |
| 82 | +} |
| 83 | + |
| 84 | +// Handles any mouse event using a delegate |
| 85 | +FReply UCustomShapeButtonManager::HandleEvent(const FPointerEvent& Event, const TFunctionRef<FReply(const TSharedRef<SCustomShapeButton>&)>& Callback) |
| 86 | +{ |
| 87 | + FReply FinalReply = FReply::Unhandled(); |
| 88 | + |
| 89 | + for (const TSoftObjectPtr<UCustomShapeButton>& It : RegisteredButtons) |
| 90 | + { |
| 91 | + const UCustomShapeButton* Button = It.Get(); |
| 92 | + SCustomShapeButton* SButton = Button ? Button->GetSlateCustomShapeButton().Get() : nullptr; |
| 93 | + if (SButton) |
| 94 | + { |
| 95 | + SButton->HandleEvent(/*out*/FinalReply, Event, Callback); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return FinalReply; |
| 100 | +} |
| 101 | + |
| 102 | +/********************************************************************************************* |
| 103 | + * Overrides |
| 104 | + ********************************************************************************************* */ |
| 105 | + |
| 106 | +// Called when this subsystem is initialized to perform initial setup |
| 107 | +void UCustomShapeButtonManager::Initialize(FSubsystemCollectionBase& Collection) |
| 108 | +{ |
| 109 | + Super::Initialize(Collection); |
| 110 | + |
| 111 | + FWorldDelegates::OnWorldCleanup.AddUObject(this, &ThisClass::OnEndPlay); |
| 112 | +} |
| 113 | + |
| 114 | +// Is called on the game world end play to cleanup data |
| 115 | +void UCustomShapeButtonManager::OnEndPlay(UWorld* World, bool bArg, bool bCond) |
| 116 | +{ |
| 117 | + RegisteredButtons.Empty(); |
| 118 | +} |
0 commit comments