Commit 0aad1860 authored by Jérémie Garcia's avatar Jérémie Garcia

cleaning update

Added comments + structure to the code
Ongoing
parent 06d4a2c1
This diff is collapsed.
// this function are called by html elements.
// TODO : create events on each html elements
function sendTrigger(message) {}
//Functions called from Html interface
function deleteCurrentCurve() {
Traj.TrajManager.deleteCurve(Traj.TrajManager.currentCurveIndex);
Traj.EventManager.hideLongTouchMenu();
}
function selectNextCurve() {
Traj.TrajManager.selectCurve((Traj.TrajManager.currentCurveIndex + 1) % Traj.TrajManager.trajectories.length);
}
function selectPreviousCurve() {
Traj.TrajManager.selectCurve((Traj.TrajManager.currentCurveIndex - 1 + Traj.TrajManager.trajectories.length) % Traj.TrajManager.trajectories.length);
}
function sendTriggerPlay() {
Traj.OSC.sendPlay("Play");
}
function sendTriggerRecord() {
Traj.OSC.sendRecord("Record");
}
//Getters and Setters
function setCanvasZoom(factor) {
//if (logger) writtraj_canvasog("traj_canvas Zoom " + factor);
Traj.View.scaleFactor = Traj.View.scaleFactor / factor;
Traj.View.pixToValue = Traj.View.scaleFactor / Traj.View.minWH;
Traj.View.repaintAll();
}
function setDrawCurvesInBackground() {
Traj.View.backgroundCurve = document.getElementById("flip-curvesinback").value == 'on';
Traj.View.traj_repaint();
Traj.View.dyn_repaint();
}
function setLoopMode() {
Traj.Player.loopMode = document.getElementById("flip-loop").value == 'on';
}
function setOrientationMode() {
Traj.EventManager.stream_orientation = document.getElementById("flip-orientation").value == 'on';
}
function setHpDistance() {
Traj.View.SPEAKER_DIST = document.getElementById("speaker_dist").value;
Traj.View.bg_repaint();
}
function exportCurrentCurve(){
exportCurrentCurveAsAntescofoNIM();
if(Traj.TrajManager.trajectories[Traj.TrajManager.currentCurveIndex]){
if(Traj.TrajManager.trajectories[Traj.TrajManager.currentCurveIndex].X.length <1000){
Traj.OSC.sendCurveSingleMessageOSC(Traj.TrajManager.trajectories[Traj.TrajManager.currentCurveIndex], Traj.TrajManager.currentCurveIndex);
}else{
Traj.OSC.sendCurveOSC(Traj.TrajManager.trajectories[Traj.TrajManager.currentCurveIndex], Traj.TrajManager.currentCurveIndex);
}
}
Traj.Session.saveInSelectedSession();
}
function exportCurrentCurveAsAntescofoNIM(){
var manager = Traj.TrajManager;
if(manager.trajectories[manager.currentCurveIndex]){
Traj.OSC.saveAntescofo(manager.trajectories[manager.currentCurveIndex], manager.currentCurveIndex, Traj.Session.currentSession);
}
}
function setTimeSliderMode() {
if (document.getElementById("flip-timeSlider").value == 'on') {
$("#slider-div").show();
} else {
$("#slider-div").hide();
}
}
function simplifyCurrentCurve(tolerance) {
var currentCurve = Traj.TrajManager.trajectories[Traj.TrajManager.currentCurveIndex];
if (currentCurve != undefined) {
var newCurve = Traj.Utils.simplifyCurve(currentCurve,tolerance);
Traj.TrajManager.trajectories.push(newCurve);
Traj.TrajManager.selectCurve(Traj.TrajManager.trajectories.length-1);
}
}
function changeSourceControled() {
var curveIdx = Traj.TrajManager.currentCurveIndex;
var curve = Traj.TrajManager.trajectories[curveIdx];
if (curve != undefined) {
var source = Number(document.getElementById("sourceControled").value);
curve.changeSource(source);
Traj.View.current_repaint();
Traj.View.traj_repaint();
Traj.View.dyn_repaint();
}
}
function activeMultiPlay() {
var curveIdx = Traj.TrajManager.currentCurveIndex;
if (Traj.TrajManager.trajectories[curveIdx]) {
Traj.TrajManager.trajectories[curveIdx].multiPlay = document.getElementById("checkPlay").checked;
Traj.TrajManager.multiPlayChanged(0);
}
}
function removeMultiPlay() {
var trajectories = Traj.TrajManager.trajectories;
for (var k=0;k<trajectories.length;k++) {
trajectories[k].multiPlay = false;
}
Traj.TrajManager.playedCurves = [];
Traj.EventManager.refreshCurveInfo();
Traj.TrajManager.multiPlayChanged();
}
function modeSelectEdit() {
var curveIdx = Traj.TrajManager.currentCurveIndex;
if (Traj.TrajManager.trajectories[curveIdx]) {
Traj.EventManager.initSlider(true)
$("#divCurveOptions").hide();
$("#divCurveOptions").removeClass( "show" ).addClass( "hide" );
$("#playDiv").show();
Traj.View.bg_repaint(true);
}
Traj.EventManager.hideLongTouchMenu();
}
function setZSliderMode() {
if (document.getElementById("flip-zSlider").value == 'on') {
$("#ZSliderDiv").show();
} else {
$("#ZSliderDiv").hide();
}
}
function changeScale(axis,scale) {
var curve = Traj.TrajManager.trajectories[Traj.TrajManager.currentCurveIndex];
if (curve != undefined) {
if (axis == 'x') {
curve.changeXScale(scale);
} else if (axis == 'y') {
curve.changeYScale(scale);
} else if (axis == 'z') {
curve.changeZScale(scale);
}
Traj.TrajManager.selectCurve(Traj.TrajManager.currentCurveIndex);
}
}
function changeTime() {
var curve = Traj.TrajManager.trajectories[Traj.TrajManager.currentCurveIndex];
if (curve != undefined) {
var inputTime = document.getElementById('timeStretch');
curve.changeTotalTime(Number(inputTime.value));
Traj.TrajManager.selectCurve(Traj.TrajManager.currentCurveIndex);
Traj.EventManager.initSlider(false);
}
}
function translateCurve() {
var curve = Traj.TrajManager.trajectories[Traj.TrajManager.currentCurveIndex];
if (curve != undefined) {
var inputX = document.getElementById('xOriPos').value;
var inputY = document.getElementById('yOriPos').value;
var inputZ = document.getElementById('zOriPos').value;
curve.translate(Number(inputX),Number(inputY),Number(inputZ));
Traj.TrajManager.selectCurve(Traj.TrajManager.currentCurveIndex);
}
}
function copyCurrentCurve() {
var curve = Traj.TrajManager.trajectories[Traj.TrajManager.currentCurveIndex];
if (curve != undefined) {
Traj.TrajManager.copyCurrentCurve(0,curve.X.length-1);
Traj.EventManager.hideLongTouchMenu();
}
}
function pasteInCurrentCurve() {
var curve = Traj.TrajManager.trajectories[Traj.TrajManager.currentCurveIndex];
if (curve != undefined) {
var beginTime = Traj.EventManager.getSliderValue();
var beginIdx = Traj.Utils.findPointIdx(beginTime,curve);
Traj.TrajManager.pasteInCurve(curve,beginIdx,beginIdx);
Traj.EventManager.hideLongTouchMenu();
//Traj.TrajManager.selectCurve(Traj.TrajManager.trajectories.length-1);
}
}
function duplicateCurrentCurve() {
var curve = Traj.TrajManager.trajectories[Traj.TrajManager.currentCurveIndex];
if (curve != undefined) {
var newCurve = curve.clone();
var XYZ = [newCurve.X[0]+0.1,newCurve.Y[0]-0.1,newCurve.Z[0]];
newCurve.translate(XYZ[0],XYZ[1],XYZ[2]);
Traj.TrajManager.trajectories.push(newCurve);
Traj.TrajManager.selectCurve(Traj.TrajManager.trajectories.length-1);
Traj.EventManager.hideLongTouchMenu();
}
}
function mirror(type) {
var curve = Traj.TrajManager.trajectories[Traj.TrajManager.currentCurveIndex];
if (curve != undefined) {
if (type == 'x') {
curve.xMiror();
} else if (type == 'y') {
curve.yMiror();
} else if (type == 'z') {
curve.zMiror();
} else if (type == 't') {
curve.tMiror();
}
Traj.TrajManager.selectCurve(Traj.TrajManager.currentCurveIndex);
}
}
////////////////////////////////////////////////////////////
///////////////////////// OSC /////////////////////////////
Traj.OSC = {
//OSC Protocol
spat_address : '/spat',
traj_address : '/traj',
pos_address : '/traj/posxyz',
trajpoint_address : '/traj/trajpoint',
traj_new_address : '/traj/newtraj',
traj_suffix : '/traj',
play_address : '/traj/play',
record_address : '/traj/record',
orientation_address : '/traj/orientation',
draw_address : '/traj/draw',
// SENDING MESSAGES
isInteger : function(value) {
if ((undefined === value) || (null === value)) {
return false;
}
return value % 1 == 0;
},
getTypeForElement : function(elem){
var type = typeof elem;
switch (type) {
case 'string':
return 's';
break;
case 'boolean':
return 'b';
break;
case 'number':
return Traj.OSC.isInteger(elem) ? 'i' : 'f';;
break;
default:
return 'f';
}
},
getTypesForArgs : function(args){
var tt = '';
if(typeof args === 'string')
return 's';
for(var i = 0; i < args.length; i++) {
tt += Traj.OSC.getTypeForElement(args[i]);
}
return tt;
},
sendMessage : function(address, msg) {
var tt = Traj.OSC.getTypesForArgs(msg);
Interface.OSC.send(address, tt, msg );
},
//RECEIVING MESSAGE
processMessages : function(msg) {
var address = msg.address;
var split = address.split('/');
//test for the correct namespace
console.log(split[1]);
if(split[1].indexOf("spat") != -1 ){
console.log(split[2]);
if(split[2].indexOf("source") != -1){
var nb = (split[2].replace("source",""));
var sourcenb = Number(nb);
var curve = new TimedCurve();
for (var k =0; k<msg.parameters.length/4;k++) {
curve.addTimedPoint(msg.parameters[4*k],msg.parameters[4*k+1],msg.parameters[4*k+2],msg.parameters[4*k+3]);
}
var manager = Traj.TrajManager;
manager.trajectories.push(curve);
curve.changeSource(sourcenb); // care : to change the source of a curve, the curve need to be already pushed in trajectories
Traj.View.traj_repaint();
//manager.selectCurve(manager.trajectories.length-1);
}
}
},
//FACILITIES
sendPosxyzMsg : function(msg) {
Traj.OSC.sendMessage(Traj.OSC.pos_address, msg);
},
sendSpatMessage: function(msg){
Traj.OSC.sendMessage(Traj.OSC.spat_address,msg);
},
sendTrajPoint : function(msg) {
Traj.OSC.sendMessage(Traj.OSC.trajpoint_address, msg);
},
//OLD VERSION
// sendTraj : function(msg) {
// Traj.OSC.sendMessage(Traj.OSC.traj_address, msg);
//},
sendTraj : function(number, msg) {
Traj.OSC.sendMessage(Traj.OSC.traj_address + "/source" + number + Traj.OSC.traj_suffix, msg);
},
sendPlay : function(msg) {
Traj.OSC.sendMessage(Traj.OSC.play_address, msg);
},
sendRecord : function(msg) {
Traj.OSC.sendMessage(Traj.OSC.record_address, msg);
},
sendOrientation : function(msg){
Traj.OSC.sendMessage(Traj.OSC.orientation_address, msg);
},
sendNewTraj : function(msg){
Traj.OSC.sendMessage(Traj.OSC.traj_new_address, msg);
},
sendDraw : function(msg) {
Traj.OSC.sendMessage(Traj.OSC.draw_address, msg);
},
streamCurveLastPoint : function(curve) {
var last_index = curve.X.length - 1;
this.streamCurvePoint(curve, last_index);
},
streamNewCurve : function(index){
Traj.OSC.sendNewTraj(index);
},
streamCurvePoint : function(curve, index) {
//ussing Spat formatting source ID xyz x y z
Traj.OSC.sendSpatMessage( ['source', curve.sourceNumber, 'xyz',curve.X[index], curve.Y[index], curve.Z[index]]);
//Traj.OSC.sendPosxyzMsg([curve.sourceNumber, curve.X[index], curve.Y[index], curve.Z[index], curve.t[index]]);
},
sendCurveOSC : function(curve, id) {
var oscPoint;
for (var i = 0; i < curve.X.length; i++) {
oscPoint = [];
oscPoint.push(id);
oscPoint.push(curve.X[i]);
oscPoint.push(curve.Y[i]);
oscPoint.push(curve.Z[i]);
oscPoint.push(curve.t[i]);
Traj.OSC.sendTrajPoint(oscPoint);
}
},
sendCurveSingleMessageOSC : function(curve, id) {
var args = [];
for (var i = 0; i < curve.X.length; i++) {
args.push(curve.X[i]);
args.push(curve.Y[i]);
args.push(curve.Z[i]);
args.push(curve.t[i]);
}
Traj.OSC.sendTraj(curve.sourceNumber, args);
},
saveAntescofo : function (curve, id, sessionID){
var args = [];
args.push(1+ sessionID); //to have same labels ias in the UI
args.push(id);
for (var i = 0; i < curve.X.length; i++) {
args.push(curve.X[i]);
args.push(curve.Y[i]);
args.push(curve.Z[i]);
args.push(curve.t[i]);
}
Traj.OSC.sendMessage("/savenimtraj", args);
}
};
////////////////////////////////////////////////////////////
///////////////////////// OSC /////////////////////////////
Trajectoires.OSC = {
Traj.OSC = {
//OSC Protocol
root_address : '/spat',
spat_address : '/spat',
traj_address : '/traj',
pos_address : '/traj/posxyz',
trajpoint_address : '/traj/trajpoint',
traj_new_address : '/traj/newtraj',
traj_suffix : '/traj',
pos_address : '/spat/posxyz',
trajpoint_address : '/spat/trajpoint',
traj_address : '/spat/traj',
traj_new_address : '/spat/newtraj',
play_address : '/spat/play',
record_address : '/spat/record',
orientation_address : '/spat/orientation',
draw_address : '/spat/draw',
play_address : '/traj/play',
record_address : '/traj/record',
orientation_address : '/traj/orientation',
draw_address : '/traj/draw',
// SENDING MESSAGES
isInteger : function(value) {
......@@ -31,7 +31,7 @@ Trajectoires.OSC = {
return 'b';
break;
case 'number':
return Trajectoires.OSC.isInteger(elem) ? 'i' : 'f';;
return Traj.OSC.isInteger(elem) ? 'i' : 'f';;
break;
default:
return 'f';
......@@ -45,13 +45,13 @@ Trajectoires.OSC = {
return 's';
for(var i = 0; i < args.length; i++) {
tt += Trajectoires.OSC.getTypeForElement(args[i]);
tt += Traj.OSC.getTypeForElement(args[i]);
}
return tt;
},
sendMessage : function(address, msg) {
var tt = Trajectoires.OSC.getTypesForArgs(msg);
var tt = Traj.OSC.getTypesForArgs(msg);
Interface.OSC.send(address, tt, msg );
},
......@@ -71,10 +71,10 @@ Trajectoires.OSC = {
for (var k =0; k<msg.parameters.length/4;k++) {
curve.addTimedPoint(msg.parameters[4*k],msg.parameters[4*k+1],msg.parameters[4*k+2],msg.parameters[4*k+3]);
}
var manager = Trajectoires.TrajManager;
var manager = Traj.Manager;
manager.trajectories.push(curve);
curve.changeSource(sourcenb); // care : to change the source of a curve, the curve need to be already pushed in trajectories
Trajectoires.View.traj_repaint();
Traj.View.traj_repaint();
//manager.selectCurve(manager.trajectories.length-1);
}
}
......@@ -83,40 +83,44 @@ Trajectoires.OSC = {
//FACILITIES
sendPosxyzMsg : function(msg) {
Trajectoires.OSC.sendMessage(Trajectoires.OSC.pos_address, msg);
Traj.OSC.sendMessage(Traj.OSC.pos_address, msg);
},
sendSpatMessage: function(msg){
Traj.OSC.sendMessage(Traj.OSC.spat_address,msg);
},
sendTrajPoint : function(msg) {
Trajectoires.OSC.sendMessage(Trajectoires.OSC.trajpoint_address, msg);
Traj.OSC.sendMessage(Traj.OSC.trajpoint_address, msg);
},
//OLD VERSION
// sendTraj : function(msg) {
// Trajectoires.OSC.sendMessage(Trajectoires.OSC.traj_address, msg);
// Traj.OSC.sendMessage(Traj.OSC.traj_address, msg);
//},
sendTraj : function(number, msg) {
Trajectoires.OSC.sendMessage(Trajectoires.OSC.root_address + "/source" + number + Trajectoires.OSC.traj_suffix, msg);
Traj.OSC.sendMessage(Traj.OSC.traj_address + "/source" + number + Traj.OSC.traj_suffix, msg);
},
sendPlay : function(msg) {
Trajectoires.OSC.sendMessage(Trajectoires.OSC.play_address, msg);
Traj.OSC.sendMessage(Traj.OSC.play_address, msg);
},
sendRecord : function(msg) {
Trajectoires.OSC.sendMessage(Trajectoires.OSC.record_address, msg);
Traj.OSC.sendMessage(Traj.OSC.record_address, msg);
},
sendOrientation : function(msg){
Trajectoires.OSC.sendMessage(Trajectoires.OSC.orientation_address, msg);
Traj.OSC.sendMessage(Traj.OSC.orientation_address, msg);
},
sendNewTraj : function(msg){
Trajectoires.OSC.sendMessage(Trajectoires.OSC.traj_new_address, msg);
Traj.OSC.sendMessage(Traj.OSC.traj_new_address, msg);
},
sendDraw : function(msg) {
Trajectoires.OSC.sendMessage(Trajectoires.OSC.draw_address, msg);
Traj.OSC.sendMessage(Traj.OSC.draw_address, msg);
},
streamCurveLastPoint : function(curve) {
......@@ -125,11 +129,13 @@ Trajectoires.OSC = {
},
streamNewCurve : function(index){
Trajectoires.OSC.sendNewTraj(index);
Traj.OSC.sendNewTraj(index);
},
streamCurvePoint : function(curve, index) {
Trajectoires.OSC.sendPosxyzMsg([curve.sourceNumber, curve.X[index], curve.Y[index], curve.Z[index], curve.t[index]]);
//ussing Spat formatting source ID xyz x y z
Traj.OSC.sendSpatMessage( ['source', curve.sourceNumber, 'xyz',curve.X[index], curve.Y[index], curve.Z[index]]);
//Traj.OSC.sendPosxyzMsg([curve.sourceNumber, curve.X[index], curve.Y[index], curve.Z[index], curve.t[index]]);
},
sendCurveOSC : function(curve, id) {
......@@ -141,7 +147,7 @@ Trajectoires.OSC = {
oscPoint.push(curve.Y[i]);
oscPoint.push(curve.Z[i]);
oscPoint.push(curve.t[i]);
Trajectoires.OSC.sendTrajPoint(oscPoint);
Traj.OSC.sendTrajPoint(oscPoint);
}
},
......@@ -153,23 +159,8 @@ Trajectoires.OSC = {
args.push(curve.Z[i]);
args.push(curve.t[i]);
}
Trajectoires.OSC.sendTraj(curve.sourceNumber, args);
},
//PREVIOUS VERSION
// sendCurveSingleMessageOSC : function(curve, id) {
// var args = [];
// args.push(id);
// for (var i = 0; i < curve.X.length; i++) {
// args.push(curve.X[i]);
// args.push(curve.Y[i]);
// args.push(curve.Z[i]);
// args.push(curve.t[i]);
// }
// Trajectoires.OSC.sendTraj(args);
//
//},
Traj.OSC.sendTraj(curve.sourceNumber, args);
},
saveAntescofo : function (curve, id, sessionID){
var args = [];
......@@ -181,7 +172,7 @@ Trajectoires.OSC = {
args.push(curve.Z[i]);
args.push(curve.t[i]);
}
Trajectoires.OSC.sendMessage("/savenimtraj", args);
Traj.OSC.sendMessage("/savenimtraj", args);
}
};
......
......@@ -8,10 +8,6 @@ Traj.Player = {
play : function() {
// Traj.TrajManager.multiPlayChanged();
// if (Traj.TrajManager.playedCurves.length > 1) {
// this.playAllCurve();
// } else { this.playCurrentCurve();}
this.playCurrentCurve();
},
......@@ -20,7 +16,7 @@ Traj.Player = {
Traj.Player.isPlaying = true;
playButton.innerHTML = 'Pause';
var curve = Traj.TrajManager.trajectories[curveIdx],
var curve = Traj.Manager.trajectories[curveIdx],
lengthTime = curve.lengthTime(),
pointCoord,
x = curve.X[0],
......@@ -36,7 +32,7 @@ Traj.Player = {
var begin = new Date().getTime();
var timeSliderPosition = Traj.EventManager.getSliderValue(); // check if the slider is not at the end (or mostly)
var timeSliderPosition = Traj.Events.getSliderValue(); // check if the slider is not at the end (or mostly)
if (timeSliderPosition < lengthTime-100) { // if it is not, change the begin time (if it is, leave it to restart at the begining)
begin -= timeSliderPosition;
}
......@@ -52,7 +48,7 @@ Traj.Player = {
// actualize time slider
//if (counter ==1) {
Traj.EventManager.setSliderValue(now);
Traj.Events.setSliderValue(now);
//}
//find idx
idx = Traj.Utils.findPointIdx(now,curve);
......@@ -80,7 +76,7 @@ Traj.Player = {
Traj.Player.repaintCurve(curve,idx,idxOld); // paint large curve
idxOld = idx;
Traj.EventManager.setZSliderValue(z);
Traj.Events.setZSliderValue(z);
}
// test if the curve is ended and if loop is activated
......@@ -106,7 +102,7 @@ Traj.Player = {
Traj.Player.isPlaying = true;
var curvesIdx = Traj.TrajManager.playedCurves; // A definir dans TrajManager
var curvesIdx = Traj.Manager.playedCurves; // A definir dans Manager
var curveArray = [],
lengthTimeArray = [],
......@@ -120,7 +116,7 @@ Traj.Player = {
counter = 0;
for (var k=0; k<curvesIdx.length;k++) {
var curve = Traj.TrajManager.trajectories[curvesIdx[k]],
var curve = Traj.Manager.trajectories[curvesIdx[k]],
lengthTime = curve.lengthTime(),
pointCoord,
x = curve.X[0],
......@@ -146,14 +142,14 @@ Traj.Player = {
var begin = new Date().getTime();
// check si le curseur du player est à la fin ou non et ajuste le begin de lecture
var timeSliderPosition = Traj.EventManager.getSliderValue();
var timeSliderPosition = Traj.Events.getSliderValue();
if (timeSliderPosition < lengthTime - 100) {
begin -= timeSliderPosition;
}
// selection de la courbe la plus longue (pour le timeSlider)
var longestCurveIdx = Traj.TrajManager.longestCurveIdxOfPlayedCurve();
Traj.TrajManager.selectCurve(longestCurveIdx);
var longestCurveIdx = Traj.Manager.longestCurveIdxOfPlayedCurve();
Traj.Manager.selectCurve(longestCurveIdx);
var now = new Date().getTime();
for (var k=0; k<curvesIdx.length;k++) {
pointCoordArray[k] = Traj.Utils.interpolate(now - begin,curveArray[k]);
......@@ -170,7 +166,7 @@ Traj.Player = {
Traj.View.dyn_repaint();
// actualize time slider
Traj.EventManager.setSliderValue(now);
Traj.Events.setSliderValue(now);
for (var k=0; k<pointCoordArray.length;k++) {
if (lengthTimeArray[k]) {
......@@ -227,8 +223,8 @@ Traj.Player = {
playCurrentCurve : function() {
var curveIdx = Traj.TrajManager.currentCurveIndex,
curve = Traj.TrajManager.trajectories[curveIdx];
var curveIdx = Traj.Manager.currentCurveIndex,
curve = Traj.Manager.trajectories[curveIdx];
Traj.View.dyn_ctx.lineWidth = 5;
Traj.View.dyn_ctx.fillStyle = curve.color;
......
......@@ -38,7 +38,7 @@ Traj.Session = {
loadTrajSession : function(idx){
var newTrajectories = this.dict[idx],
trajectories = this.makeTrajectoriesArrayFromDict(newTrajectories);
Traj.TrajManager.setTrajectories(trajectories);
Traj.Manager.setTrajectories(trajectories);
},
loadSelectedSession : function() {
......@@ -53,12 +53,12 @@ Traj.Session = {