UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
Commands.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
8
9//
10// COMMAND DECLARATION
11//
12
13#define LOC_DEFINE_REGION
14
17
18// This macro requires LOCTEXT_NAMESPACE to be defined. If you don't want the command to be placed under a sub namespace, provide "" as the namespace.
19#define UI_COMMAND_EXT( BindingContext, OutUICommandInfo, CommandIdName, FriendlyName, InDescription, CommandType, InDefaultChord, ... ) \
20 MakeUICommand_InternalUseOnly( BindingContext, OutUICommandInfo, TEXT(LOCTEXT_NAMESPACE), TEXT(CommandIdName), TEXT(CommandIdName) TEXT("_ToolTip"), "." CommandIdName, TEXT(FriendlyName), TEXT(InDescription), CommandType, InDefaultChord, ## __VA_ARGS__ );
21
22#define UI_COMMAND( CommandId, FriendlyName, InDescription, CommandType, InDefaultChord, ... ) \
23 MakeUICommand_InternalUseOnly( this, CommandId, TEXT(LOCTEXT_NAMESPACE), TEXT(#CommandId), TEXT(#CommandId) TEXT("_ToolTip"), "." #CommandId, TEXT(FriendlyName), TEXT(InDescription), CommandType, InDefaultChord, ## __VA_ARGS__ );
24
25#undef LOC_DEFINE_REGION
26
27#define UE_DECLARE_TCOMMANDS(Type, Api) template<> Api TWeakPtr<Type>& TCommands<Type>::GetInstance();
28#define UE_DEFINE_TCOMMANDS(Type) template<> TWeakPtr<Type>& TCommands<Type>::GetInstance() { static TWeakPtr<Type> Inst; return Inst; }
29
31template<typename CommandContextType>
33{
34public:
35
36
38 inline static void Register()
39 {
41 if ( !Instance.IsValid() )
42 {
43 // We store the singleton instances in the FInputBindingManager in order to prevent
44 // different modules from instantiating their own version of TCommands<CommandContextType>.
46
48 if (ExistingBindingContext.IsValid())
49 {
50 // Someone already made this set of commands, and registered it.
52 }
53 else
54 {
55 // Make a new set of commands and register it.
57
58 // Registering the first command will add the NewInstance into the Binding Manager, who holds on to it.
59 NewInstance->RegisterCommands();
60
61 // Notify that new commands have been registered
62 CommandsChanged.Broadcast(*NewInstance);
63 }
64 }
65 }
66
67 inline static bool IsRegistered()
68 {
69 return GetInstance().IsValid();
70 }
71
73 inline static CommandContextType& Get()
74 {
75 // If you get a crash here, then either your commands object wasn't registered, or it was not kept
76 // alive. The latter could happen if your RegisterCommands() function does not actually register
77 // any FUICommandInfo objects that use your TCommands object, so nothing keeps it alive.
78 return *( GetInstance().Pin() );
79 }
80
82 inline static void Unregister()
83 {
85 // The instance may not be valid if it was never used.
86 if( Instance.IsValid() )
87 {
88 auto InstancePtr = Instance.Pin();
89 FInputBindingManager::Get().RemoveContextByName(InstancePtr->GetContextName());
90
91 // Notify that new commands have been unregistered
92 CommandsChanged.Broadcast(*InstancePtr);
93
94 check(InstancePtr.IsUnique());
95 InstancePtr.Reset();
96 }
97 }
98
100 inline static const FBindingContext& GetContext()
101 {
103 check( Instance.IsValid() );
104 return *(Instance.Pin());
105 }
106
107protected:
108
114 virtual ~TCommands()
115 {
116 }
117
119#if PLATFORM_UNIX || PLATFORM_APPLE
121 {
123 return Inst;
124 }
125#else
131#endif
132
134 virtual void RegisterCommands() = 0;
135
136};
#define check(expr)
Definition AssertionMacros.h:314
SLATE_API void MakeUICommand_InternalUseOnly(FBindingContext *This, TSharedPtr< FUICommandInfo > &OutCommand, const TCHAR *InSubNamespace, const TCHAR *InCommandName, const TCHAR *InCommandNameUnderscoreTooltip, const ANSICHAR *DotCommandName, const TCHAR *FriendlyName, const TCHAR *InDescription, const EUserInterfaceActionType CommandType, const FInputChord &InDefaultChord, const FInputChord &InAlternateDefaultChord=FInputChord())
Definition Commands.cpp:8
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
FPlatformTypes::ANSICHAR ANSICHAR
An ANSI character. Normally a signed type.
Definition Platform.h:1131
SharedPointerInternals::TRawPtrProxy< ObjectType > MakeShareable(ObjectType *InObject)
Definition SharedPointer.h:1947
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
EUserInterfaceActionType
Definition UICommandInfo.h:20
Definition UICommandInfo.h:85
static SLATE_API FOnBindingContextChanged CommandsChanged
Definition UICommandInfo.h:162
SLATE_API void RemoveContextByName(const FName &InContextName)
Definition InputBindingManager.cpp:771
static SLATE_API FInputBindingManager & Get()
Definition InputBindingManager.cpp:307
SLATE_API TSharedPtr< FBindingContext > GetContextByName(const FName &InContextName)
Definition InputBindingManager.cpp:758
Definition NameTypes.h:617
Definition Text.h:385
Definition Commands.h:33
static const FBindingContext & GetContext()
Definition Commands.h:100
static CommandContextType & Get()
Definition Commands.h:73
static bool IsRegistered()
Definition Commands.h:67
static TWeakPtr< CommandContextType > & GetInstance()
Definition Commands.h:126
static void Register()
Definition Commands.h:38
virtual void RegisterCommands()=0
static void Unregister()
Definition Commands.h:82
virtual ~TCommands()
Definition Commands.h:114
TCommands(const FName InContextName, const FText &InContextDesc, const FName InContextParent, const FName InStyleSetName)
Definition Commands.h:110
Definition SharedPointer.h:153
Definition SharedPointer.h:1295
Definition InputChord.h:24