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.
 
 
 

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