SpoutDev/Spout — Issue #1395: GenericTextField acting very strange
Author: blaise64
Created: 2012-01-06 09:39:36+00:00
State: open

I post here an issue I post on the bukkit forum but I think here it's a better place
I have the same comportement with my plugin and AdminGUI : text field seems to loose focus without a reason when typing. So you need to reselect it before continuing typing.
It's a bit confusing ..
More over it seem that the right last third part of the GenericTextField widget can't be used (no possibility to type something in even if the maximum characters setting is higher than the number of caracter you typed).

May be I have a element that can cause this bug.

The first bug is the loose of the focus. I made a little test where I rewrite the writeData and readData method to print the state of the focus and the same for the method onTextFieldEvent()
Here are the results :
```15:43:26 [INFO] TextFieldChangeEvent - new text is: test_param1de - focus: false
15:43:26 [INFO] entry : writing data - focus:false
15:43:28 [INFO] TextFieldChangeEvent - new text is: test_param1dede - focus: false
15:43:28 [INFO] entry : writing data - focus:false
15:43:29 [INFO] TextFieldChangeEvent - new text is: test_param1dedede - focus: false
15:43:29 [INFO] entry : writing data - focus:false```
There is no readData so no update of the focus => focus is always reset to false when a TextFieldChangeEvent append without the setFocus(true) method because writeData is call after a TextFieldChangeEvent.
If you try you can see you loose the focus every time you stop typing during more than 1 tick...
The solution I use for this problem is to put setFocus(true) in the onTextFieldChange method :
``` public void onTextFieldChange(TextFieldChangeEvent event)
{
this.setFocus(true);
this.text=event.getNewText();
System.out.println("TextFieldChangeEvent - new text is: "+event.getNewText()+" - focus: "+isFocused());
//this.app_root.registerParameter(var, text);
}```
And this work really fine. I don't loose the focus and there is no problem when changing from one text field to another.
```23:49:01 [INFO] TextFieldChangeEvent - new text is: test_param1 - focus: true
23:49:01 [INFO] entry : writing data - focus:true
23:49:04 [INFO] TextFieldChangeEvent - new text is: test_param1 gyl - focus: true
23:49:04 [INFO] entry : writing data - focus:true
23:49:07 [INFO] TextFieldChangeEvent - new text is: test_param1 gylvhubo;vora;vatuovta - focus: true
23:49:07 [INFO] entry : writing data - focus:true
23:49:09 [INFO] TextFieldChangeEvent - new text is: test_param1 gylvhubo;vora;vatuovtano - focus: true
23:49:09 [INFO] entry : writing data - focus:true
23:49:13 [INFO] TextFieldChangeEvent - new text is: test_param1 - focus: true
23:49:13 [INFO] entry : writing data - focus:true
23:49:19 [INFO] TextFieldChangeEvent - new text is: test_param1 huvrahuilvrvbirlavuirahui - focus: true
23:49:19 [INFO] entry : writing data - focus:true```
But sometime there is a little graphic bug of the text without lost of data.

The other problem I mention is the bug of max size of the text in the TextField.
I made some testing to figure out the problem and I think I find the problem.
First of all I increase the max number of lines in my TextField.
I found that then the text reach the previous limit it goes to the next line...
More over when I resize the windows it fit well if the TextField police size is the same than the button police size.
Here is a capture when resized:
http://imageshack.us/photo/my-images/46/capturedcran20111231154.png

Here a capture when max sized windows:
http://imageshack.us/photo/my-images/16/capturedcran20111231154.png

So I take a look at the code of GenericTextProcessor class at https://github.com/SpoutDev/SpoutcraftAPI/blob/master/src/main/java/org/spoutcraft/spoutcraftapi/gui/GenericTextProcessor.java and I found this method :
```protected boolean formatText() {//line 238```
where the text is split:
```// split very long words
if (wordWidth > width) { //line 274
int i = word.length();
while (i > 0 && wordWidth > width)
wordWidth -= font.getTextWidth(String.valueOf(word.charAt(--i)));
position = position - word.length() + i;
lineBreaks.add(position);
lineWidth = 0;
word = word.substring(i);
skipIterator = true;}// check if this word would exceed the max-width of the line
else if (lineWidth + wordWidth > width) {
if (lineBreaks.size() + 1 < lineLimit) {
lineBreaks.add(position - word.length());
lineWidth = wordWidth;
} else
return false;
} else {
lineWidth += wordWidth;
}previousSpace = false;```

The wordWidth is calculate by: ```wordWidth = font.getTextWidth(word);//line 257```
I think it is this method witch doesn't give the right size.

I made another test to test this: I change the GUI size in the spoutcraft client options to small and I found that the text in the Field is resize but the limit is always to the same number of character. More over the text in the buttons is not resize.
http://imageshack.us/photo/my-images/221/capturedcran20111231163.png

So I think the MinecraftFont.getTextWidth(String s) give a size based on the number of characters and the size of a character in the police used for the buttons but it is not the same than in the TextField.
May be if you set the same police for all the widgets it will match.

I hope this can help you !