@@ -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
0 commit comments