You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

139 lines
2.6 KiB

  1. define([
  2. 'js/system/client',
  3. 'ui/factory',
  4. 'js/rendering/renderer',
  5. 'js/objects/objects',
  6. 'js/rendering/effects',
  7. 'js/rendering/numbers',
  8. 'js/input',
  9. 'js/system/events',
  10. 'js/resources',
  11. 'js/sound/sound',
  12. 'js/system/globals',
  13. 'js/components/components',
  14. 'ui/templates/tooltips/tooltips'
  15. ], function (
  16. client,
  17. uiFactory,
  18. renderer,
  19. objects,
  20. effects,
  21. numbers,
  22. input,
  23. events,
  24. resources,
  25. sound,
  26. globals,
  27. components
  28. ) {
  29. let fnQueueTick = null;
  30. const getQueueTick = updateMethod => {
  31. return () => requestAnimationFrame(updateMethod);
  32. };
  33. const loadLongPress = async () => {
  34. return new Promise(res => {
  35. require(['longPress'], res);
  36. });
  37. };
  38. return {
  39. hasFocus: true,
  40. lastRender: 0,
  41. msPerFrame: ~~(1000 / 60),
  42. init: async function () {
  43. if (isMobile) {
  44. $('.ui-container').addClass('mobile');
  45. //If we're on an ios device, we need to load longPress since that polyfills contextmenu for us
  46. if (_.isIos())
  47. await loadLongPress();
  48. }
  49. if (window.location.search.includes('hideMonetization'))
  50. $('.ui-container').addClass('hideMonetization');
  51. client.init(this.onClientReady.bind(this));
  52. },
  53. onClientReady: function () {
  54. client.request({
  55. module: 'clientConfig',
  56. method: 'getClientConfig',
  57. callback: this.onGetClientConfig.bind(this)
  58. });
  59. },
  60. onGetClientConfig: async function (config) {
  61. globals.clientConfig = config;
  62. await resources.init();
  63. await components.init();
  64. events.emit('onResourcesLoaded');
  65. this.start();
  66. },
  67. start: function () {
  68. window.onfocus = this.onFocus.bind(this, true);
  69. window.onblur = this.onFocus.bind(this, false);
  70. $(window).on('contextmenu', this.onContextMenu.bind(this));
  71. sound.init();
  72. objects.init();
  73. renderer.init();
  74. input.init();
  75. numbers.init();
  76. uiFactory.init(null);
  77. fnQueueTick = getQueueTick(this.update.bind(this));
  78. fnQueueTick();
  79. $('.loader-container').remove();
  80. },
  81. onFocus: function (hasFocus) {
  82. //Hack: Later we might want to make it not render when out of focus
  83. this.hasFocus = true;
  84. if (!hasFocus)
  85. input.resetKeys();
  86. },
  87. onContextMenu: function (e) {
  88. const allowed = ['txtUsername', 'txtPassword'].some(s => $(e.target).hasClass(s));
  89. if (!allowed) {
  90. e.preventDefault();
  91. return false;
  92. }
  93. },
  94. update: function () {
  95. const time = +new Date();
  96. if (time - this.lastRender < this.msPerFrame - 1) {
  97. fnQueueTick();
  98. return;
  99. }
  100. objects.update();
  101. renderer.update();
  102. uiFactory.update();
  103. numbers.update();
  104. renderer.render();
  105. this.lastRender = time;
  106. fnQueueTick();
  107. }
  108. };
  109. });