Hello, i have start to work on an effective way to build a chat window. I have seen some exemple but i can’t find the code anywhere… so i started my own base on what is avaiable on the tutorial part. My problem is… the code don’t seem to work very well.
First i have been trying to make the enter button not removing the focus from the text field, and it did not work. Am still trying to make it happen but i still don’t underthant why it lose its focus anyway.
Second I realise the scrooller thing was hide because of the .setClippingLayer(saChatArea); method. but when i desactive it the text simply go on and on over the limit of the ScrollArea for some reason…
Here is the code
[java]
import com.jme3.font.BitmapFont;
import com.jme3.font.LineWrapMode;
import com.jme3.input.KeyInput;
import com.jme3.input.event.KeyInputEvent;
import com.jme3.input.event.MouseButtonEvent;
import com.jme3.input.event.MouseMotionEvent;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector4f;
import java.util.ArrayList;
import java.util.List;
import tonegod.gui.controls.buttons.Button;
import tonegod.gui.controls.lists.Spinner;
import tonegod.gui.controls.scrolling.ScrollArea;
import tonegod.gui.controls.text.Label;
import tonegod.gui.controls.text.TextField;
import tonegod.gui.controls.windows.Panel;
import tonegod.gui.core.Screen;
/**
*
-
@author t0neg0d
*/
public class ZoneChat extends Panel {
ScrollArea saChatArea;
TextField tfChatInput;
Button btnChatSendMsg;
Spinner spnChannels;int sendKey; int chatHistorySize = 30; List<String> chatMessages = new ArrayList(); /** * Creates a new instance of the ChatBox control * * @param screen The screen control the Element is to be added to * @param UID A unique String identifier for the Element * @param position A Vector2f containing the x/y position of the Element */ public ZoneChat(Screen screen, String UID, Vector2f position) { this(screen, UID, position, screen.getStyle("Window").getVector2f("defaultSize"), screen.getStyle("Window").getVector4f("resizeBorders"), screen.getStyle("Window").getString("defaultImg") ); } /** * Creates a new instance of the ChatBox control * * @param screen The screen control the Element is to be added to * @param UID A unique String identifier for the Element * @param position A Vector2f containing the x/y position of the Element * @param dimensions A Vector2f containing the width/height dimensions of the Element */ public ZoneChat(Screen screen, String UID, Vector2f position, Vector2f dimensions) { this(screen, UID, position, dimensions, screen.getStyle("Window").getVector4f("resizeBorders"), screen.getStyle("Window").getString("defaultImg") ); } /** * Creates a new instance of the ChatBox control * * @param screen The screen control the Element is to be added to * @param UID A unique String identifier for the Element * @param position A Vector2f containing the x/y position of the Element * @param dimensions A Vector2f containing the width/height dimensions of the Element * @param resizeBorders A Vector4f containg the border information used when resizing the default image (x = N, y = W, z = E, w = S) * @param defaultImg The default image to use for the Slider's track */ public ZoneChat(Screen screen, String UID, Vector2f position, Vector2f dimensions, Vector4f resizeBorders, String defaultImg) { super(screen, UID, position, dimensions, resizeBorders, defaultImg); this.setIsMovable(true); this.setIsResizable(true); this.setScaleNS(false); this.setScaleEW(false); /// Zone ou est ecrit le texte saChatArea = new ScrollArea(screen, UID + ":ChatArea", new Vector2f(0, 0), new Vector2f(getWidth(), getHeight()-85), true ); saChatArea.setFontColor(ColorRGBA.LightGray); saChatArea.setTextAlign(BitmapFont.Align.Left); saChatArea.setTextPosition(5,5); saChatArea.setTextWrap(LineWrapMode.Word); saChatArea.setIsResizable(false); saChatArea.setScaleEW(true); saChatArea.setScaleNS(true); //saChatArea.setClippingLayer(saChatArea); saChatArea.setPadding(5); saChatArea.setText(""); addChild(saChatArea); // zone ou est écrit le texte tfChatInput = new TextField( screen, UID + ":ChatInput", new Vector2f(10, getHeight()-25-15), // position new Vector2f(getWidth()-20-95, 25) // grandeur ) { @Override public void controlKeyPressHook(KeyInputEvent evt, String text) { if (evt.getKeyCode() == sendKey) { if (tfChatInput.getText().length() > 0) { tfChatInput.setText(tfChatInput.getText().substring(0,tfChatInput.getText().length()-1)); sendMsg(); } } } }; tfChatInput.setScaleEW(true); tfChatInput.setScaleNS(false); // tfChatInput.setDockS(true); // tfChatInput.setDockW(true); btnChatSendMsg = new Button ( screen, UID + ":ChatSendMsg", new Vector2f(getWidth()-10-100, getHeight()-25-15), new Vector2f(100,25) ) { @Override public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled) { sendMsg(); } @Override public void onButtonFocus(MouseMotionEvent arg0) {} @Override public void onButtonLostFocus(MouseMotionEvent arg0) {} @Override public void onButtonMouseLeftDown( MouseButtonEvent arg0, boolean arg1) {} @Override public void onButtonMouseRightDown( MouseButtonEvent arg0, boolean arg1) {} @Override public void onButtonMouseRightUp(MouseButtonEvent arg0, boolean arg1) {} }; btnChatSendMsg.setScaleEW(false); btnChatSendMsg.setScaleNS(false); btnChatSendMsg.setDockS(true); btnChatSendMsg.setDockE(true); btnChatSendMsg.setText("Send"); addChild(btnChatSendMsg); addChild(tfChatInput); populateEffects("Window"); } private void sendMsg() { if (tfChatInput.getText().length() > 0) { if (!tfChatInput.getText().equals("")) { onSendMsg(tfChatInput.getText()); tfChatInput.setText("");; } } } public void receiveMsg(String msg) { chatMessages.add(msg); updateChatHistory(); } private void updateChatHistory() { if (chatMessages.size() > chatHistorySize) { chatMessages.remove(0); } rebuildChat(); } private void rebuildChat() { String displayText = ""; int index = 0; for (String s : chatMessages) { if (index > 0) displayText += "\n" + s; else displayText += s; index++; } saChatArea.setText(displayText); saChatArea.scrollToBottom(); } public void setSendKey(int sendKey) { this.sendKey = sendKey; } public void onSendMsg(String msg) { receiveMsg(msg) ; }
}
[/java]