-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChecklistDisplay.cs
262 lines (211 loc) · 8.28 KB
/
ChecklistDisplay.cs
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
using KSP.UI.Screens;
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
namespace OMIChecklist
{
[KSPAddon(KSPAddon.Startup.Flight, false)]
public class ChecklistDisplay_Flight: ChecklistDisplay
{
protected override bool IsActive()
{
return HighLogic.LoadedSceneIsFlight;
}
}
[KSPAddon(KSPAddon.Startup.EditorAny, false)]
public class ChecklistDisplay_Editors : ChecklistDisplay
{
protected override bool IsActive()
{
return HighLogic.LoadedSceneIsEditor;
}
}
//
public class ChecklistDisplay : MonoBehaviour
{
private ApplicationLauncherButton _ChecklistButton;
private static readonly Texture2D buttontexture = GameDatabase.Instance.GetTexture(ChecklistConfig.Icon_Active, false);
private Rect _windowPosition = new Rect(300, 60, 400, 650);
private GUIStyle _windowStyle;
private GUIStyle _labelStyle;
private GUIStyle _scrollStyle;
private GUIStyle _toggleStyle;
private GUIStyle _categoryStyle;
private GUIStyle _displayStyle;
private Vector2 _scrollPos = Vector2.zero;
public static bool _windowVisible = false;
private bool _initComplete = false;
private string digits = "0000";
private int selectionGridInt = -1;
private string[] selectionStrings = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" };
void Awake()
{
_ChecklistButton = ApplicationLauncher.Instance.AddModApplication(GuiOn, GuiOff, null, null, null, null,
ApplicationLauncher.AppScenes.ALWAYS, buttontexture);
}
private void GuiOn()
{
_windowVisible = true;
}
private void GuiOff()
{
_windowVisible = false;
}
protected virtual bool IsActive()
{
return false;
}
public void Start()
{
if (!_initComplete)
{
InitStyles();
}
}
private void InitStyles()
{
_windowStyle = new GUIStyle(HighLogic.Skin.window)
{
fixedWidth = 400f,
fixedHeight = 500f
};
_labelStyle = new GUIStyle(HighLogic.Skin.label);
_scrollStyle = new GUIStyle(HighLogic.Skin.scrollView);
_toggleStyle = new GUIStyle(HighLogic.Skin.toggle);
_categoryStyle = new GUIStyle(HighLogic.Skin.label)
{
alignment = TextAnchor.MiddleCenter,
fontStyle = FontStyle.Bold
};
_displayStyle = new GUIStyle(HighLogic.Skin.label)
{
alignment = TextAnchor.MiddleCenter,
fontStyle = FontStyle.Bold,
fontSize = 14
};
_initComplete = true;
}
private void OnGUI()
{
if (!_windowVisible)
return;
if (!IsActive())
return;
OnDrawWindow();
}
private void OnDrawWindow()
{
_windowPosition = GUILayout.Window(1, _windowPosition, RenderWindow, "OMI Checklists", _windowStyle);
}
// ignoring this int because "i'll never have another window" and 640k is enough
private void RenderWindow(int windowId)
{
// row 1: keypad, display, load/clear buttons
GUILayout.BeginVertical();
GUILayout.BeginHorizontal();
// yeah, what follows is pretty horrid. I mean, I did it as cleanly as I know how, but this mixing the
// ui definition with the event handler code is a nightmare.
// This mod needs to be converted to use a prefab, badly.
selectionGridInt = GUILayout.SelectionGrid(selectionGridInt, selectionStrings, 5, GUILayout.Width(28 * 5));
if (selectionGridInt != -1)
{
KeypadPressed();
}
GUILayout.Label(digits, _displayStyle, new[] { GUILayout.Width(45), GUILayout.Height(40) });
GUILayout.BeginVertical();
if (GUILayout.Button("Load", GUILayout.Width(80)))
{
// execute load with contents of display
LoadChecklist(digits);
}
if (GUILayout.Button("Clear", GUILayout.Width(80)))
{
digits = "0000"; // formalize this
}
GUILayout.EndVertical();
GUILayout.EndHorizontal();
GUILayout.EndVertical(); // row 1
// checklist title
GUILayout.BeginVertical();
// this syntax is neat
//string checklistTitle = ChecklistManager.Instance.CurrentCategoryName()
string checklistTitle = ChecklistManager.Instance.ChecklistTitle();
GUILayout.Label("Loaded: <b>" + checklistTitle + "</b>", _labelStyle);
GUILayout.EndVertical();
// row 2: category
GUILayout.BeginVertical();
GUILayout.BeginHorizontal();
if (GUILayout.Button("<", GUILayout.Width(20)))
{
ChecklistManager.Instance.PrevCategory();
}
string categoryName = ChecklistManager.Instance.CurrentCategoryName();
GUILayout.Label(categoryName, _categoryStyle, GUILayout.Width(150));
if (GUILayout.Button(">", GUILayout.Width(20)))
{
ChecklistManager.Instance.NextCategory();
}
GUILayout.EndHorizontal();
GUILayout.EndVertical(); // row 2 category
// row 3: list items and buttons
GUILayout.BeginHorizontal(); // list on the left, buttons on the right
GUILayout.BeginVertical(); // scroll list
_scrollPos = GUILayout.BeginScrollView(_scrollPos, _scrollStyle, GUILayout.Width(300), GUILayout.Height(355));
GUILayout.BeginVertical(); // the items
List<ChecklistItem> items = ChecklistManager.Instance.CurrentChecklistItems();
if (items != null)
{
foreach (ChecklistItem item in items)
{
item.Checked = GUILayout.Toggle(item.Checked, item.Caption, _toggleStyle);
}
}
GUILayout.EndVertical(); // items
GUILayout.EndScrollView(); //
GUILayout.EndVertical(); // scroll list, and left side
GUILayout.BeginVertical(); // buttons, right side
if (GUILayout.Button("Check All", GUILayout.Width(80)))
{
ChecklistManager.Instance.CheckAll(true);
}
if (GUILayout.Button("Clear All", GUILayout.Width(80)))
{
ChecklistManager.Instance.CheckAll(false);
}
// breathe
GUILayout.Label("", GUILayout.Height(25));
// proceed
if (GUILayout.Button("PRO", new[] { GUILayout.Width(80), GUILayout.Height(80) })) // whopper w/cheese
{
ChecklistManager.Instance.Proceed();
}
GUILayout.EndVertical();
GUILayout.EndHorizontal(); // list and buttons
GUI.DragWindow();
}
internal void OnDestroy()
{
if (_ChecklistButton == null)
return;
ApplicationLauncher.Instance.RemoveModApplication(_ChecklistButton);
_ChecklistButton = null;
}
// this implements a sort of round-robin input method a little like the Gemini computer?
void KeypadPressed()
{
selectionGridInt++; // 0-9 -> 1-10
string newDigit = (selectionGridInt == 10) ? "0" : selectionGridInt.ToString();
// reset so nothing is selected (docs are silent about -1 but it works as expected)
selectionGridInt = -1;
// shift left and add new digit on the end
digits = digits.Substring(1, 3) + newDigit;
}
// once Load is clicked, the 4-digit code is passed here for loading the physical file if it exists
void LoadChecklist(String fileCode)
{
digits = "0000";
ChecklistManager.Instance.LoadChecklist(fileCode);
}
}
}