From cc139ffc5d039052b7b859c326c6fde448d1b18b Mon Sep 17 00:00:00 2001 From: Michael Aschauer Date: Sat, 14 Oct 2017 13:08:20 +0200 Subject: [PATCH] more error catching, more DST export fixes --- stitchcode/gui.js | 99 +++++++++++++++++++++++++++++++++--- stitchcode/turtleShepherd.js | 96 ++++++++++++++++++++++++++++++---- 2 files changed, 178 insertions(+), 17 deletions(-) diff --git a/stitchcode/gui.js b/stitchcode/gui.js index adffee28..fe63eaec 100644 --- a/stitchcode/gui.js +++ b/stitchcode/gui.js @@ -2230,26 +2230,38 @@ IDE_Morph.prototype.uploadOrder = function () { fd.append('source', 'turtlestitch'); fd.append('url', window.location.href); fd.append('dstfile', blob, name + ".dst"); + if (SnapCloud.username) + fd.append('username', SnapCloud.username); var request = new XMLHttpRequest(); request.onreadystatechange = function () { if (request.readyState === 4) { if (request.responseText) { - var response = JSON.parse(request.responseText); - if (!response.error) { - console.log(response); - window.open(response.url); - } else { + try { + var response = JSON.parse(request.responseText); + if (!response.error) { + new DialogBoxMorph().informWithLink( + 'Upload success', + 'Your embroidery was successully uploaded.\n\n Procceed to order opening a new window.\n' + + 'If it does not open automatically, click here:' , + response.url, + world); + window.open(response.url); + } + } catch(e) { new DialogBoxMorph().inform( 'Upload Error', - 'Sorry. Ehere was an Error during upload', + 'Sorry. There was an Error during upload: \n' + request.responseText, world); - } + } + + } else { new DialogBoxMorph().inform( 'Upload Error', - 'Sorry. Ehere was an Error during upload', + 'Sorry. There was an Error during upload: \n' + + request.status + ' - ' + request.statusText, world); } } @@ -2306,3 +2318,74 @@ IDE_Morph.prototype.uploadOrder = function () { } }; +DialogBoxMorph.prototype.informWithLink = function ( + title, + textString, + url, + world, + pic +) { + var lnk = new AlignmentMorph('row', 1); + var txt = new TextMorph( + textString, + this.fontSize, + this.fontStyle, + true, + false, + 'center', + null, + null, + MorphicPreferences.isFlat ? null : new Point(1, 1), + new Color(255, 255, 255) + ), + bdy = new AlignmentMorph('column', this.padding), + myself = this; + + function linkButton(label, url) { + var btn = new PushButtonMorph( + myself, + function () { + window.open(url); + }, + ' ' + localize(label) + ' ' + ); + btn.fontSize = 10; + btn.corner = myself.buttonCorner; + btn.edge = myself.buttonEdge; + btn.outline = myself.buttonOutline; + btn.outlineColor = myself.buttonOutlineColor; + btn.outlineGradient = myself.buttonOutlineGradient; + btn.padding = myself.buttonPadding; + btn.contrast = myself.buttonContrast; + btn.drawNew(); + btn.fixLayout(); + return btn; + } + + if (!this.key) { + this.key = 'inform' + title + textString; + } + + this.labelString = title; + this.createLabel(); + if (pic) {this.setPicture(pic); } + + if (textString) { + bdy.add(txt) + } + + if (url) { + lnk.add(linkButton(url, url)); + bdy.add(lnk); + } + + bdy.fixLayout(); + + this.addBody(bdy); + + this.addButton('ok', 'OK'); + this.drawNew(); + this.fixLayout(); + this.popUp(world); +}; + diff --git a/stitchcode/turtleShepherd.js b/stitchcode/turtleShepherd.js index 89354a27..5dc19175 100644 --- a/stitchcode/turtleShepherd.js +++ b/stitchcode/turtleShepherd.js @@ -151,6 +151,67 @@ TurtleShepherd.prototype.addColorChange= function(color) { ); }; +/* +TurtleShepherd.prototype.flatten = function(max_length) { + new_cache = []; + hasFirst = false; + lastStitch = null; + + for (var i=0; i < this.cache.length; i++) { + if (this.cache[i].cmd == "color") { + new_cache.push(this.cache[i]); + } else if (this.cache[i].cmd == "move") { + stitch = this.cache[i]; + + if (hasFirst) { + x1 = Math.round(stitch.x * scale); + y1 = -Math.round(stitch.y * scale); + x0 = Math.round(lastStitch.x * scale); + y0 = -Math.round(lastStitch.y * scale); + + sum_x = 0; + sum_y = 0; + dmax = Math.max(Math.abs(x1 - x0), Math.abs(y1 - y0)); + dsteps = Math.abs(dmax / 127); + + if (dsteps <= 1) { + new_cache.push(this.cache[i]); + } else { + for(j=0;j\n"; @@ -321,7 +382,8 @@ TurtleShepherd.prototype.toDST = function() { hasFirst = false; pixels_per_millimeter = this.pixels_per_millimeter; scale = 10 / pixels_per_millimeter; - + count_stitches = 0; + count_jumps = 0; function encodeTajimaStitch(dx, dy, jump) { b1 = 0; @@ -456,10 +518,10 @@ TurtleShepherd.prototype.toDST = function() { writeHeader("LA:turtlestitch", 20, true); writeHeader("ST:" + this.steps.toString(), 11); writeHeader("CO:1", 7); - writeHeader("+X:" + Math.round(this.maxX / this.pixels_per_millimeter), 9); // Math.round(this.getMetricWidth()*10), 9); - writeHeader("-X:" + Math.round(this.minX / this.pixels_per_millimeter), 9); - writeHeader("+Y:" + Math.round(this.maxY/ this.pixels_per_millimeter), 9); //Math.round(this.getMetricHeight()*10), 9); - writeHeader("-Y:" + Math.round(this.minY / this.pixels_per_millimeter), 9); + writeHeader("+X:" + Math.round(this.maxX / this.pixels_per_millimeter) * 10, 9); // Math.round(this.getMetricWidth()*10), 9); + writeHeader("-X:" + Math.round(this.minX / this.pixels_per_millimeter) * 10, 9); + writeHeader("+Y:" + Math.round(this.maxY/ this.pixels_per_millimeter) * 10, 9); //Math.round(this.getMetricHeight()*10), 9); + writeHeader("-Y:" + Math.round(this.minY / this.pixels_per_millimeter) * 10, 9); needle_end_x = 0; needle_end_y = 0; @@ -469,8 +531,8 @@ TurtleShepherd.prototype.toDST = function() { needle_end_y = this.cache[i].y; } - writeHeader("AX:+" + Math.round(needle_end_x / this.pixels_per_millimeter), 10); - writeHeader("AY:+" + Math.round(needle_end_y / this.pixels_per_millimeter), 10); + writeHeader("AX:+" + Math.round(needle_end_x / this.pixels_per_millimeter) * 10, 10); + writeHeader("AY:+" + Math.round(needle_end_y / this.pixels_per_millimeter) * 10, 10); writeHeader("MX:0", 10); writeHeader("MY:0", 10); writeHeader("PD:******", 10); @@ -508,6 +570,7 @@ TurtleShepherd.prototype.toDST = function() { if (dsteps <= 1) { encodeTajimaStitch((x1 - x0), (y1 - y0), !stitch.penDown); + count_stitches++; } else { for(j=0;j