0
0
mirror of https://github.com/Doodle3D/doodle3d-client.git synced 2024-11-16 15:47:55 +01:00
doodle3d-client/js/buttonbehaviors.js

381 lines
10 KiB
JavaScript
Raw Normal View History

2013-07-26 22:39:28 +02:00
var btnMoveUpInterval;
var btnMoveDownInterval;
var btnTwistLeftInterval;
var btnTwistRightInterval;
var twistIncrement = Math.PI/1800;
var btnOopsInterval;
var btnNew, btnPrevious, btnNext;
2013-07-26 22:39:28 +02:00
var btnOops, btnStop, btnClear;
var btnMoveUp, btnMoveDown, btnTwistLeft, btnTwistRight;
var btnInfo, btnSettings;
var btnDebug; // debug
var displayTempEnabled = false;
var IDLE_STATE = "idle";
var PRINTING_STATE = "printing";
var state = IDLE_STATE;
var prevState = state;
2013-07-26 22:39:28 +02:00
function initButtonBehavior() {
console.log("f:initButtonBehavior >> btnNew = " + btnNew);
btnClear= $("#btnClear");
btnOops = $("#btnOops");
btnMoveUp = $("#btnMoveUp");
btnMoveDown = $("#btnMoveDown");
btnTwistLeft = $("#btnTwistLeft");
btnTwistRight = $("#btnTwistRight");
btnInfo = $("#btnInfo");
btnSettings = $("#btnSettings");
btnNew = $("#btnNew");
btnPrint= $("#btnPrint");
btnStop = $("#btnStop");
displayTemp = $("#displayTemp");
2013-07-26 22:39:28 +02:00
// btnPrevious = $("#btnPrevious");
// btnNext = $("#btnNext");
//debug
btnDebug = $(".debugBtn");
/*if (!btnNew.addEventListener) {
2013-07-26 22:39:28 +02:00
btnNew.attachEvent('onmousedown',clearDoodle);
btnNew.attachEvent('ontouchstart',clearDoodle);
btnPrint.attachEvent('onmousedown',print);
btnPrint.attachEvent('ontouchstart',print);
// btnPrevious.attachEvent('onmousedown',prevDoodle);
// btnPrevious.attachEvent('ontouchstart',prevDoodle);
// btnNext.attachEvent('onmousedown',nextDoodle);
// btnNext.attachEvent('ontouchstart',nextDoodle);
} else {
btnNew.addEventListener('mousedown',clearDoodle,false);
btnNew.addEventListener('touchstart',clearDoodle,false);
btnPrint.addEventListener('mousedown',print,false);
btnPrint.addEventListener('touchstart',print,false);
// btnPrevious.addEventListener('mousedown',prevDoodle,false);
// btnPrevious.addEventListener('touchstart',prevDoodle,false);
// btnNext.addEventListener('mousedown',nextDoodle,false);
// btnNext.addEventListener('touchstart',nextDoodle,false);
}*/
btnNew.bind('touchstart mousedown',clearDoodle);
btnPrint.bind('touchstart mousedown',print);
// not using these at the moment
$("#btnPrevious").css("opacity", "0.3");
$("#btnNext").css("opacity", "0.3");
$("#btnSave").css("opacity", "0.3");
$("#btnInfo").css("opacity", "0.3");
2013-07-26 22:39:28 +02:00
btnClear.click(function(e) {
e.preventDefault();
// console.log("clear");
clearDoodle();
});
// btnPrevious.mouseup(function(e) {
// e.preventDefault();
// console.log("btnPrevious");
// })
// btnPrevious.bind("touchend", function(e) {
// e.preventDefault();
// console.log("btnPrevious");
// })
// btnNext.mouseup(function(e) {
// e.preventDefault();
// console.log("btnNext");
// })
// btnNext.bind("touchend", function(e) {
// e.preventDefault();
// console.log("btnPrevious");
// })
function startOops(e) {
2013-07-26 22:39:28 +02:00
// console.log("btnOops mouse down");
e.preventDefault();
2013-07-26 22:39:28 +02:00
btnOopsInterval = setInterval( function() {
oopsUndo();
}, 1000/50);
}
function stopOops(e) {
2013-07-26 22:39:28 +02:00
// console.log("btnOops mouse up");
e.preventDefault();
2013-07-26 22:39:28 +02:00
clearInterval(btnOopsInterval);
}
btnOops.on('touchstart', function(e) { startOops(e); });
btnOops.on('touchend', function(e) { stopOops(e); });
btnOops.mousedown(function(e) { startOops(e); });
btnOops.mouseup(function(e) { stopOops(e); });
2013-07-26 22:39:28 +02:00
function startMoveUp(e) {
2013-07-26 22:39:28 +02:00
e.preventDefault();
// console.log("btnMoveUp mouse down");
previewUp(true);
2013-07-26 22:39:28 +02:00
clearInterval(btnMoveUpInterval);
btnMoveUpInterval = setInterval( function() {
previewUp(true);
2013-07-26 22:39:28 +02:00
}, 1000/30);
}
function stopMoveUp(e) {
2013-07-26 22:39:28 +02:00
e.preventDefault();
console.log("btnMoveUp mouse up");
2013-07-26 22:39:28 +02:00
clearInterval(btnMoveUpInterval);
previewUp();
}
btnMoveUp.mousedown(function(e) { startMoveUp(e) });
btnMoveUp.mouseup(function(e) { stopMoveUp(e) });
btnMoveUp.on('touchstart', function(e) { startMoveUp(e) });
btnMoveUp.on('touchend', function(e) { stopMoveUp(e) });
2013-07-26 22:39:28 +02:00
function startMoveDown(e) {
2013-07-26 22:39:28 +02:00
e.preventDefault();
// console.log("btnMoveDown mouse down");
previewDown(true);
2013-07-26 22:39:28 +02:00
clearInterval(btnMoveDownInterval);
btnMoveDownInterval = setInterval( function() {
previewDown(true);
2013-07-26 22:39:28 +02:00
}, 1000/30);
}
function stopMoveDown(e) {
2013-07-26 22:39:28 +02:00
e.preventDefault();
console.log("btnMoveDown mouse up");
2013-07-26 22:39:28 +02:00
clearInterval(btnMoveDownInterval);
previewDown();
}
btnMoveDown.mousedown(function(e) { startMoveDown(e) });
btnMoveDown.mouseup(function(e) { stopMoveDown(e) });
btnMoveDown.on('touchstart', function(e) { startMoveDown(e) });
btnMoveDown.on('touchend', function(e) { stopMoveDown(e) });
2013-07-26 22:39:28 +02:00
function startTwistLeft(e) {
2013-07-26 22:39:28 +02:00
e.preventDefault();
// console.log("btnTwistLeft mouse down");
previewTwistLeft(true);
2013-07-26 22:39:28 +02:00
clearInterval(btnTwistLeftInterval);
btnTwistLeftInterval = setInterval( function() {
previewTwistLeft(true);
2013-07-26 22:39:28 +02:00
}, 1000/30);
}
function stopTwistLeft(e) {
2013-07-26 22:39:28 +02:00
e.preventDefault();
// console.log("btnTwistLeft mouse up");
clearInterval(btnTwistLeftInterval);
previewTwistLeft();
}
btnTwistLeft.mousedown(function(e) { startTwistLeft(e) });
btnTwistLeft.mouseup(function(e) { stopTwistLeft(e) });
btnTwistLeft.on('touchstart', function(e) { startTwistLeft(e) });
btnTwistLeft.on('touchend', function(e) { stopTwistLeft(e) });
2013-07-26 22:39:28 +02:00
function startTwistRight(e) {
2013-07-26 22:39:28 +02:00
e.preventDefault();
// console.log("btnTwistRight mouse down");
previewTwistRight(true);
2013-07-26 22:39:28 +02:00
clearInterval(btnTwistRightInterval);
btnTwistRightInterval = setInterval( function() {
previewTwistRight(true);
2013-07-26 22:39:28 +02:00
}, 1000/30);
}
function stopTwistRight(e) {
2013-07-26 22:39:28 +02:00
e.preventDefault();
// console.log("btnTwistRight mouse up");
clearInterval(btnTwistRightInterval);
previewTwistLeft();
}
btnTwistRight.mousedown(function(e) { startTwistRight(e) });
btnTwistRight.mouseup(function(e) { stopTwistRight(e) });
btnTwistRight.on('touchstart', function(e) { startTwistRight(e) });
btnTwistRight.on('touchend', function(e) { stopTwistRight(e) });
2013-07-26 22:39:28 +02:00
/*function openSettings() {
2013-07-28 02:46:32 +02:00
console.log("f:openSettings()");
$("#contentOverlay").fadeIn(1000, function() {
loadSettings();
});
}*/
btnSettings.bind('touchstart mousedown',function () {
//e.preventDefault();
//console.log("btnSettings clicked");
2013-08-27 15:34:28 +02:00
settingsWindow.showSettings();
2013-07-26 22:39:28 +02:00
});
2013-07-28 02:46:32 +02:00
// btnSettings.on('touchend', function(e) {
// e.preventDefault();
// console.log("btnSettings touchend");
// });
2013-07-26 22:39:28 +02:00
btnInfo.mouseup(function(e) {
e.preventDefault();
console.log("btnInfo mouse up");
});
// DEBUG
// $(".agentInfo").css("display", "none");
btnDebug.click(function(e) {
console.log("debugClick");
$(".agentInfo").toggleClass("agentInfoToggle");
e.preventDefault();
})
btnStop.bind('touchstart mousedown',stopPrint);
}
function stopPrint() {
console.log("f:stopPrint() >> sendPrintCommands = " + sendPrintCommands);
if (sendPrintCommands) printer.stop();
setState(IDLE_STATE);
2013-07-26 22:39:28 +02:00
}
function prevDoodle(e) {
console.log("f:prevDoodle()");
console.log("f:prevDoodle()");
2013-07-26 22:39:28 +02:00
}
function nextDoodle(e) {
console.log("f:nextDoodle()");
}
function print(e) {
console.log("f:print() >> sendPrintCommands = " + sendPrintCommands);
if (sendPrintCommands) {
$("#textdump").text("");
if (_points.length > 2) {
setState(PRINTING_STATE);
var gcode = generate_gcode();
//startPrint(gencode);
printer.print(gcode);
// console.log("");
// console.log("");
// console.log("-------------------------------------------------");
// console.log("generated gcode:");
// console.log(gencode);
// console.log("-------------------------------------------------");
// console.log("");
// console.log("");
// console.log("");
if (debugMode) $("#textdump").text(gcode.join("\n"));
// copyToClipboard(gencode);
//*/
} else {
console.log("f:print >> not enough points!");
}
// $.post("/doodle3d.of", { data:output }, function(data) {
// btnPrint.disabled = false;
// });
} else {
console.log("sendPrintCommands is false: not sending print command to 3dprinter");
}
2013-07-26 22:39:28 +02:00
}
function clearMainView() {
// console.log("f:clearMainView()");
ctx.save();
ctx.clearRect(0,0,canvas.width, canvas.height);
ctx.restore();
}
function clearPreview() {
// console.log("f:clearPreview()");
previewCtx.save();
previewCtx.clearRect(0,0,canvas.width, canvas.height);
previewCtx.restore();
}
function oopsUndo() {
// console.log("f:oopsUndo()");
_points.pop();
redrawDoodle();
2013-07-26 22:39:28 +02:00
redrawPreview();
}
function previewUp(redrawLess) {
2013-07-26 22:39:28 +02:00
// console.log("f:previewUp()");
if (numLayers < 100) {
numLayers++;
}
// redrawPreview(redrawLess);
redrawRenderedPreview(redrawLess);
2013-07-26 22:39:28 +02:00
}
function previewDown(redrawLess) {
2013-07-26 22:39:28 +02:00
// console.log("f:previewDown()");
if (numLayers > 2) {
numLayers--;
}
// redrawPreview(redrawLess);
redrawRenderedPreview(redrawLess);
2013-07-26 22:39:28 +02:00
}
function previewTwistLeft(redrawLess) {
if (redrawLess == undefined) redrawLess = false;
2013-07-26 22:39:28 +02:00
// console.log("f:previewTwistLeft()");
// if (rStep < Math.PI) {
rStep -= twistIncrement;
// }
// redrawPreview(redrawLess);
redrawRenderedPreview(redrawLess);
2013-07-26 22:39:28 +02:00
}
function previewTwistRight(redrawLess) {
2013-07-26 22:39:28 +02:00
// console.log("f:previewTwistRight()");
// if (rStep < Math.PI) {
rStep += twistIncrement;
// }
// redrawPreview(redrawLess);
redrawRenderedPreview(redrawLess);
}
function update() {
if(!displayTempEnabled && printer.alive) {
displayTemp.show();
displayTempEnabled = true;
} else if(displayTempEnabled && !printer.alive) {
displayTemp.hide();
displayTempEnabled = false;
}
if(displayTempEnabled) {
displayTemp.text(printer.temperature+"/"+printer.targetTemperature);
}
var btnPrint= $("#btnPrint");
setState(printer.printing? PRINTING_STATE : IDLE_STATE);
}
function setState(newState) {
if(newState == state) return;
switch(newState) {
case IDLE_STATE:
btnPrint.removeClass("disabled"); // enable print button
btnStop.addClass("disabled"); // disable stop button
btnPrint.bind('touchstart mousedown',print);
break;
case PRINTING_STATE:
btnPrint.addClass("disabled"); // disable print button
btnStop.removeClass("disabled"); // enable stop button
btnPrint.unbind('touchstart mousedown');
break;
}
prevState = state;
state = newState;
2013-07-26 22:39:28 +02:00
}