Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

503 Zeilen
11 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. var qualityColors = [{
  19. r: 252,
  20. g: 252,
  21. b: 252
  22. }, {
  23. r: 7,
  24. g: 170,
  25. b: 214
  26. }, {
  27. r: 255,
  28. g: 255,
  29. b: 0
  30. }, {
  31. r: 192,
  32. g: 0,
  33. b: 207
  34. }, {
  35. r: 255,
  36. g: 108,
  37. b: 4
  38. }];
  39. return {
  40. tpl: template,
  41. centered: true,
  42. items: [],
  43. shiftDown: false,
  44. ctrlDown: false,
  45. dragItem: null,
  46. dragEl: null,
  47. hoverCell: null,
  48. modal: true,
  49. oldSpellsZIndex: 0,
  50. postRender: function () {
  51. this.onEvent('onGetItems', this.onGetItems.bind(this));
  52. this.onEvent('onDestroyItems', this.onDestroyItems.bind(this));
  53. this.onEvent('onShowInventory', this.toggle.bind(this));
  54. this.onEvent('onKeyDown', this.onKeyDown.bind(this));
  55. this.onEvent('onKeyUp', this.onKeyUp.bind(this));
  56. this.find('.grid')
  57. .on('mousemove', this.onMouseMove.bind(this))
  58. .on('mouseleave', this.onMouseDown.bind(this, null, null, false));
  59. },
  60. build: function () {
  61. var container = this.el.find('.grid')
  62. .empty();
  63. var items = this.items
  64. .filter(function (item) {
  65. return !item.eq;
  66. });
  67. var iLen = Math.max(items.length, 50);
  68. var rendered = [];
  69. for (var i = 0; i < iLen; i++) {
  70. var item = items.find(function (item) {
  71. return ((item.pos != null) && (item.pos == i));
  72. });
  73. if (!item) {
  74. var itemEl = $(tplItem)
  75. .appendTo(container);
  76. itemEl
  77. .on('mouseup', this.onMouseDown.bind(this, null, null, false))
  78. .on('mousemove', this.onHover.bind(this, itemEl, item))
  79. .on('mouseleave', this.hideTooltip.bind(this, itemEl, item))
  80. .children()
  81. .remove();
  82. continue;
  83. } else {
  84. rendered.push(item);
  85. }
  86. var imgX = -item.sprite[0] * 64;
  87. var imgY = -item.sprite[1] * 64;
  88. var itemEl = $(tplItem)
  89. .appendTo(container);
  90. var spritesheet = item.spritesheet || '../../../images/items.png';
  91. if (!item.spritesheet) {
  92. if (item.material)
  93. spritesheet = '../../../images/materials.png';
  94. else if (item.quest)
  95. spritesheet = '../../../images/questItems.png';
  96. }
  97. itemEl
  98. .data('item', item)
  99. .on('click', this.onClick.bind(this, item))
  100. .on('mousedown', this.onMouseDown.bind(this, itemEl, item, true))
  101. .on('mouseup', this.onMouseDown.bind(this, null, null, false))
  102. .on('mousemove', this.onHover.bind(this, itemEl, item))
  103. .on('mouseleave', this.hideTooltip.bind(this, itemEl, item))
  104. .find('.icon')
  105. .css('background', 'url(' + spritesheet + ') ' + imgX + 'px ' + imgY + 'px')
  106. .on('contextmenu', this.showContext.bind(this, item));
  107. if (item.quantity)
  108. itemEl.find('.quantity').html(item.quantity);
  109. else if (item.eq)
  110. itemEl.find('.quantity').html('EQ');
  111. else if (item.active)
  112. itemEl.find('.quantity').html('EQ');
  113. if (item.eq)
  114. itemEl.addClass('eq');
  115. else if (item.isNew) {
  116. itemEl.addClass('new');
  117. itemEl.find('.quantity').html('NEW');
  118. }
  119. }
  120. },
  121. onClick: function (item) {
  122. var msg = {
  123. item: item,
  124. success: true
  125. };
  126. events.emit('beforeInventoryClickItem', msg);
  127. if (!msg.success)
  128. return;
  129. if (!this.ctrlDown)
  130. return;
  131. client.request({
  132. cpn: 'social',
  133. method: 'chat',
  134. data: {
  135. message: '{' + item.name + '}',
  136. item: item
  137. }
  138. });
  139. },
  140. onMouseDown: function (el, item, down, e) {
  141. if (e.button != 0)
  142. return;
  143. if (down) {
  144. this.dragEl = el.clone()
  145. .appendTo(this.find('.grid'))
  146. .hide()
  147. .on('mouseup', this.onMouseDown.bind(this, null, null, false))
  148. .addClass('dragging');
  149. this.dragItem = el;
  150. events.emit('onHideItemTooltip', this.hoverItem);
  151. this.hoverItem = null;
  152. } else if (this.dragItem) {
  153. if ((this.hoverCell) && (this.hoverCell[0] != this.dragItem[0])) {
  154. var placeholder = $('<div></div>')
  155. .insertAfter(this.dragItem);
  156. this.dragItem.insertBefore(this.hoverCell);
  157. this.hoverCell.insertBefore(placeholder);
  158. placeholder.remove();
  159. var msgs = [{
  160. id: this.dragItem.data('item').id,
  161. pos: this.dragItem.index()
  162. }];
  163. this.items.find(function (i) {
  164. return (i.id == this.dragItem.data('item').id)
  165. }, this).pos = this.dragItem.index();
  166. var hoverCellItem = this.hoverCell.data('item');
  167. if (hoverCellItem) {
  168. msgs.push({
  169. id: hoverCellItem.id,
  170. pos: this.hoverCell.index()
  171. });
  172. this.items.find(function (i) {
  173. return (i.id == hoverCellItem.id)
  174. }, this).pos = this.hoverCell.index();
  175. }
  176. client.request({
  177. cpn: 'player',
  178. method: 'performAction',
  179. data: {
  180. cpn: 'inventory',
  181. method: 'moveItem',
  182. data: msgs
  183. }
  184. });
  185. this.build();
  186. }
  187. this.dragItem = null;
  188. this.dragEl.remove();
  189. this.dragEl = null;
  190. this.hoverCell = null;
  191. this.find('.hover').removeClass('hover');
  192. }
  193. },
  194. onMouseMove: function (e) {
  195. if (!this.dragEl)
  196. return;
  197. var offset = this.find('.grid').offset();
  198. this.dragEl.css({
  199. left: e.clientX - offset.left - 40,
  200. top: e.clientY - offset.top - 40,
  201. display: 'block'
  202. });
  203. },
  204. showContext: function (item, e) {
  205. var menuItems = {
  206. drop: {
  207. text: 'drop',
  208. callback: this.performItemAction.bind(this, item, 'dropItem')
  209. },
  210. destroy: {
  211. text: 'destroy',
  212. callback: this.performItemAction.bind(this, item, 'destroyItem')
  213. },
  214. salvage: {
  215. text: 'salvage',
  216. callback: this.performItemAction.bind(this, item, 'salvageItem')
  217. },
  218. stash: {
  219. text: 'stash',
  220. callback: this.performItemAction.bind(this, item, 'stashItem')
  221. },
  222. learn: {
  223. text: 'learn',
  224. callback: this.performItemAction.bind(this, item, 'learnAbility')
  225. },
  226. activate: {
  227. text: 'activate',
  228. callback: this.performItemAction.bind(this, item, 'activateMtx')
  229. },
  230. use: {
  231. text: 'use',
  232. callback: this.performItemAction.bind(this, item, 'useItem')
  233. },
  234. equip: {
  235. text: 'equip',
  236. callback: this.performItemAction.bind(this, item, 'equip')
  237. },
  238. augment: {
  239. text: 'craft',
  240. callback: this.openAugmentUi.bind(this, item)
  241. },
  242. mail: {
  243. text: 'mail',
  244. callback: this.openMailUi.bind(this, item)
  245. },
  246. divider: '----------'
  247. };
  248. if (item.eq) {
  249. menuItems.learn.text = 'unlearn';
  250. menuItems.equip.text = 'unequip';
  251. }
  252. if (item.active)
  253. menuItems.activate.text = 'deactivate';
  254. var config = [];
  255. if (item.ability)
  256. config.push(menuItems.learn);
  257. else if (item.type == 'mtx')
  258. config.push(menuItems.activate);
  259. else if ((item.type == 'toy') || (item.type == 'consumable'))
  260. config.push(menuItems.use);
  261. else if (item.slot) {
  262. config.push(menuItems.equip);
  263. if (!item.eq)
  264. config.push(menuItems.divider);
  265. if (!item.eq) {
  266. config.push(menuItems.augment);
  267. config.push(menuItems.divider);
  268. }
  269. }
  270. if ((!item.eq) && (!item.active)) {
  271. if (!item.quest) {
  272. if ((window.player.stash.active) && (!item.noStash))
  273. config.push(menuItems.stash);
  274. if (!item.noDrop)
  275. config.push(menuItems.drop);
  276. if ((!item.material) && (!item.noSalvage))
  277. config.push(menuItems.salvage);
  278. }
  279. if (!item.noDestroy)
  280. config.push(menuItems.destroy);
  281. }
  282. if ((!item.noDrop) && (!item.quest) && (!item.noSalvage))
  283. config.push(menuItems.mail);
  284. if (config.length > 0)
  285. events.emit('onContextMenu', config, e);
  286. e.preventDefault;
  287. return false;
  288. },
  289. hideTooltip: function () {
  290. if (this.dragEl) {
  291. this.hoverCell = null;
  292. return;
  293. }
  294. events.emit('onHideItemTooltip', this.hoverItem);
  295. this.hoverItem = null;
  296. },
  297. onHover: function (el, item, e) {
  298. if (this.dragEl) {
  299. this.hoverCell = el;
  300. this.find('.hover').removeClass('hover');
  301. el.addClass('hover');
  302. return;
  303. }
  304. if (item)
  305. this.hoverItem = item;
  306. else
  307. item = this.hoverItem;
  308. if (!item)
  309. return;
  310. var ttPos = null;
  311. if (el) {
  312. if (el.hasClass('new')) {
  313. el.removeClass('new');
  314. el.find('.quantity').html(item.quantity || '');
  315. delete item.isNew;
  316. }
  317. var elOffset = el.offset();
  318. ttPos = {
  319. x: ~~(e.clientX + 32),
  320. y: ~~(e.clientY)
  321. };
  322. }
  323. var compare = null;
  324. if (item.slot) {
  325. compare = this.items.find(function (i) {
  326. return ((i.eq) && (i.slot == item.slot));
  327. });
  328. }
  329. events.emit('onShowItemTooltip', item, ttPos, compare, false, this.shiftDown);
  330. },
  331. onGetItems: function (items, rerender) {
  332. this.items = items;
  333. if ((this.shown) && (rerender))
  334. this.build();
  335. },
  336. onDestroyItems: function (itemIds) {
  337. itemIds.forEach(function (id) {
  338. var item = this.items.find(i => i.id == id);
  339. if (item == this.hoverItem)
  340. this.hideTooltip();
  341. this.items.spliceWhere(i => i.id == id);
  342. }, this);
  343. if (this.shown)
  344. this.build();
  345. },
  346. toggle: function (show) {
  347. this.shown = !this.el.is(':visible');
  348. if (this.shown) {
  349. this.show();
  350. this.build();
  351. } else {
  352. this.hide();
  353. events.emit('onHideInventory');
  354. events.emit('onHideContextMenu');
  355. }
  356. this.hideTooltip();
  357. },
  358. beforeDestroy: function () {
  359. this.el.parent().css('background-color', 'transparent');
  360. this.el.parent().removeClass('blocking');
  361. },
  362. beforeHide: function () {
  363. if (this.oldSpellsZIndex) {
  364. $('.uiSpells').css('z-index', this.oldSpellsZIndex);
  365. this.oldSpellsZIndex = null;
  366. }
  367. },
  368. performItemAction: function (item, action) {
  369. if (!item)
  370. return;
  371. else if ((action == 'equip') && ((item.material) || (item.quest) || (item.type == 'mtx') || (item.level > window.player.stats.values.level)))
  372. return;
  373. else if ((action == 'activateMtx') && (item.type != 'mtx'))
  374. return;
  375. if ((item.factions) && (action == 'equip')) {
  376. if (item.factions.some(function (f) {
  377. return f.noEquip;
  378. }))
  379. return;
  380. }
  381. var cpn = 'inventory';
  382. if (action == 'equip')
  383. cpn = 'equipment';
  384. client.request({
  385. cpn: 'player',
  386. method: 'performAction',
  387. data: {
  388. cpn: cpn,
  389. method: action,
  390. data: item.id
  391. }
  392. });
  393. },
  394. openAugmentUi: function (item) {
  395. events.emit('onSetSmithItem', {
  396. item: item
  397. });
  398. },
  399. openMailUi: function (item) {
  400. events.emit('onSetMailItem', {
  401. item: item
  402. });
  403. },
  404. onKeyDown: function (key) {
  405. if (key == 'i')
  406. this.toggle();
  407. else if (key == 'shift') {
  408. this.shiftDown = true;
  409. if (this.hoverItem)
  410. this.onHover();
  411. } else if (key == 'ctrl')
  412. this.ctrlDown = true;
  413. },
  414. onKeyUp: function (key) {
  415. if (key == 'shift') {
  416. this.shiftDown = false;
  417. if (this.hoverItem)
  418. this.onHover();
  419. } else if (key == 'ctrl')
  420. this.ctrlDown = false;
  421. }
  422. };
  423. });