Commit c81c62d3 authored by Christian Winter's avatar Christian Winter

wip. prefabs deleting.

parent 5dd37b80
......@@ -221,6 +221,7 @@
<div class="toolcategory">
<p>Prefabs</p>
<button type="button" onclick="PrefabManager.createNewPrefab()">Create new Prefab</button>
<button type="button" onclick="PrefabManager.createNewPrefabFromSelection()">Create new Prefab from selection</button>
</div>
......
......@@ -3,6 +3,7 @@
this.position = Vector2.zero;
this.lines = [];
this.name = "Prefab";
this.instances = [];
}
get bounds() {
......@@ -44,5 +45,7 @@ class PrefabInstance {
this.position = position == undefined ? Vector2.zero : position;
this.rotation = rotation == undefined ? 0 : rotation;
this.scale = scale == undefined ? 1 : scale;
this.reference.instances.push(this);
}
}
\ No newline at end of file
......@@ -8,6 +8,20 @@
this.prefabs.push(prefab);
}
static createNewPrefab() {
if (Logic.backlogState)
if (Logic.backlogState instanceof EditPrefabState)
Logic.backlogState.apply();
let newPrefab = new Prefab();
this.addPrefab(newPrefab);
this.editPrefab(this.prefabs.length - 1);
GUI.buildPrefabUI();
Renderer.redraw();
}
static createNewPrefabFromSelection() {
let newPrefab = new Prefab();
newPrefab.lines = Selection.data.lines.concat(Selection.data.partialLines);
......@@ -37,8 +51,35 @@
}
static deletePrefabWithID(id) {
// TODO convert prefabs to lines
Selection.clearSelection();
for (let instance of this.prefabs[id].instances) {
for (let line of this.prefabs[id].lines) {
let newLine = line.copy();
newLine.move(instance.position);
newLine.start.position = LineManipulator.rotatePositionByAngle(newLine.start.position, instance.position, instance.rotation);
newLine.end.position = LineManipulator.rotatePositionByAngle(newLine.end.position, instance.position, instance.rotation);
newLine.start.position = LineManipulator.scalePoint(newLine.start.position, instance.position, instance.scale);
newLine.end.position = LineManipulator.scalePoint(newLine.end.position, instance.position, instance.scale);
Selection.data.addLine(newLine);
}
}
for (let layer of File.layers) {
layer.prefabDict.deletePrefab(this.prefabs[id]);
}
if (Logic.backlogState) {
if (Logic.backlogState instanceof EditPrefabState) {
Logic.backlogState.cancel();
}
}
Utilities.deleteArrayEntry(this.prefabs, this.prefabs[id]);
GUI.buildPrefabUI();
Renderer.redraw();
}
}
\ No newline at end of file
......@@ -5,7 +5,7 @@
this.hideInRenderPreview = false;
this.lines = [];
this.prefabInstances = [];
this.prefabDict = new PrefabDict();
}
addLine(line) {
......@@ -51,7 +51,7 @@
this.changed();
}
removeLine(line) { // SIFU TODO difference to deleteSelectedLine? just the parameter? could get selected and pass as parameter?
for (var i = 0; i < this.lines.length; ++i) {
if (this.lines[i] == line) {
......@@ -65,11 +65,12 @@
}
addPrefabInstance(instance) {
this.prefabInstances.push(instance);
this.prefabDict.addInstance(instance);
}
removePrefabInstance(instance) {
Utilities.deleteArrayEntry(this.prefabInstances, instance);
Utilities.deleteArrayEntry(
getInstancesForPrefab(instance.reference), instance);
}
cleanUp() {
......@@ -110,7 +111,7 @@
}
return points;
}
selectAllToggle() {
if (Selection.noSelection())
Selection.selectEverything();
......@@ -178,10 +179,46 @@
return layer;
}
changed()
{
changed() {
//console.log("layer changed")
//ActionHistory.writeHistory();
//this.cleanUp(); // causes bug when extruding...
}
}
class PrefabDict {
constructor() {
this.keyValuePairs = [];
}
addInstance(instance) {
for (let kv of this.keyValuePairs) {
if (kv[0] === instance.reference) {
kv[1].push(instance);
return;
}
}
// no key-value-pair for prefab
let newKV = [];
newKV[0] = instance.reference;
newKV[1] = [];
newKV[1].push(instance);
this.keyValuePairs.push(newKV);
}
getInstancesForPrefab(prefab) {
for (let kv of this.keyValuePairs)
if (kv[0] === prefab)
return kv[1];
}
deletePrefab(prefab) {
for (let i = 0; i < this.keyValuePairs.length; ++i) {
if (this.keyValuePairs[i][0] == prefab) {
this.keyValuePairs.splice(i, 1)[0];
return;
}
}
}
}
\ No newline at end of file
......@@ -139,6 +139,20 @@ class MouseHandler {
pointsToChangeSelection.push(line.end);
}
}
// check prefabs
// TODO unify with code above?
/*
for (let prefab of File.currentLayer.prefabInstances) {
console.log(prefab)
for (let p of Utilities.linesToLineEndings(prefab.reference.lines)) {
if (Utilities.pointWithinCircle(p, mousePosition.subtractVector(prefab.position), cursorRange)){
pointsToChangeSelection.push(p); // TODO check for duplicates for multiple overlapping prefabs
console.log("asf")
}
}
}
*/
} else if (Preferences.selectEdges) {
for (let i = 0; i < lines.length; i++) {
if (Utilities.distancePointToLine(mousePosition, lines[i]) <= cursorRange) {
......
......@@ -653,17 +653,24 @@
}
}
for (let prefabInstance of layer.prefabInstances) {
// TODO multibatching? or dont delete batch after rendering to reuse?
// green outline
for (let i=0; i<layer.prefabDict.keyValuePairs.length; ++i) {
let prefab = layer.prefabDict.keyValuePairs[i][0];
let instances = layer.prefabDict.keyValuePairs[i][1];
// TODO multibatching? or dont delete batch after rendering to reuse?
// green outline
if (!Logic.isPreviewing()) {
for (let line of prefabInstance.reference.lines)
this.batchLine(line.copy().move(prefabInstance.position));
for (let instance of instances) {
for (let line of prefab.lines)
this.batchLine(line.copy().move(instance.position));
}
this.renderBatchedLines(2, 'green', false, false);
}
for (let line of prefabInstance.reference.lines)
this.batchLine(line.copy().move(prefabInstance.position));
for (let instance of instances) {
for (let line of prefab.lines)
this.batchLine(line.copy().move(instance.position));
}
this.renderBatchedLines(1, 'black', false, false);
}
}
......
......@@ -39,7 +39,6 @@
}
apply() {
console.log("apply")
this.currentPrefab.lines = File.currentLayer.lines.concat(Selection.data.lines).concat(Selection.data.partialLines);
this.restore();
}
......@@ -57,7 +56,6 @@
this.restore();
}
toString() {
return "EditPrefabState";
}
......
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