// Copyright Epic Games, Inc. All Rights Reserved. #include "EditorViewportTabContent.h" #include "SEditorViewport.h" #include "EditorViewportLayoutOnePane.h" #include "EditorViewportLayoutTwoPanes.h" #include "EditorViewportLayoutThreePanes.h" #include "EditorViewportLayoutFourPanes.h" #include "EditorViewportLayout2x2.h" #include "Framework/Docking/LayoutService.h" #include "CoreGlobals.h" #include "Misc/ConfigCacheIni.h" TSharedPtr FEditorViewportTabContent::ConstructViewportLayoutByTypeName(const FName& TypeName, bool bSwitchingLayouts) { TSharedPtr ViewportLayout; // The items in these ifs should match the names in namespace EditorViewportConfigurationNames if (TypeName == EditorViewportConfigurationNames::TwoPanesHoriz) ViewportLayout = MakeShareable(new FEditorViewportLayoutTwoPanesHoriz); else if (TypeName == EditorViewportConfigurationNames::TwoPanesVert) ViewportLayout = MakeShareable(new FEditorViewportLayoutTwoPanesVert); else if (TypeName == EditorViewportConfigurationNames::FourPanes2x2) ViewportLayout = MakeShareable(new FEditorViewportLayout2x2); else if (TypeName == EditorViewportConfigurationNames::ThreePanesLeft) ViewportLayout = MakeShareable(new FEditorViewportLayoutThreePanesLeft); else if (TypeName == EditorViewportConfigurationNames::ThreePanesRight) ViewportLayout = MakeShareable(new FEditorViewportLayoutThreePanesRight); else if (TypeName == EditorViewportConfigurationNames::ThreePanesTop) ViewportLayout = MakeShareable(new FEditorViewportLayoutThreePanesTop); else if (TypeName == EditorViewportConfigurationNames::ThreePanesBottom) ViewportLayout = MakeShareable(new FEditorViewportLayoutThreePanesBottom); else if (TypeName == EditorViewportConfigurationNames::FourPanesLeft) ViewportLayout = MakeShareable(new FEditorViewportLayoutFourPanesLeft); else if (TypeName == EditorViewportConfigurationNames::FourPanesRight) ViewportLayout = MakeShareable(new FEditorViewportLayoutFourPanesRight); else if (TypeName == EditorViewportConfigurationNames::FourPanesBottom) ViewportLayout = MakeShareable(new FEditorViewportLayoutFourPanesBottom); else if (TypeName == EditorViewportConfigurationNames::FourPanesTop) ViewportLayout = MakeShareable(new FEditorViewportLayoutFourPanesTop); else /*(TypeName == EditorViewportConfigurationNames::OnePane)*/ ViewportLayout = MakeShareable(new FEditorViewportLayoutOnePane); if (!ensure(ViewportLayout.IsValid())) { ViewportLayout = MakeShareable(new FEditorViewportLayoutOnePane); } return ViewportLayout; } void FEditorViewportTabContent::Initialize(TFunction(void)> Func, TSharedPtr InParentTab, const FString& InLayoutString) { check(!InLayoutString.IsEmpty()); ParentTab = InParentTab; LayoutString = InLayoutString; FName LayoutType(*LayoutString); SetViewportConfiguration(Func, LayoutType); } void FEditorViewportTabContent::SetViewportConfiguration(TFunction(void)>& Func, const FName& ConfigurationName) { ViewportCreationFunc = Func; SetViewportConfiguration(ConfigurationName); } void FEditorViewportTabContent::SetViewportConfiguration(const FName& ConfigurationName) { check(ViewportCreationFunc != nullptr); bool bSwitchingLayouts = ActiveViewportLayout.IsValid(); if (bSwitchingLayouts) { SaveConfig(); ActiveViewportLayout.Reset(); } ActiveViewportLayout = ConstructViewportLayoutByTypeName(ConfigurationName, bSwitchingLayouts); check(ActiveViewportLayout.IsValid()); UpdateViewportTabWidget(ViewportCreationFunc); OnViewportTabContentLayoutChangedEvent.Broadcast(); } void FEditorViewportTabContent::SaveConfig() const { if (ActiveViewportLayout.IsValid()) { if (!LayoutString.IsEmpty()) { FString LayoutTypeString = ActiveViewportLayout->GetLayoutTypeName().ToString(); const FString& IniSection = FLayoutSaveRestore::GetAdditionalLayoutConfigIni(); GConfig->SetString(*IniSection, *(LayoutString + TEXT(".LayoutType")), *LayoutTypeString, GEditorPerProjectIni); } ActiveViewportLayout->SaveLayoutString(LayoutString); } } TSharedPtr FEditorViewportTabContent::GetFirstViewport() { const TMap>& EditorViewports = ActiveViewportLayout->GetViewports(); for (auto& Pair: EditorViewports) { TSharedPtr ViewportWidget = StaticCastSharedPtr(Pair.Value)->AsWidget(); TSharedPtr Viewport = StaticCastSharedPtr(ViewportWidget); if (Viewport.IsValid()) { return Viewport; break; } } return nullptr; } void FEditorViewportTabContent::UpdateViewportTabWidget(TFunction(void)>& Func) { TSharedPtr ParentTabPinned = ParentTab.Pin(); if (ParentTabPinned.IsValid() && ActiveViewportLayout.IsValid()) { TSharedRef LayoutWidget = StaticCastSharedPtr(ActiveViewportLayout)->BuildViewportLayout(Func, ParentTabPinned, SharedThis(this), LayoutString); ParentTabPinned->SetContent(LayoutWidget); if (PreviouslyFocusedViewport.IsSet()) { TSharedPtr ViewportToFocus = ActiveViewportLayout->GetViewports().FindRef(PreviouslyFocusedViewport.GetValue()); if (ViewportToFocus.IsValid()) { ViewportToFocus->SetKeyboardFocus(); } PreviouslyFocusedViewport = TOptional(); } } }