Skip to content

Commit 57cf180

Browse files
committed
move some of JSILabel's initialization into OnLoad
1 parent aaa9d7f commit 57cf180

File tree

3 files changed

+53
-84
lines changed

3 files changed

+53
-84
lines changed

RasterPropMonitor/Auxiliary modules/JSILabel.cs

Lines changed: 41 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ public enum EmissiveMode
5656
[KSPField]
5757
public string fontName = "Arial";
5858
[KSPField]
59-
public string anchor = string.Empty;
59+
public TextAnchor anchor;
6060
[KSPField]
61-
public string alignment = string.Empty;
61+
public TextAlignment alignment;
6262
[KSPField]
6363
public int fontQuality = 32;
6464

@@ -87,8 +87,7 @@ public enum EmissiveMode
8787
private bool variablePositive = false;
8888
private bool flashOn = true;
8989

90-
private JSITextMesh textObj;
91-
private Font font;
90+
[SerializeField] private JSITextMesh textObj;
9291
private readonly int emissiveFactorIndex = Shader.PropertyToID("_EmissiveFactor");
9392

9493
private List<JSILabelSet> labels = new List<JSILabelSet>();
@@ -97,20 +96,38 @@ public enum EmissiveMode
9796

9897
private int updateCountdown;
9998
private Action<float> del;
100-
/// <summary>
101-
/// The Guid of the vessel we belonged to at Start. When undocking,
102-
/// KSP will change the vessel member variable before calling OnDestroy,
103-
/// which prevents us from getting the RPMVesselComputer we registered
104-
/// with. So we have to store the Guid separately.
105-
/// </summary>
106-
private Guid registeredVessel = Guid.Empty;
99+
107100
RasterPropMonitorComputer rpmComp;
108101
private JSIFlashModule fm;
109102

110103
public override void OnLoad(ConfigNode node)
111104
{
112105
moduleConfig = ScriptableObject.CreateInstance<ConfigNodeHolder>();
113106
moduleConfig.Node = node;
107+
108+
Transform textObjTransform = JUtil.FindPropTransform(internalProp, transformName);
109+
Vector3 localScale = internalProp.transform.localScale;
110+
111+
Transform offsetTransform = new GameObject().transform;
112+
offsetTransform.gameObject.name = "JSILabel-" + this.internalProp.propID + "-" + this.GetHashCode().ToString();
113+
offsetTransform.gameObject.layer = textObjTransform.gameObject.layer;
114+
offsetTransform.SetParent(textObjTransform, false);
115+
offsetTransform.Translate(transformOffset.x * localScale.x, transformOffset.y * localScale.y, 0.0f);
116+
117+
textObj = offsetTransform.gameObject.AddComponent<JSITextMesh>();
118+
119+
var font = JUtil.LoadFont(fontName, fontQuality);
120+
121+
textObj.font = font;
122+
//textObj.fontSize = fontQuality; // This doesn't work with Unity-embedded fonts
123+
textObj.fontSize = font.fontSize;
124+
125+
textObj.anchor = anchor;
126+
textObj.alignment = alignment;
127+
128+
float sizeScalar = 32.0f / (float)font.fontSize;
129+
textObj.characterSize = fontSize * 0.00005f * sizeScalar;
130+
textObj.lineSpacing *= lineSpacing;
114131
}
115132

116133
/// <summary>
@@ -127,67 +144,22 @@ public void Start()
127144
{
128145
rpmComp = RasterPropMonitorComputer.FindFromProp(internalProp);
129146

130-
Transform textObjTransform = JUtil.FindPropTransform(internalProp, transformName);
131-
Vector3 localScale = internalProp.transform.localScale;
132-
133-
Transform offsetTransform = new GameObject().transform;
134-
offsetTransform.gameObject.name = "JSILabel-" + this.internalProp.propID + "-" + this.GetHashCode().ToString();
135-
offsetTransform.gameObject.layer = textObjTransform.gameObject.layer;
136-
offsetTransform.SetParent(textObjTransform, false);
137-
offsetTransform.Translate(transformOffset.x * localScale.x, transformOffset.y * localScale.y, 0.0f);
138-
139-
textObj = offsetTransform.gameObject.AddComponent<JSITextMesh>();
140-
141-
font = JUtil.LoadFont(fontName, fontQuality);
142-
143-
textObj.font = font;
144-
//textObj.fontSize = fontQuality; // This doesn't work with Unity-embedded fonts
145-
textObj.fontSize = font.fontSize;
146-
147-
if (!string.IsNullOrEmpty(anchor))
148-
{
149-
if (Enum.TryParse(anchor, out TextAnchor textAnchor))
150-
{
151-
textObj.anchor = textAnchor;
152-
}
153-
else
154-
{
155-
JUtil.LogErrorMessage(this, "Unrecognized anchor '{0}' in config for {1} ({2})", anchor, internalProp.propID, internalProp.propName);
156-
}
157-
}
158-
159-
if (!string.IsNullOrEmpty(alignment))
160-
{
161-
if (Enum.TryParse(alignment, out TextAlignment textAlignment))
162-
{
163-
textObj.alignment = textAlignment;
164-
}
165-
else
166-
{
167-
JUtil.LogErrorMessage(this, "Unrecognized alignment '{0}' in config for {1} ({2})", alignment, internalProp.propID, internalProp.propName);
168-
}
169-
}
170-
171-
float sizeScalar = 32.0f / (float)font.fontSize;
172-
textObj.characterSize = fontSize * 0.00005f * sizeScalar;
173-
textObj.lineSpacing = textObj.lineSpacing * lineSpacing;
174-
175147
// "Normal" mode
176148
if (string.IsNullOrEmpty(switchTransform))
177149
{
178150
// Force oneshot if there's no variables:
179151
oneshot |= !labelText.Contains("$&$");
180-
string sourceString = labelText.UnMangleConfigText();
152+
string sourceString = labelText.UnMangleConfigText();
181153

182-
if (!string.IsNullOrEmpty(sourceString) && sourceString.Length > 1)
183-
{
184-
// Alow a " character to escape leading whitespace
185-
if (sourceString[0] == '"')
154+
if (!string.IsNullOrEmpty(sourceString) && sourceString.Length > 1)
186155
{
187-
sourceString = sourceString.Substring(1);
156+
// Alow a " character to escape leading whitespace
157+
if (sourceString[0] == '"')
158+
{
159+
sourceString = sourceString.Substring(1);
160+
}
188161
}
189-
}
190-
labels.Add(new JSILabelSet(sourceString, rpmComp, oneshot));
162+
labels.Add(new JSILabelSet(sourceString, rpmComp, oneshot));
191163

192164
if (!oneshot)
193165
{
@@ -205,14 +177,12 @@ public void Start()
205177
{
206178
try
207179
{
208-
bool lOneshot = false;
209-
if (variableNodes[i].HasValue("oneshot"))
210-
{
211-
bool.TryParse(variableNodes[i].GetValue("oneshot"), out lOneshot);
212-
}
213-
if (variableNodes[i].HasValue("labelText"))
180+
string lText = variableNodes[i].GetValue("labelText");
181+
if (lText != null)
214182
{
215-
string lText = variableNodes[i].GetValue("labelText");
183+
bool lOneshot = false;
184+
variableNodes[i].TryGetValue("oneshot", ref lOneshot);
185+
216186
string sourceString = lText.UnMangleConfigText();
217187
lOneshot |= !lText.Contains("$&$");
218188
labels.Add(new JSILabelSet(sourceString, rpmComp, lOneshot));
@@ -241,7 +211,6 @@ public void Start()
241211
negativeColorValue = JUtil.ParseColor32(negativeColor, rpmComp);
242212
del = (Action<float>)Delegate.CreateDelegate(typeof(Action<float>), this, "OnCallback");
243213
rpmComp.RegisterVariableCallback(variableName, del);
244-
registeredVessel = vessel.id;
245214

246215
emissive = EmissiveMode.active;
247216

RasterPropMonitor/Core/JSITextMesh.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace JSI
3434
/// </summary>
3535
public class JSITextMesh : MonoBehaviour
3636
{
37-
private TextAlignment alignment_;
37+
[SerializeField] private TextAlignment alignment_;
3838
public TextAlignment alignment
3939
{
4040
get
@@ -52,7 +52,7 @@ public TextAlignment alignment
5252
}
5353
}
5454

55-
private TextAnchor anchor_;
55+
[SerializeField] private TextAnchor anchor_;
5656
public TextAnchor anchor
5757
{
5858
get
@@ -70,7 +70,7 @@ public TextAnchor anchor
7070
}
7171
}
7272

73-
private float characterSize_ = 1.0f;
73+
[SerializeField] private float characterSize_ = 1.0f;
7474
public float characterSize
7575
{
7676
get
@@ -88,7 +88,7 @@ public float characterSize
8888
}
8989
}
9090

91-
private Color32 color_;
91+
[SerializeField] private Color32 color_;
9292
public Color32 color
9393
{
9494
get
@@ -106,7 +106,7 @@ public Color32 color
106106
}
107107
}
108108

109-
private Font font_;
109+
[SerializeReference] private Font font_;
110110
public Font font
111111
{
112112
get
@@ -129,7 +129,7 @@ public Font font
129129
}
130130
}
131131

132-
private int fontSize_ = 32;
132+
[SerializeField] private int fontSize_ = 32;
133133
public int fontSize
134134
{
135135
get
@@ -147,7 +147,7 @@ public int fontSize
147147
}
148148
}
149149

150-
private FontStyle fontStyle_;
150+
[SerializeField] private FontStyle fontStyle_;
151151
public FontStyle fontStyle
152152
{
153153
get
@@ -165,7 +165,7 @@ public FontStyle fontStyle
165165
}
166166
}
167167

168-
private float lineSpacing_ = 1.0f;
168+
[SerializeField] private float lineSpacing_ = 1.0f;
169169
public float lineSpacing
170170
{
171171
get
@@ -183,8 +183,8 @@ public float lineSpacing
183183
}
184184
}
185185

186-
private MeshRenderer meshRenderer_;
187-
private MeshFilter meshFilter_;
186+
[SerializeField] private MeshRenderer meshRenderer_;
187+
[SerializeField] private MeshFilter meshFilter_;
188188
public Material material
189189
{
190190
get
@@ -203,7 +203,7 @@ public Mesh mesh
203203
}
204204
}
205205

206-
private string text_;
206+
[SerializeField] private string text_;
207207
private bool richText = false;
208208
private bool hasColorTags = false;
209209
public string text

RasterPropMonitor/Core/VisibilityEnabler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace JSI
99
{
1010
class VisibilityEnabler : MonoBehaviour
1111
{
12-
private Behaviour m_behaviour;
12+
[SerializeField] private Behaviour m_behaviour;
1313

1414
public void Initialize(Behaviour behaviour)
1515
{

0 commit comments

Comments
 (0)