16 var Viewer2d = Viewer2d || {};
24 Viewer2d.launch =
function() {
26 Debug.log(
"Viewer2d.launch");
28 Viewer2d.omni = document.getElementById(
"omni");
29 Viewer2d.omni.innerHTML =
"";
32 var w = Viewer2d.w = window.innerWidth;
33 var h = Viewer2d.h = window.innerHeight;
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)";
42 Viewer2d.omni.innerHTML =
"<center><div style='margin-top:" + (h/2-8) +
"px'>Loading 2-D...</div></center>";
53 Viewer2d.initCanvas();
60 Viewer2d.CANVAS_COLOR =
"rgb(102,0,51)";
61 Viewer2d.CANVAS_MIN_SIZE = 300;
62 Viewer2d.HUD_WIDTH = 200;
63 Viewer2d.HUD_MARGIN_RIGHT = 10;
64 Viewer2d.HUD_DROP_DOWN_SPEED = 10;
65 Viewer2d.TICK_REFRESH_PERIOD = 30;
75 Viewer2d.drawGridFlag =
true;
76 Viewer2d.globalAccumulate =
true;
86 Viewer2d.stations = [];
98 Viewer2d.initCanvas =
function() {
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;
105 Viewer2d.canvas.style.backgroundColor = Viewer2d.CANVAS_COLOR;
106 Viewer2d.context = Viewer2d.canvas.getContext(
'2d');
108 Viewer2d.omni.appendChild(Viewer2d.canvas);
110 Viewer2d.hud =
new Viewer2d.createHud();
112 window.onresize = Viewer2d.handleWindowResize;
113 Viewer2d.handleWindowResize();
115 Viewer2d.getConfiguration();
123 Viewer2d.tick =
function() {
127 Viewer2d.handleWindowResize =
function() {
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;
132 Debug.log(
"Viewer2d.handleWindowResize " + w +
"-" + h);
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;
142 Viewer2d.hud.handleWindowResize();
148 Viewer2d.createHud =
function() {
151 var animationTargetTop, isDropDownAnimating, isDropDownDown;
152 var controlMouseIsDown =
false;
153 var controlMouseTimeout = 0;
154 var getEventsTimeout = 0;
156 this.isInMotion =
function() {
return isDropDownAnimating; }
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";
162 this.hudNavSpeedDiv.style.left = 5 +
"px";
163 this.hudNavSpeedDiv.style.top = window.innerHeight - 95 +
"px";
166 this.checkboxUpdate =
function(i) {
167 var chk = document.getElementById(
"hudCheckbox" + i);
168 Debug.log(
"Viewer2d Hud checkboxUpdate " + i +
"=" + chk.checked);
170 if(i==0) Viewer2d.drawGridFlag = chk.checked;
171 else if(i==1) Viewer2d.globalAccumulate = chk.checked;
177 var animateDropDown =
function() {
178 var dir = (animationTargetTop - hudMouseOverDiv.offsetTop > 0)? 1: -1;
180 var tmpTop = hudMouseOverDiv.offsetTop + dir*Viewer2d.HUD_DROP_DOWN_SPEED;
181 if(Math.abs(tmpTop - animationTargetTop) <= Viewer2d.HUD_DROP_DOWN_SPEED)
183 hudMouseOverDiv.style.top = animationTargetTop +
"px";
184 isDropDownAnimating =
false;
188 hudMouseOverDiv.style.top = tmpTop +
"px";
189 window.setTimeout(animateDropDown,30);
193 var mouseOverDropDown = this.mouseOverDropDown =
function() {
194 if(isDropDownAnimating)
return;
198 isDropDownDown =
true;
199 isDropDownAnimating =
true;
200 animationTargetTop = -15;
201 window.setTimeout(animateDropDown,30);
206 var mouseOutDropDown = this.mouseOutDropDown =
function(event) {
207 if(isDropDownAnimating)
return;
211 var e =
event.toElement ||
event.relatedTarget;
214 if(e ==
this)
return;
221 isDropDownDown =
false;
222 isDropDownAnimating =
true;
223 animationTargetTop = 15 - hudMouseOverDiv.offsetHeight;
224 window.setTimeout(animateDropDown,30);
229 this.handleControlDown =
function(i,evt) {
233 if(controlMouseIsDown)
239 controlMouseIsDown =
true;
241 if(controlMouseIsDown)
243 if(controlMouseTimeout) window.clearTimeout(controlMouseTimeout);
244 controlMouseTimeout = window.setTimeout(
function() {Viewer2d.hud.handleControlDown(i)},to);
249 this.handleControlUp =
function(i) {
250 if(!controlMouseIsDown)
return;
254 if(controlMouseTimeout) window.clearTimeout(controlMouseTimeout);
255 controlMouseIsDown =
false;
260 var doControlAction =
function(i) {
268 Viewer2d.eventToDisplay += (i==0?-1:1)*parseInt(Viewer2d.events.length/5+1);
270 Viewer2d.eventToDisplay += (i==1?-1:1);
272 if(Viewer2d.eventToDisplay < 0)
273 Viewer2d.eventToDisplay = 0;
274 if(Viewer2d.eventToDisplay >= Viewer2d.events.length)
275 Viewer2d.eventToDisplay = Viewer2d.events.length - 1;
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);
285 Debug.log(
"hud doControlAction unknown action" + i);
289 Viewer2d.hud.update();
293 this.handleRunChange =
function() {
295 var s = this.hudRunNumberInput.value;
296 for(var i=0;i<s.length;++i)
298 if(!(parseInt(s[i])+1))
300 s = s.substr(0,i) + s.substr(i+1);
305 if(s+
"" != this.hudRunNumberInput.value)
307 this.hudRunNumberInput.value = s;
308 Debug.log(
"hud handleRunChange " + s);
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);
320 hudMouseOverDiv = this.hudMouseOverDiv = document.createElement(
'div');
321 hudMouseOverDiv.setAttribute(
"id",
"Viewer2d-hudMouseOver");
322 hudMouseOverDiv.style.position =
"absolute";
323 hudMouseOverDiv.style.zIndex = 100;
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":
""];
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>";
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>";
345 if(DesktopContent._theDesktopWindow)
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] +
"' />";
359 str +=
"<input id='Viewer2d-hudRunNumberControls-textInput' type='hidden' value='40' >";
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);
371 this.hudDiv.innerHTML = str;
378 hudMouseOverDiv.appendChild(this.hudDiv);
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);
386 this.hudEventNumber = document.getElementById(
'Viewer2d-hudEventNumber');
387 this.hudEventNumberControls = document.getElementById(
'Viewer2d-hudEventNumberControls');
388 this.hudRunNumberInput = document.getElementById(
'Viewer2d-hudRunNumberControls-textInput');
391 isDropDownDown =
false;
392 isDropDownAnimating =
true;
393 animationTargetTop = 15 - hudMouseOverDiv.offsetHeight;
394 window.setTimeout(animateDropDown,30);
395 this.handleWindowResize();
399 Viewer2d.getConfiguration =
function() {
400 Debug.log(
"Viewer2d getConfiguration ");
402 Viewer2d.getConfigurationHandler();
406 Viewer2d.getConfigurationHandler =
function (req) {
407 Debug.log(
"Viewer2d getConfigurationHandler " );
410 Viewer2d.stations= [];
413 Viewer2d.stations[cnt++] = [[1024, 1, 60, 90000]];
414 Viewer2d.stations[cnt++] = [[1024, 1, 60, 90000],[512, 1, 60, 90000]];
444 Viewer2d.canvas.style.zIndex = 0;
453 Viewer2d.redraw =
function () {
454 Debug.log(
"Viewer2d redraw " );
457 Viewer2d.context.save();
459 Viewer2d.context.clearRect(0, 0, Viewer2d.canvas.width, Viewer2d.canvas.height);
464 Viewer2d.context.fillStyle=
"RGBA(255,0,0,.2)";
465 Viewer2d.context.strokeStyle =
'RGBA(255,0,0,.1)';
466 Viewer2d.context.lineWidth = 100;
468 Viewer2d.context.translate(300,300);
469 Viewer2d.context.rotate(45*Math.PI/180);
471 Viewer2d.context.beginPath();
472 Viewer2d.context.moveTo(-200,0);
473 Viewer2d.context.lineTo(200,0);
474 Viewer2d.context.stroke();
479 Viewer2d.context.restore();
482 Viewer2d.context.save();
484 Viewer2d.context.fillStyle=
"RGBA(255,0,0,.2)";
485 Viewer2d.context.strokeStyle =
'RGBA(255,0,0,.1)';
486 Viewer2d.context.lineWidth = 100;
488 Viewer2d.context.translate(300,300);
489 Viewer2d.context.rotate(-45*Math.PI/180);
491 Viewer2d.context.beginPath();
492 Viewer2d.context.moveTo(-200,0);
493 Viewer2d.context.lineTo(200,0);
494 Viewer2d.context.stroke();
497 Viewer2d.context.restore();
513 Viewer2d.initForDraw =
function() {
517 Viewer2d.drawGrid =
function() {