-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathWindow.cs
More file actions
266 lines (232 loc) · 8.96 KB
/
Copy pathWindow.cs
File metadata and controls
266 lines (232 loc) · 8.96 KB
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
/**
* Window.cs
*
* Thunder Aerospace Corporation's library for the Kerbal Space Program, by Taranis Elsu
*
* (C) Copyright 2013, Taranis Elsu
*
* Kerbal Space Program is Copyright (C) 2013 Squad. See http://kerbalspaceprogram.com/. This
* project is in no way associated with nor endorsed by Squad.
*
* This code is licensed under the Attribution-NonCommercial-ShareAlike 3.0 (CC BY-NC-SA 3.0)
* creative commons license. See <http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode>
* for full details.
*
* Attribution — You are free to modify this code, so long as you mention that the resulting
* work is based upon or adapted from this code.
*
* Non-commercial - You may not use this work for commercial purposes.
*
* Share Alike — If you alter, transform, or build upon this work, you may distribute the
* resulting work only under the same or similar license to the CC BY-NC-SA 3.0 license.
*
* Note that Thunder Aerospace Corporation is a ficticious entity created for entertainment
* purposes. It is in no way meant to represent a real entity. Any similarity to a real entity
* is purely coincidental.
*/
using System;
using UnityEngine;
using PersistentTrails;
namespace Tac
{
abstract class Window<T>
{
private string windowTitle;
private int windowId;
private string configNodeName;
protected Rect windowPos;
private bool windowMouseDown;
private bool visible;
protected PartModule myPartModule;
private bool windowResizableX = false;
private bool windowResizableY = true;
protected GUIStyle closeButtonStyle;
private GUIStyle resizeStyle;
private GUIContent resizeContent;
protected Window(string windowTitle, PartModule p = null)
{
this.myPartModule = p;
this.windowTitle = windowTitle;
this.windowId = windowTitle.GetHashCode() + new System.Random().Next(65536);
configNodeName = windowTitle.Replace(" ", "");
windowPos = new Rect(60, 60, 60, 60);
windowMouseDown = false;
visible = false;
// Seems to break compile on monodevelop?
//var texture = Utilities.LoadImage<T>(IOUtils.GetFilePathFor(typeof(T), "resize.png"));
//resizeContent = (texture != null) ? new GUIContent(texture, "Drag to resize the window.") : new GUIContent("R", "Drag to resize the window.");
resizeContent = new GUIContent("R", "Drag to resize the window.");
Debug.Log("Window created: " + windowTitle);
}
public bool IsVisible()
{
return visible;
}
protected string GetConfigNodeName()
{
return configNodeName;
}
public virtual void SetVisible(bool newValue)
{
if (newValue)
{
if (!visible)
{
RenderingManager.AddToPostDrawQueue(3, new Callback(DrawWindow));
}
}
else
{
if (visible)
{
RenderingManager.RemoveFromPostDrawQueue(3, new Callback(DrawWindow));
}
}
this.visible = newValue;
}
public void SetResizeX(bool newValue)
{
windowResizableX = newValue;
}
public void SetResizeY(bool newValue)
{
windowResizableY = newValue;
}
public void ToggleVisible()
{
Debug.Log("Window.toggleVisible");
SetVisible(!visible);
}
public void SetSize(int width, int height)
{
windowPos.width = width;
windowPos.height = height;
}
public virtual void Load(ConfigNode config)
{
if (config.HasNode(configNodeName))
{
ConfigNode windowConfig = config.GetNode(configNodeName);
windowPos.x = GUIResources.GetValue(windowConfig, "x", windowPos.x);
windowPos.y = GUIResources.GetValue(windowConfig, "y", windowPos.y);
windowPos.width = GUIResources.GetValue(windowConfig, "width", windowPos.width);
windowPos.height = GUIResources.GetValue(windowConfig, "height", windowPos.height);
bool newValue = GUIResources.GetValue(windowConfig, "visible", visible);
//SetVisible(newValue);
}
}
public virtual void Save(ConfigNode config)
{
ConfigNode windowConfig;
if (config.HasNode(configNodeName))
{
windowConfig = config.GetNode(configNodeName);
}
else
{
windowConfig = new ConfigNode(configNodeName);
config.AddNode(windowConfig);
}
windowConfig.AddValue("visible", visible);
windowConfig.AddValue("x", windowPos.x);
windowConfig.AddValue("y", windowPos.y);
windowConfig.AddValue("width", windowPos.width);
windowConfig.AddValue("height", windowPos.height);
}
protected bool allowedToDraw()
{
//Debug.Log("Window::AllowedToDraw?");
if (this.myPartModule == null || this.myPartModule.vessel == null || this.myPartModule.vessel == FlightGlobals.ActiveVessel)
{
return true;
}
else
{
return false;
}
}
protected virtual void DrawWindow()
{
if (visible && allowedToDraw())
{
bool paused = false;
if (HighLogic.LoadedSceneIsFlight)
{
try
{
paused = PauseMenu.isOpen || FlightResultsDialog.isDisplaying;
}
catch (Exception)
{
// ignore the error and assume the pause menu is not open
}
}
if (!paused)
{
GUI.skin = HighLogic.Skin;
ConfigureStyles();
windowPos = GUIResources.EnsureVisible(windowPos);
windowPos = GUILayout.Window(windowId, windowPos, PreDrawWindowContents, windowTitle, GUILayout.ExpandWidth(windowResizableX),
GUILayout.ExpandHeight(windowResizableY), GUILayout.MinWidth(windowPos.width), GUILayout.MinHeight(windowPos.height));
}
}
}
protected virtual void ConfigureStyles()
{
if (closeButtonStyle == null)
{
closeButtonStyle = new GUIStyle(GUI.skin.button);
closeButtonStyle.padding = new RectOffset(5, 5, 3, 0);
closeButtonStyle.margin = new RectOffset(1, 1, 1, 1);
closeButtonStyle.stretchWidth = false;
closeButtonStyle.stretchHeight = false;
closeButtonStyle.alignment = TextAnchor.MiddleCenter;
resizeStyle = new GUIStyle(GUI.skin.button);
resizeStyle.alignment = TextAnchor.MiddleCenter;
resizeStyle.padding = new RectOffset(1, 1, 1, 1);
}
}
private void PreDrawWindowContents(int windowId)
{
DrawWindowContents(windowId);
if (GUI.Button(new Rect(windowPos.width - 24, 4, 20, 20), "X", closeButtonStyle))
{
SetVisible(false);
}
var resizeRect = new Rect(windowPos.width - 16, windowPos.height - 16, 16, 16);
GUI.Label(resizeRect, resizeContent, resizeStyle);
HandleWindowEvents(resizeRect);
GUI.DragWindow();
}
protected abstract void DrawWindowContents(int windowId);
private void HandleWindowEvents(Rect resizeRect)
{
var theEvent = Event.current;
if (theEvent != null)
{
if (theEvent.type == EventType.MouseDown && !windowMouseDown && theEvent.button == 0 && resizeRect.Contains(theEvent.mousePosition))
{
windowMouseDown = true;
theEvent.Use();
}
else if (theEvent.type == EventType.MouseDrag && windowMouseDown && theEvent.button == 0)
{
if (windowResizableX)
{
windowPos.width += theEvent.delta.x;
}
if (windowResizableY)
{
windowPos.height += theEvent.delta.y;
}
theEvent.Use();
}
else if (theEvent.type == EventType.MouseUp && windowMouseDown && theEvent.button == 0)
{
windowMouseDown = false;
theEvent.Use();
}
}
}
}
}