From 0e69501aec942a7ceedf17eba99d82eeeb55d9ef Mon Sep 17 00:00:00 2001 From: VegaZ Date: Sat, 8 Dec 2018 19:42:43 +0100 Subject: [PATCH] Code Review --- Client/Gui/Inventory/inventory.js | 69 +++++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 18 deletions(-) diff --git a/Client/Gui/Inventory/inventory.js b/Client/Gui/Inventory/inventory.js index 52cfaf4d..9cbe06ff 100644 --- a/Client/Gui/Inventory/inventory.js +++ b/Client/Gui/Inventory/inventory.js @@ -223,6 +223,35 @@ function isMouseOverFreeSlot(cX2, cY2) { } } +function isMouseOverSameItemType(cX2, cY2) { + + var x = cX2 / screenX; + var y = cY2 / screenY; + var a; + var b; + var c; + + for (var s = 0; s < 20; s++) { + a = grid[s][0] - x; + b = grid[s][1] - y; + c = Math.sqrt(a * a + b * b); + + var invSlot = (s + 1); + + if (c < sizeMul / 2) { + for (i = 0; i < items.length; i++) { + if (parseInt(items[i][4]) === invSlot) { + isItemOnSlot++; + } + } + if (isItemOnSlot === 0) { + actFreeSlot = s + 1; + return true; + } + } + } +} + mp.events.add('click', (x, y, upOrDown, leftOrRight, relativeX, relativeY, worldPosition, hitEntity) => { var cX = x / screenX; @@ -247,15 +276,17 @@ mp.events.add('click', (x, y, upOrDown, leftOrRight, relativeX, relativeY, world mouseLDown = false; - if (isMouseOverFreeSlot(x, y) && dragItem !== null) { - items[dragItem][4] = actFreeSlot; - oldDragSlot = null; - dragItem = null; - } else { - if (dragItem !== null) { + if (dragItem !== null) { + if (isMouseOverFreeSlot(x, y)) { + items[dragItem][4] = actFreeSlot; + oldDragSlot = null; + dragItem = null; + hoverItem = null; + } else { items[dragItem][4] = oldDragSlot; dragItem = null; - } + hoverItem = null; + } } } @@ -266,12 +297,9 @@ mp.events.add('click', (x, y, upOrDown, leftOrRight, relativeX, relativeY, world mouseRDown = true; if (isMouseOverItem(x, y) && dragItem === null) { - dragItem = hoverItem; + dragItem = hoverItem; + bufferItem = [items[hoverItem][0], items[hoverItem][1], items[hoverItem][2], items[hoverItem][3], items[hoverItem][4]] items[hoverItem][3]--; - if (items[hoverItem][3] === "0") { - bufferItem = [items[hoverItem][0], items[hoverItem][1], items[hoverItem][2], items[hoverItem][3], items[hoverItem][4]] - items.splice(items.indexOf(items[hoverItem]), 1); - } } } @@ -280,15 +308,20 @@ mp.events.add('click', (x, y, upOrDown, leftOrRight, relativeX, relativeY, world mouseRDown = false; - if (isMouseOverFreeSlot(x, y) && dragItem !== null) { - - items.push([items[dragItem][0], items[dragItem][1], items[dragItem][2], "1", actFreeSlot.toString()]); - dragItem = null; - } else { - if (dragItem !== null) { + if (dragItem !== null) { + if (isMouseOverFreeSlot(x, y)) { + items.push([items[dragItem][0], items[dragItem][1], items[dragItem][2], "1", actFreeSlot.toString()]); + + if (items[hoverItem][3] === "0") { + items.splice(items.indexOf(items[hoverItem]), 1); + } + dragItem = null; + hoverItem = null; + } else { items.push(bufferItem); bufferItem = null; dragItem = null; + hoverItem = null; } } }