forked from technogeeky/KSPPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SkinsLibrary.cs
189 lines (164 loc) · 7.3 KB
/
SkinsLibrary.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
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Log = KSPPP.Logging.ConsoleLogger;
namespace KSPPP
{
public static class SkinsLibrary {
#region Constructor
static SkinsLibrary()
{
_Initialized = false;
knownSkins = new Dictionary<string, GUISkin>();
GUIStyle_.knownStyles = new List<GUIStyle>();
Color_.initColorTable (); // initalize the table of known colors
}
#endregion
private static GUISkin _CurrentSkin;
private static GUIStyle _CurrentTooltip;
internal static bool _Initialized { get; private set; }
public static GUISkin DefUnitySkin { get; private set; }
public static GUISkin DefKSPSkin { get; private set; }
internal static GUISkin CurrentSkin { get { return _CurrentSkin; } }
internal static GUIStyle CurrentTooltip { get { return _CurrentTooltip; } }
internal static Dictionary<string, GUISkin> knownSkins { get; set; }
public delegate void SkinChangedEvent();
public static event SkinChangedEvent OnSkinChanged;
public delegate void TooltipChangedEvent();
public static event TooltipChangedEvent OnTooltipChanged; //FIXME: unused
internal static void InitSkinList() {
Log.Debug("in InitSkinList()");
if (!_Initialized) {
DefUnitySkin = GUI.skin;
DefKSPSkin = HighLogic.Skin;
knownSkins.Add ("Unity",DefUnitySkin);
knownSkins.Add ("KSP",DefKSPSkin);
DefUnitySkin.dumpSkins ();
SetCurrent("KSP");
_Initialized = true;
}
}
public static void SetCurrent(string SkinID) {
GUISkin OldSkin = _CurrentSkin;
Log.Debug("Setting GUISkin(string SkinID) to {0}", SkinID);
if (knownSkins.ContainsKey(SkinID)) _CurrentSkin = knownSkins[SkinID];
else Log.Now("SetCurrent: GUISkin {0} not found", SkinID);
SetCurrentTooltip(); // Now set the tooltip style as well
if (OldSkin != CurrentSkin && OnSkinChanged != null) OnSkinChanged();
}
public static void SetCurrentTooltip() {
//Use the custom skin if it exists
Log.Debug ("in SetCurrentTooltip()");
if (StyleExists(_CurrentSkin, "Tooltip")) _CurrentTooltip = GetStyle(_CurrentSkin, "Tooltip");
else { //otherwise lets build a style for the defaults or take the label style otherwise
if (_CurrentSkin == DefUnitySkin) _CurrentTooltip = new GUIStyle(DefUnitySkin.box);
else if (_CurrentSkin == DefKSPSkin) _CurrentTooltip = GenDefKSPTooltip();
else _CurrentTooltip = _CurrentSkin.label;
}
}
public static GUIStyle GenDefKSPTooltip() {
Log.Debug ("in GenDefKSPTooltip()");
GUIStyle retStyle = new GUIStyle(DefKSPSkin.label); // build a new style to return
Texture2D texBack = new Texture2D(1, 1, TextureFormat.ARGB32, false); // background texture
texBack.SetPixel(0, 0, new Color(0.5f, 0.5f, 0.5f, 0.95f)); // background color
texBack.Apply();
retStyle.normal.background = texBack; // put bkg into style
retStyle.normal.textColor = new Color32(224, 224, 224, 255); // set some text defaults
retStyle.padding = new RectOffset(3, 3, 3, 3); // set padding defaults
retStyle.alignment = TextAnchor.MiddleCenter; // set default center alignment
return retStyle;
}
public static GUISkin CopySkin(string SkinID) {
Log.Debug ("in CopySkin(string SkinID)");
if (knownSkins.ContainsKey(SkinID)) return (GUISkin)MBE.Instantiate(knownSkins[SkinID]);
else {
Log.Now("CopySkin(): GUISkin {0} not found", SkinID);
throw new SystemException(string.Format("CopySkin(): GUISkin {0} not found", SkinID));
}
}
public static void AddSkin(string SkinID, GUISkin NewSkin, bool SetAsCurrent = false) {
Log.Debug ("in AddSkin");
NewSkin.name = SkinID;
if (knownSkins.ContainsKey(SkinID)) knownSkins[SkinID] = NewSkin;
else knownSkins.Add(SkinID, NewSkin);
if (SetAsCurrent) SetCurrent(SkinID);
}
public static bool RemoveSkin(string SkinID) {
switch (SkinID) {
case "Unity":
case "KSP": Log.Now ("RemoveSkin({0}) failed: removing a built-in skin is prohibited.",SkinID); return false;
default: Log.Now ("RemoveSkin({0})",SkinID); return knownSkins.Remove (SkinID);
}
}
internal static bool SkinExists(string SkinID) { Log.Debug ("in SkinExists()"); return knownSkins.ContainsKey(SkinID); }
internal static bool StyleExists(string SkinID, string StyleID) {
return (SkinExists(SkinID) && StyleExists(knownSkins[SkinID], StyleID));
}
internal static bool StyleExists(GUISkin SkinToAction, string StyleID) {
Log.Debug ("in StyleExists(GUISkin s2a,)");
if (SkinToAction.customStyles.Any(x => x.name == StyleID))
return true;
else
{
//Log.log("Unable to find Style: {0} in Skin: {1}", StyleID, SkinToAction.name);
return false;
}
//return (SkinToAction.customStyles.Any(x => x.name == StyleID));
}
public static void AddStyle(GUIStyle NewStyle, string SkinId) {
Log.Debug ("in AddStyle(GUIStyle ns,string sid)");
if (SkinExists(SkinId)) {
GUISkin skinTemp = knownSkins[SkinId];
AddStyle(NewStyle, ref skinTemp);
}
}
internal static void AddStyle(GUIStyle NewStyle, string SkinId, string StyleID) {
Log.Debug ("in AddStyle(GUIStyle ns,string skinID, string StyleID)");
NewStyle.name = StyleID;
AddStyle(NewStyle, SkinId);
}
internal static void AddStyle(GUIStyle NewStyle, ref GUISkin SkinToAction, string StyleID) {
Log.Debug ("in AddStyle(GUIStyle ns,ref GUISkin, string StyleID)");
NewStyle.name = StyleID; // set the name
AddStyle(NewStyle, ref SkinToAction); // and push to the next method
}
internal static void AddStyle(GUIStyle NewStyle, ref GUISkin SkinToAction) {
Log.Debug ("in AddStyle(GUIStyle ns,ref GUISkin)");
if (string.IsNullOrEmpty (NewStyle.name)) {
Log.Now ("No Name Provided in the Style to add to {0}. Cannot add this.", SkinToAction.name);
return;
}
List<GUIStyle> lstTemp = SkinToAction.customStyles.ToList<GUIStyle>(); // convert to a list
if (lstTemp.Any(x => x.name == NewStyle.name)) { // add or edit the customstyle
GUIStyle styleTemp = lstTemp.First(x => x.name == NewStyle.name);
lstTemp.Remove(styleTemp); // if itexists then remove it first
}
lstTemp.Add(NewStyle); // add the new style
SkinToAction.customStyles = lstTemp.ToArray<GUIStyle>(); // write the list back to the array
}
internal static void RemoveStyle(string SkinID, string StyleID) {
Log.Debug ("in RemoveStyle(string SkinID,string StyleID)");
if (SkinExists(SkinID)) {
GUISkin skinTemp = knownSkins[SkinID];
RemoveStyle(ref skinTemp, StyleID);
}
}
internal static void RemoveStyle(ref GUISkin SkinToAction, string StyleID) {
Log.Debug ("in RemoveStyle(GUISkin s2a, string StyleID)");
if (StyleExists(SkinToAction, StyleID)) {
List<GUIStyle> lstTemp = SkinToAction.customStyles.ToList<GUIStyle>(); // convert to a list
GUIStyle styleTemp = lstTemp.First(x => x.name == StyleID); // find and ...
lstTemp.Remove(styleTemp); // ... remove the style
SkinToAction.customStyles = lstTemp.ToArray<GUIStyle>(); // write back
}
}
internal static GUIStyle GetStyle(GUISkin SkinToAction, string StyleID) {
Log.Debug ("in GetStyle(GUISkin s2a, string StyleID)");
if (StyleExists(SkinToAction, StyleID))
return SkinToAction.customStyles.First(x => x.name == StyleID);
else
return null;
}
}
}