-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathHullBreachToolBar.cs
143 lines (118 loc) · 4.4 KB
/
HullBreachToolBar.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
//Code Adapted from https://github.com/BahamutoD/VesselMover/blob/master/VesselMoverToolbar.cs
using UnityEngine;
using KSP.UI.Screens;
namespace HullBreach
{
[KSPAddon(KSPAddon.Startup.Flight, false)]
class HullBreachToolBar : MonoBehaviour
{
public static bool hasAddedButton = false;
public static bool toolbarGuiEnabled = false;
Rect toolbarRect;
float toolbarWidth = 280;
float toolbarHeight = 0;
float toolbarMargin = 6;
float toolbarLineHeight = 20;
float contentWidth;
Vector2 toolbarPosition;
Rect svRectScreenSpace;
//bool showMoveHelp = false;
//float helpHeight;
void VesselChange(Vessel v)
{
if (!v.isActiveVessel) return;
}
void Start()
{
toolbarPosition = new Vector2(Screen.width - toolbarWidth - 80, 150);
toolbarRect = new Rect(toolbarPosition.x, toolbarPosition.y + 100, toolbarWidth, toolbarHeight);
contentWidth = toolbarWidth - (2 * toolbarMargin);
AddToolbarButton();
GameEvents.onVesselChange.Add(VesselChange);
}
void OnGUI()
{
if (toolbarGuiEnabled)
{
GUI.Window(302115, toolbarRect, ToolbarWindow, "HullBreach", HighLogic.Skin.window);
}
}
void ToolbarWindow(int windowID)
{
float line = 0;
line += 1.25f;
if (!FlightGlobals.ActiveVessel) { return; }
if (config.Instance.vesselHullBreach != null)
{
if (config.ecDrain)
{
if (GUI.Button(LineRect(ref line, 1.5f), "EC Drain ON", HighLogic.Skin.button))
{
//ModuleHullBreach.Instance.ToggleHullBreach();
config.ecDrain = false;
config.SaveConfig();
}
}
else
{
if (GUI.Button(LineRect(ref line, 2), "EC Drain OFF", HighLogic.Skin.button))
{
config.ecDrain = true;
config.SaveConfig();
}
}
}
else
{
GUIStyle centerLabelStyle = new GUIStyle(HighLogic.Skin.label) { alignment = TextAnchor.UpperCenter };
GUI.Label(LineRect(ref line), "No HullBreach Module Found", centerLabelStyle);
}
//line += 0.2f;
//Rect spawnVesselRect = LineRect(ref line);
//svRectScreenSpace = new Rect(spawnVesselRect);
//svRectScreenSpace.x += toolbarRect.x;
//svRectScreenSpace.y += toolbarRect.y;
toolbarRect.height = (line * toolbarLineHeight) + (toolbarMargin * 2);
}
Rect LineRect(ref float currentLine, float heightFactor = 1)
{
Rect rect = new Rect(toolbarMargin, toolbarMargin + (currentLine * toolbarLineHeight), contentWidth, toolbarLineHeight * heightFactor);
currentLine += heightFactor + 0.1f;
return rect;
}
void LineLabel(string label, ref float line)
{
GUI.Label(LineRect(ref line), label, HighLogic.Skin.label);
}
void AddToolbarButton()
{
if (HighLogic.LoadedSceneIsFlight)
{
if (!hasAddedButton)
{
Texture buttonTexture = GameDatabase.Instance.GetTexture("HullBreach/Icon/HullBreach", false);
ApplicationLauncher.Instance.AddModApplication(ShowToolbarGUI, HideToolbarGUI, Dummy, Dummy, Dummy, Dummy, ApplicationLauncher.AppScenes.FLIGHT, buttonTexture);
hasAddedButton = true;
}
}
}
public void ShowToolbarGUI()
{
HullBreachToolBar.toolbarGuiEnabled = true;
}
public void HideToolbarGUI()
{
HullBreachToolBar.toolbarGuiEnabled = false;
}
void Dummy()
{ }
public static bool MouseIsInRect(Rect rect)
{
return rect.Contains(MouseGUIPos());
}
public static Vector2 MouseGUIPos()
{
return new Vector3(Input.mousePosition.x, Screen.height - Input.mousePosition.y, 0);
}
}
}