ctl_utils.js 18 KB

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