/
DisplayModule.cs
311 lines (262 loc) · 10.4 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
using System;
using JetBrains.Annotations;
using UnityEngine;
using UnityEngine.Profiling;
namespace MuMech
{
public class DisplayModule : ComputerModule
{
public bool Hidden;
public Rect WindowPos
{
get => !HighLogic.LoadedSceneIsEditor
? new Rect(WindowVector.x, WindowVector.y, WindowVector.z, WindowVector.w)
: new Rect(WindowVectorEditor.x, WindowVectorEditor.y, WindowVectorEditor.z, WindowVectorEditor.w);
protected 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;
}
}
}
}
// Persistence is via a Vector4 since ConfigNode doesn't know how to serialize Rects
[UsedImplicitly]
[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
[UsedImplicitly]
[Persistent(pass = (int)Pass.GLOBAL)]
public Vector4 WindowVectorEditor = new Vector4(10, 40, 0, 0);
[UsedImplicitly]
[Persistent(pass = (int)Pass.GLOBAL)]
public bool ShowInFlightConfig = true;
public bool ShowInFlight
{
get => ShowInFlightConfig;
set
{
if (ShowInFlightConfig != value)
{
ShowInFlightConfig = value;
Dirty = true;
}
}
}
[UsedImplicitly]
[Persistent(pass = (int)Pass.GLOBAL)]
public bool ShowInEditorConfig;
public bool ShowInEditor
{
get => ShowInEditorConfig;
set
{
if (ShowInEditorConfig == value) return;
ShowInEditorConfig = value;
Dirty = true;
}
}
[UsedImplicitly]
[Persistent(pass = (int)Pass.GLOBAL)]
public bool IsOverlayConfig;
public bool IsOverlay
{
get => IsOverlayConfig;
set
{
if (IsOverlayConfig == value) return;
IsOverlayConfig = value;
Dirty = true;
}
}
[UsedImplicitly]
[Persistent(pass = (int)Pass.GLOBAL)]
public bool LockedConfig;
public bool Locked
{
get => LockedConfig;
set
{
if (LockedConfig == value) return;
LockedConfig = value;
Dirty = true;
}
}
internal bool EnabledEditor;
internal bool EnabledFlight;
private GUILayoutOption[] _windowOptions;
public bool ShowInCurrentScene => HighLogic.LoadedSceneIsEditor ? ShowInEditorConfig : ShowInFlightConfig;
private readonly int _id;
private static int _nextID = 72190852;
protected DisplayModule(MechJebCore core)
: base(core)
{
_id = _nextID;
_nextID++;
}
protected virtual GUILayoutOption[] WindowOptions() => Array.Empty<GUILayoutOption>();
protected void WindowGUI(int windowID, bool draggable)
{
if (!IsOverlayConfig && 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 = !LockedConfig;
if (!LockedConfig && !IsOverlayConfig && 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();
}
private 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) return;
// Cache the array to not create one each frame
_windowOptions ??= WindowOptions();
WindowPos = GUILayout.Window(_id, WindowPos, ProfiledWindowGUI, IsOverlayConfig ? "" : 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"))
{
if (bool.TryParse(global.GetValue("enabledEditor"), out bool loadedEnabled))
{
EnabledEditor = loadedEnabled;
useOldConfig = false;
if (HighLogic.LoadedSceneIsEditor)
Enabled = loadedEnabled;
}
}
if (global != null && global.HasValue("enabledFlight"))
{
if (bool.TryParse(global.GetValue("enabledFlight"), out bool loadedEnabled))
{
EnabledFlight = loadedEnabled;
useOldConfig = false;
if (HighLogic.LoadedSceneIsFlight)
Enabled = loadedEnabled;
}
}
if (useOldConfig)
{
if (global != null && global.HasValue("enabled"))
{
if (bool.TryParse(global.GetValue("enabled"), out bool 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()
{
_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) continue;
if (active |= m.Users.RecursiveUser(this))
break;
}
return active;
}
public override void UnlockCheck()
{
if (UnlockChecked) return;
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";
}
}