Squaby Demo
import QtQuick 2.0
import "../gameScene/hud"
import "PathCreationLogic.js" as Logic
import Felgo 4.0
Item {
id: draggableArea
property real pathSize: scene.gridSize*2
property bool isDraggingPath: false
property variant firstWaypoint: Qt.point(16, 48)
property variant finalWaypoint: Qt.point(464, 208)
MultiResolutionImage {
id: firstWaypointImage
x: firstWaypoint.x-width/2
y: firstWaypoint.y-height/2
width: 2*gridSize
height: 2*gridSize
source: Qt.resolvedUrl("../../assets/img/menu_labels/range_radius_allowed.png")
}
MultiResolutionImage {
id: finalWaypointImage
x: finalWaypoint.x-width/2
y: finalWaypoint.y-height/2
width: 2*gridSize
height: 2*gridSize
source: Qt.resolvedUrl("../../assets/img/menu_labels/range_radius_forbidden.png")
}
Item {
id: pathCreationDraggedObject
visible: isDraggingPath
Rectangle {
color:"red"
width: pathSize; height:pathSize
opacity: 0.3
anchors.centerIn: parent
}
}
Item {
id: lastWaypointPosition
visible: isDraggingPath
Rectangle {
color:"green"
width: pathSize; height:pathSize
anchors.centerIn: parent
opacity: 0.5
}
}
MouseArea {
anchors.fill: firstWaypointImage
property bool targetReached: false
onPressed: {
isDraggingPath = true
console.debug("start creating a new path")
targetReached = false;
level.pathEntity.initializeFromWaypoints([]);
Logic.pressed(firstWaypoint.x, firstWaypoint.y);
}
onPositionChanged: {
var sceneMouseX = mouseX+firstWaypointImage.x;
var sceneMouseY = mouseY+firstWaypointImage.y;
if(sceneMouseX>draggableArea.width)
sceneMouseX=draggableArea.width;
else if(sceneMouseX<0)
sceneMouseX=0;
if(sceneMouseY>draggableArea.height)
sceneMouseY=draggableArea.height;
else if(sceneMouseY<0)
sceneMouseY=0;
if(targetReached)
return;
if(sceneMouseX>finalWaypointImage.x && sceneMouseX<finalWaypointImage.x+finalWaypointImage.width
&& sceneMouseY>finalWaypointImage.y && sceneMouseY<finalWaypointImage.y+finalWaypointImage.height) {
console.debug("the final waypoint is reached! stop there!")
finishPathToLastWaypoint();
targetReached = true;
} else {
Logic.positionChanged(sceneMouseX, sceneMouseY);
}
pathCreationDraggedObject.x = sceneMouseX;
pathCreationDraggedObject.y = sceneMouseY;
}
onReleased: {
console.debug("the mouse was released, build path to last waypoint")
if(!targetReached) {
finishPathToLastWaypoint();
}
}
onExited: {
console.debug("PathCreationOverlay: mouseArea.onExited")
}
}
function finishPathToLastWaypoint() {
isDraggingPath = false
Logic.released(finalWaypoint.x, finalWaypoint.y)
isDraggingPath = false;
console.debug("update the waypoints of PathEntity to this array:")
var newWaypoints = Logic.waypoints
var tempWP = new Array;
for(var i=0;i<newWaypoints.length; i++) {
console.debug("newWayPoints[", i, "]:", newWaypoints[i].x, newWaypoints[i].y)
tempWP.push({ x: newWaypoints[i].x, y:newWaypoints[i].y });
console.debug("tempWP[", i, "]:", tempWP[i].x, tempWP[i].y)
}
}
signal waypointCreated(variant waypoint)
onWaypointCreated: {
lastWaypointPosition.x = waypoint.x
lastWaypointPosition.y = waypoint.y
console.debug("PathCreationOverlay: new waypoint got created at pos", waypoint.x, waypoint.y)
level.pathEntity.appendSingleWaypoint(waypoint);
}