Automatic linebreaks

This commit is contained in:
Johannes Lohrer 2013-09-13 10:38:27 +02:00
parent ca16ac2ad5
commit e55dfe985f
3 changed files with 219 additions and 751 deletions

View File

@ -34,8 +34,8 @@ class GuiComputer(inventory: InventoryPlayer, val tileEntity: TileEntityComputer
override def initGui() = { override def initGui() = {
super.initGui() super.initGui()
textField = new GuiMultilineTextField(this.fontRenderer, 20, 0, 100, 40) textField = new GuiMultilineTextField(this.fontRenderer, 20, 0, 100, 100)
textField.setText("Hallo das ist ein Test\n über mehrere zeilen?") textField.setText("Hallo das ist ein Test\nueber mehrere zeilen? \nevtl auch 3?")
} }
override def drawScreen(mouseX: Int, mouseY: Int, dt: Float) = { override def drawScreen(mouseX: Int, mouseY: Int, dt: Float) = {
@ -44,6 +44,5 @@ class GuiComputer(inventory: InventoryPlayer, val tileEntity: TileEntityComputer
button.drawButton(this.mc, mouseX, mouseY) button.drawButton(this.mc, mouseX, mouseY)
textField.drawTextBox() textField.drawTextBox()
textField.setCursorPosition(0)
} }
} }

View File

@ -10,751 +10,221 @@ import net.minecraft.util.ChatAllowedCharacters;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
@SideOnly(Side.CLIENT) @SideOnly(Side.CLIENT)
public class GuiMultilineTextField extends Gui public class GuiMultilineTextField extends Gui {
{ /**
/** * Have the font renderer from GuiScreen to render the textbox text into the
* Have the font renderer from GuiScreen to render the textbox text into the screen. * screen.
*/ */
private final FontRenderer fontRenderer; private final FontRenderer fontRenderer;
private final int xPos; private final int xPos;
private final int yPos; private final int yPos;
/** The width of this text field. */ /** The width of this text field. */
private final int width; private final int width;
private final int height; private final int height;
/** Have the current text beign edited on the textbox. */ /** Have the current text beign edited on the textbox. */
private String text = ""; private String text = "";
private int maxStringLength = 32; // private int maxStringLength = 32;
private int cursorCounter; private int maxLineLength = 32;
private boolean enableBackgroundDrawing = true; private boolean enableBackgroundDrawing = true;
/** /**
* if true the textbox can lose focus by clicking elsewhere on the screen * if true the textbox can lose focus by clicking elsewhere on the screen
*/ */
private boolean canLoseFocus = true; private boolean canLoseFocus = true;
/** /**
* If this value is true along isEnabled, keyTyped will process the keys. * If this value is true along isEnabled, keyTyped will process the keys.
*/ */
private boolean isFocused; private boolean isFocused;
/** /**
* If this value is true along isFocused, keyTyped will process the keys. * If this value is true along isFocused, keyTyped will process the keys.
*/ */
private boolean isEnabled = true; private boolean isEnabled = true;
/** private int enabledColor = 14737632;
* The current character index that should be used as start of the rendered text. private int disabledColor = 7368816;
*/
private int lineScrollOffset; /** True if this textbox is visible */
private int cursorPosition; private boolean visible = true;
/** other selection position, maybe the same as the cursor */ public GuiMultilineTextField(FontRenderer par1FontRenderer, int xPos,
private int selectionEnd; int yPos, int width, int height) {
private int enabledColor = 14737632; this.fontRenderer = par1FontRenderer;
private int disabledColor = 7368816; this.xPos = xPos;
this.yPos = yPos;
/** True if this textbox is visible */ this.width = width;
private boolean visible = true; this.height = height;
}
public GuiMultilineTextField(FontRenderer par1FontRenderer, int xPos, int yPos, int width, int height)
{ /**
this.fontRenderer = par1FontRenderer; * Sets the text of the textbox.
this.xPos = xPos; */
this.yPos = yPos; public void setText(String text) {
this.width = width;
this.height = height; this.text = text;
}
}
/**
* Increments the cursor counter /**
*/ * Returns the text beign edited on the textbox.
public void updateCursorCounter() */
{ public String getText() {
++this.cursorCounter; return this.text;
} }
/** /**
* Sets the text of the textbox. * Draws the textbox
*/ */
public void setText(String text) public void drawTextBox() {
{ if (this.getVisible()) {
if (text.length() > this.maxStringLength) if (this.getEnableBackgroundDrawing()) {
{ drawRect(this.xPos - 1, this.yPos - 1, this.xPos + this.width
this.text = text.substring(0, this.maxStringLength); + 1, this.yPos + this.height + 1, -6250336);
} drawRect(this.xPos, this.yPos, this.xPos + this.width,
else this.yPos + this.height, -16777216);
{ }
this.text = text;
} int color = this.isEnabled ? this.enabledColor : this.disabledColor;
String[] lines = this.text.split("\n");
this.setCursorPositionEnd();
} int xStart = this.xPos + 4;
int yStart = this.yPos + 4;
/** int currentX = xStart;
* Returns the text beign edited on the textbox. for (String line : lines)
*/
public String getText() {
{ boolean completeLinePrinted = false;
return this.text; while (!completeLinePrinted) {
} String s = fontRenderer.trimStringToWidth(line, getWidth());
if (s.length() != line.length()) {
/** int end = s.lastIndexOf(" ");
* @return returns the text between the cursor and selectionEnd s = s.substring(0, end);
*/ line = line.substring(end+1);
public String getSelectedtext() }
{ else{
int i = this.cursorPosition < this.selectionEnd ? this.cursorPosition : this.selectionEnd; completeLinePrinted = true;
int j = this.cursorPosition < this.selectionEnd ? this.selectionEnd : this.cursorPosition; }
return this.text.substring(i, j);
} if (s.length() > 0) {
/** int heightOld = fontRenderer.FONT_HEIGHT;
* replaces selected text, or inserts text at the position on the cursor currentX = fontRenderer.drawStringWithShadow(s, xStart,
*/ yStart, color);
public void writeText(String par1Str) yStart += heightOld;
{
String s1 = ""; }
String s2 = ChatAllowedCharacters.filerAllowedCharacters(par1Str); }
int i = this.cursorPosition < this.selectionEnd ? this.cursorPosition : this.selectionEnd;
int j = this.cursorPosition < this.selectionEnd ? this.selectionEnd : this.cursorPosition; }
int k = this.maxStringLength - this.text.length() - (i - this.selectionEnd); }
boolean flag = false; }
if (this.text.length() > 0) /**
{ * draws the vertical line cursor in the textbox
s1 = s1 + this.text.substring(0, i); */
} private void drawCursorVertical(int par1, int par2, int par3, int par4) {
int i1;
int l;
if (par1 < par3) {
if (k < s2.length()) i1 = par1;
{ par1 = par3;
s1 = s1 + s2.substring(0, k); par3 = i1;
l = k; }
}
else if (par2 < par4) {
{ i1 = par2;
s1 = s1 + s2; par2 = par4;
l = s2.length(); par4 = i1;
} }
if (this.text.length() > 0 && j < this.text.length()) Tessellator tessellator = Tessellator.instance;
{ GL11.glColor4f(0.0F, 0.0F, 255.0F, 255.0F);
s1 = s1 + this.text.substring(j); GL11.glDisable(GL11.GL_TEXTURE_2D);
} GL11.glEnable(GL11.GL_COLOR_LOGIC_OP);
GL11.glLogicOp(GL11.GL_OR_REVERSE);
this.text = s1; tessellator.startDrawingQuads();
this.moveCursorBy(i - this.selectionEnd + l); tessellator.addVertex((double) par1, (double) par4, 0.0D);
} tessellator.addVertex((double) par3, (double) par4, 0.0D);
tessellator.addVertex((double) par3, (double) par2, 0.0D);
/** tessellator.addVertex((double) par1, (double) par2, 0.0D);
* Deletes the specified number of words starting at the cursor position. Negative numbers will delete words left of tessellator.draw();
* the cursor. GL11.glDisable(GL11.GL_COLOR_LOGIC_OP);
*/ GL11.glEnable(GL11.GL_TEXTURE_2D);
public void deleteWords(int par1) }
{
if (this.text.length() != 0) /**
{ * get enable drawing background and outline
if (this.selectionEnd != this.cursorPosition) */
{ public boolean getEnableBackgroundDrawing() {
this.writeText(""); return this.enableBackgroundDrawing;
} }
else
{ /**
this.deleteFromCursor(this.getNthWordFromCursor(par1) - this.cursorPosition); * enable drawing background and outline
} */
} public void setEnableBackgroundDrawing(boolean par1) {
} this.enableBackgroundDrawing = par1;
}
/**
* delete the selected text, otherwsie deletes characters from either side of the cursor. params: delete num /**
*/ * Sets the text colour for this textbox (disabled text will not use this
public void deleteFromCursor(int par1) * colour)
{ */
if (this.text.length() != 0) public void setTextColor(int par1) {
{ this.enabledColor = par1;
if (this.selectionEnd != this.cursorPosition) }
{
this.writeText(""); public void setDisabledTextColour(int par1) {
} this.disabledColor = par1;
else }
{
boolean flag = par1 < 0; /**
int j = flag ? this.cursorPosition + par1 : this.cursorPosition; * setter for the focused field
int k = flag ? this.cursorPosition : this.cursorPosition + par1; */
String s = ""; public void setFocused(boolean par1) {
if (j >= 0) this.isFocused = par1;
{ }
s = this.text.substring(0, j);
} /**
* getter for the focused field
if (k < this.text.length()) */
{ public boolean isFocused() {
s = s + this.text.substring(k); return this.isFocused;
} }
this.text = s; public void setEnabled(boolean par1) {
this.isEnabled = par1;
if (flag) }
{
this.moveCursorBy(par1); /**
} * returns the width of the textbox depending on if the the box is enabled
} */
} public int getWidth() {
} return this.getEnableBackgroundDrawing() ? this.width - 8 : this.width;
}
/**
* see @getNthNextWordFromPos() params: N, position /**
*/ * if true the textbox can lose focus by clicking elsewhere on the screen
public int getNthWordFromCursor(int par1) */
{ public void setCanLoseFocus(boolean par1) {
return this.getNthWordFromPos(par1, this.getCursorPosition()); this.canLoseFocus = par1;
} }
/** /**
* gets the position of the nth word. N may be negative, then it looks backwards. params: N, position * @return {@code true} if this textbox is visible
*/ */
public int getNthWordFromPos(int par1, int par2) public boolean getVisible() {
{ return this.visible;
return this.func_73798_a(par1, this.getCursorPosition(), true); }
}
/**
public int func_73798_a(int par1, int par2, boolean par3) * Sets whether or not this textbox is visible
{ */
int k = par2; public void setVisible(boolean par1) {
boolean flag1 = par1 < 0; this.visible = par1;
int l = Math.abs(par1); }
for (int i1 = 0; i1 < l; ++i1)
{
if (flag1)
{
while (par3 && k > 0 && this.text.charAt(k - 1) == 32)
{
--k;
}
while (k > 0 && this.text.charAt(k - 1) != 32)
{
--k;
}
}
else
{
int j1 = this.text.length();
k = this.text.indexOf(32, k);
if (k == -1)
{
k = j1;
}
else
{
while (par3 && k < j1 && this.text.charAt(k) == 32)
{
++k;
}
}
}
}
return k;
}
/**
* Moves the text cursor by a specified number of characters and clears the selection
*/
public void moveCursorBy(int par1)
{
this.setCursorPosition(this.selectionEnd + par1);
}
/**
* sets the position of the cursor to the provided index
*/
public void setCursorPosition(int par1)
{
this.cursorPosition = par1;
int j = this.text.length();
if (this.cursorPosition < 0)
{
this.cursorPosition = 0;
}
if (this.cursorPosition > j)
{
this.cursorPosition = j;
}
this.setSelectionPos(this.cursorPosition);
}
/**
* sets the cursors position to the beginning
*/
public void setCursorPositionZero()
{
this.setCursorPosition(0);
}
/**
* sets the cursors position to after the text
*/
public void setCursorPositionEnd()
{
this.setCursorPosition(this.text.length());
}
/**
* Call this method from you GuiScreen to process the keys into textbox.
*/
public boolean textboxKeyTyped(char par1, int par2)
{
if (this.isEnabled && this.isFocused)
{
switch (par1)
{
case 1:
this.setCursorPositionEnd();
this.setSelectionPos(0);
return true;
case 3:
GuiScreen.setClipboardString(this.getSelectedtext());
return true;
case 22:
this.writeText(GuiScreen.getClipboardString());
return true;
case 24:
GuiScreen.setClipboardString(this.getSelectedtext());
this.writeText("");
return true;
default:
switch (par2)
{
case 14:
if (GuiScreen.isCtrlKeyDown())
{
this.deleteWords(-1);
}
else
{
this.deleteFromCursor(-1);
}
return true;
case 199:
if (GuiScreen.isShiftKeyDown())
{
this.setSelectionPos(0);
}
else
{
this.setCursorPositionZero();
}
return true;
case 203:
if (GuiScreen.isShiftKeyDown())
{
if (GuiScreen.isCtrlKeyDown())
{
this.setSelectionPos(this.getNthWordFromPos(-1, this.getSelectionEnd()));
}
else
{
this.setSelectionPos(this.getSelectionEnd() - 1);
}
}
else if (GuiScreen.isCtrlKeyDown())
{
this.setCursorPosition(this.getNthWordFromCursor(-1));
}
else
{
this.moveCursorBy(-1);
}
return true;
case 205:
if (GuiScreen.isShiftKeyDown())
{
if (GuiScreen.isCtrlKeyDown())
{
this.setSelectionPos(this.getNthWordFromPos(1, this.getSelectionEnd()));
}
else
{
this.setSelectionPos(this.getSelectionEnd() + 1);
}
}
else if (GuiScreen.isCtrlKeyDown())
{
this.setCursorPosition(this.getNthWordFromCursor(1));
}
else
{
this.moveCursorBy(1);
}
return true;
case 207:
if (GuiScreen.isShiftKeyDown())
{
this.setSelectionPos(this.text.length());
}
else
{
this.setCursorPositionEnd();
}
return true;
case 211:
if (GuiScreen.isCtrlKeyDown())
{
this.deleteWords(1);
}
else
{
this.deleteFromCursor(1);
}
return true;
default:
if (ChatAllowedCharacters.isAllowedCharacter(par1))
{
this.writeText(Character.toString(par1));
return true;
}
else
{
return false;
}
}
}
}
else
{
return false;
}
}
/**
* Args: x, y, buttonClicked
*/
public void mouseClicked(int par1, int par2, int par3)
{
boolean flag = par1 >= this.xPos && par1 < this.xPos + this.width && par2 >= this.yPos && par2 < this.yPos + this.height;
if (this.canLoseFocus)
{
this.setFocused(this.isEnabled && flag);
}
if (this.isFocused && par3 == 0)
{
int l = par1 - this.xPos;
if (this.enableBackgroundDrawing)
{
l -= 4;
}
String s = this.fontRenderer.trimStringToWidth(this.text.substring(this.lineScrollOffset), this.getWidth());
this.setCursorPosition(this.fontRenderer.trimStringToWidth(s, l).length() + this.lineScrollOffset);
}
}
/**
* Draws the textbox
*/
public void drawTextBox()
{
if (this.getVisible())
{
if (this.getEnableBackgroundDrawing())
{
drawRect(this.xPos - 1, this.yPos - 1, this.xPos + this.width + 1, this.yPos + this.height + 1, -6250336);
drawRect(this.xPos, this.yPos, this.xPos + this.width, this.yPos + this.height, -16777216);
}
int color = this.isEnabled ? this.enabledColor : this.disabledColor;
int cursorMinusScroll = this.cursorPosition - this.lineScrollOffset;
int selectionMinusOffset = this.selectionEnd - this.lineScrollOffset;
String s = this.fontRenderer.trimStringToWidth(this.text.substring(this.lineScrollOffset), this.getWidth());
boolean flag = cursorMinusScroll >= 0 && cursorMinusScroll <= s.length();
boolean flag1 = this.isFocused && this.cursorCounter / 6 % 2 == 0 && flag;
int xStart = this.enableBackgroundDrawing ? this.xPos + 4 : this.xPos;
int yStart = this.enableBackgroundDrawing ? this.yPos /*+ (this.height - 8) / 2*/ : this.yPos;
int currentX = xStart;
if (selectionMinusOffset > s.length())
{
selectionMinusOffset = s.length();
}
if (s.length() > 0)
{
String s1 = flag ? s.substring(0, cursorMinusScroll) : s;
int heightOld = this.fontRenderer.FONT_HEIGHT;
this.fontRenderer.FONT_HEIGHT = 3;
currentX = this.fontRenderer.drawStringWithShadow(s1, xStart, yStart, color);
this.fontRenderer.FONT_HEIGHT = heightOld;
}
boolean flag2 = this.cursorPosition < this.text.length() || this.text.length() >= this.getMaxStringLength();
int k1 = currentX;
if (!flag)
{
k1 = cursorMinusScroll > 0 ? xStart + this.width : xStart;
}
else if (flag2)
{
k1 = currentX - 1;
--currentX;
}
if (s.length() > 0 && flag && cursorMinusScroll < s.length())
{
this.fontRenderer.drawStringWithShadow(s.substring(cursorMinusScroll), currentX, yStart, color);
}
if (flag1)
{
if (flag2)
{
Gui.drawRect(k1, yStart - 1, k1 + 1, yStart + 1 + this.fontRenderer.FONT_HEIGHT, -3092272);
}
else
{
this.fontRenderer.drawStringWithShadow("_", k1, yStart, color);
}
}
if (selectionMinusOffset != cursorMinusScroll)
{
int l1 = xStart + this.fontRenderer.getStringWidth(s.substring(0, selectionMinusOffset));
this.drawCursorVertical(k1, yStart - 1, l1 - 1, yStart + 1 + this.fontRenderer.FONT_HEIGHT);
}
}
}
/**
* draws the vertical line cursor in the textbox
*/
private void drawCursorVertical(int par1, int par2, int par3, int par4)
{
int i1;
if (par1 < par3)
{
i1 = par1;
par1 = par3;
par3 = i1;
}
if (par2 < par4)
{
i1 = par2;
par2 = par4;
par4 = i1;
}
Tessellator tessellator = Tessellator.instance;
GL11.glColor4f(0.0F, 0.0F, 255.0F, 255.0F);
GL11.glDisable(GL11.GL_TEXTURE_2D);
GL11.glEnable(GL11.GL_COLOR_LOGIC_OP);
GL11.glLogicOp(GL11.GL_OR_REVERSE);
tessellator.startDrawingQuads();
tessellator.addVertex((double)par1, (double)par4, 0.0D);
tessellator.addVertex((double)par3, (double)par4, 0.0D);
tessellator.addVertex((double)par3, (double)par2, 0.0D);
tessellator.addVertex((double)par1, (double)par2, 0.0D);
tessellator.draw();
GL11.glDisable(GL11.GL_COLOR_LOGIC_OP);
GL11.glEnable(GL11.GL_TEXTURE_2D);
}
public void setMaxStringLength(int par1)
{
this.maxStringLength = par1;
if (this.text.length() > par1)
{
this.text = this.text.substring(0, par1);
}
}
/**
* returns the maximum number of character that can be contained in this textbox
*/
public int getMaxStringLength()
{
return this.maxStringLength;
}
/**
* returns the current position of the cursor
*/
public int getCursorPosition()
{
return this.cursorPosition;
}
/**
* get enable drawing background and outline
*/
public boolean getEnableBackgroundDrawing()
{
return this.enableBackgroundDrawing;
}
/**
* enable drawing background and outline
*/
public void setEnableBackgroundDrawing(boolean par1)
{
this.enableBackgroundDrawing = par1;
}
/**
* Sets the text colour for this textbox (disabled text will not use this colour)
*/
public void setTextColor(int par1)
{
this.enabledColor = par1;
}
public void setDisabledTextColour(int par1)
{
this.disabledColor = par1;
}
/**
* setter for the focused field
*/
public void setFocused(boolean par1)
{
if (par1 && !this.isFocused)
{
this.cursorCounter = 0;
}
this.isFocused = par1;
}
/**
* getter for the focused field
*/
public boolean isFocused()
{
return this.isFocused;
}
public void setEnabled(boolean par1)
{
this.isEnabled = par1;
}
/**
* the side of the selection that is not the cursor, maye be the same as the cursor
*/
public int getSelectionEnd()
{
return this.selectionEnd;
}
/**
* returns the width of the textbox depending on if the the box is enabled
*/
public int getWidth()
{
return this.getEnableBackgroundDrawing() ? this.width - 8 : this.width;
}
/**
* Sets the position of the selection anchor (i.e. position the selection was started at)
*/
public void setSelectionPos(int par1)
{
int j = this.text.length();
if (par1 > j)
{
par1 = j;
}
if (par1 < 0)
{
par1 = 0;
}
this.selectionEnd = par1;
if (this.fontRenderer != null)
{
if (this.lineScrollOffset > j)
{
this.lineScrollOffset = j;
}
int k = this.getWidth();
String s = this.fontRenderer.trimStringToWidth(this.text.substring(this.lineScrollOffset), k);
int l = s.length() + this.lineScrollOffset;
if (par1 == this.lineScrollOffset)
{
this.lineScrollOffset -= this.fontRenderer.trimStringToWidth(this.text, k, true).length();
}
if (par1 > l)
{
this.lineScrollOffset += par1 - l;
}
else if (par1 <= this.lineScrollOffset)
{
this.lineScrollOffset -= this.lineScrollOffset - par1;
}
if (this.lineScrollOffset < 0)
{
this.lineScrollOffset = 0;
}
if (this.lineScrollOffset > j)
{
this.lineScrollOffset = j;
}
}
}
/**
* if true the textbox can lose focus by clicking elsewhere on the screen
*/
public void setCanLoseFocus(boolean par1)
{
this.canLoseFocus = par1;
}
/**
* @return {@code true} if this textbox is visible
*/
public boolean getVisible()
{
return this.visible;
}
/**
* Sets whether or not this textbox is visible
*/
public void setVisible(boolean par1)
{
this.visible = par1;
}
} }

View File

@ -11,7 +11,6 @@ class Screen(val owner: TileEntityScreen) extends IScreen with IComponent {
val (w, h) = value val (w, h) = value
PacketSender.sendScreenResolutionChange(owner, w, h) PacketSender.sendScreenResolutionChange(owner, w, h)
} }
def set(col: Int, row: Int, s: String) = def set(col: Int, row: Int, s: String) =
PacketSender.sendScreenSet(owner, col, row, s) PacketSender.sendScreenSet(owner, col, row, s)