Ejemplo n.º 1
0
 public override ConfigItemVersion Insert(ConfigItemVersion specific)
 {
     if (specific != null && specific.ValueType == ConfigValueType.Bool)
     {
         bool result = (bool)specific.VersionValue ^ (bool)this.VersionValue;
         return Clone(result);
     }
     else return this;
 }
Ejemplo n.º 2
0
        public override ConfigItemVersion Insert(ConfigItemVersion specific)
        {
            if (specific == null) return this;

            object result = null;

            switch (ValueType)
            {
                case ConfigValueType.Integer:
                    int i1 = (int)VersionValue;
                    switch (specific.ValueType)
                    {
                        case ConfigValueType.Integer:
                            result = (int)specific.VersionValue + i1;
                            break;
                        case ConfigValueType.Decimal:
                            result = Convert.ToInt32((decimal)specific.VersionValue + i1);
                            break;
                        case ConfigValueType.Real:
                            result = Convert.ToInt32((double)specific.VersionValue * i1); // factor
                            break;
                    }
                    break;
                case ConfigValueType.Decimal:
                    decimal m1 = (decimal)VersionValue;
                    switch (specific.ValueType)
                    {
                        case ConfigValueType.Integer:
                            result = (int)specific.VersionValue + m1;
                            break;
                        case ConfigValueType.Decimal:
                            result = (decimal)specific.VersionValue + m1;
                            break;
                        case ConfigValueType.Real:
                            result = (decimal)specific.VersionValue * m1; // factor
                            break;
                    }
                    break;
                case ConfigValueType.Real: // factor
                    double d1 = (double)VersionValue;
                    switch (specific.ValueType)
                    {
                        case ConfigValueType.Integer:
                            result = (int)specific.VersionValue * d1;
                            break;
                        case ConfigValueType.Decimal:
                            result = Convert.ToDecimal((double)(decimal)specific.VersionValue * d1);
                            break;
                        case ConfigValueType.Real:
                            result = (double)specific.VersionValue * d1;
                            break;
                    }
                    break;
            }
            return result == null ? this : Clone(result);
        }
Ejemplo n.º 3
0
 public override ConfigItemVersion Insert(ConfigItemVersion specific)
 {
     try
     {
         if (specific == null) return this;
         return (specific.ValueType == ConfigValueType.Parameters)
             ? Clone(StringUtils.MergeNonArrayKeyValuePairs(ToString(), specific.ToString()))
             : this;
     }
     catch
     {
         return this;
     }
 }
Ejemplo n.º 4
0
        public override ConfigItemVersion Insert(ConfigItemVersion specific)
        {
            if (specific == null) return this;

            ConfigItemVersion result;
            IMeasurement generalValue = (IMeasurement)VersionValue;
            switch (specific.ValueType)
            {
                    // Multiply by a scalar.
                case ConfigValueType.Integer:
                case ConfigValueType.Decimal:
                case ConfigValueType.Real:
                    double d = Convert.ToDouble(specific.VersionValue);
                    result = Clone(this);
                    result.VersionValue = generalValue.AsMeasurement() * d;
                    return result;

                    // If specific string is something like "Speed:kph" then convert the Measurement
                    // to a speed and then to a string using the format specifier behind ':'. This
                    // format specifier is interpreted by the MeasurementFormatInfo.Default.
                    // The resulting item is a CfgText, no longer a CfgMeasurement!
                    // You can pass a CfgText as an Application Parameter and influence the formatting
                    // using application preferences.
                case ConfigValueType.StringLiteral:
                case ConfigValueType.StringWithArgs:
                    string s = (string)specific.VersionValue;
                    int p = s.IndexOf(':');
                    string name = s.Substring(0, p);
                    string unit = s.Substring(p + 1);
                    result = new CfgText(ConfigValueType.StringLiteral)
                    {
                        Combine = this.Combine,
                        UID = specific.UID,
                        VersionValue = generalValue.AsMeasurement().ToString(name, unit)
                    };
                    return result;
            }
            return null;
        }
Ejemplo n.º 5
0
        public override ConfigItemVersion Insert(ConfigItemVersion specific)
        {
            string fmt = (string)VersionValue;
            string res = null;
            if (ValueType == ConfigValueType.StringLiteral) return this;

            if (specific != null && (specific.ValueType == ConfigValueType.Bool || specific.ValueType == ConfigValueType.Integer))
            {
                if (specific.ValueType == ConfigValueType.Bool)
                {
                    // Finds placeholders in phrases like "#{No|Yes}, you #{don't |}have permission to do this."
                    // If the boolean specific value is false, the string to the left of | is selected, and if true, the string to the right.
                    bool b = (bool)specific.VersionValue;
                    res = BoolPlaceHolderFinder.Replace(fmt, match => match.Groups[b ? 2 : 1].Value);
                }
                else // ConfigValueType.Integer
                {
                    // Finds placeholders in phrases like "This is the #{first|second|third} time.",  "It takes #{} minute#{|s}"
                    int arg = (int)specific.VersionValue;
                    res = PlaceHolderFinder.Replace(fmt, delegate(Match m)
                    {
                        System.Diagnostics.Trace.WriteLine(fmt);
                        if (arg == 0) return "";
                        string[] options = m.Groups[1].Value.Split('|');
                        if (options.Length == 1) return arg.ToString();
                        else return options[Math.Min(arg - 1, options.Length - 1)];
                    });
                }
            }
            else if (specific != null && specific.ValueType == ConfigValueType.Parameters)
            {
                CfgParams parameters = (CfgParams)specific;
                IDictionary map = parameters.Map;
                res = PlaceHolderFinder.Replace(fmt,
                    delegate (Match match) {
                        string betweenBraces = match.Groups[1].Value;
                        bool startsWithExclamation = betweenBraces.StartsWith("!");
                        string key = startsWithExclamation ? betweenBraces.Substring(1) : betweenBraces;
                        return (string)map[key] ?? (startsWithExclamation ? "" : betweenBraces);
                        // if no actual param found: item like "#{!test}" gets replaced by empty string, whereas #{test} gets replaced by "test".
                    }
                );
            }
            else
            {
                if (specific != null && !PlaceHolderFinder.IsMatch(fmt))
                {
                    fmt += " #{}"; // append at end, with space between, if no placeholder found
                }

                string arg;
                if (specific == null)
                {
                    arg = null;
                }
                else if (specific is CfgMeasurement)
                {
                    arg = ((IMeasurement)specific.VersionValue).ToString("^", MeasurementFormatInfo.Default);
                }
                else
                {
                    arg = specific.VersionValue.ToString();
                }

                // Finds placeholders as in "Hello #{mate}! How are you #{dude}?"
                // The values between #{ and } are the defaults for when there is no specific version.
                bool empty = string.IsNullOrEmpty(arg);
                res = PlaceHolderFinder.Replace(fmt,
                    delegate(Match match)
                    {
                        string s = match.Groups[1].Value;
                        if (empty)
                            if (s.StartsWith("!")) return string.Empty;
                            else return s;
                        else return arg;
                    }
                );
            }
            return Clone(res);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Insert a 
        /// </summary>
        /// <param name="specific"></param>
        /// <returns></returns>
        public override ConfigItemVersion Insert(ConfigItemVersion specific)
        {
            if (specific == null) return this;

            Color? result = null;
            double? alpha = null;
            switch (specific.ValueType)
            {
                    // for numbers: replace the alpha by the specific value.
                case ConfigValueType.Integer:
                    result = Color.FromArgb((int)specific.VersionValue, (Color)VersionValue);
                    break;
                case ConfigValueType.Decimal:
                    alpha = Convert.ToDouble(specific.VersionValue);
                    break;
                case ConfigValueType.Real:
                    alpha = (double)specific.VersionValue;
                    break;
                case ConfigValueType.Color: // blend the two colours by taking the average of each component
                    Color c1 = (Color)VersionValue;
                    Color c2 = (Color)specific.VersionValue;
                    int a = (c1.A + c2.A) >> 1;
                    int r = (c1.R + c2.R) >> 1;
                    int g = (c1.G + c2.G) >> 1;
                    int b = (c1.B + c2.B) >> 1;
                    result = Color.FromArgb(a, r, g, b);
                    break;
            }
            if (alpha.HasValue) result = Color.FromArgb(Convert.ToInt32(alpha.Value * 255), (Color)VersionValue);
            return result == null ? this : Clone(result);
        }
Ejemplo n.º 7
0
 public override ConfigItemVersion Insert(ConfigItemVersion specific)
 {
     return specific;
 }