forked from dunclaw/AirPark
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAirParkToolBar.cs
executable file
·157 lines (130 loc) · 4.95 KB
/
AirParkToolBar.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
//Code Adapted from https://github.com/BahamutoD/VesselMover/blob/master/VesselMoverToolbar.cs
using System;
using System.Collections;
using UnityEngine;
using KSP.UI.Screens;
namespace AirPark
{
[KSPAddon(KSPAddon.Startup.Flight, false)]
class AirParkToolbar : 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;
void VesselChange(Vessel v)
{
if (!v.isActiveVessel) return;
}
void Start()
{
toolbarPosition = new Vector2(Screen.width - toolbarWidth - 80, 50);
toolbarRect = new Rect(toolbarPosition.x, toolbarPosition.y + 100, toolbarWidth, toolbarHeight);
contentWidth = toolbarWidth - (2 * toolbarMargin);
AddToolbarButton();
GameEvents.onVesselChange.Add(VesselChange);
}
void OnGUI()
{
if (toolbarGuiEnabled) //&& AirParkPM.instance)
{
GUI.Window(999666, toolbarRect, ToolbarWindow, "AirPark", HighLogic.Skin.window);
}
}
void ToolbarWindow(int windowID)
{
float line = 0;
line += 1.25f;
if (!FlightGlobals.ActiveVessel) { return; }
if (AirPark.Instance)
{
if (!AirPark.Parked)
{
if (GUI.Button(LineRect(ref line, 1.5f), "Park Vessel", HighLogic.Skin.button))
{
if (FlightGlobals.ActiveVessel && !FlightGlobals.ActiveVessel.Landed)
AirPark.Instance.TogglePark();
}
}
else
{
if (GUI.Button(LineRect(ref line, 2), "Un-Park", HighLogic.Skin.button))
{
AirPark.Instance.TogglePark();
}
}
line += 0.2f;
Rect spawnVesselRect = LineRect(ref line);
svRectScreenSpace = new Rect(spawnVesselRect);
svRectScreenSpace.x += toolbarRect.x;
svRectScreenSpace.y += toolbarRect.y;
if (!AirPark.autoPark)
{
if (GUI.Button(spawnVesselRect, "Auto-Park OFF", HighLogic.Skin.button))
{
AirPark.Instance.ToggleAutoPark();
}
}
else
{
if (GUI.Button(spawnVesselRect, "Auto-Park ON", HighLogic.Skin.button))
{
AirPark.Instance.ToggleAutoPark();
}
}
}
else
{
GUIStyle centerLabelStyle = new GUIStyle(HighLogic.Skin.label) { alignment = TextAnchor.UpperCenter };
GUI.Label(LineRect(ref line), "No AirPark Module Found", centerLabelStyle);
}
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("AirPark/Icon/AirPark", false);
ApplicationLauncher.Instance.AddModApplication(ShowToolbarGUI, HideToolbarGUI, Dummy, Dummy, Dummy, Dummy, ApplicationLauncher.AppScenes.FLIGHT, buttonTexture);
hasAddedButton = true;
}
}
}
public void ShowToolbarGUI()
{
AirParkToolbar.toolbarGuiEnabled = true;
}
public void HideToolbarGUI()
{
AirParkToolbar.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);
}
}
}