Remoto - VFS: unityPane.js Source File
Remoto - VFS
unityPane.js
Go to the documentation of this file.
1 
2 define( [
3  'remoto!stdlib:js/panes/pane.js',
4  'remoto!stdlib:js/include/utils.js',
5  'remoto!stdlib:js/include/objectRegistry.js',
6  'remoto!stdlib:js/panes/panes/unityPane.css',
7  ],
8  function(pane,utils,objectRegistry)
9  {
10  'use strict';
11 
28  // Unity Pane
30 
31  unityPane.prototype = new pane;
32  function unityPane(layout)
33  {
34  pane.call(this,layout);
35 
36  this._type = "unityPane";
37 
38  this._url = null;
39 
40  this._context = {};
41  this._config = {
42  scale: 1
43  };
44  //{
45  // "urlPrefix": "https://some.domain.com/with/some/path/prefix",
46  // "config": "build.json",
47  // "progress": false
48  //}
49 
50 
51  this._progress = null;
52  this._instance = null;
53  this._createUnityInstance = null;
54  this._canvas = null; //not jquery object!
55 
56  this._active = false;
57  this._unityActivate = null;
58  this._unityDeactivate = null;
59 
60  this._subscriptions = {};
61  }
62 
63  unityPane.prototype.createHTML = function()
64  {
65  if (this._html) return this._html;
66 
67  pane.prototype.createHTML.call(this);
68 
69  var h = this._content;
70  h.addClass("unityPane");
71 
72  var c = $("<canvas class='unityCanvas'>").appendTo(this._content);
73  c.attr("tabindex","-1"); //for keyboard focus
74  c.bind("mouseenter mousemove", function() { c.focus(); } );
75 
76  this._canvas = c[0];
77 
78  //console.warn("unity create progress widget!!");
79  //this._progress =
80 
81  return this._html;
82  }
83 
84  unityPane.prototype.applySubscription = function(data,metadata)
85  {
86  //console.log("unityPane applySubscription()",arguments);
87 
88  this._config = utils.copyObject(data,this._config);
89 
90  this._context = utils.copyObject(metadata.context,this._context);
91  this._context = utils.copyObject(this._config,this._context);
92 
93  this._config = utils.resolveContextValues(this._config,this._context);
94 
95  if (!this._config.urlPrefix || !this._config.config)
96  console.error("unityPane subscription did not contain a url");
97 
98  var url = this._config.urlPrefix + "/" + this._config.config;
99 
100  if (url !== this._url)
101  {
102  this._url = url;
103 
104  //console.log("Loading unity config: ",this._url,this._config);
105  var jqxhr = $.get( this._url )
106  .done( this.configLoaded.bind(this) )
107  .fail( this.configLoadFail.bind(this,this._url) );
108 
109  this._html.trigger("paneLoadStart");
110  }
111  }
112 
113  unityPane.prototype.applyRequestError = function(command,id,reason)
114  {
115  console.error("unityPane applyRequestError",arguments);
116  }
117 
118  unityPane.prototype.applyDiff = function(diff,user)
119  {
120  //todo later... it's complicated to receive pointers to other projects after the initial load.
121  console.error("FIXME: unityPane applyDiff()",arguments);
122  }
123 
124  unityPane.prototype.resize = function()
125  {
126  //if (this._content)
127  // this._content.find("canvas").attr({
128  // width: this._content.width(),
129  // height: this._content.height()
130  // });
131  }
132 
133  unityPane.prototype.configLoaded = function(config)
134  {
135  var THIS = this;
136 
137  this._config = utils.copyObject(this._config,config);
138  this._context = utils.copyObject(this._config,this._context);
139  this._config = utils.resolveContextValues(this._config,this._context);
140 
141  //if (/iPhone|iPad|iPod|Android/i.test(navigator.userAgent))
142  // this._config.devicePixelRatio = 1;
143 
144  this._config.devicePixelRatio = this._config.scale;
145 
146  //console.log("CONFIG LOADED!",this._config);
147 
148  // https://api.jquery.com/jquery.getscript/
149  $.getScript( this._config.loaderUrl )
150  .done( function( data, textStatus, jqxhr )
151  {
152  if (textStatus === "success")
153  {
154  //console.log("loaded the loader!",THIS._config.loaderUrl,arguments);
155 
156  THIS._createUnityInstance = window.createUnityInstance;
157  delete window.createUnityInstance;
158 
159  //THIS.resize();
160 
161  THIS._createUnityInstance(THIS._canvas, THIS._config, THIS.loadProgress.bind(THIS) )
162  .then( THIS.loadComplete.bind(THIS) )
163  .catch( THIS.loadError.bind(THIS) );
164  }
165  else
166  THIS.configLoadFail(THIS._config.loaderUrl);
167  })
168  .fail( this.configLoadFail.bind(this,this._config.loaderUrl ) );
169  }
170 
171  unityPane.prototype.configLoadFail = function(url)
172  {
173  console.error("Failed to load unity setup: ",url);
174  }
175 
176  unityPane.prototype.loadError = function(error)
177  {
178  console.error("unity load error",arguments);
179  }
180 
181  unityPane.prototype.loadProgress = function(progress)
182  {
183  if (this._progress)
184  {
185  console.log("FIXME create progress widget!",progress);
186  //this._progress.value = progress;
187  }
188  else
189  console.log("unityPane load progress: ",progress);
190  }
191 
192  unityPane.prototype.loadComplete = function(unityInstance)
193  {
194  //console.log("unity load complete",arguments);
195 
196  this._instance = unityInstance;
197 
198  //if (this._progress)
199  // this._progress.hide();
200 
201  var sessionInfo = this._config.sessionInfo;
202 
203  this.toUnity = unityInstance.Module["asmLibraryArg"]["toUnity"].bind(this);
204  this._unityActivate = unityInstance.Module["asmLibraryArg"]["activatePane"].bind(this);
205  this._unityDeactivate = unityInstance.Module["asmLibraryArg"]["deactivatePane"].bind(this);
206  unityInstance.Module["asmLibraryArg"]["registerPane"]( this.fromUnity.bind(this), sessionInfo, this.vfsClientRunning.bind(this) );
207 
208  if (this._active)
209  this.activate();
210  else
211  this.deactivate();
212 
213  //moved to vfsClientRunning
214  //this._html.trigger("paneLoadComplete");
215  }
216 
217  unityPane.prototype.vfsClientRunning = function()
218  {
219  //console.log("VFS unity client running!");
220  this._html.trigger("paneLoadComplete");
221  }
222 
223  unityPane.prototype.quitComplete = function()
224  {
225  //probably never gets called because Quit() is during the destroy and we don't wait.
226  console.log("Unity quit complete!",arguments);
227  }
228 
229  unityPane.prototype.fromUnity = function(request)
230  {
231  //console.log("from unity",arguments);
232 
233  var command = request.command;
234  var attributes = request.attributes;
235 
236  switch(command)
237  {
238  case "subscribe": for (var p in attributes)
239  {
240  p = utils.cleanPath(p);
241  if (p in this._subscriptions)
242  this._subscriptions[p].increment(attributes[p]);
243  else
244  this._subscriptions[p] = new unitySubscription(p,attributes[p],this);
245  }
246 
247  break;
248 
249  case "unsubscribe": for (var p in attributes)
250  {
251  p = utils.cleanPath(p);
252  //if (p in this._subscriptions)
253  // this._subscriptions[p].decrement(attributes[p]);
254  //else
255  // this._subscriptions[p] = new unitySubscription(p,attributes[p],this);
256  console.log("can't unsubscribe yet!",p,attributes[p]);
257  }
258 
259  break;
260 
261  default: console.error("Unknown command: "+command);
262  break;
263  }
264  }
265 
266  unityPane.prototype.activate = function()
267  {
268  this._active = true;
269 
270  if (this._unityActivate)
271  this._unityActivate();
272  }
273 
274  unityPane.prototype.deactivate = function()
275  {
276  this._active = false;
277 
278  if (this._unityDeactivate)
279  this._unityDeactivate();
280  }
281 
282  unityPane.prototype.toUnity = function(message)
283  {
284  console.error("the unityPane::toUnity method will be overridden once the instance has loaded.");
285  }
286 
287  unityPane.prototype.destroy = function()
288  {
289  for (var s in this._subscriptions)
290  this._subscriptions[s].destroy();
291  this._subscriptions = {};
292 
293  if (this._instance)
294  { this._instance.Quit( this.quitComplete.bind(this) );
295  this._instance.Module["asmLibraryArg"]["_unregisterPane"]();
296  }
297 
298  pane.prototype.destroy.call(this);
299 
300  this._config = {};
301  this._instance = null;
302  this._createUnityInstance = null;
303  this._unityActivate = null;
304  this._unityDeactivate = null;
305 
306  this._canvas = null; //not jquery object!
307  this.toUnity = null;
308  }
309 
310 
312  // Unity Subscription
314 
315  function unitySubscription(path,count,instance)
316  {
317  this._path = path;
318  this._count = count * 1 || 1;
319  this._instance = instance;
320 
322  }
323 
324  unitySubscription.prototype.applySubscription = function(data,metadata)
325  {
326  //console.log("unitySubscription subscription",arguments);
327 
328  var c = {
329  command: "subscribe",
330  attributes: {
331  path: this._path,
332  data: data,
334  }
335  }
336 
337  this._instance.toUnity(c);
338  }
339 
340  unitySubscription.prototype.applyDiff = function(diff,user)
341  {
342  //console.log("unitySubscription diff",arguments);
343 
344  var c = {
345  command: "diff",
346  user: user,
347  attributes: {
348  path: this._path,
349  diff: diff
350  }
351  }
352 
353  this._instance.toUnity(c);
354  }
355 
356  unitySubscription.prototype.increment = function(c)
357  {
358  if (c && c*1)
359  { this._count += c * 1;
360  objectRegistry.registerObject(this._path,this); //should already be registered, but will cause it to load again
361  }
362  }
363 
364  unitySubscription.prototype.decrement = function(d)
365  {
366  console.log("FIXME: can't decrement yet");
367  }
368 
369  unitySubscription.prototype.destroy = function()
370  {
371  objectRegistry.unregisterObject(this._path,this);
372  }
373 
374  return unityPane;
375  }
376 );
message(m)
Change the message of an existing arrowMessage.
getter id
returns the number of milliseconds since midnight January 1, 1970 UTC
setter user
a setter DOCME
setter scale
Set a scale value for rendering.
registerObject(id, o, nosubscribe)
Register an object in the registry, and call the pathAddedCallback.
unregisterObject(id, o, silent, nounsubscribe, now)
Unregister an object from the registry.
Create a pane which will be mounted into a paneManager layout.
applyDiff(data, user)
applyRequestError(command, id, reason)
deactivate()
If a deactivate method is present on this pane's _object, call it.
resize()
DOCME.
destroy()
DOCME.
createHTML()
applySubscription(data, metadata)
pane(layout, object)
activate()
DOCME.
deactivate()
Deactivate this element and call deactivate on all its children.
setter config
a setter DOCME
getter path
a getter DOCME
progress(c, t, f, w, hoc)
The unityPane will load a WebGL-compiled Unity project into a pane.
Utility functions for javascript clients.
cleanPath(path)
Clean and normalize a resource path.
copyObject(source, dest)
Create a deep copy of a JSON object, extending the destination object recursively.
resolveContextValues(o, ctx)
Recursively resolve any context values in an object.
url(value, options)
metadata(paths)
setter error
Set the error value of this widget.