ctl_utils.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. var s_iScaleFactor = 1;
  2. var s_iOffsetX;
  3. var s_iOffsetY;
  4. var s_bIsIphone = false;
  5. var s_bFocus = true;
  6. /**
  7. * jQuery.browser.mobile (http://detectmobilebrowser.com/)
  8. * jQuery.browser.mobile will be true if the browser is a mobile device
  9. **/
  10. (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);
  11. $(window).resize(function() {
  12. sizeHandler();
  13. });
  14. function trace(szMsg){
  15. console.log(szMsg);
  16. }
  17. function isChrome(){
  18. var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
  19. return isChrome;
  20. }
  21. function isIpad() {
  22. var isIpad = navigator.userAgent.toLowerCase().indexOf('ipad') !== -1;
  23. if (!isIpad && navigator.userAgent.match(/Mac/) && navigator.maxTouchPoints && navigator.maxTouchPoints > 2) {
  24. return true;
  25. }
  26. return isIpad;
  27. }
  28. function isIpad() {
  29. var isIpad = navigator.userAgent.toLowerCase().indexOf('ipad') !== -1;
  30. if (!isIpad && navigator.userAgent.match(/Mac/) && navigator.maxTouchPoints && navigator.maxTouchPoints > 2) {
  31. return true;
  32. }
  33. return isIpad;
  34. }
  35. function isMobile(){
  36. if(isIpad()){
  37. return true;
  38. }
  39. 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)) {
  40. //MOBILE
  41. return true;
  42. }else{
  43. //DESKTOP
  44. return false;
  45. }
  46. };
  47. function isIOS() {
  48. if(isIpad()){
  49. return true;
  50. }
  51. var iDevices = [
  52. 'iPad Simulator',
  53. 'iPhone Simulator',
  54. 'iPod Simulator',
  55. 'iPad',
  56. 'iPhone',
  57. 'iPod'
  58. ];
  59. while (iDevices.length) {
  60. if (navigator.platform === iDevices.pop()){
  61. s_bIsIphone = true;
  62. return true;
  63. }
  64. }
  65. s_bIsIphone = false;
  66. return false;
  67. }
  68. function getSize(Name) {
  69. var size;
  70. var name = Name.toLowerCase();
  71. var document = window.document;
  72. var documentElement = document.documentElement;
  73. if (window["inner" + Name] === undefined) {
  74. // IE6 & IE7 don't have window.innerWidth or innerHeight
  75. size = documentElement["client" + Name];
  76. }
  77. else if (window["inner" + Name] != documentElement["client" + Name]) {
  78. // WebKit doesn't include scrollbars while calculating viewport size so we have to get fancy
  79. // Insert markup to test if a media query will match document.doumentElement["client" + Name]
  80. var bodyElement = document.createElement("body");
  81. bodyElement.id = "vpw-test-b";
  82. bodyElement.style.cssText = "overflow:scroll";
  83. var divElement = document.createElement("div");
  84. divElement.id = "vpw-test-d";
  85. divElement.style.cssText = "position:absolute;top:-1000px";
  86. // Getting specific on the CSS selector so it won't get overridden easily
  87. divElement.innerHTML = "<style>@media(" + name + ":" + documentElement["client" + Name] + "px){body#vpw-test-b div#vpw-test-d{" + name + ":7px!important}}</style>";
  88. bodyElement.appendChild(divElement);
  89. documentElement.insertBefore(bodyElement, document.head);
  90. if (divElement["offset" + Name] == 7) {
  91. // Media query matches document.documentElement["client" + Name]
  92. size = documentElement["client" + Name];
  93. }
  94. else {
  95. // Media query didn't match, use window["inner" + Name]
  96. size = window["inner" + Name];
  97. }
  98. // Cleanup
  99. documentElement.removeChild(bodyElement);
  100. }
  101. else {
  102. // Default to use window["inner" + Name]
  103. size = window["inner" + Name];
  104. }
  105. return size;
  106. };
  107. window.addEventListener("orientationchange", onOrientationChange );
  108. function onOrientationChange(){
  109. if (window.matchMedia("(orientation: portrait)").matches) {
  110. // you're in PORTRAIT mode
  111. sizeHandler();
  112. }
  113. if (window.matchMedia("(orientation: landscape)").matches) {
  114. // you're in LANDSCAPE mode
  115. sizeHandler();
  116. }
  117. }
  118. function getIOSWindowHeight() {
  119. // Get zoom level of mobile Safari
  120. // Note, that such zoom detection might not work correctly in other browsers
  121. // We use width, instead of height, because there are no vertical toolbars :)
  122. var zoomLevel = document.documentElement.clientWidth / window.innerWidth;
  123. // window.innerHeight returns height of the visible area.
  124. // We multiply it by zoom and get out real height.
  125. return window.innerHeight * zoomLevel;
  126. };
  127. // You can also get height of the toolbars that are currently displayed
  128. function getHeightOfIOSToolbars() {
  129. var tH = (window.orientation === 0 ? screen.height : screen.width) - getIOSWindowHeight();
  130. return tH > 1 ? tH : 0;
  131. };
  132. //THIS FUNCTION MANAGES THE CANVAS SCALING TO FIT PROPORTIONALLY THE GAME TO THE CURRENT DEVICE RESOLUTION
  133. function sizeHandler() {
  134. window.scrollTo(0, 1);
  135. if (!$("#canvas")){
  136. return;
  137. }
  138. var h;
  139. if(platform.name !== null && platform.name.toLowerCase() === "safari"){
  140. h = getIOSWindowHeight();
  141. }else{
  142. h = getSize("Height");
  143. }
  144. var w = getSize("Width");
  145. if(s_bFocus){
  146. _checkOrientation(w,h);
  147. }
  148. var multiplier = Math.min((h / CANVAS_HEIGHT), (w / CANVAS_WIDTH));
  149. var destW = Math.round(CANVAS_WIDTH * multiplier);
  150. var destH = Math.round(CANVAS_HEIGHT * multiplier);
  151. var iAdd = 0;
  152. if (destH < h){
  153. iAdd = h-destH;
  154. destH += iAdd;
  155. destW += iAdd*(CANVAS_WIDTH/CANVAS_HEIGHT);
  156. }else if (destW < w){
  157. iAdd = w-destW;
  158. destW += iAdd;
  159. destH += iAdd*(CANVAS_HEIGHT/CANVAS_WIDTH);
  160. }
  161. var fOffsetY = ((h / 2) - (destH / 2));
  162. var fOffsetX = ((w / 2) - (destW / 2));
  163. var fGameInverseScaling = (CANVAS_WIDTH/destW);
  164. if( fOffsetX*fGameInverseScaling < -EDGEBOARD_X ||
  165. fOffsetY*fGameInverseScaling < -EDGEBOARD_Y ){
  166. multiplier = Math.min( h / (CANVAS_HEIGHT-(EDGEBOARD_Y*2)), w / (CANVAS_WIDTH-(EDGEBOARD_X*2)));
  167. destW = Math.round(CANVAS_WIDTH * multiplier);
  168. destH = Math.round(CANVAS_HEIGHT * multiplier);
  169. fOffsetY = ( h - destH ) / 2;
  170. fOffsetX = ( w - destW ) / 2;
  171. fGameInverseScaling = (CANVAS_WIDTH/destW);
  172. }
  173. s_iOffsetX = (-1*fOffsetX * fGameInverseScaling);
  174. s_iOffsetY = (-1*fOffsetY * fGameInverseScaling);
  175. if(fOffsetY >= 0 ){
  176. s_iOffsetY = 0;
  177. }
  178. if(fOffsetX >= 0 ){
  179. s_iOffsetX = 0;
  180. }
  181. if(s_oInterface !== null){
  182. s_oInterface.refreshButtonPos( s_iOffsetX,s_iOffsetY);
  183. }
  184. if(s_oMenu !== null){
  185. s_oMenu.refreshButtonPos( s_iOffsetX,s_iOffsetY);
  186. }
  187. if(s_bIsIphone && s_oStage){
  188. canvas = document.getElementById('canvas');
  189. s_oStage.canvas.width = destW*2;
  190. s_oStage.canvas.height = destH*2;
  191. canvas.style.width = destW+"px";
  192. canvas.style.height = destH+"px";
  193. var iScale = Math.min(destW / CANVAS_WIDTH, destH / CANVAS_HEIGHT);
  194. s_oStage.scaleX = s_oStage.scaleY = iScale*2;
  195. }else if(s_bMobile || isChrome()){
  196. $("#canvas").css("width",destW+"px");
  197. $("#canvas").css("height",destH+"px");
  198. }else if(s_oStage){
  199. s_oStage.canvas.width = destW;
  200. s_oStage.canvas.height = destH;
  201. s_iScaleFactor = Math.min(destW / CANVAS_WIDTH, destH / CANVAS_HEIGHT);
  202. s_oStage.scaleX = s_oStage.scaleY = s_iScaleFactor;
  203. }
  204. if(fOffsetY < 0){
  205. $("#canvas").css("top",fOffsetY+"px");
  206. }else{
  207. // centered game
  208. fOffsetY = (h - destH)/2;
  209. $("#canvas").css("top",fOffsetY+"px");
  210. }
  211. $("#canvas").css("left",fOffsetX+"px");
  212. fullscreenHandler();
  213. };
  214. function _checkOrientation(iWidth,iHeight){
  215. if(s_bMobile && ENABLE_CHECK_ORIENTATION){
  216. if( iWidth>iHeight ){
  217. if( $(".orientation-msg-container").attr("data-orientation") === "landscape" ){
  218. $(".orientation-msg-container").css("display","none");
  219. s_oMain.startUpdate();
  220. }else{
  221. $(".orientation-msg-container").css("display","block");
  222. s_oMain.stopUpdate();
  223. }
  224. }else{
  225. if( $(".orientation-msg-container").attr("data-orientation") === "portrait" ){
  226. $(".orientation-msg-container").css("display","none");
  227. s_oMain.startUpdate();
  228. }else{
  229. $(".orientation-msg-container").css("display","block");
  230. s_oMain.stopUpdate();
  231. }
  232. }
  233. }
  234. }
  235. function createBitmap(oSprite, iWidth, iHeight){
  236. var oBmp = new createjs.Bitmap(oSprite);
  237. var hitObject = new createjs.Shape();
  238. if (iWidth && iHeight){
  239. hitObject .graphics.beginFill("#fff").drawRect(0, 0, iWidth, iHeight);
  240. }else{
  241. hitObject .graphics.beginFill("#ff0").drawRect(0, 0, oSprite.width, oSprite.height);
  242. }
  243. oBmp.hitArea = hitObject;
  244. return oBmp;
  245. }
  246. function createSprite(oSpriteSheet, szState, iRegX,iRegY,iWidth, iHeight){
  247. if(szState !== null){
  248. var oRetSprite = new createjs.Sprite(oSpriteSheet, szState);
  249. }else{
  250. var oRetSprite = new createjs.Sprite(oSpriteSheet);
  251. }
  252. var hitObject = new createjs.Shape();
  253. hitObject .graphics.beginFill("#000000").drawRect(-iRegX, -iRegY, iWidth, iHeight);
  254. oRetSprite.hitArea = hitObject;
  255. return oRetSprite;
  256. }
  257. function randomFloatBetween(minValue,maxValue,precision){
  258. if(typeof(precision) === 'undefined'){
  259. precision = 2;
  260. }
  261. return parseFloat(Math.min(minValue + (Math.random() * (maxValue - minValue)),maxValue).toFixed(precision));
  262. }
  263. function shuffle(array) {
  264. var currentIndex = array.length
  265. , temporaryValue
  266. , randomIndex
  267. ;
  268. // While there remain elements to shuffle...
  269. while (0 !== currentIndex) {
  270. // Pick a remaining element...
  271. randomIndex = Math.floor(Math.random() * currentIndex);
  272. currentIndex -= 1;
  273. // And swap it with the current element.
  274. temporaryValue = array[currentIndex];
  275. array[currentIndex] = array[randomIndex];
  276. array[randomIndex] = temporaryValue;
  277. }
  278. return array;
  279. }
  280. function bubbleSort(a)
  281. {
  282. var swapped;
  283. do {
  284. swapped = false;
  285. for (var i=0; i < a.length-1; i++) {
  286. if (a[i] > a[i+1]) {
  287. var temp = a[i];
  288. a[i] = a[i+1];
  289. a[i+1] = temp;
  290. swapped = true;
  291. }
  292. }
  293. } while (swapped);
  294. }
  295. function compare(a,b) {
  296. if (a.index > b.index)
  297. return -1;
  298. if (a.index < b.index)
  299. return 1;
  300. return 0;
  301. }
  302. function formatTime(iTime){
  303. iTime/=1000;
  304. var iMins = Math.floor(iTime/60);
  305. var iSecs = Math.floor(iTime-(iMins*60));
  306. //iSecs = parseFloat(iSecs).toFixed(1)
  307. var szRet = "";
  308. if ( iMins < 10 ){
  309. szRet += "0" + iMins + ":";
  310. }else{
  311. szRet += iMins + ":";
  312. }
  313. if ( iSecs < 10 ){
  314. szRet += "0" + iSecs;
  315. }else{
  316. szRet += iSecs;
  317. }
  318. return szRet;
  319. }
  320. function NoClickDelay(el) {
  321. this.element = el;
  322. if( window.Touch ) this.element.addEventListener('touchstart', this, false);
  323. }
  324. //Fisher-Yates Shuffle
  325. function shuffle(array) {
  326. var counter = array.length, temp, index;
  327. // While there are elements in the array
  328. while (counter > 0) {
  329. // Pick a random index
  330. index = Math.floor(Math.random() * counter);
  331. // Decrease counter by 1
  332. counter--;
  333. // And swap the last element with it
  334. temp = array[counter];
  335. array[counter] = array[index];
  336. array[index] = temp;
  337. }
  338. return array;
  339. }
  340. NoClickDelay.prototype = {
  341. handleEvent: function(e) {
  342. switch(e.type) {
  343. case 'touchstart': this.onTouchStart(e); break;
  344. case 'touchmove': this.onTouchMove(e); break;
  345. case 'touchend': this.onTouchEnd(e); break;
  346. }
  347. },
  348. onTouchStart: function(e) {
  349. e.preventDefault();
  350. this.moved = false;
  351. this.element.addEventListener('touchmove', this, false);
  352. this.element.addEventListener('touchend', this, false);
  353. },
  354. onTouchMove: function(e) {
  355. this.moved = true;
  356. },
  357. onTouchEnd: function(e) {
  358. this.element.removeEventListener('touchmove', this, false);
  359. this.element.removeEventListener('touchend', this, false);
  360. if( !this.moved ) {
  361. var theTarget = document.elementFromPoint(e.changedTouches[0].clientX, e.changedTouches[0].clientY);
  362. if(theTarget.nodeType == 3) theTarget = theTarget.parentNode;
  363. var theEvent = document.createEvent('MouseEvents');
  364. theEvent.initEvent('click', true, true);
  365. theTarget.dispatchEvent(theEvent);
  366. }
  367. }
  368. };
  369. (function() {
  370. var hidden = "hidden";
  371. // Standards:
  372. if (hidden in document)
  373. document.addEventListener("visibilitychange", onchange);
  374. else if ((hidden = "mozHidden") in document)
  375. document.addEventListener("mozvisibilitychange", onchange);
  376. else if ((hidden = "webkitHidden") in document)
  377. document.addEventListener("webkitvisibilitychange", onchange);
  378. else if ((hidden = "msHidden") in document)
  379. document.addEventListener("msvisibilitychange", onchange);
  380. // IE 9 and lower:
  381. else if ('onfocusin' in document)
  382. document.onfocusin = document.onfocusout = onchange;
  383. // All others:
  384. else
  385. window.onpageshow = window.onpagehide
  386. = window.onfocus = window.onblur = onchange;
  387. function onchange (evt) {
  388. var v = 'visible', h = 'hidden',
  389. evtMap = {
  390. focus:v, focusin:v, pageshow:v, blur:h, focusout:h, pagehide:h
  391. };
  392. evt = evt || window.event;
  393. if (evt.type in evtMap){
  394. document.body.className = evtMap[evt.type];
  395. }else{
  396. document.body.className = this[hidden] ? "hidden" : "visible";
  397. if(document.body.className === "hidden"){
  398. s_oMain.stopUpdate();
  399. s_bFocus = false;
  400. }else{
  401. s_oMain.startUpdate();
  402. s_bFocus = true;
  403. }
  404. }
  405. }
  406. })();
  407. function playSound(szSound,iVolume,bLoop){
  408. if(DISABLE_SOUND_MOBILE === false || s_bMobile === false){
  409. s_aSounds[szSound].play();
  410. s_aSounds[szSound].volume(iVolume);
  411. s_aSounds[szSound].loop(bLoop);
  412. return s_aSounds[szSound];
  413. }
  414. return null;
  415. }
  416. function stopSound(szSound){
  417. if(DISABLE_SOUND_MOBILE === false || s_bMobile === false){
  418. s_aSounds[szSound].stop();
  419. }
  420. }
  421. function setVolume(szSound, iVolume){
  422. if(DISABLE_SOUND_MOBILE === false || s_bMobile === false){
  423. s_aSounds[szSound].volume(iVolume);
  424. }
  425. }
  426. function setMute(szSound, bMute){
  427. if(DISABLE_SOUND_MOBILE === false || s_bMobile === false){
  428. s_aSounds[szSound].mute(bMute);
  429. }
  430. }
  431. function ctlArcadeResume(){
  432. if(s_oMain !== null){
  433. s_oMain.startUpdate();
  434. }
  435. }
  436. function ctlArcadePause(){
  437. if(s_oMain !== null){
  438. s_oMain.stopUpdate();
  439. }
  440. }
  441. function getParamValue(paramName){
  442. var url = window.location.search.substring(1);
  443. var qArray = url.split('&');
  444. for (var i = 0; i < qArray.length; i++)
  445. {
  446. var pArr = qArray[i].split('=');
  447. if (pArr[0] == paramName)
  448. return pArr[1];
  449. }
  450. }
  451. function fullscreenHandler(){
  452. if (!ENABLE_FULLSCREEN || !screenfull.isEnabled){
  453. return;
  454. }
  455. s_bFullscreen = screenfull.isFullscreen;
  456. if (s_oInterface !== null){
  457. s_oInterface.resetFullscreenBut();
  458. }
  459. if (s_oMenu !== null){
  460. s_oMenu.resetFullscreenBut();
  461. }
  462. }
  463. if (screenfull.isEnabled) {
  464. screenfull.on('change', function(){
  465. s_bFullscreen = screenfull.isFullscreen;
  466. if (s_oInterface !== null){
  467. s_oInterface.resetFullscreenBut(); }
  468. if (s_oMenu !== null){
  469. s_oMenu.resetFullscreenBut();}
  470. });
  471. }