Squaby Demo
var lastMousePoints = new Array;
var lastMousePointIndex = 0;
var waypoints = new Array;
var lastWaypointIndex;
var lastWaypoint;
var horizontalDifferenceWasBiggerFirst;
var verticalDifferenceWasBiggerFirst;
function pressed(mouseX, mouseY) {
delete waypoints;
waypoints = new Array;
lastWaypointIndex = -1;
addWaypoint(mouseX, mouseY);
horizontalDifferenceWasBiggerFirst = false;
verticalDifferenceWasBiggerFirst = false;
}
function positionChanged(mouseX, mouseY) {
var dx = Math.abs(lastWaypoint.x-mouseX);
var dy = Math.abs(lastWaypoint.y-mouseY);
console.debug("PathCreationLogic: positionChanged to", mouseX, mouseY, "delta:", dx, dy);
if(dx>pathSize && dy>pathSize) {
var snappedPos;
if(dx > dy) {
console.debug("creating a waypoint horizontally")
snappedPos = Qt.point(getSnappedValue(mouseX, lastWaypoint.x), lastWaypoint.y);
} else {
console.debug("creating a waypoint vertically")
snappedPos = Qt.point(lastWaypoint.x, getSnappedValue(mouseY, lastWaypoint.y));
}
console.debug("creating a wayopint at position", snappedPos.x, snappedPos.y)
addWaypoint(snappedPos.x, snappedPos.y);
}
}
function released(mouseX, mouseY) {
console.debug("PathCreationLogic: released:", mouseX, mouseY)
var dx = Math.abs(lastWaypoint.x-mouseX);
var dy = Math.abs(lastWaypoint.y-mouseY);
if(dy>pathSize){
var snappedPos = Qt.point(lastWaypoint.x, getSnappedValue(mouseY, lastWaypoint.y));
addWaypoint(snappedPos.x, snappedPos.y)
console.debug("add final waypoint at pos", snappedPos.x, snappedPos.y)
}
if(dx>pathSize){
var snappedPos = Qt.point(getSnappedValue(mouseX, lastWaypoint.x), lastWaypoint.y);
addWaypoint(snappedPos.x, snappedPos.y)
console.debug("add final waypoint at pos", snappedPos.x, snappedPos.y)
}
}
function addWaypoint(snappedPosX, snappedPosY) {
var newWaypoint = Qt.point(snappedPosX, snappedPosY)
waypoints.push( newWaypoint );
lastWaypointIndex++;
lastWaypoint=waypoints[lastWaypointIndex];
var snappedPos = {"x": snappedPosX, "y": snappedPosY};
console.debug("addWaypoint called for pos", snappedPos.x, snappedPos.y)
waypointCreated(snappedPos);
}
function getSnappedValue(newXOrY, lastXOrY) {
var delta = newXOrY-lastXOrY;
var division = delta/pathSize;
var rounded = Math.round(division);
var snappedDelta= pathSize*rounded;
var snappedAbsoluteValue = lastXOrY+snappedDelta
console.debug("to snap value:", newXOrY, "rounded:", rounded, "snappedValue:", snappedAbsoluteValue)