選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

581 行
13 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. 'js/input'
  8. ], function (
  9. events,
  10. client,
  11. template,
  12. styles,
  13. tplItem,
  14. input
  15. ) {
  16. return {
  17. tpl: template,
  18. centered: true,
  19. items: [],
  20. dragItem: null,
  21. dragEl: null,
  22. hoverCell: null,
  23. modal: true,
  24. hasClose: true,
  25. oldSpellsZIndex: 0,
  26. postRender: function () {
  27. this.onEvent('onGetItems', this.onGetItems.bind(this));
  28. this.onEvent('onDestroyItems', this.onDestroyItems.bind(this));
  29. this.onEvent('onShowInventory', this.toggle.bind(this));
  30. this.onEvent('onKeyDown', this.onKeyDown.bind(this));
  31. this.onEvent('onKeyUp', this.onKeyUp.bind(this));
  32. this.find('.grid')
  33. .on('mousemove', this.onMouseMove.bind(this))
  34. .on('mouseleave', this.onMouseDown.bind(this, null, null, false));
  35. this.find('.split-box .amount')
  36. .on('mousewheel', this.onChangeStackAmount.bind(this))
  37. .on('input', this.onEnterStackAmount.bind(this));
  38. this.find('.split-box').on('click', this.splitStackEnd.bind(this, true));
  39. this.find('.split-box .btnSplit').on('click', this.splitStackEnd.bind(this, null));
  40. this.find('.split-box .btnLess').on('click', this.onChangeStackAmount.bind(this, null, -1));
  41. this.find('.split-box .btnMore').on('click', this.onChangeStackAmount.bind(this, null, 1));
  42. },
  43. build: function () {
  44. let container = this.el.find('.grid')
  45. .empty();
  46. let items = this.items
  47. .filter(function (item) {
  48. return !item.eq;
  49. });
  50. let iLen = Math.max(items.length, 50);
  51. let rendered = [];
  52. for (let i = 0; i < iLen; i++) {
  53. let itemEl = null;
  54. let item = items.find(f => (f.pos !== null && f.pos === i));
  55. if (!item) {
  56. itemEl = $(tplItem)
  57. .appendTo(container);
  58. itemEl
  59. .on('mouseup', this.onMouseDown.bind(this, null, null, false))
  60. .on('mousemove', this.onHover.bind(this, itemEl, item))
  61. .on('mouseleave', this.hideTooltip.bind(this, itemEl, item))
  62. .children()
  63. .remove();
  64. continue;
  65. } else
  66. rendered.push(item);
  67. let imgX = -item.sprite[0] * 64;
  68. let imgY = -item.sprite[1] * 64;
  69. itemEl = $(tplItem)
  70. .appendTo(container);
  71. let spritesheet = item.spritesheet || '../../../images/items.png';
  72. if (!item.spritesheet) {
  73. if (item.material)
  74. spritesheet = '../../../images/materials.png';
  75. else if (item.quest)
  76. spritesheet = '../../../images/questItems.png';
  77. else if (item.type === 'consumable')
  78. spritesheet = '../../../images/consumables.png';
  79. }
  80. let clickHandler = this.onMouseDown.bind(this, itemEl, item, true);
  81. if (isMobile)
  82. clickHandler = this.onHover.bind(this, itemEl, item);
  83. itemEl
  84. .data('item', item)
  85. .on('click', this.onClick.bind(this, item))
  86. .on('mousedown', clickHandler)
  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 (!input.isKeyDown('ctrl', true))
  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' || item.useText) {
  265. if (item.useText)
  266. menuItems.use.text = item.useText;
  267. config.push(menuItems.use);
  268. if (!item.has('quickSlot'))
  269. config.push(menuItems.quickSlot);
  270. } else if (item.slot) {
  271. config.push(menuItems.equip);
  272. if (!item.eq)
  273. config.push(menuItems.divider);
  274. if (!item.eq) {
  275. config.push(menuItems.augment);
  276. config.push(menuItems.divider);
  277. }
  278. }
  279. if ((!item.eq) && (!item.active)) {
  280. if (!item.quest) {
  281. if ((window.player.stash.active) && (!item.noStash))
  282. config.push(menuItems.stash);
  283. if (!item.noDrop)
  284. config.push(menuItems.drop);
  285. if ((!item.material) && (!item.noSalvage))
  286. config.push(menuItems.salvage);
  287. }
  288. if (!item.noDestroy)
  289. config.push(menuItems.destroy);
  290. }
  291. if (item.quantity > 1)
  292. config.push(menuItems.split);
  293. if ((!item.noDrop) && (!item.quest))
  294. config.push(menuItems.mail);
  295. if (config.length > 0)
  296. events.emit('onContextMenu', config, e);
  297. if (isMobile)
  298. this.hideTooltip(null, this.hoverItem);
  299. e.preventDefault();
  300. return false;
  301. },
  302. splitStackStart: function (item) {
  303. let box = this.find('.split-box').show();
  304. box.data('item', item);
  305. box.find('.amount')
  306. .val('1')
  307. .focus();
  308. },
  309. splitStackEnd: function (cancel, e) {
  310. let box = this.find('.split-box');
  311. if ((cancel) || (!e) || (e.target !== box.find('.btnSplit')[0])) {
  312. if ((cancel) && (!$(e.target).hasClass('button')))
  313. box.hide();
  314. return;
  315. }
  316. box.hide();
  317. client.request({
  318. cpn: 'player',
  319. method: 'performAction',
  320. data: {
  321. cpn: 'inventory',
  322. method: 'splitStack',
  323. data: {
  324. itemId: box.data('item').id,
  325. stackSize: ~~this.find('.split-box .amount').val()
  326. }
  327. }
  328. });
  329. },
  330. onChangeStackAmount: function (e, amount) {
  331. let item = this.find('.split-box').data('item');
  332. let delta = amount;
  333. if (e)
  334. delta = (e.originalEvent.deltaY > 0) ? -1 : 1;
  335. if (input.isKeyDown('shift', true))
  336. delta *= 10;
  337. let elAmount = this.find('.split-box .amount');
  338. elAmount.val(Math.max(1, Math.min(item.quantity - 1, ~~elAmount.val() + delta)));
  339. },
  340. onEnterStackAmount: function (e) {
  341. let el = this.find('.split-box .amount');
  342. let val = el.val();
  343. if (+val !== ~~+val)
  344. el.val('');
  345. else if (val) {
  346. let item = this.find('.split-box').data('item');
  347. if (val < 0)
  348. val = '';
  349. else if (val > item.quantity - 1)
  350. val = item.quantity - 1;
  351. el.val(val);
  352. }
  353. },
  354. hideTooltip: function () {
  355. if (this.dragEl) {
  356. this.hoverCell = null;
  357. return;
  358. }
  359. events.emit('onHideItemTooltip', this.hoverItem);
  360. this.hoverItem = null;
  361. },
  362. onHover: function (el, item, e) {
  363. if (this.dragEl) {
  364. this.hoverCell = el;
  365. this.find('.hover').removeClass('hover');
  366. el.addClass('hover');
  367. return;
  368. }
  369. if (item)
  370. this.hoverItem = item;
  371. else
  372. item = this.hoverItem;
  373. if (!item)
  374. return;
  375. let ttPos = null;
  376. if (el) {
  377. if (el.hasClass('new')) {
  378. el.removeClass('new');
  379. el.find('.quantity').html((item.quantity > 1) ? item.quantity : '');
  380. delete item.isNew;
  381. }
  382. ttPos = {
  383. x: ~~(e.clientX + 32),
  384. y: ~~(e.clientY)
  385. };
  386. }
  387. events.emit('onShowItemTooltip', item, ttPos, true);
  388. },
  389. onGetItems: function (items, rerender) {
  390. this.items = items;
  391. if ((this.shown) && (rerender))
  392. this.build();
  393. },
  394. onDestroyItems: function (itemIds) {
  395. itemIds.forEach(function (id) {
  396. let item = this.items.find(i => i.id === id);
  397. if (item === this.hoverItem)
  398. this.hideTooltip();
  399. this.items.spliceWhere(i => i.id === id);
  400. }, this);
  401. if (this.shown)
  402. this.build();
  403. },
  404. toggle: function (show) {
  405. this.shown = !this.el.is(':visible');
  406. if (this.shown) {
  407. this.find('.split-box').hide();
  408. this.show();
  409. this.build();
  410. } else {
  411. this.hide();
  412. events.emit('onHideInventory');
  413. events.emit('onHideContextMenu');
  414. }
  415. this.hideTooltip();
  416. },
  417. beforeDestroy: function () {
  418. this.el.parent().css('background-color', 'transparent');
  419. this.el.parent().removeClass('blocking');
  420. },
  421. beforeHide: function () {
  422. if (this.oldSpellsZIndex) {
  423. $('.uiSpells').css('z-index', this.oldSpellsZIndex);
  424. this.oldSpellsZIndex = null;
  425. }
  426. },
  427. performItemAction: function (item, action) {
  428. if (!item)
  429. return;
  430. else if ((action === 'equip') && ((item.material) || (item.quest) || (item.type === 'mtx') || (!window.player.inventory.canEquipItem(item))))
  431. return;
  432. else if ((action === 'learnAbility') && (!window.player.inventory.canEquipItem(item)))
  433. return;
  434. else if ((action === 'activateMtx') && (item.type !== 'mtx'))
  435. return;
  436. let data = item.id;
  437. let cpn = 'inventory';
  438. if (['equip', 'setQuickSlot'].includes(action)) {
  439. cpn = 'equipment';
  440. if (action === 'setQuickSlot') {
  441. data = {
  442. itemId: item.id,
  443. slot: 0
  444. };
  445. }
  446. }
  447. if (action === 'useItem')
  448. this.hide();
  449. client.request({
  450. cpn: 'player',
  451. method: 'performAction',
  452. data: {
  453. cpn: cpn,
  454. method: action,
  455. data: data
  456. }
  457. });
  458. },
  459. openAugmentUi: function (item) {
  460. events.emit('onSetSmithItem', {
  461. item: item
  462. });
  463. },
  464. openMailUi: function (item) {
  465. events.emit('onSetMailItem', {
  466. item: item
  467. });
  468. },
  469. onKeyDown: function (key) {
  470. if (key === 'i')
  471. this.toggle();
  472. else if (key === 'shift' && this.hoverItem)
  473. this.onHover();
  474. },
  475. onKeyUp: function (key) {
  476. if (key === 'shift' && this.hoverItem)
  477. this.onHover();
  478. }
  479. };
  480. });