summaryrefslogtreecommitdiff
path: root/browser/devtools/tilt/test/head.js
blob: 25482ead637f3200eba47010f12b714a4f31cdee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";

let {devtools} = Components.utils.import("resource://gre/modules/devtools/Loader.jsm", {});
let TiltManager = devtools.require("devtools/tilt/tilt").TiltManager;
let TiltGL = devtools.require("devtools/tilt/tilt-gl");
let {EPSILON, TiltMath, vec3, mat3, mat4, quat4} = devtools.require("devtools/tilt/tilt-math");
let TiltUtils = devtools.require("devtools/tilt/tilt-utils");
let {TiltVisualizer} = devtools.require("devtools/tilt/tilt-visualizer");

let tempScope = {};
Components.utils.import("resource:///modules/devtools/LayoutHelpers.jsm", tempScope);
let LayoutHelpers = tempScope.LayoutHelpers;


const DEFAULT_HTML = "data:text/html," +
  "<DOCTYPE html>" +
  "<html>" +
    "<head>" +
      "<meta charset='utf-8'/>" +
      "<title>Three Laws</title>" +
    "</head>" +
    "<body>" +
      "<div id='first-law'>" +
        "A robot may not injure a human being or, through inaction, allow a " +
        "human being to come to harm." +
      "</div>" +
      "<div>" +
        "A robot must obey the orders given to it by human beings, except " +
        "where such orders would conflict with the First Law." +
      "</div>" +
      "<div>" +
        "A robot must protect its own existence as long as such protection " +
        "does not conflict with the First or Second Laws." +
      "</div>" +
      "<div id='far-far-away' style='position: absolute; top: 250%;'>" +
        "I like bacon." +
      "</div>" +
    "<body>" +
  "</html>";

let Tilt = TiltManager.getTiltForBrowser(window);

const STARTUP = Tilt.NOTIFICATIONS.STARTUP;
const INITIALIZING = Tilt.NOTIFICATIONS.INITIALIZING;
const INITIALIZED = Tilt.NOTIFICATIONS.INITIALIZED;
const DESTROYING = Tilt.NOTIFICATIONS.DESTROYING;
const BEFORE_DESTROYED = Tilt.NOTIFICATIONS.BEFORE_DESTROYED;
const DESTROYED = Tilt.NOTIFICATIONS.DESTROYED;
const SHOWN = Tilt.NOTIFICATIONS.SHOWN;
const HIDDEN = Tilt.NOTIFICATIONS.HIDDEN;
const HIGHLIGHTING = Tilt.NOTIFICATIONS.HIGHLIGHTING;
const UNHIGHLIGHTING = Tilt.NOTIFICATIONS.UNHIGHLIGHTING;
const NODE_REMOVED = Tilt.NOTIFICATIONS.NODE_REMOVED;

const TILT_ENABLED = Services.prefs.getBoolPref("devtools.tilt.enabled");


function isTiltEnabled() {
  info("Apparently, Tilt is" + (TILT_ENABLED ? "" : " not") + " enabled.");
  return TILT_ENABLED;
}

function isWebGLSupported() {
  let supported = !TiltGL.isWebGLForceEnabled() &&
                   TiltGL.isWebGLSupported() &&
                   TiltGL.create3DContext(createCanvas());

  info("Apparently, WebGL is" + (supported ? "" : " not") + " supported.");
  return supported;
}

function isApprox(num1, num2, delta) {
  if (Math.abs(num1 - num2) > (delta || EPSILON)) {
    info("isApprox expected " + num1 + ", got " + num2 + " instead.");
    return false;
  }
  return true;
}

function isApproxVec(vec1, vec2, delta) {
  vec1 = Array.prototype.slice.call(vec1);
  vec2 = Array.prototype.slice.call(vec2);

  if (vec1.length !== vec2.length) {
    return false;
  }
  for (let i = 0, len = vec1.length; i < len; i++) {
    if (!isApprox(vec1[i], vec2[i], delta)) {
      info("isApproxVec expected [" + vec1 + "], got [" + vec2 + "] instead.");
      return false;
    }
  }
  return true;
}

function isEqualVec(vec1, vec2) {
  vec1 = Array.prototype.slice.call(vec1);
  vec2 = Array.prototype.slice.call(vec2);

  if (vec1.length !== vec2.length) {
    return false;
  }
  for (let i = 0, len = vec1.length; i < len; i++) {
    if (vec1[i] !== vec2[i]) {
      info("isEqualVec expected [" + vec1 + "], got [" + vec2 + "] instead.");
      return false;
    }
  }
  return true;
}

function createCanvas() {
  return document.createElementNS("http://www.w3.org/1999/xhtml", "canvas");
}


function createTab(callback, location) {
  info("Creating a tab, with callback " + typeof callback +
                      ", and location " + location + ".");

  let tab = gBrowser.selectedTab = gBrowser.addTab();

  gBrowser.selectedBrowser.addEventListener("load", function onLoad() {
    gBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
    callback(tab);
  }, true);

  gBrowser.selectedBrowser.contentWindow.location = location || DEFAULT_HTML;
  return tab;
}


function createTilt(callbacks, close, suddenDeath) {
  info("Creating Tilt, with callbacks {" + Object.keys(callbacks) + "}" +
                   ", autoclose param " + close +
          ", and sudden death handler " + typeof suddenDeath + ".");

  handleFailure(suddenDeath);

  Services.prefs.setBoolPref("webgl.verbose", true);
  TiltUtils.Output.suppressAlerts = true;

  info("Attempting to start Tilt.");
  Services.obs.addObserver(onTiltOpen, INITIALIZING, false);
  Tilt.toggle();

  function onTiltOpen() {
    info("Tilt was opened.");
    Services.obs.removeObserver(onTiltOpen, INITIALIZING);

    executeSoon(function() {
      if ("function" === typeof callbacks.onTiltOpen) {
        info("Calling 'onTiltOpen'.");
        callbacks.onTiltOpen(Tilt.visualizers[Tilt.currentWindowId]);
      }
      if (close) {
        executeSoon(function() {
          info("Attempting to close Tilt.");
          Services.obs.addObserver(onTiltClose, DESTROYED, false);
          Tilt.destroy(Tilt.currentWindowId);
        });
      }
    });
  }

  function onTiltClose() {
    info("Tilt was closed.");
    Services.obs.removeObserver(onTiltClose, DESTROYED);

    executeSoon(function() {
      if ("function" === typeof callbacks.onTiltClose) {
        info("Calling 'onTiltClose'.");
        callbacks.onTiltClose();
      }
      if ("function" === typeof callbacks.onEnd) {
        info("Calling 'onEnd'.");
        callbacks.onEnd();
      }
    });
  }

  function handleFailure(suddenDeath) {
    Tilt.failureCallback = function() {
      info("Tilt FAIL.");
      Services.obs.removeObserver(onTiltOpen, INITIALIZING);

      info("Now relying on sudden death handler " + typeof suddenDeath + ".");
      suddenDeath && suddenDeath();
    }
  }
}

function getPickablePoint(presenter) {
  let vertices = presenter._meshStacks[0].vertices.components;

  let topLeft = vec3.create([vertices[0], vertices[1], vertices[2]]);
  let bottomRight = vec3.create([vertices[6], vertices[7], vertices[8]]);
  let center = vec3.lerp(topLeft, bottomRight, 0.5, []);

  let renderer = presenter._renderer;
  let viewport = [0, 0, renderer.width, renderer.height];

  return vec3.project(center, viewport, renderer.mvMatrix, renderer.projMatrix);
}