internal MASComponentTextLabel(ConfigNode config, InternalProp prop, MASFlightComputer comp) : base(config, prop, comp) { string transform = string.Empty; if (!config.TryGetValue("transform", ref transform)) { throw new ArgumentException("Missing 'transform' in TEXT_LABEL " + name); } string fontName = string.Empty; if (!config.TryGetValue("font", ref fontName)) { throw new ArgumentException("Invalid or missing 'font' in TEXT_LABEL " + name); } string styleStr = string.Empty; FontStyle style = FontStyle.Normal; if (config.TryGetValue("style", ref styleStr)) { style = MdVTextMesh.FontStyle(styleStr); } string text = string.Empty; if (!config.TryGetValue("text", ref text)) { throw new ArgumentException("Invalid or missing 'text' in TEXT_LABEL " + name); } if (!config.TryGetValue("fontSize", ref fontSize)) { throw new ArgumentException("Invalid or missing 'fontSize' in TEXT_LABEL " + name); } Vector2 transformOffset = Vector2.zero; if (!config.TryGetValue("transformOffset", ref transformOffset)) { transformOffset = Vector2.zero; } Transform textObjTransform = prop.FindModelTransform(transform); Vector3 localScale = prop.transform.localScale; Transform offsetTransform = new GameObject().transform; offsetTransform.gameObject.name = Utility.ComposeObjectName(this.GetType().Name, name, prop.propID); 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 <MdVTextMesh>(); Font font = MASLoader.GetFont(fontName.Trim()); if (font == null) { throw new ArgumentNullException("Unable to load font " + fontName + " in TEXT_LABEL " + name); } float lineSpacing = 1.0f; if (!config.TryGetValue("lineSpacing", ref lineSpacing)) { lineSpacing = 1.0f; } float sizeScalar = 32.0f / (float)font.fontSize; textObj.SetFont(font, font.fontSize, fontSize * 0.00005f * sizeScalar); textObj.SetLineSpacing(lineSpacing); textObj.fontStyle = style; string passiveColorStr = string.Empty; if (!config.TryGetValue("passiveColor", ref passiveColorStr)) { throw new ArgumentException("Invalid or missing 'passiveColor' in TEXT_LABEL " + name); } else { Color32 namedColor; if (comp.TryGetNamedColor(passiveColorStr, out namedColor)) { passiveColor = namedColor; } else { string[] startColors = Utility.SplitVariableList(passiveColorStr); if (startColors.Length < 3 || startColors.Length > 4) { throw new ArgumentException("passiveColor does not contain 3 or 4 values in TEXT_LABEL " + name); } variableRegistrar.RegisterVariableChangeCallback(startColors[0], (double newValue) => { passiveColor.r = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); if (blend) { UpdateBlendColor(); } else { UpdateBooleanColor(); } }); variableRegistrar.RegisterVariableChangeCallback(startColors[1], (double newValue) => { passiveColor.g = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); if (blend) { UpdateBlendColor(); } else { UpdateBooleanColor(); } }); variableRegistrar.RegisterVariableChangeCallback(startColors[2], (double newValue) => { passiveColor.b = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); if (blend) { UpdateBlendColor(); } else { UpdateBooleanColor(); } }); if (startColors.Length == 4) { variableRegistrar.RegisterVariableChangeCallback(startColors[3], (double newValue) => { passiveColor.a = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); if (blend) { UpdateBlendColor(); } else { UpdateBooleanColor(); } }); } } } // Final validations bool usesMulticolor = false; string activeColorStr = string.Empty; if (config.TryGetValue("activeColor", ref activeColorStr)) { usesMulticolor = true; Color32 namedColor; if (comp.TryGetNamedColor(activeColorStr, out namedColor)) { activeColor = namedColor; } else { string[] startColors = Utility.SplitVariableList(activeColorStr); if (startColors.Length < 3 || startColors.Length > 4) { throw new ArgumentException("activeColor does not contain 3 or 4 values in TEXT_LABEL " + name); } variableRegistrar.RegisterVariableChangeCallback(startColors[0], (double newValue) => { activeColor.r = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); if (blend) { UpdateBlendColor(); } else { UpdateBooleanColor(); } }); variableRegistrar.RegisterVariableChangeCallback(startColors[1], (double newValue) => { activeColor.g = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); if (blend) { UpdateBlendColor(); } else { UpdateBooleanColor(); } }); variableRegistrar.RegisterVariableChangeCallback(startColors[2], (double newValue) => { activeColor.b = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); if (blend) { UpdateBlendColor(); } else { UpdateBooleanColor(); } }); if (startColors.Length == 4) { variableRegistrar.RegisterVariableChangeCallback(startColors[3], (double newValue) => { activeColor.a = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); if (blend) { UpdateBlendColor(); } else { UpdateBooleanColor(); } }); } } } string anchor = string.Empty; if (!config.TryGetValue("anchor", ref anchor)) { anchor = string.Empty; } 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 { Utility.LogError(this, "Unrecognized anchor '{0}' in config for {1} ({2})", anchor, prop.propID, prop.propName); } } string alignment = string.Empty; if (!config.TryGetValue("alignment", ref alignment)) { alignment = string.Empty; } 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 { Utility.LogError(this, "Unrecognized alignment '{0}' in config for {1} ({2})", alignment, prop.propID, prop.propName); } } string emissive = string.Empty; config.TryGetValue("emissive", ref emissive); 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()) { emissiveMode = EmissiveMode.flash; } else { Utility.LogError(this, "Unrecognized emissive mode '{0}' in config for {1} ({2})", emissive, prop.propID, prop.propName); emissiveMode = EmissiveMode.always; } string variableName = string.Empty; if (!config.TryGetValue("variable", ref variableName) || string.IsNullOrEmpty(variableName)) { if (usesMulticolor) { throw new ArgumentException("Invalid or missing 'variable' in TEXT_LABEL " + name); } } else if (!usesMulticolor) { throw new ArgumentException("Invalid or missing 'activeColor' in TEXT_LABEL " + name); } config.TryGetValue("blend", ref blend); if (emissiveMode == EmissiveMode.flash) { if (blend == false && config.TryGetValue("flashRate", ref flashRate) && flashRate > 0.0f) { this.comp = comp; comp.RegisterFlashCallback(flashRate, FlashToggle); } else { emissiveMode = EmissiveMode.active; } } bool immutable = false; if (!config.TryGetValue("oneshot", ref immutable)) { immutable = false; } textObj.SetColor(passiveColor); textObj.SetText(text, immutable, false, comp, prop); UpdateShader(); if (!string.IsNullOrEmpty(variableName)) { variableRegistrar.RegisterVariableChangeCallback(variableName, VariableCallback); } }
internal MASPageLineGraph(ConfigNode config, InternalProp prop, MASFlightComputer comp, MASMonitor monitor, Transform pageRoot, float depth) : base(config, prop, comp) { Vector2 size = Vector2.zero; if (!config.TryGetValue("size", ref size)) { throw new ArgumentException("Unable to find 'size' in LINE_GRAPH " + name); } verticalSpan = size.y; if (!config.TryGetValue("sampleRate", ref sampleRate)) { throw new ArgumentException("Unable to find 'sampleRate' in LINE_GRAPH " + name); } maxSamples = (int)(size.x / sampleRate); graphPoints = new Vector3[maxSamples]; string sourceName = string.Empty; if (!config.TryGetValue("source", ref sourceName)) { throw new ArgumentException("Unable to find 'source' in LINE_GRAPH " + name); } variableRegistrar.RegisterVariableChangeCallback(sourceName, (double newValue) => sourceValue = (float)newValue); string sourceRange = string.Empty; if (!config.TryGetValue("sourceRange", ref sourceRange)) { throw new ArgumentException("Unable to find 'sourceRange' in LINE_GRAPH " + name); } string[] ranges = Utility.SplitVariableList(sourceRange); if (ranges.Length != 2) { throw new ArgumentException("Incorrect number of values in 'sourceRange' in LINE_GRAPH " + name); } variableRegistrar.RegisterVariableChangeCallback(ranges[0], (double newValue) => sourceRange1 = (float)newValue); variableRegistrar.RegisterVariableChangeCallback(ranges[1], (double newValue) => sourceRange2 = (float)newValue); float borderWidth = 0.0f; if (!config.TryGetValue("borderWidth", ref borderWidth)) { borderWidth = 0.0f; } else { borderWidth = Math.Max(1.0f, borderWidth); } string borderColorName = string.Empty; if (!config.TryGetValue("borderColor", ref borderColorName)) { borderColorName = string.Empty; } if (string.IsNullOrEmpty(borderColorName) == (borderWidth > 0.0f)) { throw new ArgumentException("Only one of 'borderColor' and 'borderWidth' are defined in LINE_GRAPH " + name); } string variableName = string.Empty; if (config.TryGetValue("variable", ref variableName)) { variableName = variableName.Trim(); } componentOrigin = pageRoot.position + new Vector3(monitor.screenSize.x * -0.5f, monitor.screenSize.y * 0.5f, depth); // Set up our display surface. if (borderWidth > 0.0f) { borderObject = new GameObject(); borderObject.name = Utility.ComposeObjectName(pageRoot.gameObject.name, this.GetType().Name, name + "-border", (int)(-depth / MASMonitor.depthDelta)); borderObject.layer = pageRoot.gameObject.layer; borderObject.transform.parent = pageRoot; borderObject.transform.position = pageRoot.position; borderObject.transform.Translate(monitor.screenSize.x * -0.5f + position.x, monitor.screenSize.y * 0.5f - position.y - size.y, depth); borderMaterial = new Material(MASLoader.shaders["MOARdV/Monitor"]); borderRenderer = borderObject.AddComponent <LineRenderer>(); borderRenderer.useWorldSpace = false; borderRenderer.material = borderMaterial; borderRenderer.startColor = borderColor; borderRenderer.endColor = borderColor; borderRenderer.startWidth = borderWidth; borderRenderer.endWidth = borderWidth; borderRenderer.positionCount = 4; borderRenderer.loop = true; Color32 namedColor; if (comp.TryGetNamedColor(borderColorName, out namedColor)) { borderColor = namedColor; lineRenderer.startColor = borderColor; lineRenderer.endColor = borderColor; } else { string[] startColors = Utility.SplitVariableList(borderColorName); if (startColors.Length < 3 || startColors.Length > 4) { throw new ArgumentException("borderColor does not contain 3 or 4 values in LINE_GRAPH " + name); } variableRegistrar.RegisterVariableChangeCallback(startColors[0], (double newValue) => { borderColor.r = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); borderRenderer.startColor = borderColor; borderRenderer.endColor = borderColor; }); variableRegistrar.RegisterVariableChangeCallback(startColors[1], (double newValue) => { borderColor.g = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); borderRenderer.startColor = borderColor; borderRenderer.endColor = borderColor; }); variableRegistrar.RegisterVariableChangeCallback(startColors[2], (double newValue) => { borderColor.b = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); borderRenderer.startColor = borderColor; borderRenderer.endColor = borderColor; }); if (startColors.Length == 4) { variableRegistrar.RegisterVariableChangeCallback(startColors[3], (double newValue) => { borderColor.a = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); borderRenderer.startColor = borderColor; borderRenderer.endColor = borderColor; }); } } float halfWidth = borderWidth * 0.5f; Vector3[] borderPoints = new Vector3[] { new Vector3(-halfWidth, -halfWidth, 0.0f), new Vector3(size.x + halfWidth, -halfWidth, 0.0f), new Vector3(size.x + halfWidth, size.y + halfWidth, 0.0f), new Vector3(-halfWidth, size.y + halfWidth, 0.0f) }; borderRenderer.SetPositions(borderPoints); } graphObject = new GameObject(); graphObject.name = Utility.ComposeObjectName(pageRoot.gameObject.name, this.GetType().Name, name, (int)(-depth / MASMonitor.depthDelta)); graphObject.layer = pageRoot.gameObject.layer; graphObject.transform.parent = pageRoot; graphObject.transform.position = pageRoot.position; graphObject.transform.Translate(monitor.screenSize.x * -0.5f + position.x, monitor.screenSize.y * 0.5f - position.y - size.y, depth); // add renderer stuff graphMaterial = new Material(MASLoader.shaders["MOARdV/Monitor"]); lineRenderer = graphObject.AddComponent <LineRenderer>(); lineRenderer.useWorldSpace = false; lineRenderer.material = graphMaterial; lineRenderer.startColor = sourceColor; lineRenderer.endColor = sourceColor; lineRenderer.startWidth = 2.5f; lineRenderer.endWidth = 2.5f; lineRenderer.positionCount = maxSamples; lineRenderer.loop = false; RenderPage(false); string positionString = string.Empty; if (!config.TryGetValue("position", ref positionString)) { throw new ArgumentException("Unable to find 'position' in LINE_GRAPH " + name); } else { string[] pos = Utility.SplitVariableList(positionString); if (pos.Length != 2) { throw new ArgumentException("Invalid number of values for 'position' in LINE_GRAPH " + name); } variableRegistrar.RegisterVariableChangeCallback(pos[0], (double newValue) => { position.x = (float)newValue; graphObject.transform.position = componentOrigin + new Vector3(position.x, -position.y, 0.0f); if (borderWidth > 0.0f) { borderObject.transform.position = componentOrigin + new Vector3(position.x, -position.y, 0.0f); } }); variableRegistrar.RegisterVariableChangeCallback(pos[1], (double newValue) => { position.y = (float)newValue; graphObject.transform.position = componentOrigin + new Vector3(position.x, -position.y, 0.0f); if (borderWidth > 0.0f) { borderObject.transform.position = componentOrigin + new Vector3(position.x, -position.y, 0.0f); } }); } for (int i = 0; i < maxSamples; ++i) { graphPoints[i] = Vector3.zero; } string sourceColorName = string.Empty; if (config.TryGetValue("sourceColor", ref sourceColorName)) { Color32 namedColor; if (comp.TryGetNamedColor(sourceColorName, out namedColor)) { sourceColor = namedColor; lineRenderer.startColor = sourceColor; lineRenderer.endColor = sourceColor; } else { string[] sourceColors = Utility.SplitVariableList(sourceColorName); if (sourceColors.Length < 3 || sourceColors.Length > 4) { throw new ArgumentException("sourceColor does not contain 3 or 4 values in LINE_GRAPH " + name); } variableRegistrar.RegisterVariableChangeCallback(sourceColors[0], (double newValue) => { sourceColor.r = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = sourceColor; lineRenderer.endColor = sourceColor; }); variableRegistrar.RegisterVariableChangeCallback(sourceColors[1], (double newValue) => { sourceColor.g = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = sourceColor; lineRenderer.endColor = sourceColor; }); variableRegistrar.RegisterVariableChangeCallback(sourceColors[2], (double newValue) => { sourceColor.b = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = sourceColor; lineRenderer.endColor = sourceColor; }); if (sourceColors.Length == 4) { variableRegistrar.RegisterVariableChangeCallback(sourceColors[3], (double newValue) => { sourceColor.a = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = sourceColor; lineRenderer.endColor = sourceColor; }); } } } currentSample = 0; comp.StartCoroutine(SampleData()); if (!string.IsNullOrEmpty(variableName)) { // Disable the mesh if we're in variable mode if (borderObject != null) { borderObject.SetActive(false); } graphObject.SetActive(false); variableRegistrar.RegisterVariableChangeCallback(variableName, VariableCallback); } else { currentState = true; if (borderObject != null) { borderObject.SetActive(true); } graphObject.SetActive(true); } }
internal MASPageLineString(ConfigNode config, InternalProp prop, MASFlightComputer comp, MASMonitor monitor, Transform pageRoot, float depth) : base(config, prop, comp) { string startColorString = string.Empty; if (!config.TryGetValue("startColor", ref startColorString)) { throw new ArgumentException("Unable to find 'startColor' in LINE_STRING " + name); } string endColorString = string.Empty; if (!config.TryGetValue("endColor", ref endColorString)) { endColorString = string.Empty; } string startWidthString = string.Empty; if (!config.TryGetValue("startWidth", ref startWidthString)) { throw new ArgumentException("Unable to find 'startWidth' in LINE_STRING " + name); } string endWidthString = string.Empty; if (!config.TryGetValue("endWidth", ref endWidthString)) { endWidthString = string.Empty; } string[] vertexStrings = config.GetValues("vertex"); if (vertexStrings.Length < 2) { throw new ArgumentException("Insufficient number of 'vertex' entries in LINE_STRING " + name + " (must have at least 2)"); } Vector2 position = Vector2.zero; if (!config.TryGetValue("position", ref position)) { throw new ArgumentException("Unable to find 'position' in LINE_STRING " + name); } string variableName = string.Empty; if (config.TryGetValue("variable", ref variableName)) { variableName = variableName.Trim(); } string rotationVariableName = string.Empty; config.TryGetValue("rotation", ref rotationVariableName); bool loop = false; config.TryGetValue("loop", ref loop); lineOrigin = new GameObject(); lineOrigin.name = Utility.ComposeObjectName(pageRoot.gameObject.name, this.GetType().Name, name, (int)(-depth / MASMonitor.depthDelta)); lineOrigin.layer = pageRoot.gameObject.layer; lineOrigin.transform.parent = pageRoot; lineOrigin.transform.position = pageRoot.position; lineOrigin.transform.Translate(monitor.screenSize.x * -0.5f + position.x, monitor.screenSize.y * 0.5f - position.y, depth); // add renderer stuff lineMaterial = new Material(MASLoader.shaders["MOARdV/Monitor"]); lineRenderer = lineOrigin.AddComponent <LineRenderer>(); lineRenderer.useWorldSpace = false; lineRenderer.material = lineMaterial; lineRenderer.startColor = startColor; lineRenderer.endColor = endColor; lineRenderer.startWidth = startWidth; lineRenderer.endWidth = endWidth; lineRenderer.loop = loop; int numVertices = vertexStrings.Length; lineRenderer.positionCount = numVertices; vertices = new Vector3[numVertices]; string textureName = string.Empty; if (config.TryGetValue("texture", ref textureName)) { Texture tex = GameDatabase.Instance.GetTexture(textureName, false); if (tex != null) { lineMaterial.mainTexture = tex; inverseTextureWidth = 1.0f / (float)tex.width; usesTexture = true; } } for (int i = 0; i < numVertices; ++i) { // Need to make a copy of the value for the lambda capture, // otherwise we'll try using i = numVertices in the callbacks. int index = i; vertices[i] = Vector3.zero; string[] vtx = Utility.SplitVariableList(vertexStrings[i]); if (vtx.Length != 2) { throw new ArgumentException("vertex " + (i + 1).ToString() + " does not contain two value in LINE_STRING " + name); } Action <double> vertexX = (double newValue) => { vertices[index].x = (float)newValue; lineRenderer.SetPosition(index, vertices[index]); if (usesTexture) { RecalculateTextureScale(); } }; variableRegistrar.RegisterVariableChangeCallback(vtx[0], vertexX); Action <double> vertexY = (double newValue) => { // Invert the value, since we stipulate +y is down on the monitor. vertices[index].y = -(float)newValue; lineRenderer.SetPosition(index, vertices[index]); if (usesTexture) { RecalculateTextureScale(); } }; variableRegistrar.RegisterVariableChangeCallback(vtx[1], vertexY); } lineRenderer.SetPositions(vertices); RenderPage(false); if (!string.IsNullOrEmpty(variableName)) { // Disable the lines if we're in variable mode lineOrigin.SetActive(false); variableRegistrar.RegisterVariableChangeCallback(variableName, VariableCallback); } else { lineOrigin.SetActive(true); } if (!string.IsNullOrEmpty(rotationVariableName)) { variableRegistrar.RegisterVariableChangeCallback(rotationVariableName, RotationCallback); } if (string.IsNullOrEmpty(endColorString)) { Color32 col; if (comp.TryGetNamedColor(startColorString, out col)) { startColor = col; endColor = col; } else { string[] startColors = Utility.SplitVariableList(startColorString); if (startColors.Length < 3 || startColors.Length > 4) { throw new ArgumentException("startColor does not contain 3 or 4 values in LINE_STRING " + name); } Action <double> startColorR = (double newValue) => { startColor.r = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = startColor; lineRenderer.endColor = startColor; }; variableRegistrar.RegisterVariableChangeCallback(startColors[0], startColorR); Action <double> startColorG = (double newValue) => { startColor.g = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = startColor; lineRenderer.endColor = startColor; }; variableRegistrar.RegisterVariableChangeCallback(startColors[1], startColorG); Action <double> startColorB = (double newValue) => { startColor.b = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = startColor; lineRenderer.endColor = startColor; }; variableRegistrar.RegisterVariableChangeCallback(startColors[2], startColorB); if (startColors.Length == 4) { Action <double> startColorA = (double newValue) => { startColor.a = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = startColor; lineRenderer.endColor = startColor; }; variableRegistrar.RegisterVariableChangeCallback(startColors[3], startColorA); } } lineRenderer.startColor = startColor; lineRenderer.endColor = startColor; } else { Color32 col; if (comp.TryGetNamedColor(startColorString, out col)) { startColor = col; } else { string[] startColors = Utility.SplitVariableList(startColorString); if (startColors.Length < 3 || startColors.Length > 4) { throw new ArgumentException("startColor does not contain 3 or 4 values in LINE_STRING " + name); } Action <double> startColorR = (double newValue) => { startColor.r = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = startColor; }; variableRegistrar.RegisterVariableChangeCallback(startColors[0], startColorR); Action <double> startColorG = (double newValue) => { startColor.g = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = startColor; }; variableRegistrar.RegisterVariableChangeCallback(startColors[1], startColorG); Action <double> startColorB = (double newValue) => { startColor.b = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = startColor; }; variableRegistrar.RegisterVariableChangeCallback(startColors[2], startColorB); if (startColors.Length == 4) { Action <double> startColorA = (double newValue) => { startColor.a = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = startColor; }; variableRegistrar.RegisterVariableChangeCallback(startColors[3], startColorA); } } if (comp.TryGetNamedColor(endColorString, out col)) { endColor = col; } else { string[] endColors = Utility.SplitVariableList(endColorString); if (endColors.Length < 3 || endColors.Length > 4) { throw new ArgumentException("endColor does not contain 3 or 4 values in LINE_STRING " + name); } Action <double> endColorR = (double newValue) => { endColor.r = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.endColor = endColor; }; variableRegistrar.RegisterVariableChangeCallback(endColors[0], endColorR); Action <double> endColorG = (double newValue) => { endColor.g = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.endColor = endColor; }; variableRegistrar.RegisterVariableChangeCallback(endColors[1], endColorG); Action <double> endColorB = (double newValue) => { endColor.b = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.endColor = endColor; }; variableRegistrar.RegisterVariableChangeCallback(endColors[2], endColorB); if (endColors.Length == 4) { Action <double> endColorA = (double newValue) => { endColor.a = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.endColor = endColor; }; variableRegistrar.RegisterVariableChangeCallback(endColors[3], endColorA); } } lineRenderer.startColor = startColor; lineRenderer.endColor = endColor; } if (string.IsNullOrEmpty(endWidthString)) { // Monowidth line Action <double> startWidthAction = (double newValue) => { startWidth = (float)newValue; lineRenderer.startWidth = startWidth; lineRenderer.endWidth = startWidth; }; variableRegistrar.RegisterVariableChangeCallback(startWidthString, startWidthAction); } else { Action <double> startWidthAction = (double newValue) => { startWidth = (float)newValue; lineRenderer.startWidth = startWidth; }; variableRegistrar.RegisterVariableChangeCallback(startWidthString, startWidthAction); Action <double> endWidthAction = (double newValue) => { endWidth = (float)newValue; lineRenderer.endWidth = endWidth; }; variableRegistrar.RegisterVariableChangeCallback(endWidthString, endWidthAction); } }
internal MASComponentColorShift(ConfigNode config, InternalProp prop, MASFlightComputer comp) : base(config, prop, comp) { string transform = string.Empty; if (!config.TryGetValue("transform", ref transform)) { throw new ArgumentException("Missing 'transform' in COLOR_SHIFT " + name); } string[] transforms = transform.Split(','); string colorName = "_EmissiveColor"; config.TryGetValue("colorName", ref colorName); colorIndex = Shader.PropertyToID(colorName.Trim()); localMaterial = new Material[transforms.Length]; for (int i = transforms.Length - 1; i >= 0; --i) { try { Transform t = prop.FindModelTransform(transforms[i].Trim()); Renderer r = t.GetComponent <Renderer>(); localMaterial[i] = r.material; } catch (Exception e) { Utility.LogError(this, "Can't find transform {0} in COLOR_SHIFT {1}", transforms[i].Trim(), name); throw e; } } // activeColor, passiveColor string passiveColorStr = string.Empty; if (!config.TryGetValue("passiveColor", ref passiveColorStr)) { throw new ArgumentException("Invalid or missing 'passiveColor' in COLOR_SHIFT " + name); } string variableName = string.Empty; if (config.TryGetValue("variable", ref variableName)) { variableName = variableName.Trim(); } blend = false; config.TryGetValue("blend", ref blend); if (blend == false && config.TryGetValue("flashRate", ref flashRate) && flashRate > 0.0f) { useFlash = true; comp.RegisterFlashCallback(flashRate, FlashToggle); } Color32 namedColor; if (comp.TryGetNamedColor(passiveColorStr, out namedColor)) { passiveColor = namedColor; } else { string[] startColors = Utility.SplitVariableList(passiveColorStr); if (startColors.Length < 3 || startColors.Length > 4) { throw new ArgumentException("passiveColor does not contain 3 or 4 values in COLOR_SHIFT " + name); } variableRegistrar.RegisterVariableChangeCallback(startColors[0], (double newValue) => { passiveColor.r = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); if (blend) { UpdateBlendColor(); } else { UpdateBooleanColor(); } }); variableRegistrar.RegisterVariableChangeCallback(startColors[1], (double newValue) => { passiveColor.g = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); if (blend) { UpdateBlendColor(); } else { UpdateBooleanColor(); } }); variableRegistrar.RegisterVariableChangeCallback(startColors[2], (double newValue) => { passiveColor.b = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); if (blend) { UpdateBlendColor(); } else { UpdateBooleanColor(); } }); if (startColors.Length == 4) { variableRegistrar.RegisterVariableChangeCallback(startColors[3], (double newValue) => { passiveColor.a = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); if (blend) { UpdateBlendColor(); } else { UpdateBooleanColor(); } }); } } // Final validations if (blend || useFlash || !string.IsNullOrEmpty(variableName)) { string activeColorStr = string.Empty; if (string.IsNullOrEmpty(variableName)) { throw new ArgumentException("Invalid or missing 'variable' in COLOR_SHIFT " + name); } else if (config.TryGetValue("activeColor", ref activeColorStr)) { if (comp.TryGetNamedColor(activeColorStr, out namedColor)) { activeColor = namedColor; } else { string[] startColors = Utility.SplitVariableList(activeColorStr); if (startColors.Length < 3 || startColors.Length > 4) { throw new ArgumentException("activeColor does not contain 3 or 4 values in COLOR_SHIFT " + name); } variableRegistrar.RegisterVariableChangeCallback(startColors[0], (double newValue) => { activeColor.r = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); if (blend) { UpdateBlendColor(); } else { UpdateBooleanColor(); } }); variableRegistrar.RegisterVariableChangeCallback(startColors[1], (double newValue) => { activeColor.g = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); if (blend) { UpdateBlendColor(); } else { UpdateBooleanColor(); } }); variableRegistrar.RegisterVariableChangeCallback(startColors[2], (double newValue) => { activeColor.b = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); if (blend) { UpdateBlendColor(); } else { UpdateBooleanColor(); } }); if (startColors.Length == 4) { variableRegistrar.RegisterVariableChangeCallback(startColors[3], (double newValue) => { activeColor.a = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); if (blend) { UpdateBlendColor(); } else { UpdateBooleanColor(); } }); } } } else { throw new ArgumentException("Invalid or missing 'activeColor' in COLOR_SHIFT " + name); } } // Make everything a known value before the callback fires. for (int i = localMaterial.Length - 1; i >= 0; --i) { localMaterial[i].SetColor(colorIndex, passiveColor); } if (!string.IsNullOrEmpty(variableName)) { variableRegistrar.RegisterVariableChangeCallback(variableName, VariableCallback); } }
internal MASPageImage(ConfigNode config, InternalProp prop, MASFlightComputer comp, MASMonitor monitor, Transform pageRoot, float depth) : base(config, prop, comp) { string textureName = string.Empty; if (!config.TryGetValue("texture", ref textureName)) { throw new ArgumentException("Unable to find 'texture' in IMAGE " + name); } Texture2D mainTexture = null; if (textureName == "%MAP_ICON%") { mainTexture = MASLoader.OrbitIconsAtlas(); } else if (textureName == "%NAVBALL_ICON%") { mainTexture = GameDatabase.Instance.GetTexture("Squad/Props/IVANavBall/ManeuverNode_vectors", false); } else { if (textureName == "%FLAG%") { textureName = prop.part.flagURL; } mainTexture = GameDatabase.Instance.GetTexture(textureName, false); } if (mainTexture == null) { throw new ArgumentException("Unable to find 'texture' " + textureName + " for IMAGE " + name); } bool wrap = true; if (config.TryGetValue("wrap", ref wrap)) { mainTexture.wrapMode = (wrap) ? TextureWrapMode.Repeat : TextureWrapMode.Clamp; } string variableName = string.Empty; if (config.TryGetValue("variable", ref variableName)) { variableName = variableName.Trim(); } string rotationVariableName = string.Empty; if (config.TryGetValue("rotation", ref rotationVariableName)) { config.TryGetValue("rotationOffset", ref rotationOffset); } // Need Mesh for UpdateVertices. mesh = new Mesh(); string sizeString = string.Empty; if (!config.TryGetValue("size", ref sizeString)) { size = new Vector2(mainTexture.width, mainTexture.height); UpdateVertices(); } else { string[] sizes = Utility.SplitVariableList(sizeString); if (sizes.Length != 2) { throw new ArgumentException("Invalid number of values for 'size' in IMAGE " + name); } variableRegistrar.RegisterVariableChangeCallback(sizes[0], (double newValue) => { size.x = (float)newValue; UpdateVertices(); }); variableRegistrar.RegisterVariableChangeCallback(sizes[1], (double newValue) => { size.y = (float)newValue; UpdateVertices(); }); } imageOrigin = pageRoot.position + new Vector3(monitor.screenSize.x * -0.5f, monitor.screenSize.y * 0.5f, depth); // Need imageObject for UpdatePosition. imageObject = new GameObject(); imageObject.transform.parent = pageRoot; string positionString = string.Empty; if (!config.TryGetValue("position", ref positionString)) { position = Vector2.zero; imageObject.transform.position = imageOrigin + new Vector3(position.x + rotationOffset.x + size.x * 0.5f, -(position.y + rotationOffset.y + size.y * 0.5f), 0.0f); } else { string[] pos = Utility.SplitVariableList(positionString); if (pos.Length != 2) { throw new ArgumentException("Invalid number of values for 'position' in IMAGE " + name); } variableRegistrar.RegisterVariableChangeCallback(pos[0], (double newValue) => { position.x = (float)newValue; imageObject.transform.position = imageOrigin + new Vector3(position.x + rotationOffset.x + size.x * 0.5f, -(position.y + rotationOffset.y + size.y * 0.5f), 0.0f); }); variableRegistrar.RegisterVariableChangeCallback(pos[1], (double newValue) => { position.y = (float)newValue; imageObject.transform.position = imageOrigin + new Vector3(position.x + rotationOffset.x + size.x * 0.5f, -(position.y + rotationOffset.y + size.y * 0.5f), 0.0f); }); } // Set up our surface. imageObject.name = Utility.ComposeObjectName(pageRoot.gameObject.name, this.GetType().Name, name, (int)(-depth / MASMonitor.depthDelta)); imageObject.layer = pageRoot.gameObject.layer; // add renderer stuff MeshFilter meshFilter = imageObject.AddComponent <MeshFilter>(); meshRenderer = imageObject.AddComponent <MeshRenderer>(); mesh.uv = new[] { new Vector2(0.0f, 1.0f), Vector2.one, Vector2.zero, new Vector2(1.0f, 0.0f), }; mesh.triangles = new[] { 0, 1, 2, 1, 3, 2 }; mesh.RecalculateBounds(); mesh.UploadMeshData(false); meshFilter.mesh = mesh; imageMaterial = new Material(MASLoader.shaders["MOARdV/Monitor"]); imageMaterial.mainTexture = mainTexture; meshRenderer.material = imageMaterial; RenderPage(false); currentBlend = 0.0f; passiveColor = Color.white; activeColor = Color.white; string passiveColorName = string.Empty; if (config.TryGetValue("passiveColor", ref passiveColorName)) { Color32 color32; if (comp.TryGetNamedColor(passiveColorName, out color32)) { passiveColor = color32; } else { string[] startColors = Utility.SplitVariableList(passiveColorName); if (startColors.Length < 3 || startColors.Length > 4) { throw new ArgumentException("'passiveColor' does not contain 3 or 4 values in IMAGE " + name); } variableRegistrar.RegisterVariableChangeCallback(startColors[0], (double newValue) => { passiveColor.r = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); UpdateColor(); }); variableRegistrar.RegisterVariableChangeCallback(startColors[1], (double newValue) => { passiveColor.g = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); UpdateColor(); }); variableRegistrar.RegisterVariableChangeCallback(startColors[2], (double newValue) => { passiveColor.b = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); UpdateColor(); }); if (startColors.Length == 4) { variableRegistrar.RegisterVariableChangeCallback(startColors[3], (double newValue) => { passiveColor.a = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); UpdateColor(); }); } } } string colorVariableName = string.Empty; if (config.TryGetValue("colorVariable", ref colorVariableName)) { if (string.IsNullOrEmpty(passiveColorName)) { throw new ArgumentException("'colorVariable' found, but no 'passiveColor' in IMAGE " + name); } string activeColorName = string.Empty; if (!config.TryGetValue("activeColor", ref activeColorName)) { throw new ArgumentException("'colorVariable' found, but no 'activeColor' in IMAGE " + name); } string colorRangeString = string.Empty; if (config.TryGetValue("colorRange", ref colorRangeString)) { string[] colorRanges = Utility.SplitVariableList(colorRangeString); if (colorRanges.Length != 2) { throw new ArgumentException("Expected 2 values for 'colorRange' in IMAGE " + name); } variableRegistrar.RegisterVariableChangeCallback(colorRanges[0], (double newValue) => colorRange1 = (float)newValue); variableRegistrar.RegisterVariableChangeCallback(colorRanges[1], (double newValue) => colorRange2 = (float)newValue); bool colorBlend = false; if (config.TryGetValue("colorBlend", ref colorBlend) && colorBlend == true) { variableRegistrar.RegisterVariableChangeCallback(colorVariableName, (double newValue) => { float newBlend = Mathf.InverseLerp(colorRange1, colorRange2, (float)newValue); if (!Mathf.Approximately(newBlend, currentBlend)) { currentBlend = newBlend; UpdateColor(); } }); } else { variableRegistrar.RegisterVariableChangeCallback(colorVariableName, (double newValue) => { float newBlend = (newValue.Between(colorRange1, colorRange2)) ? 1.0f : 0.0f; if (newBlend != currentBlend) { currentBlend = newBlend; UpdateColor(); } }); } } else { variableRegistrar.RegisterVariableChangeCallback(colorVariableName, (double newValue) => { float newBlend = (newValue > 0.0) ? 1.0f : 0.0f; if (newBlend != currentBlend) { currentBlend = newBlend; UpdateColor(); } }); } Color32 color32; if (comp.TryGetNamedColor(activeColorName, out color32)) { activeColor = color32; } else { string[] activeColors = Utility.SplitVariableList(activeColorName); if (activeColors.Length < 3 || activeColors.Length > 4) { throw new ArgumentException("'activeColor' does not contain 3 or 4 values in IMAGE " + name); } variableRegistrar.RegisterVariableChangeCallback(activeColors[0], (double newValue) => { activeColor.r = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); UpdateColor(); }); variableRegistrar.RegisterVariableChangeCallback(activeColors[1], (double newValue) => { activeColor.g = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); UpdateColor(); }); variableRegistrar.RegisterVariableChangeCallback(activeColors[2], (double newValue) => { activeColor.b = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); UpdateColor(); }); if (activeColors.Length == 4) { variableRegistrar.RegisterVariableChangeCallback(activeColors[3], (double newValue) => { activeColor.a = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); UpdateColor(); }); } } } // In case fixed colors are being used. UpdateColor(); string uvTilingString = string.Empty; if (config.TryGetValue("tiling", ref uvTilingString)) { string[] uvTile = Utility.SplitVariableList(uvTilingString); if (uvTile.Length != 2) { throw new ArgumentException("'tiling' does not contain 2 values in IMAGE " + name); } variableRegistrar.RegisterVariableChangeCallback(uvTile[0], (double newValue) => { float rescale = Mathf.Max((float)newValue, 0.0f); if (!Mathf.Approximately(rescale, uvScale.x)) { uvScale.x = rescale; imageMaterial.SetTextureScale("_MainTex", uvScale); } }); variableRegistrar.RegisterVariableChangeCallback(uvTile[1], (double newValue) => { float rescale = Mathf.Max((float)newValue, 0.0f); if (!Mathf.Approximately(rescale, uvScale.y)) { uvScale.y = rescale; imageMaterial.SetTextureScale("_MainTex", uvScale); } }); } string uvShiftString = string.Empty; if (config.TryGetValue("uvShift", ref uvShiftString)) { string[] uvShifting = Utility.SplitVariableList(uvShiftString); if (uvShifting.Length != 2) { throw new ArgumentException("'uvShift' does not contain 2 values in IMAGE " + name); } variableRegistrar.RegisterVariableChangeCallback(uvShifting[0], (double newValue) => { float reshift = (float)newValue; uvShift.x = reshift; imageMaterial.SetTextureOffset("_MainTex", uvShift); }); variableRegistrar.RegisterVariableChangeCallback(uvShifting[1], (double newValue) => { float reshift = (float)newValue; uvShift.y = reshift; imageMaterial.SetTextureOffset("_MainTex", uvShift); }); } if (!string.IsNullOrEmpty(variableName)) { // Disable the mesh if we're in variable mode imageObject.SetActive(false); variableRegistrar.RegisterVariableChangeCallback(variableName, VariableCallback); } else { imageObject.SetActive(true); } if (!string.IsNullOrEmpty(rotationVariableName)) { variableRegistrar.RegisterVariableChangeCallback(rotationVariableName, RotationCallback); } }
internal MASPagePolygon(ConfigNode config, InternalProp prop, MASFlightComputer comp, MASMonitor monitor, Transform pageRoot, float depth) : base(config, prop, comp) { string colorString = string.Empty; if (!config.TryGetValue("color", ref colorString)) { throw new ArgumentException("Unable to find 'color' in POLYGON " + name); } string[] vertexStrings = config.GetValues("vertex"); if (vertexStrings.Length < 3) { throw new ArgumentException("Insufficient number of 'vertex' entries in POLYGON " + name + " (must have at least 3)"); } string variableName = string.Empty; if (config.TryGetValue("variable", ref variableName)) { variableName = variableName.Trim(); } polygonOrigin = new GameObject(); polygonOrigin.name = Utility.ComposeObjectName(pageRoot.gameObject.name, this.GetType().Name, name, (int)(-depth / MASMonitor.depthDelta)); polygonOrigin.layer = pageRoot.gameObject.layer; polygonOrigin.transform.parent = pageRoot; polygonOrigin.transform.position = pageRoot.position; polygonOrigin.transform.Translate(monitor.screenSize.x * -0.5f + position.x, monitor.screenSize.y * 0.5f - position.y, depth); origin = pageRoot.position + new Vector3(monitor.screenSize.x * -0.5f, monitor.screenSize.y * 0.5f, depth); string positionString = string.Empty; if (!config.TryGetValue("position", ref positionString)) { position = Vector2.zero; throw new ArgumentException("Unable to find 'position' in POLYGON " + name); } else { string[] pos = Utility.SplitVariableList(positionString); if (pos.Length != 2) { throw new ArgumentException("Invalid number of values for 'position' in POLYGON " + name); } variableRegistrar.RegisterVariableChangeCallback(pos[0], (double newValue) => { position.x = (float)newValue; polygonOrigin.transform.position = origin + new Vector3(position.x, -position.y, 0.0f); }); variableRegistrar.RegisterVariableChangeCallback(pos[1], (double newValue) => { position.y = (float)newValue; polygonOrigin.transform.position = origin + new Vector3(position.x, -position.y, 0.0f); }); } // add renderer stuff MeshFilter meshFilter = polygonOrigin.AddComponent <MeshFilter>(); meshRenderer = polygonOrigin.AddComponent <MeshRenderer>(); mesh = new Mesh(); meshFilter.mesh = mesh; int numVertices = vertexStrings.Length; vertices = new Vector3[numVertices]; polygonMaterial = new Material(MASLoader.shaders["MOARdV/Monitor"]); polygonMaterial.color = color; meshRenderer.material = polygonMaterial; for (int i = 0; i < numVertices; ++i) { // Need to make a copy of the value for the lambda capture, // otherwise we'll try using i = numVertices in the callbacks. int index = i; vertices[i] = Vector3.zero; string[] vtx = Utility.SplitVariableList(vertexStrings[i]); if (vtx.Length != 2) { throw new ArgumentException("vertex " + (i + 1).ToString() + " does not contain two values in POLYGON " + name); } variableRegistrar.RegisterVariableChangeCallback(vtx[0], (double newValue) => { vertices[index].x = (float)newValue; retriangulate = true; }); variableRegistrar.RegisterVariableChangeCallback(vtx[1], (double newValue) => { // Invert the value, since we stipulate +y is down on the monitor. vertices[index].y = -(float)newValue; retriangulate = true; }); } mesh.vertices = vertices; // For 3 or 4 vertices, the index array is invariant. Load it now and be done with it. if (numVertices == 3) { mesh.triangles = new[] { 0, 2, 1 }; } else if (numVertices == 4) { mesh.triangles = new[] { 0, 2, 1, 0, 3, 2 }; } RenderPage(false); string rotationVariableName = string.Empty; if (config.TryGetValue("rotation", ref rotationVariableName)) { variableRegistrar.RegisterVariableChangeCallback(rotationVariableName, RotationCallback); } if (!string.IsNullOrEmpty(variableName)) { // Disable the mesh if we're in variable mode polygonOrigin.SetActive(false); variableRegistrar.RegisterVariableChangeCallback(variableName, VariableCallback); } else { currentState = true; polygonOrigin.SetActive(true); } Color32 col; if (comp.TryGetNamedColor(colorString, out col)) { color = col; polygonMaterial.color = color; } else { string[] colors = Utility.SplitVariableList(colorString); if (colors.Length < 3 || colors.Length > 4) { throw new ArgumentException("color does not contain 3 or 4 values in POLYGON " + name); } variableRegistrar.RegisterVariableChangeCallback(colors[0], (double newValue) => { color.r = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); polygonMaterial.color = color; }); variableRegistrar.RegisterVariableChangeCallback(colors[1], (double newValue) => { color.g = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); polygonMaterial.color = color; }); variableRegistrar.RegisterVariableChangeCallback(colors[2], (double newValue) => { color.b = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); polygonMaterial.color = color; }); if (colors.Length == 4) { variableRegistrar.RegisterVariableChangeCallback(colors[3], (double newValue) => { color.a = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); polygonMaterial.color = color; }); } } }
internal MASPageEllipse(ConfigNode config, InternalProp prop, MASFlightComputer comp, MASMonitor monitor, Transform pageRoot, float depth) : base(config, prop, comp) { string startColorString = string.Empty; if (!config.TryGetValue("startColor", ref startColorString)) { throw new ArgumentException("Unable to find 'startColor' in ELLIPSE " + name); } string endColorString = string.Empty; if (!config.TryGetValue("endColor", ref endColorString)) { endColorString = string.Empty; } string startWidthString = string.Empty; if (!config.TryGetValue("startWidth", ref startWidthString)) { throw new ArgumentException("Unable to find 'startWidth' in ELLIPSE " + name); } string endWidthString = string.Empty; if (!config.TryGetValue("endWidth", ref endWidthString)) { endWidthString = string.Empty; } numVertices = 0; if (!config.TryGetValue("vertexCount", ref numVertices)) { throw new ArgumentException("Unable to find 'vertexCount' in ELLIPSE " + name); } else if (numVertices < 3) { throw new ArgumentException("'vertexCount' must be at least 3 in ELLIPSE " + name); } string variableName = string.Empty; if (config.TryGetValue("variable", ref variableName)) { variableName = variableName.Trim(); } string rotationVariableName = string.Empty; config.TryGetValue("rotation", ref rotationVariableName); lineOrigin = new GameObject(); lineOrigin.name = Utility.ComposeObjectName(pageRoot.gameObject.name, this.GetType().Name, name, (int)(-depth / MASMonitor.depthDelta)); lineOrigin.layer = pageRoot.gameObject.layer; lineOrigin.transform.parent = pageRoot; lineOrigin.transform.position = pageRoot.position; lineOrigin.transform.Translate(monitor.screenSize.x * -0.5f + position.x, monitor.screenSize.y * 0.5f - position.y, depth); ellipseOrigin = pageRoot.position + new Vector3(monitor.screenSize.x * -0.5f, monitor.screenSize.y * 0.5f, depth); string positionString = string.Empty; if (!config.TryGetValue("position", ref positionString)) { position = Vector2.zero; throw new ArgumentException("Unable to find 'position' in ELLIPSE " + name); } else { string[] pos = Utility.SplitVariableList(positionString); if (pos.Length != 2) { throw new ArgumentException("Invalid number of values for 'position' in ELLIPSE " + name); } variableRegistrar.RegisterVariableChangeCallback(pos[0], (double newValue) => { position.x = (float)newValue; lineOrigin.transform.position = ellipseOrigin + new Vector3(position.x, -position.y, 0.0f); }); variableRegistrar.RegisterVariableChangeCallback(pos[1], (double newValue) => { position.y = (float)newValue; lineOrigin.transform.position = ellipseOrigin + new Vector3(position.x, -position.y, 0.0f); }); } // add renderer stuff lineMaterial = new Material(MASLoader.shaders["MOARdV/Monitor"]); lineRenderer = lineOrigin.AddComponent <LineRenderer>(); lineRenderer.useWorldSpace = false; lineRenderer.material = lineMaterial; lineRenderer.startColor = startColor; lineRenderer.endColor = endColor; lineRenderer.startWidth = startWidth; lineRenderer.endWidth = endWidth; ++numVertices; lineRenderer.positionCount = numVertices; vertices = new Vector3[numVertices]; string textureName = string.Empty; if (config.TryGetValue("texture", ref textureName)) { Texture tex = GameDatabase.Instance.GetTexture(textureName, false); if (tex != null) { lineMaterial.mainTexture = tex; inverseTextureWidth = 1.0f / (float)tex.width; usesTexture = true; } } string startAngleName = string.Empty; if (config.TryGetValue("startAngle", ref startAngleName)) { variableRegistrar.RegisterVariableChangeCallback(startAngleName, (double newValue) => { startAngle = (float)newValue; RecalculateVertices(); }); } else { startAngle = 0.0f; } string endAngleName = string.Empty; if (config.TryGetValue("endAngle", ref endAngleName)) { if (string.IsNullOrEmpty(startAngleName)) { throw new ArgumentException("Missing 'startAngle', but found 'endAngle' in ELLIPSE " + name); } else { variableRegistrar.RegisterVariableChangeCallback(endAngleName, (double newValue) => { endAngle = (float)newValue; RecalculateVertices(); }); } } else if (!string.IsNullOrEmpty(startAngleName)) { throw new ArgumentException("Found 'startAngle', but missing 'endAngle' in ELLIPSE " + name); } else { endAngle = 360.0f; } string radiusXName = string.Empty; if (!config.TryGetValue("radiusX", ref radiusXName)) { throw new ArgumentException("Unable to find 'radiusX' in ELLIPSE " + name); } string radiusYName = string.Empty; if (!config.TryGetValue("radiusY", ref radiusYName)) { Action <double> newRadius = (double newValue) => { radiusX = (float)newValue; radiusY = radiusX; RecalculateVertices(); }; variableRegistrar.RegisterVariableChangeCallback(radiusXName, newRadius); } else { Action <double> newRadiusX = (double newValue) => { radiusX = (float)newValue; RecalculateVertices(); }; variableRegistrar.RegisterVariableChangeCallback(radiusXName, newRadiusX); Action <double> newRadiusY = (double newValue) => { radiusY = (float)newValue; RecalculateVertices(); }; variableRegistrar.RegisterVariableChangeCallback(radiusYName, newRadiusY); } RenderPage(false); if (!string.IsNullOrEmpty(variableName)) { // Disable the lines if we're in variable mode lineOrigin.SetActive(false); variableRegistrar.RegisterVariableChangeCallback(variableName, VariableCallback); } else { lineOrigin.SetActive(true); } if (!string.IsNullOrEmpty(rotationVariableName)) { variableRegistrar.RegisterVariableChangeCallback(rotationVariableName, RotationCallback); } if (string.IsNullOrEmpty(endColorString)) { Color32 namedColor; if (comp.TryGetNamedColor(startColorString, out namedColor)) { startColor = namedColor; lineRenderer.startColor = startColor; lineRenderer.endColor = startColor; } else { string[] startColors = Utility.SplitVariableList(startColorString); if (startColors.Length < 3 || startColors.Length > 4) { throw new ArgumentException("startColor does not contain 3 or 4 values in ELLIPSE " + name); } Action <double> startColorR = (double newValue) => { startColor.r = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = startColor; lineRenderer.endColor = startColor; }; variableRegistrar.RegisterVariableChangeCallback(startColors[0], startColorR); Action <double> startColorG = (double newValue) => { startColor.g = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = startColor; lineRenderer.endColor = startColor; }; variableRegistrar.RegisterVariableChangeCallback(startColors[1], startColorG); Action <double> startColorB = (double newValue) => { startColor.b = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = startColor; lineRenderer.endColor = startColor; }; variableRegistrar.RegisterVariableChangeCallback(startColors[2], startColorB); if (startColors.Length == 4) { Action <double> startColorA = (double newValue) => { startColor.a = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = startColor; lineRenderer.endColor = startColor; }; variableRegistrar.RegisterVariableChangeCallback(startColors[3], startColorA); } } } else { Color32 namedColor; if (comp.TryGetNamedColor(startColorString, out namedColor)) { startColor = namedColor; lineRenderer.startColor = startColor; lineRenderer.endColor = endColor; } else { string[] startColors = Utility.SplitVariableList(startColorString); if (startColors.Length < 3 || startColors.Length > 4) { throw new ArgumentException("startColor does not contain 3 or 4 values in ELLIPSE " + name); } Action <double> startColorR = (double newValue) => { startColor.r = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = startColor; }; variableRegistrar.RegisterVariableChangeCallback(startColors[0], startColorR); Action <double> startColorG = (double newValue) => { startColor.g = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = startColor; }; variableRegistrar.RegisterVariableChangeCallback(startColors[1], startColorG); Action <double> startColorB = (double newValue) => { startColor.b = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = startColor; }; variableRegistrar.RegisterVariableChangeCallback(startColors[2], startColorB); if (startColors.Length == 4) { Action <double> startColorA = (double newValue) => { startColor.a = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = startColor; }; variableRegistrar.RegisterVariableChangeCallback(startColors[3], startColorA); } } if (comp.TryGetNamedColor(endColorString, out namedColor)) { endColor = namedColor; lineRenderer.startColor = startColor; lineRenderer.endColor = endColor; } else { string[] endColors = Utility.SplitVariableList(endColorString); if (endColors.Length < 3 || endColors.Length > 4) { throw new ArgumentException("endColor does not contain 3 or 4 values in ELLIPSE " + name); } Action <double> endColorR = (double newValue) => { endColor.r = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.endColor = endColor; }; variableRegistrar.RegisterVariableChangeCallback(endColors[0], endColorR); Action <double> endColorG = (double newValue) => { endColor.g = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.endColor = endColor; }; variableRegistrar.RegisterVariableChangeCallback(endColors[1], endColorG); Action <double> endColorB = (double newValue) => { endColor.b = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.endColor = endColor; }; variableRegistrar.RegisterVariableChangeCallback(endColors[2], endColorB); if (endColors.Length == 4) { Action <double> endColorA = (double newValue) => { endColor.a = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.endColor = endColor; }; variableRegistrar.RegisterVariableChangeCallback(endColors[3], endColorA); } } } if (string.IsNullOrEmpty(endWidthString)) { // Monowidth line Action <double> startWidthAction = (double newValue) => { startWidth = (float)newValue; lineRenderer.startWidth = startWidth; lineRenderer.endWidth = startWidth; }; variableRegistrar.RegisterVariableChangeCallback(startWidthString, startWidthAction); } else { Action <double> startWidthAction = (double newValue) => { startWidth = (float)newValue; lineRenderer.startWidth = startWidth; }; variableRegistrar.RegisterVariableChangeCallback(startWidthString, startWidthAction); Action <double> endWidthAction = (double newValue) => { endWidth = (float)newValue; lineRenderer.endWidth = endWidth; }; variableRegistrar.RegisterVariableChangeCallback(endWidthString, endWidthAction); } }
internal MASComponentInternalText(ConfigNode config, InternalProp prop, MASFlightComputer comp) : base(config, prop, comp) { string transform = string.Empty; if (!config.TryGetValue("transform", ref transform)) { throw new ArgumentException("Missing 'transform' in INTERNAL_TEXT " + name); } string text = string.Empty; if (!config.TryGetValue("text", ref text)) { throw new ArgumentException("Invalid or missing 'text' in INTERNAL_TEXT " + name); } Color passiveColor = Color.white; string passiveColorStr = string.Empty; if (!config.TryGetValue("passiveColor", ref passiveColorStr)) { throw new ArgumentException("Invalid or missing 'passiveColor' in INTERNAL_TEXT " + name); } else { Color32 namedColor; if (comp.TryGetNamedColor(passiveColorStr, out namedColor)) { passiveColor = namedColor; } else { string[] startColors = Utility.SplitVariableList(passiveColorStr); if (startColors.Length < 3 || startColors.Length > 4) { throw new ArgumentException("passiveColor does not contain 3 or 4 values in INTERNAL_TEXT " + name); } float x; if (!float.TryParse(startColors[0], out x)) { throw new ArgumentException("Unable to parse passiveColor red value in INTERNAL_TEXT " + name); } passiveColor.r = Mathf.Clamp01(x * (1.0f / 255.0f)); if (!float.TryParse(startColors[1], out x)) { throw new ArgumentException("Unable to parse passiveColor green value in INTERNAL_TEXT " + name); } passiveColor.g = Mathf.Clamp01(x * (1.0f / 255.0f)); if (!float.TryParse(startColors[2], out x)) { throw new ArgumentException("Unable to parse passiveColor blue value in INTERNAL_TEXT " + name); } passiveColor.b = Mathf.Clamp01(x * (1.0f / 255.0f)); if (startColors.Length == 4) { if (!float.TryParse(startColors[3], out x)) { throw new ArgumentException("Unable to parse passiveColor alpha value in INTERNAL_TEXT " + name); } passiveColor.a = Mathf.Clamp01(x * (1.0f / 255.0f)); } } } text = MdVTextMesh.UnmangleText(text); // See if this is a single-line text, or multi-line. The latter is not supported here. string[] textRows = text.Split(Utility.LineSeparator, StringSplitOptions.RemoveEmptyEntries); if (textRows.Length > 1) { throw new ArgumentException("Multi-line text is not supported in INTERNAL_TEXT " + name); } text = textRows[0]; bool mutable = false; if (text.Contains(MdVTextMesh.VariableListSeparator[0]) || text.Contains(MdVTextMesh.VariableListSeparator[1])) { mutable = true; } Transform textObjTransform = prop.FindModelTransform(transform); // TODO: Allow variable fontSize? float fontSize = 0.15f; textObj = InternalComponents.Instance.CreateText("Arial", fontSize, textObjTransform, (mutable) ? "-" : text, passiveColor, false, "TopLeft"); try { Transform q = textObj.text.transform; q.Translate(0.0f, 0.0048f, 0.0f); } catch (Exception) { } if (mutable) { string[] sections = text.Split(MdVTextMesh.VariableListSeparator, StringSplitOptions.RemoveEmptyEntries); if (sections.Length != 2) { throw new ArgumentException("Error parsing text in INTERNAL_TEXT " + name); } MdVTextMesh.TextRow tr = new MdVTextMesh.TextRow(); tr.formatString = sections[0]; // See if this text contains formatted rich text nudges if (tr.formatString.Contains("[")) { throw new ArgumentException("Formatted rich text is not supported in INTERNAL_TEXT " + name); } string[] variables = sections[1].Split(';'); tr.variable = new Variable[variables.Length]; tr.evals = new object[variables.Length]; tr.callback = (double dontCare) => { tr.rowInvalidated = true; }; for (int var = 0; var < tr.variable.Length; ++var) { try { tr.variable[var] = variableRegistrar.RegisterVariableChangeCallback(variables[var], tr.callback); } catch (Exception e) { Utility.LogError(this, "Variable {0} threw an exception", variables[var]); throw e; } } tr.rowInvalidated = true; tr.EvaluateVariables(); textObj.text.text = tr.formattedData; textRow = tr; coroutineEnabled = true; comp.StartCoroutine(StringUpdateCoroutine()); } }
internal MASComponentIntLight(ConfigNode config, InternalProp prop, MASFlightComputer comp) : base(config, prop, comp) { string lightName = string.Empty; string[] lightTransforms = null; if (config.TryGetValue("lightName", ref lightName)) { lightTransforms = lightName.Split(','); for (int i = 0; i < lightTransforms.Length; ++i) { lightTransforms[i] = lightTransforms[i].Trim(); } } Light[] availableLights = prop.part.internalModel.FindModelComponents <Light>(); if (availableLights != null && availableLights.Length > 0) { List <Light> lights = new List <Light>(availableLights); if (lightTransforms != null) { for (int i = lights.Count - 1; i >= 0; --i) { if (Array.FindIndex(lightTransforms, x => x == lights[i].name) == -1) { lights.RemoveAt(i); } } } if (lights.Count > 0) { controlledLights = lights.ToArray(); } } if (controlledLights == null) { Utility.LogError(this, "No named lights for INT_LIGHT {0} found in internalModel '{1}'", name, prop.part.internalModel.internalName); return; } string variableName = string.Empty; if (!config.TryGetValue("variable", ref variableName) || string.IsNullOrEmpty(variableName)) { throw new ArgumentException("Invalid or missing 'variable' in INT_LIGHT " + name); } string colorString = string.Empty; if (config.TryGetValue("color", ref colorString)) { Color32 color32; if (comp.TryGetNamedColor(colorString, out color32)) { lightColor = color32; UpdateColor(); } else { string[] colors = Utility.SplitVariableList(colorString); if (colors.Length < 3 || colors.Length > 4) { throw new ArgumentException("'lightColor' does not contain 3 or 4 values in INT_LIGHT " + name); } variableRegistrar.RegisterVariableChangeCallback(colors[0], (double newValue) => { lightColor.r = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); UpdateColor(); }); variableRegistrar.RegisterVariableChangeCallback(colors[1], (double newValue) => { lightColor.g = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); UpdateColor(); }); variableRegistrar.RegisterVariableChangeCallback(colors[2], (double newValue) => { lightColor.b = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); UpdateColor(); }); if (colors.Length == 4) { variableRegistrar.RegisterVariableChangeCallback(colors[3], (double newValue) => { lightColor.a = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); UpdateColor(); }); } } } string intensityString = string.Empty; if (config.TryGetValue("intensity", ref intensityString)) { variableRegistrar.RegisterVariableChangeCallback(intensityString, (double newValue) => { float intensity = Mathf.Clamp((float)newValue, 0.0f, 8.0f); for (int i = controlledLights.Length - 1; i >= 0; --i) { controlledLights[i].intensity = intensity; } }); } currentState = false; for (int i = 0; i < controlledLights.Length; ++i) { controlledLights[i].enabled = currentState; } variableRegistrar.RegisterVariableChangeCallback(variableName, VariableCallback); }
internal MASPageGroundTrack(ConfigNode config, InternalProp prop, MASFlightComputer comp, MASMonitor monitor, Transform pageRoot, float depth) : base(config, prop, comp) { this.comp = comp; if (!config.TryGetValue("vertexCount", ref vertexCount)) { throw new ArgumentException("Unable to find 'vertexCount' in GROUND_TRACK " + name); } if (vertexCount < 2) { throw new ArgumentException("'vertexCount' needs to be at least 2 in GROUND_TRACK " + name); } vesselVertex1 = new Vector3[vertexCount]; vesselVertex2 = new Vector3[vertexCount]; targetVertex1 = new Vector3[vertexCount]; targetVertex2 = new Vector3[vertexCount]; maneuverVertex1 = new Vector3[vertexCount]; maneuverVertex2 = new Vector3[vertexCount]; positions = new Vector3d[vertexCount]; float width = 0.0f; if (!config.TryGetValue("size", ref width)) { throw new ArgumentException("Unable to find 'size' in GROUND_TRACK " + name); } size.x = width; size.y = width * 0.5f; float lineWidth = 1.0f; if (!config.TryGetValue("lineWidth", ref lineWidth)) { throw new ArgumentException("Unable to find 'lineWidth' in GROUND_TRACK " + name); } componentOrigin = pageRoot.position + new Vector3(monitor.screenSize.x * -0.5f, monitor.screenSize.y * 0.5f, depth); for (int i = 0; i < numObjects; ++i) { lineOrigin[i] = new GameObject(); lineOrigin[i].transform.parent = pageRoot; lineOrigin[i].transform.position = pageRoot.position; lineOrigin[i].transform.Translate(monitor.screenSize.x * -0.5f + position.x, monitor.screenSize.y * 0.5f - position.y, depth); lineOrigin[i].name = Utility.ComposeObjectName(pageRoot.gameObject.name, this.GetType().Name, name + "-" + i.ToString(), (int)(-depth / MASMonitor.depthDelta)); lineOrigin[i].layer = pageRoot.gameObject.layer; lineMaterial[i] = new Material(MASLoader.shaders["MOARdV/Monitor"]); lineRenderer[i] = lineOrigin[i].AddComponent <LineRenderer>(); lineRenderer[i].useWorldSpace = false; lineRenderer[i].material = lineMaterial[i]; lineRenderer[i].startWidth = lineWidth; lineRenderer[i].endWidth = lineWidth; lineRenderer[i].positionCount = vertexCount; lineRenderer[i].enabled = false; } lineRenderer[0].SetPositions(vesselVertex1); lineRenderer[1].SetPositions(vesselVertex2); lineRenderer[2].SetPositions(targetVertex1); lineRenderer[3].SetPositions(targetVertex2); lineRenderer[4].SetPositions(maneuverVertex1); lineRenderer[5].SetPositions(maneuverVertex2); string positionString = string.Empty; if (!config.TryGetValue("position", ref positionString)) { position = Vector2.zero; throw new ArgumentException("Unable to find 'position' in GROUND_TRACK " + name); } else { string[] pos = Utility.SplitVariableList(positionString); if (pos.Length != 2) { throw new ArgumentException("Invalid number of values for 'position' in GROUND_TRACK " + name); } variableRegistrar.RegisterVariableChangeCallback(pos[0], (double newValue) => { position.x = (float)newValue; UpdateComponentPositions(); }); variableRegistrar.RegisterVariableChangeCallback(pos[1], (double newValue) => { position.y = (float)newValue; UpdateComponentPositions(); }); } string vesselColorString = string.Empty; if (config.TryGetValue("vesselColor", ref vesselColorString)) { Color32 color; if (comp.TryGetNamedColor(vesselColorString, out color)) { vesselColor = color; lineRenderer[0].startColor = vesselColor; lineRenderer[0].endColor = vesselColor; lineRenderer[1].startColor = vesselColor; lineRenderer[1].endColor = vesselColor; } else { string[] vesselColors = Utility.SplitVariableList(vesselColorString); if (vesselColors.Length < 3 || vesselColors.Length > 4) { throw new ArgumentException("vesselColor does not contain 3 or 4 values in GROUND_TRACK " + name); } variableRegistrar.RegisterVariableChangeCallback(vesselColors[0], (double newValue) => { vesselColor.r = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer[0].startColor = vesselColor; lineRenderer[0].endColor = vesselColor; lineRenderer[1].startColor = vesselColor; lineRenderer[1].endColor = vesselColor; }); variableRegistrar.RegisterVariableChangeCallback(vesselColors[1], (double newValue) => { vesselColor.g = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer[0].startColor = vesselColor; lineRenderer[0].endColor = vesselColor; lineRenderer[1].startColor = vesselColor; lineRenderer[1].endColor = vesselColor; }); variableRegistrar.RegisterVariableChangeCallback(vesselColors[2], (double newValue) => { vesselColor.b = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer[0].startColor = vesselColor; lineRenderer[0].endColor = vesselColor; lineRenderer[1].startColor = vesselColor; lineRenderer[1].endColor = vesselColor; }); if (vesselColors.Length == 4) { variableRegistrar.RegisterVariableChangeCallback(vesselColors[3], (double newValue) => { vesselColor.a = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer[0].startColor = vesselColor; lineRenderer[0].endColor = vesselColor; lineRenderer[1].startColor = vesselColor; lineRenderer[1].endColor = vesselColor; }); } } updateVessel = true; } else { lineOrigin[0].SetActive(false); lineOrigin[1].SetActive(false); updateVessel = false; } string maneuverColorString = string.Empty; if (config.TryGetValue("maneuverColor", ref maneuverColorString)) { Color32 color; if (comp.TryGetNamedColor(maneuverColorString, out color)) { maneuverColor = color; lineRenderer[4].startColor = maneuverColor; lineRenderer[4].endColor = maneuverColor; lineRenderer[5].startColor = maneuverColor; lineRenderer[5].endColor = maneuverColor; } else { string[] maneuverColors = Utility.SplitVariableList(maneuverColorString); if (maneuverColors.Length < 3 || maneuverColors.Length > 4) { throw new ArgumentException("maneuverColor does not contain 3 or 4 values in GROUND_TRACK " + name); } variableRegistrar.RegisterVariableChangeCallback(maneuverColors[0], (double newValue) => { maneuverColor.r = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer[4].startColor = maneuverColor; lineRenderer[4].endColor = maneuverColor; lineRenderer[5].startColor = maneuverColor; lineRenderer[5].endColor = maneuverColor; }); variableRegistrar.RegisterVariableChangeCallback(maneuverColors[1], (double newValue) => { maneuverColor.g = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer[4].startColor = maneuverColor; lineRenderer[4].endColor = maneuverColor; lineRenderer[5].startColor = maneuverColor; lineRenderer[5].endColor = maneuverColor; }); variableRegistrar.RegisterVariableChangeCallback(maneuverColors[2], (double newValue) => { maneuverColor.b = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer[4].startColor = maneuverColor; lineRenderer[4].endColor = maneuverColor; lineRenderer[5].startColor = maneuverColor; lineRenderer[5].endColor = maneuverColor; }); if (maneuverColors.Length == 4) { variableRegistrar.RegisterVariableChangeCallback(maneuverColors[3], (double newValue) => { maneuverColor.a = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer[4].startColor = maneuverColor; lineRenderer[4].endColor = maneuverColor; lineRenderer[5].startColor = maneuverColor; lineRenderer[5].endColor = maneuverColor; }); } } updateManeuver = true; } else { lineOrigin[4].SetActive(false); lineOrigin[5].SetActive(false); updateManeuver = false; } string targetColorString = string.Empty; if (config.TryGetValue("targetColor", ref targetColorString)) { Color32 color; if (comp.TryGetNamedColor(targetColorString, out color)) { targetColor = color; lineRenderer[2].startColor = targetColor; lineRenderer[2].endColor = targetColor; lineRenderer[3].startColor = targetColor; lineRenderer[3].endColor = targetColor; } else { string[] targetColors = Utility.SplitVariableList(targetColorString); if (targetColors.Length < 3 || targetColors.Length > 4) { throw new ArgumentException("targetColor does not contain 3 or 4 values in GROUND_TRACK " + name); } variableRegistrar.RegisterVariableChangeCallback(targetColors[0], (double newValue) => { targetColor.r = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer[2].startColor = targetColor; lineRenderer[2].endColor = targetColor; lineRenderer[3].startColor = targetColor; lineRenderer[3].endColor = targetColor; }); variableRegistrar.RegisterVariableChangeCallback(targetColors[1], (double newValue) => { targetColor.g = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer[2].startColor = targetColor; lineRenderer[2].endColor = targetColor; lineRenderer[3].startColor = targetColor; lineRenderer[3].endColor = targetColor; }); variableRegistrar.RegisterVariableChangeCallback(targetColors[2], (double newValue) => { targetColor.b = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer[2].startColor = targetColor; lineRenderer[2].endColor = targetColor; lineRenderer[3].startColor = targetColor; lineRenderer[3].endColor = targetColor; }); if (targetColors.Length == 4) { variableRegistrar.RegisterVariableChangeCallback(targetColors[3], (double newValue) => { targetColor.a = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer[2].startColor = targetColor; lineRenderer[2].endColor = targetColor; lineRenderer[3].startColor = targetColor; lineRenderer[3].endColor = targetColor; }); } } updateTarget = true; } else { lineOrigin[2].SetActive(false); lineOrigin[3].SetActive(false); updateTarget = false; } string startLongitudeString = string.Empty; if (!config.TryGetValue("startLongitude", ref startLongitudeString)) { startLongitude = -180.0f; startLongitudeNormalized = startLongitude / 360.0f; } else { variableRegistrar.RegisterVariableChangeCallback(startLongitudeString, (double newValue) => { float longitude = (float)Utility.NormalizeLongitude(newValue); if (!Mathf.Approximately(longitude, startLongitude)) { startLongitude = longitude; startLongitudeNormalized = startLongitude / 360.0f; } }); } string variableName = string.Empty; if (config.TryGetValue("variable", ref variableName)) { // Disable the mesh if we're in variable mode SetAllActive(false); variableRegistrar.RegisterVariableChangeCallback(variableName, (double newValue) => { if (EvaluateVariable(newValue)) { SetAllActive(currentState); } }); } else { currentState = true; SetAllActive(true); } comp.StartCoroutine(LineUpdateCoroutine()); }
internal MASPageVerticalBar(ConfigNode config, InternalProp prop, MASFlightComputer comp, MASMonitor monitor, Transform pageRoot, float depth) : base(config, prop, comp) { string textureName = string.Empty; if (!config.TryGetValue("texture", ref textureName)) { textureName = string.Empty; } Texture2D mainTexture = null; if (!string.IsNullOrEmpty(textureName)) { mainTexture = GameDatabase.Instance.GetTexture(textureName, false); if (mainTexture == null) { throw new ArgumentException("Unable to find 'texture' " + textureName + " for VERTICAL_BAR " + name); } mainTexture.wrapMode = TextureWrapMode.Clamp; } Vector2 size = Vector2.zero; if (!config.TryGetValue("size", ref size)) { throw new ArgumentException("Unable to find 'size' in VERTICAL_BAR " + name); } barHeight = size.y; string sourceName = string.Empty; if (!config.TryGetValue("source", ref sourceName)) { throw new ArgumentException("Unable to find 'input' in VERTICAL_BAR " + name); } string sourceRange = string.Empty; if (!config.TryGetValue("sourceRange", ref sourceRange)) { throw new ArgumentException("Unable to find 'sourceRange' in VERTICAL_BAR " + name); } string[] ranges = Utility.SplitVariableList(sourceRange); if (ranges.Length != 2) { throw new ArgumentException("Incorrect number of values in 'sourceRange' in VERTICAL_BAR " + name); } variableRegistrar.RegisterVariableChangeCallback(ranges[0], (double newValue) => sourceRange1 = (float)newValue); variableRegistrar.RegisterVariableChangeCallback(ranges[1], (double newValue) => sourceRange2 = (float)newValue); string anchorName = string.Empty; if (config.TryGetValue("anchor", ref anchorName)) { anchorName = anchorName.Trim(); if (anchorName == VBarAnchor.Top.ToString()) { anchor = VBarAnchor.Top; } else if (anchorName == VBarAnchor.Bottom.ToString()) { anchor = VBarAnchor.Bottom; } else if (anchorName == VBarAnchor.Middle.ToString()) { anchor = VBarAnchor.Middle; } else { throw new ArgumentException("Uncrecognized 'anchor' " + anchorName + " in VERTICAL_BAR " + name); } } else { anchor = VBarAnchor.Bottom; } float borderWidth = 0.0f; if (!config.TryGetValue("borderWidth", ref borderWidth)) { borderWidth = 0.0f; } else { borderWidth = Math.Max(1.0f, borderWidth); } string borderColorName = string.Empty; if (!config.TryGetValue("borderColor", ref borderColorName)) { borderColorName = string.Empty; } if (string.IsNullOrEmpty(borderColorName) == (borderWidth > 0.0f)) { throw new ArgumentException("Only one of 'borderColor' and 'borderWidth' are defined in VERTICAL_BAR " + name); } string variableName = string.Empty; if (config.TryGetValue("variable", ref variableName)) { variableName = variableName.Trim(); } // Set up our display surface. imageOrigin = pageRoot.position + new Vector3(monitor.screenSize.x * -0.5f, monitor.screenSize.y * 0.5f, depth); if (borderWidth > 0.0f) { borderObject = new GameObject(); borderObject.name = Utility.ComposeObjectName(pageRoot.gameObject.name, this.GetType().Name, name + "-border", (int)(-depth / MASMonitor.depthDelta)); borderObject.layer = pageRoot.gameObject.layer; borderObject.transform.parent = pageRoot; borderObject.transform.position = imageOrigin + new Vector3(position.x, -(position.y + size.y), 0.0f); borderMaterial = new Material(MASLoader.shaders["MOARdV/Monitor"]); lineRenderer = borderObject.AddComponent <LineRenderer>(); lineRenderer.useWorldSpace = false; lineRenderer.material = borderMaterial; lineRenderer.startColor = borderColor; lineRenderer.endColor = borderColor; lineRenderer.startWidth = borderWidth; lineRenderer.endWidth = borderWidth; Color32 namedColor; if (comp.TryGetNamedColor(borderColorName, out namedColor)) { borderColor = namedColor; lineRenderer.startColor = borderColor; lineRenderer.endColor = borderColor; } else { string[] startColors = Utility.SplitVariableList(borderColorName); if (startColors.Length < 3 || startColors.Length > 4) { throw new ArgumentException("borderColor does not contain 3 or 4 values in VERTICAL_BAR " + name); } variableRegistrar.RegisterVariableChangeCallback(startColors[0], (double newValue) => { borderColor.r = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = borderColor; lineRenderer.endColor = borderColor; }); variableRegistrar.RegisterVariableChangeCallback(startColors[1], (double newValue) => { borderColor.g = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = borderColor; lineRenderer.endColor = borderColor; }); variableRegistrar.RegisterVariableChangeCallback(startColors[2], (double newValue) => { borderColor.b = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = borderColor; lineRenderer.endColor = borderColor; }); if (startColors.Length == 4) { variableRegistrar.RegisterVariableChangeCallback(startColors[3], (double newValue) => { borderColor.a = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = borderColor; lineRenderer.endColor = borderColor; }); } } float halfWidth = borderWidth * 0.5f - 0.5f; Vector3[] borderPoints = new Vector3[] { new Vector3(-halfWidth, -halfWidth, 0.0f), new Vector3(size.x + halfWidth, -halfWidth, 0.0f), new Vector3(size.x + halfWidth, size.y + halfWidth, 0.0f), new Vector3(-halfWidth, size.y + halfWidth, 0.0f), new Vector3(-halfWidth, -halfWidth, 0.0f) }; lineRenderer.positionCount = 5; lineRenderer.SetPositions(borderPoints); } imageObject = new GameObject(); imageObject.name = Utility.ComposeObjectName(pageRoot.gameObject.name, this.GetType().Name, name, (int)(-depth / MASMonitor.depthDelta)); imageObject.layer = pageRoot.gameObject.layer; imageObject.transform.parent = pageRoot; imageObject.transform.position = imageOrigin + new Vector3(position.x, -position.y, 0.0f); string positionString = string.Empty; if (!config.TryGetValue("position", ref positionString)) { throw new ArgumentException("Unable to find 'position' in VERTICAL_BAR " + name); } else { string[] pos = Utility.SplitVariableList(positionString); if (pos.Length != 2) { throw new ArgumentException("Invalid number of values for 'position' in VERTICAL_BAR " + name); } if (borderWidth > 0.0f) { variableRegistrar.RegisterVariableChangeCallback(pos[0], (double newValue) => { position.x = (float)newValue; borderObject.transform.position = imageOrigin + new Vector3(position.x, -(position.y + size.y), 0.0f); imageObject.transform.position = imageOrigin + new Vector3(position.x, -position.y, 0.0f); }); variableRegistrar.RegisterVariableChangeCallback(pos[1], (double newValue) => { position.y = (float)newValue; borderObject.transform.position = imageOrigin + new Vector3(position.x, -(position.y + size.y), 0.0f); imageObject.transform.position = imageOrigin + new Vector3(position.x, -position.y, 0.0f); }); } else { variableRegistrar.RegisterVariableChangeCallback(pos[0], (double newValue) => { position.x = (float)newValue; imageObject.transform.position = imageOrigin + new Vector3(position.x, -position.y, 0.0f); }); variableRegistrar.RegisterVariableChangeCallback(pos[1], (double newValue) => { position.y = (float)newValue; imageObject.transform.position = imageOrigin + new Vector3(position.x, -position.y, 0.0f); }); } } // add renderer stuff MeshFilter meshFilter = imageObject.AddComponent <MeshFilter>(); meshRenderer = imageObject.AddComponent <MeshRenderer>(); mesh = new Mesh(); vertices[0] = new Vector3(0.0f, 0.0f, 0.0f); vertices[1] = new Vector3(size.x, 0.0f, 0.0f); vertices[2] = new Vector3(0.0f, -size.y, 0.0f); vertices[3] = new Vector3(size.x, -size.y, 0.0f); mesh.vertices = vertices; uv[0] = new Vector2(0.0f, 1.0f); uv[1] = Vector2.one; uv[2] = Vector2.zero; uv[3] = new Vector2(1.0f, 0.0f); mesh.uv = uv; mesh.triangles = new[] { 0, 1, 2, 1, 3, 2 }; mesh.RecalculateBounds(); mesh.UploadMeshData(false); meshFilter.mesh = mesh; imageMaterial = new Material(MASLoader.shaders["MOARdV/Monitor"]); if (mainTexture != null) { imageMaterial.mainTexture = mainTexture; } imageMaterial.SetColor(colorField, sourceColor); meshRenderer.material = imageMaterial; RenderPage(false); string sourceColorName = string.Empty; if (config.TryGetValue("sourceColor", ref sourceColorName)) { Color32 namedColor; if (comp.TryGetNamedColor(sourceColorName, out namedColor)) { sourceColor = namedColor; imageMaterial.SetColor(colorField, sourceColor); } else { string[] startColors = Utility.SplitVariableList(sourceColorName); if (startColors.Length < 3 || startColors.Length > 4) { throw new ArgumentException("sourceColor does not contain 3 or 4 values in VERTICAL_BAR " + name); } variableRegistrar.RegisterVariableChangeCallback(startColors[0], (double newValue) => { sourceColor.r = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); imageMaterial.SetColor(colorField, sourceColor); }); variableRegistrar.RegisterVariableChangeCallback(startColors[1], (double newValue) => { sourceColor.g = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); imageMaterial.SetColor(colorField, sourceColor); }); variableRegistrar.RegisterVariableChangeCallback(startColors[2], (double newValue) => { sourceColor.b = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); imageMaterial.SetColor(colorField, sourceColor); }); if (startColors.Length == 4) { variableRegistrar.RegisterVariableChangeCallback(startColors[3], (double newValue) => { sourceColor.a = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); imageMaterial.SetColor(colorField, sourceColor); }); } } } variableRegistrar.RegisterVariableChangeCallback(sourceName, SourceCallback); if (!string.IsNullOrEmpty(variableName)) { // Disable the mesh if we're in variable mode imageObject.SetActive(false); if (borderObject != null) { borderObject.SetActive(false); } variableRegistrar.RegisterVariableChangeCallback(variableName, VariableCallback); } else { if (borderObject != null) { borderObject.SetActive(true); } imageObject.SetActive(true); } }