Exemplo n.º 1
0
        public override void previewFrame(ITarget target, float frame, int frameRate, bool play, float playSpeed)
        {
            if (keys == null || keys.Count <= 0)
            {
                return;
            }

            GameObject go   = GetTarget(target) as GameObject;
            Component  comp = GetTargetComp(target);

            if (!comp || !go)
            {
                return;
            }

            if (!isCached)
            {
                RefreshData(comp);
            }

            // if before or equal to first frame, or is the only frame
            PropertyKey firstKey = keys[0] as PropertyKey;

            if (firstKey.endFrame == -1 || (frame <= (float)firstKey.frame && !firstKey.canTween))
            {
                //go.rotation = (cache[0] as AMPropertyAction).getStartQuaternion();
                setComponentValueFromCachedInfo(comp, firstKey.getValue(valueType));
                refreshTransform(go);
                return;
            }

            // if lies on property action
            for (int i = 0; i < keys.Count; i++)
            {
                PropertyKey key     = keys[i] as PropertyKey;
                PropertyKey keyNext = i + 1 < keys.Count ? keys[i + 1] as PropertyKey : null;

                if (frame >= (float)key.endFrame && keyNext != null && (!keyNext.canTween || keyNext.endFrame != -1))
                {
                    continue;
                }
                // if no ease
                if (!key.canTween || keyNext == null)
                {
                    setComponentValueFromCachedInfo(comp, key.getValue(valueType));
                    refreshTransform(go);
                    return;
                }
                // else find value using easing function

                float numFrames = (float)key.getNumberOfFrames(frameRate);

                float framePositionInAction = Mathf.Clamp(frame - (float)key.frame, 0f, numFrames);

                float t;

                if (key.hasCustomEase())
                {
                    t = Utility.EaseCustom(0.0f, 1.0f, framePositionInAction / key.getNumberOfFrames(frameRate), key.easeCurve);
                }
                else
                {
                    var ease = Utility.GetEasingFunction(key.easeType);
                    t = ease(framePositionInAction, key.getNumberOfFrames(frameRate), key.amplitude, key.period);
                }

                //qCurrent.x = ease(qStart.x,qEnd.x,percentage);
                switch ((ValueType)valueType)
                {
                case ValueType.Integer:
                    setComponentValueFromCachedInfo(comp, keyNext != null ? Mathf.RoundToInt(Mathf.Lerp(Convert.ToSingle(key.val), Convert.ToSingle(keyNext.val), t)) : Convert.ToInt32(key.val));
                    break;

                case ValueType.Long:
                    setComponentValueFromCachedInfo(comp, keyNext != null ? (long)Mathf.RoundToInt(Mathf.Lerp(Convert.ToSingle(key.val), Convert.ToSingle(keyNext.val), t)) : Convert.ToInt64(key.val));
                    break;

                case ValueType.Float:
                    setComponentValueFromCachedInfo(comp, keyNext != null ? Mathf.Lerp(Convert.ToSingle(key.val), Convert.ToSingle(keyNext.val), t) : Convert.ToSingle(key.val));
                    break;

                case ValueType.Double:
                    setComponentValueFromCachedInfo(comp, keyNext != null ? key.val + ((double)t) * (keyNext.val - key.val) : key.val);
                    break;

                case ValueType.Vector2:
                    setComponentValueFromCachedInfo(comp, keyNext != null ? Vector2.Lerp(key.vect2, keyNext.vect2, t) : key.vect2);
                    break;

                case ValueType.Vector3:
                    setComponentValueFromCachedInfo(comp, keyNext != null ? Vector3.Lerp(key.vect3, keyNext.vect3, t) : key.vect3);
                    break;

                case ValueType.Color:
                    setComponentValueFromCachedInfo(comp, keyNext != null ? Color.Lerp(key.color, keyNext.color, t) : key.color);
                    break;

                case ValueType.Rect:
                    if (keyNext != null)
                    {
                        Rect vStartRect   = key.rect;
                        Rect vEndRect     = keyNext.rect;
                        Rect vCurrentRect = new Rect();
                        vCurrentRect.x      = Mathf.Lerp(vStartRect.x, vEndRect.x, t);
                        vCurrentRect.y      = Mathf.Lerp(vStartRect.y, vEndRect.y, t);
                        vCurrentRect.width  = Mathf.Lerp(vStartRect.width, vEndRect.width, t);
                        vCurrentRect.height = Mathf.Lerp(vStartRect.height, vEndRect.height, t);
                        setComponentValueFromCachedInfo(comp, vCurrentRect);
                    }
                    else
                    {
                        setComponentValueFromCachedInfo(comp, key.rect);
                    }
                    break;

                case ValueType.Vector4:
                    setComponentValueFromCachedInfo(comp, keyNext != null ? Vector4.Lerp(key.vect4, keyNext.vect4, t) : key.vect4);
                    break;

                case ValueType.Quaternion:
                    setComponentValueFromCachedInfo(comp, keyNext != null ? Quaternion.Slerp(key.quat, keyNext.quat, t) : key.quat);
                    break;

                default:
                    Debug.LogError("Animator: Invalid ValueType " + valueType.ToString());
                    break;
                }
                refreshTransform(go);
                return;
            }
        }
Exemplo n.º 2
0
        public string getValueString(System.Type type, PropertyTrack.ValueType valueType, PropertyKey nextKey, bool brief)
        {
            System.Text.StringBuilder s = new System.Text.StringBuilder();

            if (PropertyTrack.isValueTypeNumeric(valueType))
            {
                //s+= start_val.ToString();
                s.Append(formatNumeric(val));
                if (!brief && nextKey != null)
                {
                    s.Append(" -> "); s.Append(formatNumeric(nextKey.val));
                }
                //if(!brief && endFrame != -1) s += " -> "+end_val.ToString();
            }
            else if (valueType == PropertyTrack.ValueType.Bool)
            {
                s.Append(val > 0.0 ? "(true)" : "(false)");
            }
            else if (valueType == PropertyTrack.ValueType.String)
            {
                s.AppendFormat("\"{0}\"", valString);
            }
            else if (valueType == PropertyTrack.ValueType.Vector2)
            {
                s.Append(vect2.ToString());
                if (!brief && nextKey != null)
                {
                    s.Append(" -> "); s.Append(nextKey.vect2.ToString());
                }
            }
            else if (valueType == PropertyTrack.ValueType.Vector3)
            {
                s.Append(vect3.ToString());
                if (!brief && nextKey != null)
                {
                    s.Append(" -> "); s.Append(nextKey.vect3.ToString());
                }
            }
            else if (valueType == PropertyTrack.ValueType.Color)
            {
                //return null;
                s.Append(color.ToString());
                if (!brief && nextKey != null)
                {
                    s.Append(" -> "); s.Append(nextKey.color.ToString());
                }
            }
            else if (valueType == PropertyTrack.ValueType.Rect)
            {
                //return null;
                s.Append(rect.ToString());
                if (!brief && nextKey != null)
                {
                    s.Append(" -> "); s.Append(nextKey.rect.ToString());
                }
            }
            else if (valueType == PropertyTrack.ValueType.Vector4)
            {
                s.Append(vect4.ToString());
                if (!brief && nextKey != null)
                {
                    s.Append(" -> "); s.Append(nextKey.vect4.ToString());
                }
            }
            else if (valueType == PropertyTrack.ValueType.Quaternion)
            {
                s.Append(quat.ToString());
                if (!brief && nextKey != null)
                {
                    s.Append(" -> "); s.Append(nextKey.quat.ToString());
                }
            }
            else if (valueType == PropertyTrack.ValueType.Sprite)
            {
                s.AppendFormat("\"{0}\"", valObj ? valObj.name : "none");
            }
            else if (valueType == PropertyTrack.ValueType.Enum)
            {
                s.Append(System.Enum.ToObject(type, (int)val).ToString());
            }
            return(s.ToString());
        }
Exemplo n.º 3
0
        // add key
        public PropertyKey addKey(ITarget target, int _frame)
        {
            Component comp = GetTargetComp(target);

            RefreshData(comp);

            PropertyKey k = null;

            foreach (PropertyKey key in keys)
            {
                // if key exists on frame, update key
                if (key.frame == _frame)
                {
                    k = key;
                }
            }

            if (k == null)
            {
                k        = new PropertyKey();
                k.frame  = _frame;
                k.interp = canTween ? Key.Interpolation.Linear : Key.Interpolation.None;
                // add a new key
                keys.Add(k);
            }

            if (isValueTypeNumeric(valueType))
            {
                k.val = Convert.ToDouble(getCachedInfoValue(target));
            }
            else if (valueType == ValueType.Bool)
            {
                k.valb = Convert.ToBoolean(getCachedInfoValue(target));
            }
            else if (valueType == ValueType.String)
            {
                k.valString = Convert.ToString(getCachedInfoValue(target));
            }
            else if (valueType == ValueType.Vector2)
            {
                k.vect2 = (Vector2)getCachedInfoValue(target);
            }
            else if (valueType == ValueType.Vector3)
            {
                k.vect3 = (Vector3)getCachedInfoValue(target);
            }
            else if (valueType == ValueType.Color)
            {
                k.color = (Color)getCachedInfoValue(target);
            }
            else if (valueType == ValueType.Rect)
            {
                k.rect = (Rect)getCachedInfoValue(target);
            }
            else if (valueType == ValueType.Vector4)
            {
                k.vect4 = (Vector4)getCachedInfoValue(target);
            }
            else if (valueType == ValueType.Quaternion)
            {
                k.quat = (Quaternion)getCachedInfoValue(target);
            }
            else if (valueType == ValueType.Sprite)
            {
                k.valObj = (UnityEngine.Object)getCachedInfoValue(target);
            }
            else if (valueType == ValueType.Enum)
            {
                k.val = Convert.ToDouble(getCachedInfoValue(target));
            }
            else
            {
                Debug.LogError("Animator: Invalid ValueType " + valueType.ToString());
            }

            // update cache
            updateCache(target);
            return(k);
        }