In my game, I implemented an inventory system. When you click on the screen, MousePressedEvent goes through all layers in the game, to all objects that inherit EventListener (My EventListener ). The EventListener class EventListener fine, and using it as shown below, I managed to get my inventory so that you can remove items from the slot and return them back. However, I would like to extract them from any slot containing the elements and place them in any other slot (if the target slot is empty). I thought that I would allow this, since in the if I did not check that if a slot is selected, I add it to the slot independently. But it doesnβt actually work. Any ideas?
Code in Slot.java class:
public boolean onMousePressed(MousePressedEvent e) { Point p = new Point(Mouse.getX(), Mouse.getY()); if (!this.getBounds().contains(p)) return false; boolean left = (e.getButton() == MouseEvent.BUTTON1); boolean right = (e.getButton() == MouseEvent.BUTTON3); boolean hasItems = (items.size() > 0); if (this.getBounds().contains(p)){ if (right && !selected && hasItems){ select(true); s = new Slot(new Vector2i(Mouse.getX(), Mouse.getY())); addComponent(s); s.add(items.get(0)); remove(items.get(items.size() - 1)); } else if (right && selected){ s.add(items.get(0)); remove(items.get(items.size() - 1)); if (items.size() == 0) { setBackgroundImage(ImageUtil.getImage("/ui/panels/inventory/slot.png")); selected = false; return true; } return true; } else if ((left || right) && s==null) { return true; } else if (left && s != null){
In pseudo code:
If (Mouse is clicked) : if (the mouse isn't the bounds of the slot) return false (alert we haven't handled the event) if (we contain the mouse cursor) : if (right is pressed and we aren't selected) : select create a temporary slot at the mouse location remove item from this slot add it to the temporary slot return true else if (right is pressed and we are selected) : add item to temporary slot remove item from selected slot return true else if (we press left or right while temporary slot is null) : return true (tell the dispatcher we have handled the event) //This following else if statement is supposed to add an item to a clicked slot whether that slot is selected or not, but doesn't work else if (left is pressed and temporary slot isn't null) : add the item to the clicked slot remove it from the temporary one return true return false if none of the above applies
Thanks:)
source share