Example #1
0
            /// <summary>
            /// Try to get a blink color source from a ParsedParameters. The expected format is:
            ///
            /// blink(onSource, onMillis, offSource, offMillis)
            /// </summary>
            public static IColorSource TryParse(PartModule module, ParsedParameters parsedParams)
            {
                if (parsedParams == null)
                {
                    return(null);
                }
                if (!TYPE_NAME.Equals(parsedParams.Identifier))
                {
                    return(null);
                }
                if ((parsedParams.Count < 4) || (parsedParams.Count > 5))
                {
                    throw new ColorSourceException(
                              module,
                              TYPE_NAME + "() source specified " + parsedParams.Count + " parameters (4-5 required)");
                }

                IColorSource onSource;

                try
                {
                    onSource = FindPrivate(module, parsedParams[0]);
                }
                catch (ColorSourceException e)
                {
                    throw new ColorSourceException(module, TYPE_NAME + "() source has invalid 'on' parameter", e);
                }


                long onMillis;

                try
                {
                    onMillis = long.Parse(parsedParams[1]);
                }
                catch (FormatException e)
                {
                    throw new ColorSourceException(module, TYPE_NAME + "(): Invalid 'on' milliseconds value '" + parsedParams[1] + "' (must be an integer)", e);
                }
                if (onMillis < 1)
                {
                    throw new ColorSourceException(module, TYPE_NAME + "(): 'on' milliseconds must be positive");
                }

                IColorSource offSource;

                try
                {
                    offSource = FindPrivate(module, parsedParams[2]);
                }
                catch (ColorSourceException e)
                {
                    throw new ColorSourceException(module, TYPE_NAME + "() source has invalid 'off' parameter", e);
                }

                long offMillis;

                try
                {
                    offMillis = long.Parse(parsedParams[3]);
                }
                catch (FormatException e)
                {
                    throw new ColorSourceException(module, TYPE_NAME + "(): Invalid 'off' milliseconds value '" + parsedParams[3] + "' (must be an integer)", e);
                }
                if (offMillis < 1)
                {
                    throw new ColorSourceException(module, TYPE_NAME + "(): 'off' milliseconds must be positive");
                }

                float phase = 0F;

                if (parsedParams.Count > 4)
                {
                    try
                    {
                        phase = float.Parse(parsedParams[4]);
                    }
                    catch (FormatException e)
                    {
                        throw new ColorSourceException(module, TYPE_NAME + "(): Invalid phase value '" + parsedParams[4] + "' (must be a float)", e);
                    }
                }

                return(new BlinkColorSource(onSource, onMillis, offSource, offMillis, phase));
            }
Example #2
0
            /// <summary>
            /// Try to get a pulsate color source from a ParsedParameters. The expected format is:
            ///
            /// pulsate(origin, cycleMillis, multiplier)
            ///
            /// ...where multiplier is a float in the range 0 - 1.
            /// </summary>
            public static IColorSource TryParse(PartModule module, ParsedParameters parsedParams)
            {
                if (parsedParams == null)
                {
                    return(null);
                }
                if (!TYPE_NAME.Equals(parsedParams.Identifier))
                {
                    return(null);
                }
                if ((parsedParams.Count < 3) || (parsedParams.Count > 4))
                {
                    throw new ColorSourceException(
                              module,
                              TYPE_NAME + "() source specified " + parsedParams.Count + " parameters (3-4 required)");
                }

                IColorSource origin;

                try
                {
                    origin = FindPrivate(module, parsedParams[0]);
                }
                catch (ColorSourceException e)
                {
                    throw new ColorSourceException(module, TYPE_NAME + "() source has invalid origin", e);
                }

                long cycleMillis;

                try
                {
                    cycleMillis = long.Parse(parsedParams[1]);
                }
                catch (FormatException e)
                {
                    throw new ColorSourceException(module, TYPE_NAME + "(): Invalid cycle milliseconds value '" + parsedParams[1] + "'", e);
                }
                if (cycleMillis < 1)
                {
                    throw new ColorSourceException(module, TYPE_NAME + "(): cycle milliseconds must be positive");
                }

                float multiplier2;

                try
                {
                    multiplier2 = float.Parse(parsedParams[2]);
                }
                catch (FormatException e)
                {
                    throw new ColorSourceException(module, TYPE_NAME + "(): Invalid multiplier value '" + parsedParams[2] + "' (must be a float)", e);
                }
                if ((multiplier2 < 0) || (multiplier2 > 1))
                {
                    throw new ColorSourceException(module, TYPE_NAME + "(): Invalid multiplier value '" + parsedParams[2] + "' (must be in range 0 - 1)");
                }

                float multiplier1 = 1;

                if (parsedParams.Count > 3)
                {
                    try
                    {
                        multiplier1 = float.Parse(parsedParams[3]);
                    }
                    catch (FormatException e)
                    {
                        throw new ColorSourceException(module, TYPE_NAME + "(): Invalid multiplier value '" + parsedParams[3] + "' (must be a float)", e);
                    }
                    if ((multiplier1 < 0) || (multiplier1 > 1))
                    {
                        throw new ColorSourceException(module, TYPE_NAME + "(): Invalid multiplier value '" + parsedParams[3] + "' (must be in range 0 - 1)");
                    }
                }

                if ((multiplier1 >= 1) && (multiplier2 >= 1))
                {
                    return(origin);
                }

                return(new PulsateColorSource(origin, cycleMillis, multiplier1, multiplier2));
            }
Example #3
0
        /// <summary>
        /// Given a color source ID (which might be a literal color, the ID of a controller,
        /// or a parameterized source), try to find the appropriate source. If it can't
        /// be parsed or found, throws a ColorSourceException.
        private static IColorSource FindPrivate(PartModule module, string sourceID)
        {
            if (string.IsNullOrEmpty(sourceID))
            {
                throw new ColorSourceException(module, "Null or empty color source");
            }

            // Maybe it's a color string.
            if (Colors.IsColorString(sourceID))
            {
                return(Constant(Colors.Parse(sourceID, Color.black)));
            }

            // Is it a parameterized source?
            ParsedParameters parsedParams = ParsedParameters.TryParse(sourceID);

            if (parsedParams != null)
            {
                // It has the right syntax for a parameterizeed source. Do we recognize it?
                for (int i = 0; i < PARSEABLE_SOURCES.Length; ++i)
                {
                    IColorSource candidate = PARSEABLE_SOURCES[i](module, parsedParams);
                    if (candidate != null)
                    {
                        return(candidate);
                    }
                }
                throw new ColorSourceException(module, "Unknown function type '" + parsedParams.Identifier + "'");
            }

            // Maybe it's another field on the module?
            for (int i = 0; i < module.Fields.Count; ++i)
            {
                BaseField field = module.Fields[i];
                if (!sourceID.Equals(field.name))
                {
                    continue;
                }
                if (!ColorSourceIDField.Is(field))
                {
                    throw new ColorSourceException(
                              module, sourceID + " field on " + module.ClassName + " of "
                              + module.part.GetTitle() + " is not a color source ID field");
                }
                return(Find(module, field.GetValue <string>(module)));
            }

            // Maybe it's a module on the part?
            for (int i = 0; i < module.part.Modules.Count; ++i)
            {
                IColorSource candidate = module.part.Modules[i] as IColorSource;
                if (candidate == null)
                {
                    continue;
                }
                if (sourceID.Equals(candidate.ColorSourceID))
                {
                    return(candidate);
                }
            }

            // not found
            throw new ColorSourceException(module, "Can't find a color source named '" + sourceID + "'");
        }