ctl_utils.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 isChrome(){
  17. var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
  18. return isChrome;
  19. }
  20. function isIOS() {
  21. var iDevices = [
  22. 'iPad Simulator',
  23. 'iPhone Simulator',
  24. 'iPod Simulator',
  25. 'iPad',
  26. 'iPhone',
  27. 'iPod'
  28. ];
  29. if (navigator.userAgent.toLowerCase().indexOf("iphone") !== -1){
  30. s_bIsIphone = true;
  31. }
  32. while (iDevices.length) {
  33. if (navigator.platform === iDevices.pop()){
  34. return true;
  35. }
  36. }
  37. s_bIsIphone = false;
  38. return false;
  39. }
  40. window.addEventListener("orientationchange", onOrientationChange );
  41. function onOrientationChange(){
  42. if (window.matchMedia("(orientation: portrait)").matches) {
  43. // you're in PORTRAIT mode
  44. sizeHandler();
  45. }
  46. if (window.matchMedia("(orientation: landscape)").matches) {
  47. // you're in LANDSCAPE mode
  48. sizeHandler();
  49. }
  50. }
  51. function getSize(Name) {
  52. var size;
  53. var name = Name.toLowerCase();
  54. var document = window.document;
  55. var documentElement = document.documentElement;
  56. if (window["inner" + Name] === undefined) {
  57. // IE6 & IE7 don't have window.innerWidth or innerHeight
  58. size = documentElement["client" + Name];
  59. }
  60. else if (window["inner" + Name] != documentElement["client" + Name]) {
  61. // WebKit doesn't include scrollbars while calculating viewport size so we have to get fancy
  62. // Insert markup to test if a media query will match document.doumentElement["client" + Name]
  63. var bodyElement = document.createElement("body");
  64. bodyElement.id = "vpw-test-b";
  65. bodyElement.style.cssText = "overflow:scroll";
  66. var divElement = document.createElement("div");
  67. divElement.id = "vpw-test-d";
  68. divElement.style.cssText = "position:absolute;top:-1000px";
  69. // Getting specific on the CSS selector so it won't get overridden easily
  70. divElement.innerHTML = "<style>@media(" + name + ":" + documentElement["client" + Name] + "px){body#vpw-test-b div#vpw-test-d{" + name + ":7px!important}}</style>";
  71. bodyElement.appendChild(divElement);
  72. documentElement.insertBefore(bodyElement, document.head);
  73. if (divElement["offset" + Name] == 7) {
  74. // Media query matches document.documentElement["client" + Name]
  75. size = documentElement["client" + Name];
  76. }
  77. else {
  78. // Media query didn't match, use window["inner" + Name]
  79. size = window["inner" + Name];
  80. }
  81. // Cleanup
  82. documentElement.removeChild(bodyElement);
  83. }
  84. else {
  85. // Default to use window["inner" + Name]
  86. size = window["inner" + Name];
  87. }
  88. return size;
  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_oGame !== null){
  153. s_oGame.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_oStage.scaleX = s_oStage.scaleY = iScale*2;
  166. }else if(s_bMobile || isChrome()){
  167. $("#canvas").css("width",destW+"px");
  168. $("#canvas").css("height",destH+"px");
  169. }else{
  170. s_oStage.canvas.width = destW;
  171. s_oStage.canvas.height = destH;
  172. s_iScaleFactor = Math.min(destW / CANVAS_WIDTH, destH / CANVAS_HEIGHT);
  173. s_oStage.scaleX = s_oStage.scaleY = s_iScaleFactor;
  174. }
  175. if(fOffsetY < 0){
  176. $("#canvas").css("top",fOffsetY+"px");
  177. }else{
  178. $("#canvas").css("top","0px");
  179. }
  180. $("#canvas").css("left",fOffsetX+"px");
  181. fullscreenHandler();
  182. };
  183. function _checkOrientation(iWidth,iHeight){
  184. if(s_bMobile && ENABLE_CHECK_ORIENTATION){
  185. if( iWidth>iHeight ){
  186. if( $(".orientation-msg-container").attr("data-orientation") === "landscape" ){
  187. $(".orientation-msg-container").css("display","none");
  188. s_oMain.startUpdate();
  189. }else{
  190. $(".orientation-msg-container").css("display","block");
  191. s_oMain.stopUpdate();
  192. }
  193. }else{
  194. if( $(".orientation-msg-container").attr("data-orientation") === "portrait" ){
  195. $(".orientation-msg-container").css("display","none");
  196. s_oMain.startUpdate();
  197. }else{
  198. $(".orientation-msg-container").css("display","block");
  199. s_oMain.stopUpdate();
  200. }
  201. }
  202. }
  203. }
  204. function inIframe() {
  205. try {
  206. return window.self !== window.top;
  207. } catch (e) {
  208. return true;
  209. }
  210. }
  211. function createBitmap(oSprite, iWidth, iHeight){
  212. var oBmp = new createjs.Bitmap(oSprite);
  213. var hitObject = new createjs.Shape();
  214. if (iWidth && iHeight){
  215. hitObject .graphics.beginFill("#fff").drawRect(0, 0, iWidth, iHeight);
  216. }else{
  217. hitObject .graphics.beginFill("#ff0").drawRect(0, 0, oSprite.width, oSprite.height);
  218. }
  219. oBmp.hitArea = hitObject;
  220. return oBmp;
  221. }
  222. function createSprite(oSpriteSheet, szState, iRegX,iRegY,iWidth, iHeight){
  223. if(szState !== null){
  224. var oRetSprite = new createjs.Sprite(oSpriteSheet, szState);
  225. }else{
  226. var oRetSprite = new createjs.Sprite(oSpriteSheet);
  227. }
  228. var hitObject = new createjs.Shape();
  229. hitObject .graphics.beginFill("#000000").drawRect(-iRegX, -iRegY, iWidth, iHeight);
  230. oRetSprite.hitArea = hitObject;
  231. return oRetSprite;
  232. }
  233. function randomFloatBetween(minValue,maxValue,precision){
  234. if(typeof(precision) === 'undefined'){
  235. precision = 2;
  236. }
  237. return parseFloat(Math.min(minValue + (Math.random() * (maxValue - minValue)),maxValue).toFixed(precision));
  238. }
  239. function shuffle(array) {
  240. var currentIndex = array.length
  241. , temporaryValue
  242. , randomIndex
  243. ;
  244. // While there remain elements to shuffle...
  245. while (0 !== currentIndex) {
  246. // Pick a remaining element...
  247. randomIndex = Math.floor(Math.random() * currentIndex);
  248. currentIndex -= 1;
  249. // And swap it with the current element.
  250. temporaryValue = array[currentIndex];
  251. array[currentIndex] = array[randomIndex];
  252. array[randomIndex] = temporaryValue;
  253. }
  254. return array;
  255. }
  256. function NoClickDelay(el) {
  257. this.element = el;
  258. if( window.Touch ) this.element.addEventListener('touchstart', this, false);
  259. }
  260. NoClickDelay.prototype = {
  261. handleEvent: function(e) {
  262. switch(e.type) {
  263. case 'touchstart': this.onTouchStart(e); break;
  264. case 'touchmove': this.onTouchMove(e); break;
  265. case 'touchend': this.onTouchEnd(e); break;
  266. }
  267. },
  268. onTouchStart: function(e) {
  269. e.preventDefault();
  270. this.moved = false;
  271. this.element.addEventListener('touchmove', this, false);
  272. this.element.addEventListener('touchend', this, false);
  273. },
  274. onTouchMove: function(e) {
  275. this.moved = true;
  276. },
  277. onTouchEnd: function(e) {
  278. this.element.removeEventListener('touchmove', this, false);
  279. this.element.removeEventListener('touchend', this, false);
  280. if( !this.moved ) {
  281. var theTarget = document.elementFromPoint(e.changedTouches[0].clientX, e.changedTouches[0].clientY);
  282. if(theTarget.nodeType == 3) theTarget = theTarget.parentNode;
  283. var theEvent = document.createEvent('MouseEvents');
  284. theEvent.initEvent('click', true, true);
  285. theTarget.dispatchEvent(theEvent);
  286. }
  287. }
  288. };
  289. function playSound(szSound,iVolume,bLoop){
  290. if(DISABLE_SOUND_MOBILE === false || s_bMobile === false){
  291. s_aSounds[szSound].play();
  292. s_aSounds[szSound].volume(iVolume);
  293. s_aSounds[szSound].loop(bLoop);
  294. return s_aSounds[szSound];
  295. }
  296. return null;
  297. }
  298. function stopSound(szSound){
  299. if(DISABLE_SOUND_MOBILE === false || s_bMobile === false){
  300. s_aSounds[szSound].stop();
  301. }
  302. }
  303. function setVolume(szSound, iVolume){
  304. if(DISABLE_SOUND_MOBILE === false || s_bMobile === false){
  305. s_aSounds[szSound].volume(iVolume);
  306. }
  307. }
  308. function setMute(szSound, bMute){
  309. if(DISABLE_SOUND_MOBILE === false || s_bMobile === false){
  310. s_aSounds[szSound].mute(bMute);
  311. }
  312. }
  313. function ctlArcadeResume(){
  314. if(s_oMain !== null){
  315. s_oMain.startUpdate();
  316. }
  317. }
  318. function ctlArcadePause(){
  319. if(s_oMain !== null){
  320. s_oMain.stopUpdate();
  321. }
  322. }
  323. function getParamValue(paramName){
  324. var url = window.location.search.substring(1);
  325. var qArray = url.split('&');
  326. for (var i = 0; i < qArray.length; i++)
  327. {
  328. var pArr = qArray[i].split('=');
  329. if (pArr[0] == paramName)
  330. return pArr[1];
  331. }
  332. }
  333. (function() {
  334. var hidden = "hidden";
  335. // Standards:
  336. if (hidden in document)
  337. document.addEventListener("visibilitychange", onchange);
  338. else if ((hidden = "mozHidden") in document)
  339. document.addEventListener("mozvisibilitychange", onchange);
  340. else if ((hidden = "webkitHidden") in document)
  341. document.addEventListener("webkitvisibilitychange", onchange);
  342. else if ((hidden = "msHidden") in document)
  343. document.addEventListener("msvisibilitychange", onchange);
  344. // IE 9 and lower:
  345. else if ('onfocusin' in document)
  346. document.onfocusin = document.onfocusout = onchange;
  347. // All others:
  348. else
  349. window.onpageshow = window.onpagehide
  350. = window.onfocus = window.onblur = onchange;
  351. function onchange (evt) {
  352. var v = 'visible', h = 'hidden',
  353. evtMap = {
  354. focus:v, focusin:v, pageshow:v, blur:h, focusout:h, pagehide:h
  355. };
  356. evt = evt || window.event;
  357. if (evt.type in evtMap){
  358. document.body.className = evtMap[evt.type];
  359. }else{
  360. document.body.className = this[hidden] ? "hidden" : "visible";
  361. if(document.body.className === "hidden"){
  362. s_oMain.stopUpdate();
  363. }else{
  364. s_oMain.startUpdate();
  365. }
  366. }
  367. }
  368. })();
  369. function fullscreenHandler(){
  370. if(!ENABLE_FULLSCREEN){
  371. return;
  372. }
  373. if(screen.height < window.innerHeight+3 && screen.height > window.innerHeight-3){
  374. s_bFullscreen = true;
  375. }else{
  376. s_bFullscreen = false;
  377. }
  378. if (s_oGame !== null){
  379. s_oGame.resetFullscreenBut();
  380. }
  381. if (s_oMenu !== null){
  382. s_oMenu.resetFullscreenBut();
  383. }
  384. }
  385. if (screenfull.enabled) {
  386. screenfull.on('change', function(){
  387. s_bFullscreen = screenfull.isFullscreen;
  388. if (s_oGame !== null){
  389. s_oGame.resetFullscreenBut();
  390. }
  391. if (s_oMenu !== null){
  392. s_oMenu.resetFullscreenBut();
  393. }
  394. });
  395. }