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.
 
 
 

196 line
3.9 KiB

  1. define([
  2. 'ui/uiBase',
  3. 'js/system/events',
  4. 'js/system/client',
  5. 'js/system/globals',
  6. 'js/misc/tosAcceptanceValid'
  7. ], function (
  8. uiBase,
  9. events,
  10. client,
  11. globals,
  12. tosAcceptanceValid
  13. ) {
  14. return {
  15. uis: [],
  16. root: '',
  17. ingameUisBuilt: false,
  18. init: function (root) {
  19. if (root)
  20. this.root = root + '/';
  21. events.on('onBuildIngameUis', this.onBuildIngameUis.bind(this));
  22. events.on('onUiKeyDown', this.onUiKeyDown.bind(this));
  23. events.on('onResize', this.onResize.bind(this));
  24. globals.clientConfig.uiLoginList.forEach(u => {
  25. if (u.path)
  26. this.buildModUi(u);
  27. else
  28. this.build(u);
  29. });
  30. },
  31. onBuildIngameUis: async function () {
  32. if (!this.ingameUisBuilt) {
  33. events.clearQueue();
  34. await Promise.all(
  35. globals.clientConfig.uiList
  36. .filter(u => u.autoLoadOnPlay !== false)
  37. .map(u => {
  38. const uiType = u.path ? u.path.split('/').pop() : u;
  39. return new Promise(res => {
  40. const doneCheck = () => {
  41. const isDone = this.uis.some(ui => ui.type === uiType);
  42. if (isDone) {
  43. res();
  44. return;
  45. }
  46. setTimeout(doneCheck, 100);
  47. };
  48. this.build(uiType, { path: u.path });
  49. doneCheck();
  50. });
  51. })
  52. );
  53. this.ingameUisBuilt = true;
  54. }
  55. client.request({
  56. threadModule: 'instancer',
  57. method: 'clientAck',
  58. data: {}
  59. });
  60. },
  61. buildModUi: function (config) {
  62. const type = config.path.split('/').pop();
  63. this.build(type, {
  64. path: config.path
  65. });
  66. },
  67. build: function (type, options) {
  68. let className = 'ui' + type[0].toUpperCase() + type.substr(1);
  69. let el = $('.' + className);
  70. if (el.length > 0)
  71. return;
  72. this.getTemplate(type, options);
  73. },
  74. getTemplate: function (type, options) {
  75. let path = null;
  76. if (options && options.path)
  77. path = options.path + `\\${type}.js`;
  78. else {
  79. const entryInClientConfig = globals.clientConfig.uiList.find(u => u.type === type);
  80. if (entryInClientConfig)
  81. path = entryInClientConfig.path;
  82. else
  83. path = this.root + 'ui/templates/' + type + '/' + type;
  84. }
  85. require([path], this.onGetTemplate.bind(this, options, type));
  86. },
  87. onGetTemplate: function (options, type, template) {
  88. let ui = $.extend(true, { type }, uiBase, template);
  89. ui.setOptions(options);
  90. requestAnimationFrame(this.renderUi.bind(this, ui));
  91. },
  92. renderUi: function (ui) {
  93. ui.render();
  94. ui.el.data('ui', ui);
  95. this.uis.push(ui);
  96. },
  97. onResize: function () {
  98. this.uis.forEach(function (ui) {
  99. if (ui.centered)
  100. ui.center();
  101. else if ((ui.centeredX) || (ui.centeredY))
  102. ui.center(ui.centeredX, ui.centeredY);
  103. }, this);
  104. },
  105. onUiKeyDown: function (keyEvent) {
  106. if (keyEvent.key === 'esc') {
  107. this.uis.forEach(u => {
  108. if (!u.modal || !u.shown)
  109. return;
  110. keyEvent.consumed = true;
  111. u.toggle();
  112. });
  113. $('.uiOverlay').hide();
  114. events.emit('onHideContextMenu');
  115. } else if (['o', 'j', 'h', 'i'].indexOf(keyEvent.key) > -1)
  116. $('.uiOverlay').hide();
  117. },
  118. preload: function () {
  119. require([
  120. 'death',
  121. 'dialogue',
  122. 'equipment',
  123. 'events',
  124. 'hud',
  125. 'inventory',
  126. 'overlay',
  127. 'passives',
  128. 'quests',
  129. 'reputation',
  130. 'stash'
  131. ].map(m => 'ui/templates/' + m + '/' + m), this.afterPreload.bind(this));
  132. },
  133. afterPreload: function () {
  134. if (!globals.clientConfig.tos.required || tosAcceptanceValid()) {
  135. this.build('characters');
  136. return;
  137. }
  138. this.build('terms');
  139. },
  140. update: function () {
  141. let uis = this.uis;
  142. let uLen = uis.length;
  143. for (let i = 0; i < uLen; i++) {
  144. let u = uis[i];
  145. if (u.update)
  146. u.update();
  147. }
  148. },
  149. exitGame: function () {
  150. $('[class^="ui"]:not(.ui-container)').toArray().forEach(el => {
  151. let ui = $(el).data('ui');
  152. if (ui && ui.destroy)
  153. ui.destroy();
  154. });
  155. this.ingameUisBuilt = false;
  156. },
  157. getUi: function (type) {
  158. return this.uis.find(u => u.type === type);
  159. }
  160. };
  161. });