11using System ;
22using System . Collections . Generic ;
33using System . Linq ;
4+ using System . Reflection . Emit ;
45using System . Text ;
56using System . Threading . Tasks ;
67using JetBrains . Annotations ;
8+ using TMPro ;
79using UnityEngine ;
810
911namespace JSI
@@ -204,11 +206,13 @@ class LabelBatch
204206 public GameObject batchRoot ;
205207 public MeshRenderer renderer ;
206208 public MeshFilter meshFilter ;
209+ public JSILabel . TextBatchInfo batchInfo ;
207210
208211 public List < JSITextMesh > textMeshes = new List < JSITextMesh > ( ) ;
209212 public bool needsUpdate = true ;
213+
210214
211- public LabelBatch ( )
215+ public LabelBatch ( JSILabel firstLabel )
212216 {
213217 batchRoot = new GameObject ( "Label Batch Root" ) ;
214218 batchRoot . layer = 20 ;
@@ -217,8 +221,52 @@ public LabelBatch()
217221 renderer = batchRoot . AddComponent < MeshRenderer > ( ) ;
218222 renderer . shadowCastingMode = UnityEngine . Rendering . ShadowCastingMode . Off ;
219223 renderer . receiveShadows = true ;
220- renderer . material = new Material ( JUtil . LoadInternalShader ( "RPM/JSILabel" ) ) ;
224+ renderer . material = firstLabel . textObj . material ;
225+ renderer . material . mainTexture = firstLabel . batchInfo . font . material . mainTexture ;
221226 meshFilter = batchRoot . AddComponent < MeshFilter > ( ) ;
227+
228+ batchInfo = firstLabel . batchInfo ;
229+
230+ if ( batchInfo . variableName != null )
231+ {
232+ firstLabel . rpmComp . RegisterVariableCallback ( batchInfo . variableName , VariableChangedCallback ) ;
233+ }
234+ }
235+
236+ public void VariableChangedCallback ( float value )
237+ {
238+ Color32 newColor ;
239+ float newEmissive ; // TODO: flash support...
240+
241+ if ( value == 0.0f )
242+ {
243+ newColor = batchInfo . zeroColor ;
244+ newEmissive = 0.0f ;
245+ }
246+ else if ( value < 0.0f )
247+ {
248+ newColor = batchInfo . negativeColor ;
249+ newEmissive = 0.0f ;
250+ }
251+ else
252+ {
253+ newColor = batchInfo . positiveColor ;
254+ newEmissive = 1.0f ;
255+ }
256+
257+ foreach ( var textMesh in textMeshes )
258+ {
259+ if ( ! newColor . Compare ( textMesh . color ) )
260+ {
261+ textMesh . color = newColor ;
262+ needsUpdate = true ;
263+ }
264+ }
265+
266+ if ( newEmissive != renderer . material . GetFloat ( JSILabel . emissiveFactorIndex ) )
267+ {
268+ renderer . material . SetFloat ( JSILabel . emissiveFactorIndex , newEmissive ) ;
269+ }
222270 }
223271
224272 public void LateUpdate ( )
@@ -249,10 +297,8 @@ public void AddStaticLabel(JSILabel label)
249297 LabelBatch labelBatch ;
250298 if ( ! labelBatches . TryGetValue ( label . batchInfo , out labelBatch ) )
251299 {
252- labelBatch = new LabelBatch ( ) ;
300+ labelBatch = new LabelBatch ( label ) ;
253301 labelBatches . Add ( label . batchInfo , labelBatch ) ;
254- labelBatch . renderer . material = label . batchInfo . font . material ;
255- // TODO: hook up variable callback
256302 // TODO: hook up flashing behavior
257303 }
258304
@@ -278,5 +324,17 @@ void LateUpdate()
278324 labelBatch . LateUpdate ( ) ;
279325 }
280326 }
327+
328+ void OnDestroy ( )
329+ {
330+ RasterPropMonitorComputer rpmComp = RasterPropMonitorComputer . FindFromProp ( internalProp ) ;
331+ foreach ( var labelBatch in labelBatches )
332+ {
333+ if ( labelBatch . Key . variableName != null )
334+ {
335+ rpmComp . UnregisterVariableCallback ( labelBatch . Key . variableName , labelBatch . Value . VariableChangedCallback ) ;
336+ }
337+ }
338+ }
281339 }
282340}
0 commit comments