25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

644 satır
16 KiB

  1. define([
  2. 'js/system/events',
  3. 'js/system/client',
  4. 'html!ui/templates/inventory/template',
  5. 'css!ui/templates/inventory/styles',
  6. 'html!ui/templates/inventory/templateItem',
  7. 'html!ui/templates/inventory/templateTooltip',
  8. 'js/input'
  9. ], function (
  10. events,
  11. client,
  12. template,
  13. styles,
  14. tplItem,
  15. tplTooltip,
  16. input
  17. ) {
  18. return {
  19. tpl: template,
  20. centered: true,
  21. items: [],
  22. shiftDown: false,
  23. ctrlDown: false,
  24. dragItem: null,
  25. dragEl: null,
  26. hoverCell: null,
  27. modal: true,
  28. oldSpellsZIndex: 0,
  29. postRender: function () {
  30. this.onEvent('onGetItems', this.onGetItems.bind(this));
  31. this.onEvent('onDestroyItems', this.onDestroyItems.bind(this));
  32. this.onEvent('onShowInventory', this.toggle.bind(this));
  33. this.onEvent('onKeyDown', this.onKeyDown.bind(this));
  34. this.onEvent('onKeyUp', this.onKeyUp.bind(this));
  35. this.find('.grid')
  36. .on('mousemove', this.onMouseMove.bind(this))
  37. .on('mouseleave', this.onMouseDown.bind(this, null, null, false));
  38. this.find('.split-box .amount')
  39. .on('mousewheel', this.onChangeStackAmount.bind(this))
  40. .on('input', this.onEnterStackAmount.bind(this));
  41. this.find('.split-box').on('click', this.splitStackEnd.bind(this, true));
  42. this.find('.split-box .btnSplit').on('click', this.splitStackEnd.bind(this, null));
  43. this.find('.split-box .btnLess').on('click', this.onChangeStackAmount.bind(this, null, -1));
  44. this.find('.split-box .btnMore').on('click', this.onChangeStackAmount.bind(this, null, 1));
  45. },
  46. build: function () {
  47. let container = this.el.find('.grid')
  48. .empty();
  49. let items = this.items
  50. .filter(function (item) {
  51. return !item.eq;
  52. });
  53. let iLen = Math.max(items.length, 50);
  54. let rendered = [];
  55. for (let i = 0; i < iLen; i++) {
  56. let itemEl = null;
  57. let item = items.find(f => (f.pos !== null && f.pos === i));
  58. if (!item) {
  59. itemEl = $(tplItem)
  60. .appendTo(container);
  61. itemEl
  62. .on('mouseup', this.onMouseDown.bind(this, null, null, false))
  63. .on('mousemove', this.onHover.bind(this, itemEl, item))
  64. .on('mouseleave', this.hideTooltip.bind(this, itemEl, item))
  65. .children()
  66. .remove();
  67. continue;
  68. } else
  69. rendered.push(item);
  70. let imgX = -item.sprite[0] * 64;
  71. let imgY = -item.sprite[1] * 64;
  72. itemEl = $(tplItem)
  73. .appendTo(container);
  74. let spritesheet = item.spritesheet || '../../../images/items.png';
  75. if (!item.spritesheet) {
  76. if (item.material)
  77. spritesheet = '../../../images/materials.png';
  78. else if (item.quest)
  79. spritesheet = '../../../images/questItems.png';
  80. else if (item.type === 'consumable')
  81. spritesheet = '../../../images/consumables.png';
  82. }
  83. itemEl
  84. .data('item', item)
  85. .on('click', this.onClick.bind(this, item))
  86. .on('mousedown', this.onMouseDown.bind(this, itemEl, item, true))
  87. .on('mouseup', this.onMouseDown.bind(this, null, null, false))
  88. .on('mousemove', this.onHover.bind(this, itemEl, item))
  89. .on('mouseleave', this.hideTooltip.bind(this, itemEl, item))
  90. .find('.icon')
  91. .css('background', 'url(' + spritesheet + ') ' + imgX + 'px ' + imgY + 'px')
  92. .on('contextmenu', this.showContext.bind(this, item));
  93. if (item.quantity > 1 || item.eq || item.active || item.has('quickSlot')) {
  94. let elQuantity = itemEl.find('.quantity');
  95. let txtQuantity = item.quantity;
  96. if (!txtQuantity)
  97. txtQuantity = item.has('quickSlot') ? 'QS' : 'EQ';
  98. elQuantity.html(txtQuantity);
  99. //If the item doesn't have a quantity and we reach this point
  100. //it must mean that it's active, EQd or QSd
  101. if (!item.quantity)
  102. itemEl.addClass('eq');
  103. } else if (item.isNew) {
  104. itemEl.addClass('new');
  105. itemEl.find('.quantity').html('NEW');
  106. }
  107. }
  108. },
  109. onClick: function (item) {
  110. let msg = {
  111. item: item,
  112. success: true
  113. };
  114. events.emit('beforeInventoryClickItem', msg);
  115. if (!msg.success)
  116. return;
  117. if (!this.ctrlDown)
  118. return;
  119. client.request({
  120. cpn: 'social',
  121. method: 'chat',
  122. data: {
  123. message: '{' + item.name + '}',
  124. item: item
  125. }
  126. });
  127. },
  128. onMouseDown: function (el, item, down, e) {
  129. if (e.button !== 0)
  130. return;
  131. if (down) {
  132. this.dragEl = el.clone()
  133. .appendTo(this.find('.grid'))
  134. .hide()
  135. .on('mouseup', this.onMouseDown.bind(this, null, null, false))
  136. .addClass('dragging');
  137. this.dragItem = el;
  138. events.emit('onHideItemTooltip', this.hoverItem);
  139. this.hoverItem = null;
  140. } else if (this.dragItem) {
  141. let method = 'moveItem';
  142. if ((this.hoverCell) && (this.hoverCell[0] !== this.dragItem[0])) {
  143. let placeholder = $('<div></div>')
  144. .insertAfter(this.dragItem);
  145. this.dragItem.insertBefore(this.hoverCell);
  146. this.hoverCell.insertBefore(placeholder);
  147. placeholder.remove();
  148. let msgs = [{
  149. id: this.dragItem.data('item').id,
  150. pos: this.dragItem.index()
  151. }];
  152. this.items.find(function (i) {
  153. return (i.id === this.dragItem.data('item').id);
  154. }, this).pos = this.dragItem.index();
  155. let hoverCellItem = this.hoverCell.data('item');
  156. if (hoverCellItem) {
  157. if ((hoverCellItem.name !== this.dragItem.data('item').name) || (!hoverCellItem.quantity)) {
  158. msgs.push({
  159. id: hoverCellItem.id,
  160. pos: this.hoverCell.index()
  161. });
  162. this.items.find(function (i) {
  163. return (i.id === hoverCellItem.id);
  164. }, this).pos = this.hoverCell.index();
  165. } else {
  166. method = 'combineStacks';
  167. msgs = {
  168. fromId: this.dragItem.data('item').id,
  169. toId: hoverCellItem.id
  170. };
  171. }
  172. }
  173. client.request({
  174. cpn: 'player',
  175. method: 'performAction',
  176. data: {
  177. cpn: 'inventory',
  178. method: method,
  179. data: msgs
  180. }
  181. });
  182. this.build();
  183. }
  184. this.dragItem = null;
  185. this.dragEl.remove();
  186. this.dragEl = null;
  187. this.hoverCell = null;
  188. this.find('.hover').removeClass('hover');
  189. }
  190. },
  191. onMouseMove: function (e) {
  192. if (!this.dragEl)
  193. return;
  194. let offset = this.find('.grid').offset();
  195. this.dragEl.css({
  196. left: e.clientX - offset.left - 40,
  197. top: e.clientY - offset.top - 40,
  198. display: 'block'
  199. });
  200. },
  201. showContext: function (item, e) {
  202. let menuItems = {
  203. drop: {
  204. text: 'drop',
  205. callback: this.performItemAction.bind(this, item, 'dropItem')
  206. },
  207. destroy: {
  208. text: 'destroy',
  209. callback: this.performItemAction.bind(this, item, 'destroyItem')
  210. },
  211. salvage: {
  212. text: 'salvage',
  213. callback: this.performItemAction.bind(this, item, 'salvageItem')
  214. },
  215. stash: {
  216. text: 'stash',
  217. callback: this.performItemAction.bind(this, item, 'stashItem')
  218. },
  219. learn: {
  220. text: 'learn',
  221. callback: this.performItemAction.bind(this, item, 'learnAbility')
  222. },
  223. quickSlot: {
  224. text: 'quickslot',
  225. callback: this.performItemAction.bind(this, item, 'setQuickSlot')
  226. },
  227. activate: {
  228. text: 'activate',
  229. callback: this.performItemAction.bind(this, item, 'activateMtx')
  230. },
  231. use: {
  232. text: 'use',
  233. callback: this.performItemAction.bind(this, item, 'useItem')
  234. },
  235. equip: {
  236. text: 'equip',
  237. callback: this.performItemAction.bind(this, item, 'equip')
  238. },
  239. augment: {
  240. text: 'craft',
  241. callback: this.openAugmentUi.bind(this, item)
  242. },
  243. mail: {
  244. text: 'mail',
  245. callback: this.openMailUi.bind(this, item)
  246. },
  247. split: {
  248. text: 'split stack',
  249. callback: this.splitStackStart.bind(this, item)
  250. },
  251. divider: '----------'
  252. };
  253. if (item.eq) {
  254. menuItems.learn.text = 'unlearn';
  255. menuItems.equip.text = 'unequip';
  256. }
  257. if (item.active)
  258. menuItems.activate.text = 'deactivate';
  259. let config = [];
  260. if (item.ability)
  261. config.push(menuItems.learn);
  262. else if (item.type === 'mtx')
  263. config.push(menuItems.activate);
  264. else if (item.type === 'toy' || item.type === 'consumable') {
  265. config.push(menuItems.use);
  266. if (!item.has('quickSlot'))
  267. config.push(menuItems.quickSlot);
  268. } else if (item.slot) {
  269. config.push(menuItems.equip);
  270. if (!item.eq)
  271. config.push(menuItems.divider);
  272. if (!item.eq) {
  273. config.push(menuItems.augment);
  274. config.push(menuItems.divider);
  275. }
  276. }
  277. if ((!item.eq) && (!item.active)) {
  278. if (!item.quest) {
  279. if ((window.player.stash.active) && (!item.noStash))
  280. config.push(menuItems.stash);
  281. if (!item.noDrop)
  282. config.push(menuItems.drop);
  283. if ((!item.material) && (!item.noSalvage))
  284. config.push(menuItems.salvage);
  285. }
  286. if (!item.noDestroy)
  287. config.push(menuItems.destroy);
  288. }
  289. if (item.quantity > 1)
  290. config.push(menuItems.split);
  291. if ((!item.noDrop) && (!item.quest))
  292. config.push(menuItems.mail);
  293. if (config.length > 0)
  294. events.emit('onContextMenu', config, e);
  295. e.preventDefault();
  296. return false;
  297. },
  298. splitStackStart: function (item) {
  299. let box = this.find('.split-box').show();
  300. box.data('item', item);
  301. box.find('.amount')
  302. .val('1')
  303. .focus();
  304. },
  305. splitStackEnd: function (cancel, e) {
  306. let box = this.find('.split-box');
  307. if ((cancel) || (!e) || (e.target !== box.find('.btnSplit')[0])) {
  308. if ((cancel) && (!$(e.target).hasClass('button')))
  309. box.hide();
  310. return;
  311. }
  312. box.hide();
  313. client.request({
  314. cpn: 'player',
  315. method: 'performAction',
  316. data: {
  317. cpn: 'inventory',
  318. method: 'splitStack',
  319. data: {
  320. itemId: box.data('item').id,
  321. stackSize: ~~this.find('.split-box .amount').val()
  322. }
  323. }
  324. });
  325. },
  326. onChangeStackAmount: function (e, amount) {
  327. let item = this.find('.split-box').data('item');
  328. let delta = amount;
  329. if (e)
  330. delta = (e.originalEvent.deltaY > 0) ? -1 : 1;
  331. if (this.shiftDown)
  332. delta *= 10;
  333. let elAmount = this.find('.split-box .amount');
  334. elAmount.val(Math.max(1, Math.min(item.quantity - 1, ~~elAmount.val() + delta)));
  335. },
  336. onEnterStackAmount: function (e) {
  337. let el = this.find('.split-box .amount');
  338. let val = el.val();
  339. if (val !== ~~val)
  340. el.val('');
  341. else if (val) {
  342. let item = this.find('.split-box').data('item');
  343. if (val < 0)
  344. val = '';
  345. else if (val > item.quantity - 1)
  346. val = item.quantity - 1;
  347. el.val(val);
  348. }
  349. },
  350. hideTooltip: function () {
  351. if (this.dragEl) {
  352. this.hoverCell = null;
  353. return;
  354. }
  355. events.emit('onHideItemTooltip', this.hoverItem);
  356. this.hoverItem = null;
  357. },
  358. onHover: function (el, item, e) {
  359. if (this.dragEl) {
  360. this.hoverCell = el;
  361. this.find('.hover').removeClass('hover');
  362. el.addClass('hover');
  363. return;
  364. }
  365. if (item)
  366. this.hoverItem = item;
  367. else
  368. item = this.hoverItem;
  369. if (!item)
  370. return;
  371. let ttPos = null;
  372. if (el) {
  373. if (el.hasClass('new')) {
  374. el.removeClass('new');
  375. el.find('.quantity').html((item.quantity > 1) ? item.quantity : '');
  376. delete item.isNew;
  377. }
  378. ttPos = {
  379. x: ~~(e.clientX + 32),
  380. y: ~~(e.clientY)
  381. };
  382. }
  383. let compare = null;
  384. if (item.slot) {
  385. compare = this.items.find(function (i) {
  386. return ((i.eq) && (i.slot === item.slot));
  387. });
  388. // check special cases for mismatched weapon/offhand scenarios (only valid when comparing)
  389. if ((!compare) && (this.shiftDown)) {
  390. let equippedTwoHanded = this.items.find(function (i) {
  391. return ((i.eq) && (i.slot === 'twoHanded'));
  392. });
  393. let equippedOneHanded = this.items.find(function (i) {
  394. return ((i.eq) && (i.slot === 'oneHanded'));
  395. });
  396. let equippedOffhand = this.items.find(function (i) {
  397. return ((i.eq) && (i.slot === 'offHand'));
  398. });
  399. if (item.slot === 'twoHanded') {
  400. if (!equippedOneHanded)
  401. compare = equippedOffhand;
  402. else if (!equippedOffhand)
  403. compare = equippedOneHanded;
  404. else {
  405. // compare against oneHanded and offHand combined by creating a virtual item that is the sum of the two
  406. compare = $.extend(true, {}, equippedOneHanded);
  407. compare.refItem = equippedOneHanded;
  408. for (let s in equippedOffhand.stats) {
  409. if (!compare.stats[s])
  410. compare.stats[s] = 0;
  411. compare.stats[s] += equippedOffhand.stats[s];
  412. }
  413. }
  414. }
  415. if (item.slot === 'oneHanded')
  416. compare = equippedTwoHanded;
  417. // this case is kind of ugly, but we don't want to go in when comparing an offHand to (oneHanded + empty offHand) - that should just use the normal compare which is offHand to empty
  418. if ((item.slot === 'offHand') && (equippedTwoHanded)) {
  419. // since we're comparing an offhand to an equipped Twohander, we need to clone the 'spell' values over (setting damage to zero) so that we can properly display how much damage
  420. // the player would lose by switching to the offhand (which would remove the twoHander)
  421. // keep a reference to the original item for use in onHideToolTip
  422. let spellClone = $.extend(true, {}, equippedTwoHanded.spell);
  423. spellClone.name = '';
  424. spellClone.values.damage = 0;
  425. let clone = $.extend(true, {}, item, {
  426. spell: spellClone
  427. });
  428. clone.refItem = item;
  429. item = clone;
  430. compare = equippedTwoHanded;
  431. }
  432. }
  433. }
  434. events.emit('onShowItemTooltip', item, ttPos, compare, false, this.shiftDown);
  435. },
  436. onGetItems: function (items, rerender) {
  437. this.items = items;
  438. if ((this.shown) && (rerender))
  439. this.build();
  440. },
  441. onDestroyItems: function (itemIds) {
  442. itemIds.forEach(function (id) {
  443. let item = this.items.find(i => i.id === id);
  444. if (item === this.hoverItem)
  445. this.hideTooltip();
  446. this.items.spliceWhere(i => i.id === id);
  447. }, this);
  448. if (this.shown)
  449. this.build();
  450. },
  451. toggle: function (show) {
  452. this.shown = !this.el.is(':visible');
  453. if (this.shown) {
  454. this.find('.split-box').hide();
  455. this.show();
  456. this.build();
  457. } else {
  458. this.hide();
  459. events.emit('onHideInventory');
  460. events.emit('onHideContextMenu');
  461. }
  462. this.hideTooltip();
  463. },
  464. beforeDestroy: function () {
  465. this.el.parent().css('background-color', 'transparent');
  466. this.el.parent().removeClass('blocking');
  467. },
  468. beforeHide: function () {
  469. if (this.oldSpellsZIndex) {
  470. $('.uiSpells').css('z-index', this.oldSpellsZIndex);
  471. this.oldSpellsZIndex = null;
  472. }
  473. },
  474. performItemAction: function (item, action) {
  475. if (!item)
  476. return;
  477. else if ((action === 'equip') && ((item.material) || (item.quest) || (item.type === 'mtx') || (!window.player.inventory.canEquipItem(item))))
  478. return;
  479. else if ((action === 'learnAbility') && (!window.player.inventory.canEquipItem(item)))
  480. return;
  481. else if ((action === 'activateMtx') && (item.type !== 'mtx'))
  482. return;
  483. let data = item.id;
  484. let cpn = 'inventory';
  485. if (['equip', 'setQuickSlot'].includes(action)) {
  486. cpn = 'equipment';
  487. if (action === 'setQuickSlot') {
  488. data = {
  489. itemId: item.id,
  490. slot: 0
  491. };
  492. }
  493. }
  494. if (action === 'useItem')
  495. this.hide();
  496. client.request({
  497. cpn: 'player',
  498. method: 'performAction',
  499. data: {
  500. cpn: cpn,
  501. method: action,
  502. data: data
  503. }
  504. });
  505. },
  506. openAugmentUi: function (item) {
  507. events.emit('onSetSmithItem', {
  508. item: item
  509. });
  510. },
  511. openMailUi: function (item) {
  512. events.emit('onSetMailItem', {
  513. item: item
  514. });
  515. },
  516. onKeyDown: function (key) {
  517. if (key === 'i')
  518. this.toggle();
  519. else if (key === 'shift') {
  520. this.shiftDown = true;
  521. if (this.hoverItem)
  522. this.onHover();
  523. } else if (key === 'ctrl')
  524. this.ctrlDown = true;
  525. },
  526. onKeyUp: function (key) {
  527. if (key === 'shift') {
  528. this.shiftDown = false;
  529. if (this.hoverItem)
  530. this.onHover();
  531. } else if (key === 'ctrl')
  532. this.ctrlDown = false;
  533. }
  534. };
  535. });