From 103e822313b5b9658a25c935b6febf8eccf515ff Mon Sep 17 00:00:00 2001 From: Ian Prest Date: Fri, 3 Jul 2015 19:22:18 -0400 Subject: [PATCH] Fix serialization errors with rotated clusters. -- If the first row in your layout was rotated, the x & y coordinates didn't get output correctly. -- The serializer didn't take into account that *both* x & y are updated when *either* rx & ry are specified in the output; as a result, it would mis-calculate the correct x & y coordinates for some rotated keys. -- Fixes #57 and #61 --- serial.js | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/serial.js b/serial.js index 6f64be5..1143c91 100644 --- a/serial.js +++ b/serial.js @@ -60,6 +60,9 @@ var $serial = {}; rows.push(meta); } + var newRow = true; + current.y--; // will be incremented on first row + // Serialize row/key-data $serial.sortKeys(keys); keys.forEach(function(key) { @@ -67,22 +70,30 @@ var $serial = {}; var label = key.labels.join("\n").trimEnd(); // start a new row when necessary - if(row.length>0 && (key.y !== current.y || key.rotation_angle != cluster.r || key.rotation_x != cluster.rx || key.rotation_y != cluster.ry)) { + var clusterChanged = (key.rotation_angle != cluster.r || key.rotation_x != cluster.rx || key.rotation_y != cluster.ry); + var rowChanged = (key.y !== current.y); + if(row.length>0 && (rowChanged || clusterChanged)) { // Push the old row rows.push(row); row = []; + newRow = true; + } + if(newRow) { // Set up for the new row - if(key.rotation_angle != cluster.r || key.rotation_x != cluster.rx || key.rotation_y != cluster.ry) { - cluster.r = key.rotation_angle; - cluster.rx = key.rotation_x; - cluster.ry = key.rotation_y; - current.x = cluster.rx; - current.y = cluster.ry; - } else { - current.x = cluster.rx; - current.y++; - } + current.y++; + + // 'y' is reset if *either* 'rx' or 'ry' are changed + if(key.rotation_y != cluster.ry || key.rotation_x != cluster.rx) + current.y = key.rotation_y; + current.x = key.rotation_x; // always reset x to rx (which defaults to zero) + + // Update current cluster + cluster.r = key.rotation_angle; + cluster.rx = key.rotation_x; + cluster.ry = key.rotation_y; + + newRow = false; } current.rotation_angle = serializeProp(props, "r", key.rotation_angle, current.rotation_angle); @@ -118,7 +129,7 @@ var $serial = {}; return rows; } - function dserializeError(msg,data) { + function deserializeError(msg,data) { throw "Error: " + msg + (data ? (":\n " + $serial.toJsonL(data)) : ""); } $serial.deserialize = function(rows) { @@ -144,9 +155,9 @@ var $serial = {}; current.x2 = current.y2 = current.width2 = current.height2 = 0; current.nub = current.stepped = false; } else { - if(key.r != null) { if(k!=0) {dserializeError("'r' can only be used on the first key in a row", key);} current.rotation_angle = key.r; } - if(key.rx != null) { if(k!=0) {dserializeError("'rx' can only be used on the first key in a row", key);} current.rotation_x = cluster.x = key.rx; $.extend(current, cluster); } - if(key.ry != null) { if(k!=0) {dserializeError("ry' can only be used on the first key in a row", key);} current.rotation_y = cluster.y = key.ry; $.extend(current, cluster); } + if(key.r != null) { if(k!=0) {deserializeError("'r' can only be used on the first key in a row", key);} current.rotation_angle = key.r; } + if(key.rx != null) { if(k!=0) {deserializeError("'rx' can only be used on the first key in a row", key);} current.rotation_x = cluster.x = key.rx; $.extend(current, cluster); } + if(key.ry != null) { if(k!=0) {deserializeError("ry' can only be used on the first key in a row", key);} current.rotation_y = cluster.y = key.ry; $.extend(current, cluster); } if(key.a != null) { current.align = key.a; } if(key.f) { current.fontheight = current.fontheight2 = key.f; } if(key.f2) { current.fontheight2 = key.f2; }