ctl_utils.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. var s_iOffsetX = 0;
  2. var s_iOffsetY = 0;
  3. var s_fInverseScaling = 0;
  4. var s_bIsIphone = false;
  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. function trace(szMsg){
  11. console.log(szMsg);
  12. }
  13. function isChrome(){
  14. var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
  15. return isChrome;
  16. }
  17. function isIOS() {
  18. var iDevices = [
  19. 'iPad Simulator',
  20. 'iPhone Simulator',
  21. 'iPod Simulator',
  22. 'iPad',
  23. 'iPhone',
  24. 'iPod'
  25. ];
  26. if (navigator.userAgent.toLowerCase().indexOf("iphone") !== -1){
  27. s_bIsIphone = true;
  28. }
  29. while (iDevices.length) {
  30. if (navigator.platform === iDevices.pop()){
  31. return true;
  32. }
  33. }
  34. s_bIsIphone = false;
  35. return false;
  36. }
  37. $(window).resize(function() {
  38. sizeHandler();
  39. });
  40. function getSize(Name) {
  41. var size;
  42. var name = Name.toLowerCase();
  43. var document = window.document;
  44. var documentElement = document.documentElement;
  45. if (window["inner" + Name] === undefined) {
  46. // IE6 & IE7 don't have window.innerWidth or innerHeight
  47. size = documentElement["client" + Name];
  48. }
  49. else if (window["inner" + Name] != documentElement["client" + Name]) {
  50. // WebKit doesn't include scrollbars while calculating viewport size so we have to get fancy
  51. // Insert markup to test if a media query will match document.doumentElement["client" + Name]
  52. var bodyElement = document.createElement("body");
  53. bodyElement.id = "vpw-test-b";
  54. bodyElement.style.cssText = "overflow:scroll";
  55. var divElement = document.createElement("div");
  56. divElement.id = "vpw-test-d";
  57. divElement.style.cssText = "position:absolute;top:-1000px";
  58. // Getting specific on the CSS selector so it won't get overridden easily
  59. divElement.innerHTML = "<style>@media(" + name + ":" + documentElement["client" + Name] + "px){body#vpw-test-b div#vpw-test-d{" + name + ":7px!important}}</style>";
  60. bodyElement.appendChild(divElement);
  61. documentElement.insertBefore(bodyElement, document.head);
  62. if (divElement["offset" + Name] == 7) {
  63. // Media query matches document.documentElement["client" + Name]
  64. size = documentElement["client" + Name];
  65. }
  66. else {
  67. // Media query didn't match, use window["inner" + Name]
  68. size = window["inner" + Name];
  69. }
  70. // Cleanup
  71. documentElement.removeChild(bodyElement);
  72. }
  73. else {
  74. // Default to use window["inner" + Name]
  75. size = window["inner" + Name];
  76. }
  77. return size;
  78. };
  79. window.addEventListener("orientationchange", onOrientationChange );
  80. function onOrientationChange(){
  81. if (window.matchMedia("(orientation: portrait)").matches) {
  82. // you're in PORTRAIT mode
  83. sizeHandler();
  84. }
  85. if (window.matchMedia("(orientation: landscape)").matches) {
  86. // you're in LANDSCAPE mode
  87. sizeHandler();
  88. }
  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_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. // centered game
  179. fOffsetY = (h - destH)/2;
  180. $("#canvas").css("top",fOffsetY+"px");
  181. }
  182. $("#canvas").css("left",fOffsetX+"px");
  183. fullscreenHandler();
  184. };
  185. function _checkOrientation(iWidth,iHeight){
  186. if(s_bMobile && ENABLE_CHECK_ORIENTATION){
  187. if( iWidth>iHeight ){
  188. if( $(".orientation-msg-container").attr("data-orientation") === "landscape" ){
  189. $(".orientation-msg-container").css("display","none");
  190. s_oMain.startUpdate();
  191. }else{
  192. $(".orientation-msg-container").css("display","block");
  193. s_oMain.stopUpdate();
  194. }
  195. }else{
  196. if( $(".orientation-msg-container").attr("data-orientation") === "portrait" ){
  197. $(".orientation-msg-container").css("display","none");
  198. s_oMain.startUpdate();
  199. }else{
  200. $(".orientation-msg-container").css("display","block");
  201. s_oMain.stopUpdate();
  202. }
  203. }
  204. }
  205. }
  206. function createBitmap(oSprite, iWidth, iHeight){
  207. var oBmp = new createjs.Bitmap(oSprite);
  208. var hitObject = new createjs.Shape();
  209. if (iWidth && iHeight){
  210. hitObject .graphics.beginFill("#fff").drawRect(0, 0, iWidth, iHeight);
  211. }else{
  212. hitObject .graphics.beginFill("#ff0").drawRect(0, 0, oSprite.width, oSprite.height);
  213. }
  214. oBmp.hitArea = hitObject;
  215. return oBmp;
  216. }
  217. function createSprite(oSpriteSheet, szState, iRegX,iRegY,iWidth, iHeight){
  218. if(szState !== null){
  219. var oRetSprite = new createjs.Sprite(oSpriteSheet, szState);
  220. }else{
  221. var oRetSprite = new createjs.Sprite(oSpriteSheet);
  222. }
  223. var hitObject = new createjs.Shape();
  224. hitObject .graphics.beginFill("#000000").drawRect(-iRegX, -iRegY, iWidth, iHeight);
  225. oRetSprite.hitArea = hitObject;
  226. return oRetSprite;
  227. }
  228. function randomFloatBetween(minValue,maxValue,precision){
  229. if(typeof(precision) === 'undefined'){
  230. precision = 2;
  231. }
  232. return parseFloat(Math.min(minValue + (Math.random() * (maxValue - minValue)),maxValue).toFixed(precision));
  233. }
  234. function rotateVector2D( iAngle, v) {
  235. var iX = v.getX() * Math.cos( iAngle ) + v.getY() * Math.sin( iAngle );
  236. var iY = v.getX() * (-Math.sin( iAngle )) + v.getY() * Math.cos( iAngle );
  237. v.set( iX, iY );
  238. }
  239. function tweenVectorsOnX( vStart, vEnd, iLerp ){
  240. var iNewX = vStart + iLerp *( vEnd-vStart);
  241. return iNewX;
  242. }
  243. function shuffle(array) {
  244. var currentIndex = array.length
  245. , temporaryValue
  246. , randomIndex
  247. ;
  248. // While there remain elements to shuffle...
  249. while (0 !== currentIndex) {
  250. // Pick a remaining element...
  251. randomIndex = Math.floor(Math.random() * currentIndex);
  252. currentIndex -= 1;
  253. // And swap it with the current element.
  254. temporaryValue = array[currentIndex];
  255. array[currentIndex] = array[randomIndex];
  256. array[randomIndex] = temporaryValue;
  257. }
  258. return array;
  259. }
  260. function bubbleSort(a)
  261. {
  262. var swapped;
  263. do {
  264. swapped = false;
  265. for (var i=0; i < a.length-1; i++) {
  266. if (a[i] > a[i+1]) {
  267. var temp = a[i];
  268. a[i] = a[i+1];
  269. a[i+1] = temp;
  270. swapped = true;
  271. }
  272. }
  273. } while (swapped);
  274. }
  275. function compare(a,b) {
  276. if (a.index > b.index)
  277. return -1;
  278. if (a.index < b.index)
  279. return 1;
  280. return 0;
  281. }
  282. //----------------------
  283. // Linear
  284. /**
  285. * Interpolates a value between b and c parameters
  286. * <p></br><b>Note:</b></br>
  287. * &nbsp&nbsp&nbspt and d parameters can be in frames or seconds/milliseconds
  288. *
  289. * @param t Elapsed time
  290. * @param b Initial position
  291. * @param c Final position
  292. * @param d Duration
  293. * @return A value between b and c parameters
  294. */
  295. function easeLinear (t, b, c, d){
  296. return c*t/d + b;
  297. }
  298. //----------------------
  299. // Quad
  300. /**
  301. * Interpolates a value between b and c parameters
  302. * <p></br><b>Note:</b></br>
  303. * &nbsp&nbsp&nbspt and d parameters can be in frames or seconds/milliseconds
  304. *
  305. * @param t Elapsed time
  306. * @param b Initial position
  307. * @param c Final position
  308. * @param d Duration
  309. * @return A value between b and c parameters
  310. */
  311. function easeInQuad (t, b, c, d){
  312. return c*(t/=d)*t + b;
  313. }
  314. //----------------------
  315. // Sine
  316. /**
  317. * Interpolates a value between b and c parameters
  318. * <p></br><b>Note:</b></br>
  319. * &nbsp&nbsp&nbspt and d parameters can be in frames or seconds/milliseconds
  320. *
  321. * @param t Elapsed time
  322. * @param b Initial position
  323. * @param c Final position
  324. * @param d Duration
  325. * @return A value between b and c parameters
  326. */
  327. function easeInSine (t, b, c, d) {
  328. return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
  329. }
  330. function easeInCubic (t, b, c, d) {
  331. return c*(t/=d)*t*t + b;
  332. };
  333. function getTrajectoryPoint(t,p){
  334. var result = new createjs.Point();
  335. var oneMinusTSq = (1-t) * (1-t);
  336. var TSq = t*t;
  337. result.x = oneMinusTSq*p.start.x+2*(1-t)*t*p.traj.x+TSq*p.end.x;
  338. result.y = oneMinusTSq*p.start.y+2*(1-t)*t*p.traj.y+TSq*p.end.y;
  339. return result;
  340. }
  341. function formatTime(iTime){
  342. iTime/=1000;
  343. var iMins = Math.floor(iTime/60);
  344. var iSecs = Math.floor(iTime-(iMins*60));
  345. //iSecs = parseFloat(iSecs).toFixed(1)
  346. var szRet = "";
  347. if ( iMins < 10 ){
  348. szRet += "0" + iMins + ":";
  349. }else{
  350. szRet += iMins + ":";
  351. }
  352. if ( iSecs < 10 ){
  353. szRet += "0" + iSecs;
  354. }else{
  355. szRet += iSecs;
  356. }
  357. return szRet;
  358. }
  359. function degreesToRadians(iAngle){
  360. return iAngle * Math.PI / 180;
  361. }
  362. function checkRectCollision(bitmap1,bitmap2) {
  363. var b1, b2;
  364. b1 = getBounds(bitmap1,0.9);
  365. b2 = getBounds(bitmap2,0.98);
  366. return calculateIntersection(b1,b2);
  367. }
  368. function calculateIntersection(rect1, rect2){
  369. // first we have to calculate the
  370. // center of each rectangle and half of
  371. // width and height
  372. var dx, dy, r1={}, r2={};
  373. r1.cx = rect1.x + (r1.hw = (rect1.width /2));
  374. r1.cy = rect1.y + (r1.hh = (rect1.height/2));
  375. r2.cx = rect2.x + (r2.hw = (rect2.width /2));
  376. r2.cy = rect2.y + (r2.hh = (rect2.height/2));
  377. dx = Math.abs(r1.cx-r2.cx) - (r1.hw + r2.hw);
  378. dy = Math.abs(r1.cy-r2.cy) - (r1.hh + r2.hh);
  379. if (dx < 0 && dy < 0) {
  380. dx = Math.min(Math.min(rect1.width,rect2.width),-dx);
  381. dy = Math.min(Math.min(rect1.height,rect2.height),-dy);
  382. return {x:Math.max(rect1.x,rect2.x),
  383. y:Math.max(rect1.y,rect2.y),
  384. width:dx,
  385. height:dy,
  386. rect1: rect1,
  387. rect2: rect2};
  388. } else {
  389. return null;
  390. }
  391. }
  392. function getBounds(obj,iTolerance) {
  393. var bounds={x:Infinity,y:Infinity,width:0,height:0};
  394. if ( obj instanceof createjs.Container ) {
  395. bounds.x2 = -Infinity;
  396. bounds.y2 = -Infinity;
  397. var children = obj.children, l=children.length, cbounds, c;
  398. for ( c = 0; c < l; c++ ) {
  399. cbounds = getBounds(children[c],1);
  400. if ( cbounds.x < bounds.x ) bounds.x = cbounds.x;
  401. if ( cbounds.y < bounds.y ) bounds.y = cbounds.y;
  402. if ( cbounds.x + cbounds.width > bounds.x2 ) bounds.x2 = cbounds.x + cbounds.width;
  403. if ( cbounds.y + cbounds.height > bounds.y2 ) bounds.y2 = cbounds.y + cbounds.height;
  404. //if ( cbounds.x - bounds.x + cbounds.width > bounds.width ) bounds.width = cbounds.x - bounds.x + cbounds.width;
  405. //if ( cbounds.y - bounds.y + cbounds.height > bounds.height ) bounds.height = cbounds.y - bounds.y + cbounds.height;
  406. }
  407. if ( bounds.x == Infinity ) bounds.x = 0;
  408. if ( bounds.y == Infinity ) bounds.y = 0;
  409. if ( bounds.x2 == Infinity ) bounds.x2 = 0;
  410. if ( bounds.y2 == Infinity ) bounds.y2 = 0;
  411. bounds.width = bounds.x2 - bounds.x;
  412. bounds.height = bounds.y2 - bounds.y;
  413. delete bounds.x2;
  414. delete bounds.y2;
  415. } else {
  416. var gp,gp2,gp3,gp4,imgr={},sr;
  417. if ( obj instanceof createjs.Bitmap ) {
  418. sr = obj.sourceRect || obj.image;
  419. imgr.width = sr.width * iTolerance;
  420. imgr.height = sr.height * iTolerance;
  421. } else if ( obj instanceof createjs.Sprite ) {
  422. if ( obj.spriteSheet._frames && obj.spriteSheet._frames[obj.currentFrame] && obj.spriteSheet._frames[obj.currentFrame].image ) {
  423. var cframe = obj.spriteSheet.getFrame(obj.currentFrame);
  424. imgr.width = cframe.rect.width;
  425. imgr.height = cframe.rect.height;
  426. imgr.regX = cframe.regX;
  427. imgr.regY = cframe.regY;
  428. } else {
  429. bounds.x = obj.x || 0;
  430. bounds.y = obj.y || 0;
  431. }
  432. } else {
  433. bounds.x = obj.x || 0;
  434. bounds.y = obj.y || 0;
  435. }
  436. imgr.regX = imgr.regX || 0; imgr.width = imgr.width || 0;
  437. imgr.regY = imgr.regY || 0; imgr.height = imgr.height || 0;
  438. bounds.regX = imgr.regX;
  439. bounds.regY = imgr.regY;
  440. gp = obj.localToGlobal(0 -imgr.regX,0 -imgr.regY);
  441. gp2 = obj.localToGlobal(imgr.width-imgr.regX,imgr.height-imgr.regY);
  442. gp3 = obj.localToGlobal(imgr.width-imgr.regX,0 -imgr.regY);
  443. gp4 = obj.localToGlobal(0 -imgr.regX,imgr.height-imgr.regY);
  444. bounds.x = Math.min(Math.min(Math.min(gp.x,gp2.x),gp3.x),gp4.x);
  445. bounds.y = Math.min(Math.min(Math.min(gp.y,gp2.y),gp3.y),gp4.y);
  446. bounds.width = Math.max(Math.max(Math.max(gp.x,gp2.x),gp3.x),gp4.x) - bounds.x;
  447. bounds.height = Math.max(Math.max(Math.max(gp.y,gp2.y),gp3.y),gp4.y) - bounds.y;
  448. }
  449. return bounds;
  450. }
  451. function NoClickDelay(el) {
  452. this.element = el;
  453. if( window.Touch ) this.element.addEventListener('touchstart', this, false);
  454. }
  455. //Fisher-Yates Shuffle
  456. function shuffle(array) {
  457. var counter = array.length, temp, index;
  458. // While there are elements in the array
  459. while (counter > 0) {
  460. // Pick a random index
  461. index = Math.floor(Math.random() * counter);
  462. // Decrease counter by 1
  463. counter--;
  464. // And swap the last element with it
  465. temp = array[counter];
  466. array[counter] = array[index];
  467. array[index] = temp;
  468. }
  469. return array;
  470. }
  471. NoClickDelay.prototype = {
  472. handleEvent: function(e) {
  473. switch(e.type) {
  474. case 'touchstart': this.onTouchStart(e); break;
  475. case 'touchmove': this.onTouchMove(e); break;
  476. case 'touchend': this.onTouchEnd(e); break;
  477. }
  478. },
  479. onTouchStart: function(e) {
  480. e.preventDefault();
  481. this.moved = false;
  482. this.element.addEventListener('touchmove', this, false);
  483. this.element.addEventListener('touchend', this, false);
  484. },
  485. onTouchMove: function(e) {
  486. this.moved = true;
  487. },
  488. onTouchEnd: function(e) {
  489. this.element.removeEventListener('touchmove', this, false);
  490. this.element.removeEventListener('touchend', this, false);
  491. if( !this.moved ) {
  492. var theTarget = document.elementFromPoint(e.changedTouches[0].clientX, e.changedTouches[0].clientY);
  493. if(theTarget.nodeType == 3) theTarget = theTarget.parentNode;
  494. var theEvent = document.createEvent('MouseEvents');
  495. theEvent.initEvent('click', true, true);
  496. theTarget.dispatchEvent(theEvent);
  497. }
  498. }
  499. };
  500. (function() {
  501. var hidden = "hidden";
  502. // Standards:
  503. if (hidden in document)
  504. document.addEventListener("visibilitychange", onchange);
  505. else if ((hidden = "mozHidden") in document)
  506. document.addEventListener("mozvisibilitychange", onchange);
  507. else if ((hidden = "webkitHidden") in document)
  508. document.addEventListener("webkitvisibilitychange", onchange);
  509. else if ((hidden = "msHidden") in document)
  510. document.addEventListener("msvisibilitychange", onchange);
  511. // IE 9 and lower:
  512. else if ('onfocusin' in document)
  513. document.onfocusin = document.onfocusout = onchange;
  514. // All others:
  515. else
  516. window.onpageshow = window.onpagehide
  517. = window.onfocus = window.onblur = onchange;
  518. function onchange (evt) {
  519. var v = 'visible', h = 'hidden',
  520. evtMap = {
  521. focus:v, focusin:v, pageshow:v, blur:h, focusout:h, pagehide:h
  522. };
  523. evt = evt || window.event;
  524. if (evt.type in evtMap){
  525. document.body.className = evtMap[evt.type];
  526. }else{
  527. document.body.className = this[hidden] ? "hidden" : "visible";
  528. if(document.body.className === "hidden"){
  529. s_oMain.stopUpdate();
  530. }else{
  531. s_oMain.startUpdate();
  532. }
  533. }
  534. }
  535. })();
  536. function ctlArcadeResume(){
  537. if(s_oMain !== null){
  538. s_oMain.startUpdate();
  539. }
  540. }
  541. function ctlArcadePause(){
  542. if(s_oMain !== null){
  543. s_oMain.stopUpdate();
  544. }
  545. }
  546. function getParamValue(paramName){
  547. var url = window.location.search.substring(1);
  548. var qArray = url.split('&');
  549. for (var i = 0; i < qArray.length; i++)
  550. {
  551. var pArr = qArray[i].split('=');
  552. if (pArr[0] == paramName)
  553. return pArr[1];
  554. }
  555. }
  556. function playSound(szSound,iVolume,bLoop){
  557. if(DISABLE_SOUND_MOBILE === false || s_bMobile === false){
  558. s_aSounds[szSound].play();
  559. s_aSounds[szSound].volume(iVolume);
  560. s_aSounds[szSound].loop(bLoop);
  561. return s_aSounds[szSound];
  562. }
  563. return null;
  564. }
  565. function stopSound(szSound){
  566. if(DISABLE_SOUND_MOBILE === false || s_bMobile === false){
  567. s_aSounds[szSound].stop();
  568. }
  569. }
  570. function setVolume(szSound, iVolume){
  571. if(DISABLE_SOUND_MOBILE === false || s_bMobile === false){
  572. s_aSounds[szSound].volume(iVolume);
  573. }
  574. }
  575. function setMute(szSound, bMute){
  576. if(DISABLE_SOUND_MOBILE === false || s_bMobile === false){
  577. s_aSounds[szSound].mute(bMute);
  578. }
  579. }
  580. function fullscreenHandler(){
  581. if (!ENABLE_FULLSCREEN || screenfull.enabled === false){
  582. return;
  583. }
  584. if(screen.height < window.innerHeight+3 && screen.height > window.innerHeight-3){
  585. s_bFullscreen = true;
  586. }else{
  587. s_bFullscreen = false;
  588. }
  589. if (s_oInterface !== null){
  590. s_oInterface.resetFullscreenBut();
  591. }
  592. if (s_oMenu !== null){
  593. s_oMenu.resetFullscreenBut();
  594. }
  595. }
  596. if (screenfull.enabled) {
  597. screenfull.on('change', function(){
  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. }