plugins/org.symbian.tools.tmw.previewer/preview/script/accelerometer.js
author Eugene Ostroukhov <eugeneo@symbian.org>
Thu, 02 Sep 2010 16:31:16 -0700
changeset 485 df4f55e8569e
parent 470 d4809db37847
permissions -rw-r--r--
Fixed copyright headers and removed unneeded WRT mentions from TMW code
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
485
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 470
diff changeset
     1
/*******************************************************************************
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 470
diff changeset
     2
 * Copyright (c) 2010 Symbian Foundation and/or its subsidiary(-ies).
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 470
diff changeset
     3
 * All rights reserved.
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 470
diff changeset
     4
 * This component and the accompanying materials are made available
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 470
diff changeset
     5
 * under the terms of the License "Eclipse Public License v1.0"
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 470
diff changeset
     6
 * which accompanies this distribution, and is available
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 470
diff changeset
     7
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 470
diff changeset
     8
 *
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 470
diff changeset
     9
 * Initial Contributors:
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 470
diff changeset
    10
 * Symbian Foundation - initial contribution.
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 470
diff changeset
    11
 * Contributors:
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 470
diff changeset
    12
 * Description:
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 470
diff changeset
    13
 * Overview:
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 470
diff changeset
    14
 * Details:
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 470
diff changeset
    15
 * Platforms/Drives/Compatibility:
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 470
diff changeset
    16
 * Assumptions/Requirement/Pre-requisites:
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 470
diff changeset
    17
 * Failures and causes:
df4f55e8569e Fixed copyright headers and removed unneeded WRT mentions from TMW code
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 470
diff changeset
    18
 *******************************************************************************/
402
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    19
function RotationSupport(accelerationCallback) {
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    20
	this.controls = new RotationControls(accelerationCallback);
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    21
}
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    22
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    23
function RotationControls(accelCallback) {
407
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    24
	this.angleX = 0;
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    25
	this.angleY = 0;
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    26
	this.angleZ = 0;
403
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
    27
	var control = this;
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
    28
	this.accelerationCallback = accelCallback;
402
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
    29
407
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    30
	setupListeners("xaxis", "xleft", "xright", updateAngleX, this.angleX);
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    31
	setupListeners("yaxis", "yleft", "yright", updateAngleY, this.angleY);
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    32
	setupListeners("zaxis", "zleft", "zright", updateAngleZ, this.angleZ);
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    33
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    34
	window.setTimeout(function() {
412
72448215f517 Bug 2785 - Orientation and Resolution Controls Placements
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 410
diff changeset
    35
		control.paint(true);
407
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    36
	}, 50);
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    37
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    38
	function setupListeners(inputId, leftArrow, rightArrow, fn, initial) {
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    39
		var input = $("#" + inputId);
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    40
		input.numeric();
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    41
		input.val(initial);
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    42
		input.change(function() {
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    43
			adjust(input, fn, 0);
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    44
		});
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    45
		handleButton(leftArrow, input, fn, -1);
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    46
		handleButton(rightArrow, input, fn, 1);
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    47
	}
403
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
    48
407
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    49
	function handleButton(buttonId, input, fn, increment) {
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    50
		var id = false;
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    51
		var timeout = false;
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    52
		var stop = function() {
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    53
			if (timeout) {
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    54
				window.clearTimeout(timeout);
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    55
				timeout = false;
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    56
				adjust(input, fn, increment);
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    57
			}
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    58
			if (id) {
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    59
				window.clearInterval(id);
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    60
				id = false;
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    61
			}
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    62
		};
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    63
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    64
		$("#" + buttonId).mousedown(function() {
413
817cf8083ab8 Bug 3136 - Preview - infinity loop on accelerometer
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 412
diff changeset
    65
			if (!timeout && !id) {
817cf8083ab8 Bug 3136 - Preview - infinity loop on accelerometer
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 412
diff changeset
    66
				timeout = window.setTimeout(function() {
817cf8083ab8 Bug 3136 - Preview - infinity loop on accelerometer
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 412
diff changeset
    67
					id = window.setInterval(function() {
817cf8083ab8 Bug 3136 - Preview - infinity loop on accelerometer
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 412
diff changeset
    68
						timeout = false;
817cf8083ab8 Bug 3136 - Preview - infinity loop on accelerometer
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 412
diff changeset
    69
						adjust(input, fn, increment);
817cf8083ab8 Bug 3136 - Preview - infinity loop on accelerometer
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 412
diff changeset
    70
					}, 10);
817cf8083ab8 Bug 3136 - Preview - infinity loop on accelerometer
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 412
diff changeset
    71
				}, 250);
817cf8083ab8 Bug 3136 - Preview - infinity loop on accelerometer
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 412
diff changeset
    72
			}
817cf8083ab8 Bug 3136 - Preview - infinity loop on accelerometer
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 412
diff changeset
    73
		}).mouseup(stop).focusout(stop).blur(stop).mouseleave(stop).dblclick(
817cf8083ab8 Bug 3136 - Preview - infinity loop on accelerometer
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 412
diff changeset
    74
				stop);
407
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    75
	}
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    76
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    77
	function adjust(input, callback, increment) {
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    78
		var n = parseInt(input.val());
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    79
		if (isNaN(n)) {
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    80
			n = 0;
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    81
		}
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    82
		var val = fix(n + increment);
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    83
		input.val(val);
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    84
		callback(val);
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    85
	}
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    86
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    87
	function fix(n) {
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    88
		while (n < -180) {
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    89
			n = n + 360;
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    90
		}
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    91
		while (n >= 180) {
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    92
			n = n - 360;
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    93
		}
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    94
		return n;
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    95
	}
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    96
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    97
	function updateAngleX(angle) {
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
    98
		control.angleX = angle;
403
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
    99
		control.paint();
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   100
	}
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   101
407
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
   102
	function updateAngleY(angle) {
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
   103
		control.angleY = angle;
403
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   104
		control.paint();
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   105
	}
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   106
407
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
   107
	function updateAngleZ(angle) {
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
   108
		control.angleZ = angle;
403
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   109
		control.paint();
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   110
	}
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   111
}
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   112
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   113
RotationControls.prototype.paint = function(ignoreListeners) {
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   114
	var width = 50, height = 100, depth = 10;
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   115
	var margin = 5, bottomMargin = 15;
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   116
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   117
	var canvas = document.getElementById("phoneposition");
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   118
	var ctx = canvas.getContext("2d");
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   119
	ctx.clearRect(0, 0, canvas.width, canvas.height);
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   120
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   121
	var r = 62;
402
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   122
412
72448215f517 Bug 2785 - Orientation and Resolution Controls Placements
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 410
diff changeset
   123
	var xy = (180 - this.angleX) * Math.PI / 180;
403
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   124
	var yz = (this.angleY - 180) * Math.PI / 180;
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   125
	var xz = (180 - this.angleZ) * Math.PI / 180 + Math.PI / 2;
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   126
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   127
	var back = translateBack(xy, xz, yz);
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   128
	if ((back[0].z + back[2].z) / 2 < 0) {
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   129
		paint(canvas, ctx, back);
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   130
	} else {
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   131
		paint(canvas, ctx, translateFace(xy, xz, yz));
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   132
		paintScreen(canvas, ctx, translateScreen(xy, xz, yz));
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   133
	}
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   134
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   135
	var dz = 0;
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   136
	if (back[0].z > back[3].z) {
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   137
		var bottom = translateBottom(xy, xz, yz);
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   138
		paint(canvas, ctx, bottom);
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   139
		dz = bottom[1].y - bottom[0].y;
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   140
	} else if (back[0].z != back[3].z) {
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   141
		var top = translateTop(xy, xz, yz);
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   142
		paint(canvas, ctx, top);
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   143
		dz = top[1].y - top[0].y;
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   144
	}
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   145
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   146
	if (back[1].z > back[0].z) {
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   147
		paint(canvas, ctx, translateLeft(xy, xz, yz));
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   148
	} else if (back[1].z != back[0].z) {
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   149
		paint(canvas, ctx, translateRight(xy, xz, yz));
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   150
	}
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   151
	if (!ignoreListeners) {
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   152
		var accelX = (back[1].y - back[0].y) / width;
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   153
		var accelY = (back[0].y - back[3].y) / height;
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   154
		var accelZ = dz / depth;
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   155
		notifyAcceleration(accelX, accelY, accelZ);
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   156
	}
402
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   157
	function translateFace(xy, xz, yz) {
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   158
		var px = width / 2;
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   159
		var py = height / 2;
403
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   160
		var pz = depth / 2;
402
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   161
		var points = [ {
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   162
			x : px,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   163
			y : py,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   164
			z : pz
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   165
		}, {
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   166
			x : px,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   167
			y : -py,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   168
			z : pz
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   169
		}, {
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   170
			x : -px,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   171
			y : -py,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   172
			z : pz
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   173
		}, {
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   174
			x : -px,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   175
			y : py,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   176
			z : pz
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   177
		} ];
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   178
		return rotate(points, xy, xz, yz);
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   179
	}
403
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   180
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   181
	function translateScreen(xy, xz, yz) {
402
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   182
		var px = width / 2;
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   183
		var py = height / 2;
403
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   184
		var pz = depth / 2;
402
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   185
		var points = [ {
403
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   186
			x : px - margin,
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   187
			y : py - bottomMargin,
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   188
			z : pz
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   189
		}, {
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   190
			x : px - margin,
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   191
			y : -py + bottomMargin,
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   192
			z : pz
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   193
		}, {
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   194
			x : -px + margin,
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   195
			y : -py + margin,
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   196
			z : pz
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   197
		}, {
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   198
			x : -px + margin,
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   199
			y : py - margin,
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   200
			z : pz
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   201
		} ];
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   202
		return rotate(points, xy, xz, yz);
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   203
	}
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   204
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   205
	function translateBack(xy, xz, yz) {
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   206
		var px = width / 2;
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   207
		var py = height / 2;
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   208
		var pz = -depth / 2;
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   209
		var points = [ {
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   210
			x : px,
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   211
			y : py,
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   212
			z : pz
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   213
		}, {
402
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   214
			x : px,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   215
			y : -py,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   216
			z : pz
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   217
		}, {
403
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   218
			x : -px,
402
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   219
			y : -py,
403
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   220
			z : pz
402
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   221
		}, {
403
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   222
			x : -px,
402
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   223
			y : py,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   224
			z : pz
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   225
		} ];
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   226
		return rotate(points, xy, xz, yz);
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   227
	}
403
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   228
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   229
	function translateTop(xy, xz, yz) {
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   230
		var px = width / 2;
402
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   231
		var py = height / 2;
403
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   232
		var pz = depth / 2;
402
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   233
		var points = [ {
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   234
			x : px,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   235
			y : -py,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   236
			z : pz
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   237
		}, {
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   238
			x : px,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   239
			y : -py,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   240
			z : -pz
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   241
		}, {
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   242
			x : px,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   243
			y : py,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   244
			z : -pz
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   245
		}, {
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   246
			x : px,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   247
			y : py,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   248
			z : pz
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   249
		} ];
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   250
		return rotate(points, xy, xz, yz);
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   251
	}
403
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   252
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   253
	function translateBottom(xy, xz, yz) {
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   254
		var px = -width / 2;
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   255
		var py = height / 2;
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   256
		var pz = depth / 2;
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   257
		var points = [ {
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   258
			x : px,
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   259
			y : -py,
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   260
			z : pz
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   261
		}, {
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   262
			x : px,
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   263
			y : -py,
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   264
			z : -pz
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   265
		}, {
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   266
			x : px,
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   267
			y : py,
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   268
			z : -pz
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   269
		}, {
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   270
			x : px,
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   271
			y : py,
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   272
			z : pz
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   273
		} ];
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   274
		return rotate(points, xy, xz, yz);
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   275
	}
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   276
402
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   277
	function translateLeft(xy, xz, yz) {
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   278
		var px = width / 2;
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   279
		var py = height / 2;
403
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   280
		var pz = depth / 2;
402
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   281
		var points = [ {
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   282
			x : px,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   283
			y : py,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   284
			z : pz
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   285
		}, {
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   286
			x : px,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   287
			y : py,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   288
			z : -pz
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   289
		}, {
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   290
			x : -px,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   291
			y : py,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   292
			z : -pz
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   293
		}, {
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   294
			x : -px,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   295
			y : py,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   296
			z : pz
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   297
		} ];
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   298
		return rotate(points, xy, xz, yz);
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   299
	}
403
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   300
402
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   301
	function translateRight(xy, xz, yz) {
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   302
		var px = width / 2;
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   303
		var py = -height / 2;
403
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   304
		var pz = depth / 2;
402
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   305
		var points = [ {
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   306
			x : px,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   307
			y : py,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   308
			z : pz
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   309
		}, {
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   310
			x : px,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   311
			y : py,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   312
			z : -pz
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   313
		}, {
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   314
			x : -px,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   315
			y : py,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   316
			z : -pz
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   317
		}, {
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   318
			x : -px,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   319
			y : py,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   320
			z : pz
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   321
		} ];
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   322
		return rotate(points, xy, xz, yz);
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   323
	}
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   324
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   325
	function rotate(points, xy, xz, yz) {
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   326
		var res = new Array();
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   327
		for ( var p in points) {
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   328
			var px = points[p].x;
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   329
			var py = points[p].y;
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   330
			var pz = points[p].z;
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   331
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   332
			var rx = Math.sqrt(px * px + py * py);
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   333
			var angXY = Math.atan(px / py) + (px < 0 ? Math.PI : 0);
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   334
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   335
			px = rx * Math.sin(angXY + xy);
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   336
			py = rx * Math.cos(angXY + xy);
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   337
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   338
			var rz = Math.sqrt(px * px + pz * pz);
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   339
			var angXZ = (px == 0 ? 0 : Math.atan(pz / px))
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   340
					+ (px < 0 ? Math.PI : 0);
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   341
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   342
			px = rz * Math.sin(angXZ + xz);
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   343
			pz = rz * Math.cos(angXZ + xz);
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   344
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   345
			var ry = Math.sqrt(py * py + pz * pz);
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   346
			var angYZ = (pz == 0 ? 0 : Math.atan(py / pz))
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   347
					+ (pz < 0 ? Math.PI : 0);
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   348
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   349
			py = ry * Math.sin(angYZ + yz);
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   350
			pz = ry * Math.cos(angYZ + yz);
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   351
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   352
			res.push( {
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   353
				x : px,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   354
				y : py,
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   355
				z : pz
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   356
			});
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   357
		}
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   358
		return res;
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   359
	}
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   360
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   361
	function paint(canvas, ctx, points) {
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   362
		var xcoord = canvas.width / 2;
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   363
		var ycoord = canvas.height / 2;
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   364
416
461da1f79f43 Bug 3137 - If console is blank, provide usage instructions
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 413
diff changeset
   365
		ctx.fillStyle = "#FAC82F";
402
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   366
		ctx.strokeStyle = "black";
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   367
		ctx.beginPath();
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   368
		ctx.moveTo(xcoord + points[3].x, ycoord + points[3].y);
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   369
		for (point in points) {
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   370
			ctx.lineTo(xcoord + points[point].x, ycoord + points[point].y);
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   371
		}
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   372
		ctx.fill();
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   373
		ctx.beginPath();
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   374
		ctx.moveTo(xcoord + points[3].x, ycoord + points[3].y);
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   375
		for (point in points) {
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   376
			ctx.lineTo(xcoord + points[point].x, ycoord + points[point].y);
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   377
		}
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   378
		ctx.stroke();
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   379
	}
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   380
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   381
	function paintScreen(canvas, ctx, screen) {
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   382
		var xcoord = canvas.width / 2;
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   383
		var ycoord = canvas.height / 2;
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   384
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   385
		ctx.fillStyle = "grey";
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   386
		ctx.beginPath();
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   387
		ctx.moveTo(xcoord + screen[3].x, ycoord + screen[3].y);
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   388
		for (point in screen) {
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   389
			ctx.lineTo(xcoord + screen[point].x, ycoord + screen[point].y);
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   390
		}
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   391
		ctx.fill();
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   392
	}
403
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   393
402
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   394
	function notifyAcceleration(x, y, z) {
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   395
		accelerationCallback(x, y, z);
f943a50b6689 Acceleration and orientation - initial cut
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff changeset
   396
	}
403
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   397
};
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   398
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   399
RotationSupport.prototype.setAngles = function(x, y, z) {
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   400
	this.controls.angleX = x;
407
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
   401
	this.controls.angleY = z; // It is extremly messy - this UI was developed
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
   402
	// separately from the rest and follows
403
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   403
	this.controls.angleZ = y; // different conventions
407
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
   404
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
   405
	$("#xaxis").val(this.controls.angleX);
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
   406
	$("#yaxis").val(this.controls.angleY);
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
   407
	$("#zaxis").val(this.controls.angleZ);
c7c8bde493b1 Orientation controls were updated
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 403
diff changeset
   408
403
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   409
	this.controls.paint(true);
ba20dd5983c7 Acceleration and orientation sensors support
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 402
diff changeset
   410
};