JME3 Nifty Chat - Lightnet/openwizcoder GitHub Wiki

Nifty has a built basic chat input and output. It deal with the GUI with out having to do the detail coding. But it still need to send and get from the chat coding.

UI_Chat.xml

<?xml version="1.0" encoding="UTF-8"?>
<nifty>

	<useStyles filename="nifty-default-styles.xml" />
	<useControls filename="nifty-default-controls.xml" />

	<!-- +++++++++++++++++++++++++++++++++++++++ -->
	<!-- start screen -->
	<!-- +++++++++++++++++++++++++++++++++++++++ -->
        
	<screen id="start" controller="org.openwizcoder.ui.UIChatScreenController">
		<layer id="layer" childLayout="center" align="center">
                     <panel id="chatpanel" width="80%" height="80%" align="center" valign="center" childLayout="vertical" backgroundColor="#55a5" padding="10">
			<control id="chatcontrol" name="nifty-chat" width="100%" height="50%" lines="14" sendLabel="Send Message" />
                        </panel>
		</layer>
	</screen>
</nifty>

UIChatScreenController.java


import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.NiftyEventSubscriber;
import de.lessvoid.nifty.controls.Chat;
import de.lessvoid.nifty.controls.ChatTextSendEvent;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.render.NiftyImage;
import de.lessvoid.nifty.screen.Screen;

public class UIChatScreenController implements ScreenController{
    public Nifty nifty;
    public Screen screen;
    NiftyImage newImage;// = nifty.getRenderEngine().createImage("Textures/member_icon.png", false);

    @Override
    public void bind(Nifty nifty, Screen screen) {
        this.nifty = nifty;
        this.screen = screen;
        try {
            this.newImage = this.nifty.getRenderEngine().createImage("Textures/member_icon.png", false);
            AddPlayer("guest", newImage);
            System.out.println("image dectected");
        } catch (Exception e) {
             System.out.println("image error dectected");
        }
    }
    
    @Override
    public void onStartScreen() {
        
    }
    
    @Override
    public void onEndScreen() {
        
    }

    @NiftyEventSubscriber(id="chatcontrol")
    public final void onSendText(final String id, final ChatTextSendEvent event) {
        if (!event.getText().isEmpty()) {
            String text = event.getText();
            System.out.println("chat event received: " + text);
            Message("guest",text,this.newImage);
        }
        System.out.println("event dectected");
    }
    
    public void AddPlayer(String playerName, NiftyImage avatarImage){
        final Element chatPanel = nifty.getCurrentScreen().findElementByName("chatpanel");
        final Chat chat = chatPanel.findNiftyControl("chatcontrol", Chat.class);
        
        if(avatarImage !=null){
            chat.addPlayer(playerName, avatarImage);    
        }else{
            chat.addPlayer(playerName, newImage);
        }
    }
    
    public void RemovePlayer(String playerName){
        final Element chatPanel = nifty.getCurrentScreen().findElementByName("chatpanel");
        final Chat chatController = chatPanel.findNiftyControl("chatcontrol", Chat.class);
        chatController.removePlayer(playerName);        
    }
    
    public void Message(String playerName,String chatLine,NiftyImage avatarImage){
        System.out.println("image default dectected");
        final Element chatPanel = nifty.getCurrentScreen().findElementByName("chatpanel");
        /*
        if(chatPanel !=null){
            System.out.println("found element");
        }else{
            System.out.println("not found element");
        }
        */
        final Chat chatController = chatPanel.findNiftyControl("chatcontrol", Chat.class);
        /*
        if(chatController !=null){
            System.out.println("found Controller");
        }else{
            System.out.println("not found Controller");
        }
        */
        if(avatarImage !=null){
            chatController.receivedChatLine(playerName +">" + chatLine, avatarImage);
            System.out.println("image mod dectected");
        }else{
            //newImage = this.nifty.getRenderEngine().createImage("Textures/member_icon.png", false);
            chatController.receivedChatLine(playerName +">" + chatLine, newImage);
            System.out.println("image default dectected:"+playerName +">" + chatLine);
        }
    }
    
}

AppChatTest.java

import com.jme3.app.SimpleApplication;
import com.jme3.niftygui.NiftyJmeDisplay;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openwizcoder.ui.UIChatScreenController;


public class AppChatTest extends SimpleApplication implements ScreenController {
	public Nifty nifty;

	public static void main(final String[] args) throws Exception {
            Logger.getLogger("").setLevel(Level.SEVERE);
            AppChatTest app = new AppChatTest();
            app.setShowSettings(false);
            app.start();
	}

    public void Init_Nifty(){
        NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager,
                                                          inputManager,
                                                          audioRenderer,
                                                          guiViewPort);
        nifty = niftyDisplay.getNifty();
        UIChatScreenController chatbox = new UIChatScreenController();
        nifty.fromXml("Interface/UI_Chat.xml" ,"start",chatbox);
        // attach the nifty display to the gui view port as a processor
        guiViewPort.addProcessor(niftyDisplay); 
        
    }    

    @Override
    public void simpleInitApp() {
        Init_Nifty();
        setDisplayStatView(false);
        mouseInput.setCursorVisible(true);
        this.pauseOnFocus = false;
        this.flyCam.setEnabled(false);
    }

   public void bind(Nifty nifty, Screen screen) {
        System.out.println("bind( " + screen.getScreenId() + ")");
    }

    public void onStartScreen() {
        System.out.println("onStartScreen");
    }

    public void onEndScreen() {
        System.out.println("onEndScreen");
    }

    public void quit(){
        nifty.gotoScreen("end");
    }
}
⚠️ **GitHub.com Fallback** ⚠️