How can I manage the elements jme3 through nifty?

hi guy,

I want to create an app that work with textfield and button. Based on what I write in the textfield, when I press the button, the application reply.

I tried to create the text and button by Nifty and the app I inizializate in simpleInitApp(). Now, how can I manage the element jme3 through nifty? What is the best solution for my problem?? For you it is better to use the elements swing?

thanks!!!

For you it is better to use the elements swing?


YAY! Never ;).

The best solution for this is to create your nifty controllers like app states, and attach them to the stateManager. Then your nifty controller will have access to the app and stateManager objects. Read this:

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:nifty_gui_scenarios#get_access_to_application_and_update_loop

how do you convert an Element in a textfield?

for example, I have in element a text

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

now, in niftyElement, what is there? How do I do to appear a box of colour red, or blue, if I write in textfield blue?

Is it possible the casting from Element to String?

thanks!

It is in manual :



http://i.imgur.com/5crC8.png



Then instead of using the setter method use the getter.



The tutorial is this : https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:nifty_gui_java_interaction#java_modifies_nifty_elements_and_events



You can read the tutorials directly in jMP, just type F1.

hi,

I write an exercise-app to learn the comunication between a nifty element and a jme element. I set a number(String) that when arrive at 109 change the color at cube. but I want to change the color at the cube with a textfield( I write in textfield a color and the cube changes color). I have some problem to learn the tutorials because I am italian… Can anybody to alter my code of class ? If it is more light, I send all project…

thanks thanks thanks…



[java]

package tutorial;

import com.jme3.app.Application;

import com.jme3.app.state.AbstractAppState;

import com.jme3.app.state.AppStateManager;

import com.jme3.asset.AssetManager;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Vector3f;

import com.jme3.scene.Geometry;

import com.jme3.scene.Node;

import com.jme3.scene.shape.Box;

import de.lessvoid.nifty.Nifty;

import de.lessvoid.nifty.NiftyMethodInvoker;

import de.lessvoid.nifty.controls.textfield.TextFieldLogic;

import de.lessvoid.nifty.elements.Element;

import de.lessvoid.nifty.elements.render.ImageRenderer;

import de.lessvoid.nifty.elements.render.TextRenderer;

import de.lessvoid.nifty.render.NiftyImage;

import de.lessvoid.nifty.screen.Screen;

import de.lessvoid.nifty.screen.ScreenController;



public class MyStartScreen extends AbstractAppState implements ScreenController {



private Nifty nifty;

Node pivot;

AssetManager assetManager;

private Application app;

private Screen screen;

int i=0;



public MyStartScreen(Node pivot, AssetManager assetManager) {



this.pivot = pivot;

this.assetManager= assetManager;

}

public void sayHello(String din){

System.out.println(“Nifty dice:”+din);

Element textfieeld = nifty.getCurrentScreen().findElementByName(“textback”);



try{

//String qq = textfieeld.getRenderer(TextRenderer.class).getOriginalText();//nothing error, but doesn’t work! :frowning:

/this.textField.initWithText(textElement.getRenderer(TextRenderer.class).getOriginalText());// doesn’t work! :frowning:

//!!!run, but doesn’t go well!!!

String qw = textfieeld.getRenderer(TextRenderer.class).getWrappedText();

// String qw = textfieeld.initWithText(textfieeld.getRenderer(TextRenderer.class).getWrappedText());





System.out.println(“Nifty dice:”+qw);

}

catch(Exception e){

System.out.println(“error with textfield”);

}

}

public void gestionebutton(String myarg){

// find old text

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

// Element niftyElement = getElement().findElementByName("#testo1"); //test getElement()

// swap old with new text

//how it set in a string the new test that i insert in a textfield?then from string in int…it is easy!

i++;

niftyElement.getRenderer(TextRenderer.class).setText(“10”+i);

String q = niftyElement.getRenderer(TextRenderer.class).getOriginalText();

System.out.println(q+" questo la label con il numero che incremento");

//String q = niftyElement.getRenderer(TextRenderer.class).getText();//nooooooooo

System.out.println("Nifty says "+myarg);//niftyElement.getRenderer(TextRenderer.class).getText());

if(q.equals(“109”)){

System.out.println(“entrato nell’if”);

CuboAction cubo = new CuboAction(assetManager, pivot,“ciao”,-2f, 0f, 1f);

}



// load or create new image

NiftyImage img = nifty.getRenderEngine().createImage(

“Interface/yang.png”, false);

// find old image

Element niftyElement2 = nifty.getCurrentScreen().findElementByName(“playerhealth”);

// swap old with new image

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

//Similarly, to change the onClick() event of an element, create an ElementInteraction object:

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

//maybe this is the right way…

//niftyElement3.getElementInteraction().getPrimary().setOnMouseOver(new NiftyMethodInvoker(nifty, “changejme()”, this));

}

public void startGame(String nextScreen) {

nifty.gotoScreen(nextScreen); // switch to another screen

}



public void quitGame() {

app.stop();

}



public String getPlayerName() {

return System.getProperty(“user.name”);

}



/
* Nifty GUI ScreenControl methods /

public void bind(Nifty nifty, Screen screen) {

this.nifty = nifty;

this.screen = screen;

}



public void onStartScreen() {

}



public void onEndScreen() {

}



/
* jME3 AppState methods /

@Override

public void initialize(AppStateManager stateManager, Application app) {

this.app = app;

}



@Override

public void update(float tpf) {

if (screen.getScreenId().equals(“hud”)) {

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

niftyElement.getRenderer(TextRenderer.class).setText(tpf
1000/10 + “”); // fake score

}

}

}

[/java]

Nifty Text Fields’ events works with EventBus, it means you have to subscribre your TextField by using the @NiftyEventSubscriber annotation. More details can be found here : http://sourceforge.net/apps/mediawiki/nifty-gui/index.php?title=Nifty_Standard_Controls_%28Nifty_1.3%29

hi,

perhaps I much to ask, but is very difficult, is there somewhere a complete project? I am desperately…

how can I reward? thanks…! how does it set the string included in textfield in a string variable?

No. There aren’t any complete project, but there are a bunch of examples on nifty homepage…

Btw It’s very easy, Are you blind or can’t you read xD?



Put it in your nifty control :



[java]

@NiftyEventSubscriber(id = “score”)

public void onScoreChanged(final String id, final TextFieldChangedEvent event) {

TextField score = event.getTextFieldControl();

String text = event.getText();

}

[/java]



The same thing for the others controls which supports EventBus. You can find the controls which supports EventBus in link above. Here is how you do it for Dropdowns:



[java]

@NiftyEventSubscriber(id = “myDropdown”)

public void onMyDropdownSelectionChanged(final String id, final DropDownSelectionChangedEvent<MyDropDownModel> event) {

MyDropDownModel selected = event.getSelection();

//do something with your drop down model

}

[/java]



Aditionally, you can create one event method for several controls which shares the same control class. Like this:



[java]

@NiftyEventSubscriber(pattern = “.*TextField”)

public void onTextFieldChanged(final String id, final TextFieldChangedEvent event) {

TextField someTextField = event.getTextFieldControl();

String text = event.getText();

//You will know how is the text field by the id

}

[/java]