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

446 lines
12 KiB
JavaScript
Raw Normal View History

2013-12-20 16:31:41 +01:00
/*
* This file is part of the Doodle3D project (http://doodle3d.com).
*
* Copyright (c) 2013, Doodle3D
* This software is licensed under the terms of the GNU GPL v2 or later.
* See file LICENSE.txt or visit http://www.gnu.org/licenses/gpl.html for full license details.
*/
2013-07-26 22:39:28 +02:00
var twistIncrement = Math.PI/1800;
2014-01-13 16:38:26 +01:00
var btnNew, btnPrevious, btnNext, btnOops, btnStop, btnInfo;
var btnSettings, btnWordArt;
var btnToggleEdit, buttonGroupEdit, btnZoom, btnMove, btnRotate;
2014-01-16 16:07:40 +01:00
var btnToggleVerticalShapes, btnHeight, btnTwist, btnShape, btnConv, btnStraight, btnSine, btnDiv;
2014-01-13 16:38:26 +01:00
var buttonGroupAdd, popupWordArt;
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
2014-01-09 17:05:03 +01:00
$(".btn").Button(); //initalizes all buttons
2014-01-10 17:21:43 +01:00
btnOops = $("#btnOops");
btnInfo = $("#btnInfo");
btnSettings = $("#btnSettings");
btnNew = $("#btnNew");
btnPrint= $("#btnPrint");
btnStop = $("#btnStop");
btnPrevious = $("#btnPrevious");
btnNext = $("#btnNext");
btnSave = $("#btnSave");
2014-01-16 16:07:40 +01:00
buttonGroupAdd = $("#buttonGroupAdd");
btnShape = $("#btnShape");
btnWordArt = $("#btnWordArt");
popupWordArt = $("#popupWordArt");
popupShape = $("#popupShape");
popupMask = $("#popupMask");
logoPanel = $("#logopanel");
btnToggleEdit = $("#btnToggleEdit");
buttonGroupEdit = $("#buttonGroupEdit");
2014-01-10 17:21:43 +01:00
btnZoom = $("#btnZoom");
btnMove = $("#btnMove");
btnRotate = $("#btnRotate");
2014-01-16 16:07:40 +01:00
btnToggleVerticalShapes = $("#btnToggleVerticalShapes");
buttonGroupVerticalShapes = $("#buttonGroupVerticalShapes");
btnHeight = $("#btnHeight");
btnTwist = $("#btnTwist");
2014-01-10 17:21:43 +01:00
btnStraight = $("#btnStraight");
btnDiv = $("#btnDiv");
btnConv = $("#btnConv");
btnSine = $("#btnSine");
btnAdd = $("#btnAdd");
2014-01-16 12:16:09 +01:00
logoPanel.on("onButtonClick", onLogo);
2014-01-09 17:05:03 +01:00
btnNew.on("onButtonClick", onBtnNew);
2014-01-13 18:04:51 +01:00
btnAdd.on("onButtonClick", onBtnAdd);
2014-01-09 17:05:03 +01:00
btnWordArt.on("onButtonClick", onBtnWordArt);
2014-01-13 18:04:51 +01:00
btnShape.on("onButtonClick", onBtnShape);
2014-01-09 17:05:03 +01:00
btnPrint.on("onButtonClick", onBtnPrint);
btnOops.on("onButtonHold", onBtnOops);
2014-01-13 18:04:51 +01:00
// vertical shape buttons
btnToggleVerticalShapes.on("onButtonClick", onBtnToggleVerticalShapes);
2014-01-16 16:07:40 +01:00
btnHeight.on("onButtonHold", onBtnHeight);
btnTwist.on("onButtonHold", onBtnTwist);
2014-01-09 17:05:03 +01:00
btnStraight.on("onButtonClick", onBtnStraight);
2014-01-13 18:04:51 +01:00
btnDiv.on("onButtonClick", onBtnDiv);
btnConv.on("onButtonClick", onBtnConv);
btnSine.on("onButtonClick", onBtnSine);
btnToggleEdit.on("onButtonClick", onBtnToggleEdit);
btnMove.on("onButtonHold", onBtnMove);
btnZoom.on("onButtonHold", onBtnZoom);
btnRotate.on("onButtonHold", onBtnRotate);
2014-01-09 18:10:54 +01:00
getSavedSketchStatus();
setSketchModified(false);
2013-07-26 22:39:28 +02:00
2014-01-13 15:52:40 +01:00
function onBtnToggleVerticalShapes() {
var btnImg;
if(buttonGroupVerticalShapes.is(":hidden")) {
btnImg = "img/buttons/btnArrowClose.png";
} else {
btnImg = "img/buttons/btnArrowOpen.png";
}
btnToggleVerticalShapes.attr("src",btnImg);
buttonGroupVerticalShapes.fadeToggle(BUTTON_GROUP_SHOW_DURATION);
2014-01-09 18:10:54 +01:00
}
2013-07-26 22:39:28 +02:00
2014-01-16 12:16:09 +01:00
function onLogo() {
location.reload();
}
2014-01-09 17:05:03 +01:00
function onBtnAdd() {
2014-01-13 15:52:40 +01:00
buttonGroupAdd.fadeToggle(BUTTON_GROUP_SHOW_DURATION);
}
2014-01-09 17:05:03 +01:00
function onBtnStraight() {
setVerticalShape(verticalShapes.NONE);
}
2014-01-09 17:05:03 +01:00
function onBtnDiv() {
setVerticalShape(verticalShapes.DIVERGING);
}
2014-01-09 17:05:03 +01:00
function onBtnConv() {
setVerticalShape(verticalShapes.CONVERGING);
}
2014-01-09 17:05:03 +01:00
function onBtnSine() {
setVerticalShape(verticalShapes.SINUS);
}
2014-01-09 17:05:03 +01:00
function hitTest(cursor,button,radius) {
return distance(cursor.x,cursor.y,button.x,button.y)<radius;
}
2014-01-13 18:04:51 +01:00
2014-01-13 16:38:26 +01:00
function onBtnToggleEdit() {
var btnImg;
if(buttonGroupEdit.is(":hidden")) {
btnImg = "img/buttons/btnArrowClose.png";
} else {
btnImg = "img/buttons/btnArrowOpen.png";
}
btnToggleEdit.attr("src",btnImg);
buttonGroupEdit.fadeToggle(BUTTON_GROUP_SHOW_DURATION);
}
2014-01-13 18:04:51 +01:00
function onBtnMove(e,cursor) {
var w = btnMove.width();
var h = btnMove.height();
var speedX = (cursor.x-w/2)*0.3;
var speedY = (cursor.y-h/2)*0.3;
//console.log("move speed: ",speedX,speedY);
2014-01-13 18:04:51 +01:00
moveShape(speedX,speedY);
}
function onBtnZoom(e,cursor) {
var h = btnZoom.height();
var multiplier = (h/2-cursor.y)*0.003 + 1;
zoomShape(multiplier);
}
function onBtnRotate(e,cursor) {
var h = btnZoom.height();
var multiplier = (h/2-cursor.y)*0.003;
2014-01-16 16:07:40 +01:00
rotateShape(-multiplier);
2014-01-13 18:04:51 +01:00
}
2014-01-16 16:07:40 +01:00
function onBtnHeight(e,cursor) {
var h = btnHeight.height();
if(cursor.y < h/2) {
previewUp(true);
} else {
previewDown(true);
}
}
2014-01-16 16:07:40 +01:00
function onBtnTwist(e,cursor) {
var h = btnTwist.height();
var multiplier = (cursor.y-h/2)*0.0005;
previewTwist(multiplier,true);
}
2014-01-09 17:05:03 +01:00
function onBtnOops(e) {
oopsUndo();
}
function onBtnNew(e) {
clearDoodle();
}
2014-01-13 18:04:51 +01:00
2014-01-09 17:05:03 +01:00
function onBtnPrint(e) {
print();
}
2014-01-09 17:05:03 +01:00
function onBtnWordArt(e) {
showWordArtDialog();
}
function onBtnShape(e) {
showShapeDialog();
2014-01-10 17:21:43 +01:00
buttonGroupAdd.fadeOut();
2014-01-09 17:05:03 +01:00
}
2013-07-26 22:39:28 +02:00
enableButton(btnSettings, openSettingsWindow);
2014-01-17 12:51:27 +01:00
2013-10-29 21:27:17 +01:00
// 29-okt-2013 - we're not doing help for smartphones at the moment
if (clientInfo.isSmartphone) {
btnInfo.addClass("disabled");
} else {
2014-01-17 12:51:27 +01:00
function onBtnInfo(e) {
helpTours.startTour(helpTours.WELCOMETOUR);
}
enableButton(btnInfo, onBtnInfo);
2013-10-29 21:27:17 +01:00
}
}
2014-01-09 17:05:03 +01:00
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 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();
// also make new Image, otherwise the previously cached preview can be redrawn with move up/down or twist left/right
doodleImageCapture = new Image();
// 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();
if (clientInfo.isSmartphone) {
// do not recalc the whole preview's bounds during undo if client device is a smartphone
redrawDoodle(false);
} else {
// recalc the whole preview's bounds during if client device is not a smartphone
redrawDoodle(true);
}
2013-07-26 22:39:28 +02:00
redrawPreview();
}
2014-01-09 17:05:03 +01:00
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++;
}
setSketchModified(true);
// 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--;
}
setSketchModified(true);
// redrawPreview(redrawLess);
redrawRenderedPreview(redrawLess);
2013-07-26 22:39:28 +02:00
}
function previewTwistLeft(redrawLess) {
2014-01-16 16:07:40 +01:00
previewTwist(-twistIncrement,true)
2013-07-26 22:39:28 +02:00
}
function previewTwistRight(redrawLess) {
2014-01-16 16:07:40 +01:00
previewTwist(twistIncrement,true)
}
function previewTwist(increment,redrawLess) {
console.log("previewTwist: ",increment);
if (redrawLess == undefined) redrawLess = false;
rStep += increment;
if(rStep < -previewRotationLimit) rStep = -previewRotationLimit;
else if(rStep > previewRotationLimit) rStep = previewRotationLimit;
redrawRenderedPreview(redrawLess);
setSketchModified(true);
}
function resetTwist() {
rStep = 0;
redrawRenderedPreview();
setSketchModified(true);
}
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: /* fall-through */
case Printer.BUFFERING_STATE: /* fall-through */
case Printer.PRINTING_STATE: /* fall-through */
case Printer.STOPPING_STATE:
thermometer.show();
break;
default:
thermometer.hide();
break;
}
// progress indicator
switch(newState) {
case Printer.PRINTING_STATE:
progressbar.show();
break;
default:
2013-10-17 14:40:33 +02:00
progressbar.hide();
break;
}
/* settings button */
switch(newState) {
case Printer.IDLE_STATE:
enableButton(btnSettings, openSettingsWindow);
break;
case Printer.WIFIBOX_DISCONNECTED_STATE: /* fall-through */
case Printer.BUFFERING_STATE: /* fall-through */
case Printer.PRINTING_STATE: /* fall-through */
case Printer.STOPPING_STATE:
disableButton(btnSettings);
break;
default:
enableButton(btnSettings, openSettingsWindow);
break;
}
/* save, next and prev buttons */
switch(newState) {
case Printer.WIFIBOX_DISCONNECTED_STATE:
disableButton(btnPrevious);
disableButton(btnNext);
disableButton(btnSave);
break;
default:
updatePrevNextButtonState();
if (isModified) enableButton(btnSave, saveSketch);
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);
} else if(prevState == Printer.PRINTING_STATE && newState == Printer.STOPPING_STATE) {
console.log("stopmsg show");
message.set("Printer stopping",Message.INFO,false);
} else if(prevState == Printer.STOPPING_STATE && newState == Printer.IDLE_STATE) {
console.log("stopmsg hide");
message.hide();
}
state = newState;
2013-10-11 16:17:58 +02:00
hasControl = newHasControl;
}