Commit 4238c455 authored by Christian Winter's avatar Christian Winter

saving and loading prefabs working

parent 6d58f64d
Pipeline #4760619 passed with stage
class Prefab{
constructor() {
this.position = Vector2.zero;
// this.position = Vector2.zero;
this.lines = [];
this.name = "Prefab";
this.instances = [];
......@@ -44,7 +44,7 @@ class PrefabInstance {
this.reference = prefabReference;
this.position = position == undefined ? Vector2.zero : position;
this.rotation = rotation == undefined ? 0 : rotation;
this.scale = scale == undefined ? 1 : scale;
this.scale = scale == undefined ? new Vector2(1, 1) : scale;
this.reference.instances.push(this);
}
......
......@@ -2,6 +2,7 @@
static init() {
console.log("PrefabManager created.");
this.prefabs = [];
GUI.buildPrefabUI();
}
static addPrefab(prefab) {
......@@ -91,4 +92,9 @@
if (Logic.backlogState instanceof EditPrefabState)
Logic.backlogState.apply();
}
static generateIDsForPrefabs() {
for (let i = 0; i < this.prefabs.length; ++i)
this.prefabs[i].id = i;
}
}
\ No newline at end of file
......@@ -84,6 +84,8 @@ class Exporter {
}
static generateSVGString() {
PrefabManager.generateIDsForPrefabs();
let b = Preferences.artBounds;
let svgData = '<';
svgData += 'svg xmlns="http://www.w3.org/2000/svg" version="1.1" ' +
......@@ -97,31 +99,34 @@ class Exporter {
// FIXME should just also use lines in selection...
Selection.clearSelection();
// layers
for (let layer of layers) {
svgData += '\t<g ';
svgData += 'name="' + layer.name + '" ';
svgData += 'visible="' + layer.visible + '" ';
svgData += 'hideInRenderPreview="' + layer.hideInRenderPreview + '" ';
// just for displaying svg
// just for displaying svg
if (layer.visible == false || (layer.hideInRenderPreview && !Preferences.renderGuideLayers))
svgData += 'display="none" ';
svgData += '>\n';
svgData += this.generateSVGStringForLines(layer.lines, 2, (layer.hideInRenderPreview && Preferences.renderGuideLayers));
svgData += this.generateSVGStringForPrefabInstances(layer.prefabDict, 2, (layer.hideInRenderPreview && Preferences.renderGuideLayers));
svgData += '\t</g>\n';
}
// prefabs
svgData += this.generateSVGStringForPrefabs(1);
svgData += '</svg>';
return svgData;
}
static generateSVGStringForLines(lines, indentations, dashedLines) {
let text = "";
let indent = "";
for (var i = 0; i < indentations; i++) {
......@@ -150,4 +155,88 @@ class Exporter {
}
return text;
}
static generateSVGStringForPrefabInstances(prefabDict, indentations, dashedLines) {
let text = "";
let indent = "";
for (var i = 0; i < indentations; i++) {
indent += "\t"
}
for (let kv of prefabDict.keyValuePairs) {
let prefab = kv[0];
let instances = kv[1];
for (let instance of instances) {
text += indent;
text += "<g prefabReferenceID='" + prefab.id + "'";
text += " positionX='" + instance.position.x + "'";
text += " positionY='" + instance.position.y + "'";
text += " rotation='" + instance.rotation + "'";
text += " scaleX='" + instance.scale.x + "'";
text += " scaleY='" + instance.scale.y + "'";
text += "> \n";
for (let line of prefab.lines) {
line = line.copy().move(instance.position);
text +=
indent + "\t" +
'<line ' +
'x1="' + line.start.position.x + '" ' +
'y1="' + line.start.position.y + '" ' +
'x2="' + line.end.position.x + '" ' +
'y2="' + line.end.position.y + '" ' +
'stroke="' + line.color.toHexString() + '" ' +
'stroke-opacity="' + (line.color.a) + '" ' +
'stroke-width="' + line.thickness + '" ' +
'stroke-linecap="round"';
if (dashedLines) {
text += " ";
text += 'style="stroke-dasharray:' + Settings.guideDashSize + ',' + Settings.guideDashSize + '"';
}
text += '/> \n'
}
text += indent + "</g> \n";
}
}
return text;
}
static generateSVGStringForPrefabs(indentations) {
let text = "";
let indent = "";
for (var i = 0; i < indentations; i++) {
indent += "\t"
}
for (let prefab of PrefabManager.prefabs)
{
text += indent;
text += "<g prefabID='" + prefab.id + "' " + "name='" + prefab.name + "'> \n";
for (let line of prefab.lines) {
text +=
indent + "\t" + // additional indent for lines
'<line ' +
'x1="' + line.start.position.x + '" ' +
'y1="' + line.start.position.y + '" ' +
'x2="' + line.end.position.x + '" ' +
'y2="' + line.end.position.y + '" ' +
'stroke="' + line.color.toHexString() + '" ' +
'stroke-opacity="' + (line.color.a) + '" ' +
'stroke-width="' + line.thickness + '" ' +
'stroke-linecap="round"';
text += '/> \n'
}
text += indent + "</g> \n";
}
return text;
}
}
\ No newline at end of file
......@@ -26,6 +26,7 @@
ActionHistory.init();
File.init();
Selection.init();
PrefabManager.init();
File.createNewLayer(true);
File.updateStats();
......@@ -115,6 +116,7 @@
ActionHistory.init();
File.init();
Selection.init();
PrefabManager.init();
//File.createNewLayer(true);
File.updateStats();
......@@ -125,11 +127,46 @@
let vB = viewBox.split(" ");
Preferences.artBounds = new ArtBounds(vB[0], vB[1], vB[2], vB[3]);
// iterate over whole document to get prefabs first
for (let g of svg.childNodes) {
if (g.nodeType != 1)
continue;
let id = g.getAttribute("prefabID");
if (!id)
continue;
let prefab = new Prefab();
prefab.name = g.getAttribute("name");
let lines = [];
for (let line of g.childNodes) {
if (line.nodeType != 1)
continue;
lines.push(new Line(
Number(line.getAttribute("x1")),
Number(line.getAttribute("y1")),
Number(line.getAttribute("x2")),
Number(line.getAttribute("y2")),
Color.hexToColor(line.getAttribute('stroke')),
Number(line.getAttribute('stroke-width'))
));
}
prefab.lines = lines;
PrefabManager.addPrefab(prefab);
}
for (let g of svg.childNodes) {
if (g.nodeType != 1)
continue;
// if prefab skip
let id = g.getAttribute("prefabID");
if (id)
continue;
// else create layer
let layer = File.createNewLayer(true);
layer.name = g.getAttribute("name");
layer.visible = g.getAttribute("visible") == "false" ? false : true;
......@@ -140,6 +177,22 @@
if (line.nodeType != 1)
continue;
// if prefab-instance
id = line.getAttribute("prefabReferenceID");
if (id) {
let pos = new Vector2(
Number(line.getAttribute("positionX")),
Number(line.getAttribute("positionY")));
let rot = Number(line.getAttribute("rotation"));
let scal = new Vector2(
Number(line.getAttribute("scaleX")),
Number(line.getAttribute("scaleY")));
let instance = new PrefabInstance(PrefabManager.prefabs[id], pos, rot, scal);
layer.addPrefabInstance(instance);
continue;
}
lines.push(new Line(
Number(line.getAttribute("x1")),
Number(line.getAttribute("y1")),
......@@ -152,6 +205,8 @@
// not using addLine() because due to 'cutLines' it could lead to unwanted results
layer.lines = lines;
}
GUI.buildPrefabUI();
GUI.objectHierarchyChanged();
Renderer.redraw();
File.updateStats();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment