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.
 
 
 

186 lines
4.3 KiB

  1. define([
  2. 'js/system/events',
  3. 'html!ui/templates/hud/template',
  4. 'css!ui/templates/hud/styles',
  5. 'js/system/client',
  6. 'ui/shared/renderItem'
  7. ], function (
  8. events,
  9. template,
  10. styles,
  11. client,
  12. renderItem
  13. ) {
  14. return {
  15. tpl: template,
  16. stats: null,
  17. items: null,
  18. quickItem: null,
  19. postRender: function () {
  20. this.onEvent('onGetStats', this.events.onGetStats.bind(this));
  21. this.onEvent('onGetPortrait', this.events.onGetPortrait.bind(this));
  22. this.onEvent('onGetItems', this.events.onGetItems.bind(this));
  23. this.onEvent('onDestroyItems', this.events.onDestroyItems.bind(this));
  24. this.onEvent('onKeyDown', this.events.onKeyDown.bind(this));
  25. this.find('.quickItem')
  26. .on('mousemove', this.showQuickItemTooltip.bind(this, true))
  27. .on('mouseleave', this.showQuickItemTooltip.bind(this, false))
  28. .on('click', this.useQuickItem.bind(this));
  29. setInterval(this.update.bind(this), 100);
  30. },
  31. build: function () {
  32. let stats = this.stats;
  33. let boxes = this.find('.statBox');
  34. [
  35. stats.hp / stats.hpMax,
  36. stats.mana / stats.manaMax,
  37. stats.xp / stats.xpMax
  38. ].forEach((s, i) => boxes.eq(i).find('div:first-child').width(Math.max(0, Math.min(100, ~~(s * 100))) + '%'));
  39. this.find('.statManaReserve').width(Math.max(0, Math.min(100, ~~(stats.manaReservePercent * 100))) + '%');
  40. boxes.eq(0).find('.text').html(Math.floor(stats.hp) + '/' + ~~stats.hpMax);
  41. boxes.eq(1).find('.text').html(Math.floor(stats.mana) + '/' + ~~stats.manaMax);
  42. boxes.eq(2).find('.text').html('level: ' + stats.level);
  43. },
  44. useQuickItem: function () {
  45. const quickItem = this.items.find(f => f.has('quickSlot'));
  46. if (!quickItem)
  47. return;
  48. events.emit('onHideItemTooltip', quickItem);
  49. events.emit('onUseQuickItem', quickItem);
  50. client.request({
  51. cpn: 'player',
  52. method: 'performAction',
  53. data: {
  54. cpn: 'equipment',
  55. method: 'useQuickSlot',
  56. data: {
  57. slot: 0
  58. }
  59. }
  60. });
  61. },
  62. showQuickItemTooltip: function (show, e) {
  63. const item = this.quickItem;
  64. if (show) {
  65. let ttPos = null;
  66. if (e) {
  67. ttPos = {
  68. x: ~~(e.clientX + 32),
  69. y: ~~(e.clientY)
  70. };
  71. }
  72. events.emit('onShowItemTooltip', item, ttPos);
  73. } else
  74. events.emit('onHideItemTooltip', item);
  75. },
  76. update: function () {
  77. if (!this.items)
  78. return;
  79. const quickItem = this.items.find(f => f.has('quickSlot'));
  80. if (!quickItem)
  81. return;
  82. if (!quickItem.cd) {
  83. this.find('.quickItem').find('.cooldown').css({
  84. width: '0%'
  85. });
  86. return;
  87. }
  88. let elapsed = quickItem.cdMax - quickItem.cd;
  89. let width = 1 - (elapsed / quickItem.cdMax);
  90. if (width <= 0)
  91. width = 0;
  92. width = Math.ceil((width * 100) / 4) * 4;
  93. this.find('.quickItem').find('.cooldown').css({
  94. width: width + '%'
  95. });
  96. },
  97. events: {
  98. onGetStats: function (stats) {
  99. this.stats = stats;
  100. this.build();
  101. },
  102. onGetPortrait: function (portrait) {
  103. let spritesheet = portrait.spritesheet || '../../../images/portraitIcons.png';
  104. let x = portrait.x * -64;
  105. let y = portrait.y * -64;
  106. this.find('.portrait')
  107. .css({
  108. background: 'url("' + spritesheet + '") ' + x + 'px ' + y + 'px',
  109. visibility: 'visible'
  110. });
  111. },
  112. onDestroyItems: function (itemIds) {
  113. const quickItem = this.items.find(f => f.has('quickSlot'));
  114. if (!quickItem || itemIds.includes(quickItem.id)) {
  115. this.find('.quickItem')
  116. .hide()
  117. .find('.icon')
  118. .css('background', '');
  119. if (quickItem)
  120. events.emit('onHideItemTooltip', quickItem);
  121. }
  122. },
  123. onGetItems: function (items) {
  124. this.items = items;
  125. const quickItem = items.find(f => f.has('quickSlot'));
  126. this.quickItem = quickItem;
  127. if (!quickItem) {
  128. const oldQuickItem = this.find('.quickItem').data('item');
  129. if (oldQuickItem)
  130. events.emit('onHideItemTooltip', oldQuickItem);
  131. this.find('.quickItem')
  132. .hide()
  133. .removeData('item')
  134. .find('.icon')
  135. .css('background', '');
  136. return;
  137. }
  138. const itemContainer = this.find('.quickItem').show();
  139. const itemEl = renderItem(null, quickItem, itemContainer);
  140. if (itemEl.data('item') && itemEl.data('item').id === quickItem.id)
  141. return;
  142. itemEl.data('item', quickItem);
  143. },
  144. onKeyDown: function (key) {
  145. if (key === 'r')
  146. this.useQuickItem();
  147. }
  148. }
  149. };
  150. });