fix jump and start encodings, add color change commmands to file emb file exports

pull/68/head
Michael Aschauer 2018-10-19 15:48:36 +02:00
rodzic 7e1ba3b6d7
commit 9c68ef7c13
2 zmienionych plików z 126 dodań i 53 usunięć

Wyświetl plik

@ -74,7 +74,7 @@ SpriteMorph.prototype.addJumpLine = function(x1, y1, x2, y2) {
stage.reRender();
};
SpriteMorph.prototype.addStitchPoint = function(x1, y1, x2, y2) {
SpriteMorph.prototype.addStitchPoint = function(x2, y2) {
var stage = this.parentThatIsA(StageMorph);
var material = this.cache.findMaterial( 0x0000ff, 1);
@ -121,7 +121,7 @@ SpriteMorph.prototype.addStitchPoint = function(x1, y1, x2, y2) {
SpriteMorph.prototype.addDensityPoint = function(x1, y1) {
var stage = this.parentThatIsA(StageMorph);
var geometry = turtle.cache.findGeometry('densityPoint', [3, 6,]);
var geometry = this.cache.findGeometry('densityPoint', [3, 6,]);
if (!geometry) {
geometry = new THREE.CircleGeometry( 3, 6 );
geometry.vertices.shift();
@ -154,34 +154,41 @@ SpriteMorph.prototype.forward = function (steps) {
oldx = this.xPosition();
oldy = this.yPosition();
if (dist >= 0) {
dest = this.position().distanceAngle(dist, this.heading);
} else {
dest = this.position().distanceAngle(
Math.abs(dist),
(this.heading - 180)
Math.abs(dist), (this.heading - 180)
);
}
this.setPosition(dest);
this.positionTalkBubble();
warn = stage.turtleShepherd.moveTo(
oldx, oldy,
this.xPosition(), this.yPosition(),
this.isDown );
if (dist != 0) {
this.setPosition(dest);
this.positionTalkBubble();
if (warn) {
this.addDensityPoint(this.xPosition(), this.yPosition());
warn = stage.turtleShepherd.moveTo(
oldx, oldy,
this.xPosition(), this.yPosition(),
this.isDown );
//console.log(this.isDown, this.xPosition(), this.yPosition(), dist);
if (this.isDown) {
this.addStitch(oldx, oldy, this.xPosition(), this.yPosition());
this.addStitchPoint(this.xPosition(), this.yPosition());
if (warn) {
this.addDensityPoint(this.xPosition(), this.yPosition());
}
if (this.parentThatIsA(StageMorph).turtleShepherd.isEmpty()) {
this.addStitchPoint(0,0);
}
} else {
this.addJumpLine(oldx, oldy, this.xPosition(), this.yPosition());
}
stage.moveTurtle(this.xPosition(), this.yPosition());
}
if (this.isDown)
this.addStitch(oldx, oldy, this.xPosition(), this.yPosition());
else {
this.addJumpLine(oldx, oldy, this.xPosition(), this.yPosition());
}
this.addStitchPoint(oldx, oldy, this.xPosition(), this.yPosition());
stage.moveTurtle(this.xPosition(), this.yPosition());
//this.changed();
};
@ -211,24 +218,36 @@ SpriteMorph.prototype.gotoXY = function (x, y, justMe, noShadow) {
oldx = this.xPosition();
oldy = this.yPosition();
this.origGotoXY(x, y, justMe);
if ( (Math.abs(this.xPosition()-oldx)<=0.01 && Math.abs(this.yPosition()-oldy)<=0.01) ) {
x = !isFinite(+x) ? 0 : +x;
y = !isFinite(+y) ? 0 : +y;
var a = (oldx - this.xPosition());
var b = (oldy - this.yPosition());
var dist = Math.sqrt(a*a + b*b);
if ( dist <= 1) {
// jump in place - don't add / ignore
console.log("jump in place - don't add / ignore");
//console.log("jump in place - don't add / ignore", this.isDown,this.xPosition(), this.yPosition(), dist);
} else {
warn = this.parentThatIsA(StageMorph).turtleShepherd.moveTo(
oldx, oldy,
this.xPosition(), this.yPosition(),
this.isDown );
//console.log("goto", this.isDown,this.xPosition(), this.yPosition(), dist);
if (this.isDown) {
this.addStitch(oldx, oldy, this.xPosition(), this.yPosition());
this.addStitchPoint(this.xPosition(), this.yPosition());
if (warn) {
this.addDensityPoint(this.xPosition(), this.yPosition());
}
if (this.parentThatIsA(StageMorph).turtleShepherd.isEmpty())
this.addStitchPoint(0,0);
} else {
this.addJumpLine(oldx, oldy, this.xPosition(), this.yPosition());
}
this.addStitchPoint(oldx, oldy, this.xPosition(), this.yPosition());
stage.moveTurtle(this.xPosition(), this.yPosition());
}
};
@ -269,7 +288,6 @@ SpriteMorph.prototype.gotoXYBy = function (x, y, stepsize) {
}
if (rest > 0) {
this.gotoXY(x,y);
}
}
};
@ -292,21 +310,20 @@ SpriteMorph.prototype.gotoXYIn = function (x, y, steps) {
if (a == 0 && b == 0)
dist = 0;
var deltaX = (x - this.xPosition()) * this.parent.scale;
var deltaY = (y - this.yPosition()) * this.parent.scale;
var angle = Math.abs(deltaX) < 0.001 ? (deltaY < 0 ? 90 : 270)
: Math.round(
(deltaX >= 0 ? 0 : 180)
- (Math.atan(deltaY / deltaX) * 57.2957795131)
);
this.setHeading(angle + 90);
if (dist > 0) {
var stepsize = dist / steps;
var deltaX = (x - this.xPosition()) * this.parent.scale;
var deltaY = (y - this.yPosition()) * this.parent.scale;
var angle = Math.abs(deltaX) < 0.001 ? (deltaY < 0 ? 90 : 270)
: Math.round(
(deltaX >= 0 ? 0 : 180)
- (Math.atan(deltaY / deltaX) * 57.2957795131)
);
this.setHeading(angle + 90);
for(i=0; i < steps; i++) {
this.forward(stepsize);
}
}
};
@ -1129,6 +1146,7 @@ SpriteMorph.prototype.resetAll = function () {
myself.gotoXY(0,0);
myself.setHeading(90);
myself.clear();
myself.setColor(new Color(0,0,0,0));
}

Wyświetl plik

@ -20,6 +20,8 @@ TurtleShepherd.prototype.init = function() {
this.maxLength = 121;
this.calcTooLong = true;
this.densityMax = 15;
this.colors = [];
this.newColor = false;
};
@ -40,6 +42,8 @@ TurtleShepherd.prototype.clear = function() {
this.density = {};
this.tooLongCount = 0;
this.densityWarning = false;
this.colors = [];
this.newColor = false;
};
@ -55,6 +59,9 @@ TurtleShepherd.prototype.isMetric = function() {
return this.metric;
};
TurtleShepherd.prototype.isEmpty = function() {
return this.steps > 1;
};
TurtleShepherd.prototype.hasSteps = function() {
return this.steps > 0;
@ -123,8 +130,8 @@ TurtleShepherd.prototype.moveTo= function(x1, y1, x2, y2, penState) {
warn = false;
// ignore jump stitches withouth any previous stitches
if (this.steps === 0 && !penState)
return
//if (this.steps === 0 && !penState)
// return
if (this.steps === 0) {
this.initX = x1;
@ -142,8 +149,20 @@ TurtleShepherd.prototype.moveTo= function(x1, y1, x2, y2, penState) {
}
);
this.density[Math.round(x1) + "x" + Math.round(y1)] = 1;
if (this.colors.length < 1) {
if (this.newColor) {
this.colors.push(this.newColor);
this.newColor = false;
} else {
this.colors.push({r:0,g:0,b:0,a:255});
}
}
}
if (this.newColor) {
this.pushColorChangeNow();
}
if (x2 < this.minX) this.minX = x2;
if (x2 > this.maxX) this.maxX = x2;
@ -197,17 +216,34 @@ TurtleShepherd.prototype.moveTo= function(x1, y1, x2, y2, penState) {
TurtleShepherd.prototype.addColorChange= function(color) {
var c = {
r: Math.round(color.r),
g: Math.round(color.g),
b: Math.round(color.b),
a: Math.round(color.a) || 255
};
this.newColor = c;
};
TurtleShepherd.prototype.pushColorChangeNow = function() {
c = this.newColor;
index = this.colors.findIndex(x => (x.r == c.r && x.b == x.b && x.g == c.g && x.a == c.a) );
console.log(index, this.newColor, this.colors);
if (index < 0) {
index = this.colors.push(this.newColor)-1;
}
this.cache.push(
{
"cmd":"color",
"color":{
r: Math.round(color.r),
g: Math.round(color.g),
b: Math.round(color.b),
a: Math.round(color.a) || 0
},
"color": this.newColor,
"thread": index
}
);
this.newColor = false;
};
/*
@ -381,11 +417,25 @@ TurtleShepherd.prototype.toEXP = function() {
for (var i=0; i < this.cache.length; i++) {
if (this.cache[i].cmd == "color") {
//expArr.push(0x01);
//expArr.push(0x01);
expArr.push(0x80);
expArr.push(0x01);
expArr.push(0x00);
expArr.push(0x00);
move(0,0);
} else if (this.cache[i].cmd == "move") {
stitch = this.cache[i];
if (!hasFirst) {
if (!stitch.penDown) {
expArr.push(0x80);
expArr.push(0x04);
}
move(0,0);
lastStitch = {cmd: "move", x: 0, y: -0, penDown: stitch.penDown}
hasFirst = true;
}
if (hasFirst) {
x1 = Math.round(stitch.x * scale);
y1 = -Math.round(stitch.y * scale);
@ -402,7 +452,6 @@ TurtleShepherd.prototype.toEXP = function() {
if (dsteps <= 1) {
if (!stitch.penDown) {
//ignore color
expArr.push(0x80);
expArr.push(0x04);
}
@ -579,7 +628,7 @@ TurtleShepherd.prototype.toDST = function() {
writeHeader("LA:turtlestitch", 20, true);
writeHeader("ST:" + this.steps.toString(), 11);
writeHeader("CO:1", 7);
writeHeader("CO:" + this.colors.length, 7);
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);
@ -611,13 +660,22 @@ TurtleShepherd.prototype.toDST = function() {
}
for (i=0; i < this.cache.length; i++) {
if (this.cache[i].cmd == "color") {
//expArr.push(0x01);
//expArr.push(0x01);
expArr.push(0x00);
expArr.push(0x00);
expArr.push(0x43);
encodeTajimaStitch(0,0,false);
} else if (this.cache[i].cmd == "move") {
stitch = this.cache[i];
if (!hasFirst) {
encodeTajimaStitch(0,0,!stitch.penDown);
lastStitch = {cmd: "move", x: 0, y: -0, penDown: stitch.penDown}
hasFirst = true;
}
if (hasFirst) {
x1 = Math.round(stitch.x * scale);
y1 = Math.round(stitch.y * scale);
@ -658,10 +716,7 @@ TurtleShepherd.prototype.toDST = function() {
}
}
}
} else {
encodeTajimaStitch(Math.round(stitch.x), Math.round(stitch.y) ,false);
count_stitches++;
}
}
lastStitch = stitch;
hasFirst = true;
}