-
Notifications
You must be signed in to change notification settings - Fork 6
/
CC_MonoBehaviourWindow.cs
416 lines (350 loc) · 14.5 KB
/
CC_MonoBehaviourWindow.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/* Part of KSPPluginFramework
Version 1.2
Forum Thread:http://forum.kerbalspaceprogram.com/threads/66503-KSP-Plugin-Framework
Author: TriggerAu, 2014
License: The MIT License (MIT)
*/
using System;
using UnityEngine;
namespace CapCom.Framework
{
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
sealed class WindowInitialsAttribute : Attribute
{
public Boolean Visible { get; set; }
public Boolean DragEnabled { get; set; }
public Boolean ClampToScreen { get; set; }
public Boolean TooltipsEnabled { get; set; }
public String Caption { get; set; }
}
/// <summary>
/// An Extended version of the UnityEngine.MonoBehaviour Class
/// Basically a template for a Window, has the MonoBehaviourExtended properties and extra bits to make drawing a window easier
/// </summary>
public abstract class CC_MBW : CC_MBE
{
#region "Constructors"
internal CC_MBW()
: base()
{
//do the assembly name add so we get different windowIDs for multiple plugins
this.WindowID = UnityEngine.Random.Range(1000, 2000000) + _AssemblyName.GetHashCode();
this._Visible = false;
LogFormatted_DebugOnly("WindowID:{0}", WindowID);
//and look for any customattributes
WindowInitialsAttribute[] attrs = (WindowInitialsAttribute[])Attribute.GetCustomAttributes(this.GetType(), typeof(WindowInitialsAttribute));
foreach (WindowInitialsAttribute attr in attrs)
{
Visible = attr.Visible;
DragEnabled = attr.DragEnabled;
ClampToScreen = attr.ClampToScreen;
TooltipsEnabled = attr.TooltipsEnabled;
WindowCaption = attr.Caption;
}
}
///CANT USE THE ONES BELOW HERE AS WE NEED TO INSTANTIATE THE WINDOW USING AddComponent()
//internal MonoBehaviourWindow(String Caption)
// : this()
//{
// this.WindowCaption = Caption;
//}
//internal MonoBehaviourWindow(String Caption, Rect Position)
// : this(Caption)
//{
// this.WindowRect = Position;
//}
//TODO: Look at using this
// http://answers.unity3d.com/questions/445444/add-component-in-one-line-with-parameters.html
//internal static MonoBehaviourWindow CreateComponent(GameObject AttachTo)
//{
// MonoBehaviourWindow monoReturn;
// monoReturn = AttachTo.AddComponent<MonoBehaviourWindow>();
// return monoReturn;
//}
#endregion
protected override void Awake()
{
//just some debugging stuff here
LogFormatted_DebugOnly("New MBWindow Awakened");
//base.Awake();
}
protected override void Start()
{
base.Start();
GameEvents.onShowUI.Add(UIOn);
GameEvents.onHideUI.Add(UIOff);
GameEvents.onGUIMissionControlSpawn.Add(UIOff);
GameEvents.onGUIMissionControlDespawn.Add(UIOn);
GameEvents.onGUIRnDComplexSpawn.Add(UIOff);
GameEvents.onGUIRnDComplexDespawn.Add(UIOn);
GameEvents.onGUIAdministrationFacilitySpawn.Add(UIOff);
GameEvents.onGUIAdministrationFacilityDespawn.Add(UIOn);
GameEvents.onGUIAstronautComplexSpawn.Add(UIOff);
GameEvents.onGUIAstronautComplexDespawn.Add(UIOn);
}
protected override void OnDestroy()
{
base.OnDestroy();
GameEvents.onShowUI.Remove(UIOn);
GameEvents.onHideUI.Remove(UIOff);
GameEvents.onGUIMissionControlSpawn.Remove(UIOff);
GameEvents.onGUIMissionControlDespawn.Remove(UIOn);
GameEvents.onGUIRnDComplexSpawn.Remove(UIOff);
GameEvents.onGUIRnDComplexDespawn.Remove(UIOn);
GameEvents.onGUIAdministrationFacilitySpawn.Remove(UIOff);
GameEvents.onGUIAdministrationFacilityDespawn.Remove(UIOn);
GameEvents.onGUIAstronautComplexSpawn.Remove(UIOff);
GameEvents.onGUIAstronautComplexDespawn.Remove(UIOn);
}
private void UIOn()
{
showUI = true;
}
private void UIOff()
{
showUI = false;
}
private bool showUI = true;
/// <summary>
/// WindowID variable - randomly set at window creation
/// </summary>
internal Int32 WindowID { get; private set; }
/// <summary>
/// Window position on screen, is fed in to the Window routine and the resulting position after GUILayout is what you read
/// </summary>
internal Rect WindowRect;
/// <summary>
/// Caption of the Window
/// </summary>
internal String WindowCaption = null;
/// <summary>
/// Style of the Window
/// </summary>
internal GUIStyle WindowStyle = null;
/// <summary>
/// Layout Options for the GUILayout.Window function
/// </summary>
internal GUILayoutOption[] WindowOptions = null;
/// <summary>
/// Whether the window is draggable by mouse
/// </summary>
internal Boolean DragEnabled = false;
/// <summary>
/// A defined area (like a handle) where you can drag from. This is from the top left corner of the window. Lets you make isso it can be only draggable from a certain point, icon, title bar, etc
/// </summary>
internal Rect DragRect;
internal float _Scale = 1;
internal float Scale
{
get { return _Scale; }
set
{
if (value > 0.5f && value < 2.5f)
_Scale = value;
}
}
/// <summary>
/// Whether the window can be moved off the visible screen
/// </summary>
internal Boolean ClampToScreen = true;
/// <summary>
/// How close to the edges it can get if clamping is enabled - this can be negative if you want to allow it to go off screen by a certain amount
/// </summary>
internal RectOffset ClampToScreenOffset = new RectOffset(0, 0, 0, 0);
private Boolean _Visible;
/// <summary>
/// Whether the Window is visible or not. Changing this value will add/remove the window from the RenderingManager.PostDrawQueue
/// </summary>
internal Boolean Visible
{
get { return _Visible; }
set { _Visible = value; }
}
protected override void OnGUIEvery()
{
base.OnGUIEvery();
if (!showUI)
return;
if (!_Visible)
return;
Matrix4x4 previousGuiMatrix = GUI.matrix;
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(_Scale, _Scale, 1));
this.DrawGUI();
GUI.matrix = previousGuiMatrix;
}
/// <summary>
/// This is the Code that draws the window and sets the skin
/// !!!! You have to set the skin before drawing the window or you will scratch your head for ever
/// </summary>
internal void DrawGUI()
{
//this sets the skin on each draw loop
GUI.skin = CC_SkinsLibrary.CurrentSkin;
//keep the window locked to the screen if its supposed to be
if (ClampToScreen)
WindowRect = WindowRect.ClampToScreen(ClampToScreenOffset);
//Are we using a custom style of the skin style for the window
if (WindowStyle == null)
{
WindowRect = GUILayout.Window(WindowID, WindowRect, DrawWindowInternal, WindowCaption, WindowOptions);
}
else
{
WindowRect = GUILayout.Window(WindowID, WindowRect, DrawWindowInternal, WindowCaption, WindowStyle, WindowOptions);
}
//Draw the tooltip of its there to be drawn
if (TooltipsEnabled)
DrawToolTip();
}
/// <summary>
/// Time that the last iteration of RepeatingWorkerFunction ran for. Can use this value to see how much impact your code is having
/// </summary>
internal TimeSpan DrawWindowInternalDuration { get; private set; }
/// <summary>
/// Private function that handles wrapping the drawwindow functionality with the supplementary stuff - dragging, tooltips, etc
/// </summary>
/// <param name="id">The ID of the Window being drawn</param>
private void DrawWindowInternal(Int32 id)
{
//record the start date
DateTime Duration = DateTime.Now;
DrawWindowPre(id);
//This calls the must be overridden code
DrawWindow(id);
DrawWindowPost(id);
//Set the Tooltip variable based on whats in this window
if (TooltipsEnabled)
SetTooltipText();
//Are we allowing window drag
if (DragEnabled)
if (DragRect.height == 0 && DragRect.width == 0)
GUI.DragWindow();
else
GUI.DragWindow(DragRect);
//Now calc the duration
DrawWindowInternalDuration = (DateTime.Now - Duration);
}
/// <summary>
/// This is the optionally overridden function that runs before DrawWindow
/// </summary>
/// <param name="id">The ID of the Window being drawn</param>
protected virtual void DrawWindowPre(Int32 id) { }
/// <summary>
/// This is the must be overridden function that equates to the content of the window
/// </summary>
/// <param name="id">The ID of the Window being drawn</param>
protected abstract void DrawWindow(Int32 id);
/// <summary>
/// This is the optionally overridden function that runs after DrawWindow, but before tooltips, etc
/// </summary>
/// <param name="id">The ID of the Window being drawn</param>
protected virtual void DrawWindowPost(Int32 id) { }
#region Tooltip Work
/// <summary>
/// Whether tooltips should be displayed for this window
/// </summary>
internal Boolean TooltipsEnabled = false;
/// <summary>
/// Is a Tooltip currently showing
/// </summary>
internal Boolean TooltipShown { get; private set; }
/// <summary>
/// Whereis the tooltip positioned
/// </summary>
internal Rect TooltipPosition { get { return _TooltipPosition; } }
private Rect _TooltipPosition = new Rect();
/// <summary>
/// An offset from the mouse position to put the top left of the tooltip. Use this to get the tooltip out from under the cursor
/// </summary>
internal Vector2d TooltipMouseOffset = new Vector2d();
/// <summary>
/// Whether the Tooltip should stay where first drawn or follow the mouse
/// </summary>
internal Boolean TooltipStatic = false;
/// <summary>
/// How long the tooltips should be displayed before auto-hiding
///
/// Set to 0 to have them last for ever
/// </summary>
internal Int32 TooltipDisplayForSecs = 15;
/// <summary>
/// Maximum width in pixels of the tooltip window. Text will wrap inside that width.
///
/// Set to 0 for unity default behaviour
/// </summary>
internal Int32 TooltipMaxWidth = 250;
//Store the tooltip text from throughout the code
private String strToolTipText = "";
private String strLastTooltipText = "";
//store how long the tooltip has been displayed for
private Single fltTooltipTime = 0f;
/// <summary>
/// This is the meat of drawing the tooltip on screen
/// </summary>
private void DrawToolTip()
{
//Added drawing check to turn off tooltips when window hides
if (TooltipsEnabled && Visible && (strToolTipText != "") && ((TooltipDisplayForSecs == 0) || (fltTooltipTime < (Single)TooltipDisplayForSecs)))
{
GUIContent contTooltip = new GUIContent(strToolTipText);
GUIStyle styleTooltip = CC_SkinsLibrary.CurrentTooltip;
//if the content of the tooltip changes then reset the counter
if (!TooltipShown || (strToolTipText != strLastTooltipText))
fltTooltipTime = 0f;
//Calc the size of the Tooltip
_TooltipPosition.x = Event.current.mousePosition.x + (Single)TooltipMouseOffset.x;
_TooltipPosition.y = Event.current.mousePosition.y + (Single)TooltipMouseOffset.y;
//do max width calc if needed
if (TooltipMaxWidth > 0)
{
//calc the new width and height
float minwidth, maxwidth;
CC_SkinsLibrary.CurrentTooltip.CalcMinMaxWidth(contTooltip, out minwidth, out maxwidth); // figure out how wide one line would be
_TooltipPosition.width = Math.Min(TooltipMaxWidth - CC_SkinsLibrary.CurrentTooltip.padding.horizontal, maxwidth); //then work out the height with a max width
_TooltipPosition.height = CC_SkinsLibrary.CurrentTooltip.CalcHeight(contTooltip, TooltipPosition.width); // heres the result
}
else
{
//calc the new width and height
Vector2 Size = CC_SkinsLibrary.CurrentTooltip.CalcSize(contTooltip);
_TooltipPosition.width = Size.x;
_TooltipPosition.height = Size.y;
}
//set the style props for text layout
styleTooltip.stretchHeight = !(TooltipMaxWidth > 0);
styleTooltip.stretchWidth = !(TooltipMaxWidth > 0);
styleTooltip.wordWrap = (TooltipMaxWidth > 0);
//clamp it accordingly
if (ClampToScreen)
_TooltipPosition = _TooltipPosition.ClampToScreen(ClampToScreenOffset);
//Draw the Tooltip
GUI.Label(TooltipPosition, contTooltip, styleTooltip);
//On top of everything
GUI.depth = 0;
//update how long the tip has been on the screen and reset the flags
fltTooltipTime += Time.deltaTime;
TooltipShown = true;
}
else
{
//clear the flags
TooltipShown = false;
}
//if we've moved to a diffn tooltip then reset the counter
if (strToolTipText != strLastTooltipText) fltTooltipTime = 0f;
//make sure the last text is correct
strLastTooltipText = strToolTipText;
}
/// <summary>
/// This function is run at the end of each draw loop to store the tooltiptext for later
/// </summary>
private void SetTooltipText()
{
if (Event.current.type == EventType.Repaint)
{
strToolTipText = GUI.tooltip;
}
}
#endregion
}
}