Two new nifty interaction snippets added to the wiki. Change image, change interaction

I’ve added two snippets of information on how to interact with nifty.



One is about changing the image of an element, which is taken from the official nifty blog, but i thought it could be nice to have in jme3’s wiki too.



The second is investigated by myself, on how to change the interaction element.



You can also alter the appearance and functions of your nifty elements from java.



Here’s an example of how to change an image:



[java]NiftyImage img = nifty.getRenderEngine().createImage(“Interface/Images/image.png”, false);

Element niftyElement = nifty.getCurrentScreen().findElementByName(“myElement”);

niftyElement.getRenderer(ImageRenderer.class).setImage(img);[/java]



To change the onClick() event of an element:



[java]Element niftyElement = nifty.getCurrentScreen().findElementByName(“myElement”);

niftyElement.setInteraction(new ElementInteraction(nifty) {



@Override

public void onClick() {

// call java functions normally.

niftyController.onElementClicked();

super.onClick();

}



@Override

public boolean onClick(MouseInputEvent inputEvent) {

niftyController.onElementClicked();

return super.onClick(inputEvent);

}



});[/java]



For this to work, there already needs to be an tag inside your xml element:



[xml][/xml]

2 Likes

Just a head’s up here.



the “findElementByName()” method here will look for “myElement” in IDs among the controls and panels and layers. It will NOT search in the “name” tags.



So, if you have something defined like so:



[java]

⪪label id=“hint_text” name=“text” font=“aurulent-sans-17.fnt” color="#ffff" text=“Please make a selection by clicking on the appropriate button.” align=“center” valign=“center”>

[/java]

and your method looks for “text”, it will not work. It only will find “hint_text” for that element.



So, to resume, that method should be called findElementByID.



Among all the examples I have found I often found the name=“somename”, so I naturally thought this method would look for name tags, as it happens in CSS. Maybe using both would be better? findElementByID() and findElementByName() each looking for its proper tag.



Anyway. Hopefully that might help someone.

One more thing to the onclick-Event:

You can also change the Method that is called when the Element is clicked:



[java]Element el = nifty.getCurrentScreen().findElementByName(“myElement”);

NiftyMethodInvoker invoker = new NiftyMethodInvoker(

nifty,

“method()”,

nifty.getCurrentScreen().getScreenController());

el.getInteraction().setOnClickMethod(invoker, true);[/java]



Maybe someone thinks it’s helpful :wink:

1 Like

I don’t know if it’s related, but you can use AssetCache when image isn’t on filesystem:



[java]

private void setNiftyImage(Element niftyElement,BufferedImage b) {

TextureKey key =new TextureKey(“textures/Monkey.jpg”,false);

Texture tex = assetManager.loadTexture(key);

tex.setAnisotropicFilter(16);

tex.setMagFilter(MagFilter.Bilinear.Bilinear);

AWTLoader loader =new AWTLoader();



Image imageJME=null;



imageJME = loader.load(b, true);

tex.setImage(imageJME);

((DesktopAssetManager)assetManager).addToCache(new AssetKey(“pippo”), tex);

NiftyImage img = nifty.getRenderEngine().createImage(“pippo”, false);



niftyElement.getRenderer(ImageRenderer.class).setImage(img);



}

[/java]