Ejemplo n.º 1
0
        public static void Animate <T> (this Animatable self, string name, Func <float, T> transform, Action <T> callback, uint rate = 16, uint length = 250,
                                        Func <float, float> easing = null, Action <T, bool> finished = null, Func <bool> repeat = null)
        {
            if (transform == null)
            {
                throw new ArgumentNullException("transform");
            }
            if (callback == null)
            {
                throw new ArgumentNullException("callback");
            }
            if (self == null)
            {
                throw new ArgumentNullException("widget");
            }

            self.AbortAnimation(name);
            name += self.GetHashCode().ToString();

            Action <float>       step  = f => callback(transform(f));
            Action <float, bool> final = null;

            if (finished != null)
            {
                final = (f, b) => finished(transform(f), b);
            }

            var info = new Info {
                Rate   = rate,
                Length = length,
                Easing = easing ?? Easing.Linear
            };

            Tweener tweener = new Tweener(info.Length, info.Rate);

            tweener.Easing        = info.Easing;
            tweener.Handle        = name;
            tweener.ValueUpdated += HandleTweenerUpdated;
            tweener.Finished     += HandleTweenerFinished;

            info.tweener  = tweener;
            info.callback = step;
            info.finished = final;
            info.repeat   = repeat ?? (() => false);
            info.Owner    = self;

            animations[name] = info;
            tweener.Start();

            info.callback(0.0f);
        }
Ejemplo n.º 2
0
        public static void AnimateKinetic(this Animatable self, string name, Func <double, double, bool> callback, double velocity, double drag, Action finished = null)
        {
            self.AbortAnimation(name);
            name += self.GetHashCode().ToString();

            System.Diagnostics.Stopwatch sw = new Stopwatch();
            sw.Start();

            double sign = velocity / Math.Abs(velocity);

            velocity = Math.Abs(velocity);

            var tick = Ticker.Default.Insert(() => {
                var ms = sw.ElapsedMilliseconds;
                sw.Stop();
                sw.Reset();
                sw.Start();

                velocity -= drag * ms;
                velocity  = Math.Max(0, velocity);

                bool result = false;
                if (velocity > 0)
                {
                    result = callback(sign * velocity * ms, velocity);
                }

                if (!result)
                {
                    finished();
                    kinetics.Remove(name);
                    sw.Stop();
                }
                return(result);
            });

            kinetics [name] = tick;
        }