NiftyGui Attaching Element to Mouse

Still working on this inventory system (thanks again for everyone who has helped so far). My next problem is splitting item stacks. The actual splitting is going fine. As a part of the split, I create a new Element with half of the original stack’s size and a parent of the inventory layer (btw, is this a good spot to put the new element, or are there other suggestions?).

My problem is I’d like to have this new Element stuck to the mouse until I click on an empty slot. Currently the new stack just sits on the inventory layer until I click and drag it.

Here’s some code:

[java]public class ItemController extends DraggableControl
private boolean m_splitting = false;
private String m_image = null;
private String m_usage = null;
private String m_tooltip = null;
private int m_amount = 0;

public void initialize(String image, String usage, String tooltip, String amount)
this.draggable.findElementById("#image").getRenderer(ImageRenderer.class).setImage(nifty.getRenderEngine().createImage(image, false));
m_image = image;
m_usage = usage;
m_tooltip = tooltip;
m_amount = Integer.parseInt(amount);

public void onClick(final int mouseX, final int mouseY)
if (m_splitting && m_amount > 1)
int stack1 = 0;
int stack2 = 0;
stack1 = m_amount / 2;
stack2 = (int) Math.round(m_amount / 2.0);

  Element parent = this.screen.findElementById("inventoryLayer");
  Element e = new CustomControlCreator("InventoryItem").create(this.nifty, this.screen, parent);
  e.getControl(ItemController.class).initialize(m_image, m_usage, m_tooltip, Integer.toString(stack2));
  // FIXME: attach new Element to mouse somehow??



And some XML snippets:
[xml]<controlDefinition name=“InventoryItem”>
<control name=“draggable” width=“64px” height=“64px” revert=“true” childLayout=“center” inputMapping=“inventory.InventoryInputMapping” controller=“inventory.ItemController” style=“nifty-panel” focusable=“true”>
<image id="#image" filename=“Icons/Error.png” width=“64px” height=“64px”/>
<text id="#text" text=“1” font=“Interface/Fonts/Default.fnt” valign=“bottom” align=“right” width=“100%”/>
<interact onClick=“onClick()” onSecondaryClick=“onSecondaryClick()” onMouseOver=“onHover()”/>
<onHover name=“InventoryTooltip” targetElement=“tooltipPanel”/>

<controlDefinition name=“InventoryWindow”>
<control name=“window” width=“300px” height=“74px” visible=“false” hideOnClose=“true”>
<panel id="#bagPanel" childLayout=“absolute”/>

<screen id=“inventory” controller=“inventory.InventoryScreenController”>
<layer id=“inventoryLayer” childLayout=“absolute”>
<control id=“inventoryWindow_1” name=“InventoryWindow” title=“Bag 1”/>
<control id=“inventoryWindow_2” name=“InventoryWindow”/>
<control id=“inventoryWindow_3” name=“InventoryWindow”/>
<control id=“inventoryWindow_4” name=“InventoryWindow”/>
<control id=“inventoryWindow_5” name=“InventoryWindow”/>
<layer id=“tooltipLayer” childLayout=“absolute”>
<panel id=“tooltipPanel” visible=“false” childLayout=“vertical” padding=“20px,50px,20px,50px” backgroundColor="#0000">
<text id=“content” font=“Interface/Fonts/Default.fnt” text=“empty” align=“center” valign=“center” />

If I remember correctly, there is a method for draggable that starts the “drag” that you could pattern your call after.

1 Like

Looks much better now. Thanks!