Tizen 2.1 base
[platform/upstream/libbullet.git] / Demos / NativeClient / bin_html / tumbler.js
1 // Copyright (c) 2011 The Native Client Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 /**
6  * @fileoverview  The tumbler Application object.  This object instantiates a
7  * Trackball object and connects it to the element named |tumbler_content|.
8  * It also conditionally embeds a debuggable module or a release module into
9  * the |tumbler_content| element.
10  */
11
12 // Requires tumbler
13 // Requires tumbler.Dragger
14 // Requires tumbler.Trackball
15
16 /**
17  * Constructor for the Application class.  Use the run() method to populate
18  * the object with controllers and wire up the events.
19  * @constructor
20  */
21 tumbler.Application = function() {
22   /**
23    * The native module for the application.  This refers to the module loaded
24    * via the <embed> tag.
25    * @type {Element}
26    * @private
27    */
28   this.module_ = null;
29
30   /**
31    * The trackball object.
32    * @type {tumbler.Trackball}
33    * @private
34    */
35   this.trackball_ = null;
36
37   /**
38    * The mouse-drag event object.
39    * @type {tumbler.Dragger}
40    * @private
41    */
42   this.dragger_ = null;
43
44   /**
45    * The function objects that get attached as event handlers.  These are
46    * cached so that they can be removed when they are no longer needed.
47    * @type {function}
48    * @private
49    */
50   this.boundModuleDidLoad_ = null;
51 }
52
53 /**
54  * The ids used for elements in the DOM.  The Tumlber Application expects these
55  * elements to exist.
56  * @enum {string}
57  * @private
58  */
59 tumbler.Application.DomIds_ = {
60   MODULE: 'tumbler',  // The <embed> element representing the NaCl module
61   VIEW: 'tumbler_view'  // The <div> containing the NaCl element.
62 }
63
64 /**
65  * Called by the module loading function once the module has been loaded.
66  * @param {?Element} nativeModule The instance of the native module.
67  */
68 tumbler.Application.prototype.moduleDidLoad = function() {
69   this.module_ = document.getElementById(tumbler.Application.DomIds_.MODULE);
70   // Unbind the load function.
71   this.boundModuleDidLoad_ = null;
72
73   /**
74    * Set the camera orientation property on the NaCl module.
75    * @param {Array.<number>} orientation A 4-element array representing the
76    *     camera orientation as a quaternion.
77    */
78   this.module_.setCameraOrientation = function(orientation) {
79       var methodString = 'setCameraOrientation ' +
80                          'orientation:' +
81                          JSON.stringify(orientation);
82       this.postMessage(methodString);
83   }
84
85   this.trackball_ = new tumbler.Trackball();
86   this.dragger_ = new tumbler.Dragger(this.module_);
87   this.dragger_.addDragListener(this.trackball_);
88 }
89
90 /**
91  * Asserts that cond is true; issues an alert and throws an Error otherwise.
92  * @param {bool} cond The condition.
93  * @param {String} message The error message issued if cond is false.
94  */
95 tumbler.Application.prototype.assert = function(cond, message) {
96   if (!cond) {
97     message = "Assertion failed: " + message;
98     alert(message);
99     throw new Error(message);
100   }
101 }
102
103 /**
104  * The run() method starts and 'runs' the application.  The trackball object
105  * is allocated and all the events get wired up.
106  * @param {?String} opt_contentDivName The id of a DOM element in which to
107  *     embed the Native Client module.  If unspecified, defaults to
108  *     VIEW.  The DOM element must exist.
109  */
110 tumbler.Application.prototype.run = function(opt_contentDivName) {
111   contentDivName = opt_contentDivName || tumbler.Application.DomIds_.VIEW;
112   var contentDiv = document.getElementById(contentDivName);
113   this.assert(contentDiv, "Missing DOM element '" + contentDivName + "'");
114
115   // Note that the <EMBED> element is wrapped inside a <DIV>, which has a 'load'
116   // event listener attached.  This method is used instead of attaching the
117   // 'load' event listener directly to the <EMBED> element to ensure that the
118   // listener is active before the NaCl module 'load' event fires.
119   this.boundModuleDidLoad_ = this.moduleDidLoad.bind(this);
120   contentDiv.addEventListener('load', this.boundModuleDidLoad_, true);
121
122   // Load the published .nexe.  This includes the 'nacl' attribute which
123   // shows how to load multi-architecture modules.  Each entry in the "nexes"
124   // object in the  .nmf manifest file is a key-value pair: the key is the
125   // runtime ('x86-32', 'x86-64', etc.); the value is a URL for the desired
126   // NaCl module.  To load the debug versions of your .nexes, set the 'nacl'
127   //  attribute to the _dbg.nmf version of the manifest file.
128   contentDiv.innerHTML = '<embed id="'
129                          + tumbler.Application.DomIds_.MODULE + '" '
130                          + 'src=tumbler.nmf '
131                          + 'type="application/x-nacl" '
132                          + 'width="480" height="480" />'
133 }