ctl_utils.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. var s_iScaleFactor = 1;
  2. var s_bIsIphone = false;
  3. var s_iOffsetX;
  4. var s_iOffsetY;
  5. var s_fInverseScaling = 0;
  6. var s_fScaleCanvas = 0;
  7. /**
  8. * jQuery.browser.mobile (http://detectmobilebrowser.com/)
  9. * jQuery.browser.mobile will be true if the browser is a mobile device
  10. **/
  11. (function(a){(jQuery.browser = jQuery.browser || {}).mobile = /android|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(ad|hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|symbian|tablet|treo|up\.(browser|link)|vodafone|wap|webos|windows (ce|phone)|xda|xiino/i.test(a) || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|e\-|e\/|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(di|rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|xda(\-|2|g)|yas\-|your|zeto|zte\-/i.test(a.substr(0, 4))})(navigator.userAgent || navigator.vendor || window.opera);
  12. $(window).resize(function() {
  13. sizeHandler();
  14. });
  15. function trace(szMsg){
  16. console.log(szMsg);
  17. }
  18. function isChrome(){
  19. var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
  20. return isChrome;
  21. }
  22. function isIOS() {
  23. var iDevices = [
  24. 'iPad Simulator',
  25. 'iPhone Simulator',
  26. 'iPod Simulator',
  27. 'iPad',
  28. 'iPhone',
  29. 'iPod'
  30. ];
  31. if (navigator.userAgent.toLowerCase().indexOf("iphone") !== -1){
  32. s_bIsIphone = true;
  33. }
  34. while (iDevices.length) {
  35. if (navigator.platform === iDevices.pop()){
  36. return true;
  37. }
  38. }
  39. s_bIsIphone = false;
  40. return false;
  41. }
  42. window.addEventListener("orientationchange", onOrientationChange);
  43. function onOrientationChange(){
  44. if (window.matchMedia("(orientation: portrait)").matches) {
  45. // you're in PORTRAIT mode
  46. sizeHandler();
  47. }
  48. if (window.matchMedia("(orientation: landscape)").matches) {
  49. // you're in LANDSCAPE mode
  50. sizeHandler();
  51. }
  52. }
  53. function getSize(Name) {
  54. var size;
  55. var name = Name.toLowerCase();
  56. var document = window.document;
  57. var documentElement = document.documentElement;
  58. if (window["inner" + Name] === undefined) {
  59. // IE6 & IE7 don't have window.innerWidth or innerHeight
  60. size = documentElement["client" + Name];
  61. }
  62. else if (window["inner" + Name] != documentElement["client" + Name]) {
  63. // WebKit doesn't include scrollbars while calculating viewport size so we have to get fancy
  64. // Insert markup to test if a media query will match document.doumentElement["client" + Name]
  65. var bodyElement = document.createElement("body");
  66. bodyElement.id = "vpw-test-b";
  67. bodyElement.style.cssText = "overflow:scroll";
  68. var divElement = document.createElement("div");
  69. divElement.id = "vpw-test-d";
  70. divElement.style.cssText = "position:absolute;top:-1000px";
  71. // Getting specific on the CSS selector so it won't get overridden easily
  72. divElement.innerHTML = "<style>@media(" + name + ":" + documentElement["client" + Name] + "px){body#vpw-test-b div#vpw-test-d{" + name + ":7px!important}}</style>";
  73. bodyElement.appendChild(divElement);
  74. documentElement.insertBefore(bodyElement, document.head);
  75. if (divElement["offset" + Name] == 7) {
  76. // Media query matches document.documentElement["client" + Name]
  77. size = documentElement["client" + Name];
  78. }
  79. else {
  80. // Media query didn't match, use window["inner" + Name]
  81. size = window["inner" + Name];
  82. }
  83. // Cleanup
  84. documentElement.removeChild(bodyElement);
  85. }
  86. else {
  87. // Default to use window["inner" + Name]
  88. size = window["inner" + Name];
  89. }
  90. return size;
  91. };
  92. function getIOSWindowHeight() {
  93. // Get zoom level of mobile Safari
  94. // Note, that such zoom detection might not work correctly in other browsers
  95. // We use width, instead of height, because there are no vertical toolbars :)
  96. var zoomLevel = document.documentElement.clientWidth / window.innerWidth;
  97. // window.innerHeight returns height of the visible area.
  98. // We multiply it by zoom and get out real height.
  99. return window.innerHeight * zoomLevel;
  100. };
  101. // You can also get height of the toolbars that are currently displayed
  102. function getHeightOfIOSToolbars() {
  103. var tH = (window.orientation === 0 ? screen.height : screen.width) - getIOSWindowHeight();
  104. return tH > 1 ? tH : 0;
  105. };
  106. //THIS FUNCTION MANAGES THE CANVAS SCALING TO FIT PROPORTIONALLY THE GAME TO THE CURRENT DEVICE RESOLUTION
  107. function sizeHandler() {
  108. window.scrollTo(0, 1);
  109. if (!$("#canvas")){
  110. return;
  111. }
  112. var h;
  113. var iOS = (navigator.userAgent.match(/(iPad|iPhone|iPod)/g) ? true : false);
  114. if (iOS){
  115. h = getIOSWindowHeight();
  116. } else{
  117. h = getSize("Height");
  118. }
  119. var w = getSize("Width");
  120. _checkOrientation(w,h);
  121. s_fScaleCanvas = Math.min((h / CANVAS_HEIGHT), (w / CANVAS_WIDTH))
  122. s_iScaleFactor = s_fScaleCanvas;
  123. var destW = CANVAS_WIDTH * s_iScaleFactor;
  124. var destH = CANVAS_HEIGHT * s_iScaleFactor;
  125. var iAdd = 0;
  126. if (destH < h){
  127. iAdd = h - destH;
  128. destH += iAdd;
  129. destW += iAdd * (CANVAS_WIDTH / CANVAS_HEIGHT);
  130. } else if (destW < w){
  131. iAdd = w - destW;
  132. destW += iAdd;
  133. destH += iAdd * (CANVAS_HEIGHT / CANVAS_WIDTH);
  134. }
  135. var fOffsetY = ((h / 2) - (destH / 2));
  136. var fOffsetX = ((w / 2) - (destW / 2));
  137. var fGameInverseScaling = (CANVAS_WIDTH / destW);
  138. if (fOffsetX * fGameInverseScaling < - EDGEBOARD_X || fOffsetY * fGameInverseScaling < - EDGEBOARD_Y){
  139. s_iScaleFactor = Math.min(h / (CANVAS_HEIGHT - (EDGEBOARD_Y * 2)), w / (CANVAS_WIDTH - (EDGEBOARD_X * 2)));
  140. destW = CANVAS_WIDTH * s_iScaleFactor;
  141. destH = CANVAS_HEIGHT * s_iScaleFactor;
  142. fOffsetY = (h - destH) / 2;
  143. fOffsetX = (w - destW) / 2;
  144. fGameInverseScaling = (CANVAS_WIDTH / destW);
  145. }
  146. s_fInverseScaling = fGameInverseScaling;
  147. s_iOffsetX = ( - 1 * fOffsetX * fGameInverseScaling);
  148. s_iOffsetY = ( - 1 * fOffsetY * fGameInverseScaling);
  149. if (fOffsetY >= 0){
  150. s_iOffsetY = 0;
  151. }
  152. if (fOffsetX >= 0){
  153. s_iOffsetX = 0;
  154. }
  155. if (s_oInterface !== null){
  156. s_oInterface.refreshButtonPos(s_iOffsetX, s_iOffsetY);
  157. }
  158. if (s_oMenu !== null){
  159. s_oMenu.refreshButtonPos(s_iOffsetX, s_iOffsetY);
  160. }
  161. if(s_bIsIphone){
  162. canvas = document.getElementById('canvas');
  163. s_oStage.canvas.width = destW*2;
  164. s_oStage.canvas.height = destH*2;
  165. canvas.style.width = destW+"px";
  166. canvas.style.height = destH+"px";
  167. var iScale = Math.min(destW / CANVAS_WIDTH, destH / CANVAS_HEIGHT);
  168. s_oStage.scaleX = s_oStage.scaleY = iScale*2;
  169. }else if(s_bMobile || isChrome()){
  170. $("#canvas").css("width",destW+"px");
  171. $("#canvas").css("height",destH+"px");
  172. }else{
  173. s_oStage.canvas.width = destW;
  174. s_oStage.canvas.height = destH;
  175. s_iScaleFactor = Math.min(destW / CANVAS_WIDTH, destH / CANVAS_HEIGHT);
  176. s_oStage.scaleX = s_oStage.scaleY = s_iScaleFactor;
  177. $("#canvas").css("width",destW+"px");
  178. $("#canvas").css("height",destH+"px");
  179. }
  180. if (fOffsetY < 0){
  181. $("#canvas").css("top", fOffsetY + "px");
  182. s_iCanvasOffsetHeight = fOffsetY;
  183. } else{
  184. fOffsetY = (h - destH)/2;
  185. $("#canvas").css("top",fOffsetY+"px");
  186. //$("#canvas").css("top", "0px");
  187. s_iCanvasOffsetHeight = 0;
  188. }
  189. $("#canvas").css("left", fOffsetX + "px");
  190. resizeCanvas3D();
  191. s_iCanvasResizeWidth = destW;
  192. s_iCanvasResizeHeight = destH;
  193. s_iCanvasOffsetWidth = fOffsetX;
  194. fullscreenHandler();
  195. };
  196. function _checkOrientation(iWidth,iHeight){
  197. if(s_bMobile && ENABLE_CHECK_ORIENTATION){
  198. if( iWidth>iHeight ){
  199. if( $(".orientation-msg-container").attr("data-orientation") === "landscape" ){
  200. $(".orientation-msg-container").css("display","none");
  201. s_oMain.startUpdate();
  202. }else{
  203. $(".orientation-msg-container").css("display","block");
  204. s_oMain.stopUpdate();
  205. }
  206. }else{
  207. if( $(".orientation-msg-container").attr("data-orientation") === "portrait" ){
  208. $(".orientation-msg-container").css("display","none");
  209. s_oMain.startUpdate();
  210. }else{
  211. $(".orientation-msg-container").css("display","block");
  212. s_oMain.stopUpdate();
  213. }
  214. }
  215. }
  216. }
  217. function createBitmap(oSprite, iWidthHitArea, iHeightHitArea){
  218. var oBmp = new createjs.Bitmap(oSprite);
  219. var hitObject = new createjs.Shape();
  220. if (iWidthHitArea && iHeightHitArea){
  221. hitObject.graphics.beginFill("#fff").drawRect( - iWidthHitArea / 2, - iHeightHitArea / 2, iWidthHitArea, iHeightHitArea);
  222. } else{
  223. hitObject.graphics.beginFill("#ff0").drawRect(0, 0, oSprite.width, oSprite.height);
  224. }
  225. oBmp.hitArea = hitObject;
  226. return oBmp;
  227. }
  228. function createSprite(oSpriteSheet, szState, iRegX, iRegY, iWidth, iHeight){
  229. if (szState !== null){
  230. var oRetSprite = new createjs.Sprite(oSpriteSheet, szState);
  231. } else{
  232. var oRetSprite = new createjs.Sprite(oSpriteSheet);
  233. }
  234. var hitObject = new createjs.Shape();
  235. hitObject.graphics.beginFill("#000000").drawRect( - iRegX, - iRegY, iWidth, iHeight);
  236. oRetSprite.hitArea = hitObject;
  237. return oRetSprite;
  238. }
  239. function randomFloatBetween(minValue, maxValue, precision){
  240. if (typeof (precision) === 'undefined'){
  241. precision = 2;
  242. }
  243. return parseFloat(Math.min(minValue + (Math.random() * (maxValue - minValue)), maxValue).toFixed(precision));
  244. }
  245. function shuffle(array) {
  246. var currentIndex = array.length
  247. , temporaryValue
  248. , randomIndex
  249. ;
  250. // While there remain elements to shuffle...
  251. while (0 !== currentIndex) {
  252. // Pick a remaining element...
  253. randomIndex = Math.floor(Math.random() * currentIndex);
  254. currentIndex -= 1;
  255. // And swap it with the current element.
  256. temporaryValue = array[currentIndex];
  257. array[currentIndex] = array[randomIndex];
  258. array[randomIndex] = temporaryValue;
  259. }
  260. return array;
  261. }
  262. function formatTime(iTime){
  263. iTime /= 1000;
  264. var iMins = Math.floor(iTime / 60);
  265. var iSecs = iTime - (iMins * 60);
  266. iSecs = parseFloat(iSecs).toFixed(1)
  267. var szRet = "";
  268. if (iMins < 10){
  269. szRet += "0" + iMins + ":";
  270. } else{
  271. szRet += iMins + ":";
  272. }
  273. if (iSecs < 10){
  274. szRet += "0" + iSecs;
  275. } else{
  276. szRet += iSecs;
  277. }
  278. return szRet;
  279. }
  280. function degreesToRadians(iAngle){
  281. return iAngle * Math.PI / 180;
  282. }
  283. function checkRectCollision(bitmap1, bitmap2) {
  284. var b1, b2;
  285. b1 = getBounds(bitmap1, 0.9);
  286. b2 = getBounds(bitmap2, 0.98);
  287. return calculateIntersection(b1, b2);
  288. }
  289. function calculateIntersection(rect1, rect2){
  290. // first we have to calculate the
  291. // center of each rectangle and half of
  292. // width and height
  293. var dx, dy, r1 = {}, r2 = {};
  294. r1.cx = rect1.x + (r1.hw = (rect1.width / 2));
  295. r1.cy = rect1.y + (r1.hh = (rect1.height / 2));
  296. r2.cx = rect2.x + (r2.hw = (rect2.width / 2));
  297. r2.cy = rect2.y + (r2.hh = (rect2.height / 2));
  298. dx = Math.abs(r1.cx - r2.cx) - (r1.hw + r2.hw);
  299. dy = Math.abs(r1.cy - r2.cy) - (r1.hh + r2.hh);
  300. if (dx < 0 && dy < 0) {
  301. dx = Math.min(Math.min(rect1.width, rect2.width), - dx);
  302. dy = Math.min(Math.min(rect1.height, rect2.height), - dy);
  303. return {x:Math.max(rect1.x, rect2.x),
  304. y:Math.max(rect1.y, rect2.y),
  305. width:dx,
  306. height:dy,
  307. rect1: rect1,
  308. rect2: rect2};
  309. } else {
  310. return null;
  311. }
  312. }
  313. function getBounds(obj, iTolerance) {
  314. var bounds = {x:Infinity, y:Infinity, width:0, height:0};
  315. if (obj instanceof createjs.Container) {
  316. bounds.x2 = - Infinity;
  317. bounds.y2 = - Infinity;
  318. var children = obj.children, l = children.length, cbounds, c;
  319. for (c = 0; c < l; c++) {
  320. cbounds = getBounds(children[c], 1);
  321. if (cbounds.x < bounds.x) bounds.x = cbounds.x;
  322. if (cbounds.y < bounds.y) bounds.y = cbounds.y;
  323. if (cbounds.x + cbounds.width > bounds.x2) bounds.x2 = cbounds.x + cbounds.width;
  324. if (cbounds.y + cbounds.height > bounds.y2) bounds.y2 = cbounds.y + cbounds.height;
  325. //if ( cbounds.x - bounds.x + cbounds.width > bounds.width ) bounds.width = cbounds.x - bounds.x + cbounds.width;
  326. //if ( cbounds.y - bounds.y + cbounds.height > bounds.height ) bounds.height = cbounds.y - bounds.y + cbounds.height;
  327. }
  328. if (bounds.x == Infinity) bounds.x = 0;
  329. if (bounds.y == Infinity) bounds.y = 0;
  330. if (bounds.x2 == Infinity) bounds.x2 = 0;
  331. if (bounds.y2 == Infinity) bounds.y2 = 0;
  332. bounds.width = bounds.x2 - bounds.x;
  333. bounds.height = bounds.y2 - bounds.y;
  334. delete bounds.x2;
  335. delete bounds.y2;
  336. } else {
  337. var gp, gp2, gp3, gp4, imgr = {}, sr;
  338. if (obj instanceof createjs.Bitmap) {
  339. sr = obj.sourceRect || obj.image;
  340. imgr.width = sr.width * iTolerance;
  341. imgr.height = sr.height * iTolerance;
  342. } else if (obj instanceof createjs.Sprite) {
  343. if (obj.spriteSheet._frames && obj.spriteSheet._frames[obj.currentFrame] && obj.spriteSheet._frames[obj.currentFrame].image) {
  344. var cframe = obj.spriteSheet.getFrame(obj.currentFrame);
  345. imgr.width = cframe.rect.width;
  346. imgr.height = cframe.rect.height;
  347. imgr.regX = cframe.regX;
  348. imgr.regY = cframe.regY;
  349. } else {
  350. bounds.x = obj.x || 0;
  351. bounds.y = obj.y || 0;
  352. }
  353. } else {
  354. bounds.x = obj.x || 0;
  355. bounds.y = obj.y || 0;
  356. }
  357. imgr.regX = imgr.regX || 0; imgr.width = imgr.width || 0;
  358. imgr.regY = imgr.regY || 0; imgr.height = imgr.height || 0;
  359. bounds.regX = imgr.regX;
  360. bounds.regY = imgr.regY;
  361. gp = obj.localToGlobal(0 - imgr.regX, 0 - imgr.regY);
  362. gp2 = obj.localToGlobal(imgr.width - imgr.regX, imgr.height - imgr.regY);
  363. gp3 = obj.localToGlobal(imgr.width - imgr.regX, 0 - imgr.regY);
  364. gp4 = obj.localToGlobal(0 - imgr.regX, imgr.height - imgr.regY);
  365. bounds.x = Math.min(Math.min(Math.min(gp.x, gp2.x), gp3.x), gp4.x);
  366. bounds.y = Math.min(Math.min(Math.min(gp.y, gp2.y), gp3.y), gp4.y);
  367. bounds.width = Math.max(Math.max(Math.max(gp.x, gp2.x), gp3.x), gp4.x) - bounds.x;
  368. bounds.height = Math.max(Math.max(Math.max(gp.y, gp2.y), gp3.y), gp4.y) - bounds.y;
  369. }
  370. return bounds;
  371. }
  372. function NoClickDelay(el) {
  373. this.element = el;
  374. if (window.Touch) this.element.addEventListener('touchstart', this, false);
  375. }
  376. NoClickDelay.prototype = {
  377. handleEvent: function(e) {
  378. switch (e.type) {
  379. case 'touchstart': this.onTouchStart(e); break;
  380. case 'touchmove': this.onTouchMove(e); break;
  381. case 'touchend': this.onTouchEnd(e); break;
  382. }
  383. },
  384. onTouchStart: function(e) {
  385. e.preventDefault();
  386. this.moved = false;
  387. this.element.addEventListener('touchmove', this, false);
  388. this.element.addEventListener('touchend', this, false);
  389. },
  390. onTouchMove: function(e) {
  391. this.moved = true;
  392. },
  393. onTouchEnd: function(e) {
  394. this.element.removeEventListener('touchmove', this, false);
  395. this.element.removeEventListener('touchend', this, false);
  396. if (!this.moved) {
  397. var theTarget = document.elementFromPoint(e.changedTouches[0].clientX, e.changedTouches[0].clientY);
  398. if (theTarget.nodeType == 3) theTarget = theTarget.parentNode;
  399. var theEvent = document.createEvent('MouseEvents');
  400. theEvent.initEvent('click', true, true);
  401. theTarget.dispatchEvent(theEvent);
  402. }
  403. }
  404. };
  405. (function() {
  406. var hidden = "hidden";
  407. // Standards:
  408. if (hidden in document)
  409. document.addEventListener("visibilitychange", onchange);
  410. else if ((hidden = "mozHidden") in document)
  411. document.addEventListener("mozvisibilitychange", onchange);
  412. else if ((hidden = "webkitHidden") in document)
  413. document.addEventListener("webkitvisibilitychange", onchange);
  414. else if ((hidden = "msHidden") in document)
  415. document.addEventListener("msvisibilitychange", onchange);
  416. // IE 9 and lower:
  417. else if ('onfocusin' in document)
  418. document.onfocusin = document.onfocusout = onchange;
  419. // All others:
  420. else
  421. window.onpageshow = window.onpagehide
  422. = window.onfocus = window.onblur = onchange;
  423. function onchange (evt) {
  424. var v = 'visible', h = 'hidden',
  425. evtMap = {
  426. focus:v, focusin:v, pageshow:v, blur:h, focusout:h, pagehide:h
  427. };
  428. evt = evt || window.event;
  429. if (evt.type in evtMap){
  430. document.body.className = evtMap[evt.type];
  431. } else{
  432. document.body.className = this[hidden] ? "hidden" : "visible";
  433. if (document.body.className === "hidden"){
  434. s_oMain.stopUpdate();
  435. } else{
  436. s_oMain.startUpdate();
  437. }
  438. }
  439. }
  440. })();
  441. function playSound(szSound,iVolume,bLoop){
  442. if(DISABLE_SOUND_MOBILE === false || s_bMobile === false){
  443. s_aSounds[szSound].play();
  444. s_aSounds[szSound].volume(iVolume);
  445. s_aSounds[szSound].loop(bLoop);
  446. return s_aSounds[szSound];
  447. }
  448. return null;
  449. }
  450. function stopSound(szSound){
  451. if(DISABLE_SOUND_MOBILE === false || s_bMobile === false){
  452. s_aSounds[szSound].stop();
  453. }
  454. }
  455. function setVolume(szSound, iVolume){
  456. if(DISABLE_SOUND_MOBILE === false || s_bMobile === false){
  457. s_aSounds[szSound].volume(iVolume);
  458. }
  459. }
  460. function setMute(szSound, bMute){
  461. if(DISABLE_SOUND_MOBILE === false || s_bMobile === false){
  462. s_aSounds[szSound].mute(bMute);
  463. }
  464. }
  465. function fadeSound(iStart,iEnd,iDuration,szSound){
  466. if(DISABLE_SOUND_MOBILE === false || s_bMobile === false){
  467. s_aSounds[szSound].fade(iStart,iEnd,iDuration);
  468. }
  469. }
  470. function ctlArcadeResume(){
  471. if (s_oMain !== null){
  472. s_oMain.startUpdate();
  473. }
  474. }
  475. function ctlArcadePause(){
  476. if (s_oMain !== null){
  477. s_oMain.stopUpdate();
  478. }
  479. }
  480. function getParamValue(paramName){
  481. var url = window.location.search.substring(1);
  482. var qArray = url.split('&');
  483. for (var i = 0; i < qArray.length; i++)
  484. {
  485. var pArr = qArray[i].split('=');
  486. if (pArr[0] == paramName)
  487. return pArr[1];
  488. }
  489. }
  490. function rotateVector2D(iAngle, o) {
  491. var iX = o.x * Math.cos(iAngle) + o.y * Math.sin(iAngle);
  492. var iY = o.x * ( - Math.sin(iAngle)) + o.y * Math.cos(iAngle);
  493. return {x : iX, y : iY};
  494. }
  495. function normalize(o, len){
  496. if (len > 0){
  497. o.x /= len; o.y /= len;
  498. }
  499. return o
  500. };
  501. function findNearestIntersectingObject(clientX, clientY, camera, objects) {
  502. // Get the picking ray from the point
  503. //var projector = new THREE.Projector();
  504. var SCREEN_WIDTH = CANVAS_RESIZE_WIDTH + OFFSET_WIDTH * 2;
  505. var SCREEN_HEIGHT = CANVAS_RESIZE_HEIGHT + OFFSET_HEIGHT * 2;
  506. var raycaster = new THREE.Raycaster();
  507. var mouse3D = new THREE.Vector3();
  508. // Get 3D point form the client x y
  509. mouse3D.x = (clientX / SCREEN_WIDTH) * 2 - 1;
  510. mouse3D.y = - (clientY / SCREEN_HEIGHT) * 2 + 1;
  511. mouse3D.z = 0.5;
  512. raycaster.setFromCamera(mouse3D, camera);
  513. var hits = raycaster.intersectObjects(objects, true);
  514. // Find the closest intersecting object
  515. // Now, cast the ray all render objects in the scene to see if they collide. Take the closest one.
  516. //var hits = raycaster.intersectObjects(objects,true);
  517. // console.log(hits);
  518. var closest = false;
  519. if (hits.length > 0) {
  520. closest = hits[0];
  521. }
  522. return closest;
  523. }
  524. function distance(x1, y1, x2, y2){
  525. var iDx = x1 - x2;
  526. var iDy = y1 - y2;
  527. return Math.sqrt((iDx * iDx) + (iDy * iDy));
  528. }
  529. function distance2(x1, y1, x2, y2){
  530. var iDx = x1 - x2;
  531. var iDy = y1 - y2;
  532. return ((iDx * iDx) + (iDy * iDy));
  533. }
  534. function resizeCanvas3D(){
  535. $("canvas").each(function(){
  536. if ($(this).attr("id") == "#canvas"){
  537. return;
  538. }
  539. $(this).css("width", $("#canvas").css("width"));
  540. $(this).css("height", $("#canvas").css("height"));
  541. $(this).css("position", $("#canvas").css("position"));
  542. $(this).css("left", $("#canvas").css("left"));
  543. $(this).css("top", $("#canvas").css("top"));
  544. });
  545. }
  546. function createOrthoGraphicCamera(){
  547. var oCamera = new THREE.PerspectiveCamera(FOV, CANVAS_WIDTH / CANVAS_HEIGHT, NEAR, FAR);
  548. // oCamera.lookAt(new THREE.Vector3(OFFSET_TRACK_POSITION.x, OFFSET_TRACK_POSITION.y, OFFSET_TRACK_POSITION.z));
  549. oCamera.rotation.x = CAMERA_PROPERTIES.rot_x * (Math.PI / 180);
  550. oCamera.rotation.y = CAMERA_PROPERTIES.rot_y * (Math.PI / 180);
  551. oCamera.rotation.z = CAMERA_PROPERTIES.rot_z * (Math.PI / 180);
  552. //oCamera.zoom = 6;//4.0;
  553. // oCamera.left = CANVAS_WIDTH / -2;
  554. // oCamera.right = CANVAS_WIDTH / 2;
  555. // oCamera.top = CANVAS_HEIGHT / 2;
  556. // oCamera.bottom = CANVAS_HEIGHT / -2;
  557. oCamera.position.set(CAMERA_PROPERTIES.x, CAMERA_PROPERTIES.y, CAMERA_PROPERTIES.z);
  558. oCamera.updateProjectionMatrix();
  559. oCamera.updateMatrixWorld();
  560. return oCamera;
  561. }
  562. function rotateVector2D(iAngle, o) {
  563. var iX = o.x * Math.cos(iAngle) + o.y * Math.sin(iAngle);
  564. var iY = o.x * ( - Math.sin(iAngle)) + o.y * Math.cos(iAngle);
  565. return {x : iX, y : iY, z : 0};
  566. }
  567. Math.radians = function(degrees) {
  568. return degrees * Math.PI / 180;
  569. };
  570. // Converts from radians to degrees.
  571. Math.degrees = function(radians) {
  572. return radians * (180 / Math.PI);
  573. };
  574. function distanceV3 (x1, y1, z1, x2, y2, z2){
  575. var iDx = x1 - x2;
  576. var iDy = y1 - y2;
  577. var iDz = z1 - z2;
  578. return Math.sqrt((iDx * iDx) + (iDy * iDy) + (iDz * iDz));
  579. }
  580. function distanceV2 (v1, v2){
  581. var iDx = v1.x - v2.x;
  582. var iDy = v1.y - v2.y;
  583. return Math.sqrt((iDx * iDx) + (iDy * iDy));
  584. }
  585. function saveItem(szItem, oValue){
  586. localStorage.setItem(szItem, oValue);
  587. }
  588. function getItem(szItem){
  589. return localStorage.getItem(szItem);
  590. }
  591. function clearAllItem(){
  592. localStorage.clear();
  593. }
  594. function fullscreenHandler(){
  595. if (!ENABLE_FULLSCREEN || !screenfull.enabled){
  596. return;
  597. }
  598. s_bFullscreen = screenfull.isFullscreen;
  599. if (s_oInterface !== null){
  600. s_oInterface.resetFullscreenBut();
  601. }
  602. if (s_oMenu !== null){
  603. s_oMenu.resetFullscreenBut();
  604. }
  605. }
  606. if (screenfull.enabled) {
  607. screenfull.on('change', function(){
  608. s_bFullscreen = screenfull.isFullscreen;
  609. if (s_oInterface !== null){
  610. s_oInterface.resetFullscreenBut();
  611. }
  612. if (s_oMenu !== null){
  613. s_oMenu.resetFullscreenBut();
  614. }
  615. });
  616. }