示例#1
0
        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 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);
            }
        }