Skip to content

Commit f0934cf

Browse files
committed
Fix scrollview opening to middle instead of top, split Window into 2 files
1 parent 2fc41c5 commit f0934cf

3 files changed

Lines changed: 180 additions & 167 deletions

File tree

src/KSPDataExport.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@
9090
<Compile Include="DataExport.cs" />
9191
<Compile Include="LoggableValue.cs" />
9292
<Compile Include="Utilities.cs" />
93-
<Compile Include="Window.cs" />
93+
<Compile Include="Window.Main.cs" />
94+
<Compile Include="Window.LoggedVals.cs" />
9495
</ItemGroup>
9596
<ItemGroup>
9697
<Folder Include="Properties\" />

src/Window.LoggedVals.cs

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
using System;
2+
using System.Collections;
3+
using System.Linq;
4+
using System.Reflection;
5+
using UnityEngine;
6+
using UnityEngine.UI;
7+
8+
namespace KSPDataExport
9+
{
10+
/// <summary>
11+
/// Loggable values window
12+
/// </summary>
13+
public partial class Window : MonoBehaviour
14+
{
15+
private void ShowLoggedValuesWindow()
16+
{
17+
if (!_loggedValsDialog)
18+
{
19+
SpawnLoggedValuesWindow();
20+
}
21+
22+
if (_loggedValsDialog != null && !_loggedValsDialog.gameObject.activeSelf)
23+
{
24+
_loggedValsDialog.gameObject.SetActive(true);
25+
}
26+
}
27+
28+
private void HideLoggedValuesWindow()
29+
{
30+
if (_loggedValsDialog && _loggedValsDialog.gameObject.activeSelf)
31+
{
32+
_loggedValsDialog.gameObject.SetActive(false);
33+
}
34+
}
35+
36+
// Ensure scrollbar is at top when initialized
37+
private IEnumerator FixScrollPivotNextFrame(DialogGUIScrollList list)
38+
{
39+
yield return null;
40+
41+
FieldInfo fi = typeof(DialogGUIScrollList)
42+
.GetField("scrollRect",
43+
BindingFlags.Instance | BindingFlags.NonPublic);
44+
45+
ScrollRect sr = fi?.GetValue(list) as ScrollRect;
46+
if (!sr || !sr.content) yield break;
47+
48+
sr.content.pivot = new Vector2(0, 1);
49+
sr.verticalNormalizedPosition = 1f;
50+
}
51+
52+
53+
private void BuildLoggedValuesDialogStructure()
54+
{
55+
_loggedValuesDialogContent.Clear();
56+
57+
for (int i = 0; i < Enum.GetValues(typeof(Category)).Length; i++)
58+
{
59+
Category category = (Category)i;
60+
var categoryValues = DataExport.LoggableValues.Where(v => v.Category == category).OrderBy(v => v.Name)
61+
.ToList();
62+
if (categoryValues.Count == 0) continue;
63+
64+
// Category Header
65+
DialogGUILabel header = new DialogGUILabel(Enum.GetName(typeof(Category), i), true);
66+
_loggedValuesDialogContent.Add(header);
67+
_loggedValuesDialogContent.Add(new DialogGUISpace(6));
68+
69+
// Toggles for values in this category
70+
foreach (LoggableValue value in categoryValues)
71+
{
72+
var toggle = new DialogGUIToggle(
73+
value.Logging,
74+
value.Name,
75+
(state) => { value.Logging = state; },
76+
190f
77+
);
78+
_loggedValuesDialogContent.Add(toggle);
79+
}
80+
81+
_loggedValuesDialogContent.Add(new DialogGUISpace(10));
82+
}
83+
}
84+
85+
private void SpawnLoggedValuesWindow()
86+
{
87+
const float windowWidth = 240;
88+
const float windowHeight = 600f;
89+
90+
var contentLayout = new DialogGUIVerticalLayout(
91+
10,
92+
0,
93+
4,
94+
new RectOffset(6, 6, 10, 10),
95+
TextAnchor.MiddleLeft
96+
);
97+
98+
foreach (var item in _loggedValuesDialogContent)
99+
{
100+
contentLayout.AddChild(item);
101+
}
102+
103+
var scrollList = new DialogGUIScrollList(
104+
new Vector2(windowWidth - 20, windowHeight - 60),
105+
false,
106+
true,
107+
new DialogGUIVerticalLayout(
108+
0, 0, 0, new RectOffset(),
109+
TextAnchor.UpperCenter, new DialogGUIContentSizer(
110+
ContentSizeFitter.FitMode.Unconstrained,
111+
ContentSizeFitter.FitMode.PreferredSize,
112+
true), contentLayout)
113+
);
114+
115+
116+
_loggedValsDialog = PopupDialog.SpawnPopupDialog(
117+
new Vector2(0.85f, 0.5f),
118+
new Vector2(0.85f, 0.5f),
119+
new MultiOptionDialog(
120+
"KSPDataExportLoggedVals",
121+
"",
122+
"Logged Values",
123+
HighLogic.UISkin,
124+
new Rect(0.85f, 0.5f, windowWidth, windowHeight),
125+
scrollList
126+
),
127+
true,
128+
HighLogic.UISkin,
129+
false
130+
);
131+
132+
StartCoroutine(FixScrollPivotNextFrame(scrollList));
133+
134+
if (!_loggedValsDialog) return;
135+
_loggedValsDialog.gameObject.AddComponent<DialogCloseButton>().OnDismiss = () =>
136+
{
137+
_showLoggedVals = false;
138+
};
139+
140+
_loggedValsDialog.gameObject.SetActive(false);
141+
}
142+
143+
private void DismissLoggedValuesWindow()
144+
{
145+
if (_loggedValsDialog == null) return;
146+
_loggedValsDialog.Dismiss();
147+
_loggedValsDialog = null;
148+
}
149+
}
150+
}

0 commit comments

Comments
 (0)