2011-02-03 03:12:01 +00:00
|
|
|
GUI.millis = function() {
|
|
|
|
var d = new Date();
|
|
|
|
return d.getTime();
|
2011-02-12 01:01:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
GUI.Controller.prototype.at = function(when, what, tween) {
|
2011-02-13 17:23:10 +00:00
|
|
|
// TODO: Disable if we're using loaded JSON. Don't want to duplicate events.
|
2011-02-12 01:01:02 +00:00
|
|
|
if (!this.scrubber) {
|
|
|
|
GUI.error('You must create a new Timer for this GUI in order to define events.');
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
this.scrubber.add(new GUI.ScrubberPoint(this.scrubber, when, what));
|
|
|
|
this.scrubber.render();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
GUI.loadJSON = function(json) {
|
|
|
|
if (typeof json == 'string') {
|
|
|
|
json = eval('('+json+')');
|
|
|
|
}
|
|
|
|
GUI.loadedJSON = json;
|
|
|
|
}
|
|
|
|
|
|
|
|
GUI.loadedJSON = null;
|
|
|
|
|
|
|
|
GUI.getJSON = function() {
|
2011-02-13 17:23:10 +00:00
|
|
|
var guis = [];
|
2011-02-12 01:01:02 +00:00
|
|
|
for (var i in GUI.allGuis) {
|
|
|
|
guis.push(GUI.allGuis[i].getJSON());
|
|
|
|
}
|
2011-02-13 17:23:10 +00:00
|
|
|
var obj = {guis:guis};
|
|
|
|
return {guis:guis};
|
2011-02-03 03:12:01 +00:00
|
|
|
}
|
2011-02-12 01:01:02 +00:00
|
|
|
|
2011-02-15 17:13:59 +00:00
|
|
|
GUI.closeSave = function() {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
2011-02-13 17:23:10 +00:00
|
|
|
GUI.save = function() {
|
2011-02-03 03:12:01 +00:00
|
|
|
|
2011-02-13 17:23:10 +00:00
|
|
|
var jsonString = JSON.stringify(GUI.getJSON());
|
|
|
|
|
|
|
|
var dialogue = document.createElement('div');
|
|
|
|
dialogue.setAttribute('id', 'guidat-save-dialogue');
|
|
|
|
|
|
|
|
var a = document.createElement('a');
|
|
|
|
a.setAttribute('href', window.location.href+'?gui='+escape(jsonString));
|
|
|
|
a.innerHTML = 'Use this URL.';
|
|
|
|
|
|
|
|
var span2 = document.createElement('span');
|
|
|
|
span2.innerHTML = '… or paste this into the beginning of your source:';
|
|
|
|
|
|
|
|
var textarea = document.createElement('textarea');
|
2011-02-15 17:13:59 +00:00
|
|
|
//textarea.setAttribute('disabled', 'true');
|
2011-02-13 17:23:10 +00:00
|
|
|
textarea.innerHTML += 'GUI.loadJSON('+jsonString+');';
|
2011-02-15 17:13:59 +00:00
|
|
|
|
|
|
|
var close = document.createElement('div');
|
|
|
|
close.setAttribute('id', 'guidat-save-dialogue-close');
|
|
|
|
close.addEventListener('click', function() {
|
|
|
|
GUI.closeSave();
|
|
|
|
}, false);
|
2011-02-13 17:23:10 +00:00
|
|
|
|
2011-02-02 19:43:49 +00:00
|
|
|
|
2011-02-13 17:23:10 +00:00
|
|
|
dialogue.appendChild(a);
|
|
|
|
dialogue.appendChild(span2);
|
|
|
|
dialogue.appendChild(textarea);
|
2011-02-15 17:13:59 +00:00
|
|
|
document.body.appendChild(dialogue);
|
|
|
|
|
|
|
|
textarea.addEventListener('click', function() {
|
|
|
|
this.select();
|
|
|
|
}, false);
|
|
|
|
|
2011-02-12 01:01:02 +00:00
|
|
|
|
2011-02-13 17:23:10 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GUI.Timer = function(gui) {
|
|
|
|
|
2011-02-02 19:43:49 +00:00
|
|
|
var _this = this;
|
|
|
|
|
2011-02-02 20:51:58 +00:00
|
|
|
this.hoverPoint = null;
|
|
|
|
this.activePoint = null;
|
|
|
|
|
2011-02-02 19:43:49 +00:00
|
|
|
this.gui = gui;
|
|
|
|
this.gui.timer = this;
|
|
|
|
|
2011-02-08 20:56:00 +00:00
|
|
|
this.gui.domElement.setAttribute('class', 'guidat time');
|
2011-02-09 18:27:59 +00:00
|
|
|
this.gui.domElement.style.width = '100%';
|
|
|
|
|
2011-02-08 20:56:00 +00:00
|
|
|
// Put toggle button on top.
|
|
|
|
var toggleButton = this.gui.domElement.lastChild;
|
2011-02-09 18:27:59 +00:00
|
|
|
|
2011-02-08 20:56:00 +00:00
|
|
|
this.gui.domElement.removeChild(toggleButton);
|
|
|
|
this.gui.domElement.insertBefore(toggleButton, this.gui.domElement.firstChild);
|
|
|
|
|
2011-02-09 18:27:59 +00:00
|
|
|
// Create tween dropdown.
|
|
|
|
this.tweenSelector = document.createElement('select');
|
|
|
|
this.tweenSelector.setAttribute('class', 'guidat-tween-selector');
|
|
|
|
for (var i in GUI.Easing) {
|
|
|
|
var opt = document.createElement('option');
|
|
|
|
opt.innerHTML = i;
|
|
|
|
this.tweenSelector.appendChild(opt);
|
|
|
|
}
|
|
|
|
this.tweenSelector.addEventListener('change', function(e) {
|
|
|
|
if (_this.activePoint != null) {
|
|
|
|
_this.activePoint.tween = GUI.Easing[this.value];
|
|
|
|
}
|
|
|
|
}, false);
|
|
|
|
this.gui.domElement.appendChild(this.tweenSelector);
|
2011-02-11 22:54:39 +00:00
|
|
|
|
2011-02-09 18:27:59 +00:00
|
|
|
|
2011-02-11 22:54:39 +00:00
|
|
|
this.showTweenSelector = function() {
|
|
|
|
_this.tweenSelector.style.display = 'block';
|
|
|
|
}
|
|
|
|
this.hideTweenSelector = function() {
|
|
|
|
_this.tweenSelector.style.display = 'none';
|
|
|
|
}
|
|
|
|
|
|
|
|
this.hideTweenSelector();
|
2011-02-09 18:27:59 +00:00
|
|
|
|
2011-02-02 19:43:49 +00:00
|
|
|
var playhead = 0;
|
|
|
|
var lastPlayhead = 0;
|
2011-02-08 15:40:38 +00:00
|
|
|
|
2011-02-02 19:43:49 +00:00
|
|
|
var playListeners = [];
|
|
|
|
var windowListeners = [];
|
|
|
|
|
|
|
|
var windowWidth = 10000;
|
2011-02-03 03:12:01 +00:00
|
|
|
var windowMin = -windowWidth/4;
|
2011-02-02 19:43:49 +00:00
|
|
|
|
|
|
|
var thisTime;
|
|
|
|
var lastTime;
|
|
|
|
|
|
|
|
var playInterval = -1;
|
|
|
|
var playResolution = 1000/60;
|
|
|
|
|
|
|
|
var playing = false;
|
|
|
|
|
2011-02-08 15:40:38 +00:00
|
|
|
var snapIncrement = 250;
|
|
|
|
var useSnap = false;
|
|
|
|
|
2011-02-09 18:27:59 +00:00
|
|
|
this.__defineGetter__('useSnap', function() {
|
2011-02-08 15:40:38 +00:00
|
|
|
return useSnap;
|
|
|
|
});
|
|
|
|
|
2011-02-09 18:27:59 +00:00
|
|
|
this.__defineSetter__('useSnap', function(v) {
|
2011-02-08 15:40:38 +00:00
|
|
|
useSnap = v;
|
|
|
|
for (var i in _this.scrubbers) {
|
|
|
|
_this.scrubbers[i].render();
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2011-02-09 18:27:59 +00:00
|
|
|
this.__defineGetter__('snapIncrement', function() {
|
2011-02-08 15:40:38 +00:00
|
|
|
return snapIncrement;
|
|
|
|
});
|
|
|
|
|
2011-02-09 18:27:59 +00:00
|
|
|
this.__defineSetter__('snapIncrement', function(v) {
|
2011-02-08 15:40:38 +00:00
|
|
|
if (snapIncrement > 0) {
|
|
|
|
snapIncrement = v;
|
|
|
|
for (var i in _this.scrubbers) {
|
|
|
|
_this.scrubbers[i].render();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.snap = function(t) {
|
|
|
|
|
|
|
|
if (!this.useSnap) {
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
var r = Math.round(t/this.snapIncrement)*this.snapIncrement;
|
|
|
|
return r;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
this.scrubbers = [];
|
|
|
|
|
2011-02-09 18:27:59 +00:00
|
|
|
window.addEventListener('keyup', function(e) {
|
|
|
|
if (GUI.disableKeyListeners) return;
|
2011-02-03 00:01:12 +00:00
|
|
|
switch (e.keyCode) {
|
|
|
|
case 32:
|
|
|
|
_this.playPause();
|
|
|
|
break;
|
|
|
|
case 13:
|
|
|
|
_this.stop();
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
if (_this.activePoint != null) {
|
|
|
|
_this.activePoint.remove();
|
|
|
|
_this.activePoint = null;
|
|
|
|
}
|
2011-02-14 03:24:35 +00:00
|
|
|
_this.hideTweenSelector();
|
2011-02-03 00:01:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}, false);
|
|
|
|
|
2011-02-12 01:01:02 +00:00
|
|
|
this.getJSON = function() {
|
2011-02-08 15:40:38 +00:00
|
|
|
|
|
|
|
var scrubberArr = [];
|
|
|
|
|
|
|
|
for (var i in _this.scrubbers) {
|
2011-02-12 01:01:02 +00:00
|
|
|
scrubberArr.push(_this.scrubbers[i].getJSON());
|
2011-02-08 15:40:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var obj = {'windowMin':_this.windowMin,
|
|
|
|
'windowWidth':_this.windowWidth,
|
|
|
|
'playhead':_this.playhead,
|
|
|
|
'snapIncrement': _this.snapIncrement,
|
2011-02-13 17:23:10 +00:00
|
|
|
'useSnap': _this.useSnap,
|
2011-02-08 15:40:38 +00:00
|
|
|
'scrubbers': scrubberArr};
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2011-02-09 18:27:59 +00:00
|
|
|
this.__defineGetter__('windowMin', function() {
|
2011-02-02 19:43:49 +00:00
|
|
|
return windowMin;
|
|
|
|
});
|
|
|
|
|
2011-02-09 18:27:59 +00:00
|
|
|
this.__defineSetter__('windowMin', function(v) {
|
2011-02-02 19:43:49 +00:00
|
|
|
windowMin = v;
|
|
|
|
for (var i in windowListeners) {
|
|
|
|
windowListeners[i].call(windowListeners[i]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-02-09 18:27:59 +00:00
|
|
|
this.__defineGetter__('windowWidth', function() {
|
2011-02-02 19:43:49 +00:00
|
|
|
return windowWidth;
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2011-02-09 18:27:59 +00:00
|
|
|
this.__defineSetter__('windowWidth', function(v) {
|
|
|
|
// TODO: Make these constants.
|
|
|
|
windowWidth = GUI.constrain(v, 1000, 60000);
|
2011-02-02 19:43:49 +00:00
|
|
|
for (var i in windowListeners) {
|
|
|
|
windowListeners[i].call(windowListeners[i]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-02-09 18:27:59 +00:00
|
|
|
this.__defineGetter__('playhead', function() {
|
2011-02-02 19:43:49 +00:00
|
|
|
return playhead;
|
|
|
|
});
|
|
|
|
|
2011-02-09 18:27:59 +00:00
|
|
|
this.__defineSetter__('playhead', function(t) {
|
2011-02-02 19:43:49 +00:00
|
|
|
lastPlayhead = playhead;
|
|
|
|
playhead = t;
|
2011-02-03 00:01:12 +00:00
|
|
|
if (playing) {
|
|
|
|
windowMin += ((playhead-windowWidth/2)-windowMin)*0.3;
|
|
|
|
}
|
2011-02-02 19:43:49 +00:00
|
|
|
for (var i = 0; i < playListeners.length; i++) {
|
|
|
|
playListeners[i].call(this, playhead, lastPlayhead);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-02-09 18:27:59 +00:00
|
|
|
this.__defineGetter__('playing', function() {
|
2011-02-02 19:43:49 +00:00
|
|
|
return playing;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.play = function() {
|
|
|
|
playing = true;
|
2011-02-03 03:12:01 +00:00
|
|
|
lastTime = GUI.millis();
|
2011-02-02 19:43:49 +00:00
|
|
|
if (playInterval == -1) {
|
|
|
|
playInterval = setInterval(this.update, playResolution);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.update = function() {
|
2011-02-03 03:12:01 +00:00
|
|
|
thisTime = GUI.millis();
|
2011-02-02 19:43:49 +00:00
|
|
|
_this.playhead = _this.playhead + (thisTime - lastTime);
|
|
|
|
lastTime = thisTime;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.pause = function() {
|
|
|
|
playing = false;
|
|
|
|
clearInterval(playInterval);
|
|
|
|
playInterval = -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.playPause = function() {
|
|
|
|
if (playing) {
|
|
|
|
this.pause();
|
|
|
|
} else {
|
|
|
|
this.play();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.stop = function() {
|
|
|
|
this.pause();
|
|
|
|
this.playhead = 0;
|
2011-02-03 03:12:01 +00:00
|
|
|
this.windowMin = -windowWidth/4;
|
2011-02-02 19:43:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.addPlayListener = function(fnc) {
|
|
|
|
playListeners.push(fnc);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.addWindowListener = function(fnc) {
|
|
|
|
windowListeners.push(fnc);
|
|
|
|
};
|
2011-02-13 17:23:10 +00:00
|
|
|
|
|
|
|
// Load saved stuff.
|
|
|
|
if (gui.json && gui.json.timer) {
|
|
|
|
this.playhead = gui.json.timer.playhead;
|
|
|
|
this.snapIncrement = gui.json.timer.snapIncrement;
|
|
|
|
this.useSnap = gui.json.timer.useSnap;
|
|
|
|
this.windowMin = gui.json.timer.windowMin;
|
|
|
|
this.windowWidth = gui.json.timer.windowWidth;
|
|
|
|
}
|
2011-02-14 03:24:35 +00:00
|
|
|
}
|
|
|
|
|