/
DisplayModule.cs
321 lines (275 loc) · 10.7 KB
/
DisplayModule.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
using System;
using UnityEngine;
using UnityEngine.Profiling;
namespace MuMech
{
public class DisplayModule : ComputerModule
{
public bool hidden;
public Rect windowPos
{
get
{
if (!HighLogic.LoadedSceneIsEditor)
return new Rect(windowVector.x, windowVector.y, windowVector.z, windowVector.w);
return new Rect(windowVectorEditor.x, windowVectorEditor.y, windowVectorEditor.z, windowVectorEditor.w);
}
set
{
var newPos = new Vector4(
Math.Min(Math.Max(value.x, 0), GuiUtils.scaledScreenWidth - value.width),
Math.Min(Math.Max(value.y, 0), GuiUtils.scaledScreenHeight - value.height),
value.width, value.height
);
newPos.x = Mathf.Clamp(newPos.x, 10 - value.width, GuiUtils.scaledScreenWidth - 10);
newPos.y = Mathf.Clamp(newPos.y, 10 - value.height, GuiUtils.scaledScreenHeight - 10);
if (!HighLogic.LoadedSceneIsEditor)
{
if (windowVector != newPos)
{
Dirty = true;
windowVector = newPos;
}
}
else
{
if (windowVectorEditor != newPos)
{
Dirty = true;
windowVectorEditor = newPos;
}
}
}
}
// Those field should be private but Persistent has a bug that prevent it to work properly on parent class private fields
[Persistent(pass = (int)Pass.GLOBAL)]
public Vector4 windowVector = new Vector4(10, 40, 0, 0); //Persistence is via a Vector4 since ConfigNode doesn't know how to serialize Rects
[Persistent(pass = (int)Pass.GLOBAL)]
public Vector4
windowVectorEditor = new Vector4(10, 40, 0, 0); //Persistence is via a Vector4 since ConfigNode doesn't know how to serialize Rects
[Persistent(pass = (int)Pass.GLOBAL)]
public bool showInFlight = true;
public bool ShowInFlight
{
get => showInFlight;
set
{
if (showInFlight != value)
{
showInFlight = value;
Dirty = true;
}
}
}
[Persistent(pass = (int)Pass.GLOBAL)]
public bool showInEditor;
public bool ShowInEditor
{
get => showInEditor;
set
{
if (showInEditor != value)
{
showInEditor = value;
Dirty = true;
}
}
}
[Persistent(pass = (int)Pass.GLOBAL)]
public bool isOverlay;
public bool IsOverlay
{
get => isOverlay;
set
{
if (isOverlay != value)
{
isOverlay = value;
Dirty = true;
}
}
}
[Persistent(pass = (int)Pass.GLOBAL)]
public bool locked;
public bool Locked
{
get => locked;
set
{
if (locked != value)
{
locked = value;
Dirty = true;
}
}
}
internal bool enabledEditor;
internal bool enabledFlight;
private GUILayoutOption[] windowOptions;
public bool showInCurrentScene => HighLogic.LoadedSceneIsEditor ? showInEditor : showInFlight;
public int ID;
public static int nextID = 72190852;
public DisplayModule(MechJebCore core)
: base(core)
{
ID = nextID;
nextID++;
}
public virtual GUILayoutOption[] WindowOptions() => new GUILayoutOption[0];
protected void WindowGUI(int windowID, bool draggable)
{
if (!isOverlay && GUI.Button(new Rect(windowPos.width - 18, 2, 16, 16), ""))
{
Enabled = false;
}
// if (GUI.Button(new Rect(windowPos.width - 40, 2, 20, 16), (locked ? "X" : "O"))) // lock button needs an icon, letters look crap
// {
// locked = !locked;
// }
bool allowDrag = !locked;
if (!locked && !isOverlay && Core.Settings.useTitlebarDragging)
{
float x = Mouse.screenPos.x / GuiUtils.scale;
float y = Mouse.screenPos.y / GuiUtils.scale;
allowDrag = x >= windowPos.xMin + 3 && x <= windowPos.xMin + windowPos.width - 3 &&
y >= windowPos.yMin + 3 && y <= windowPos.yMin + 17;
}
if (draggable && allowDrag)
GUI.DragWindow();
}
protected void ProfiledWindowGUI(int windowID)
{
Profiler.BeginSample(GetType().Name);
WindowGUI(windowID);
Profiler.EndSample();
}
protected virtual void WindowGUI(int windowID) => WindowGUI(windowID, true);
public virtual void DrawGUI(bool inEditor)
{
if (showInCurrentScene)
{
// Cache the array to not create one each frame
if (windowOptions == null)
windowOptions = WindowOptions();
windowPos = GUILayout.Window(ID, windowPos, ProfiledWindowGUI, isOverlay ? "" : GetName(), windowOptions);
// var windows = core.GetComputerModules<DisplayModule>(); // on ice until there's a way to find which window is active, unless you like dragging other windows by snapping
//
// foreach (var w in windows)
// {
// if (w == this) { continue; }
//
// var diffL = (w.windowPos.x + w.windowPos.width - 2) - windowPos.x;
// var diffR = w.windowPos.x - (windowPos.x + windowPos.width + 2);
// var diffT = (w.windowPos.y + w.windowPos.height - 2) - windowPos.y;
// var diffB = w.windowPos.y - (windowPos.y + windowPos.height + 2);
//
// if (Math.Abs(diffL) <= 8)
// {
// SetPos(w.windowPos.x + w.windowPos.width - 2, windowPos.y);
// }
//
// if (Math.Abs(diffR) <= 8)
// {
// SetPos(w.windowPos.x - windowPos.width + 2, windowPos.y);
// }
// }
}
}
public override void OnSave(ConfigNode local, ConfigNode type, ConfigNode global)
{
base.OnSave(local, type, global);
if (global != null)
{
if (HighLogic.LoadedSceneIsEditor)
enabledEditor = Enabled;
if (HighLogic.LoadedSceneIsFlight)
enabledFlight = Enabled;
global.AddValue("enabledEditor", enabledEditor);
global.AddValue("enabledFlight", enabledFlight);
}
// if (global != null) global.AddValue("locked", locked);
}
public override void OnLoad(ConfigNode local, ConfigNode type, ConfigNode global)
{
base.OnLoad(local, type, global);
bool useOldConfig = true;
if (global != null && global.HasValue("enabledEditor"))
{
bool loadedEnabled;
if (bool.TryParse(global.GetValue("enabledEditor"), out loadedEnabled))
{
enabledEditor = loadedEnabled;
useOldConfig = false;
if (HighLogic.LoadedSceneIsEditor)
Enabled = loadedEnabled;
}
}
if (global != null && global.HasValue("enabledFlight"))
{
bool loadedEnabled;
if (bool.TryParse(global.GetValue("enabledFlight"), out loadedEnabled))
{
enabledFlight = loadedEnabled;
useOldConfig = false;
if (HighLogic.LoadedSceneIsFlight)
Enabled = loadedEnabled;
}
}
if (useOldConfig)
{
if (global != null && global.HasValue("enabled"))
{
bool loadedEnabled;
if (bool.TryParse(global.GetValue("enabled"), out loadedEnabled))
{
Enabled = loadedEnabled;
}
}
enabledEditor = Enabled;
enabledFlight = Enabled;
}
// if (global != null && global.HasValue("locked"))
// {
// bool loadedLocked;
// if (bool.TryParse(global.GetValue("locked"), out loadedLocked)) locked = loadedLocked;
// }
}
private ComputerModule[] makesActive;
public virtual bool isActive()
{
if (makesActive == null)
makesActive = new ComputerModule[] { Core.Attitude, Core.Thrust, Core.Rover, Core.Node, Core.RCS, Core.Rcsbal };
bool active = false;
for (int i = 0; i < makesActive.Length; i++)
{
ComputerModule m = makesActive[i];
if (m != null)
{
if (active |= m.Users.RecursiveUser(this))
break;
}
}
return active;
}
public override void UnlockCheck()
{
if (!UnlockChecked)
{
bool prevEn = Enabled;
Enabled = true;
base.UnlockCheck();
if (UnlockParts.Trim().Length > 0 || UnlockTechs.Trim().Length > 0 || !IsSpaceCenterUpgradeUnlocked())
{
hidden = !Enabled;
if (hidden)
{
prevEn = false;
}
}
Enabled = prevEn;
}
}
public virtual string GetName() => "Display Module";
public virtual string IconName() => "Display Module Icon";
}
}