Пример #1
0
        /// <summary>
        /// Interprets the data as representing an integer that may be positive or negative.
        /// Assumes the data is already in the proper format. Use <see cref="IsActionValid"/> to ensure.
        /// </summary>
        public float GetDataAsFloat(float origValue, float minValue, float maxValue)
        {
            string[] chunks = ActionData.Split('|');
            if (chunks.Length != 2)
            {
                throw new Exception("Was expecting numeric setting to have two pieces of data.");
            }

            if (Setting.AllSettings[Target].ValueType != ShortcutTargetDataType.Integer &&
                Setting.AllSettings[Target].ValueType != ShortcutTargetDataType.Float)
            {
                throw new Exception("Was expecting setting to a numeric data type.");
            }

            float value = float.Parse(chunks[0]);

            if (chunks[1].Equals("set"))
            {
                value = Utils.ClampF(value, minValue, maxValue);
            }
            else if (chunks[1].Equals("add"))
            {
                value = Utils.ClampF(origValue + value, minValue, maxValue);
            }
            else if (chunks[1].Equals("sub"))
            {
                value = Utils.ClampF(origValue - value, minValue, maxValue);
            }
            else if (chunks[1].Equals("mul"))
            {
                value = Utils.ClampF(origValue * value, minValue, maxValue);
            }
            else if (chunks[1].Equals("div"))
            {
                value = Utils.ClampF(value == 0 ? maxValue : origValue / value, minValue, maxValue);
            }
            else if (chunks[1].Equals("add-wrap"))
            {
                float val = origValue + value;
                while (val < minValue)
                {
                    val += maxValue;
                }
                while (val > maxValue)
                {
                    val -= maxValue;
                }

                value = Utils.ClampF(val, minValue, maxValue);
            }
            else if (chunks[1].Equals("sub-wrap"))
            {
                float val = origValue - value;
                while (val < minValue)
                {
                    val += maxValue;
                }
                while (val > maxValue)
                {
                    val -= maxValue;
                }

                value = Utils.ClampF(val, minValue, maxValue);
            }
            else
            {
                value = float.Parse(ActionData);
            }

            return(value);
        }