File size: 3,878 Bytes
b315adb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright Epic Games, Inc. All Rights Reserved.

#include "SGameResponsivePanel.h"

#include "Framework/Application/SlateApplication.h"
#include "HAL/PlatformApplicationMisc.h"
#include "Layout/ArrangedChildren.h"
#include "Widgets/SViewport.h"

#define LOCTEXT_NAMESPACE "GameSetting"

//TODO Nick Darnell
// Hello.  It appears you've discovered this widget.
// This widget currently isn't being generally used.  I'm prototyping out some
// ideas for settings.  Talk to me.

SGameResponsivePanel::SGameResponsivePanel()
	: InnerGrid(SNew(SGridPanel))
{
	SetCanTick(false);
	bCanSupportFocus = false;
	bHasCustomPrepass = true;
	bHasRelativeLayoutScale = true;
	bCanWrapVertically = true;
}

void SGameResponsivePanel::Construct(const FArguments& InArgs)

{
	ChildSlot
	[
		InnerGrid
	];
}

SGridPanel::FSlot& SGameResponsivePanel::AddSlot()

{
	SGridPanel::FSlot* Slot;
	InnerGrid->AddSlot(InnerGrid->GetChildren()->Num(), 0)
		.Expose(Slot);
	InnerSlots.Add(Slot);

	RefreshLayout();

	return *Slot;
}

int32 SGameResponsivePanel::RemoveSlot(const TSharedRef<SWidget>& SlotWidget)

{
	for (int32 SlotIdx = 0; SlotIdx < InnerSlots.Num(); ++SlotIdx)
	{
		if (SlotWidget == InnerSlots[SlotIdx]->GetWidget())
		{
			InnerSlots.RemoveAt(SlotIdx);
			break;
		}
	}

	return InnerGrid->RemoveSlot(SlotWidget);
}

void SGameResponsivePanel::ClearChildren()

{
	InnerGrid->ClearChildren();
}

void SGameResponsivePanel::EnableVerticalStacking(const bool bCanVerticallyWrap)

{
	bCanWrapVertically = bCanVerticallyWrap;
}

bool SGameResponsivePanel::CustomPrepass(float LayoutScaleMultiplier)

{
	RefreshResponsiveness();
	return true;
}

void SGameResponsivePanel::OnArrangeChildren(const FGeometry& AllottedGeometry, FArrangedChildren& ArrangedChildren) const

{
	ArrangedChildren.AddWidget(AllottedGeometry.MakeChild(
		ChildSlot.GetWidget(),
		FVector2D(0, 0),
		AllottedGeometry.GetLocalSize() / Scale,
		Scale
	));
}

FVector2D SGameResponsivePanel::ComputeDesiredSize(float InLayoutScale) const

{
	return SCompoundWidget::ComputeDesiredSize(InLayoutScale) * Scale;
}

float SGameResponsivePanel::GetRelativeLayoutScale(int32 ChildIndex, float LayoutScaleMultiplier) const

{
	return Scale;
}

bool SGameResponsivePanel::ShouldWrap() const

{
	if (PhysialScreenSize.IsZero() || !bCanWrapVertically)
	{
		return false;
	}

	return (PhysialScreenSize.X < 7);
}

void SGameResponsivePanel::RefreshResponsiveness()

{
	PhysialScreenSize = FVector2D(0, 0);

	TSharedPtr<SViewport> GameViewport = FSlateApplication::Get().GetGameViewport();
	if (GameViewport.IsValid())
	{
		TSharedPtr<ISlateViewport> ViewportInterface = GameViewport->GetViewportInterface().Pin();
		if (ViewportInterface.IsValid())
		{
			const FIntPoint ViewportSize = ViewportInterface->GetSize();

			int32 ScreenDensity = 0;
			FPlatformApplicationMisc::GetPhysicalScreenDensity(ScreenDensity);
			
			if (ScreenDensity != 0)
			{
				PhysialScreenSize = ViewportSize / (float)ScreenDensity;
			}
		}
	}

	const bool bShouldWrap = ShouldWrap();
	const float NewScale = bShouldWrap ? 1.5f : 1.0f;
	if (!FMath::IsNearlyEqual(NewScale, Scale))
	{
		Scale = NewScale;
		RefreshLayout();
		Invalidate(EInvalidateWidgetReason::Prepass);
	}
}

void SGameResponsivePanel::RefreshLayout()

{
	const bool bShouldWrap = ShouldWrap();

	InnerGrid->ClearFill();

	for (int32 SlotIdx = 0; SlotIdx < InnerSlots.Num(); ++SlotIdx)
	{
		InnerSlots[SlotIdx]->SetColumn(bShouldWrap ? 0 : SlotIdx);
		InnerSlots[SlotIdx]->SetRow(bShouldWrap ? SlotIdx : 0);

		if (!bShouldWrap)
		{
			InnerGrid->SetColumnFill(SlotIdx, 1.0f);
		}
	}

	if (bShouldWrap)
	{
		InnerGrid->SetColumnFill(0, 1.0f);
	}
}

/////////////////////////////////////////////////////

#undef LOCTEXT_NAMESPACE