summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorLorn Potter <lorn.potter@nokia.com>2012-05-29 13:49:12 +1000
committerQt by Nokia <qt-info@nokia.com>2012-05-29 06:33:01 +0200
commita0b89b80c933cca852e62cc73208cdbc0d3dcc25 (patch)
tree2be55e9adeeb6a510657de433a4d2e7014f5b330 /examples
parente2fda7e34f1c8ff39a86800c14ec7cecb085c2af (diff)
downloadqtsensors-a0b89b80c933cca852e62cc73208cdbc0d3dcc25.tar.gz
make mouse move better ine Maze example, add acceleration. Fix up docs
Use only one mouse gif and rotate it instead. also add screensaver inhibit. Change-Id: I4268bdec9346ee389e9b485e8c37d43a1e607128 Reviewed-by: Lincoln Ramsay <lincoln.ramsay@nokia.com>
Diffstat (limited to 'examples')
-rw-r--r--examples/QtSensors/QtSensors_maze/Mouse.qml76
-rw-r--r--examples/QtSensors/QtSensors_maze/QtSensors_maze.qml115
-rw-r--r--examples/QtSensors/QtSensors_maze/content/mouse_left.gifbin970 -> 0 bytes
-rw-r--r--examples/QtSensors/QtSensors_maze/content/mouse_leftdown.gifbin976 -> 0 bytes
-rw-r--r--examples/QtSensors/QtSensors_maze/content/mouse_leftup.gifbin976 -> 0 bytes
-rw-r--r--examples/QtSensors/QtSensors_maze/content/mouse_right.gifbin971 -> 0 bytes
-rw-r--r--examples/QtSensors/QtSensors_maze/content/mouse_rightdown.gifbin977 -> 0 bytes
-rw-r--r--examples/QtSensors/QtSensors_maze/content/mouse_rightup.gifbin975 -> 0 bytes
-rw-r--r--examples/QtSensors/QtSensors_maze/content/mouse_up.gifbin970 -> 0 bytes
-rw-r--r--examples/QtSensors/QtSensors_maze/lib.js8
10 files changed, 89 insertions, 110 deletions
diff --git a/examples/QtSensors/QtSensors_maze/Mouse.qml b/examples/QtSensors/QtSensors_maze/Mouse.qml
index 55dac18..434aceb 100644
--- a/examples/QtSensors/QtSensors_maze/Mouse.qml
+++ b/examples/QtSensors/QtSensors_maze/Mouse.qml
@@ -51,66 +51,46 @@ Item {
y: 0
width: Lib.cellDimension
height: Lib.cellDimension
- state: 'right'
+ property int angle
AnimatedImage {
id: img
+ source: "content/mouse_down.gif"
anchors.fill: parent
visible: true
}
- //for different moves we have different gif animations
- states: [
- State {
- name: "up"
- PropertyChanges { target: img; source: "content/mouse_up.gif" }
- },
- State {
- name: "down"
- PropertyChanges { target: img; source: "content/mouse_down.gif" }
- },
- State {
- name: "left"
- PropertyChanges { target: img; source: "content/mouse_left.gif" }
- },
- State {
- name: "right"
- PropertyChanges { target: img; source: "content/mouse_right.gif" }
- },
-
- State {
- name: "rightup"
- PropertyChanges { target: img; source: "content/mouse_rightup.gif" }
- },
- State {
- name: "rightdown"
- PropertyChanges { target: img; source: "content/mouse_rightdown.gif" }
- },
- State {
- name: "leftup"
- PropertyChanges { target: img; source: "content/mouse_leftup.gif" }
- },
- State {
- name: "leftdown"
- PropertyChanges { target: img; source: "content/mouse_leftdown.gif" }
- }
-
- ]
+ function distance(origX, origY, newX, newY) {
+ return Math.sqrt((Math.pow((newX - origX),2)) + (Math.pow((newY - origY),2)))
+ }
//Function for moving the mouse
function move(newx, newy)
{
- var newstatus = "";
- if (mouse.x < newx)
- newstatus += "right";
- else if (mouse.x > newx)
- newstatus += "left";
- if (mouse.y < newy)
- newstatus += "down";
- else if (mouse.y > newy)
- newstatus += "up";
- mouse.state = newstatus;
+ if (mouse.x === newx && mouse.y === newy)
+ return
+ // somehow this actually works
+//! [0]
+ var a = newy - mouse.y
+ var b = newx - mouse.x
+ var c = distance(mouse.x, mouse.y, newx, newy)
+ var radians_to_degrees = 57.2957795
+
+ if (a > 0)
+ angle = -Math.acos(a / b) * radians_to_degrees
+ else
+ angle = -Math.asin(b / c) * radians_to_degrees
+ if (b > 0)
+ angle = -Math.acos(a / c) * radians_to_degrees
+ else
+ angle = Math.acos(a / c) * radians_to_degrees
+
+ if (angle < 0)
+ angle = 360 + angle
+
+ img.rotation = angle
mouse.x = newx;
mouse.y = newy;
+//! [0]
}
}
diff --git a/examples/QtSensors/QtSensors_maze/QtSensors_maze.qml b/examples/QtSensors/QtSensors_maze/QtSensors_maze.qml
index f52c00f..40c1d1d 100644
--- a/examples/QtSensors/QtSensors_maze/QtSensors_maze.qml
+++ b/examples/QtSensors/QtSensors_maze/QtSensors_maze.qml
@@ -75,6 +75,7 @@ import "components"
//! [0]
import QtSensors 5.0
//! [0]
+import QtSystemInfo 5.0
//Import the javascript functions for this game
import "lib.js" as Lib
@@ -85,6 +86,7 @@ ApplicationWindow {
property Mouse mouseCtrl;
property LabyrinthSquare cheeseSquare;
property Congratulation congratulation;
+ ScreenSaver { screenSaverEnabled: !tiltTimer.running }
Rectangle {
id: gameRect
@@ -108,17 +110,17 @@ ApplicationWindow {
//create labyrinth elements (only at the first time)
var needloadcomponent = false;
- if (Lib.objectArray === null){
+ if (Lib.objectArray === null) {
needloadcomponent = true;
Lib.objectArray = new Array(Lib.dimension * Lib.dimension);
}
var idx = 0;
- for (var y = 0; y < Lib.dimension; y++ ){
- for (var x = 0; x < Lib.dimension; x++ ){
+ for (var y = 0; y < Lib.dimension; y++ ) {
+ for (var x = 0; x < Lib.dimension; x++ ) {
var component = null;
//create labyrinth components (only at the first time)
- if (needloadcomponent){
+ if (needloadcomponent) {
component = Qt.createComponent("LabyrinthSquare.qml");
if (component.status == Component.Ready) {
var square = component.createObject(parent);
@@ -127,7 +129,7 @@ ApplicationWindow {
square.val = Lib.labyrinth[x][y];
square.updateImage();
Lib.objectArray[idx] = square;
- if (x == (Lib.dimension - 1) && y == (Lib.dimension - 1)){
+ if (x == (Lib.dimension - 1) && y == (Lib.dimension - 1)) {
cheeseSquare = square;
var component1 = Qt.createComponent("Congratulation.qml");
if (component1.status == Component.Ready) {
@@ -140,7 +142,7 @@ ApplicationWindow {
else{
Lib.objectArray[idx].val = Lib.labyrinth[x][y];
Lib.objectArray[idx].updateImage();
- if (x == (Lib.dimension - 1) && y == (Lib.dimension - 1)){
+ if (x == (Lib.dimension - 1) && y == (Lib.dimension - 1)) {
cheeseSquare = Lib.objectArray[idx];
congratulation.visible = false;
}
@@ -152,7 +154,7 @@ ApplicationWindow {
//Lib.printLab(); //this is for debug. Labyrinth will be printed out in the console
//Create the mouse control (only at the first time)
- if (mouseCtrl === null){
+ if (mouseCtrl === null) {
var component = Qt.createComponent("Mouse.qml");
if (component.status == Component.Ready) {
mouseCtrl = component.createObject(parent);
@@ -164,7 +166,6 @@ ApplicationWindow {
//Start the Tilt reader timer
tiltTimer.running = true;
- tiltSensor.calibrate();
}
}
}
@@ -193,7 +194,7 @@ ApplicationWindow {
return;
//check if already solved
- if (Lib.won !== true){
+ if (Lib.won !== true) {
Lib.sec += 0.05;
timePlayingLabel.text = Math.floor(Lib.sec) + " seconds";
@@ -203,60 +204,45 @@ ApplicationWindow {
//! [3]
var xstep = 0;
- if (tiltSensor.yRotation > 0)
- xstep = 1;
- else if (tiltSensor.yRotation < 0)
- xstep = -1;
+ xstep = tiltSensor.yRotation * 0.1 //acceleration
+
var ystep = 0;
- if (tiltSensor.xRotation > 0)
- ystep = 1;
- else if (tiltSensor.xRotation < 0)
- ystep = -1;
+ ystep = tiltSensor.xRotation * 0.1 //acceleration
//! [3]
+//! [4]
+ if (xstep < 1 && xstep > 0)
+ xstep = 0
+ else if (xstep > -1 && xstep < 0)
+ xstep = 0
- if (xstep < 0){
- if (mouseCtrl.x > 0){
- if (Lib.canMove(mouseCtrl.x + xstep, mouseCtrl.y)){
- xval = mouseCtrl.x + xstep;
- }
- }
- }
- else if (xstep > 0){
- if (mouseCtrl.x < (Lib.cellDimension * (Lib.dimension - 1))){
- if (Lib.canMove(mouseCtrl.x + xstep, mouseCtrl.y)){
- xval = mouseCtrl.x + xstep;
- }
- }
- }
- if (ystep < 0){
- if (mouseCtrl.y > 0){
- if (Lib.canMove(mouseCtrl.x, mouseCtrl.y + ystep)){
- yval = mouseCtrl.y + ystep;
- }
- }
- }
- else if (ystep > 0){
- if (mouseCtrl.y < (Lib.cellDimension * (Lib.dimension - 1))){
- if (Lib.canMove(mouseCtrl.x, mouseCtrl.y + ystep)){
- yval = mouseCtrl.y + ystep;
- }
- }
- }
- if (xval >= 0 && yval >= 0)
- mouseCtrl.move(xval, yval);
+ if (ystep < 1 && ystep > 0)
+ ystep = 0;
+ else if (ystep > -1 && ystep < 0)
+ ystep = 0;
- //move the mouse in the allwed position
- else{
- if (xval >= 0){
- mouseCtrl.move(xval, mouseCtrl.y);
- }
- if (yval >= 0){
- mouseCtrl.move(mouseCtrl.x, yval);
- }
- }
- }
- else{
+ if ((xstep < 0 && mouseCtrl.x > 0
+ && Lib.canMove(mouseCtrl.x + xstep,mouseCtrl.y))) {
+ xval = mouseCtrl.x + xstep;
+
+ } else if (xstep > 0 && mouseCtrl.x < (Lib.cellDimension * (Lib.dimension - 1))
+ && Lib.canMove(mouseCtrl.x + xstep,mouseCtrl.y)) {
+ xval = mouseCtrl.x + xstep;
+ } else
+ xval = mouseCtrl.x;
+
+ if (ystep < 0 && mouseCtrl.y > 0
+ && Lib.canMove(mouseCtrl.x, mouseCtrl.y + ystep)) {
+ yval = mouseCtrl.y + ystep;
+ } else if (ystep > 0 && (mouseCtrl.y < (Lib.cellDimension * (Lib.dimension - 1)))
+ && Lib.canMove(mouseCtrl.x, mouseCtrl.y + ystep)) {
+ yval = mouseCtrl.y + ystep;
+ } else
+ yval = mouseCtrl.y
+ mouseCtrl.move(xval, yval);
+//! [4]
+
+ } else {
//game won, stop the tilt meter
mainWnd.cheeseSquare.val = 4;
mainWnd.cheeseSquare.updateImage();
@@ -267,6 +253,7 @@ ApplicationWindow {
}
}
+
//Button to start a new Game
Button{
id: newGameButton
@@ -282,6 +269,18 @@ ApplicationWindow {
startTimer.start();
}
}
+ Button{
+ id: calibrateButton
+ anchors.left: gameRect.left
+ anchors.top: newGameButton.bottom
+ anchors.topMargin: 5
+ height: 30
+ width: 100
+ text: "calibrate"
+ onClicked: {
+ tiltSensor.calibrate();
+ }
+ }
//Label to print out the game time
Text{
diff --git a/examples/QtSensors/QtSensors_maze/content/mouse_left.gif b/examples/QtSensors/QtSensors_maze/content/mouse_left.gif
deleted file mode 100644
index 265d2ba..0000000
--- a/examples/QtSensors/QtSensors_maze/content/mouse_left.gif
+++ /dev/null
Binary files differ
diff --git a/examples/QtSensors/QtSensors_maze/content/mouse_leftdown.gif b/examples/QtSensors/QtSensors_maze/content/mouse_leftdown.gif
deleted file mode 100644
index bdcd708..0000000
--- a/examples/QtSensors/QtSensors_maze/content/mouse_leftdown.gif
+++ /dev/null
Binary files differ
diff --git a/examples/QtSensors/QtSensors_maze/content/mouse_leftup.gif b/examples/QtSensors/QtSensors_maze/content/mouse_leftup.gif
deleted file mode 100644
index a8b1e8f..0000000
--- a/examples/QtSensors/QtSensors_maze/content/mouse_leftup.gif
+++ /dev/null
Binary files differ
diff --git a/examples/QtSensors/QtSensors_maze/content/mouse_right.gif b/examples/QtSensors/QtSensors_maze/content/mouse_right.gif
deleted file mode 100644
index a95d02c..0000000
--- a/examples/QtSensors/QtSensors_maze/content/mouse_right.gif
+++ /dev/null
Binary files differ
diff --git a/examples/QtSensors/QtSensors_maze/content/mouse_rightdown.gif b/examples/QtSensors/QtSensors_maze/content/mouse_rightdown.gif
deleted file mode 100644
index 00bc3e1..0000000
--- a/examples/QtSensors/QtSensors_maze/content/mouse_rightdown.gif
+++ /dev/null
Binary files differ
diff --git a/examples/QtSensors/QtSensors_maze/content/mouse_rightup.gif b/examples/QtSensors/QtSensors_maze/content/mouse_rightup.gif
deleted file mode 100644
index e0153eb..0000000
--- a/examples/QtSensors/QtSensors_maze/content/mouse_rightup.gif
+++ /dev/null
Binary files differ
diff --git a/examples/QtSensors/QtSensors_maze/content/mouse_up.gif b/examples/QtSensors/QtSensors_maze/content/mouse_up.gif
deleted file mode 100644
index 19220eb..0000000
--- a/examples/QtSensors/QtSensors_maze/content/mouse_up.gif
+++ /dev/null
Binary files differ
diff --git a/examples/QtSensors/QtSensors_maze/lib.js b/examples/QtSensors/QtSensors_maze/lib.js
index fccce18..3bae6a8 100644
--- a/examples/QtSensors/QtSensors_maze/lib.js
+++ b/examples/QtSensors/QtSensors_maze/lib.js
@@ -228,19 +228,19 @@ function canMove(x, y)
var dy = ycenter - (idy * cellDimension + ( cellDimension / 2 ));
if (dx > 0){
- if (labyrinth[idx + 1][idy] == 1)
+ if (labyrinth[idx][idy] == 1)
return false;
}
if (dx < 0){
- if (labyrinth[idx - 1][idy] == 1)
+ if (labyrinth[idx][idy] == 1)
return false;
}
if (dy > 0){
- if (labyrinth[idx][idy + 1] == 1)
+ if (labyrinth[idx][idy] == 1)
return false;
}
if (dy < 0){
- if (labyrinth[idx][idy - 1] == 1)
+ if (labyrinth[idx][idy] == 1)
return false;
}
//check if won