Skip to content

Commit fce6c37

Browse files
committed
Graph Saving
1 parent 871f86b commit fce6c37

2 files changed

Lines changed: 138 additions & 3 deletions

File tree

src/public/component/component.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ let bindtonode = false;
2323
let start = null;
2424
let end = null;
2525

26+
let json;
27+
2628
function Main()
2729
{
30+
graph = new Graph();
2831
url = ParseURL();
2932
RC3 = document.getElementById("studios.vanish.component.3D");
3033
RC2 = document.getElementById("studios.vanish.component.2D");
@@ -62,7 +65,9 @@ function Main()
6265
function UpdateURL()
6366
{
6467
let url = "@" + round(ME.Camera.Location.X, 2) + "," + round(ME.Camera.Location.Y, 2) + "," + round(ME.Camera.Location.Z, 2) + "z" + ((RenderedFloor / 2) + 1);
65-
window.dispatchEvent(new CustomEvent("_event_onURLChange", { detail: { camera: round(ME.Camera.Location.X, 2) + ", " + round(ME.Camera.Location.Y, 2) + ", " + round(ME.Camera.Location.Z, 2)}}));
68+
//url += "?graph=" + JSON.stringify(graph.ToJson());
69+
70+
window.dispatchEvent(new CustomEvent("_event_onURLChange", { detail: { camera: round(ME.Camera.Location.X, 2) + ", " + round(ME.Camera.Location.Y, 2) + ", " + round(ME.Camera.Location.Z, 2) } }));
6671
window.history.replaceState({ "html": url }, "", url)
6772
}
6873
function round(num, p)
@@ -75,6 +80,10 @@ function ParseURL()
7580
if (url.includes("@"))
7681
{
7782
let paramstr = url.substring(url.indexOf("@") + 1);
83+
if (paramstr.includes("?"))
84+
{
85+
paramstr = paramstr.substring(0, url.indexOf("?"));
86+
}
7887
while (paramstr.includes(","))
7988
{
8089
param.push(paramstr.substring(0, paramstr.indexOf(",")));
@@ -101,6 +110,7 @@ function ParseURL()
101110
RenderedFloor = 0;
102111
}
103112
let url_search = new URL(url);
113+
json = url_search.searchParams.get("graph");
104114
url = url.substring(url.indexOf("/") + 1);
105115
if (url.includes("?")) url = url.substring(0, url.indexOf("?"));
106116
if (url.endsWith("/")) url = url.substring(0, url.length - 1);
@@ -112,7 +122,10 @@ function ParseURL()
112122
}
113123
function Initialize()
114124
{
115-
graph = new Graph();
125+
if (json)
126+
{
127+
graph.FromJson(ME, JSON.parse(json));
128+
}
116129
}
117130
function _event_onKeyPress(event)
118131
{
@@ -305,6 +318,10 @@ function _event_onNavigationSelect(event)
305318
graph.GetPath(start, end);
306319
}
307320
}
321+
else if (navigation === "_navigation_file_save")
322+
{
323+
graph.ToJson();
324+
}
308325
}
309326
function _event_onMouseDown(event)
310327
{
@@ -384,6 +401,7 @@ function _event_modal_onOk(event)
384401
_event_onNavigationSelect({ detail: { key: "_navigation_node_bulkcreate", number: bulkcreate-- } });
385402
}, 1);
386403
}
404+
UpdateURL();
387405
}
388406
function _event_modal_onDelete(event)
389407
{
@@ -398,6 +416,7 @@ function _event_modal_onDelete(event)
398416
}
399417
selectedNode = null;
400418
selectedNodeIndex = -1;
419+
UpdateURL();
401420
}
402421
function _event_modal_onCreateNeighbor(event)
403422
{
@@ -445,6 +464,7 @@ function _event_modal_onElementExecuteIndex(event)
445464
}
446465
function _event_modal_onElementDelete(event)
447466
{
467+
UpdateURL();
448468
for (let i = 0; i < graph.Elements.length; i++)
449469
{
450470
if (graph.Elements[i].Node === selectedNode)
@@ -500,5 +520,4 @@ function Render()
500520
ME.Device2D.lineTo(MousePosition.X, MousePosition.Y);
501521
ME.Device2D.stroke();
502522
}
503-
//g.Render(ME, 0);
504523
}

src/public/component/element.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,122 @@ let Graph = class Graph
189189
ME.Device2D.strokeStyle = '#000000';
190190
}
191191
}
192+
ToJson()
193+
{
194+
let js = {};
195+
js["nodes"] = [];
196+
for (let i = 0; i < this.Nodes.length; i++)
197+
{
198+
let child = {
199+
"name": this.Nodes[i].Name,
200+
"id": this.Nodes[i].ID,
201+
"location": {
202+
"x": this.Nodes[i].Location.X,
203+
"y": this.Nodes[i].Location.Y,
204+
"z": this.Nodes[i].Location.Z
205+
},
206+
"enabled": this.Nodes[i].Enabled,
207+
"neighbors": []
208+
};
209+
for (let j = 0; j < this.Nodes[i].Neighbors.length; j++)
210+
{
211+
child["neighbors"].push({
212+
"distance": this.Nodes[i].Neighbors[j].Distance,
213+
"end": this.Nodes[i].Neighbors[j].EndNode.ID,
214+
});
215+
}
216+
js["nodes"].push(child);
217+
}
218+
js["elements"] = [];
219+
for (let i = 0; i < this.Elements.length; i++)
220+
{
221+
let id = null;
222+
if (this.Elements[i].Node)
223+
{
224+
id = this.Elements[i].Node.ID;
225+
}
226+
let child = {
227+
"name": this.Elements[i].Name,
228+
"type": this.Elements[i].Type,
229+
"node": id,
230+
"vertices": [],
231+
"indices": []
232+
}
233+
for (let j = 0; j < this.Elements[i].Object.Vertices.length; j++)
234+
{
235+
child["vertices"].push([
236+
this.Elements[i].Object.Vertices[j].X,
237+
this.Elements[i].Object.Vertices[j].Y,
238+
this.Elements[i].Object.Vertices[j].Z,
239+
this.Elements[i].Object.Vertices[j].R,
240+
this.Elements[i].Object.Vertices[j].G,
241+
this.Elements[i].Object.Vertices[j].B,
242+
this.Elements[i].Object.Vertices[j].A,
243+
]);
244+
}
245+
for (let j = 0; j < this.Elements[i].Object.Indices.length; j++)
246+
{
247+
child["indices"].push(this.Elements[i].Object.Indices[j].indices);
248+
}
249+
js["elements"].push(child);
250+
}
251+
console.log(JSON.stringify(js));
252+
return js;
253+
}
254+
FromJson(ME, js)
255+
{
256+
console.log(js);
257+
for (let i = 0; i < js["nodes"].length; i++)
258+
{
259+
let n = new Node(js["nodes"][i]["name"], new Vertex(js["nodes"][i]["location"]["x"], js["nodes"][i]["location"]["y"], js["nodes"][i]["location"]["z"]));
260+
n.ID = js["nodes"][i]["id"];
261+
n.Enabled = js["nodes"][i]["enabled"];
262+
this.Nodes.push(n);
263+
}
264+
for (let i = 0; i < this.Nodes.length; i++)
265+
{
266+
let n = this.Nodes[i];
267+
for (let j = 0; j < js["nodes"][i]["neighbors"].length; j++)
268+
{
269+
for (let k = 0; k < this.Nodes.length; k++)
270+
{
271+
if (this.Nodes[k].ID === js["nodes"][i]["neighbors"][j]["end"])
272+
{
273+
n.Neighbors.push(new Neighbor(this.Nodes[k], js["nodes"][i]["neighbors"][j]["distance"]));
274+
break;
275+
}
276+
}
277+
}
278+
}
279+
for (let i = 0; i < js["elements"].length; i++)
280+
{
281+
let v = [];
282+
for (let j = 0; j < js["elements"][i]["vertices"].length; j++)
283+
{
284+
let c = js["elements"][i]["vertices"][j];
285+
v.push(new GraphicsVertex(c[0], c[1], c[2], c[3], c[4], c[5], c[6]));
286+
}
287+
let ind = [];
288+
for (let j = 0; j < js["elements"][i]["indices"].length; j++)
289+
{
290+
let c = js["elements"][i]["indices"][j];
291+
ind.push(new Index(c[0], c[1], c[2]));
292+
}
293+
console.log(v);
294+
let n = new Element(new Object3D(ME, v, ind, "New Object"), js["elements"][i]["name"], js["elements"][i]["type"]);
295+
if (js["elements"][i]["node"] != null)
296+
{
297+
for (let k = 0; k < this.Nodes.length; k++)
298+
{
299+
if (this.Nodes[k].ID === js["elements"][i]["node"])
300+
{
301+
n.BindToNode(this.Nodes[k]);
302+
}
303+
}
304+
}
305+
this.Elements.push(n);
306+
}
307+
}
192308
}
193309
let nID = 0;
194310
let Node = class Node

0 commit comments

Comments
 (0)