forked from judnich/Kosmos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
267 lines (214 loc) · 6.8 KB
/
index.js
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Copyright (C) 2013 John Judnich
// Released under The MIT License - see "LICENSE" file for details.
// NOTE: Needs major cleanup, decouple events from html, change to coffeescript
// This file is meant to cover all UI stuff and abstract that away from the main
// simulation/game code found in Main.coffee. This file isn't that "important" right
// now in the sense that most of Kosmos is in the 3D rendering stuff, not the management
// of slider bars.
_speed = 0.0;
_reverseMode = false;
_sliderMouseDown = false;
_ignoreWarning = false;
_autopilotMode = true;
function detectMobile() {
if( navigator.userAgent.match(/Android/i)
|| navigator.userAgent.match(/webOS/i)
|| navigator.userAgent.match(/iPhone/i)
|| navigator.userAgent.match(/iPad/i)
|| navigator.userAgent.match(/iPod/i)
|| navigator.userAgent.match(/BlackBerry/i)
|| navigator.userAgent.match(/Windows Phone/i) ){
return true;
}
else {
return false;
}
}
function jsMain() {
updateMessagePositions();
showIntro();
var is_chrome = navigator.userAgent.indexOf('Chrome') > -1;
var is_explorer = navigator.userAgent.indexOf('MSIE') > -1;
var is_firefox = navigator.userAgent.indexOf('Firefox') > -1;
var is_safari = navigator.userAgent.indexOf("Safari") > -1;
var is_opera = navigator.userAgent.indexOf("Presto") > -1;
if ((is_chrome)&&(is_safari)) {is_safari=false;}
var is_mobile = detectMobile();
var OSName="Unknown OS";
if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";
if (is_mobile) {
document.location = "http://judnich.github.io/KosmosMobile";
}
if ( OSName=="Windows" || !is_firefox ) {
if (_ignoreWarning != true) {
document.getElementById("browserErrorMessage").style.display = "block";
return;
}
}
function resizeCanvas() {
// correct canvas dimensions due to sidebar
document.getElementById("rightbar").style.width =
(window.innerWidth - document.getElementById("sidebar").clientWidth) + "px";
}
resizeCanvas();
// set up slider events
var slider = document.getElementById("slider");
function sliderEvent(x, y) {
_speed = 1.0 - ((y-3) / (slider.clientHeight+3));
if (_speed > 1) _speed = 1;
updateSpeed();
}
function mouseWheelEvent(event) {
if (event.wheelDelta)
d = event.wheelDelta;
else
d = event.detail * -30.0; // stupid firefox incompatibility
_speed -= d / 4000.0
if (_speed > 1) _speed = 1;
updateSpeed();
event.preventDefault();
return false;
}
var mousewheelevt=(/Firefox/i.test(navigator.userAgent))? "DOMMouseScroll" : "mousewheel" //FF doesn't recognize mousewheel as of FF3.x
if (document.attachEvent) //if IE (and Opera depending on user setting)
document.attachEvent("on"+mousewheelevt, mouseWheelEvent)
else if (document.addEventListener) //WC3 browsers
document.addEventListener(mousewheelevt, mouseWheelEvent, false)
document.body.style.overflow = 'hidden';
$(slider).mousedown(function(e) {
_sliderMouseDown = true;
var offX = (e.offsetX || e.clientX - $(e.target).offset().left);
var offY = (e.offsetY || e.clientY - $(e.target).offset().top);
sliderEvent(offX, offY);
});
$(slider).mouseup(function(event) {
_sliderMouseDown = false;
});
$(slider).mousemove(function(e) {
if (_sliderMouseDown) {
var offX = (e.offsetX || e.clientX - $(e.target).offset().left);
var offY = (e.offsetY || e.clientY - $(e.target).offset().top);
sliderEvent(offX, offY);
}
});
$(document).mouseup(function(event) {
_sliderMouseDown = false;
});
// CoffeeScript code entry point
kosmosMain();
updateSpeed();
showIntro();
window.onresize = function() {
resizeCanvas();
updateMessagePositions();
kosmosResize();
}
document.onkeydown = function(event) {
hideIntro();
// down arrow / S / -
if (event.keyCode == 189 || event.keyCode == 83 || event.keyCode == 40) {
_speed -= 0.005;
}
// up arrow / W / +
if (event.keyCode == 187 || event.keyCode == 87 || event.keyCode == 38) {
_speed += 0.005;
}
// escape
if (event.keyCode == 27) {
_speed = 0;
}
// space
if (event.keyCode == 32) {
_reverseMode = !_reverseMode;
updateButtons();
}
updateSpeed();
}
}
function hideIntro() {
var msg = document.getElementById("introMessage");
msg.style.display = "none";
}
function hideBrowserMessage() {
var msg = document.getElementById("browserErrorMessage");
msg.style.display = "none";
_ignoreWarning = true;
jsMain();
}
function hideErrorMessage() {
var msg = document.getElementById("glErrorMessage");
msg.style.display = "none";
}
function updateMessagePositions() {
var sidebarWidth = document.getElementById("sidebar").clientWidth;
var messages = document.getElementsByClassName("message");
for (var i = 0; i < messages.length; ++i) {
var msg = messages[i];
var oldDisp = msg.style.display;
msg.style.display = "block";
var x = sidebarWidth + (window.innerWidth-sidebarWidth)/2 - (msg.clientWidth+23)/2;
var y = window.innerHeight/2 - (msg.clientHeight+23)/2;
msg.style.display = oldDisp;
msg.style.left = x;
msg.style.top = y;
}
}
function showIntro() {
var msg = document.getElementById("introMessage");
msg.style.display = "block";
}
function showShare() {
hideIntro();
var msg = document.getElementById("shareMessage");
msg.style.display = "block";
btn = document.getElementById("shareBtn");
btn.className = "btn lit";
saveLocation(); // this updates the share link as well
}
function hideShare() {
var msg = document.getElementById("shareMessage");
msg.style.display = "none";
btn = document.getElementById("shareBtn");
btn.className = "btn";
}
function updateSpeed() {
hideIntro();
if (_speed > 1.0) _speed = 1.0;
// the first few percent of the speed bar is 0, to give a reasonable click region for it
var speedBarZeroArea = 0.07;
var slider = document.getElementById("slider");
var cSpeed = _speed;
if (cSpeed > 1) {
cSpeed = 1;
} else if (cSpeed <= speedBarZeroArea) {
cSpeed = speedBarZeroArea;
_speed = speedBarZeroArea - 0.0001;
slider.className = "slider sliderDisabled";
} else {
slider.className = "slider";
}
var sliderBar = document.getElementById("sliderBar");
sliderBar.style.height = ((1.0 - cSpeed) * 100.0) + "%";
var speed = (_speed - speedBarZeroArea);
kosmosSetSpeed(speed, _reverseMode);
}
function updateButtons() {
document.getElementById("reverse").className = (_reverseMode ? "btn lit" : "btn");
document.getElementById("autopilot").className = (_autopilotMode ? "btn lit" : "btn");
}
function toggleReverse() {
_reverseMode = !_reverseMode;
updateButtons();
updateSpeed();
}
function toggleAutopilot() {
_autopilotMode = !_autopilotMode;
updateButtons();
kosmosSetAutopilot(_autopilotMode);
}
function killSimulation() {
kosmosKill();
}