aboutsummaryrefslogtreecommitdiff
path: root/js/panels/Timeline/Keyframe.reel/Keyframe.js
blob: c82a9086f48a2afbaad7304be5aa50597437d614 (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
var Montage = require("montage/core/core").Montage;
var Component = require("montage/ui/component").Component;
var ElementsMediator = require("js/mediators/element-mediator").ElementMediator;

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

    hasTemplate:{
        value: true
    },

    _position:{
        value:0
    },

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

    _id:{
        value:0
    },

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

    _timelinePosition:{
        value:0
    },

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

    _containingTrack:{
        value:{}
    },

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

    _animatedProperties:{
        value:[]
    },

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

    containingSpan:{
        value: null
    },

    prepareForDraw:{
        value:function(){
            this.tweenkeyframe.addEventListener("click", this, false);
            this.animatedProperties = new Array();
            this.animatedProperties["top"] = this.containingTrack.animatedElement.offsetTop;
            this.animatedProperties["left"] = this.containingTrack.animatedElement.offsetLeft;
        }
    },

    draw:{
        value:function(){
            this.tweenkeyframe.style.left = (this.position - 3) + "px";
        }
    },

    handleElementChange:{
        value:function (event) {
            if(this.application.ninja.selectedElements[0]._element != this.containingTrack.animatedElement){
                alert("Wrong element selected for this keyframe track");
                return;
            }

            if(event.detail.source && event.detail.source !== "keyframe") {
                this.containingSpan.highlightSpan();
                if(this.containingTrack.animatedElement.offsetTop != this.animatedProperties["top"] && this.containingTrack.animatedElement.offsetLeft != this.animatedProperties["left"]){
                    this.animatedProperties["top"] = this.containingTrack.animatedElement.offsetTop;
                    this.animatedProperties["left"] = this.containingTrack.animatedElement.offsetLeft;
                    this.containingTrack.keyFramePropertyData[this.id] = this.animatedProperties;
                    this.containingTrack.updateKeyframeRule();
                }
            }
        }
    },

    deselect:{
        value:function(){
            this.tweenkeyframe.classList.remove("keyframeSelected");
            this.eventManager.removeEventListener("elementChange", this, false);
        }
    },

    select:{
        value:function(){
            this.application.ninja.timeline.deselectKeyframes();
            this.tweenkeyframe.classList.add("keyframeSelected");
            this.application.ninja.timeline.playhead.style.left = (this.timelinePosition - 2) + "px";
            this.application.ninja.timeline.playheadmarker.style.left = this.timelinePosition + "px";
            this.application.ninja.timeline.selectedKeyframes.push(this);

            var currentMillisecPerPixel = Math.floor(this.application.ninja.timeline.millisecondsOffset / 80);
            var currentMillisec = currentMillisecPerPixel * this.timelinePosition;
            this.application.ninja.timeline.updateTimeText(currentMillisec);

            var currentTop = this.animatedProperties["top"] + "px";
            var currentLeft = this.animatedProperties["left"] + "px";

            ElementsMediator.setProperty([this.containingTrack.animatedElement], "top", [currentTop], "Change", "keyframe");
            ElementsMediator.setProperty([this.containingTrack.animatedElement], "left", [currentLeft], "Change", "keyframe");

            // turn on element change event listener
            this.eventManager.addEventListener("elementChange", this, false);
        }
    },

    handleClick:{
        value:function(ev){
            this.select();
        }
    }
});