Commit 460511ad authored by Christian Winter's avatar Christian Winter

cursor class now includes working getter for currentPosition

parent ca908553
......@@ -5,7 +5,10 @@
}
// TODO use me
get currentPosition() {
return GridManager.grid.getNearestPointFor(this.position);
if (Application.shouldSnap())
return GridManager.grid.getNearestPointFor(this.position);
else
return this.position.copy();
}
intersect(obj) {
......@@ -13,12 +16,21 @@
throw("not defined for Line!")
} else if (obj instanceof LineEnding) {
let radius = this.range + (obj.line.thickness * 0.5);
console.log(Vector2.sqrDistance(obj.position, this.position) <= radius * radius)
return Vector2.sqrDistance(obj.position, this.position) <= radius * radius;
} else if (obj instanceof Vector2) {
return Vector2.sqrDistance(obj, this.position) <= this.range * this.range;
}
console.log("false")
return false;
}
withinRange(factor) {
withinTimesRange(pos, times) {
if (pos instanceof LineEnding)
pos = pos.position;
let radius = this.range;
return Vector2.sqrDistance(this.position, pos) <= radius * radius;
}
}
\ No newline at end of file
......@@ -54,21 +54,21 @@
this.logic.currentState.update(screenPosDelta);
} else {
// calculate cursor position and delta and stuff
mousePosition = this.camera.screenSpaceToCanvasSpace(newPosScreenSpace.copy());
this._updateCurrentPosition();
Application.cursor.position = this.camera.screenSpaceToCanvasSpace(newPosScreenSpace.copy());
//this._updateCurrentPosition();
this.oldPos = currentPosition;
this.oldPos = Application.cursor.currentPosition;
// handle individual states
if (this.logic.currentState instanceof ContinuousDrawingState) {
this.logic.currentState.update();
} else if (this.logic.currentState instanceof InputRecorderState) {
this.logic.currentState.currentPos = mousePosition.copy();
this.logic.currentState.currentPos = Application.cursor.position.copy();
this.logic.currentState.updateValue();
}
else if (this.logic.currentState instanceof BoxSelectionState) {
if (this.logic.currentState.startPos) {
this.logic.currentState.currentPos = mousePosition.copy();
this.logic.currentState.currentPos = Application.cursor.position.copy();
}
} else if (this.logic.currentState instanceof BrushSelectionState) {
this.logic.currentState.update();
......@@ -80,7 +80,7 @@
this.logic.currentState instanceof DrawingState ||
this.logic.currentState instanceof ContinuousDrawingState ||
this.logic.currentState instanceof MeasureState) {
GUI.writeToStatusbarLeft(currentPosition.toString());
GUI.writeToStatusbarLeft(Application.cursor.currentPosition.toString());
}
}
......@@ -129,7 +129,7 @@
if (Input.isKeyDown(Key.Alt))
pivot = new Vector2(0, 0);
else
pivot = currentPosition.copy();
pivot = Application.cursor.currentPosition;
if (Preferences.gridType == 2)
if (placePolarGridCenterAt2DCursor == true)
......@@ -242,7 +242,7 @@
Application.redrawAllViews();
}
else if (this.logic.currentState instanceof ContinuousDrawingState) {
this.logic.currentState.oldPos = Application.shouldSnap() ? currentPosition.copy() : mousePosition.copy();
this.logic.currentState.oldPos = Application.shouldSnap() ? Application.cursor.currentPosition : Application.cursor.position.copy();
}
else if (this.logic.currentState instanceof MeasureState) {
this.logic.currentState.mouseDown(0);
......@@ -415,14 +415,7 @@
this.logic.currentState.exit()
}
}
_updateCurrentPosition() {
currentPosition = mousePosition.copy();
if (Application.shouldSnap()) // TODO should also change button text
currentPosition = GridManager.grid.getNearestPointFor(currentPosition);
}
_adjustMouseDownButtonBools(e, state) {
if (e.button == 0)
this.LMBDown = state;
......@@ -512,7 +505,7 @@
break;
case Key.Control:
this._updateCurrentPosition();
//this._updateCurrentPosition();
Application.redrawAllViews();
break;
case Key.E:
......@@ -558,7 +551,7 @@
break;
case Key.Control:
this._updateCurrentPosition();
//this._updateCurrentPosition();
Application.redrawAllViews();
break;
}
......@@ -639,9 +632,10 @@
// TODO extract one part into data class? and other into selection-data?
// TODO withinRadius, magic number? use epsilon?
getAllLineEndingsAt(clickPoint, withinRadius) {
let points = [];
let lines = this.data.lines.concat(this.selection.data.partialLines); // TODO maybe put function in other file? not sure if this should be in Layer...
let lines = this.data.lines.concat(this.selection.data.partialLines);
for (let i = 0; i < lines.length; ++i) {
if (Utilities.pointWithinCircle(lines[i].start.position, clickPoint, withinRadius))
points.push(lines[i].start);
......@@ -664,7 +658,7 @@
_m = line.start.position.addVector(line.end.position);
_m = _m.divide(2);
_m = new LineEnding(_m.x, _m.y, line);
m = Application.cursor.intersect(_m, mousePosition, Application.cursor.range);
m = Application.cursor.intersect(_m);
if (s && !m && !e)
pointsToChangeSelection.push(line.start);
......@@ -673,10 +667,10 @@
else if (m) { // if one line ending and the middle of the line is within cursor range select whole line. otherwise weird selection happens when whole line is inside cursor range
pointsToChangeSelection.push(line.start);
pointsToChangeSelection.push(line.end);
} else if (Utilities.distancePointToLine(mousePosition, line) <= Application.cursor.range + (line.thickness * 0.5)) {
if (Utilities.pointWithinCircle(line.start, mousePosition, Application.cursor.range * 3))
} else if (Utilities.distancePointToLine(Application.cursor.position, line) <= Application.cursor.range + (line.thickness * 0.5)) {
if (Application.cursor.withinTimesRange(line.start, 3))
pointsToChangeSelection.push(line.start);
else if (Utilities.pointWithinCircle(line.end, mousePosition, Application.cursor.range * 3))
else if (Application.cursor.withinTimesRange(line.end, 3))
pointsToChangeSelection.push(line.end);
else {
pointsToChangeSelection.push(line.start);
......@@ -698,7 +692,7 @@
// */
} else if (Preferences.selectEdges) {
for (let i = 0; i < lines.length; i++) {
if (Utilities.distancePointToLine(mousePosition, lines[i]) <= Application.cursor.range) {
if (Utilities.distancePointToLine(Application.cursor.position, lines[i]) <= Application.cursor.range) {
pointsToChangeSelection.push(lines[i].start);
pointsToChangeSelection.push(lines[i].end);
}
......
......@@ -14,7 +14,7 @@
selectionHit() {
for (let line of this.getAsLines())
if (Utilities.distancePointToLine(mousePosition, line) <= Application.cursor.range)
if (Utilities.distancePointToLine(Application.cursor.position, line) <= Application.cursor.range)
return true;
for (let instance of this.reference.data.instances)
......@@ -23,7 +23,7 @@
line.start.position = this.objectSpaceForPosition(line.start.position);
line.end.position = this.objectSpaceForPosition(line.end.position);
if (Utilities.distancePointToLine(mousePosition, line) <= Application.cursor.range)
if (Utilities.distancePointToLine(Application.cursor.position, line) <= Application.cursor.range)
return true;
}
}
......
......@@ -5,9 +5,6 @@ var offscreenContext;
let toolbar;
var currentPosition = new Vector2(0, 0);
let mousePosition = new Vector2(0, 0);
let currentLineThickness = 1;
let currentLineColor = Color.black();
......
......@@ -430,12 +430,12 @@
let screenpos;
if (dontSnap)
screenpos = this.view.camera.canvasSpaceToScreenSpace(mousePosition.copy());
screenpos = this.view.camera.canvasSpaceToScreenSpace(Application.cursor.position.copy());
else
screenpos = this.view.camera.canvasSpaceToScreenSpace(currentPosition.copy());
screenpos = this.view.camera.canvasSpaceToScreenSpace(Application.cursor.currentPosition);
if (this.view.logic.currentState.artBounds)
screenpos = this.view.camera.canvasSpaceToScreenSpace(mousePosition.copy().rounded());
screenpos = this.view.camera.canvasSpaceToScreenSpace(Application.cursor.position.copy().rounded());
this.drawLineFromTo(new Vector2(0, screenpos.y), new Vector2(this.view.canvas.width, screenpos.y), Settings.crossHairLineWidth, Settings.crossHairColor, true, true);
this.drawLineFromTo(new Vector2(screenpos.x, 0), new Vector2(screenpos.x, this.view.canvas.height), Settings.crossHairLineWidth, Settings.crossHairColor, true, true);
......@@ -615,7 +615,7 @@
drawPreviewLine() {
if (this.view.logic.currentState instanceof DrawingState) {
let start = this.view.logic.currentState.startPos;
let end = currentPosition;
let end = Application.cursor.currentPosition;
this.drawLineFromTo(start, end, currentLineThickness, currentLineColor, false);
} else if (this.view.logic.currentState instanceof MeasureState) {
if (this.view.logic.currentState.startPosLMB && !this.view.logic.currentState.stoppedLMB) {
......@@ -638,11 +638,11 @@
}
this.drawRealCircle(currentPosition, currentLineThickness * 0.5, 1, currentLineColor.toString(), false, true, true);
this.drawRealCircle(mousePosition, Application.cursor.range, 2, Settings.selectionColor, false, true);
this.drawRealCircle(Application.cursor.currentPosition, currentLineThickness * 0.5, 1, currentLineColor.toString(), false, true, true);
this.drawRealCircle(Application.cursor.position, Application.cursor.range, 2, Settings.selectionColor, false, true);
if (this.view.logic.currentState instanceof BrushSelectionState)
this.drawRealCircle(mousePosition, Application.cursor.range, 2, Settings.boxSelectionColor, false, true, true);
this.drawRealCircle(Application.cursor.position, Application.cursor.range, 2, Settings.boxSelectionColor, false, true, true);
else if (this.view.logic.currentState instanceof MeasureState)
this.view.context.setLineDash([]);
......@@ -779,8 +779,8 @@
for (let line of lines)
this.drawLineFromTo(
line.start.position.addVector(currentPosition),
line.end.position.addVector(currentPosition),
line.start.position.addVector(Application.cursor.currentPosition),
line.end.position.addVector(Application.cursor.currentPosition),
line.thickness + Settings.prefabOutlineSize,
color
);
......
......@@ -150,7 +150,7 @@
selectionHit() {
for (let line of this.data.lines)
if (Utilities.distancePointToLine(mousePosition, line) <= Application.cursor.range)
if (Utilities.distancePointToLine(Application.cursor.position, line) <= Application.cursor.range)
return true;
for (let p of this.data.points)
......@@ -159,7 +159,7 @@
for (let instance of this.data.instances)
for (let line of instance.getAsLines())
if (Utilities.distancePointToLine(mousePosition, line) <= Application.cursor.range)
if (Utilities.distancePointToLine(Application.cursor.position, line) <= Application.cursor.range)
return true;
return false;
......
......@@ -9,8 +9,8 @@
start(selectType) {
this.selectionType = selectType;
this.startPos = mousePosition.copy();
this.currentPos = mousePosition.copy();
this.startPos = Application.cursor.position.copy();
this.currentPos = Application.cursor.position.copy();
}
execute() {
......
......@@ -28,10 +28,10 @@
else if (m) { // if one line ending and the middle of the line is within cursor range select whole line. otherwise weird selection happens when whole line is inside cursor range
newSelPoints.push(line.start);
newSelPoints.push(line.end);
} else if (Utilities.distancePointToLine(mousePosition, line) <= Application.cursor.range + (line.thickness * 0.5)) {
if (Utilities.pointWithinCircle(line.start, mousePosition, Application.cursor.range * 3))
} else if (Utilities.distancePointToLine(Application.cursor.position, line) <= Application.cursor.range + (line.thickness * 0.5)) {
if (Application.cursor.withinTimesRange(line.start, 3))
newSelPoints.push(line.start);
else if (Utilities.pointWithinCircle(line.end, mousePosition, Application.cursor.range * 3))
else if (Application.cursor.withinTimesRange(line.end, 3))
newSelPoints.push(line.end);
else {
newSelPoints.push(line.start);
......@@ -41,7 +41,7 @@
}
} else if (Preferences.selectEdges) {
for (let line of Application.currentView.data.lines.concat(Application.currentView.selection.data.partialLines)) {
if (Utilities.distancePointToLine(mousePosition, line) <= Application.cursor.range + (line.thickness * 0.5)) {
if (Utilities.distancePointToLine(Application.cursor.position, line) <= Application.cursor.range + (line.thickness * 0.5)) {
newSelPoints.push(line.start);
newSelPoints.push(line.end);
}
......@@ -93,10 +93,10 @@
else if (m) { // if one line ending and the middle of the line is within cursor range select whole line. otherwise weird selection happens when whole line is inside cursor range
newUnselPoints.push(line.start);
newUnselPoints.push(line.end);
} else if (Utilities.distancePointToLine(mousePosition, line) <= Application.cursor.range + (line.thickness * 0.5)) {
if (Utilities.pointWithinCircle(line.start, mousePosition, Application.cursor.range * 3))
} else if (Utilities.distancePointToLine(Application.cursor.position, line) <= Application.cursor.range + (line.thickness * 0.5)) {
if (Application.cursor.withinTimesRange(line.start, 3))
newUnselPoints.push(line.start);
else if (Utilities.pointWithinCircle(line.end, mousePosition, Application.cursor.range * 3))
else if (Application.cursor.withinTimesRange(line.end, 3))
newUnselPoints.push(line.end);
else {
newUnselPoints.push(line.start);
......@@ -106,7 +106,7 @@
}
} else if (Preferences.selectEdges) {
for (let line of Application.currentView.selection.data.lines.concat(Application.currentView.selection.data.partialLines)) {
if (Utilities.distancePointToLine(mousePosition, line) <= Application.cursor.range + (line.thickness * 0.5)) {
if (Utilities.distancePointToLine(Application.cursor.position, line) <= Application.cursor.range + (line.thickness * 0.5)) {
newUnselPoints.push(line.start);
newUnselPoints.push(line.end);
}
......
......@@ -18,7 +18,7 @@
if (this.oldPos != undefined) {
this.lineStart = this.oldPos.copy();
this.lineEnd = Application.shouldSnap() ? currentPosition.copy() : mousePosition.copy();
this.lineEnd = Application.shouldSnap() ? Application.cursor.currentPosition : Application.cursor.position.copy();
if (this.lineStart.x != this.lineEnd.x || this.lineStart.y != this.lineEnd.y)
......@@ -32,9 +32,9 @@
}
if (Application.shouldSnap())
this.oldPos = currentPosition.copy();
this.oldPos = Application.cursor.currentPosition;
else
this.oldPos = mousePosition.copy();
this.oldPos = Application.cursor.position.copy();
}
toString() {
......
class DrawingState extends State {
enter() {
this.startPos = currentPosition.copy();
this.startPos = Application.cursor.currentPosition;
}
exit() {
......@@ -8,7 +8,7 @@
return;
let start = this.startPos;
let end = currentPosition.copy();
let end = Application.cursor.currentPosition;
if (start.x != end.x || start.y != end.y) {
Application.currentView.data.addLine(new Line(start, end));
......
......@@ -11,16 +11,16 @@
mouseDown(button) {
if (button == 0) {
this.startPosLMB = currentPosition.copy();
this.currentPosLMB = currentPosition.copy();
this.startPosLMB = Application.cursor.currentPosition;
this.currentPosLMB = Application.cursor.currentPosition;
this.stoppedLMB = false;
} else if (button == 1) {
this.startPosMMB = currentPosition.copy();
this.currentPosMMB = currentPosition.copy();
this.startPosMMB = Application.cursor.currentPosition;
this.currentPosMMB = Application.cursor.currentPosition;
this.stoppedMMB = false;
} else if (button == 2) {
this.startPosRMB = currentPosition.copy();
this.currentPosRMB = currentPosition.copy();
this.startPosRMB = Application.cursor.currentPosition;
this.currentPosRMB = Application.cursor.currentPosition;
this.stoppedRMB = false;
}
}
......@@ -44,11 +44,11 @@
}
update() {
if (this.startPosLMB && !this.stoppedLMB)
this.currentPosLMB = currentPosition.copy();
this.currentPosLMB = Application.cursor.currentPosition;
else if (this.startPosMMB && !this.stoppedMMB)
this.currentPosMMB = currentPosition.copy();
this.currentPosMMB = Application.cursor.currentPosition;
else if (this.startPosRMB && !this.stoppedRMB)
this.currentPosRMB = currentPosition.copy();
this.currentPosRMB = Application.cursor.currentPosition;
}
keyDown(keycode) {
......
......@@ -4,8 +4,8 @@
}
enter() {
this.startPos = mousePosition.copy();
this.currentPos = mousePosition.copy();
this.startPos = Application.cursor.position.copy();
this.currentPos = Application.cursor.position.copy();
}
exit() {
......
......@@ -21,9 +21,9 @@
if (dontPlacePrefabButDuplicateEachLineIndividually) {
// TODO addLines(lines)
for (let line of this.currentPrefab.getAsLines())
Application.currentView.data.addLine(line.copy().move(currentPosition));
Application.currentView.data.addLine(line.copy().move(Application.cursor.currentPosition));
} else {
Application.currentView.data.addInstance(new PrefabInstance(this.currentPrefab, currentPosition));
Application.currentView.data.addInstance(new PrefabInstance(this.currentPrefab, Application.cursor.currentPosition));
}
Application.currentView.data.changed();
}
......
......@@ -5,8 +5,8 @@
enter() {
// BUT maybe not needed if inherited from inputrecorder...
// todo maybe pass this as argument to inputrecorder-constructor and call 'this'.methods()?
this.startPos = mousePosition.copy();
this.currentPos = mousePosition.copy();
this.startPos = Application.cursor.position.copy();
this.currentPos = Application.cursor.position.copy();
this.updateValue();
}
......
......@@ -4,8 +4,8 @@
}
enter() {
this.startPos = mousePosition.copy();
this.currentPos = mousePosition.copy();
this.startPos = Application.cursor.position.copy();
this.currentPos = Application.cursor.position.copy();
//this.updateValue();
}
......
class ZoomState extends State {
enter() {
this.startZoom = Application.currentView.camera.zoom;
this.startPosScreenSpace = Application.currentView.camera.canvasSpaceToScreenSpace(mousePosition);
this.startPosScreenSpace = Application.currentView.camera.canvasSpaceToScreenSpace(Application.cursor.position);
}
exit() {
......
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