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.
 
 
 

343 lines
8.0 KiB

  1. define([
  2. 'js/system/events',
  3. 'js/system/client',
  4. 'html!ui/templates/equipment/template',
  5. 'css!ui/templates/equipment/styles'
  6. ], function(
  7. events,
  8. client,
  9. template,
  10. styles
  11. ) {
  12. return {
  13. tpl: template,
  14. centered: true,
  15. modal: true,
  16. stats: null,
  17. equipment: null,
  18. hoverItem: null,
  19. hoverEl: null,
  20. hoverCompare: null,
  21. shiftDown: false,
  22. postRender: function() {
  23. this.onEvent('onGetStats', this.onGetStats.bind(this));
  24. this.onEvent('onGetItems', this.onGetItems.bind(this));
  25. this.onEvent('onShowEquipment', this.toggle.bind(this));
  26. this.find('.tab').on('click', this.onTabClick.bind(this));
  27. this.onEvent('onKeyDown', this.onKeyDown.bind(this));
  28. this.onEvent('onKeyUp', this.onKeyUp.bind(this));
  29. },
  30. toggle: function(show) {
  31. this.shown = !this.el.is(':visible');
  32. if (this.shown) {
  33. this.find('.itemList').hide();
  34. this.show();
  35. this.onGetStats();
  36. this.onGetItems();
  37. } else {
  38. this.find('.itemList').hide();
  39. this.hide();
  40. }
  41. this.onHoverItem(null, null, null);
  42. },
  43. onKeyDown: function(key) {
  44. if (key == 'j')
  45. this.toggle();
  46. else if (key == 'shift') {
  47. this.shiftDown = true;
  48. if (this.hoverItem)
  49. this.onHoverItem(this.hoverEl, this.hoverItem, this.hoverCompare);
  50. }
  51. },
  52. onKeyUp: function(key) {
  53. if (key == 'shift') {
  54. this.shiftDown = false;
  55. if (this.hoverItem)
  56. this.onHoverItem(this.hoverEl, this.hoverItem, null);
  57. }
  58. },
  59. onTabClick: function(e) {
  60. this.find('.tab.selected').removeClass('selected');
  61. $(e.currentTarget).addClass('selected');
  62. this.onGetStats(this.stats);
  63. },
  64. onGetItems: function(items) {
  65. items = items || this.items;
  66. this.items = items;
  67. if (!this.shown)
  68. return;
  69. this.find('.slot').addClass('empty');
  70. var skipSpellId = 0;
  71. this.find('[slot]')
  72. .removeData('item')
  73. .addClass('empty')
  74. .find('.icon')
  75. .off()
  76. .css('background', '')
  77. .on('click', this.buildSlot.bind(this));
  78. items
  79. .filter(function(item) {
  80. var spellId = item.spellId;
  81. if ((spellId != null) && (item.slot))
  82. skipSpellId = spellId;
  83. return ((item.eq) && ((item.slot) || (item.spellId != null)));
  84. }, this)
  85. .forEach(function(item) {
  86. var imgX = -item.sprite[0] * 64;
  87. var imgY = -item.sprite[1] * 64;
  88. var slot = item.slot;
  89. if (!slot) {
  90. var spellId = item.spellId;
  91. if (spellId > skipSpellId)
  92. spellId--;
  93. slot = 'rune-' + spellId;
  94. }
  95. var spritesheet = item.spritesheet || '../../../images/items.png';
  96. var elSlot = this.find('[slot="' + slot + '"]');
  97. elSlot
  98. .data('item', item)
  99. .removeClass('empty')
  100. .find('.icon')
  101. .css('background', 'url("' + spritesheet + '") ' + imgX + 'px ' + imgY + 'px')
  102. .off()
  103. .on('mousemove', this.onHoverItem.bind(this, elSlot, item, null))
  104. .on('mouseleave', this.onHoverItem.bind(this, null, null))
  105. .on('click', this.buildSlot.bind(this, elSlot));
  106. }, this);
  107. },
  108. buildSlot: function(el) {
  109. if (el.currentTarget)
  110. el = $(el.currentTarget).parent();
  111. var slot = el.attr('slot');
  112. var isRune = (slot.indexOf('rune') == 0);
  113. var container = this.find('.itemList')
  114. .empty()
  115. .show();
  116. this.hoverCompare = el.data('item');
  117. var items = this.items
  118. .filter(function(item) {
  119. if (isRune)
  120. return ((!item.slot) && (item.spell) && (!item.eq));
  121. else
  122. return ((item.slot == slot) && (!item.eq));
  123. }, this);
  124. items.splice(0, 0, {
  125. name: 'None',
  126. slot: this.hoverCompare ? this.hoverCompare.slot : null,
  127. id: this.hoverCompare ? this.hoverCompare.id : null,
  128. empty: true
  129. });
  130. if (this.hoverCompare)
  131. items.splice(1, 0, this.hoverCompare);
  132. items
  133. .forEach(function(item) {
  134. var sprite = item.sprite || [7, 0];
  135. var spriteSheet = item.empty ? '../../../images/uiIcons.png' : item.spritesheet || '../../../images/items.png';
  136. var imgX = -sprite[0] * 64;
  137. var imgY = -sprite[1] * 64;
  138. var el = $('<div class="slot"><div class="icon"></div></div>')
  139. .appendTo(container);
  140. el
  141. .find('.icon')
  142. .css('background', 'url("' + spriteSheet + '") ' + imgX + 'px ' + imgY + 'px')
  143. .on('mousemove', this.onHoverItem.bind(this, el, item, null))
  144. .on('mouseleave', this.onHoverItem.bind(this, null, null))
  145. .on('click', this.equipItem.bind(this, item));
  146. if (item == this.hoverCompare)
  147. el.find('.icon').addClass('eq');
  148. }, this);
  149. if (items.length == 0)
  150. container.hide();
  151. },
  152. equipItem: function(item) {
  153. if (item == this.hoverCompare) {
  154. this.find('.itemList').hide();
  155. return;
  156. }
  157. var cpn = 'equipment';
  158. var method = 'equip';
  159. var data = item.id;
  160. if (item.empty)
  161. method = 'unequip';
  162. if (!item.slot) {
  163. cpn = 'inventory';
  164. method = 'learnAbility';
  165. data = {
  166. id: item.id,
  167. replaceId: this.hoverCompare ? this.hoverCompare.id : null
  168. };
  169. if (item.empty) {
  170. if (!this.hoverCompare) {
  171. this.find('.itemList').hide();
  172. return;
  173. }
  174. else
  175. data = this.hoverCompare.id;
  176. }
  177. }
  178. client.request({
  179. cpn: 'player',
  180. method: 'performAction',
  181. data: {
  182. cpn: cpn,
  183. method: method,
  184. data: data
  185. }
  186. });
  187. this.find('.itemList').hide();
  188. },
  189. onHoverItem: function(el, item, compare, e) {
  190. if (el) {
  191. this.hoverItem = item;
  192. this.hoverEl = el;
  193. var ttPos = null;
  194. if (e) {
  195. ttPos = {
  196. x: ~~(e.clientX + 32),
  197. y: ~~(e.clientY)
  198. };
  199. }
  200. events.emit('onShowItemTooltip', item, ttPos, this.hoverCompare, false, this.shiftDown);
  201. } else {
  202. events.emit('onHideItemTooltip', this.hoverItem);
  203. this.hoverItem = null;
  204. }
  205. },
  206. onGetStats: function(stats) {
  207. if (stats)
  208. this.stats = stats;
  209. stats = this.stats;
  210. if (!this.shown)
  211. return;
  212. var container = this.el.find('.stats');
  213. container
  214. .children('*:not(.tabs)')
  215. .remove();
  216. var xpRemaining = (stats.xpMax - stats.xp).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
  217. var newStats = {
  218. basic: {
  219. level: stats.level,
  220. 'next level': xpRemaining + 'xp',
  221. gap1: '',
  222. gold: window.player.trade.gold,
  223. gap2: '',
  224. hp: ~~stats.hp + '/' + stats.hpMax,
  225. mana: ~~stats.mana + '/' + stats.manaMax,
  226. 'hp regen': stats.regenHp,
  227. 'mana regen': stats.regenMana + '%',
  228. gap3: '',
  229. str: stats.str,
  230. int: stats.int,
  231. dex: stats.dex
  232. },
  233. offense: {
  234. 'crit chance': (~~(stats.critChance * 10) / 10) + '%',
  235. gap1: '',
  236. 'arcane increase': stats.elementArcanePercent + '%',
  237. 'fire increase': stats.elementFirePercent + '%',
  238. 'frost increase': stats.elementFrostPercent + '%',
  239. 'holy increase': stats.elementHolyPercent + '%',
  240. 'physical increase': stats.elementPhysicalPercent + '%',
  241. 'poison increase': stats.elementPoisonPercent + '%',
  242. gap2: '',
  243. 'damage increase': stats.dmgPercent + '%'
  244. },
  245. defense: {
  246. armor: stats.armor,
  247. gap1: '',
  248. 'arcane resist': stats.elementArcaneResist,
  249. 'fire resist': stats.elementFireResist,
  250. 'frost resist': stats.elementFrostResist,
  251. 'holy resist': stats.elementHolyResist,
  252. 'physical resist': stats.elementPhysicalResist,
  253. 'poison resist': stats.elementPoisonResist,
  254. gap2: '',
  255. 'all resist': stats.elementAllResist
  256. },
  257. misc: {
  258. 'magic find': stats.magicFind,
  259. gap1: '',
  260. 'sprint chance': (stats.sprintChance || 0) + '%',
  261. gap2: '',
  262. 'xp increase': stats.xpIncrease + '%',
  263. }
  264. }[this.find('.tab.selected').html()];
  265. for (var s in newStats) {
  266. var label = s + ': ';
  267. var value = newStats[s];
  268. var isGap = false;
  269. if (label.indexOf('gap') == 0) {
  270. isGap = true;
  271. label = '';
  272. value = '';
  273. }
  274. var row = $('<div class="stat"><font class="q0">' + label + '</font><font color="#999">' + value + '</font></div>')
  275. .appendTo(container);
  276. if (s == 'gold')
  277. row.addClass('gold');
  278. else if ((s == 'level') || (s == 'next level'))
  279. row.addClass('blueText');
  280. if (isGap)
  281. row.addClass('empty');
  282. }
  283. }
  284. };
  285. });