New TRS Gizmo: change the default editor mode for the asset editors

The new TRS gizmo system uses FEditorModeTools::GetShowWidget() to update the visible state of the gizmo.
    However, for asset editors that don't override their default mode (as is the case with BP), the result is obtained from the FLevelEditorSelectModeWidgetHelper::ShouldDrawWidget function, which gets its data from the GCurrentLevelEditingViewportClient...
    The FWidget rendering works mainly because it bypasses the result of FEditorModeTools::GetShowWidget() and relies on the value of FEditorViewportClient::GetWidgetMode() instead. VPCs like FSCSEditorViewportClient, for example, will change the widget mode dynamically according to the selection.
    To change this behavior for new TRS gizmo without changing the current behavior, we swap the default mode with AssetEdModes::EM_AssetDefault if (and only if) FBuiltinEditorModes::EM_Default is one of the default modes and we're not dealing with the level editor's mode manager.

+ remove the direct uses of "Gizmos.UseLegacyWidget" in several places to ease the migration to user settings

#jira UE-200162
#rb brooke.hubert

[CL 30409058 by benoit gadreau in ue5-main branch]
This commit is contained in:
benoit gadreau
2023-12-20 04:08:20 -05:00
parent d23e4bb6bd
commit 28625b2fc2
13 changed files with 138 additions and 29 deletions
@@ -3,6 +3,7 @@
#include "EditorGizmos/EditorTransformGizmoSource.h"
#include "Editor.h"
#include "EditorInteractiveGizmoManager.h"
#include "EditorModeManager.h"
#include "EditorViewportClient.h"
#include "EditorGizmos/EditorTransformGizmoUtil.h"
@@ -88,11 +89,7 @@ bool UEditorTransformGizmoSource::CanInteract() const
bool bUseLegacyWidget = (WidgetMode == UE::Widget::WM_TranslateRotateZ || WidgetMode == UE::Widget::WM_2D);
if (!bUseLegacyWidget)
{
static IConsoleVariable* const UseLegacyWidgetCVar = IConsoleManager::Get().FindConsoleVariable(TEXT("Gizmos.UseLegacyWidget"));
if (ensure(UseLegacyWidgetCVar))
{
bUseLegacyWidget = UseLegacyWidgetCVar->GetInt() > 0;
}
bUseLegacyWidget = !UEditorInteractiveGizmoManager::UsesNewTRSGizmos();
}
return !bUseLegacyWidget;
}
@@ -12,6 +12,8 @@
#include "EditorGizmos/EditorTransformGizmoBuilder.h"
#include "EditorGizmos/EditorTransformGizmoDataBinder.h"
#include "EditorViewportClient.h"
#include "EditorModes.h"
#include "Tools/DefaultEdMode.h"
#include UE_INLINE_GENERATED_CPP_BY_NAME(EditorTransformGizmoUtil)
@@ -253,6 +255,9 @@ void UEditorTransformGizmoContextObject::UpdateGizmo(const TArray<FEditorViewpor
const UModeManagerInteractiveToolsContext* ToolsContext = ModeTools->GetInteractiveToolsContext();
const TObjectPtr<UInteractiveToolManager> ToolManager = ToolsContext->ToolManager;
// swap default mode
const bool Swapped = SwapDefaultMode(FBuiltinEditorModes::EM_Default, FAssetEdModes::EM_AssetDefault);
if (UE::EditorTransformGizmoUtil::FindDefaultTransformGizmo(ToolManager))
{
@@ -262,7 +267,7 @@ void UEditorTransformGizmoContextObject::UpdateGizmo(const TArray<FEditorViewpor
// create a new one
if (UTransformGizmo* TransformGizmo = UE::EditorTransformGizmoUtil::GetDefaultTransformGizmo(ToolManager))
{
TransformGizmo->SetVisibility(false);
TransformGizmo->SetVisibility(Swapped ? UEditorInteractiveGizmoManager::UsesNewTRSGizmos() : false);
}
}
@@ -274,11 +279,12 @@ void UEditorTransformGizmoContextObject::InitializeCVarBinding()
auto OnGizmoVariableChanged = [this](IConsoleVariable* InLegacyWidgetCVar)
{
const bool bUseLegacyGizmo = InLegacyWidgetCVar ? UseLegacyWidgetCVar->GetInt() > 0 : false;
if (bUseLegacyGizmo)
{
// remove viewports' binding + gizmos as they are useless
// swap back default mode
(void)SwapDefaultMode(FAssetEdModes::EM_AssetDefault, FBuiltinEditorModes::EM_Default);
// remove viewports' binding + gizmos as they are useless
const UModeManagerInteractiveToolsContext* ToolsContext = ModeTools->GetInteractiveToolsContext();
const TObjectPtr<UInteractiveToolManager> ToolManager = ToolsContext->ToolManager;
UE::EditorTransformGizmoUtil::RemoveDefaultTransformGizmo(ToolManager);
@@ -353,3 +359,31 @@ void UEditorTransformGizmoContextObject::RemoveViewportsBinding()
ViewportClientsChangedHandle.Reset();
}
}
bool UEditorTransformGizmoContextObject::SwapDefaultMode(const FEditorModeID InCurrentDefaultMode, const FEditorModeID InNewDefaultMode) const
{
// swap only if we're not dealing with the level editor + InCurrentDefaultMode is one of the default modes
if (!ModeTools->IsDefaultMode(InCurrentDefaultMode))
{
return false;
}
const bool bIsLevelMode = ModeTools == &GLevelEditorModeTools();
if (bIsLevelMode)
{
return false;
}
// replace default mode
ModeTools->RemoveDefaultMode(InCurrentDefaultMode);
ModeTools->AddDefaultMode(InNewDefaultMode);
if (ModeTools->IsModeActive(InCurrentDefaultMode))
{
// activate it if the previous one was
ModeTools->DeactivateMode(InCurrentDefaultMode);
ModeTools->ActivateMode(InNewDefaultMode);
}
return true;
}
@@ -27,20 +27,33 @@ class FCanvas;
#define LOCTEXT_NAMESPACE "UEditorInteractiveGizmoManager"
static TAutoConsoleVariable<int32> CVarUseLegacyWidget(
namespace GizmoManagerLocals
{
static int32 UseLegacyWidget = 1;
}
static FAutoConsoleVariableRef CVarUseLegacyWidget(
TEXT("Gizmos.UseLegacyWidget"),
1,
GizmoManagerLocals::UseLegacyWidget,
TEXT("Specify whether to use selection-based gizmos or legacy widget\n")
TEXT("0 = enable UE5 transform and other selection-based gizmos.\n")
TEXT("1 = enable legacy UE4 transform widget."),
FConsoleVariableDelegate::CreateLambda([](IConsoleVariable*)
{
GizmoManagerLocals::UseLegacyWidget = FMath::Clamp(GizmoManagerLocals::UseLegacyWidget, 0, 1);
}),
ECVF_RenderThreadSafe);
bool UEditorInteractiveGizmoManager::UsesNewTRSGizmos()
{
return (GizmoManagerLocals::UseLegacyWidget == 0);
}
UEditorInteractiveGizmoManager::UEditorInteractiveGizmoManager() :
UInteractiveGizmoManager()
{
Registry = NewObject<UEditorInteractiveGizmoRegistry>();
bShowEditorGizmos = (CVarUseLegacyWidget.GetValueOnGameThread() == 0);
bShowEditorGizmos = UsesNewTRSGizmos();
}
@@ -175,8 +188,23 @@ bool UEditorInteractiveGizmoManager::GetShowEditorGizmosForView(IToolsContextRen
void UEditorInteractiveGizmoManager::UpdateActiveEditorGizmos()
{
const bool bEnableEditorGizmos = UsesNewTRSGizmos();
if (!bEnableEditorGizmos)
{
if (bShowEditorGizmos)
{
if (UTransformGizmo* Gizmo = FindDefaultTransformGizmo())
{
DestroyGizmo(Gizmo);
}
DestroyAllEditorGizmos();
}
bShowEditorGizmos = false;
return;
}
const bool bEditorModeToolsSupportsWidgetDrawing = EditorModeManager ? EditorModeManager->GetShowWidget() : true;
const bool bEnableEditorGizmos = (CVarUseLegacyWidget.GetValueOnGameThread() == 0);
const bool bNewShowEditorGizmos = bEditorModeToolsSupportsWidgetDrawing && bEnableEditorGizmos;
if (bShowEditorGizmos != bNewShowEditorGizmos)
@@ -101,6 +101,9 @@ private:
EDITORINTERACTIVETOOLSFRAMEWORK_API void RemoveCVarBinding();
EDITORINTERACTIVETOOLSFRAMEWORK_API void RemoveViewportsBinding();
typedef FName FEditorModeID;
bool SwapDefaultMode(const FEditorModeID InCurrentDefaultMode, const FEditorModeID InNewDefaultMode) const;
FOnGizmoCreated OnGizmoCreated;
FEditorModeTools* ModeTools = nullptr;
@@ -129,6 +129,11 @@ public:
/** instance/builder identifiers for transform gizmo */
static const FString& TransformInstanceIdentifier();
static const FString& TransformBuilderIdentifier();
/**
* Returns true if the new TRS gizmos are used.
*/
static bool UsesNewTRSGizmos();
protected: