aboutsummaryrefslogtreecommitdiff
path: root/js/panels/Timeline/Tween.reel/Tween.js
blob: e5288ad9792a615a5d90608f65d38dc6133eb8ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* <copyright>
 This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
 No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
 (c) Copyright 2011 Motorola Mobility, Inc.  All Rights Reserved.
 </copyright> */

var Montage = require("montage/core/core").Montage;
var Component = require("montage/ui/component").Component;

var Tween = exports.Tween = Montage.create(Component, {

    hasTemplate:{
        value: true
    },

    _tweenData:{
        value:{}
    },

    tweenData:{
        get:function(){
            return this._tweenData;
        },
        set:function(val){
            this._tweenData = val;
            if(this._tweenData){
                this.setData();
            }
        }
    },

    _spanWidth: {
        value: 0
    },

    spanWidth: {
        serializable:true,
        get: function(){
            return this._spanWidth;
        },
        set: function(value){
            this._spanWidth = value;
            this.tweenData.spanWidth = value;
            this.needsDraw=true;
        }
    },

    _spanPosition:{
        value: 0
    },

    spanPosition:{
        serializable:true,
        get:function () {
            return this._spanPosition;
        },
        set:function (value) {
            this._spanPosition = value;
            this.tweenData.spanPosition = value;
            this.needsDraw=true;
        }
    },

    _keyFramePosition:{
        value:0
    },

    keyFramePosition:{
        serializable:true,
        get:function () {
            return this._keyFramePosition;
        },
        set:function (value) {
            this._keyFramePosition = value;
            this.tweenData.keyFramePosition = value;
            this.needsDraw=true;
        }
    },

    _keyFrameMillisec:{
        value:0
    },

    keyFrameMillisec:{
        serializable:true,
        get:function () {
            return this._keyFrameMillisec;
        },
        set:function (value) {
            this._keyFrameMillisec = value;
        }
    },

    _tweenID:{
        value:0
    },

    tweenID:{
        serializable:true,
        get:function () {
            return this._tweenID;
        },
        set:function (value) {
            this._tweenID = value;
        }
    },

    _tweenedProperties:{
        value:[]
    },

    tweenedProperties:{
        serializable:true,
        get:function(){
            return this._tweenedProperties;
        },
        set:function(val){
            this._tweenedProperties = val;
        }
    },

    _isTweenAnimated:{
        value:false
    },

    isTweenAnimated:{
        serializable:true,
        get:function () {
            return this._isTweenAnimated;
        },
        set:function (value) {
            this._isTweenAnimated = value;
        }
    },
    
    _isDragging: {
    	value: false
    },
    isDragging: {
    	serializable: true,
        get:function () {
            return this._isDragging;
        },
        set:function (newVal) {
            this._isDragging = newVal;
        }
    	
    },

    draw:{
        value:function () {
            this.element.style.left = this.spanPosition + "px";
            this.keyframe.position = this.spanWidth;
            this.tweenspan.spanWidth = this.spanWidth;
            if(this.isTweenAnimated){
                this.tweenspan.highlightSpan();
            }
        }
    },

    setData:{
        value:function(){
            this.spanWidth = this.tweenData.spanWidth;
            this.keyFramePosition = this.tweenData.keyFramePosition;
            this.spanPosition = this.tweenData.spanPosition;
            this.keyFrameMillisec = this.tweenData.keyFrameMillisec;
            this.tweenID = this.tweenData.tweenID;
            this.tweenedProperties = this.tweenData.tweenedProperties;
            this.isTweenAnimated = this.tweenData.isTweenAnimated;
            this.needsDraw = true;
        }
    },

    handleElementChange:{
        value:function (event) {
            // temp - testing var
        	var useAbsolute = true;

            if (event.detail.source && event.detail.source !== "tween") {

                if(this.parentComponent.parentComponent.isSubproperty){
                    this.setStyleTweenProperty(event.detail);
                } else {
                    // check for correct element selection
                    if (this.application.ninja.selectedElements[0] != this.parentComponent.parentComponent.animatedElement) {
                        console.log("Wrong element selected for this keyframe track");
                    } else {
                        this.setTweenProperties(event.detail);
                    }
                }
            }
        }
    },

    setTweenProperties:{
        value:function (eventDetail) {
            if(this.parentComponent.parentComponent.animatedElement.offsetTop != this.tweenedProperties["top"]){
                this.tweenedProperties["top"] = this.parentComponent.parentComponent.animatedElement.offsetTop + "px";
            }
            if(this.parentComponent.parentComponent.animatedElement.offsetLeft != this.tweenedProperties["left"]){
                this.tweenedProperties["left"] = this.parentComponent.parentComponent.animatedElement.offsetLeft + "px";
            }
            if (this.parentComponent.parentComponent.animatedElement.offsetWidth != this.tweenedProperties["width"]){
                this.tweenedProperties["width"] = this.parentComponent.parentComponent.animatedElement.offsetWidth + "px";
            }
            if (this.parentComponent.parentComponent.animatedElement.offsetHeight != this.tweenedProperties["height"]){
                this.tweenedProperties["height"] = this.parentComponent.parentComponent.animatedElement.offsetHeight + "px";
            }
            // tell track to update css rule
            this.parentComponent.parentComponent.updateKeyframeRule();
            this.isTweenAnimated = true;
        }
    },

    setStyleTweenProperty:{
        value:function (eventDetail) {
            //console.log("Setting style tween properties for: " + this.parentComponent.parentComponent.trackEditorProperty);
            //console.log(eventDetail);
            if(eventDetail.type == "setProperties"){
                // need to ignore top, left, width, and height
                //console.log(eventDetail.data.value[0]);
                this.tweenedProperties[this.parentComponent.parentComponent.trackEditorProperty] = eventDetail.data.value[0];
                this.parentComponent.parentComponent.updatePropKeyframeRule();

            } else if(eventDetail.type == "setColor"){
                var prop = this.parentComponent.parentComponent.trackEditorProperty;
                this.tweenedProperties[prop] = eventDetail.data.value.color.css;
                this.parentComponent.parentComponent.updatePropKeyframeRule();

            } else if(eventDetail.type == "setProperty"){
                // need to ignore top, left, width, and height
                //console.log(eventDetail.data.value[0]);
                this.tweenedProperties[this.parentComponent.parentComponent.trackEditorProperty] = eventDetail.data.value[0];
                this.parentComponent.parentComponent.updatePropKeyframeRule();

            }else {
                console.log("TWEEN Unhandled type - setStyleTweenProperty : " + eventDetail.type);
            }
        }
    },

    setKeyframeEase:{
        value:function(easeType){
            // easeTypes - ease, ease-out, ease-in, ease-in-out, linear, cubic-bezier(x1, y1, x2, y2)
            this.tweenedProperties["-webkit-animation-timing-function"] = easeType;
            if(this.parentComponent.parentComponent.isSubproperty){
                this.parentComponent.parentComponent.updatePropKeyframeRule();
            } else {
                this.parentComponent.parentComponent.updateKeyframeRule();
            }
        }
    },

    selectTween:{
        value: function(){
            // turn on event listener for element change
            this.eventManager.addEventListener("elementChange", this, false);

            // select the containing layer
            var selectIndex = this.application.ninja.timeline.getLayerIndexByID(this.parentComponent.parentComponent.trackID);
            this.application.ninja.timeline.selectLayer(selectIndex, true);

            // tell timeline to deselect all other tweens and push this one into the selectedTweens in timeline
            this.application.ninja.timeline.deselectTweens();
            this.application.ninja.timeline.selectedTweens.push(this);

            // update playhead position and time text
            this.application.ninja.timeline.playhead.style.left = (this.keyFramePosition - 2) + "px";
            this.application.ninja.timeline.playheadmarker.style.left = this.keyFramePosition + "px";
            var currentMillisecPerPixel = Math.floor(this.application.ninja.timeline.millisecondsOffset / 80);
            var currentMillisec = currentMillisecPerPixel * this.keyFramePosition;
            this.application.ninja.timeline.updateTimeText(currentMillisec);

            if(this.parentComponent.parentComponent.isSubproperty){
                // set property specific style on element
                var currentValue = this.tweenedProperties[this.parentComponent.parentComponent.trackEditorProperty];
                this.application.ninja.elementMediator.setProperty([this.parentComponent.parentComponent.animatedElement], this.parentComponent.parentComponent.trackEditorProperty, [currentValue], "Change", "tween");
                console.log(currentValue);
            } else {
                // move animated element to correct position on stage
                var currentTop = this.tweenedProperties["top"];
                var currentLeft = this.tweenedProperties["left"];
                var currentWidth = this.tweenedProperties["width"];
                var currentHeight = this.tweenedProperties["height"];

                this.application.ninja.elementMediator.setProperty([this.parentComponent.parentComponent.animatedElement], "top", [currentTop], "Change", "tween");
                this.application.ninja.elementMediator.setProperty([this.parentComponent.parentComponent.animatedElement], "left", [currentLeft], "Change", "tween");
                this.application.ninja.elementMediator.setProperty([this.parentComponent.parentComponent.animatedElement], "width", [currentWidth], "Change", "tween");
                this.application.ninja.elementMediator.setProperty([this.parentComponent.parentComponent.animatedElement], "height", [currentHeight], "Change", "tween");
            }
        }
    },

    deselectTween:{
        value:function(){
            // turn off event listener for element change
            this.eventManager.removeEventListener("elementChange", this, false);
            // deselect the keyframe for this tween
            this.keyframe.deselectKeyframe();
        }
    }
});