public void addItem(InventoryItem item, Vector2?forceCellPosition = null) { var inventoryNode = item.getInventoryNode(); inventoryNode.setCellPosition(calculateNextOpenCellPosition(forceCellPosition)); var position = this.cellPositionToLocal(inventoryNode.getCellPosition()); inventoryNode.MarginLeft = position.x; inventoryNode.MarginTop = position.y; if (!this.hasItem(item)) { this.items.Add(item); } var parent = inventoryNode.GetParentOrNull <Node>(); if (parent != null) { if (parent != this) { parent.RemoveChild(inventoryNode); } else { /** * already at the right place, no need to change anything */ } } else { this.AddChild(inventoryNode); } }
public void setPickedUpItem(InventoryItem item = null, InventoryBag bag = null, bool ignoreJustPickedUpItem = false) { if (pickedUpItem != null) { /** * if we already had an item picked up but we are either * dropping it, or switching it to another item, then * notify the item that this is happening */ pickedUpItem.getInventoryNode().onDrop(); var parent = pickedUpItem.getInventoryNode().GetParentOrNull <Node>(); if (parent != null) { parent.RemoveChild(pickedUpItem.getInventoryNode()); } if (bag != null) { bag.AddChild(pickedUpItem.getInventoryNode()); } if (this.mouseDragNode != null) { this.RemoveChild(mouseDragNode); } } if (item != null) { /** * notify the item that it was picked up */ item.getInventoryNode().onPickup(bag); var parent = item.getInventoryNode().GetParentOrNull <Node>(); if (parent != null) { parent.RemoveChild(item.getInventoryNode()); } this.mouseDragNode = (Control)(item.getInventoryNode().getMouseDragNode().Duplicate()); this.mouseDragNode.SetMouseFilter(Control.MouseFilterEnum.Ignore); this.mouseDragNode.SetGlobalPosition(this.GetViewport().GetMousePosition() - (item.getInventoryNode().GetCustomMinimumSize() / 2)); this.AddChild(this.mouseDragNode); } pickedUpItem = item; if (pickedUpItem != null && !ignoreJustPickedUpItem) { justPickedUpItem = true; } }
public override void _Input(InputEvent @event) { if (@event is InputEventMouseMotion eventMouseMotion) { if (this.hasPickedUpItem()) { //this.AcceptEvent(); //handled in _GuiInput instead InventoryItem item = this.getPickedUpItem(); this.mouseDragNode.SetGlobalPosition(eventMouseMotion.GetGlobalPosition() - (item.getInventoryNode().GetCustomMinimumSize() / 2)); } } }