~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/generic/scriptengines/webkit/dashboard/AppleClasses/AppleAnimator.js

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2008 Stefan Buller <hikingpete@cain.afraid.org>
 
3
 *
 
4
 * This library is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU Lesser General Public
 
6
 * License as published by the Free Software Foundation; either
 
7
 * version 2.1 of the License, or (at your option) version 3, or any
 
8
 * later version accepted by the membership of KDE e.V. (or its
 
9
 * successor approved by the membership of KDE e.V.), which shall
 
10
 * act as a proxy defined in Section 6 of version 3 of the license.
 
11
 *
 
12
 * This library is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * Lesser General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Lesser General Public 
 
18
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 
19
 */
 
20
 
 
21
function AppleAnimator(duration, interval, start, finish, handler)
 
22
{
 
23
    //properties specified by Apple
 
24
    this.duration = duration;
 
25
    this.interval = interval;
 
26
    this.animations = [];
 
27
    this.timer = 0;
 
28
    this.oncomplete; //no documentation exists for this.
 
29
 
 
30
    //internal members:
 
31
    this.intervalPointer = undefined;
 
32
 
 
33
    var that = this;
 
34
 
 
35
    if (arguments.length === 5) {
 
36
        var animation = new AppleAnimation(start, finish, handler);
 
37
        that.addAnimation(animation); //I hope I can use this already
 
38
    }
 
39
};
 
40
 
 
41
AppleAnimator.prototype.addAnimation = function (animation) {
 
42
    //I'm unsure what the behaviour of this function should be if
 
43
    //AppleAnimator.start() has already been called.
 
44
    animation.initialize(this.duration, this.interval);
 
45
    this.animations.push(animation);
 
46
};
 
47
 
 
48
AppleAnimator.prototype.start = function ()
 
49
{
 
50
    if (this.intervalPointer == undefined) {
 
51
 
 
52
        var that = this; //for the closure (may not be necessary?)
 
53
        function intervalHandler() {
 
54
            //setInterval doesn't pass 'this', so we need a closure instead.
 
55
            that.timer += that.interval;
 
56
            if (that.timer > that.duration) {
 
57
                that.stop();
 
58
            } else {
 
59
                for (var i=0; i<that.animations.length; i++) {
 
60
                    var animation = that.animations[i];
 
61
                    animation.step();
 
62
                    animation.callback(animation, animation.now,
 
63
                            animation.from, animation.to);
 
64
                }
 
65
            }
 
66
        }
 
67
 
 
68
        this.intervalPointer = setInterval(intervalHandler, this.interval);
 
69
    }
 
70
};
 
71
 
 
72
AppleAnimator.prototype.stop = function ()
 
73
{
 
74
    if (this.intervalPointer != undefined) {
 
75
        clearInterval(this.intervalPointer);
 
76
        delete this.intervalPointer;
 
77
        //Maybe this.timer should be set to 0, and animations re-initialized
 
78
    }
 
79
};
 
80
 
 
81
function AppleAnimation(start, finish, handler)
 
82
{
 
83
    this.from = start;
 
84
    this.to = finish;
 
85
    this.now = undefined;
 
86
    this.callback = handler;
 
87
 
 
88
    //internal members:
 
89
    //prototype.initialize
 
90
    //this.step
 
91
};
 
92
 
 
93
AppleAnimation.prototype.initialize = function(duration, interval)
 
94
{
 
95
    this.now = this.from;
 
96
 
 
97
    var frac = interval / duration;
 
98
    var diff = this.to - this.from; //Oh memory/speed trade-off (premature optimization?)
 
99
 
 
100
    this.step = function () {
 
101
        //This is a closure, and don't forget it.
 
102
        this.now += diff * frac;
 
103
    }
 
104
};
 
105
 
 
106
function AppleRect(left, top, right, bottom)
 
107
{
 
108
    this.left = left;
 
109
    this.top = top;
 
110
    this.right = right;
 
111
    this.bottom = bottom;
 
112
};
 
113
 
 
114
function AppleRectAnimation(start, finish, handler)
 
115
{
 
116
    this.from = start;
 
117
    this.to = finish;
 
118
    this.now = undefined;
 
119
    this.callback = handler;
 
120
};
 
121
 
 
122
AppleRectAnimation.prototype.initialize = function(duration, interval)
 
123
{
 
124
    this.now = this.from;
 
125
 
 
126
    var frac = interval / duration;
 
127
    var diff = {
 
128
            left: this.to.left - this.from.left,
 
129
            top: this.to.top - this.from.top,
 
130
            right: this.to.right - this.from.right,
 
131
            bottom: this.to.bottom - this.from.bottom
 
132
    }
 
133
 
 
134
    this.step = function () {
 
135
        //This is a closure, and don't forget it.
 
136
        this.now = new AppleRect(this.now.left + frac * diff.left,
 
137
                                 this.now.top + frac * diff.top,
 
138
                                 this.now.right + frac * diff.right,
 
139
                                 this.now.bottom + frac * diff.right)
 
140
    }
 
141
};
 
142