Skip to content

Commit

Permalink
saving and loading from the UI
Browse files Browse the repository at this point in the history
  • Loading branch information
ixchow committed Dec 21, 2022
1 parent 02025ed commit 9a42b4b
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 10 deletions.
29 changes: 29 additions & 0 deletions code/sv.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,35 @@ export class Body {
const data = JSON.parse(json);
return Body.fromData(data, library);
}
toData() {
let data = [];
//for convenience:
for (let i = 0; i < this.cells.length; ++i) {
this.cells[i].index = i;
}

for (const cell of this.cells) {
const template = cell.template.signature();
const vertices = [];
for (const v of cell.vertices) {
vertices.push([v[0], v[1], v[2]]);
}
const connections = [];
for (const con of cell.connections) {
if (con === null) {
connections.push(null);
} else {
connections.push({cell:con.cell.index, face:con.face});
}
}
data.push({template, vertices, connections});
}

for (const cell of this.cells) {
delete cell.index;
}
return data;
}
static fromData(data, library) {
if (!Array.isArray(data)) throw new Error("");
let body = new Body();
Expand Down
72 changes: 62 additions & 10 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@
#file {
display:none;
}
#fileLabel span {
cursor:pointer;
text-decoration:underline;
}

#views {
display:grid;
Expand All @@ -43,14 +39,42 @@

/*background: #0008 ;*/
}
#view-controls {
#controls {
grid-column: 1 / 3 ;
grid-row: 1 ;
font-size: 16px;
line-height:16px;
padding: 5px 10px;
display:flex;
flex-flow: row wrap;
align-items:center;
/*background: #f008 ;*/
}

.controlsButton {
box-sizing:border-box;
cursor:pointer;
display:block;
background:#cfbb96;
padding:4px 8px;
margin:0 2px;
box-shadow: 0 1px 2px 0 #0008;
text-decoration:none;
line-height:1;
border: none;
border-radius:2px;
height:100%;
font-size:inherit;
}

.controlsButton:hover {
background:#f6d69b;
}

.controlsButton:active {
box-shadow: 0 0 1px 0 #0008;
}

#view-3d {
grid-column: 1 ;
grid-row: 2 ;
Expand Down Expand Up @@ -93,12 +117,13 @@
<div id="views">

<!-- controls bar at the top of the window -->
<div id="view-controls">
<h1 id="file-name">No File Loaded</h1>
<div id="controls">
<input id="file" type="file" />
<label id="fileLabel" for="file"><span>Choose a .blocks file</span> or drag one into the window to load it.</label>
<label id="fileLabel" for="file" class="controlsButton">Load</label>
<span id="fileName">(no file loaded)</span>
<!-- <button id="benchmark">benchmark</button>
<span id="benchmark-result"></span> -->
<button id="save" class="controlsButton">Save</button>
</div>

<!-- 3d view -->
Expand Down Expand Up @@ -128,7 +153,7 @@ <h1>Blocks:</h1>
window.currentFile = null;

function setFilename(name) {
document.getElementById("file-name").firstChild.textContent = name;
document.getElementById("fileName").textContent = name;
}
function readFile(file) {
console.log("Attempting to read file: '" + file.name + "'");
Expand All @@ -142,8 +167,10 @@ <h1>Blocks:</h1>
let reader = readFile.reader = new FileReader();
reader.onload = function(){
console.log("File was " + reader.result.byteLength + " bytes long.");
window.blocks = sv.Blocks.fromArrayBuffer(reader.result);
window.body = sv.Body.fromArrayBuffer(reader.result, library);

bodyDirty();
mouseDirty();
resetCamera();
requestRedraw();
window.currentFile = file;
Expand Down Expand Up @@ -197,6 +224,27 @@ <h1>Blocks:</h1>
file.addEventListener('click', function(evt){
file.value = ""; //reset so 'change' event fires
});

document.getElementById("save").addEventListener('click', function(evt){
console.log("Saving");
const text = JSON.stringify(body.toData());
fileSave(text, "solid-knitting.body");
});

//from knitout-live-visualizer:
function fileSave(sourceText, fileIdentity) {
var workElement = document.createElement("a");
if ('download' in workElement) {
workElement.href = "data:" + 'text/plain' + "charset=utf-8," + escape(sourceText);
workElement.setAttribute("download", fileIdentity);
document.body.appendChild(workElement);
var eventMouse = document.createEvent("MouseEvents");
eventMouse.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
workElement.dispatchEvent(eventMouse);
document.body.removeChild(workElement);
} else throw 'File saving not supported for this browser';
}

</script>

<!-- Solid Knitting UI code -->
Expand Down Expand Up @@ -288,6 +336,7 @@ <h1>Blocks:</h1>

requestRedraw();
}
window.resetCamera = resetCamera;

//--- controls ---

Expand Down Expand Up @@ -318,12 +367,14 @@ <h1>Blocks:</h1>
MOUSE.dirty = true;
requestRedraw(); //mouse will be updated as part of redraw
}
window.mouseDirty = mouseDirty;

function bodyDirty() {
visBody.dirty = true;
visWire.dirty = true;
requestRedraw();
}
window.bodyDirty = bodyDirty;

function setMouseOver() {
MOUSE.over = null;
Expand Down Expand Up @@ -1581,6 +1632,7 @@ <h1>Blocks:</h1>
redraw();
});
}
window.requestRedraw = requestRedraw;

resetCamera();
requestRedraw();
Expand Down
1 change: 1 addition & 0 deletions layer-3x3.body
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"template":"cast-on x x +y1 x -y1 +l1","vertices":[[-3.7430969636795886,-1.6252327300133296,-0.47873546053033694],[-3.7430969636795886,-1.6252327300133296,0.40926453946966307],[-3.7430969636795886,-0.6252327300133296,-0.47873546053033694],[-3.7430969636795886,-0.6252327300133296,0.40926453946966307],[-1.7430969636795886,-1.6252327300133296,-0.47873546053033694],[-1.7430969636795886,-1.6252327300133296,0.40926453946966307],[-1.7430969636795886,-0.6252327300133296,-0.47873546053033694],[-1.7430969636795886,-0.6252327300133296,0.40926453946966307]],"connections":[null,null,null,null,{"cell":1,"face":2},{"cell":3,"face":0}]},{"template":"cast-on x x +y1 x -y1 +l1","vertices":[[-1.1345541769857128,-1.7060522761819126,-0.43060638903295706],[-1.1345541769857128,-1.7060522761819126,0.45739361096704295],[-1.1345541769857128,-0.7060522761819126,-0.43060638903295706],[-1.1345541769857128,-0.7060522761819126,0.45739361096704295],[0.8654458230142873,-1.7060522761819126,-0.43060638903295706],[0.8654458230142873,-1.7060522761819126,0.45739361096704295],[0.8654458230142873,-0.7060522761819126,-0.43060638903295706],[0.8654458230142873,-0.7060522761819126,0.45739361096704295]],"connections":[null,null,{"cell":0,"face":4},null,{"cell":2,"face":2},{"cell":4,"face":0}]},{"template":"cast-on x x +y1 x -y1 +l1","vertices":[[1.482849801225643,-1.6941405573127941,-0.430601183310432],[1.482849801225643,-1.6941405573127941,0.457398816689568],[1.482849801225643,-0.6941405573127941,-0.430601183310432],[1.482849801225643,-0.6941405573127941,0.457398816689568],[3.482849801225643,-1.6941405573127941,-0.430601183310432],[3.482849801225643,-1.6941405573127941,0.457398816689568],[3.482849801225643,-0.6941405573127941,-0.430601183310432],[3.482849801225643,-0.6941405573127941,0.457398816689568]],"connections":[null,null,{"cell":1,"face":4},null,null,{"cell":5,"face":0}]},{"template":"knit -l1 -y1 x +y1 +l1 +L1","vertices":[[-3.8638806133148424,-0.019836156970391094,-0.34251632914492897],[-3.8638806133148424,-0.019836156970391094,0.545483670855071],[-3.8638806133148424,0.9801638430296089,-0.34251632914492897],[-3.8638806133148424,0.9801638430296089,0.545483670855071],[-1.8638806133148424,-0.019836156970391094,-0.34251632914492897],[-1.8638806133148424,-0.019836156970391094,0.545483670855071],[-1.8638806133148424,0.9801638430296089,-0.34251632914492897],[-1.8638806133148424,0.9801638430296089,0.545483670855071]],"connections":[{"cell":0,"face":5},null,null,{"cell":4,"face":1},{"cell":6,"face":0},null]},{"template":"knit -l1 -y1 x +y1 +l1 +L1","vertices":[[-1.1891759517311542,-0.036424504308564476,-0.33611108224456127],[-1.1891759517311542,-0.036424504308564476,0.5518889177554387],[-1.1891759517311542,0.9635754956914355,-0.33611108224456127],[-1.1891759517311542,0.9635754956914355,0.5518889177554387],[0.8108240482688458,-0.036424504308564476,-0.33611108224456127],[0.8108240482688458,-0.036424504308564476,0.5518889177554387],[0.8108240482688458,0.9635754956914355,-0.33611108224456127],[0.8108240482688458,0.9635754956914355,0.5518889177554387]],"connections":[{"cell":1,"face":5},{"cell":3,"face":3},null,{"cell":5,"face":1},{"cell":7,"face":0},null]},{"template":"knit -l1 -y1 x +y1 +l1 +L1","vertices":[[1.553336644294073,-0.08813710383399398,-0.36029742940876114],[1.553336644294073,-0.08813710383399398,0.5277025705912388],[1.553336644294073,0.911862896166006,-0.36029742940876114],[1.553336644294073,0.911862896166006,0.5277025705912388],[3.553336644294073,-0.08813710383399398,-0.36029742940876114],[3.553336644294073,-0.08813710383399398,0.5277025705912388],[3.553336644294073,0.911862896166006,-0.36029742940876114],[3.553336644294073,0.911862896166006,0.5277025705912388]],"connections":[{"cell":2,"face":5},{"cell":4,"face":3},null,null,{"cell":8,"face":0},null]},{"template":"knit -l1 +y1 x -y1 +l1 +L1","vertices":[[-3.7974944815200176,1.7606201616364867,-0.444],[-3.7974944815200176,1.7606201616364867,0.444],[-3.7974944815200176,2.7606201616364867,-0.444],[-3.7974944815200176,2.7606201616364867,0.444],[-1.7974944815200176,1.7606201616364867,-0.444],[-1.7974944815200176,1.7606201616364867,0.444],[-1.7974944815200176,2.7606201616364867,-0.444],[-1.7974944815200176,2.7606201616364867,0.444]],"connections":[{"cell":3,"face":4},null,null,{"cell":7,"face":1},{"cell":11,"face":0},null]},{"template":"knit -l1 +y1 x -y1 +l1 +L1","vertices":[[-1.0838138458917708,1.6647972774407491,-0.444],[-1.0838138458917708,1.6647972774407491,0.444],[-1.0838138458917708,2.664797277440749,-0.444],[-1.0838138458917708,2.664797277440749,0.444],[0.9161861541082292,1.6647972774407491,-0.444],[0.9161861541082292,1.6647972774407491,0.444],[0.9161861541082292,2.664797277440749,-0.444],[0.9161861541082292,2.664797277440749,0.444]],"connections":[{"cell":4,"face":4},{"cell":6,"face":3},null,{"cell":8,"face":1},{"cell":10,"face":0},null]},{"template":"knit -l1 +y1 x -y1 +l1 +L1","vertices":[[1.6209557084580948,1.6895646386762553,-0.444],[1.6209557084580948,1.6895646386762553,0.444],[1.6209557084580948,2.6895646386762553,-0.444],[1.6209557084580948,2.6895646386762553,0.444],[3.620955708458095,1.6895646386762553,-0.444],[3.620955708458095,1.6895646386762553,0.444],[3.620955708458095,2.6895646386762553,-0.444],[3.620955708458095,2.6895646386762553,0.444]],"connections":[{"cell":5,"face":4},{"cell":7,"face":3},null,null,{"cell":9,"face":0},null]},{"template":"knit -l1 -y1 x +y1 +l1 +L1","vertices":[[1.6472480504430935,3.6092204615181678,-0.5014134478590027],[1.6472480504430935,3.6092204615181678,0.3865865521409973],[1.6472480504430935,4.609220461518168,-0.5014134478590027],[1.6472480504430935,4.609220461518168,0.3865865521409973],[3.6472480504430935,3.6092204615181678,-0.5014134478590027],[3.6472480504430935,3.6092204615181678,0.3865865521409973],[3.6472480504430935,4.609220461518168,-0.5014134478590027],[3.6472480504430935,4.609220461518168,0.3865865521409973]],"connections":[{"cell":8,"face":4},{"cell":10,"face":3},null,null,null,null]},{"template":"knit -l1 -y1 x +y1 +l1 +L1","vertices":[[-1.0881864265883676,3.592223313914757,-0.45243882983322964],[-1.0881864265883676,3.592223313914757,0.4355611701667704],[-1.0881864265883676,4.592223313914757,-0.45243882983322964],[-1.0881864265883676,4.592223313914757,0.4355611701667704],[0.9118135734116325,3.592223313914757,-0.45243882983322964],[0.9118135734116325,3.592223313914757,0.4355611701667704],[0.9118135734116325,4.592223313914757,-0.45243882983322964],[0.9118135734116325,4.592223313914757,0.4355611701667704]],"connections":[{"cell":7,"face":4},{"cell":11,"face":3},null,{"cell":9,"face":1},null,null]},{"template":"knit -l1 -y1 x +y1 +l1 +L1","vertices":[[-3.8099794784781498,3.5573815784079716,-0.444],[-3.8099794784781498,3.5573815784079716,0.444],[-3.8099794784781498,4.557381578407972,-0.444],[-3.8099794784781498,4.557381578407972,0.444],[-1.8099794784781498,3.5573815784079716,-0.444],[-1.8099794784781498,3.5573815784079716,0.444],[-1.8099794784781498,4.557381578407972,-0.444],[-1.8099794784781498,4.557381578407972,0.444]],"connections":[{"cell":6,"face":4},null,null,{"cell":10,"face":1},null,null]}]

0 comments on commit 9a42b4b

Please sign in to comment.