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.
 
 
 

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