Commit 5dd37b80 authored by Christian Winter's avatar Christian Winter

editing prefabs working basically

parent 18043837
......@@ -29,6 +29,7 @@
<script src="scripts/states/ZoomState.js"></script>
<script src="scripts/states/RotateState.js"></script>
<script src="scripts/states/ScaleState.js"></script>
<script src="scripts/states/EditPrefabState.js"></script>
<script src="scripts/managers/Camera.js"></script>
<script src="scripts/input/MouseHandler.js"></script>
......
......@@ -39,8 +39,10 @@
}
class PrefabInstance {
constructor(position, prefabReference) {
this.position = position;
constructor(prefabReference, position, rotation, scale) {
this.reference = prefabReference;
this.position = position == undefined ? Vector2.zero : position;
this.rotation = rotation == undefined ? 0 : rotation;
this.scale = scale == undefined ? 1 : scale;
}
}
\ No newline at end of file
......@@ -10,16 +10,21 @@
static createNewPrefabFromSelection() {
let newPrefab = new Prefab();
newPrefab.lines = Selection.data.lines.concat(Selection.data.partialLines);
for (let line of Selection.data.lines.concat(Selection.data.partialLines))
newPrefab.addLine(line.copy());
File.currentLayer.addPrefabInstance(new PrefabInstance(newPrefab, Vector2.zero));
// TODO check for nested prefabs
Selection.data = new SelectionData();
this.addPrefab(newPrefab);
GUI.buildPrefabUI();
Renderer.redraw();
}
static editPrefab(id) {
GUI.notify("Editing prefabs doesn't work yet.", 'green')
Logic.backlogState = new EditPrefabState(this.prefabs[id]);
GUI.notify("Editing prefab.", 'green')
}
static selectPrefabWithID(id) {
......
......@@ -70,6 +70,14 @@
case Key.E:
Utilities.extrudeSelection();
break;
case Key.Enter:
if (Logic.backlogState) {
if (Logic.backlogState instanceof EditPrefabState) {
Logic.setState(Logic.backlogState);
Logic.currentState.apply();
}
}
break;
}
......
......@@ -61,6 +61,7 @@ class Logic {
this.previousState = new IdleState();
this.isRenderPreviewing = false;
this.waitingState = null;
this.backlogState = null;
}
static start() {
......@@ -117,7 +118,7 @@ class Logic {
Camera.canvasOffset.x = canvas.width * 0.5;
Camera.canvasOffset.y = canvas.height * 0.5;
//*
/*
Saver.loadAutoSave();
/*/
Saver.newFile();
......@@ -210,6 +211,9 @@ class Logic {
if (Logic.currentState instanceof ContinousDrawingState)
return;
if (Logic.backlogState)
Logic.backlogState.cancel();
if (Logic.currentState.cancel)
Logic.currentState.cancel();
......
......@@ -133,10 +133,11 @@
}
class SelectionData {
constructor(lines, partialLines, points) {
constructor(lines, partialLines, points, prefabInstances) {
this.lines = lines == undefined ? [] : lines;
this.partialLines = partialLines == undefined ? [] : partialLines;
this.points = points == undefined ? [] : points;
this.prefabInstances = prefabInstances == undefined ? [] : prefabInstances;
}
addPoint(point) {
......@@ -222,6 +223,9 @@ class SelectionData {
newSelData.addPoint(line.start);
}
// TODO copy()? foreach?
newSelData.prefabInstances = this.prefabInstances
return newSelData;
}
......
class EditPrefabState extends State {
constructor(prefab) {
super();
this.currentPrefab = prefab;
this.store();
}
enter() {
}
exit() {
}
store() {
this.selectionData = Selection.data;
this.layers = File.layers;
this.currentLayer = File.currentLayer;
File.layers = [];
Selection.data = new SelectionData();
File.currentLayer = new Layer();
for (let line of this.currentPrefab.lines)
File.currentLayer.lines.push(line.copy());
Logic.setState(new IdleState());
Renderer.redraw();
}
restore() {
File.layers = this.layers;
File.currentLayer = this.currentLayer;
Selection.data = this.selectionData;
Logic.backlogState = null;
Renderer.redraw();
}
apply() {
console.log("apply")
this.currentPrefab.lines = File.currentLayer.lines.concat(Selection.data.lines).concat(Selection.data.partialLines);
this.restore();
}
keyDown(keycode) {
switch (keycode) {
case Key.Enter:
this.apply();
break;
}
}
cancel() {
this.restore();
}
toString() {
return "EditPrefabState";
}
}
\ No newline at end of file
class PrefabBrushState extends State {
constructor(prefab) {
super();
super();
this.currentPrefab = prefab;
}
......@@ -13,7 +13,7 @@
}
place() {
File.currentLayer.addPrefabInstance(new PrefabInstance(currentPosition, this.currentPrefab));
File.currentLayer.addPrefabInstance(new PrefabInstance(this.currentPrefab, currentPosition));
/*
for (let line of this.currentPrefab.lines)
File.currentLayer.addLine(line.copy().move(currentPosition));
......
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