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

388 lines
11 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
2013-07-26 22:39:28 +02:00
var state;
var prevState;
2013-10-11 16:17:58 +02:00
var hasControl;
var gcodeGenerateDelayer;
var gcodeGenerateDelay = 50;
2013-07-26 22:39:28 +02:00
function initButtonBehavior() {
console.log("f:initButtonBehavior");
2013-07-26 22:39:28 +02:00
// btnClear= $(".btnClear");
btnOops = $(".btnOops");
2013-07-26 22:39:28 +02:00
btnMoveUp = $("#btnMoveUp");
btnMoveDown = $("#btnMoveDown");
btnTwistLeft = $("#btnTwistLeft");
btnTwistRight = $("#btnTwistRight");
btnInfo = $(".btnInfo");
btnSettings = $(".btnSettings");
btnNew = $(".btnNew");
btnPrint= $(".btnPrint");
btnStop = $(".btnStop");
2013-07-26 22:39:28 +02:00
btnPrevious = $(".btnPrevious");
btnNext = $(".btnNext");
2013-07-26 22:39:28 +02:00
//debug
//btnDebug = $(".debugBtn");
2013-07-26 22:39:28 +02:00
btnNew.on('touchstart mousedown', clearDoodle);
btnPrint.on('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();
// });
2013-07-26 22:39:28 +02:00
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);
2013-08-30 09:52:02 +02:00
previewTwistRight();
}
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");
helpTours.startTour(helpTours.WELCOMETOUR);
2013-07-26 22:39:28 +02:00
});
// DEBUG
/*
2013-07-26 22:39:28 +02:00
// $(".agentInfo").css("display", "none");
btnDebug.click(function(e) {
console.log("debugClick");
$(".agentInfo").toggleClass("agentInfoToggle");
e.preventDefault();
})
//*/
//btnStop.on('touchstart mousedown',stopPrint);
}
function stopPrint() {
console.log("f:stopPrint() >> sendPrintCommands = " + sendPrintCommands);
2013-10-16 18:32:24 +02:00
//if (!confirm("Weet je zeker dat je huidige print wilt stoppen?")) return;
if (sendPrintCommands) printer.stop();
2013-10-14 17:40:56 +02:00
//setState(Printer.STOPPING_STATE,printer.hasControl);
printer.overruleState(Printer.STOPPING_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);
//$(".btnPrint").css("display","none");
if (_points.length > 2) {
//setState(Printer.BUFFERING_STATE,printer.hasControl);
2013-10-14 17:40:56 +02:00
printer.overruleState(Printer.BUFFERING_STATE);
2013-10-16 18:32:24 +02:00
btnStop.css("display","none"); // hack
// we put the gcode generation in a little delay
// so that for example the print button is disabled right away
clearTimeout(gcodeGenerateDelayer);
gcodeGenerateDelayer = setTimeout(function() {
var gcode = generate_gcode();
if (sendPrintCommands) {
if(gcode.length > 0) {
printer.print(gcode);
} else {
printer.overruleState(Printer.IDLE_STATE);
printer.startStatusCheckInterval();
}
} else {
console.log("sendPrintCommands is false: not sending print command to 3dprinter");
}
if (debugMode) {
$("#textdump").text("");
$("#textdump").text(gcode.join("\n"));
}
}, gcodeGenerateDelay);
} else {
console.log("f:print >> not enough points!");
}
//alert("Je tekening zal nu geprint worden");
//$(".btnPrint").css("display","block");
// $.post("/doodle3d.of", { data:output }, function(data) {
// btnPrint.disabled = false;
// });
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 resetPreview() {
// console.log("f:resetPreview()");
// clear preview canvas
2013-07-26 22:39:28 +02:00
previewCtx.save();
previewCtx.clearRect(0,0,canvas.width, canvas.height);
previewCtx.restore();
// reset height and rotation to default values
numLayers = previewDefaults.numLayers; // current number of preview layers
rStep = previewDefaults.rotation; // Math.PI/180; //Math.PI/40; //
2013-07-26 22:39:28 +02:00
}
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 < maxNumLayers) {
2013-07-26 22:39:28 +02:00
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 > minNumLayers) {
2013-07-26 22:39:28 +02:00
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 > -previewRotationLimit) 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 < previewRotationLimit) rStep += twistIncrement;
// redrawPreview(redrawLess);
redrawRenderedPreview(redrawLess);
}
function update() {
2013-10-11 16:17:58 +02:00
setState(printer.state,printer.hasControl);
thermometer.update(printer.temperature, printer.targetTemperature);
2013-10-17 14:40:33 +02:00
progressbar.update(printer.currentLine, printer.totalLines);
}
function setState(newState,newHasControl) {
2013-10-11 16:17:58 +02:00
if(newState == state && newHasControl == hasControl) return;
prevState = state;
console.log("setState: ",prevState," > ",newState," ( ",newHasControl,")");
setDebugText("State: "+newState);
// print button
2013-10-11 16:17:58 +02:00
var printEnabled = (newState == Printer.IDLE_STATE && newHasControl);
if(printEnabled) {
btnPrint.removeClass("disabled"); // enable print button
2013-10-11 15:33:51 +02:00
btnPrint.unbind('touchstart mousedown');
btnPrint.bind('touchstart mousedown',print);
2013-10-11 16:17:58 +02:00
} else {
btnPrint.addClass("disabled"); // disable print button
btnPrint.unbind('touchstart mousedown');
}
// stop button
2013-10-11 16:17:58 +02:00
var stopEnabled = ((newState == Printer.PRINTING_STATE || newState == Printer.BUFFERING_STATE) && newHasControl);
if(stopEnabled) {
btnStop.removeClass("disabled");
btnStop.unbind('touchstart mousedown');
btnStop.bind('touchstart mousedown',stopPrint);
2013-10-11 16:17:58 +02:00
} else {
btnStop.addClass("disabled");
btnStop.unbind('touchstart mousedown');
}
// thermometer
switch(newState) {
case Printer.IDLE_STATE:
case Printer.BUFFERING_STATE:
case Printer.PRINTING_STATE:
case Printer.STOPPING_STATE:
thermometer.show();
break;
default:
thermometer.hide();
break;
}
// progress indicator
switch(newState) {
case Printer.PRINTING_STATE:
2013-10-17 14:40:33 +02:00
progressbar.show();
break;
default:
2013-10-17 14:40:33 +02:00
progressbar.hide();
break;
}
if(newState == Printer.WIFIBOX_DISCONNECTED_STATE) {
message.set("Lost connection to WiFi box",Message.ERROR);
} else if(prevState == Printer.WIFIBOX_DISCONNECTED_STATE) {
message.set("Connected to WiFi box",Message.INFO,true);
} else if(newState == Printer.DISCONNECTED_STATE) {
message.set("Printer disconnected",Message.WARNING,true);
} else if(prevState == Printer.DISCONNECTED_STATE && newState == Printer.IDLE_STATE ||
prevState == Printer.UNKNOWN_STATE && newState == Printer.IDLE_STATE) {
message.set("Printer connected",Message.INFO,true);
}
state = newState;
2013-10-11 16:17:58 +02:00
hasControl = newHasControl;
}