ctl_utils.js 17 KB

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