plugins/org.symbian.tools.tmw.previewer/preview/script/emulator.js
changeset 470 d4809db37847
parent 423 742a5cb59f9a
child 491 27c0e5ff3255
equal deleted inserted replaced
469:4d198a32ac7d 470:d4809db37847
       
     1 /**
       
     2  * Copyright (c) 2009-2010 Symbian Foundation and/or its subsidiary(-ies). All
       
     3  * rights reserved. This component and the accompanying materials are made
       
     4  * available under the terms of the License "Eclipse Public License v1.0" which
       
     5  * accompanies this distribution, and is available at the URL
       
     6  * "http://www.eclipse.org/legal/epl-v10.html".
       
     7  * 
       
     8  * Initial Contributors: Nokia Corporation - initial contribution.
       
     9  * 
       
    10  * Contributors:
       
    11  * 
       
    12  * Description:
       
    13  * 
       
    14  */
       
    15 var ORIENTATIONS = {
       
    16 	DisplayRightUp : "DisplayRightUp",
       
    17 	DisplayLeftUp : "DisplayLeftUp",
       
    18 	DisplayUp : "DisplayUp",
       
    19 	DisplayDown : "DisplayDown",
       
    20 	DisplayUpwards : "DisplayUpwards",
       
    21 	DisplayDownwards : "DisplayDownwards"
       
    22 };
       
    23 
       
    24 var MODES = {
       
    25 	portrait : "portrait",
       
    26 	landscape : "landscape"
       
    27 };
       
    28 
       
    29 function EmulatorState(x, y, z, orientation) {
       
    30 	this.XAxis = x;
       
    31 	this.YAxis = y;
       
    32 	this.ZAxis = z;
       
    33 	this.portrait = orientation;
       
    34 }
       
    35 
       
    36 function Emulator() {
       
    37 	this.child = false;
       
    38 	this.accelerationCallback = false;
       
    39 	this.iconFile = 'preview/images/default-Icon.png';
       
    40 	this.loaded = false;
       
    41 	this.FORCE = 62;
       
    42 	this.state = new EmulatorState(0, this.FORCE, 0, true);
       
    43 	this.orientation = ORIENTATIONS.DisplayUp;
       
    44 	this.modeForced = false;
       
    45 
       
    46 	this.plist = {
       
    47 		DisplayName : '',
       
    48 		Identifier : '',
       
    49 		MainHTML : '',
       
    50 		AllowNetworkAccess : "false",
       
    51 		Version : '',
       
    52 		MiniViewEnabled : "false",
       
    53 		is_browserReady : false
       
    54 	};
       
    55 };
       
    56 
       
    57 Emulator.prototype.setAccelerationCallback = function(acceleration) {
       
    58 	this.accelerationCallback = acceleration;
       
    59 	this.accelerationCallback(this.state.XAxis, this.state.YAxis,
       
    60 			this.state.ZAxis, this.orientation);
       
    61 };
       
    62 
       
    63 Emulator.prototype.orientationFromAcceleration= function(x, y, z) {
       
    64 	var orientation = NOKIA.emulator.orientation;
       
    65 
       
    66 	var xangle = Math.asin(x) * 180 / Math.PI;
       
    67 	if (xangle > 55) {
       
    68 		orientation = ORIENTATIONS.DisplayRightUp;
       
    69 	} else if (xangle < -55) {
       
    70 		orientation = ORIENTATIONS.DisplayLeftUp;
       
    71 	}
       
    72 
       
    73 	var yangle = Math.asin(y) * 180 / Math.PI;
       
    74 	if (yangle > 55) {
       
    75 		orientation = ORIENTATIONS.DisplayUp;
       
    76 	} else if (yangle < -55) {
       
    77 		orientation = ORIENTATIONS.DisplayDown;
       
    78 	}
       
    79 
       
    80 	var zangle = Math.asin(z) * 180 / Math.PI;
       
    81 	if (zangle > 75) {
       
    82 		orientation = ORIENTATIONS.DisplayUpwards;
       
    83 	} else if (zangle < -75) {
       
    84 		orientation = ORIENTATIONS.DisplayDownwards;
       
    85 	}
       
    86 
       
    87 	return orientation;
       
    88 };
       
    89 
       
    90 Emulator.prototype.accelerationChanged = function(x, y, z) {
       
    91 	this.state.XAxis = x * this.FORCE;
       
    92 	this.state.YAxis = y * this.FORCE;
       
    93 	this.state.ZAxis = z * this.FORCE;
       
    94 
       
    95 	var orientation = this.orientationFromAcceleration(x, y, z);
       
    96 
       
    97 	if (orientation != this.orientation) {
       
    98 		this.orientation = orientation;
       
    99 		NOKIA.helper
       
   100 				.setPreference(EmulatorPreferences.ORIENTATION, orientation);
       
   101 		NOKIA.emulator.render();
       
   102 	}
       
   103 
       
   104 	if (this.accelerationCallback) {
       
   105 		this.accelerationCallback(this.state.XAxis, this.state.YAxis,
       
   106 				this.state.ZAxis, this.orientation);
       
   107 	}
       
   108 };
       
   109 
       
   110 Emulator.prototype.setAccelerationValues = function(x, y, z) {
       
   111 	window.setTimeout(function() {
       
   112 		NOKIA.rotationSupport.setAngles(x * 90, y * 90, z * 90);
       
   113 		accelerationCallback(Math.sin(-x * Math.PI / 2), Math.cos(-x * Math.PI / 2), 0);
       
   114 	}, 5);
       
   115 };
       
   116 
       
   117 Emulator.prototype.showOrientationAngles = function(orientation) {
       
   118 	var x = 0;
       
   119 	var y = 0;
       
   120 	var z = 0;
       
   121 	switch (orientation) {
       
   122 	case ORIENTATIONS.DisplayUp:
       
   123 		break;
       
   124 	case ORIENTATIONS.DisplayDown:
       
   125 		x = 2;
       
   126 		break;
       
   127 	case ORIENTATIONS.DisplayRightUp:
       
   128 		x = -1;
       
   129 		break;
       
   130 	case ORIENTATIONS.DisplayLeftUp:
       
   131 		x = 1;
       
   132 		break;
       
   133 	case ORIENTATIONS.DisplayUpwards:
       
   134 		x = NOKIA.mode == MODES.portrait ? 0 : -1;
       
   135 		z = 1;
       
   136 		break;
       
   137 	case ORIENTATIONS.DisplayDownwards:
       
   138 		x = NOKIA.mode == MODES.portrait ? 0 : -1;
       
   139 		z = -1;
       
   140 		break;
       
   141 	}
       
   142 	this.setAccelerationValues(x, y, z);
       
   143 	this.orientation = orientation;
       
   144 	NOKIA.helper.setPreference(EmulatorPreferences.ORIENTATION, orientation);
       
   145 	window.setTimeout(function() {
       
   146 		NOKIA.emulator.render();
       
   147 	}, 50);
       
   148 };
       
   149 
       
   150 Emulator.prototype.load = function() {
       
   151 	if (this.loaded)
       
   152 		return false;
       
   153 	NOKIA.layout._console_minimized = (NOKIA.helper
       
   154 			.getPreference('__SYM_NOKIA_CONSOLE_OPEN') != "true");
       
   155 	// load the saved device Info
       
   156 	var device = NOKIA.helper.getPreference('__SYM_NOKIA_EMULATOR_DEVICE');
       
   157 	NOKIA.currentDevice = device || NOKIA.currentDevice;
       
   158 
       
   159 	// load the saved device mode
       
   160 	var mode = NOKIA.helper.getPreference('__SYM_NOKIA_EMULATOR_DEVICE_MODE');
       
   161 	if (mode != null)
       
   162 		NOKIA.mode = mode;
       
   163 
       
   164 	var orientation = NOKIA.helper
       
   165 			.getPreference(EmulatorPreferences.ORIENTATION);
       
   166 	if (orientation != null) {
       
   167 		this.orientation = orientation;
       
   168 		this.showOrientationAngles(orientation);
       
   169 	}
       
   170 
       
   171 	// SAVE the device DATA
       
   172 	NOKIA.helper.setPreference('__SYM_NOKIA_EMULATOR_DEVICE',
       
   173 			NOKIA.currentDevice);
       
   174 	NOKIA.helper.setPreference('__SYM_NOKIA_EMULATOR_DEVICE_MODE', NOKIA.mode);
       
   175 
       
   176 	this.loaded = true;
       
   177 };
       
   178 
       
   179 Emulator.prototype.render = function() {
       
   180 	this.load();
       
   181 	// Selecting Resoltion
       
   182 	var resOptions = $("#resOptions")[0];
       
   183 	for ( var i = 0; i < NOKIA.resolution.length; i++) {
       
   184 		if (NOKIA.resolution[i] == NOKIA.currentDevice) {
       
   185 			resOptions.options[i].selected = true;
       
   186 			break;
       
   187 		}
       
   188 	}
       
   189 
       
   190 	var deg = 0;
       
   191 	switch (NOKIA.emulator.orientation) {
       
   192 	case ORIENTATIONS.DisplayUp:
       
   193 		deg = 0;
       
   194 		break;
       
   195 	case ORIENTATIONS.DisplayDown:
       
   196 		deg = 180;
       
   197 		break;
       
   198 	case ORIENTATIONS.DisplayRightUp:
       
   199 		deg = -90;
       
   200 		break;
       
   201 	case ORIENTATIONS.DisplayLeftUp:
       
   202 		deg = 90;
       
   203 		break;
       
   204 	default:
       
   205 		if (NOKIA.mode == 'portrait') {
       
   206 			deg = 0;
       
   207 		} else {
       
   208 			deg = -90;
       
   209 		}
       
   210 		break;
       
   211 	}
       
   212 	if (this.orientationSupports() && !this.modeForced) {
       
   213 		if (deg == 0) {
       
   214 			NOKIA.mode = 'portrait';
       
   215 		} else if (deg == -90) {
       
   216 			NOKIA.mode = 'landscape';
       
   217 		}
       
   218 	}
       
   219 	NOKIA.helper.setPreference('__SYM_NOKIA_EMULATOR_DEVICE_MODE', NOKIA.mode);
       
   220 
       
   221 	if (!NOKIA.emulator.orientationSupports())
       
   222 		NOKIA.mode = NOKIA.deviceList[NOKIA.currentDevice]['default'];
       
   223 
       
   224 	this.setStyle();
       
   225 
       
   226 	var or = (NOKIA.mode == 'portrait') ? deg : (deg + 90);
       
   227 	var val = "rotate(" + or + "deg)";
       
   228 	$("#DeviceDisplayLayout").css("-moz-transform", val);
       
   229 	$("#DeviceDisplayLayout").css("-webkit-transform", val);
       
   230 };
       
   231 
       
   232 Emulator.prototype.setMode = function(mode) {
       
   233 	NOKIA.mode = mode;
       
   234 
       
   235 	// SAVE the device DATA
       
   236 	NOKIA.helper.setPreference('__SYM_NOKIA_EMULATOR_DEVICE_MODE', NOKIA.mode);
       
   237 
       
   238 	NOKIA.emulator.render();
       
   239 };
       
   240 
       
   241 Emulator.prototype.orientationSupports = function() {
       
   242 	return NOKIA.deviceList[NOKIA.currentDevice]['orientation'];
       
   243 };
       
   244 
       
   245 Emulator.prototype.setStyle = function() {
       
   246 	if (!NOKIA.helper.checkDependencies()) {
       
   247 		setTimeout(NOKIA.emulator.setStyle, 1000);
       
   248 	}
       
   249 
       
   250 	var deviceProperties = NOKIA.deviceList[NOKIA.currentDevice][NOKIA.mode];
       
   251 	var style = deviceProperties['style'];
       
   252 
       
   253 	// Apply Style and propertis to Device layers
       
   254 	$("#DeviceDisplayLayout").css(style['layout']);
       
   255 	$('#DisplayArea').css(style['display']);
       
   256 
       
   257 	NOKIA.emulator.setWidgetStyle();
       
   258 
       
   259 	$('#SoftKeysArea').css( {
       
   260 		'width' : style['menu']['width'] + 'px',
       
   261 		'height' : style['menu']['height'] + 'px',
       
   262 		'float' : style['menu']['float']
       
   263 	});
       
   264 
       
   265 	$('#SoftKeysArea > ul > li').css('width',
       
   266 			parseInt(style['menu']['width'] / 2) - 10);
       
   267 
       
   268 	NOKIA.emulator.setMenuItemsStyle();
       
   269 
       
   270 	$('#SoftKeys').css(style['softkeys']);
       
   271 	// $('#SoftKeys > a > img').css(style['softkeysImg']);
       
   272 
       
   273 	NOKIA.menu.createSFKArea();
       
   274 
       
   275 	$("#DeviceDisplayLayout").show();
       
   276 	$("#InspectorTab").show();
       
   277 
       
   278 	if (/chrome/.test(navigator.userAgent.toLowerCase())) {
       
   279 		$("#InspectorBtn").show();
       
   280 	}
       
   281 	if (/mac/.test(navigator.userAgent.toLowerCase())) {
       
   282 		$("#MacShortcut").show();
       
   283 	} else {
       
   284 		$("#UniversalShortcut").show();
       
   285 	}
       
   286 },
       
   287 
       
   288 Emulator.prototype.setWidgetStyle = function() {
       
   289 	var style = NOKIA.deviceList[NOKIA.currentDevice][NOKIA.mode]['style'];
       
   290 	var height;
       
   291 	if (NOKIA.menu.softkeys_visibility || NOKIA.menu.is_softkeys_visible)
       
   292 		height = parseInt(style['widget']['height'] - style['menu']['height']);
       
   293 	else
       
   294 		height = style['widget']['height'];
       
   295 
       
   296 	$('#WidgetArea').css( {
       
   297 		'width' : style['widget']['width'] + 'px',
       
   298 		'height' : height + 'px',
       
   299 		'float' : style['widget']['float']
       
   300 	});
       
   301 };
       
   302 
       
   303 Emulator.prototype.setMenuItemsStyle = function() {
       
   304 	var style = NOKIA.deviceList[NOKIA.currentDevice][NOKIA.mode]['style'];
       
   305 	var count = 1;
       
   306 	try {
       
   307 		if (NOKIA.emulator.child.menu) {
       
   308 			count = parseInt(NOKIA.helper
       
   309 					.getElementsLengthInObject(NOKIA.emulator.child.menu.items)) + 1;
       
   310 		}
       
   311 	} catch (e) {
       
   312 		count = 1;
       
   313 	}
       
   314 	var height = parseInt(count * style['menu']['optionKeysheight']) + 10;
       
   315 	var top = parseInt(style['widget']['height'] - height);
       
   316 
       
   317 	$('#MenuItemsArea').css(
       
   318 			{
       
   319 				'width' : style['widget']['width'] + 'px',
       
   320 				'height' : height + 'px',
       
   321 				'marginTop' : '-2px',
       
   322 				'top' : (style['widget']['height'] - height
       
   323 						- style['menu']['height'] + 2)
       
   324 						+ 'px',
       
   325 				'position' : 'relative'
       
   326 			});
       
   327 };
       
   328 
       
   329 Emulator.prototype.turn = function(direction) {
       
   330 	var newOrientation = ORIENTATIONS.DisplayUp;
       
   331 	if (this.orientation == ORIENTATIONS.DisplayUpwards || this.orientation == ORIENTATIONS.DisplayDownwards) {
       
   332 		if (NOKIA.mode == MODES.portrait) {
       
   333 			this.orientation = ORIENTATIONS.DisplayUp;
       
   334 		} else {
       
   335 			this.orientation = ORIENTATIONS.DisplayRightUp;
       
   336 		}
       
   337 	}
       
   338 	if (direction > 0) { // Clockwise
       
   339 		switch (this.orientation) {
       
   340 		case ORIENTATIONS.DisplayUp:
       
   341 			newOrientation = ORIENTATIONS.DisplayLeftUp;
       
   342 			break;
       
   343 		case ORIENTATIONS.DisplayLeftUp:
       
   344 			newOrientation = ORIENTATIONS.DisplayDown;
       
   345 			break;
       
   346 		case ORIENTATIONS.DisplayDown:
       
   347 			newOrientation = ORIENTATIONS.DisplayRightUp;
       
   348 			break;
       
   349 		case ORIENTATIONS.DisplayRightUp:
       
   350 			newOrientation = ORIENTATIONS.DisplayUp;
       
   351 			break;
       
   352 		}
       
   353 	} else {
       
   354 		switch (this.orientation) {
       
   355 		case ORIENTATIONS.DisplayUp:
       
   356 			newOrientation = ORIENTATIONS.DisplayRightUp;
       
   357 			break;
       
   358 		case ORIENTATIONS.DisplayLeftUp:
       
   359 			newOrientation = ORIENTATIONS.DisplayUp;
       
   360 			break;
       
   361 		case ORIENTATIONS.DisplayDown:
       
   362 			newOrientation = ORIENTATIONS.DisplayLeftUp;
       
   363 			break;
       
   364 		case ORIENTATIONS.DisplayRightUp:
       
   365 			newOrientation = ORIENTATIONS.DisplayDown;
       
   366 			break;
       
   367 		}
       
   368 	}
       
   369 	this.orientation = newOrientation;
       
   370 	this.showOrientationAngles(newOrientation);
       
   371 	this.render();
       
   372 };
       
   373 
       
   374 function accelerationCallback(x, y, z) {
       
   375 	NOKIA.emulator.accelerationChanged(x, y, z);
       
   376 }