/// <summary> /// Tear down /// </summary> public void OnDestroy() { if (fm != null) { fm.flashSubscribers -= FlashToggle; } //JUtil.LogMessage(this, "OnDestroy() for {0}", GetHashCode()); if (del != null) { try { rpmComp.UnregisterVariableCallback(variableName, del); } catch { //JUtil.LogMessage(this, "Trapped exception unregistering JSIVariableLabel (you can ignore this)"); } } Destroy(textObj); textObj = null; }
/// <summary> /// Start everything up and get it configured. /// </summary> public void Start() { try { rpmComp = RasterPropMonitorComputer.Instantiate(internalProp, true); Transform textObjTransform = internalProp.FindModelTransform(transformName); Vector3 localScale = internalProp.transform.localScale; Transform offsetTransform = new GameObject().transform; offsetTransform.gameObject.name = "JSILabel-" + this.internalProp.propID + "-" + this.GetHashCode().ToString(); offsetTransform.gameObject.layer = textObjTransform.gameObject.layer; offsetTransform.SetParent(textObjTransform, false); offsetTransform.Translate(transformOffset.x * localScale.x, transformOffset.y * localScale.y, 0.0f); textObj = offsetTransform.gameObject.AddComponent<JSITextMesh>(); font = JUtil.LoadFont(fontName, fontQuality); textObj.font = font; //textObj.fontSize = fontQuality; // This doesn't work with Unity-embedded fonts textObj.fontSize = font.fontSize; if (!string.IsNullOrEmpty(anchor)) { if (anchor == TextAnchor.LowerCenter.ToString()) { textObj.anchor = TextAnchor.LowerCenter; } else if (anchor == TextAnchor.LowerLeft.ToString()) { textObj.anchor = TextAnchor.LowerLeft; } else if (anchor == TextAnchor.LowerRight.ToString()) { textObj.anchor = TextAnchor.LowerRight; } else if (anchor == TextAnchor.MiddleCenter.ToString()) { textObj.anchor = TextAnchor.MiddleCenter; } else if (anchor == TextAnchor.MiddleLeft.ToString()) { textObj.anchor = TextAnchor.MiddleLeft; } else if (anchor == TextAnchor.MiddleRight.ToString()) { textObj.anchor = TextAnchor.MiddleRight; } else if (anchor == TextAnchor.UpperCenter.ToString()) { textObj.anchor = TextAnchor.UpperCenter; } else if (anchor == TextAnchor.UpperLeft.ToString()) { textObj.anchor = TextAnchor.UpperLeft; } else if (anchor == TextAnchor.UpperRight.ToString()) { textObj.anchor = TextAnchor.UpperRight; } else { JUtil.LogErrorMessage(this, "Unrecognized anchor '{0}' in config for {1} ({2})", anchor, internalProp.propID, internalProp.propName); } } if (!string.IsNullOrEmpty(alignment)) { if (alignment == TextAlignment.Center.ToString()) { textObj.alignment = TextAlignment.Center; } else if (alignment == TextAlignment.Left.ToString()) { textObj.alignment = TextAlignment.Left; } else if (alignment == TextAlignment.Right.ToString()) { textObj.alignment = TextAlignment.Right; } else { JUtil.LogErrorMessage(this, "Unrecognized alignment '{0}' in config for {1} ({2})", alignment, internalProp.propID, internalProp.propName); } } float sizeScalar = 32.0f / (float)font.fontSize; textObj.characterSize = fontSize * 0.00005f * sizeScalar; textObj.lineSpacing = textObj.lineSpacing * lineSpacing; // "Normal" mode if (string.IsNullOrEmpty(switchTransform)) { // Force oneshot if there's no variables: oneshot |= !labelText.Contains("$&$"); string sourceString = labelText.UnMangleConfigText(); if (!string.IsNullOrEmpty(sourceString) && sourceString.Length > 1) { // Alow a " character to escape leading whitespace if (sourceString[0] == '"') { sourceString = sourceString.Substring(1); } } labels.Add(new JSILabelSet(sourceString, rpmComp, oneshot)); if (!oneshot) { rpmComp.UpdateDataRefreshRate(refreshRate); } } else // Switchable mode { SmarterButton.CreateButton(internalProp, switchTransform, Click); audioOutput = JUtil.SetupIVASound(internalProp, switchSound, switchSoundVolume, false); foreach (ConfigNode node in GameDatabase.Instance.GetConfigNodes("PROP")) { if (node.GetValue("name") == internalProp.propName) { ConfigNode moduleConfig = node.GetNodes("MODULE")[moduleID]; ConfigNode[] variableNodes = moduleConfig.GetNodes("VARIABLESET"); for (int i = 0; i < variableNodes.Length; i++) { try { bool lOneshot = false; if (variableNodes[i].HasValue("oneshot")) { bool.TryParse(variableNodes[i].GetValue("oneshot"), out lOneshot); } if (variableNodes[i].HasValue("labelText")) { string lText = variableNodes[i].GetValue("labelText"); string sourceString = lText.UnMangleConfigText(); lOneshot |= !lText.Contains("$&$"); labels.Add(new JSILabelSet(sourceString, rpmComp, lOneshot)); if (!lOneshot) { rpmComp.UpdateDataRefreshRate(refreshRate); } } } catch (ArgumentException e) { JUtil.LogErrorMessage(this, "Error in building prop number {1} - {0}", e.Message, internalProp.propID); } } break; } } } if (!string.IsNullOrEmpty(zeroColor)) { zeroColorValue = JUtil.ParseColor32(zeroColor, part, ref rpmComp); textObj.color = zeroColorValue; } bool usesMultiColor = false; if (!(string.IsNullOrEmpty(variableName) || string.IsNullOrEmpty(positiveColor) || string.IsNullOrEmpty(negativeColor) || string.IsNullOrEmpty(zeroColor))) { usesMultiColor = true; positiveColorValue = JUtil.ParseColor32(positiveColor, part, ref rpmComp); negativeColorValue = JUtil.ParseColor32(negativeColor, part, ref rpmComp); del = (Action<float>)Delegate.CreateDelegate(typeof(Action<float>), this, "OnCallback"); rpmComp.RegisterVariableCallback(variableName, del); registeredVessel = vessel.id; // Initialize the text color. Actually, callback registration takes care of that. } if (string.IsNullOrEmpty(emissive)) { if (usesMultiColor) { emissiveMode = EmissiveMode.active; } else { emissiveMode = EmissiveMode.always; } } else if (emissive.ToLower() == EmissiveMode.always.ToString()) { emissiveMode = EmissiveMode.always; } else if (emissive.ToLower() == EmissiveMode.never.ToString()) { emissiveMode = EmissiveMode.never; } else if (emissive.ToLower() == EmissiveMode.active.ToString()) { emissiveMode = EmissiveMode.active; } else if (emissive.ToLower() == EmissiveMode.passive.ToString()) { emissiveMode = EmissiveMode.passive; } else if (emissive.ToLower() == EmissiveMode.flash.ToString()) { if (flashRate > 0.0f) { emissiveMode = EmissiveMode.flash; fm = JUtil.InstallFlashModule(part, flashRate); if (fm != null) { fm.flashSubscribers += FlashToggle; } } else { emissiveMode = EmissiveMode.active; } } else { JUtil.LogErrorMessage(this, "Unrecognized emissive mode '{0}' in config for {1} ({2})", emissive, internalProp.propID, internalProp.propName); emissiveMode = EmissiveMode.always; } UpdateShader(); } catch (Exception e) { JUtil.LogErrorMessage(this, "Start failed in prop {1} ({2}) with exception {0}", e, internalProp.propID, internalProp.propName); labels.Add(new JSILabelSet("ERR", rpmComp, true)); } }
/// <summary> /// Start everything up and get it configured. /// </summary> public void Start() { try { rpmComp = RasterPropMonitorComputer.Instantiate(internalProp, true); Transform textObjTransform = internalProp.FindModelTransform(transformName); Vector3 localScale = internalProp.transform.localScale; Transform offsetTransform = new GameObject().transform; offsetTransform.gameObject.name = "JSILabel-" + this.internalProp.propID + "-" + this.GetHashCode().ToString(); offsetTransform.gameObject.layer = textObjTransform.gameObject.layer; offsetTransform.SetParent(textObjTransform, false); offsetTransform.Translate(transformOffset.x * localScale.x, transformOffset.y * localScale.y, 0.0f); textObj = offsetTransform.gameObject.AddComponent <JSITextMesh>(); font = JUtil.LoadFont(fontName, fontQuality); textObj.font = font; //textObj.fontSize = fontQuality; // This doesn't work with Unity-embedded fonts textObj.fontSize = font.fontSize; if (!string.IsNullOrEmpty(anchor)) { if (anchor == TextAnchor.LowerCenter.ToString()) { textObj.anchor = TextAnchor.LowerCenter; } else if (anchor == TextAnchor.LowerLeft.ToString()) { textObj.anchor = TextAnchor.LowerLeft; } else if (anchor == TextAnchor.LowerRight.ToString()) { textObj.anchor = TextAnchor.LowerRight; } else if (anchor == TextAnchor.MiddleCenter.ToString()) { textObj.anchor = TextAnchor.MiddleCenter; } else if (anchor == TextAnchor.MiddleLeft.ToString()) { textObj.anchor = TextAnchor.MiddleLeft; } else if (anchor == TextAnchor.MiddleRight.ToString()) { textObj.anchor = TextAnchor.MiddleRight; } else if (anchor == TextAnchor.UpperCenter.ToString()) { textObj.anchor = TextAnchor.UpperCenter; } else if (anchor == TextAnchor.UpperLeft.ToString()) { textObj.anchor = TextAnchor.UpperLeft; } else if (anchor == TextAnchor.UpperRight.ToString()) { textObj.anchor = TextAnchor.UpperRight; } else { JUtil.LogErrorMessage(this, "Unrecognized anchor '{0}' in config for {1} ({2})", anchor, internalProp.propID, internalProp.propName); } } if (!string.IsNullOrEmpty(alignment)) { if (alignment == TextAlignment.Center.ToString()) { textObj.alignment = TextAlignment.Center; } else if (alignment == TextAlignment.Left.ToString()) { textObj.alignment = TextAlignment.Left; } else if (alignment == TextAlignment.Right.ToString()) { textObj.alignment = TextAlignment.Right; } else { JUtil.LogErrorMessage(this, "Unrecognized alignment '{0}' in config for {1} ({2})", alignment, internalProp.propID, internalProp.propName); } } float sizeScalar = 32.0f / (float)font.fontSize; textObj.characterSize = fontSize * 0.00005f * sizeScalar; textObj.lineSpacing = textObj.lineSpacing * lineSpacing; // "Normal" mode if (string.IsNullOrEmpty(switchTransform)) { // Force oneshot if there's no variables: oneshot |= !labelText.Contains("$&$"); string sourceString = labelText.UnMangleConfigText(); if (!string.IsNullOrEmpty(sourceString) && sourceString.Length > 1) { // Alow a " character to escape leading whitespace if (sourceString[0] == '"') { sourceString = sourceString.Substring(1); } } labels.Add(new JSILabelSet(sourceString, rpmComp, oneshot)); if (!oneshot) { rpmComp.UpdateDataRefreshRate(refreshRate); } } else // Switchable mode { SmarterButton.CreateButton(internalProp, switchTransform, Click); audioOutput = JUtil.SetupIVASound(internalProp, switchSound, switchSoundVolume, false); foreach (ConfigNode node in GameDatabase.Instance.GetConfigNodes("PROP")) { if (node.GetValue("name") == internalProp.propName) { ConfigNode moduleConfig = node.GetNodes("MODULE")[moduleID]; ConfigNode[] variableNodes = moduleConfig.GetNodes("VARIABLESET"); for (int i = 0; i < variableNodes.Length; i++) { try { bool lOneshot = false; if (variableNodes[i].HasValue("oneshot")) { bool.TryParse(variableNodes[i].GetValue("oneshot"), out lOneshot); } if (variableNodes[i].HasValue("labelText")) { string lText = variableNodes[i].GetValue("labelText"); string sourceString = lText.UnMangleConfigText(); lOneshot |= !lText.Contains("$&$"); labels.Add(new JSILabelSet(sourceString, rpmComp, lOneshot)); if (!lOneshot) { rpmComp.UpdateDataRefreshRate(refreshRate); } } } catch (ArgumentException e) { JUtil.LogErrorMessage(this, "Error in building prop number {1} - {0}", e.Message, internalProp.propID); } } break; } } } if (!string.IsNullOrEmpty(zeroColor)) { zeroColorValue = JUtil.ParseColor32(zeroColor, part, ref rpmComp); textObj.color = zeroColorValue; } bool usesMultiColor = false; if (!(string.IsNullOrEmpty(variableName) || string.IsNullOrEmpty(positiveColor) || string.IsNullOrEmpty(negativeColor) || string.IsNullOrEmpty(zeroColor))) { usesMultiColor = true; positiveColorValue = JUtil.ParseColor32(positiveColor, part, ref rpmComp); negativeColorValue = JUtil.ParseColor32(negativeColor, part, ref rpmComp); del = (Action <float>)Delegate.CreateDelegate(typeof(Action <float>), this, "OnCallback"); rpmComp.RegisterVariableCallback(variableName, del); registeredVessel = vessel.id; // Initialize the text color. Actually, callback registration takes care of that. } if (string.IsNullOrEmpty(emissive)) { if (usesMultiColor) { emissiveMode = EmissiveMode.active; } else { emissiveMode = EmissiveMode.always; } } else if (emissive.ToLower() == EmissiveMode.always.ToString()) { emissiveMode = EmissiveMode.always; } else if (emissive.ToLower() == EmissiveMode.never.ToString()) { emissiveMode = EmissiveMode.never; } else if (emissive.ToLower() == EmissiveMode.active.ToString()) { emissiveMode = EmissiveMode.active; } else if (emissive.ToLower() == EmissiveMode.passive.ToString()) { emissiveMode = EmissiveMode.passive; } else if (emissive.ToLower() == EmissiveMode.flash.ToString()) { if (flashRate > 0.0f) { emissiveMode = EmissiveMode.flash; fm = JUtil.InstallFlashModule(part, flashRate); if (fm != null) { fm.flashSubscribers += FlashToggle; } } else { emissiveMode = EmissiveMode.active; } } else { JUtil.LogErrorMessage(this, "Unrecognized emissive mode '{0}' in config for {1} ({2})", emissive, internalProp.propID, internalProp.propName); emissiveMode = EmissiveMode.always; } UpdateShader(); } catch (Exception e) { JUtil.LogErrorMessage(this, "Start failed in prop {1} ({2}) with exception {0}", e, internalProp.propID, internalProp.propName); labels.Add(new JSILabelSet("ERR", rpmComp, true)); } }