From a841a8506e456fabe6880caa121c82d800d4927d Mon Sep 17 00:00:00 2001 From: NathanSweet Date: Tue, 28 Aug 2012 01:32:54 -0700 Subject: [PATCH] TextField, support for right aligned text. --- .../badlogic/gdx/scenes/scene2d/ui/TextField.java | 33 ++++++++++++++-------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/TextField.java b/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/TextField.java index 83662fd5e..80c848906 100644 --- a/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/TextField.java +++ b/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/TextField.java @@ -98,6 +98,7 @@ public class TextField extends Widget { KeyRepeatTask keyRepeatTask = new KeyRepeatTask(); float keyRepeatInitialTime = 0.4f; float keyRepeatTime = 0.1f; + boolean rightAligned; public TextField (String text, Skin skin) { this(text, skin.get(TextFieldStyle.class)); @@ -265,6 +266,7 @@ public class TextField extends Widget { text = text.substring(0, cursor - 1) + text.substring(cursor); updateDisplayText(); cursor--; + renderOffset = 0; } else { delete(); } @@ -328,22 +330,19 @@ public class TextField extends Widget { } private void calculateOffsets () { - float position = glyphPositions.get(cursor); - float distance = position - Math.abs(renderOffset); float visibleWidth = getWidth(); if (style.background != null) visibleWidth -= style.background.getLeftWidth() + style.background.getRightWidth(); - // check whether the cursor left the left or right side of - // the visible area and adjust renderoffset. + // Check if the cursor has gone out the left or right side of the visible area and adjust renderoffset. + float position = glyphPositions.get(cursor); + float distance = position - Math.abs(renderOffset); if (distance <= 0) { if (cursor > 0) renderOffset = -glyphPositions.get(cursor - 1); else renderOffset = 0; - } else { - if (distance > visibleWidth) { - renderOffset -= distance - visibleWidth; - } + } else if (distance > visibleWidth) { + renderOffset -= distance - visibleWidth; } // calculate first visible char based on render offset @@ -356,7 +355,7 @@ public class TextField extends Widget { if (glyphPositions.items[i] >= start) { visibleTextStart = i; startPos = glyphPositions.items[i]; - textOffset = glyphPositions.items[visibleTextStart] - start; + textOffset = startPos - start; break; } } @@ -372,11 +371,16 @@ public class TextField extends Widget { if (hasSelection) { int minIndex = Math.min(cursor, selectionStart); int maxIndex = Math.max(cursor, selectionStart); - float minX = Math.max(glyphPositions.get(minIndex), glyphPositions.get(visibleTextStart)); + float minX = Math.max(glyphPositions.get(minIndex), startPos); float maxX = Math.min(glyphPositions.get(maxIndex), glyphPositions.get(visibleTextEnd)); selectionX = minX; selectionWidth = maxX - minX; } + + if (rightAligned) { + textOffset = visibleWidth - (glyphPositions.items[visibleTextEnd] - startPos); + if (hasSelection) selectionX += textOffset; + } } @Override @@ -426,8 +430,9 @@ public class TextField extends Widget { if (focused) { blink(); if (cursorOn && cursorPatch != null) { - cursorPatch.draw(batch, x + bgLeftWidth + glyphPositions.get(cursor) + renderOffset - 1, y + textY - - textBounds.height - font.getDescent(), cursorPatch.getMinWidth(), textBounds.height + font.getDescent() / 2); + cursorPatch.draw(batch, x + textOffset + glyphPositions.get(cursor) - glyphPositions.items[visibleTextStart] + 1, y + + textY - textBounds.height - font.getDescent(), cursorPatch.getMinWidth(), textBounds.height + font.getDescent() + / 2); } } } @@ -672,6 +677,10 @@ public class TextField extends Widget { return prefHeight; } + public void setRightAligned (boolean rightAligned) { + this.rightAligned = rightAligned; + } + /** If true, the text in this text field will be shown as bullet characters. The font must have character 149 or this will have * no affect. */ public void setPasswordMode (boolean passwordMode) { -- 2.11.0