/// <summary> /// Handle a changed value /// </summary> /// <param name="newValue"></param> private void VariableCallback(double newValue, CompoundPageText cpt) { bool newState = (newValue > 0.0); if (newState != cpt.currentState) { cpt.currentState = newState; if (cpt.textObject != null) { cpt.textObject.SetActive(cpt.currentState); } if (!coroutineActive) { coroutineActive = true; comp.StartCoroutine(TextMethodUpdate()); } } }
internal MASPageCompoundText(ConfigNode config, InternalProp prop, MASFlightComputer comp, MASMonitor monitor, Transform pageRoot, float depth) : base(config, prop, comp) { this.comp = comp; if (!config.TryGetValue("maxLines", ref maxLines)) { throw new ArgumentException("Missing 'maxLines' in COMPOUND_TEXT " + name); } if (maxLines < 1) { throw new ArgumentException("'maxLines' must be greater than zero in COMPOUND_TEXT " + name); } string localFonts = string.Empty; if (!config.TryGetValue("font", ref localFonts)) { localFonts = string.Empty; } string styleStr = string.Empty; FontStyle style = FontStyle.Normal; if (config.TryGetValue("style", ref styleStr)) { style = MdVTextMesh.FontStyle(styleStr); } else { style = monitor.defaultStyle; } Vector2 fontSize = Vector2.zero; if (!config.TryGetValue("fontSize", ref fontSize) || fontSize.x < 0.0f || fontSize.y < 0.0f) { fontSize = monitor.fontSize; } lineAdvance = fontSize.y; Color32 textColor; string textColorStr = string.Empty; if (!config.TryGetValue("textColor", ref textColorStr) || string.IsNullOrEmpty(textColorStr)) { textColor = monitor.textColor_; } else { textColor = Utility.ParseColor32(textColorStr, comp); } // Set up our text. textOrigin = pageRoot.position + new Vector3(monitor.screenSize.x * -0.5f, monitor.screenSize.y * 0.5f, depth); rootObject = new GameObject(); rootObject.name = Utility.ComposeObjectName(pageRoot.gameObject.name, this.GetType().Name, name, (int)(-depth / MASMonitor.depthDelta)); rootObject.layer = pageRoot.gameObject.layer; rootObject.transform.parent = pageRoot; rootObject.transform.position = textOrigin; string positionString = string.Empty; if (config.TryGetValue("position", ref positionString)) { string[] positions = Utility.SplitVariableList(positionString); if (positions.Length != 2) { throw new ArgumentException("position does not contain 2 values in COMPOUND_TEXT " + name); } variableRegistrar.RegisterVariableChangeCallback(positions[0], (double newValue) => { position.x = (float)newValue * monitor.fontSize.x; rootObject.transform.position = textOrigin + new Vector3(position.x, -position.y, 0.0f); }); variableRegistrar.RegisterVariableChangeCallback(positions[1], (double newValue) => { position.y = (float)newValue * monitor.fontSize.y; rootObject.transform.position = textOrigin + new Vector3(position.x, -position.y, 0.0f); }); } Font font; if (string.IsNullOrEmpty(localFonts)) { font = monitor.defaultFont; } else { font = MASLoader.GetFont(localFonts.Trim()); } List <CompoundPageText> textNodes = new List <CompoundPageText>(); ConfigNode[] textConfigNodes = config.GetNodes("TEXT"); foreach (ConfigNode textNode in textConfigNodes) { CompoundPageText cpt = new CompoundPageText(); if (!textNode.TryGetValue("name", ref cpt.name)) { cpt.name = "anonymous"; } string variableName = string.Empty; if (!textNode.TryGetValue("variable", ref variableName)) { variableName.Trim(); } string text = string.Empty; if (textNode.TryGetValue("text", ref text)) { cpt.textObject = new GameObject(); cpt.textObject.name = Utility.ComposeObjectName(pageRoot.gameObject.name, this.GetType().Name + textNodes.Count, cpt.name, (int)(-depth / MASMonitor.depthDelta)); cpt.textObject.layer = rootObject.layer; cpt.textObject.transform.parent = rootObject.transform; cpt.textObject.transform.position = rootObject.transform.position; cpt.textMesh = cpt.textObject.AddComponent <MdVTextMesh>(); cpt.textMesh.material = new Material(MASLoader.shaders["MOARdV/TextMonitor"]); cpt.textMesh.SetFont(font, fontSize); cpt.textMesh.SetColor(textColor); cpt.textMesh.material.SetFloat(Shader.PropertyToID("_EmissiveFactor"), 1.0f); cpt.textMesh.fontStyle = style; // text, immutable, preserveWhitespace, comp, prop cpt.textMesh.SetText(text, false, true, comp, prop); } // Process callbacks if (!string.IsNullOrEmpty(variableName)) { if (cpt.textObject != null) { cpt.textObject.SetActive(false); } variableRegistrar.RegisterVariableChangeCallback(variableName, (double newValue) => VariableCallback(newValue, cpt)); } else { cpt.currentState = true; if (!coroutineActive) { coroutineActive = true; comp.StartCoroutine(TextMethodUpdate()); } } textNodes.Add(cpt); } textElements = textNodes.ToArray(); string masterVariableName = string.Empty; if (config.TryGetValue("variable", ref masterVariableName)) { rootObject.SetActive(false); variableRegistrar.RegisterVariableChangeCallback(masterVariableName, VariableCallback); } else { rootObject.SetActive(true); } RenderPage(false); }