Commit ca7f708d authored by Christian Winter's avatar Christian Winter

internal structure. bevore renaming 'Camera.' to 'Application.currentView.camera'

parent 830e0a0f
......@@ -68,6 +68,17 @@
<script src="scripts/PrefabManager.js"></script>
<script src="scripts/Prefab.js"></script>
<script src="scripts/View.js"></script>
<script src="scripts/Application.js"></script>
<script>
function onLoad() {
var App = new Application();
}
</script>
</head>
<body onload="onLoad();">
<div id="menubar">
......
class Application {
constructor() {
console.log("Application.constructor()");
this.initializeClasses();
this.currentState; // Drawing, EditingPrefab
this.views = [];
this.currentView = new View();
window.addEventListener("keydown", evt => Input.keyDown(evt), false);
window.addEventListener("keyup", evt => Input.keyUp(evt), false);
GUI.genereateGridSettings();
Logic.start();
}
initializeClasses() {
GUI.init();
Preferences.init();
//Key.init();
Input.init();
MouseHandler.init();
Logic.init();
Saver.init();
File.init();
//Camera.init();
Renderer.init();
RenderManager.init();
Exporter.init();
Settings.init();
Selection.init();
GridManager.init();
ActionHistory.init();
PrefabManager.init();
}
}
\ No newline at end of file
class Utilities {
static init() {
}
static getMousePos(e) {
Utilities = new class {
getMousePos(e) {
let rect = canvas.getBoundingClientRect();
return new Vector2(
e.clientX - rect.left,
......@@ -10,7 +7,7 @@ class Utilities {
);
}
static deleteArrayEntry(array, entry) {
deleteArrayEntry(array, entry) {
for (var i = 0; i < array.length; ++i) {
if (array[i] == entry) {
return array.splice(i, 1)[0];
......@@ -19,13 +16,13 @@ class Utilities {
return null;
}
static movePointsBy(points, delta) {
movePointsBy(points, delta) {
for (let i = 0; i < points.length; ++i) {
points[i].position = points[i].position.addVector(delta);
}
}
static moveSelectionBy(delta) {
moveSelectionBy(delta) {
let points = Selection.getAllSelectedPoints();
for (let instance of Selection.data.prefabInstances)
......@@ -36,7 +33,7 @@ class Utilities {
}
}
static calculateBBCenter(points) {
calculateBBCenter(points) {
let min = new Vector2(Infinity, Infinity);
let max = new Vector2(-Infinity, -Infinity);
......@@ -55,7 +52,7 @@ class Utilities {
return center;
}
static calculateCenterOfMass(points) {
calculateCenterOfMass(points) {
let centerOfMass = new Vector2(0, 0);
for (let point of points) {
......@@ -67,7 +64,7 @@ class Utilities {
return centerOfMass;
}
static snapSelectedPointsToGrid() {
snapSelectedPointsToGrid() {
if (!Preferences.showGrid)
return;
......@@ -79,7 +76,7 @@ class Utilities {
Renderer.redraw();
}
static distancePointToLine(position, line) {
distancePointToLine(position, line) {
let se = line.end.position.subtractVector(line.start.position);
let sp = position.subtractVector(line.start.position);
let ep = position.subtractVector(line.end.position);
......@@ -91,7 +88,7 @@ class Utilities {
return Math.abs((se.x * sp.y - se.y * sp.x) / se.magnitude());
}
static cutWithSelection() {
cutWithSelection() {
let selLines = Selection.data.lines.concat(Selection.data.partialLines);
for (let line of selLines) {
......@@ -100,7 +97,7 @@ class Utilities {
Renderer.redraw();
}
static cutLines(cutter, lines, useDrawnLineAsRealLine) {
cutLines(cutter, lines, useDrawnLineAsRealLine) {
let intersections = [];
intersections.push(cutter.start);
this.addPointSorted(intersections, cutter.end);
......@@ -132,15 +129,15 @@ class Utilities {
}
}
static addPointSorted(points, point) {
addPointSorted(points, point) {
for (let i = 0; i < points.length; i++) {
if (eq(point.position.y, points[i].position.y) && eq(point.position.x, points[i].position.x)) {
if (this.eq(point.position.y, points[i].position.y) && this.eq(point.position.x, points[i].position.x)) {
if (points[i].line == null)
points[i].line = point.line;
return;
}
if (point.position.x > points[i].position.x ||
(eq(point.position.x, points[i].position.x) && point.position.y > points[i].position.y)) {
(this.eq(point.position.x, points[i].position.x) && point.position.y > points[i].position.y)) {
points.splice(i, 0, point);
return;
}
......@@ -148,7 +145,7 @@ class Utilities {
points.push(point);
}
static intersect(line1, line2) {
intersect(line1, line2) {
let points = [];
let v1 = line1.end.position.subtractVector(line1.start.position);
let v2 = line2.end.position.subtractVector(line2.start.position);
......@@ -159,7 +156,7 @@ class Utilities {
let s = ls / rs;
let t = lt / rt;
if (eq(ls, 0) && eq(rs, 0) && eq(lt, 0) && eq(rt, 0)) {
if (this.eq(ls, 0) && this.eq(rs, 0) && this.eq(lt, 0) && this.eq(rt, 0)) {
let minX = Math.min(line1.start.position.x, line1.end.position.x, line2.start.position.x, line2.end.position.x);
let minY = Math.min(line1.start.position.y, line1.end.position.y, line2.start.position.y, line2.end.position.y);
let maxX = Math.max(line1.start.position.x, line1.end.position.x, line2.start.position.x, line2.end.position.x);
......@@ -181,7 +178,7 @@ class Utilities {
return points;
}
static linesToLineEndings(lines) {
linesToLineEndings(lines) {
let points = [];
for (let line of lines) {
points.push(line.start);
......@@ -190,7 +187,7 @@ class Utilities {
return points;
}
static mergeSelectedPoints() {
mergeSelectedPoints() {
let endings = Selection.getAllSelectedPoints();
let center = this.calculateBBCenter(endings);
......@@ -201,7 +198,7 @@ class Utilities {
}
// TODO make it possible to pass in, position as Vector2 and rotate as angle
static makeSketchy(position, positionSteps, angle, angleStep) {
makeSketchy(position, positionSteps, angle, angleStep) {
let originalData = Selection.data.copy();
File.createNewLayer(true, File.currentLayer.name + "_Sketch");
......@@ -241,7 +238,7 @@ class Utilities {
}
// TODO when extruding line multiple times the line and points create the same outer lines...
static extrudeSelection() {
extrudeSelection() {
if (Logic.currentState instanceof InputRecorderState)
Logic.currentState.execute();
......@@ -269,14 +266,14 @@ class Utilities {
Logic.setState(new MoveLinesState());
}
static pointWithinCircle(p, center, radius) {
pointWithinCircle(p, center, radius) {
if (p instanceof LineEnding)
return Vector2.distance(p.position, center) <= radius + (p.line.thickness * 0.5);
return Vector2.distance(p, center) <= radius;
}
static fill(points) {
fill(points) {
if (points.length > 20)
if (!confirm("Are you sure you want to contine with the fill operation with " + points.length + " points. This could take a while to compute."))
return;
......@@ -294,7 +291,7 @@ class Utilities {
Renderer.redraw();
}
static makeCircle(center, radius, segments, overshoot) {
makeCircle(center, radius, segments, overshoot) {
let stepSize = 360 / segments;
let rad = stepSize * (Math.PI / 180);
for (let i = 0; i < segments; ++i) {
......@@ -317,7 +314,7 @@ class Utilities {
Renderer.redraw();
}
static measure() {
measure() {
let points = Selection.getAllSelectedPoints();
let x;
......@@ -333,11 +330,10 @@ class Utilities {
}
}
}
}
// TODO put into class
function eq(a, b) {
let margin = 0.1;
let diff = Math.abs(a - b)
return diff < margin;
}
\ No newline at end of file
eq(a, b) {
let margin = 0.1;
let diff = Math.abs(a - b)
return diff < margin;
}
}
class View {
constructor() {
this.canvas = document.getElementById('canvas');
this.canvas.contentEditable = true; // to make canvas.focus() working
this.canvas.addEventListener("mousemove", evt => MouseHandler.mouseMove(evt));
this.canvas.addEventListener("mouseup", evt => MouseHandler.mouseUp(evt));
this.canvas.addEventListener("mousedown", evt => MouseHandler.mouseDown(evt));
this.canvas.addEventListener("wheel", evt => MouseHandler.mouseScroll(evt));
this.canvas.addEventListener("mouseenter", evt => MouseHandler.canvasMouseEnter(evt));
this.canvas.addEventListener("mouseleave", evt => MouseHandler.canvasMouseLeave(evt));
this.canvas.style.background = Settings.canvasColor;
this.context = this.canvas.getContext('2d');
this.camera = new Camera(this);
this.camera.canvasOffset.x = this.canvas.width * 0.5;
this.camera.canvasOffset.y = this.canvas.height * 0.5;
// this.uiButtons = [];
// this.uiButtonPanel= [];
Renderer.setContext(this.context);
}
// onmouseenter
// onmouseleave
}
\ No newline at end of file
......@@ -10,7 +10,6 @@ class MouseHandler {
}
static start() {
Camera.multiplyZoomBy(1, true);
this.mouseMoved(new Vector2(0, 0));
}
......@@ -29,7 +28,7 @@ class MouseHandler {
Logic.currentState.update(screenPosDelta);
} else {
// calculate cursor position and delta and stuff
mousePosition = Camera.screenSpaceToCanvasSpace(newPosScreenSpace.copy());
mousePosition = Application.currentView.camera.screenSpaceToCanvasSpace(newPosScreenSpace.copy());
this.updateCurrentPosition();
this.oldPos = currentPosition;
......
class Camera {
static init() {
console.log("Camera created.");
constructor(view) {
this.view = view;
this.zoom = 1;
this.minZoom = 0.1;
this.maxZoom = 1000;
this.canvasOffset = new Vector2(0, 0);
// TODO better way to initialize?
this.multiplyZoomBy(1, true);
}
static multiplyZoomBy(delta, keepCenter)
multiplyZoomBy(delta, keepCenter)
{
if (keepCenter)
this.pushCameraCenter();
......@@ -20,7 +23,7 @@
Renderer.redraw();
}
static zoomBy(delta, keepCenter) {
zoomBy(delta, keepCenter) {
if (keepCenter)
this.pushCameraCenter();
......@@ -31,27 +34,27 @@
Renderer.redraw();
}
static setZoom(val, keepCenter) {
setZoom(val, keepCenter) {
if (keepCenter)
this.pushCameraCenter();
console.log(this)
this.zoom = Math.min(this.maxZoom, Math.max(this.minZoom, val));
if (keepCenter)
this.popCameraCenter();
Renderer.redraw();
GUI.writeToStats("Zoom", (this.zoom * 100).toFixed(2) + " %");
}
static pushCameraCenter() {
pushCameraCenter() {
// TODO maybe there is a better option than saving center and comparing difference?
this.center = new Vector2(canvas.width * 0.5, canvas.height * 0.5);
this.center = new Vector2(this.view.canvas.width * 0.5, this.view.canvas.height * 0.5);
this.worldCenter = this.screenSpaceToCanvasSpace(this.center);
}
static popCameraCenter() {
popCameraCenter() {
let newWorldCenter = this.screenSpaceToCanvasSpace(this.center);
let diff = newWorldCenter.subtractVector(this.worldCenter);
this.canvasOffset = this.canvasOffset.addVector(diff);
......@@ -61,26 +64,27 @@
}
static screenSpaceToCanvasSpace(vec2) {
screenSpaceToCanvasSpace(vec2) {
return vec2
.divide(this.zoom)
.subtractVector(this.canvasOffset);
}
static canvasSpaceToScreenSpace(vec2) {
canvasSpaceToScreenSpace(vec2) {
return vec2
.addVector(this.canvasOffset)
.multiply(this.zoom)
;
}
static getVisibleBounds() {
getVisibleBounds() {
return new Bounds(
this.screenSpaceToCanvasSpace(new Vector2(0, 0)),
this.screenSpaceToCanvasSpace(new Vector2(Renderer.currentContext.canvas.width, Renderer.currentContext.canvas.height))
);
}
static resetView() {
resetView() {
pivot = Vector2.zero;
this.setZoom(1, false);
......
......@@ -118,9 +118,6 @@ class Logic {
canvas.style.background = Settings.canvasColor;
this.layoutGUI();
Camera.canvasOffset.x = canvas.width * 0.5;
Camera.canvasOffset.y = canvas.height * 0.5;
Saver.loadAutoSave();
File.updateStats();
......
class Renderer {
static init() {
console.log("DrawManager created.");
console.log("Renderer created.");
this.batchedLines = [];
this.batchedCircles = [];
this.screenBounds = null;
......
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