Commit 18ab0891 authored by Christian Winter's avatar Christian Winter

wip. prefabeditorrenderer

parent d207b2d4
......@@ -50,6 +50,8 @@
<script src="scripts/saving/Saver.js"></script>
<script src="scripts/managers/Renderer.js"></script>
<script src="scripts/managers/SceneRenderer.js"></script>
<script src="scripts/managers/PrefabEditorRenderer.js"></script>
<script src="scripts/managers/GUI.js"></script>
<script src="scripts/saving/Exporter.js"></script>
......@@ -220,7 +222,7 @@
<option value="1">Triangle grid</option>
<option value="2">Polar grid</option>
</select>
<button type="button" onclick="Logic.toggleGridVisiblity(this)">Hide grid</button>
<button type="button" onclick="Application.toggleGridVisiblity(this)">Hide grid</button>
<button type="button" onclick="GridManager.resetGrid();">Reset grid</button>
<div id="gridSettings"></div>
</div>
......@@ -237,16 +239,16 @@
<tr title="Color">
<td>Color</td>
<td><input type="color" id="lineColor" name="color" onchange="Logic.setCurrentLineColor(Color.hexToColor(value));"></td>
<td><input type="color" id="lineColor" name="color" onchange="Application.setCurrentLineColor(Color.hexToColor(value));"></td>
</tr>
<tr title="Alpha value of the line color">
<td>Opacity</td>
<td><input type="range" id="lineAlpha" name="a" min="0" max="1" step="0.05" value="1" onchange="Logic.setCurrentLineAlpha(+value);"></td>
<td><input type="range" id="lineAlpha" name="a" min="0" max="1" step="0.05" value="1" onchange="Application.setCurrentLineAlpha(+value);"></td>
</tr>
<tr title="Line thickness in px">
<td>Size</td>
<td><input type="number" id="lineThickness" name="Line thickness" min="1" max="25" value="1" onchange="Logic.setCurrentLineThickness(+value);"></td>
<td><input type="number" id="lineThickness" name="Line thickness" min="1" max="25" value="1" onchange="Application.setCurrentLineThickness(+value);"></td>
</tr>
</table>
</div>
......@@ -255,7 +257,7 @@
<div class="toolcategory">
<p class="foldingHeader">Layers</p>
<div class="toolcategorycontainer">
<button type="button" onclick="File.createNewLayer(true)">Create new Layer</button>
<button type="button" onclick="Application.sceneView.file.createNewLayer(true)">Create new Layer</button>
<div class="toolcategorycontainerlist" id="layersDiv">
</div>
......
......@@ -12,11 +12,18 @@
}
start() {
this.initializeClasses();
this.sceneView = new View(sceneView);
this.sceneView.renderer = new SceneRenderer(this.sceneView);
this.prefabEditorView = new View(prefabEditor);
// TODO should be in constructor
this.prefabEditorView.renderer = new PrefabEditorRenderer(this.prefabEditorView);
this.renderPreview = new View(renderPreview);
this.renderPreview.renderer = new SceneRenderer(this.renderPreview);
this.initializeClasses();
this.layoutGUI();
......@@ -42,7 +49,7 @@
window.addEventListener('resize', evt => this.layoutGUI(evt), false);
Logic.start();
this.start2();
// center cameras of views
......@@ -52,6 +59,54 @@
this.redrawAllViews();
}
start2() {
window.addEventListener("contextmenu", function (e) { e.preventDefault(); return false; });
window.addEventListener('focus', function () {
Input.clearKeys();
if (Preferences.developerMode)
GUI.notify("received focus");
}, false);
window.onbeforeunload = function () {
if (Input.isKeyDown(Key.Control))
localStorage.removeItem(Saver.autosaveFileName);
else
Saver.autoSave();
Preferences.savePreferences();
};
window.onerror = function () {
if (Preferences.developerMode == true)
GUI.notify("<font color='red'>An error occured! Press F12 to see what went wrong.</font>");
//Logic._clearStates();
};
offscreenCanvas = document.getElementById('offscreenCanvas');
offscreenContext = offscreenCanvas.getContext('2d');
toolbar = document.getElementById('toolbar');
// Setup the dnd listeners.
let dropZone = document.body;
dropZone.addEventListener('dragover', Saver.handleDragOver, false);
dropZone.addEventListener('drop', Saver.handleFileSelect, false);
Saver.loadAutoSave();
this.sceneView.file.updateStats();
for (var i = 0; i < waitingForStart.length; i++) {
waitingForStart[i].start();
}
//ActionHistory.writeHistory();
$(".foldingHeader").each(function () {
$(this).mousedown(function () {
$(this).next().slideToggle();
})
});
}
adjustRenderPreview() {
let max = Math.max(Preferences.artBounds.width, Preferences.artBounds.height);
renderPreview.style.width = Preferences.artBounds.width / max * 300 + "px";
......@@ -66,11 +121,7 @@
Preferences.init();
Input.init();
Logic.init();
Saver.init();
File.init();
//Selection.init();
ActionHistory.init();
PrefabManager.init();
......@@ -166,6 +217,7 @@
this.prefabEditorView.renderer.setQualitySetting(setting);
}
/*
createGUIFromScratch() {
let body = document.body;
......@@ -205,5 +257,37 @@
div.style.width = 250;
div.style.height = window.innerHeight / 2;
body.appendChild(div);
} // */
shouldSnap() {
return (Preferences.snapToGrid && !this.tmpSwitchSnapToGrid()) || (!Preferences.snapToGrid && this.tmpSwitchSnapToGrid());
}
tmpCutLines() {
return Input.isKeyDown(Key.Alt);
}
tmpSwitchSnapToGrid() {
return Input.isKeyDown(Key.Control);
}
toggleGridVisiblity(senderButton) {
Preferences.showGrid = !Preferences.showGrid;
senderButton.innerHTML = Preferences.showGrid ? "Hide grid" : "Show grid";
Application.redrawAllViews();
}
setCurrentLineAlpha(alpha) {
currentLineColor.a = alpha;
this.useCurrentColorForSelection();
}
setCurrentLineColor(color) {
let alpha = currentLineColor.a;
currentLineColor = color;
currentLineColor.a = alpha;
GUI.lineColor.value = color.toHexString();
this.useCurrentColorForSelection();
}
}
\ No newline at end of file
......@@ -101,11 +101,11 @@ class LineManipulator {
}
static growSelection() {
File.growSelection(true);
Application.currentView.file.growSelection(true);
}
static selectLinked() {
File.selectLinked();
Application.currentView.file.selectLinked();
}
static resetPosition(selection) {
......
......@@ -84,7 +84,7 @@ Utilities = new class {
let selLines = selection.data.lines.concat(selection.data.partialLines);
for (let line of selLines) {
Utilities.cutLines(line, File.currentLayer.lines, false);
Utilities.cutLines(line, Application.currentView.file.currentLayer.lines, false);
}
Application.redrawAllViews();
}
......@@ -190,9 +190,10 @@ Utilities = new class {
}
// TODO make it possible to pass in, position as Vector2 and rotate as angle
// FIXME problem with adding layers. can only work on sceneView
makeSketchy(position, positionSteps, angle, angleStep) {
let originalData = Application.currentView.selection.data.copy();
File.createNewLayer(true, File.currentLayer.name + "_Sketch");
Application.currentView.file.createNewLayer(true, Application.currentView.file.currentLayer.name + "_Sketch");
if (position && angle) {
for (var y = -position.y; y <= position.y; ++y) {
......@@ -279,7 +280,7 @@ Utilities = new class {
Application.currentView.selection.clearSelection();
Application.currentView.selection.data = newSelData;
File.updateStats();
Application.currentView.file.updateStats();
Application.redrawAllViews();
}
......@@ -299,10 +300,10 @@ Utilities = new class {
end = end.addVector(direction.multiply(overshoot));
}
File.currentLayer.lines.push(new Line(start, end, currentLineColor, currentLineThickness));
Application.currentView.file.currentLayer.lines.push(new Line(start, end, currentLineColor, currentLineThickness));
}
File.updateStats();
Application.currentView.file.updateStats();
Application.redrawAllViews();
}
......
This diff is collapsed.
class File {
constructor(view) {
constructor(view, createFirstLayer) {
this.view = view;
this.layers = [];
this.currentLayer = null;
let layer = new Layer();
this.layers.push(layer);
this.currentLayer = layer;
}
static createNewLayer(selectNewLayer, name, addToBottom) {
createNewLayer(selectNewLayer, name, addToBottom) {
let layer = new Layer();
if (name)
layer.name = name;
......@@ -22,14 +24,14 @@
return layer;
}
static getCurrentLayerID()
getCurrentLayerID()
{
for (let i = 0; i < this.layers.length; ++i)
if (this.layers[i] == this.currentLayer)
return i;
}
static moveLayerDown(id) {
moveLayerDown(id) {
if (id <= 0)
return;
......@@ -43,7 +45,7 @@
Application.redrawAllViews();
}
static moveLayerUp(id) {
moveLayerUp(id) {
if (id >= this.layers.length-1)
return;
......@@ -57,7 +59,7 @@
Application.redrawAllViews();
}
static deleteLayerWithID(id) {
deleteLayerWithID(id) {
if (id < 0 || id >= this.layers.length)
return;
......@@ -80,7 +82,7 @@
Application.redrawAllViews();
}
static selectLayerWithID(id) {
selectLayerWithID(id) {
if (id < 0 || id >= this.layers.length)
return;
......@@ -94,7 +96,7 @@
Application.redrawAllViews();
}
static toggleVisibilityOfLayerWithID(id) {
toggleVisibilityOfLayerWithID(id) {
if (this.currentLayer)
this.view.selection.clearSelection();
......@@ -107,13 +109,13 @@
GUI.objectHierarchyChanged();
Application.redrawAllViews();
}
static toggleRenderableOfLayerWithID(id) {
toggleRenderableOfLayerWithID(id) {
this.layers[id].hideInRenderPreview= !this.layers[id].hideInRenderPreview;
GUI.objectHierarchyChanged();
Application.redrawAllViews();
}
static selectNextVisibleLayer(id) {
selectNextVisibleLayer(id) {
if (this.currentLayer == this.layers[id]) {
if (this.layers.length == 1) {
this.createNewLayer(true);
......@@ -156,7 +158,7 @@
}
}
static renameLayerWithID(id) {
renameLayerWithID(id) {
let name = prompt("New name for layer: ");
if (name) {
......@@ -165,12 +167,12 @@
GUI.objectHierarchyChanged();
}
static changeNameForLayerWithID(id, name) {
changeNameForLayerWithID(id, name) {
this.layers[id].name = name;
}
// TODO called too often? no bugs, just curious
static updateStats() {
updateStats() {
let amountLines = 0;
for (var i = 0; i < this.layers.length; i++)
amountLines += this.layers[i].lines.length;
......@@ -179,7 +181,7 @@
GUI.writeToStats("Lines in File", amountLines);
}
static addLine(line, select) {
addLine(line, select) {
if (!this.currentLayer) {
console.log("There was no layer, so there has been created one.");
this.createNewLayer(true);
......@@ -193,29 +195,29 @@
}
// SIFU FIXME all duplicates of current object. just poltergeisting...
static getAllPointsAt(point, cursorRange) {
getAllPointsAt(point, cursorRange) {
return this.currentLayer.getAllPointsAt(point, cursorRange);
}
static cleanUp() {
cleanUp() {
this.currentLayer.cleanUp();
}
static selectAllToggle() {
selectAllToggle() {
this.currentLayer.selectAllToggle();
}
static growSelection(redraw) {
growSelection(redraw) {
this.currentLayer.growSelection(redraw);
}
static selectLinked() {
selectLinked() {
this.currentLayer.selectLinked();
}
static removeLine(line) {
removeLine(line) {
this.currentLayer.removeLine(line);
}
static getBounds(padding) {
getBounds(padding) {
let min = new Vector2(Infinity, Infinity);
let max = new Vector2(-Infinity, -Infinity);
......@@ -270,7 +272,7 @@
return new Bounds(min.subtract(maxLineWidth), max.add(maxLineWidth));
}
static changed()
changed()
{
//this.currentLayer.cleanUp();
}
......
......@@ -12,7 +12,7 @@
if (guideLine) {
let done = false;
for (let layer of File.layers) {
for (let layer of Application.sceneView.file.layers) {
if (layer.hideInRenderPreview) {
layer.lines.push(line);
done = true;
......@@ -21,7 +21,7 @@
}
}
if (!done) {
let layer = File.createNewLayer(false, "Guides", true);
let layer = Application.sceneView.file.createNewLayer(false, "Guides", true);
layer.hideInRenderPreview = true;
GUI.objectHierarchyChanged();
......@@ -29,7 +29,7 @@
GUI.notify("Guide layer added.");
}
}
else if (Logic.tmpCutLines()) {
else if (Application.tmpCutLines()) {
Application.currentView.selection.clearSelection();
Utilities.cutLines(line, this.lines, false);
}
......@@ -60,7 +60,7 @@
}
}
File.updateStats();
Application.sceneView.file.updateStats();
this.changed();
}
......@@ -96,7 +96,7 @@
if (deletedLinesCounter > 0)
GUI.notify("Cleaned up " + deletedLinesCounter + " lines.");
File.updateStats();
Application.sceneView.file.updateStats();
}
getAllPointsAt(clickPoint, withinRadius) {
......@@ -120,7 +120,7 @@
// TODO replace me with method in return line
getAllPoints() {
return Utilities.linesToLineEndings(File.currentLayer.lines);
return Utilities.linesToLineEndings(Application.sceneView.file.currentLayer.lines);
}
growSelection(redraw) {
......
......@@ -32,7 +32,7 @@ class Input {
}
if (Application.currentView)
Logic.currentState.keyDown(e.keyCode);
Application.currentView.logic.currentState.keyDown(e.keyCode);
if (Application.currentFocus)
Application.currentFocus.keyDown(e.keyCode);
......@@ -66,7 +66,8 @@ class Input {
switch (e.keyCode) {
default:
Logic.currentState.keyUp(e.keyCode);
if (Application.currentView)
Application.currentView.logic.currentState.keyUp(e.keyCode);
if (Application.currentFocus)
Application.currentFocus.keyUp(e.keyCode);
break;
......@@ -81,7 +82,7 @@ class Input {
static clearKeys() {
for (let keycode in Input.downKeys) {
if (Input.downKeys[keycode] == true) {
Logic.currentState.keyUp(+keycode);
Application.currentView.logic.currentState.keyUp(+keycode);
if (Application.currentFocus)
Application.currentFocus.keyUp(+keycode);
}
......
......@@ -411,7 +411,7 @@
this.layersDiv.removeChild(this.layersDiv.firstChild);
}
let layers = File.layers;
let layers = Application.sceneView.file.layers;
let table = document.createElement("table");
table.setAttribute("id", "layers");
......@@ -458,7 +458,7 @@
button.setAttribute("type", "button");
//button.setAttribute("class", "smallLayerButton");
button.setAttribute("title", "Hide / unhide layer");
button.setAttribute("onclick", "File.toggleVisibilityOfLayerWithID(" + i + ")");
button.setAttribute("onclick", "Application.sceneView.file.toggleVisibilityOfLayerWithID(" + i + ")");
if (layers[i].visible)
button.innerHTML = "O";
else
......@@ -472,7 +472,7 @@
button.setAttribute("type", "button");
//button.setAttribute("class", "smallLayerButton");
button.setAttribute("title", "Hide / unhide for render preview only");
button.setAttribute("onclick", "File.toggleRenderableOfLayerWithID(" + i + ")");
button.setAttribute("onclick", "Application.sceneView.file.toggleRenderableOfLayerWithID(" + i + ")");
if (layers[i].hideInRenderPreview)
button.innerHTML = "::";
else
......@@ -486,12 +486,12 @@
button.setAttribute("type", "button");
button.setAttribute("class", "layerbutton");
button.setAttribute("title", "Rightclick to rename");
button.setAttribute("onmousedown", "File.selectLayerWithID(" + i + ")");
button.setAttribute("onmousedown", "Application.sceneView.file.selectLayerWithID(" + i + ")");
button.setAttribute("oncontextmenu", "this.focus(); document.execCommand('selectAll',false,null);");
button.setAttribute("contenteditable", "true");
button.setAttribute("oninput", "File.changeNameForLayerWithID(" + i + ", this.innerHTML)");
button.setAttribute("oninput", "Application.sceneView.file.changeNameForLayerWithID(" + i + ", this.innerHTML)");
if (File.currentLayer == layers[i]) {
if (Application.sceneView.file.currentLayer == layers[i]) {
button.setAttribute("id", "selectedButton");
tr.setAttribute("id", "selectedLayer");
}
......@@ -504,7 +504,7 @@
button = document.createElement("button");
button.setAttribute("type", "button");
button.setAttribute("title", "Move layer up");
button.setAttribute("onclick", "File.moveLayerUp(" + i + ")");
button.setAttribute("onclick", "Application.sceneView.file.moveLayerUp(" + i + ")");
button.innerHTML = "&uarr;";
td.appendChild(button);
// move layer up button
......@@ -513,7 +513,7 @@
button = document.createElement("button");
button.setAttribute("type", "button");
button.setAttribute("title", "Move layer down");
button.setAttribute("onclick", "File.moveLayerDown(" + i + ")");
button.setAttribute("onclick", "Application.sceneView.file.moveLayerDown(" + i + ")");
button.innerHTML = "&darr;";
td.appendChild(button);
// delete layer button
......@@ -522,7 +522,7 @@
button = document.createElement("button");
button.setAttribute("type", "button");
button.setAttribute("title", "Delete layer");
button.setAttribute("onclick", "File.deleteLayerWithID(" + i + ")");
button.setAttribute("onclick", "Application.sceneView.file.deleteLayerWithID(" + i + ")");
button.innerHTML = "<font color='red'>X</font>";
td.appendChild(button);
}
......
......@@ -14,7 +14,6 @@ let currentLineColor = Color.black();
let drawPolyLine;
let tmpCutLines = false;
let waitingForStart = [];
let mirrorX = false;
......@@ -31,55 +30,7 @@ class Logic {
this.backlogState = null;
}
static start() {
window.addEventListener("contextmenu", function (e) { e.preventDefault(); return false; });
window.addEventListener('focus', function () {
Input.clearKeys();
if (Preferences.developerMode)
GUI.notify("received focus");
}, false);
window.onbeforeunload = function () {
if (Input.isKeyDown(Key.Control))
localStorage.removeItem(Saver.autosaveFileName);
else
Saver.autoSave();
Preferences.savePreferences();
};
window.onerror = function () {
if (Preferences.developerMode == true)
GUI.notify("<font color='red'>An error occured! Press F12 to see what went wrong.</font>");
//Logic._clearStates();
};
offscreenCanvas = document.getElementById('offscreenCanvas');
offscreenContext = offscreenCanvas.getContext('2d');
toolbar = document.getElementById('toolbar');
// Setup the dnd listeners.
let dropZone = document.body;
dropZone.addEventListener('dragover', Saver.handleDragOver, false);
dropZone.addEventListener('drop', Saver.handleFileSelect, false);
Saver.loadAutoSave();
File.updateStats();
for (var i = 0; i < waitingForStart.length; i++) {
waitingForStart[i].start();
}
//ActionHistory.writeHistory();
$(".foldingHeader").each(function () {
$(this).mousedown(function () {
$(this).next().slideToggle();
})
});
}
static setState(state) {
setState(state) {
if (this.currentState == state)
return;
......@@ -96,7 +47,7 @@ class Logic {
}
static setWaitingState(state) {
setWaitingState(state) {
this.waitingState = state;
let text = "null";
if (state)
......@@ -105,7 +56,7 @@ class Logic {
Application.redrawAllViews();
}
static setBacklogState(state) {
setBacklogState(state) {
if (this.backlogState)
this.backlogState.exit();
......@@ -115,73 +66,41 @@ class Logic {
this.backlogState.enter();
}
static useWaitingState() {
useWaitingState() {
this.setState(this.waitingState);
}
static isPreviewing() {
isPreviewing() {
return Application.currentView.isRenderPreviewing;
}
static toggleGridVisiblity(senderButton) {
Preferences.showGrid = !Preferences.showGrid;
senderButton.innerHTML = Preferences.showGrid ? "Hide grid" : "Show grid";
Application.redrawAllViews();
}
static shouldSnap() {
return (Preferences.snapToGrid && !Logic.tmpSwitchSnapToGrid()) || (!Preferences.snapToGrid && Logic.tmpSwitchSnapToGrid());
}
static tmpCutLines() {
return Input.isKeyDown(Key.Alt);
}
static tmpSwitchSnapToGrid() {
return Input.isKeyDown(Key.Control);
}
static _clearStates() {
_clearStates() {
// TODO HACK
if (Logic.currentState instanceof ContinousDrawingState)
if (this.currentState instanceof ContinousDrawingState)
return;
if (Logic.backlogState)
Logic.backlogState.cancel();
if (this.backlogState)
this.backlogState.cancel();
if (Logic.currentState.cancel)
Logic.currentState.cancel();
if (this.currentState.cancel)
this.currentState.cancel();
Logic.previousState = new IdleState();
Logic.setState(new IdleState());
Logic.setWaitingState(null);
this.previousState = new IdleState();
this.setState(new IdleState());
this.setWaitingState(null);
console.log("States cleared.");
// TODO also reset ctrlDown and so on?
}
static setCurrentLineAlpha(alpha) {
currentLineColor.a = alpha;
this.useCurrentColorForSelection();
}
static setCurrentLineColor(color) {
let alpha = currentLineColor.a;
currentLineColor = color;
currentLineColor.a = alpha;
GUI.lineColor.value = color.toHexString();
this.useCurrentColorForSelection();
}
static useCurrentColorForSelection() {
useCurrentColorForSelection() {
for (let line of this.view.selection.data.lines.concat(this.view.selection.data.partialLines))
line.color = currentLineColor.copy();
Application.redrawAllViews();
}
static setCurrentLineThickness(thickness) {
setCurrentLineThickness(thickness) {
currentLineThickness = thickness;
for (let line of this.view.selection.data.lines.concat(this.view.selection.data.partialLines))
......@@ -191,7 +110,7 @@ class Logic {
Application.redrawAllViews();
}