-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathToolbarSuperWrapper.cs
165 lines (137 loc) · 4.62 KB
/
ToolbarSuperWrapper.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
using System;
using UnityEngine;
using AppScenes = KSP.UI.Screens.ApplicationLauncher.AppScenes;
namespace DaMichelToolbarSuperWrapper
{
public class ToolbarInfo
{
public String name;
public String tooltip;
public String toolbarTexture;
public String launcherTexture;
public GameScenes[] visibleInScenes;
};
public abstract class PluginWithToolbarSupport : UnityEngine.MonoBehaviour
{
private bool visibleByToolbars = true;
private bool visibleByKspGui = true;
private bool useToolbar = true;
private bool useAppLauncher = true;
private IButton toolbarButton = null;
private KSP.UI.Screens.ApplicationLauncherButton applauncherButton = null;
protected abstract ToolbarInfo GetToolbarInfo();
protected bool isGuiVisible
{
get { return visibleByKspGui && visibleByToolbars; }
}
protected bool buttonVisible
{
get { return applauncherButton != null && applauncherButton.isActiveAndEnabled; }
}
protected abstract void OnGuiVisibilityChange();
protected void SaveImmutableToolbarSettings(ConfigNode node)
{
node.AddValue("useToolbar", useToolbar);
node.AddValue("useAppLauncher", useAppLauncher);
}
protected void SaveMutableToolbarSettings(ConfigNode node)
{
node.AddValue("active", visibleByToolbars);
}
protected void LoadImmutableToolbarSettings(ConfigNode node)
{
node.TryGetValue("useToolbar", ref useToolbar);
node.TryGetValue("useAppLauncher", ref useAppLauncher);
}
protected void LoadMutableToolbarSettings(ConfigNode node)
{
node.TryGetValue("active", ref visibleByToolbars);
}
private void OnHideByToolbar()
{
visibleByToolbars = false;
OnGuiVisibilityChange();
}
private void OnShowByToolbar()
{
visibleByToolbars = true;
OnGuiVisibilityChange();
}
private void OnHideByKspGui()
{
visibleByKspGui = false;
OnGuiVisibilityChange();
}
private void OnShowByKspGui()
{
visibleByKspGui = true;
OnGuiVisibilityChange();
}
protected void InitializeToolbars()
{
if (ToolbarManager.ToolbarAvailable && useToolbar && toolbarButton == null)
{
ToolbarInfo tb = GetToolbarInfo();
toolbarButton = ToolbarManager.Instance.add(tb.name, tb.name);
toolbarButton.TexturePath = tb.toolbarTexture;
toolbarButton.ToolTip = tb.tooltip;
toolbarButton.Visibility = new GameScenesVisibility(tb.visibleInScenes);
toolbarButton.Enabled = true;
toolbarButton.OnClick += (e) =>
{
visibleByToolbars = !visibleByToolbars;
OnGuiVisibilityChange();
};
}
GameEvents.onHideUI.Add(OnHideByKspGui);
GameEvents.onShowUI.Add(OnShowByKspGui);
if (useAppLauncher)
GameEvents.onGUIApplicationLauncherReady.Add(OnGUIAppLauncherReady);
}
private void OnGUIAppLauncherReady()
{
if (applauncherButton == null)
{
ToolbarInfo tb = GetToolbarInfo();
var m = new System.Collections.Generic.Dictionary<GameScenes, AppScenes>();
m.Add(GameScenes.FLIGHT, AppScenes.FLIGHT | AppScenes.MAPVIEW);
m.Add(GameScenes.EDITOR, AppScenes.SPH | AppScenes.VAB);
m.Add(GameScenes.SPACECENTER, AppScenes.SPACECENTER);
// and so on ...
AppScenes v = AppScenes.NEVER;
foreach (GameScenes s in tb.visibleInScenes)
{
v |= m[s];
}
applauncherButton = KSP.UI.Screens.ApplicationLauncher.Instance.AddModApplication(
OnShowByToolbar,
OnHideByToolbar,
null,
null,
null,
null,
v,
(Texture)GameDatabase.Instance.GetTexture(tb.launcherTexture, false));
if (visibleByToolbars)
applauncherButton.SetTrue(false);
}
}
protected void TearDownToolbars()
{
// unregister, or else errors occur
GameEvents.onHideUI.Remove(OnHideByKspGui);
GameEvents.onShowUI.Remove(OnShowByKspGui);
GameEvents.onGUIApplicationLauncherReady.Remove(OnGUIAppLauncherReady);
if (applauncherButton != null)
{
KSP.UI.Screens.ApplicationLauncher.Instance.RemoveModApplication(applauncherButton);
applauncherButton = null;
}
if (toolbarButton != null)
{
toolbarButton.Destroy();
toolbarButton = null;
}
}
};
}