コード例 #1
0
ファイル: AniMate.cs プロジェクト: bfowle/Ani.Mate
    // Extract animation properties from Hash
    private void CreateAnimations(System.Object obj, Hashtable properties, float duration,
        Hashtable options, AniType type) {

        foreach (DictionaryEntry item in properties) {
            // Extract name and value
            string name = (string)item.Key;
            System.Object value = item.Value;
            // Create value object
            AniValue aniv = new AniValue(obj, name);
            // Get current value
            System.Object current = aniv.Get();

            System.Object start = null;
            System.Object target = null;
            System.Object diff = null;

            // Setup variables
            if (type == AniType.To) {
                start = current;
                target = value;
            } else if (type == AniType.From) {
                start = value;
                target = current;
            } else if (type == AniType.By) {
                start = current;
                diff = value;
            }

            // Cast value to destination type
            System.Object argument = System.Convert.ChangeType(item.Value, aniv.ValueType());
            // Callback
            DefaultCallback callback = (DefaultCallback)options["callback"];

            // Calculate difference for To and From
            if ((type == AniType.To ||
                 type == AniType.From) &&
                DriveNeedsDiff((AnimationDriveType)options["drive"])) {

                try {
                    //diff = target - start;
                    //test = start + 0.1 * diff;

                    System.Type startType = start.GetType();

                    // --- Builtin types
                    if (startType != target.GetType()) {
                        diff = (float)target - (float)start;
                    } else if (startType == typeof(short)) {
                        diff = (short)target - (short)start;
                    } else if (startType == typeof(int)) {
                        diff = (int)target - (int)start;
                    } else if (startType == typeof(long)) {
                        diff = (long)target - (long)start;
                    } else if (startType == typeof(float)) {
                        diff = (float)target - (float)start;
                    } else if (startType == typeof(double)) {
                        diff = (double)target - (double)start;
                    } else if (startType == typeof(decimal)) {
                        diff = (decimal)target - (decimal)start;
                    // --- Unity types
                    } else if (startType == typeof(Vector2)) {
                        diff = (Vector2)target - (Vector2)start;
                    } else if (startType == typeof(Vector3)) {
                        diff = (Vector3)target - (Vector3)start;
                    } else if (startType == typeof(Vector4)) {
                        diff = (Vector4)target - (Vector4)start;
                    } else if (startType == typeof(Color)) {
                        diff = (Color)target - (Color)start;
                    // --- Fallback
                    } else {
                        diff = (float)target - (float)start;
                    }
                } catch {
                    throw new System.Exception("Cannot find diff between " + start.GetType() + " and " + target.GetType() + ": Operation +, - or * not supported.");
                }
            }

            // Start time
            float startTime = 0;
            float delay = (float)options["delay"];
            if (delay > 0) {
                startTime = Time.time + delay;
            }

            // Create animation object
            AniMator mat = new AniMator(start, target, diff, duration, (float)options["delay"], (AnimationEasingType)options["easing"],
                (EasingType)options["direction"], (AnimationDriveType)options["drive"]);

            // Add animation to main list
            AniProp anim = new AniProp(aniv, mat, type, duration, callback, startTime, argument, options);
            // Regular animation
            animations.Add(anim);

            // From: Set to starting value
            if (type == AniType.From) {
                aniv.Set(start);
            }
        }
    }