otsdaq_utilities  v2_05_02_indev
Viewer2d.js
1 //=====================================================================================
2 //
3 // Created Dec, 2013
4 // by Ryan Rivera ((rrivera at fnal.gov))
5 //
6 // Viewer2d.js
7 //
8 // requires an omni div, that will be made full window size (window.innerWidth and
9 // window.innerHeight) at position 0,0
10 //
11 // public function list:
12 // Viewer2d.launch()
13 //
14 //=====================================================================================
15 
16 var Viewer2d = Viewer2d || {}; //define namespace
17 
18 
20 //public function definitions
21 
22 // Viewer2d.launch ~~
23 // called to start 2d viewer
24 Viewer2d.launch = function() {
25 
26  Debug.log("Viewer2d.launch");
27 
28  Viewer2d.omni = document.getElementById("omni");
29  Viewer2d.omni.innerHTML = ""; //clear all content
30 
31  //initialize with loading message in center
32  var w = Viewer2d.w = window.innerWidth;
33  var h = Viewer2d.h = window.innerHeight;
34 
35  Viewer2d.omni.style.position = "absolute";
36  Viewer2d.omni.style.left = 0 + "px";
37  Viewer2d.omni.style.top = 0 + "px";
38  Viewer2d.omni.style.width = w + "px";
39  Viewer2d.omni.style.height = h + "px";
40  Viewer2d.omni.style.backgroundColor = "rgb(30,30,30)";
41 
42  Viewer2d.omni.innerHTML = "<center><div style='margin-top:" + (h/2-8) + "px'>Loading 2-D...</div></center>";
43 
44 
45  //load/insert libraries and setup canvas
46  // steps:
47  // - setup canvas behind "loading..."
48  // - setup hud
49  // - get default configuration from server
50  // - get default geometry from server
51  // - bring canvas in front of "loading..."
52 
53  Viewer2d.initCanvas();
54 }
55 
56 //end public function definitions
58 
59 
60 Viewer2d.CANVAS_COLOR = "rgb(102,0,51)";
61 Viewer2d.CANVAS_MIN_SIZE = 300; //dont allow w or h to go less than this
62 Viewer2d.HUD_WIDTH = 200;
63 Viewer2d.HUD_MARGIN_RIGHT = 10;
64 Viewer2d.HUD_DROP_DOWN_SPEED = 10;
65 Viewer2d.TICK_REFRESH_PERIOD = 30; //ms
66 
67 Viewer2d.omni;
68 Viewer2d.hud;
69 Viewer2d.canvas;
70 Viewer2d.context;
71 Viewer2d.w;
72 Viewer2d.h;
73 Viewer2d.tickTimer;
74 
75 Viewer2d.drawGridFlag = true;
76 Viewer2d.globalAccumulate = true;
77 
78 
79 
80 //station consists one or two planes, only can be two planes if they are strip planes
81 // [station 0, (optional station 1)]
82 
83 //a plane consists of strips or pixels
84 // [cols, rows, cell width, cell height] //microns
85 
86 Viewer2d.stations = [];
87 
88 
89 //"private" function list
90 // Viewer2d.initCanvas
91 // Viewer2d.handleWindowResize
92 // Viewer2d.createHud
93 // Viewer2d.redraw
94 // Viewer2d.getConfiguration
95 // Viewer2d.getConfigurationHandler
96 
97 
98 Viewer2d.initCanvas = function() {
99 
100  Viewer2d.canvas = document.createElement('canvas');
101  Viewer2d.canvas.style.position = "absolute";
102  Viewer2d.canvas.style.left = 0 + "px";
103  Viewer2d.canvas.style.top = 0 + "px";
104  Viewer2d.canvas.style.zIndex = -1; //-1 is not visible.. change to 0 to bring to front
105  Viewer2d.canvas.style.backgroundColor = Viewer2d.CANVAS_COLOR;
106  Viewer2d.context = Viewer2d.canvas.getContext('2d');
107 
108  Viewer2d.omni.appendChild(Viewer2d.canvas);
109 
110  Viewer2d.hud = new Viewer2d.createHud();
111 
112  window.onresize = Viewer2d.handleWindowResize;
113  Viewer2d.handleWindowResize();
114 
115  Viewer2d.getConfiguration();
116 
117  //if(Viewer2d.tickTimer) clearInterval(Viewer2d.tickTimer);
118  //Viewer2d.tickTimer = setInterval(Viewer2d.tick, Viewer2d.TICK_REFRESH_PERIOD);
119 }
120 
121 //Viewer2d.tick ~~
122 // periodically check for data
123 Viewer2d.tick = function() {
124  Viewer2d.redraw();
125 }
126 
127 Viewer2d.handleWindowResize = function() {
128 
129  var w = window.innerWidth < Viewer2d.CANVAS_MIN_SIZE? Viewer2d.CANVAS_MIN_SIZE:window.innerWidth;
130  var h = window.innerHeight < Viewer2d.CANVAS_MIN_SIZE? Viewer2d.CANVAS_MIN_SIZE:window.innerHeight;
131 
132  Debug.log("Viewer2d.handleWindowResize " + w + "-" + h);
133 
134  Viewer2d.omni.style.width = w + "px";
135  Viewer2d.omni.style.height = h + "px";
136  Viewer2d.canvas.style.width = w + "px";
137  Viewer2d.canvas.style.height = h + "px";
138  Viewer2d.canvas.width = w;
139  Viewer2d.canvas.height = h;
140 
141  //reposition HUD
142  Viewer2d.hud.handleWindowResize();
143 
144  //redraw 2d world
145  Viewer2d.redraw();
146 }
147 
148 Viewer2d.createHud = function() {
149 
150  var hudMouseOverDiv;
151  var animationTargetTop, isDropDownAnimating, isDropDownDown;
152  var controlMouseIsDown = false;
153  var controlMouseTimeout = 0;
154  var getEventsTimeout = 0;
155 
156  this.isInMotion = function() { return isDropDownAnimating; }
157 
158  this.handleWindowResize = function() {
159  Debug.log("Viewer2d Hud handleWindowResize");
160  this.hudMouseOverDiv.style.left = window.innerWidth - this.hudMouseOverDiv.offsetWidth - Viewer2d.HUD_MARGIN_RIGHT + "px";
161 
162  this.hudNavSpeedDiv.style.left = 5 + "px";
163  this.hudNavSpeedDiv.style.top = window.innerHeight - 95 + "px";
164  }
165 
166  this.checkboxUpdate = function(i) {
167  var chk = document.getElementById("hudCheckbox" + i);
168  Debug.log("Viewer2d Hud checkboxUpdate " + i + "=" + chk.checked);
169 
170  if(i==0) Viewer2d.drawGridFlag = chk.checked; //grid
171  else if(i==1) Viewer2d.globalAccumulate = chk.checked; //globalAccumulate
172 
173  Viewer2d.redraw();
174  }
175 
176  // animateDropDown ~~
177  var animateDropDown = function() {
178  var dir = (animationTargetTop - hudMouseOverDiv.offsetTop > 0)? 1: -1;
179 
180  var tmpTop = hudMouseOverDiv.offsetTop + dir*Viewer2d.HUD_DROP_DOWN_SPEED;
181  if(Math.abs(tmpTop - animationTargetTop) <= Viewer2d.HUD_DROP_DOWN_SPEED) //done
182  {
183  hudMouseOverDiv.style.top = animationTargetTop + "px";
184  isDropDownAnimating = false;
185  return;
186  }
187  //else still going towards target
188  hudMouseOverDiv.style.top = tmpTop + "px";
189  window.setTimeout(animateDropDown,30);
190  }
191 
192  // mouseOverDropDown ~~
193  var mouseOverDropDown = this.mouseOverDropDown = function() {
194  if(isDropDownAnimating) return; //do nothing if animating currently
195 
196  if(!isDropDownDown) //start animation
197  {
198  isDropDownDown = true;
199  isDropDownAnimating = true;
200  animationTargetTop = -15;
201  window.setTimeout(animateDropDown,30);
202  }
203  }
204 
205  // mouseOutDropDown ~~
206  var mouseOutDropDown = this.mouseOutDropDown = function(event) {
207  if(isDropDownAnimating) return; //do nothing if animating currently
208 
209  if(event)
210  {
211  var e = event.toElement || event.relatedTarget;
212  while(e) //if moving within drop down menu ignore
213  {
214  if(e == this) return;
215  e = e.parentNode;
216  }
217  }
218 
219  if(isDropDownDown) //start animation
220  {
221  isDropDownDown = false;
222  isDropDownAnimating = true;
223  animationTargetTop = 15 - hudMouseOverDiv.offsetHeight;
224  window.setTimeout(animateDropDown,30);
225  }
226  }
227 
228  //for event and run number buttons
229  this.handleControlDown = function(i,evt) {
230  //Debug.log("hud control down " + i);
231 
232  var to = 500;
233  if(controlMouseIsDown) //already mouse down, so do action
234  {
235  to = 10;
236  doControlAction(i);
237  }
238  else if(evt)
239  controlMouseIsDown = true;
240 
241  if(controlMouseIsDown)
242  {
243  if(controlMouseTimeout) window.clearTimeout(controlMouseTimeout);
244  controlMouseTimeout = window.setTimeout(function() {Viewer2d.hud.handleControlDown(i)},to);
245  }
246  }
247 
248  //for event and run number buttons
249  this.handleControlUp = function(i) {
250  if(!controlMouseIsDown) return;
251 
252  //Debug.log("hud control up " + i);
253 
254  if(controlMouseTimeout) window.clearTimeout(controlMouseTimeout);
255  controlMouseIsDown = false;
256  doControlAction(i); //do action on mouse up
257  }
258 
259  //for event and run number buttons
260  var doControlAction = function(i) {
261  switch(i)
262  {
263  case 0:
264  case 1:
265  case 2:
266  case 3:
267  if(i!=1 && i!=2)
268  Viewer2d.eventToDisplay += (i==0?-1:1)*parseInt(Viewer2d.events.length/5+1);
269  else
270  Viewer2d.eventToDisplay += (i==1?-1:1);
271 
272  if(Viewer2d.eventToDisplay < 0)
273  Viewer2d.eventToDisplay = 0;
274  if(Viewer2d.eventToDisplay >= Viewer2d.events.length)
275  Viewer2d.eventToDisplay = Viewer2d.events.length - 1;
276  break;
277  case 4: //dec run #
278  case 5: //inc run #
279  Viewer2d.hud.hudRunNumberInput.value = parseInt(Viewer2d.hud.hudRunNumberInput.value) + (i*2-9);
280  if(Viewer2d.hud.hudRunNumberInput.value < 0) Viewer2d.hud.hudRunNumberInput.value = 0;
281  if(getEventsTimeout) window.clearTimeout(getEventsTimeout);
282  getEventsTimeout = window.setTimeout(Viewer2d.getEvents,1000);
283  break;
284  default:
285  Debug.log("hud doControlAction unknown action" + i);
286  return;
287  }
288 
289  Viewer2d.hud.update();
290  }
291 
292  //only allow positive integers, remove all non numbers and parse as int
293  this.handleRunChange = function() {
294  //Debug.log("hud handleRunChange " );
295  var s = this.hudRunNumberInput.value;
296  for(var i=0;i<s.length;++i)
297  {
298  if(!(parseInt(s[i])+1)) //remove non numbers
299  {
300  s = s.substr(0,i) + s.substr(i+1);
301  --i; //rewind
302  }
303  }
304  s = parseInt(s);
305  if(s+"" != this.hudRunNumberInput.value) //string comparison
306  {
307  this.hudRunNumberInput.value = s;
308  Debug.log("hud handleRunChange " + s);
309  }
310  }
311 
312 
313  this.hudNavSpeedDiv = document.createElement('div');
314  this.hudNavSpeedDiv.setAttribute("id", "Viewer2d-hudNavSpeedOverlay");
315  this.hudNavSpeedDiv.style.position = "absolute";
316  this.hudNavSpeedDiv.style.zIndex = 100;
317  Viewer2d.omni.appendChild(this.hudNavSpeedDiv);
318 
319 
320  hudMouseOverDiv = this.hudMouseOverDiv = document.createElement('div');
321  hudMouseOverDiv.setAttribute("id", "Viewer2d-hudMouseOver");
322  hudMouseOverDiv.style.position = "absolute";
323  hudMouseOverDiv.style.zIndex = 100;
324 
325  this.hudDiv = document.createElement('div');
326  this.hudDiv.setAttribute("id", "Viewer2d-hud");
327  var chkLabels = ["Show Axes","Show Grid","Show Tracks", "Mouse Nav","Fly-By","Accumulated"];
328  var chkDefaults = ["checked","checked","checked","checked",Viewer2d.enableFlyBy?"checked":"",Viewer2d.accumulateEvents?"checked":""];
329  var str = "";
330  for(var i=0;i<chkLabels.length;++i)
331  str += "<input type='checkbox' id='hudCheckbox" + i + "' onchange='Viewer2d.hud.checkboxUpdate(" + i +
332  ");' " + chkDefaults[i] + "><label for='hudCheckbox" + i + "'>" + chkLabels[i] + "</label><br>";
333 
334  //add event controls
335  str += "<center><div id='Viewer2d-hudEventNumberControls'><div id='Viewer2d-hudEventNumber'></div>";
336  var evtNumBtns = ["<<","<",">",">>"];
337  for(var i=0;i<evtNumBtns.length;++i)
338  str += "<input type='button' onmousedown='Viewer2d.hud.handleControlDown(" + i + ",event);' " +
339  "onmouseup='Viewer2d.hud.handleControlUp(" + i + ");' " +
340  "onmouseout='Viewer2d.hud.handleControlUp(" + i + ");' " +
341  "value='" + evtNumBtns[i] + "' />";
342  str += "</div></center>";
343 
344  //add run controls
345  if(DesktopContent._theDesktopWindow) //only add if successful login
346  {
347  str += "<div id='Viewer2d-hudRunNumberControls'>Run #: " +
348  "<input id='Viewer2d-hudRunNumberControls-textInput' oninput='Viewer2d.hud.handleRunChange();' type='text' value='40' > ";
349  evtNumBtns = ["<",">"];
350  for(var i=0;i<evtNumBtns.length;++i)
351  str += "<input type='button' onmousedown='Viewer2d.hud.handleControlDown(" + (i+4) + ",event);' " +
352  "onmouseup='Viewer2d.hud.handleControlUp(" + (i+4) + ");' " +
353  "onmouseout='Viewer2d.hud.handleControlUp(" + (i+4) + ");' " +
354  "value='" + evtNumBtns[i] + "' />";
355  str += "</div>";
356  }
357  else
358  {
359  str += "<input id='Viewer2d-hudRunNumberControls-textInput' type='hidden' value='40' >";
360 
361  this.hudUrlOverlay = document.createElement('div');
362  this.hudUrlOverlay.setAttribute("id", "Viewer2d-hudUrlOverlay");
363  this.hudUrlOverlay.style.position = "absolute";
364  this.hudUrlOverlay.style.zIndex = 100;
365  this.hudUrlOverlay.style.left = 5 + "px";
366  this.hudUrlOverlay.style.top = 5 + "px";
367  this.hudUrlOverlay.innerHTML = "Try on your own mobile device!<br>http://tinyurl.com/q6lhdrm";
368  Viewer2d.omni.appendChild(this.hudUrlOverlay);
369  }
370 
371  this.hudDiv.innerHTML = str;
372 
373 // _historyEl.onmousewheel = handleUserInputScroll;
374 // _historyEl.onmousedown = handleUserInputScroll;
375 // this.hudEventScrollbar.onscroll = this.handleEventScroll;
376 // _historyEl.onmousemove = handleMouseMoveScroll;
377 
378  hudMouseOverDiv.appendChild(this.hudDiv);
379 
380  hudMouseOverDiv.style.top = hudMouseOverDiv.offsetHeight - 15 + "px";
381  hudMouseOverDiv.style.width = Viewer2d.HUD_WIDTH + "px";
382  hudMouseOverDiv.onmouseover = mouseOverDropDown;
383  hudMouseOverDiv.onmouseout = mouseOutDropDown;
384  Viewer2d.omni.appendChild(hudMouseOverDiv);
385 
386  this.hudEventNumber = document.getElementById('Viewer2d-hudEventNumber'); //get reference
387  this.hudEventNumberControls = document.getElementById('Viewer2d-hudEventNumberControls'); //get reference
388  this.hudRunNumberInput = document.getElementById('Viewer2d-hudRunNumberControls-textInput'); //get reference
389 
390  //setup dropdown effect
391  isDropDownDown = false;
392  isDropDownAnimating = true;
393  animationTargetTop = 15 - hudMouseOverDiv.offsetHeight;
394  window.setTimeout(animateDropDown,30);
395  this.handleWindowResize();
396 }
397 
398 
399 Viewer2d.getConfiguration = function() {
400  Debug.log("Viewer2d getConfiguration ");
401  //DesktopContent.XMLHttpRequest("Request?RequestType=get2dConfiguration", "", Viewer2d.getConfigurationHandler);
402  Viewer2d.getConfigurationHandler();
403 }
404 
405 // Viewer2d.getConfigurationHandler ~~
406 Viewer2d.getConfigurationHandler = function (req) {
407  Debug.log("Viewer2d getConfigurationHandler " );
408 
409 
410  Viewer2d.stations= [];
411 
412  var cnt = 0;
413  Viewer2d.stations[cnt++] = [[1024, 1, 60, 90000]];
414  Viewer2d.stations[cnt++] = [[1024, 1, 60, 90000],[512, 1, 60, 90000]];
415 
416  //var objects = req.responseXML.getElementsByTagName("object");
417 
418 // Viewer2d.geometry = [];
419 // var gi;
420 // var gl = Viewer2d.gl;
421 // var locPoints, xyzPoints, objectType;
422 // for(var i=0;i<objects.length;++i)
423 // {
424 // //TODO if different geometries, use object type
425 // //objectType = objects[i].getElementsByTagName("object_type");
426 // //Debug.log("Rotating3d getGeometry objectType " + objectType[0].getAttribute("value"));
427 //
428 // xyzPoints = objects[i].getElementsByTagName("xyz_point");
429 //
430 // locPoints = [];
431 // for(var j=0;j<xyzPoints.length;++j)
432 // locPoints[locPoints.length] = xyzPoints[j].getAttribute("value")/(j%3==2?Viewer2d.Z_SQUASH_FACTOR:1.0);
433 //
434 //
435 // gi = Viewer2d.geometry.length;
436 // Viewer2d.geometry[gi] = gl.createBuffer();
437 // gl.bindBuffer(gl.ARRAY_BUFFER, Viewer2d.geometry[gi]);
438 // gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(locPoints), gl.STATIC_DRAW);
439 // Viewer2d.geometry[gi].itemSize = 3;
440 // Viewer2d.geometry[gi].numItems = locPoints.length/3;
441 // }
442 //
443 // Debug.log("Viewer2d getGeometryHandler geometry objects " + Viewer2d.geometry.length);
444  Viewer2d.canvas.style.zIndex = 0; //Bring canvas to front, above "Loading..." if not already there
445 //
446 // Viewer2d.cameraAction = true;
447 
448  Viewer2d.redraw();
449 }
450 
451 //Viewer2d.redraw ~~
452 // called to redraw 2d objects
453 Viewer2d.redraw = function () {
454  Debug.log("Viewer2d redraw " );
455 
456 
457  Viewer2d.context.save();
458 
459  Viewer2d.context.clearRect(0, 0, Viewer2d.canvas.width, Viewer2d.canvas.height);
460 
461  //
462 
463 
464  Viewer2d.context.fillStyle="RGBA(255,0,0,.2)";
465  Viewer2d.context.strokeStyle = 'RGBA(255,0,0,.1)';
466  Viewer2d.context.lineWidth = 100;
467 
468  Viewer2d.context.translate(300,300);
469  Viewer2d.context.rotate(45*Math.PI/180);
470 
471  Viewer2d.context.beginPath();
472  Viewer2d.context.moveTo(-200,0);
473  Viewer2d.context.lineTo(200,0);
474  Viewer2d.context.stroke();
475 
476 
477  //Viewer2d.context.fillRect(-100,-50,200,100);
478 
479  Viewer2d.context.restore();
480 
481 
482  Viewer2d.context.save();
483 
484  Viewer2d.context.fillStyle="RGBA(255,0,0,.2)";
485  Viewer2d.context.strokeStyle = 'RGBA(255,0,0,.1)';
486  Viewer2d.context.lineWidth = 100;
487 
488  Viewer2d.context.translate(300,300);
489  Viewer2d.context.rotate(-45*Math.PI/180);
490 
491  Viewer2d.context.beginPath();
492  Viewer2d.context.moveTo(-200,0);
493  Viewer2d.context.lineTo(200,0);
494  Viewer2d.context.stroke();
495  //Viewer2d.context.fillRect(-100,-50,200,100);
496 
497  Viewer2d.context.restore();
498 
499 // if(Viewer2d.hud.isInMotion()) return;
500 //
501 // Viewer2d.initForDraw();
502 // if(Viewer2d.drawAxesFlag) Viewer2d.drawAxes();
503 // if(Viewer2d.drawGridFlag) Viewer2d.drawGrid();
504 // Viewer2d.drawGeometry();
505 //
506 // //drawScene();
507 //
508 // Viewer2d.drawClusters((Viewer2d.accumulateEvents && !Viewer2d.enableFlyBy)?-1:Viewer2d.eventToDisplay);
509 // if(Viewer2d.drawTracksFlag) Viewer2d.drawTracks((Viewer2d.accumulateEvents && !Viewer2d.enableFlyBy)?-1:Viewer2d.eventToDisplay);
510 // Viewer2d.hud.update(); //update hud
511 }
512 
513 Viewer2d.initForDraw = function() {
514 
515 }
516 
517 Viewer2d.drawGrid = function() {
518 
519 }
520 
521 
522 
523