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.
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.
13 // Requires tumbler.Dragger
14 // Requires tumbler.Trackball
17 * Constructor for the Application class. Use the run() method to populate
18 * the object with controllers and wire up the events.
21 tumbler.Application = function() {
23 * The native module for the application. This refers to the module loaded
24 * via the <embed> tag.
31 * The trackball object.
32 * @type {tumbler.Trackball}
35 this.trackball_ = null;
38 * The mouse-drag event object.
39 * @type {tumbler.Dragger}
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.
50 this.boundModuleDidLoad_ = null;
54 * The ids used for elements in the DOM. The Tumlber Application expects these
59 tumbler.Application.DomIds_ = {
60 MODULE: 'tumbler', // The <embed> element representing the NaCl module
61 VIEW: 'tumbler_view' // The <div> containing the NaCl element.
65 * Called by the module loading function once the module has been loaded.
66 * @param {?Element} nativeModule The instance of the native module.
68 tumbler.Application.prototype.moduleDidLoad = function() {
69 this.module_ = document.getElementById(tumbler.Application.DomIds_.MODULE);
70 // Unbind the load function.
71 this.boundModuleDidLoad_ = null;
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.
78 this.module_.setCameraOrientation = function(orientation) {
79 var methodString = 'setCameraOrientation ' +
81 JSON.stringify(orientation);
82 this.postMessage(methodString);
85 this.trackball_ = new tumbler.Trackball();
86 this.dragger_ = new tumbler.Dragger(this.module_);
87 this.dragger_.addDragListener(this.trackball_);
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.
95 tumbler.Application.prototype.assert = function(cond, message) {
97 message = "Assertion failed: " + message;
99 throw new Error(message);
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.
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 + "'");
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);
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 + '" '
131 + 'type="application/x-nacl" '
132 + 'width="480" height="480" />'