[All Fixed] Nifty custom control problem

Started moving some stuff out from the main XML and converted those to custom controls.



I’m getting a crash with my “Inventory Grid” control when that control’s bind(…) method is called.



That control contains 77 inventory slots. 7 rows of 11 slots. The problem is Nifty seems to only load/read the first panel. The reminder 76 are not in the screen. :confused:



Here’s part of the custom control:

[xml]

<?xml version=“1.0” encoding=“UTF-8”?>

<nifty-controls>

<controlDefinition name=“inventoryGrid” childRootid="#panel-content" controller=“com.madjack.games.disenthral.gui.nifty.HUD” revert=“false” drop=“false” handle="#panel-frame">

<interact onMouseOver=“hideInventoryHint()” />

<!-- Inventory grid -->

<!-- First Row -->

<panel style="#panel" height=“48” width=“50” childLayout=“absolute” id="#inv01" x=“0” y=“0” >

<control id="#invDrp01" name=“droppable” width=“100%” height=“100%” childLayout=“absolute” x=“0” y=“0”>

<control id="#invDrg01" name=“draggable” x=“0” y=“0”>

<interact onMouseOver=“showHint(01)” />

</control>

</control>

</panel>

<panel style="#panel" height=“48” width=“50” childLayout=“absolute” id="#inv02" x=“51” y=“0” >

<control id="#invDrp02" name=“droppable” width=“100%” height=“100%” childLayout=“absolute” x=“0” y=“0” >

<control id="#invDrg02" name=“draggable” x=“0” y=“0”>

<interact onMouseOver=“showHint(02)” />

</control>

</control>

</panel>

<panel style="#panel" height=“48” width=“50” childLayout=“absolute” id="#inv03" x=“102” y=“0” >

<control id="#invDrp03" name=“droppable” width=“100%” height=“100%” childLayout=“absolute” x=“0” y=“0” >

<control id="#invDrg03" name=“draggable” x=“0” y=“0” >

<interact onMouseOver=“showHint(03)” />

</control>

</control>

</panel>

<… etc …>

</controlDefinition>

</nifty-controls>

[/xml]



Here’s the bind(…) method (which is actually in the screen controller).

[java]

@Override

public void bind(Nifty nifty, Screen screen, Element element, Properties parameter, Attributes controlDefinitionAttributes) {

for (int i = 1; i < 78; i++) {

DroppableControl dc = screen.findControl("#invDrp" + df.format(i), DroppableControl.class);

dc.addFilter(this); // <


LINE 1052 IS HERE
System.out.println("Got droppable " + "invDrp" + df.format(i));
}
}
[/java]

Here's the console output:

[java]
Loading HUD.
Got droppable invDrp01 // <--- First droppable is found and added to the filter.
Aug 10, 2012 11:14:00 AM com.jme3.app.Application handleError
SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.NullPointerException
at com.madjack.games.disenthral.gui.nifty.HUD.bind(HUD.java:1052)
at de.lessvoid.nifty.controls.NiftyInputControl.bindControl(NiftyInputControl.java:121)
at de.lessvoid.nifty.elements.Element.bindControls(Element.java:1783)
at de.lessvoid.nifty.elements.Element.bindControls(Element.java:1780)
at de.lessvoid.nifty.elements.Element.bindControls(Element.java:1780)
at de.lessvoid.nifty.elements.Element.bindControls(Element.java:1780)
at de.lessvoid.nifty.screen.Screen.bindControls(Screen.java:810)
at de.lessvoid.nifty.screen.Screen.startScreen(Screen.java:204)
at de.lessvoid.nifty.Nifty.gotoScreenInternal(Nifty.java:618)
at de.lessvoid.nifty.Nifty.gotoScreen(Nifty.java:586)
at de.lessvoid.nifty.Nifty.fromXml(Nifty.java:439)
at com.madjack.games.disenthral.ScreensManager.loadHUD(ScreensManager.java:491)
at com.madjack.games.disenthral.ScreensManager.processCommand(ScreensManager.java:290)
at com.madjack.games.disenthral.ScreensManager.update(ScreensManager.java:115)
at com.jme3.app.state.AppStateManager.update(AppStateManager.java:255)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:238)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:152)
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:181)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:229)
at java.lang.Thread.run(Thread.java:722)
[/java]

As you can see, the screen loads and adds the filter to the first invDrp01 but then suddenly chokes on the second.

Using a breakpoint I checked the content of the loaded screen and effectively, only one of those is in memory.

The XML has been externally validated (here) and it also has been internally validated using nifty.validateXml("Interface/hud.xml"); No error output.

I'm at a loss to explain that behavior.

Anyone has a suggestion?

Thanks.

Took a while to find, but the problem is that you have to wrap things in a panel.



So, by adding a panel tag to wrap around all inventory slots fixed the issue.



What I don’t understand is the logic behind this.



Oh well, it works now.

Seems I’m getting a second issue.



I dynamically add an image in a slot when the player is given an item. Since I’ve switched to custom control the image isn’t displayed. There’s no error. I can add the image and even remove it without as much as a warning, it’s just not there.



The code to add an icon:

[java]

private void addImage(String slot, final String filename) {

// Only addToFreeSlot if there’s nothing there already.

if (screen.findElementByName(“i” + slot) != null) {

return;

}

Element draggable = screen.findElementByName(“invDrg” + df.format(Integer.valueOf(slot)));

ImageBuilder ib = new ImageBuilder(“i” + df.format(Integer.valueOf(slot))) {



{

filename(filename);

x(“0”);

y(“0”);

width(“50”);

height(“48”);

}

};

ib.build(nifty, screen, draggable);

draggable.getParent().layoutElements();

}

[/java]



An example of inventory slot to add the image on:

[xml]

<panel height=“48” width=“50” childLayout=“absolute” id=“inv01” x=“0” y=“0” >

<control id=“invDrp01” name=“droppable” width=“100%” height=“100%” childLayout=“absolute” x=“0” y=“0”>

<control id=“invDrg01” name=“draggable” x=“0” y=“0”>

<interact onMouseOver=“showHint(01)” />

</control>

</control>

</panel>

[/xml]



I tried changing the id of the control to start with a “#” but got the same result.



Ideas? Suggestions?



Thanks.

Looks like everything has to be put into a damn panel… Even if the way it was before it was a custom control worked fine.

1 Like

now add it to the wiki :slight_smile: <3