JME3 eating nifty input

Hi Guys,

I’m trying to make a chat box that pops up only when you hit a certain key, then closes when you hit enter. The only problem I have is that I have spacebar bound to jump in jme and it’s making nifty not accept spaces into the textfield. Any ideas how to fix this?

Thanks,

Set a flag for GUI focus… and split your logic?

Or…

Use:

[java]
ChatBoxExt chatWin = new ChatBoxExt(screen, new Vector2f(10,10));
screen.addElement(chatWin);
[/java]

Which would do all this for you. (tonegodGUI plugin… save yourself 6000 hours worth of trying to get Nifty to cooperate)

If I want to roll my own chatbox with tonegodgui will I have the same issue?

Does tonegodgui support 9patchs?

@bgilb said: If I want to roll my own chatbox with tonegodgui will I have the same issue?

Does tonegodgui support 9patchs?

It does support 9-patch (actually all elements are 9-patch).

By default this issue would be easier to deal with, as events are JME event that are filtered through the guiNode > rootNode.

It would still require a single if statement in the onAction portion of your event listener.

  1. Space bar trigger happens
  2. if (guiFocus) { dont jump } else { jump }

Hmmm… wonder who had an issue with me suggesting an alternate approach to your gui. You don’t have to use that suggestion, of course… though it is a TON easier to implement than Nifty.

EDIT: When I say issue… I’m referring to events in general. This has been a long time issue with JME + Nifty. It has gotten MUCH better, just a familiar topic is all.

Actually, in all fairness, me saying this library will make your life easier without showing you is unfair.

If you have a moment, give me you design criteria for the chatbox, I’ll provide you with an example using the library… you can review the code, compare it to what you have had to do to make this work in Nifty and decide for yourself.

Is there no easier way in tonegodgui to have a method like, for now consider only inputs that come through tonegodgui?

Seems weird to have ifs all over the place just for a chatbox. Am i got to have to do this for W moving forward too?

@bgilb said: Is there no easier way in tonegodgui to have a method like, for now consider only inputs that come through tonegodgui?

Seems weird to have ifs all over the place just for a chatbox. Am i got to have to do this for W moving forward too?

Yes and no. GUI and Scene Graph are completely separate things that you are trying to use the same events for.

This is much easier to accomplish for mouse input, because the GUI either has focus or it doesn’t based on position of cursor and action taken.

Key binding is very specific to your application and no library could possible guess what your intent is going to be. In the game that I am working on, keybinding is dynamic and can be set by the user in-game… making it even more impossible for the library to determine this.

EDIT: However, TabFocus for keyboard events are definately covered by tonegodGUI… once an element has focus that is. You can even group input control via Forms to allow for TAB or SHIFT+TAB to navigate them.

@bgilb said: Is there no easier way in tonegodgui to have a method like, for now consider only inputs that come through tonegodgui?

Seems weird to have ifs all over the place just for a chatbox. Am i got to have to do this for W moving forward too?

Actually… you only really need to do this with the Key you want to you to set focus in the chatbox (in tonegodGUI library). Once the control has focus, it consumes events if used.

Okay I tried to use tonegodgui, but ran into more issues.

Space doesn’t work, but doesn’t make him jump. Weird?
Enter doesn’t work and also escape stops working to close the app.
I first tested in an empty app, then moved it to my game.

[java]

/**
*

  • @author bgilb
    */
    public class IngameGui implements ActionListener {

    SharedParts sharedParts;

    Screen guiScreen;
    TextField txtChatBox;
    Label lblChatHistory;

    public IngameGui(SharedParts sharedParts) {
    this.sharedParts = sharedParts;

     guiScreen = new Screen(sharedParts.app);
     sharedParts.app.getGuiNode().addControl(guiScreen);
     
     txtChatBox = new TextField(guiScreen, "txtChatBox", new Vector2f(10f, 10f)) {
    
         @Override
         public void controlKeyPressHook(KeyInputEvent evt, String text){
             if(evt.getKeyCode() == KeyInput.KEY_RETURN){
                 if(txtChatBox.getText().length() > 0){
                     sendMessage(txtChatBox.getText());
                 }
                 
                 txtChatBox.setText("");
                 txtChatBox.setIsEnabled(false);
                 txtChatBox.resetTabFocus();
             }
         }
     };
     guiScreen.addElement(txtChatBox);
     txtChatBox.setIsEnabled(false);
    
     sharedParts.app.getInputManager().addMapping("ToggleChat", new KeyTrigger(KeyInput.KEY_Y));
     sharedParts.app.getInputManager().addListener(this, "ToggleChat");
    

    }

    public void sendMessage(String m){

    }

    public void onAction(String name, boolean isPressed, float tpf) {
    if (name.equals(“ToggleChat”) && !isPressed) {
    System.out.println(“ToggleChat”);
    txtChatBox.setIsEnabled(true);
    txtChatBox.setTabFocus();
    }
    }
    }
    [/java]

@bgilb said: Okay I tried to use tonegodgui, but ran into more issues.

Space doesn’t work, but doesn’t make him jump. Weird?
Enter doesn’t work and also escape stops working to close the app.
I first tested in an empty app, then moved it to my game.

[java]

/**
*

  • @author bgilb
    */
    public class IngameGui implements ActionListener {

    SharedParts sharedParts;

    Screen guiScreen;
    TextField txtChatBox;
    Label lblChatHistory;

    public IngameGui(SharedParts sharedParts) {
    this.sharedParts = sharedParts;

     guiScreen = new Screen(sharedParts.app);
     sharedParts.app.getGuiNode().addControl(guiScreen);
     
     txtChatBox = new TextField(guiScreen, "txtChatBox", new Vector2f(10f, 10f)) {
    
         @Override
         public void controlKeyPressHook(KeyInputEvent evt, String text){
             if(evt.getKeyCode() == KeyInput.KEY_RETURN){
                 if(txtChatBox.getText().length() > 0){
                     sendMessage(txtChatBox.getText());
                 }
                 
                 txtChatBox.setText("");
                 txtChatBox.setIsEnabled(false);
                 txtChatBox.resetTabFocus();
             }
         }
     };
     guiScreen.addElement(txtChatBox);
     txtChatBox.setIsEnabled(false);
    
     sharedParts.app.getInputManager().addMapping("ToggleChat", new KeyTrigger(KeyInput.KEY_Y));
     sharedParts.app.getInputManager().addListener(this, "ToggleChat");
    

    }

    public void sendMessage(String m){

    }

    public void onAction(String name, boolean isPressed, float tpf) {
    if (name.equals(“ToggleChat”) && !isPressed) {
    System.out.println(“ToggleChat”);
    txtChatBox.setIsEnabled(true);
    txtChatBox.setTabFocus();
    }
    }
    }
    [/java]

Have a look at the ChatBoxExt control under extras. This should have everything you need (or at least an example of how to)… this includes:

An optional filters button+ window for showing removing messages by channel
An optional send button… or it will use enter key
A channel select list and a scroll area for displaying messages with color coding for channels.

Also, keep in mind instead of different screens, the library lets you use AppStates to add/remove components/functionality as you need.

If you want to roll your own:

Extend Panel, Window or Element
Call the super class if not Element
Add components

in you onAction method you would do something along these lines:

[java]
if (name.equals(“ChatFocus”) || name.equals(“SlashChatFocus”) && !isPressed) {
if (!app.getChatWindow().getChatInput().getHasFocus()) {
if (name.equals(“SlashChatFocus”))
app.getChatWindow().getChatInput().setText("/");
app.getGUIScreen().forceEventElement(app.getChatWindow().getChatInput());
app.getChatWindow().getChatInput().setHasFocus(true);
} else {
app.getChatWindow().getChatInput().setText("");
app.getChatWindow().getChatInput().resetTabFocus();
app.getGUIScreen().resetTabFocusElement();
app.getChatWindow().getChatInput().setHasFocus(false);
}
}
[/java]

The redundant setting of the control focus is due to forcing tab focus as apposed to mouse/touch input. This will potentially change, but not effect using the above.

From this point, the control will have keyboard focus and consume events that shouldn’t go to your app.

Thanks for the help. I think I’m getting closer. I am using the ChatBox control, but was wondering how I can set focus to the textbox with a custom bound key? My game wont have a mouse available so I would need that.

I tried this which didn’t work:
[java]

        screen.setTabFocusElement(chatBox.getChildElementById(chatBox.getUID()+":ChatInput"));
        chatBox.getChildElementById(chatBox.getUID()+":ChatInput").setHasFocus(true); 

[/java]