Commit 08538035 authored by Christian Winter's avatar Christian Winter

selection stuff for prefabs

parent 4238c455
class Prefab{
constructor() {
// this.position = Vector2.zero;
this.lines = [];
this.name = "Prefab";
this.instances = [];
}
get bounds() {
let min = new Vector2(Infinity, Infinity);
let max = new Vector2(-Infinity, -Infinity);
for (let line of this.lines) {
min.x = Math.min(min.x, line.start.position.x);
min.y = Math.min(min.y, line.start.position.y);
max.x = Math.max(max.x, line.start.position.x);
max.y = Math.max(max.y, line.start.position.y);
min.x = Math.min(min.x, line.end.position.x);
min.y = Math.min(min.y, line.end.position.y);
max.x = Math.max(max.x, line.end.position.x);
max.y = Math.max(max.y, line.end.position.y);
}
return new Bounds(min, max);
}
addLine(line) {
this.lines.push(line);
}
......@@ -48,4 +28,40 @@ class PrefabInstance {
this.reference.instances.push(this);
}
destroy() {
Utilities.deleteArrayEntry(this.reference.instances, this);
}
selectionHit() {
for (let line of this.reference.lines) {
// prepare line
line = line.copy().move(this.position);
// check for hit
if (Utilities.distancePointToLine(mousePosition, line) <= cursorRange)
return true;
}
return false;
}
get bounds() {
let min = new Vector2(Infinity, Infinity);
let max = new Vector2(-Infinity, -Infinity);
for (let line of this.lines) {
min.x = Math.min(min.x, line.start.position.x);
min.y = Math.min(min.y, line.start.position.y);
max.x = Math.max(max.x, line.start.position.x);
max.y = Math.max(max.y, line.start.position.y);
min.x = Math.min(min.x, line.end.position.x);
min.y = Math.min(min.y, line.end.position.y);
max.x = Math.max(max.x, line.end.position.x);
max.y = Math.max(max.y, line.end.position.y);
}
return new Bounds(min.addVector(this.position), max.addVector(this.position));
}
}
\ No newline at end of file
......@@ -213,6 +213,13 @@ class PrefabDict {
return kv[1];
}
removeInstance(instance) {
for (let i = 0; i < this.keyValuePairs.length; ++i)
if (this.keyValuePairs[i][0] == instance.reference)
Utilities.deleteArrayEntry(this.keyValuePairs[i][1], instance);
}
deletePrefab(prefab) {
for (let i = 0; i < this.keyValuePairs.length; ++i) {
if (this.keyValuePairs[i][0] == prefab) {
......
......@@ -125,6 +125,8 @@ class MouseHandler {
let lines = File.currentLayer.lines.concat(Selection.data.lines).concat(Selection.data.partialLines);
let pointsToChangeSelection = [];
let prefabInstancesToChangeSelection = [];
if (Preferences.selectVertices && Preferences.selectEdges) {
let s, e, m, _m;
......@@ -145,20 +147,17 @@ class MouseHandler {
pointsToChangeSelection.push(line.end);
}
}
// check prefabs
// check prefab instances
// 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")
}
}
//*
for (let kv of File.currentLayer.prefabDict.keyValuePairs) {
let prefab = kv[0];
let instances = kv[1];
for (let instance of instances)
if (instance.selectionHit())
prefabInstancesToChangeSelection.push(instance);
}
*/
// */
} else if (Preferences.selectEdges) {
for (let i = 0; i < lines.length; i++) {
if (Utilities.distancePointToLine(mousePosition, lines[i]) <= cursorRange) {
......@@ -173,9 +172,14 @@ class MouseHandler {
}
}
// change selection
Selection.changeSelectionForPoints(pointsToChangeSelection);
if (pointsToChangeSelection.length > 0) {
for (let instance of prefabInstancesToChangeSelection)
Selection.data.addPrefabInstance(instance);
if (pointsToChangeSelection.length > 0 || prefabInstancesToChangeSelection.length > 0) {
Logic.setState(new MoveLinesState());
Logic.currentState.initializedWithRMBDown = true;
}
......
......@@ -7,9 +7,13 @@
static clearSelection(dontTriggerChangedEvent) {
File.currentLayer.lines = File.currentLayer.lines.concat(this.data.lines).concat(this.data.partialLines);
for (let instance of Selection.data.prefabInstances)
File.currentLayer.prefabDict.addInstance(instance);
this.data.points = [];
this.data.partialLines = [];
this.data.lines = [];
this.data.prefabInstances = [];
if (dontTriggerChangedEvent != true)
this.data.changed();
......@@ -20,11 +24,18 @@
this.data.lines = File.currentLayer.lines;
File.currentLayer.lines = [];
for (let kv of File.currentLayer.prefabDict.keyValuePairs) {
let prefab = kv[0];
let instances = kv[1];
for (let i = instances.length - 1; i >= 0; --i)
Selection.data.addPrefabInstance(instances[i]);
}
this.data.changed();
}
static noSelection() {
return this.data.points.length == 0 && this.data.partialLines.length == 0 && this.data.lines.length == 0;
return this.data.points.length == 0 && this.data.partialLines.length == 0 && this.data.lines.length == 0 && this.data.prefabInstances.length == 0;
}
static invertSelection() {
......@@ -36,6 +47,18 @@
this.data.points[i] = this.data.points[i].opposite;
}
tmp = this.data.prefabInstances;
this.data.prefabInstances = [];
for (let kv of File.currentLayer.prefabDict.keyValuePairs) {
let prefab = kv[0];
let instances = kv[1];
for (let i = instances.length - 1; i >= 0; --i)
Selection.data.addPrefabInstance(instances[i]);
}
for (let instance of tmp)
File.currentLayer.prefabDict.addInstance(instance);
this.data.changed();
}
......@@ -49,6 +72,11 @@
this.data.points = [];
for (let instance of this.data.prefabInstances)
instance.destroy();
this.data.prefabInstances = [];
File.updateStats();
File.currentLayer.changed();
this.data.changed();
......@@ -207,6 +235,14 @@ class SelectionData {
//this.changed();
}
addPrefabInstance(instance) {
if (this.prefabInstances.includes(instance))
return;
File.currentLayer.prefabDict.removeInstance(instance);
this.prefabInstances.push(instance);
}
copy() {
let newSelData = new SelectionData();
......
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