Skip to content

Commit

Permalink
Serialize, Deserialize
Browse files Browse the repository at this point in the history
  • Loading branch information
jmickle66666666 committed Dec 24, 2018
1 parent cc2a248 commit 0772d1d
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 14 deletions.
102 changes: 97 additions & 5 deletions js/jzbuilder.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion js/jzbuilder.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/geo/edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class Edge {

public start:Vertex;
public end:Vertex;

public edgeLink:Edge;
public sector : Sector;

Expand Down
4 changes: 4 additions & 0 deletions src/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,14 @@ function onMouseDown(e:MouseEvent) {
if (Tool.activeTool.onMouseDown) {
Tool.activeTool.onMouseDown(e);
}

dirty = true;
}

function onMouseUp(e:MouseEvent) {
if (Tool.activeTool.onMouseUp) {
Tool.activeTool.onMouseUp(e);
}

dirty = true;
}
88 changes: 88 additions & 0 deletions src/io/mapio.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
class MapIO {
static serialize(data:MapData):string {
let timer = Util.timer("serialize");
let output:string = "JZB01\n";
data.sectors.forEach(s => {
output += MapIO.serializeSector(s) + "\n";
});
// console.log(output);
timer.stop();
return output;
}

static serializeSector(sector:Sector):string {
let output:string = "s(";
for (let i = 0; i < sector.edges.length; i++) {
output += MapIO.serializeEdge(sector.edges[i]);
}
// sector.edges.forEach(e=> {
// output+=MapIO.serializeEdge(e);
// });
return output;
}

static serializeEdge(edge:Edge):string {
return "e("+MapIO.serializeVertex(edge.start)+MapIO.serializeVertex(edge.end)+")";
}

static serializeVertex(vertex:Vertex):string {
return "v(" +vertex.x + "," + vertex.y + ")";
}


static unserialize(data:string):MapData {
let timer = Util.timer("unserialize");
let output:MapData = new MapData();
output.sectors.length = 0;
let lines:Array<string> = data.split('\n');
if (lines[0] != "JZB01") {
console.error("Incorrect version. Expected JZB01, got "+lines[0]);
return null;
}

for (let i = 1; i < lines.length; i++) {
output.sectors.push(MapIO.unserializeSector(lines[i]));
}

output.updateEdgePairs();
timer.stop();
return output;
}

static unserializeSector(data:string):Sector {

let output:Sector = new Sector();

// s(e(v(1,1)v(1,1))e(v(1,1)v(1,1))e(v(1,1)v(1,1)))

let edges:Array<string> = data.split('e');

// s( (v(1,1)v(1,1)) (v(1,1)v(1,1)) (v(1,1)v(1,1)))

for (let i = 1; i < edges.length; i++) {
output.edges.push(MapIO.unserializeEdge(edges[i]));
}

output.update();
return output;
}

static unserializeEdge(data:string):Edge {
// (v(1,1)v(1,1))...

let vs:Array<string> = data.split('v');

// ( (1,1) (1,1))...
let e:Edge = new Edge(MapIO.unserializeVertex(vs[1]), MapIO.unserializeVertex(vs[2]));
// console.log(e);
return e;
}

static unserializeVertex(data:string):Vertex {
let output : Vertex = new Vertex(0, 0);
let commaIndex:number = data.indexOf(',');
output.x = Number(data.substr(1, commaIndex - 1));
output.y = Number(data.substr(commaIndex + 1, data.indexOf(')') - commaIndex - 1));
return output;
}
}
14 changes: 14 additions & 0 deletions src/mapdata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,20 @@ class MapData {

}

quicksaveData:string;

quicksave():void {
this.quicksaveData = MapIO.serialize(this);
}

quickload():void {
mapData = MapIO.unserialize(this.quicksaveData);
}

testload():void {
mapData = MapIO.unserialize(MapIO.serialize(this));
}

// Keeping this in case there's something i can take from it

// createSplits(v:Vertex) {
Expand Down
24 changes: 16 additions & 8 deletions src/tools/basetool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,28 @@ class BaseTool implements ITool {

} else if (Input.mode == InputMode.SECTOR && this.selectedSectors.length != 0) {

ContextMenu.create(
new MenuItem(
"Selected Sectors: " + this.selectedSectors.length,
null
)
);
// ContextMenu.create(
// new MenuItem(
// "Selected Sectors: " + this.selectedSectors.length,
// null
// )
// );

} else {
// general menu!

ContextMenu.create(
new MenuItem(
"Test",
function() { console.log("clicked Test item") }
"Testload",
function() { mapData.testload(); }
),
new MenuItem(
"Quickload",
function() { mapData.quickload(); }
),
new MenuItem(
"Quicksave",
function() { mapData.quicksave(); }
)
);

Expand Down
11 changes: 11 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ class Util {
return Math.sqrt(Util.sqrDist(a, b));
}

static timer(name:string) {
var start = new Date();
return {
stop: function() {
var end = new Date();
var time = end.getTime() - start.getTime();
console.log('Timer:', name, 'finished in', time, 'ms');
}
}
};

// From: https://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment
static sqr(x) { return x * x }
static dist2(v, w) { return Util.sqr(v.x - w.x) + Util.sqr(v.y - w.y) }
Expand Down

0 comments on commit 0772d1d

Please sign in to comment.