org.symbian.tools.wrttools.previewer/preview/script/emulator.js
changeset 403 ba20dd5983c7
parent 402 f943a50b6689
child 407 c7c8bde493b1
--- a/org.symbian.tools.wrttools.previewer/preview/script/emulator.js	Tue Jun 29 13:44:16 2010 -0700
+++ b/org.symbian.tools.wrttools.previewer/preview/script/emulator.js	Tue Jun 29 17:21:51 2010 -0700
@@ -12,6 +12,15 @@
  * Description:
  * 
  */
+var ORIENTATIONS = {
+	DisplayRightUp : "DisplayRightUp",
+	DisplayLeftUp : "DisplayLeftUp",
+	DisplayUp : "DisplayUp",
+	DisplayDown : "DisplayDown",
+	DisplayUpwards : "DisplayUpwards",
+	DisplayDownwards : "DisplayDownwards"
+};
+
 function Emulator() {
 	this.child = false;
 	this.accelerationCallback = false;
@@ -19,7 +28,8 @@
 	this.loaded = false;
 	this.FORCE = 62;
 	this.state = new EmulatorState(0, this.FORCE, 0, true);
-	this.orientation = "DisplayUp";
+	this.orientation = ORIENTATIONS.DisplayUp;
+	this.modeForced = false;
 
 	this.plist = {
 		DisplayName : '',
@@ -34,7 +44,8 @@
 
 Emulator.prototype.setAccelerationCallback = function(acceleration) {
 	this.accelerationCallback = acceleration;
-	this.accelerationCallback(this.state.XAxis, this.state.YAxis, this.state.ZAxis, this.orientation);
+	this.accelerationCallback(this.state.XAxis, this.state.YAxis,
+			this.state.ZAxis, this.orientation);
 };
 
 function orientationFromAcceleration(x, y, z) {
@@ -42,44 +53,53 @@
 
 	var xangle = Math.asin(x) * 180 / Math.PI;
 	if (xangle > 55) {
-		orientation = "DisplayRightUp";
+		orientation = ORIENTATIONS.DisplayRightUp;
 	} else if (xangle < -55) {
-		orientation = "DisplayLeftUp";
+		orientation = ORIENTATIONS.DisplayLeftUp;
 	}
 
 	var yangle = Math.asin(y) * 180 / Math.PI;
 	if (yangle > 55) {
-		orientation = "DisplayUp";
+		orientation = ORIENTATIONS.DisplayUp;
 	} else if (yangle < -55) {
-		orientation = "DisplayDown";
+		orientation = ORIENTATIONS.DisplayDown;
 	}
 
 	var zangle = Math.asin(z) * 180 / Math.PI;
 	if (zangle > 75) {
-		orientation = "DisplayUpwards";
+		orientation = ORIENTATIONS.DisplayUpwards;
 	} else if (zangle < -75) {
-		orientation = "DisplayDownwards";
+		orientation = ORIENTATIONS.DisplayDownwards;
 	}
 
 	return orientation;
 }
 
-
 Emulator.prototype.accelerationChanged = function(x, y, z) {
 	this.state.XAxis = x * this.FORCE;
 	this.state.YAxis = y * this.FORCE;
 	this.state.ZAxis = z * this.FORCE;
-	
+
 	var orientation = orientationFromAcceleration(x, y, z);
-	
+
 	if (orientation != this.orientation) {
 		this.orientation = orientation;
-		NOKIA.layout.render();
+		NOKIA.helper.setPreference("__SYM_DEVICE_ORIENTATION", orientation);
+		NOKIA.emulator.render();
+	}
+
+	if (this.accelerationCallback) {
+		this.accelerationCallback(this.state.XAxis, this.state.YAxis,
+				this.state.ZAxis, this.orientation);
 	}
-	
-	if (this.accelerationCallback) {
-		this.accelerationCallback(this.state.XAxis, this.state.YAxis, this.state.ZAxis, this.orientation);
-	}
+};
+
+Emulator.prototype.setAccelerationValues = function(x, y, z) {
+	window.setTimeout(function() {
+		NOKIA.emulator.accelerationChanged(x, y, z);
+		NOKIA.rotationSupport.setAngles(180 - x * 90, y * 90 + 180,
+				180 + z * 90);
+	}, 5);
 };
 
 Emulator.prototype.load = function() {
@@ -91,26 +111,42 @@
 	var device = NOKIA.helper.getPreference('__SYM_NOKIA_EMULATOR_DEVICE');
 	NOKIA.currentDevice = device || NOKIA.currentDevice;
 
+	var orientation = NOKIA.helper.getPreference("__SYM_DEVICE_ORIENTATION");
+	if (orientation != null) {
+		this.orientation = orientation;
+		switch (orientation) {
+		case ORIENTATIONS.DisplayUp:
+			this.setAccelerationValues(0, 0, 0);
+			break;
+		case ORIENTATIONS.DisplayDown:
+			this.setAccelerationValues(2, 0, 0);
+			break;
+		case ORIENTATIONS.DisplayRightUp:
+			this.setAccelerationValues(1, 0, 0);
+			break;
+		case ORIENTATIONS.DisplayLeftUp:
+			this.setAccelerationValues(-1, 0, 0);
+			break;
+		case ORIENTATIONS.DisplayUpwards:
+			this.setAccelerationValues(0, 0, 1);
+			break;
+		case ORIENTATIONS.DisplayDownwards:
+			this.setAccelerationValues(0, 0, -1);
+			break;
+		}
+	}
+
 	// load the saved device mode
 	var mode = NOKIA.helper.getPreference('__SYM_NOKIA_EMULATOR_DEVICE_MODE');
 	if (mode != null)
 		NOKIA.mode = mode;
 
-	var orientation = Number(NOKIA.helper
-			.getPreference('__SYM_NOKIA_EMULATOR_ORIENTATION'));
-	if (typeof orientation == "number" && orientation >= -90
-			&& orientation <= 180)
-		NOKIA.orientation = orientation;
-
 	// SAVE the device DATA
 	NOKIA.helper.setPreference('__SYM_NOKIA_EMULATOR_DEVICE',
 			NOKIA.currentDevice);
 	NOKIA.helper.setPreference('__SYM_NOKIA_EMULATOR_DEVICE_MODE', NOKIA.mode);
-	NOKIA.helper.setPreference('__SYM_NOKIA_EMULATOR_ORIENTATION',
-			NOKIA.orientation);
 
 	this.loaded = true;
-
 };
 
 Emulator.prototype.render = function() {
@@ -124,20 +160,42 @@
 		}
 	}
 
+	var deg = 0;
+	switch (NOKIA.emulator.orientation) {
+	case ORIENTATIONS.DisplayUp:
+		deg = 0;
+		break;
+	case ORIENTATIONS.DisplayDown:
+		deg = 180;
+		break;
+	case ORIENTATIONS.DisplayRightUp:
+		deg = -90;
+		break;
+	case ORIENTATIONS.DisplayLeftUp:
+		deg = 90;
+		break;
+	default:
+		if (NOKIA.mode == 'portrait') {
+			deg = 0;
+		} else {
+			deg = -90;
+		}
+		break;
+	}
+	if (this.orientationSupports() && !this.modeForced) {
+		if (deg == 0) {
+			NOKIA.mode = 'portrait';
+		} else if (deg == -90) {
+			NOKIA.mode = 'landscape';
+		}
+	}
+
 	if (!NOKIA.emulator.orientationSupports())
 		NOKIA.mode = NOKIA.deviceList[NOKIA.currentDevice]['default'];
 
-	if (typeof NOKIA.deviceList == 'undefined'
-			|| typeof NOKIA.deviceList[NOKIA.currentDevice] == 'undefined'
-			|| typeof NOKIA.deviceList[NOKIA.currentDevice][NOKIA.mode] == 'undefined') {
-		alert('Deive resolution: ' + NOKIA.currentDevice + ' or the mode: '
-				+ NOKIA.mode + ' not found');
-		return false;
-	}
+	this.setStyle();
 
-	this.setStyle();
-	var or = (NOKIA.mode == 'portrait') ? NOKIA.orientation
-			: (NOKIA.orientation + 90);
+	var or = (NOKIA.mode == 'portrait') ? deg : (deg + 90);
 	var val = "rotate(" + or + "deg)";
 	$("#DeviceDisplayLayout").css("-moz-transform", val);
 	$("#DeviceDisplayLayout").css("-webkit-transform", val);
@@ -152,18 +210,6 @@
 	NOKIA.emulator.render();
 };
 
-Emulator.prototype.toggle = function(o) {
-	NOKIA.orientation = o;
-	NOKIA.helper.setPreference('__SYM_NOKIA_EMULATOR_ORIENTATION',
-			NOKIA.orientation);
-	NOKIA.emulator.child.device.implementation.setOrientation(o, 0);
-	if (NOKIA.emulator.orientationSupports() && (o == 0 || o == -90)) {
-		NOKIA.emulator.setMode(o == 0 ? 'portrait' : 'landscape');
-	} else {
-		NOKIA.emulator.render();
-	}
-};
-
 Emulator.prototype.orientationSupports = function() {
 	return NOKIA.deviceList[NOKIA.currentDevice]['orientation'];
 };