Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mellotanica.game.ui;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Input;
- import com.badlogic.gdx.graphics.Color;
- import com.badlogic.gdx.graphics.g2d.Batch;
- import com.badlogic.gdx.graphics.g2d.BitmapFont;
- import com.badlogic.gdx.graphics.g2d.GlyphLayout;
- import com.badlogic.gdx.math.Interpolation;
- import com.badlogic.gdx.math.MathUtils;
- import com.badlogic.gdx.math.Vector2;
- import com.badlogic.gdx.scenes.scene2d.Actor;
- import com.badlogic.gdx.scenes.scene2d.Event;
- import com.badlogic.gdx.scenes.scene2d.EventListener;
- import com.badlogic.gdx.scenes.scene2d.Group;
- import com.badlogic.gdx.scenes.scene2d.InputEvent;
- import com.badlogic.gdx.scenes.scene2d.InputListener;
- import com.badlogic.gdx.scenes.scene2d.Stage;
- import com.badlogic.gdx.scenes.scene2d.Touchable;
- import com.badlogic.gdx.scenes.scene2d.ui.List;
- import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
- import com.badlogic.gdx.scenes.scene2d.ui.Skin;
- import com.badlogic.gdx.scenes.scene2d.ui.Widget;
- import com.badlogic.gdx.scenes.scene2d.utils.ArraySelection;
- import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
- import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
- import com.badlogic.gdx.scenes.scene2d.utils.Disableable;
- import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
- import com.badlogic.gdx.scenes.scene2d.utils.UIUtils;
- import com.badlogic.gdx.utils.Align;
- import com.badlogic.gdx.utils.Array;
- import com.badlogic.gdx.utils.Clipboard;
- import com.badlogic.gdx.utils.FloatArray;
- import com.badlogic.gdx.utils.ObjectSet;
- import com.badlogic.gdx.utils.Pool;
- import com.badlogic.gdx.utils.Pools;
- import com.badlogic.gdx.utils.TimeUtils;
- import com.badlogic.gdx.utils.Timer;
- import com.mellotanica.game.utils.SelectListener;
- import static com.badlogic.gdx.scenes.scene2d.actions.Actions.fadeIn;
- import static com.badlogic.gdx.scenes.scene2d.actions.Actions.fadeOut;
- import static com.badlogic.gdx.scenes.scene2d.actions.Actions.removeActor;
- import static com.badlogic.gdx.scenes.scene2d.actions.Actions.sequence;
- /** A single-line text input field with dropdown menu search engine style.
- * <p>
- * The preferred height of a text field is the height of the {@link SearchFieldStyle#font} and {@link SearchFieldStyle#background}.
- * The preferred width of a text field is 150, a relatively arbitrary size.
- * <p>
- * The text field will copy the currently selected text when ctrl+c is pressed, and paste any text in the clipboard when ctrl+v is
- * pressed. Clipboard functionality is provided via the {@link Clipboard} interface. Currently there are two standard
- * implementations, one for the desktop and one for Android. The Android clipboard is a stub, as copy & pasting on Android is not
- * supported yet.
- * <p>
- * The text field allows you to specify an {@link OnscreenKeyboard} for displaying a softkeyboard and piping all key events
- * generated by the keyboard to the text field. There are two standard implementations, one for the desktop and one for Android.
- * The desktop keyboard is a stub, as a softkeyboard is not needed on the desktop. The Android {@link OnscreenKeyboard}
- * implementation will bring up the default IME.
- * @author mzechner
- * @author Nathan Sweet
- * @author mellotanica */
- public class SearchField<T> extends Widget implements Disableable {
- /* TextField fields */
- static private final char BACKSPACE = 8;
- static protected final char ENTER_DESKTOP = '\r';
- static protected final char ENTER_ANDROID = '\n';
- static private final char TAB = '\t';
- static private final char DELETE = 127;
- static private final char BULLET = 149;
- static private final Vector2 tmp1 = new Vector2();
- static private final Vector2 tmp2 = new Vector2();
- static private final Vector2 tmp3 = new Vector2();
- static public float keyRepeatInitialTime = 0.4f;
- static public float keyRepeatTime = 0.1f;
- protected String text;
- protected int cursor, selectionStart;
- protected boolean hasSelection;
- protected boolean writeEnters;
- protected final GlyphLayout layout = new GlyphLayout();
- protected final FloatArray glyphPositions = new FloatArray();
- SearchField.SearchFieldStyle style;
- private String messageText;
- protected CharSequence displayText;
- Clipboard clipboard;
- InputListener inputListener;
- SearchField.SearchFieldListener listener;
- SearchField.SearchFieldFilter filter;
- SearchField.OnscreenKeyboard keyboard = new SearchField.DefaultOnscreenKeyboard();
- boolean focusTraversal = true, onlyFontChars = true, disabled;
- private int textHAlign = Align.left;
- private float selectionX, selectionWidth;
- String undoText = "";
- long lastChangeTime;
- boolean passwordMode;
- private StringBuilder passwordBuffer;
- private char passwordCharacter = BULLET;
- protected float fontOffset, textHeight, textOffset;
- float renderOffset;
- private int visibleTextStart, visibleTextEnd;
- private int maxLength = 0;
- private float blinkTime = 0.32f;
- boolean cursorOn = true;
- long lastBlink;
- /* SelectBox fields */
- static final Vector2 temp = new Vector2();
- final Array<T> items = new Array();
- private float prefWidth, prefHeight;
- final ArraySelection<T> selection = new ArraySelection(items);
- SearchFieldList<T> searchFieldList;
- SearchField.KeyRepeatTask keyRepeatTask = new SearchField.KeyRepeatTask();
- boolean programmaticChangeEvents;
- /* SearchField fields */
- SearchFieldHelper updateHelper;
- public SearchField (String text, Skin skin) {
- this(text, skin.get(SearchField.SearchFieldStyle.class), null);
- }
- public SearchField (String text, Skin skin, SearchFieldHelper helper) {
- this(text, skin.get(SearchField.SearchFieldStyle.class), helper);
- }
- public SearchField (String text, Skin skin, String styleName) {
- this(text, skin.get(styleName, SearchField.SearchFieldStyle.class), null);
- }
- public SearchField (String text, Skin skin, String styleName, SearchFieldHelper helper) {
- this(text, skin.get(styleName, SearchField.SearchFieldStyle.class), helper);
- }
- public SearchField (String text, SearchField.SearchFieldStyle style, SearchFieldHelper helper) {
- setStyle(style);
- clipboard = Gdx.app.getClipboard();
- initialize();
- setText(text);
- setSize(getPrefWidth(), getPrefHeight());
- updateHelper = helper;
- searchFieldList = new SearchFieldList<T>(this);
- }
- protected void initialize () {
- addListener(inputListener = createInputListener());
- }
- protected InputListener createInputListener () {
- return new SearchField.SearchFieldClickListener();
- }
- protected int letterUnderCursor (float x) {
- x -= textOffset + fontOffset - style.font.getData().cursorX - glyphPositions.get(visibleTextStart);
- int n = this.glyphPositions.size;
- float[] glyphPositions = this.glyphPositions.items;
- for (int i = 1; i < n; i++) {
- if (glyphPositions[i] > x) {
- if (glyphPositions[i] - x <= x - glyphPositions[i - 1]) return i;
- return i - 1;
- }
- }
- return n - 1;
- }
- protected boolean isWordCharacter (char c) {
- return Character.isLetterOrDigit(c);
- }
- protected int[] wordUnderCursor (int at) {
- String text = this.text;
- int start = at, right = text.length(), left = 0, index = start;
- if(at>= text.length()){
- left = text.length();
- right = 0;
- }
- else {
- for (; index < right; index++) {
- if (!isWordCharacter(text.charAt(index))) {
- right = index;
- break;
- }
- }
- for (index = start - 1; index > -1; index--) {
- if (!isWordCharacter(text.charAt(index))) {
- left = index + 1;
- break;
- }
- }
- }
- return new int[] {left, right};
- }
- int[] wordUnderCursor (float x) {
- return wordUnderCursor(letterUnderCursor(x));
- }
- boolean withinMaxLength (int size) {
- return maxLength <= 0 || size < maxLength;
- }
- public void setMaxLength (int maxLength) {
- this.maxLength = maxLength;
- }
- public int getMaxLength () {
- return this.maxLength;
- }
- /** When false, text set by {@link #setText(String)} may contain characters not in the font, a space will be displayed instead.
- * When true (the default), characters not in the font are stripped by setText. Characters not in the font are always stripped
- * when typed or pasted. */
- public void setOnlyFontChars (boolean onlyFontChars) {
- this.onlyFontChars = onlyFontChars;
- }
- public void setStyle (SearchField.SearchFieldStyle style) {
- if (style == null) throw new IllegalArgumentException("style cannot be null.");
- this.style = style;
- textHeight = style.font.getCapHeight() - style.font.getDescent() * 2;
- invalidateHierarchy();
- }
- /** Returns the text field's style. Modifying the returned style may not have an effect until {@link #setStyle(SearchField.SearchFieldStyle)}
- * is called. */
- public SearchField.SearchFieldStyle getStyle () {
- return style;
- }
- protected void calculateOffsets () {
- float visibleWidth = getWidth();
- if (style.background != null) visibleWidth -= style.background.getLeftWidth() + style.background.getRightWidth();
- int glyphCount = glyphPositions.size;
- float[] glyphPositions = this.glyphPositions.items;
- // Check if the cursor has gone out the left or right side of the visible area and adjust renderOffset.
- float distance = glyphPositions[Math.max(0, cursor - 1)] + renderOffset;
- if (distance <= 0)
- renderOffset -= distance;
- else {
- int index = Math.min(glyphCount - 1, cursor + 1);
- float minX = glyphPositions[index] - visibleWidth;
- if (-renderOffset < minX) renderOffset = -minX;
- }
- // Prevent renderOffset from starting too close to the end, eg after text was deleted.
- float maxOffset = 0;
- float width = glyphPositions[glyphCount - 1];
- for (int i = glyphCount - 2; i >= 0; i--) {
- float x = glyphPositions[i];
- if (width - x > visibleWidth) break;
- maxOffset = x;
- }
- if (-renderOffset > maxOffset) renderOffset = -maxOffset;
- // calculate first visible char based on render offset
- visibleTextStart = 0;
- float startX = 0;
- for (int i = 0; i < glyphCount; i++) {
- if (glyphPositions[i] >= -renderOffset) {
- visibleTextStart = Math.max(0, i);
- startX = glyphPositions[i];
- break;
- }
- }
- // calculate last visible char based on visible width and render offset
- int length = displayText.length();
- visibleTextEnd = Math.min(length, cursor + 1);
- for (; visibleTextEnd <= length; visibleTextEnd++)
- if (glyphPositions[visibleTextEnd] > startX + visibleWidth) break;
- visibleTextEnd = Math.max(0, visibleTextEnd - 1);
- if ((textHAlign & Align.left) == 0) {
- textOffset = visibleWidth - (glyphPositions[visibleTextEnd] - startX);
- if ((textHAlign & Align.center) != 0) textOffset = Math.round(textOffset * 0.5f);
- } else
- textOffset = startX + renderOffset;
- // calculate selection x position and width
- if (hasSelection) {
- int minIndex = Math.min(cursor, selectionStart);
- int maxIndex = Math.max(cursor, selectionStart);
- float minX = Math.max(glyphPositions[minIndex] - glyphPositions[visibleTextStart], -textOffset);
- float maxX = Math.min(glyphPositions[maxIndex] - glyphPositions[visibleTextStart], visibleWidth - textOffset);
- selectionX = minX;
- selectionWidth = maxX - minX - style.font.getData().cursorX;
- }
- }
- @Override
- public void draw (Batch batch, float parentAlpha) {
- Stage stage = getStage();
- boolean focused = stage != null && stage.getKeyboardFocus() == this;
- if (!focused) keyRepeatTask.cancel();
- final BitmapFont font = style.font;
- final Color fontColor = (disabled && style.disabledFontColor != null) ? style.disabledFontColor
- : ((focused && style.focusedFontColor != null) ? style.focusedFontColor : style.fontColor);
- final Drawable selection = style.selection;
- final Drawable cursorPatch = style.cursor;
- final Drawable background = (disabled && style.disabledBackground != null) ? style.disabledBackground
- : ((focused && style.focusedBackground != null) ? style.focusedBackground : style.background);
- Color color = getColor();
- float x = getX();
- float y = getY();
- float width = getWidth();
- float height = getHeight();
- batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
- float bgLeftWidth = 0, bgRightWidth = 0;
- if (background != null) {
- background.draw(batch, x, y, width, height);
- bgLeftWidth = background.getLeftWidth();
- bgRightWidth = background.getRightWidth();
- }
- float textY = getTextY(font, background);
- calculateOffsets();
- if (focused && hasSelection && selection != null) {
- drawSelection(selection, batch, font, x + bgLeftWidth, y + textY);
- }
- float yOffset = font.isFlipped() ? -textHeight : 0;
- if (displayText.length() == 0) {
- if (!focused && messageText != null) {
- if (style.messageFontColor != null) {
- font.setColor(style.messageFontColor.r, style.messageFontColor.g, style.messageFontColor.b,
- style.messageFontColor.a * color.a * parentAlpha);
- } else
- font.setColor(0.7f, 0.7f, 0.7f, color.a * parentAlpha);
- BitmapFont messageFont = style.messageFont != null ? style.messageFont : font;
- messageFont.draw(batch, messageText, x + bgLeftWidth, y + textY + yOffset, 0, messageText.length(),
- width - bgLeftWidth - bgRightWidth, textHAlign, false, "...");
- }
- } else {
- font.setColor(fontColor.r, fontColor.g, fontColor.b, fontColor.a * color.a * parentAlpha);
- drawText(batch, font, x + bgLeftWidth, y + textY + yOffset);
- }
- if (focused && !disabled) {
- blink();
- if (cursorOn && cursorPatch != null) {
- drawCursor(cursorPatch, batch, font, x + bgLeftWidth, y + textY);
- }
- }
- }
- protected float getTextY (BitmapFont font, Drawable background) {
- float height = getHeight();
- float textY = textHeight / 2 + font.getDescent();
- if (background != null) {
- float bottom = background.getBottomHeight();
- textY = textY + (height - background.getTopHeight() - bottom) / 2 + bottom;
- } else {
- textY = textY + height / 2;
- }
- if (font.usesIntegerPositions()) textY = (int)textY;
- return textY;
- }
- /** Draws selection rectangle **/
- protected void drawSelection (Drawable selection, Batch batch, BitmapFont font, float x, float y) {
- selection.draw(batch, x + textOffset + selectionX + fontOffset, y - textHeight - font.getDescent(), selectionWidth,
- textHeight);
- }
- protected void drawText (Batch batch, BitmapFont font, float x, float y) {
- font.draw(batch, displayText, x + textOffset, y, visibleTextStart, visibleTextEnd, 0, Align.left, false);
- }
- protected void drawCursor (Drawable cursorPatch, Batch batch, BitmapFont font, float x, float y) {
- cursorPatch.draw(batch,
- x + textOffset + glyphPositions.get(cursor) - glyphPositions.get(visibleTextStart) + fontOffset + font.getData().cursorX,
- y - textHeight - font.getDescent(), cursorPatch.getMinWidth(), textHeight);
- }
- void updateDisplayText () {
- BitmapFont font = style.font;
- BitmapFont.BitmapFontData data = font.getData();
- String text = this.text;
- int textLength = text.length();
- StringBuilder buffer = new StringBuilder();
- for (int i = 0; i < textLength; i++) {
- char c = text.charAt(i);
- buffer.append(data.hasGlyph(c) ? c : ' ');
- }
- String newDisplayText = buffer.toString();
- if (passwordMode && data.hasGlyph(passwordCharacter)) {
- if (passwordBuffer == null) passwordBuffer = new StringBuilder(newDisplayText.length());
- if (passwordBuffer.length() > textLength)
- passwordBuffer.setLength(textLength);
- else {
- for (int i = passwordBuffer.length(); i < textLength; i++)
- passwordBuffer.append(passwordCharacter);
- }
- displayText = passwordBuffer;
- } else
- displayText = newDisplayText;
- layout.setText(font, displayText);
- glyphPositions.clear();
- float x = 0;
- if (layout.runs.size > 0) {
- GlyphLayout.GlyphRun run = layout.runs.first();
- FloatArray xAdvances = run.xAdvances;
- fontOffset = xAdvances.first();
- for (int i = 1, n = xAdvances.size; i < n; i++) {
- glyphPositions.add(x);
- x += xAdvances.get(i);
- }
- } else
- fontOffset = 0;
- glyphPositions.add(x);
- if (selectionStart > newDisplayText.length()) selectionStart = textLength;
- if(updateHelper != null){
- setItems(updateHelper.search(text));
- hideList();
- showList();
- }
- }
- private void blink () {
- if (!Gdx.graphics.isContinuousRendering()) {
- cursorOn = true;
- return;
- }
- long time = TimeUtils.nanoTime();
- if ((time - lastBlink) / 1000000000.0f > blinkTime) {
- cursorOn = !cursorOn;
- lastBlink = time;
- }
- }
- /** Copies the contents of this SearchField to the {@link Clipboard} implementation set on this SearchField. */
- public void copy () {
- if (hasSelection && !passwordMode) {
- clipboard.setContents(text.substring(Math.min(cursor, selectionStart), Math.max(cursor, selectionStart)));
- }
- }
- /** Copies the selected contents of this SearchField to the {@link Clipboard} implementation set on this SearchField, then removes
- * it. */
- public void cut () {
- cut(programmaticChangeEvents);
- }
- void cut (boolean fireChangeEvent) {
- if (hasSelection && !passwordMode) {
- copy();
- cursor = delete(fireChangeEvent);
- updateDisplayText();
- }
- }
- void paste (String content, boolean fireChangeEvent) {
- if (content == null) return;
- StringBuilder buffer = new StringBuilder();
- int textLength = text.length();
- if (hasSelection) textLength -= Math.abs(cursor - selectionStart);
- BitmapFont.BitmapFontData data = style.font.getData();
- for (int i = 0, n = content.length(); i < n; i++) {
- if (!withinMaxLength(textLength + buffer.length())) break;
- char c = content.charAt(i);
- if (!(writeEnters && (c == ENTER_ANDROID || c == ENTER_DESKTOP))) {
- if (c == '\r' || c == '\n') continue;
- if (onlyFontChars && !data.hasGlyph(c)) continue;
- if (filter != null && !filter.acceptChar(this, c)) continue;
- }
- buffer.append(c);
- }
- content = buffer.toString();
- if (hasSelection) cursor = delete(fireChangeEvent);
- if (fireChangeEvent)
- changeText(text, insert(cursor, content, text));
- else
- text = insert(cursor, content, text);
- updateDisplayText();
- cursor += content.length();
- }
- String insert (int position, CharSequence text, String to) {
- if (to.length() == 0) return text.toString();
- return to.substring(0, position) + text + to.substring(position, to.length());
- }
- int delete (boolean fireChangeEvent) {
- int from = selectionStart;
- int to = cursor;
- int minIndex = Math.min(from, to);
- int maxIndex = Math.max(from, to);
- String newText = (minIndex > 0 ? text.substring(0, minIndex) : "")
- + (maxIndex < text.length() ? text.substring(maxIndex, text.length()) : "");
- if (fireChangeEvent)
- changeText(text, newText);
- else
- text = newText;
- clearSelection();
- return minIndex;
- }
- /** Focuses the next SearchField. If none is found, the keyboard is hidden. Does nothing if the text field is not in a stage.
- * @param up If true, the SearchField with the same or next smallest y coordinate is found, else the next highest. */
- public void next (boolean up) {
- Stage stage = getStage();
- if (stage == null) return;
- SearchField current = this;
- while (true) {
- current.getParent().localToStageCoordinates(tmp1.set(getX(), getY()));
- SearchField textField = current.findNextSearchField(stage.getActors(), null, tmp2, tmp1, up);
- if (textField == null) { // Try to wrap around.
- if (up)
- tmp1.set(Float.MIN_VALUE, Float.MIN_VALUE);
- else
- tmp1.set(Float.MAX_VALUE, Float.MAX_VALUE);
- textField = current.findNextSearchField(getStage().getActors(), null, tmp2, tmp1, up);
- }
- if (textField == null) {
- Gdx.input.setOnscreenKeyboardVisible(false);
- break;
- }
- if (stage.setKeyboardFocus(textField)) break;
- current = textField;
- }
- }
- private SearchField findNextSearchField (Array<Actor> actors, SearchField best, Vector2 bestCoords, Vector2 currentCoords,
- boolean up) {
- for (int i = 0, n = actors.size; i < n; i++) {
- Actor actor = actors.get(i);
- if (actor == this) continue;
- if (actor instanceof SearchField) {
- SearchField textField = (SearchField)actor;
- if (textField.isDisabled() || !textField.focusTraversal) continue;
- Vector2 actorCoords = actor.getParent().localToStageCoordinates(tmp3.set(actor.getX(), actor.getY()));
- if ((actorCoords.y < currentCoords.y || (actorCoords.y == currentCoords.y && actorCoords.x > currentCoords.x)) ^ up) {
- if (best == null
- || (actorCoords.y > bestCoords.y || (actorCoords.y == bestCoords.y && actorCoords.x < bestCoords.x)) ^ up) {
- best = (SearchField)actor;
- bestCoords.set(actorCoords);
- }
- }
- } else if (actor instanceof Group)
- best = findNextSearchField(((Group)actor).getChildren(), best, bestCoords, currentCoords, up);
- }
- return best;
- }
- public InputListener getDefaultInputListener () {
- return inputListener;
- }
- /** @param listener May be null. */
- public void setSearchFieldListener (SearchField.SearchFieldListener listener) {
- this.listener = listener;
- }
- /** @param filter May be null. */
- public void setSearchFieldFilter (SearchField.SearchFieldFilter filter) {
- this.filter = filter;
- }
- public SearchField.SearchFieldFilter getSearchFieldFilter () {
- return filter;
- }
- /** If true (the default), tab/shift+tab will move to the next text field. */
- public void setFocusTraversal (boolean focusTraversal) {
- this.focusTraversal = focusTraversal;
- }
- /** @return May be null. */
- public String getMessageText () {
- return messageText;
- }
- /** Sets the text that will be drawn in the text field if no text has been entered.
- * @param messageText may be null. */
- public void setMessageText (String messageText) {
- this.messageText = messageText;
- }
- /** @param str If null, "" is used. */
- public void appendText (String str) {
- if (str == null) str = "";
- clearSelection();
- cursor = text.length();
- paste(str, programmaticChangeEvents);
- }
- /** @param str If null, "" is used. */
- public void setText (String str) {
- if (str == null) str = "";
- if (str.equals(text)) return;
- clearSelection();
- String oldText = text;
- text = "";
- paste(str, false);
- if (programmaticChangeEvents) changeText(oldText, text);
- cursor = 0;
- }
- /** @return Never null, might be an empty string. */
- public String getText () {
- return text;
- }
- /** @param oldText May be null.
- * @return True if the text was changed. */
- boolean changeText (String oldText, String newText) {
- if (newText.equals(oldText)) return false;
- text = newText;
- ChangeListener.ChangeEvent changeEvent = Pools.obtain(ChangeListener.ChangeEvent.class);
- boolean cancelled = fire(changeEvent);
- text = cancelled ? oldText : newText;
- Pools.free(changeEvent);
- return !cancelled;
- }
- /** If false, methods that change the text will not fire {@link ChangeListener.ChangeEvent}, the event will be fired only when user changes
- * the text. */
- public void setProgrammaticChangeEvents (boolean programmaticChangeEvents) {
- this.programmaticChangeEvents = programmaticChangeEvents;
- }
- public boolean getProgrammaticChangeEvents () {
- return programmaticChangeEvents;
- }
- public int getSelectionStart () {
- return selectionStart;
- }
- public String getSelection () {
- return hasSelection ? text.substring(Math.min(selectionStart, cursor), Math.max(selectionStart, cursor)) : "";
- }
- /** Sets the selected text. */
- public void setSelection (int selectionStart, int selectionEnd) {
- if (selectionStart < 0) throw new IllegalArgumentException("selectionStart must be >= 0");
- if (selectionEnd < 0) throw new IllegalArgumentException("selectionEnd must be >= 0");
- selectionStart = Math.min(text.length(), selectionStart);
- selectionEnd = Math.min(text.length(), selectionEnd);
- if (selectionEnd == selectionStart) {
- clearSelection();
- return;
- }
- if (selectionEnd < selectionStart) {
- int temp = selectionEnd;
- selectionEnd = selectionStart;
- selectionStart = temp;
- }
- hasSelection = true;
- this.selectionStart = selectionStart;
- cursor = selectionEnd;
- }
- public void selectAll () {
- setSelection(0, text.length());
- }
- public void clearSelection () {
- hasSelection = false;
- }
- /** Sets the cursor position and clears any selection. */
- public void setCursorPosition (int cursorPosition) {
- if (cursorPosition < 0) throw new IllegalArgumentException("cursorPosition must be >= 0");
- clearSelection();
- cursor = Math.min(cursorPosition, text.length());
- }
- public int getCursorPosition () {
- return cursor;
- }
- /** Default is an instance of {@link SearchField.DefaultOnscreenKeyboard}. */
- public SearchField.OnscreenKeyboard getOnscreenKeyboard () {
- return keyboard;
- }
- public void setOnscreenKeyboard (SearchField.OnscreenKeyboard keyboard) {
- this.keyboard = keyboard;
- }
- public void setClipboard (Clipboard clipboard) {
- this.clipboard = clipboard;
- }
- /** Sets text horizontal alignment (left, center or right).
- * @see Align */
- public void setAlignment (int alignment) {
- this.textHAlign = alignment;
- }
- /** If true, the text in this text field will be shown as bullet characters.
- * @see #setPasswordCharacter(char) */
- public void setPasswordMode (boolean passwordMode) {
- this.passwordMode = passwordMode;
- updateDisplayText();
- }
- public boolean isPasswordMode () {
- return passwordMode;
- }
- /** Sets the password character for the text field. The character must be present in the {@link BitmapFont}. Default is 149
- * (bullet). */
- public void setPasswordCharacter (char passwordCharacter) {
- this.passwordCharacter = passwordCharacter;
- if (passwordMode) updateDisplayText();
- }
- public void setBlinkTime (float blinkTime) {
- this.blinkTime = blinkTime;
- }
- public void setDisabled (boolean disabled) {
- if (disabled && !this.disabled) hideList();
- this.disabled = disabled;
- }
- public boolean isDisabled () {
- return disabled;
- }
- protected void moveCursor (boolean forward, boolean jump) {
- int limit = forward ? text.length() : 0;
- int charOffset = forward ? 0 : -1;
- while ((forward ? ++cursor < limit : --cursor > limit) && jump) {
- if (!continueCursor(cursor, charOffset)) break;
- }
- }
- protected boolean continueCursor (int index, int offset) {
- char c = text.charAt(index + offset);
- return isWordCharacter(c);
- }
- class KeyRepeatTask extends Timer.Task {
- int keycode;
- public void run () {
- inputListener.keyDown(null, keycode);
- }
- }
- /** Interface for listening to typed characters.
- * @author mzechner */
- static public interface SearchFieldListener {
- public void keyTyped (SearchField textField, char c);
- }
- /** Interface for filtering characters entered into the text field.
- * @author mzechner */
- static public interface SearchFieldFilter {
- public boolean acceptChar (SearchField textField, char c);
- static public class DigitsOnlyFilter implements SearchField.SearchFieldFilter {
- @Override
- public boolean acceptChar (SearchField textField, char c) {
- return Character.isDigit(c);
- }
- }
- }
- /** An interface for onscreen keyboards. Can invoke the default keyboard or render your own keyboard!
- * @author mzechner */
- static public interface OnscreenKeyboard {
- public void show (boolean visible);
- }
- /** The default {@link SearchField.OnscreenKeyboard} used by all {@link SearchField} instances. Just uses
- * {@link Input#setOnscreenKeyboardVisible(boolean)} as appropriate. Might overlap your actual rendering, so use with care!
- * @author mzechner */
- static public class DefaultOnscreenKeyboard implements SearchField.OnscreenKeyboard {
- @Override
- public void show (boolean visible) {
- Gdx.input.setOnscreenKeyboardVisible(visible);
- }
- }
- /** Basic input listener for the text field */
- public class SearchFieldClickListener extends ClickListener {
- public void clicked (InputEvent event, float x, float y) {
- int count = getTapCount() % 4;
- if (count == 0) clearSelection();
- if (count == 2) {
- int[] array = wordUnderCursor(x);
- setSelection(array[0], array[1]);
- }
- if (count == 3) selectAll();
- }
- public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
- if (!super.touchDown(event, x, y, pointer, button)) return false;
- if (pointer == 0 && button != 0) return false;
- if (disabled) return true;
- setCursorPosition(x, y);
- selectionStart = cursor;
- Stage stage = getStage();
- if (stage != null) stage.setKeyboardFocus(SearchField.this);
- keyboard.show(true);
- hasSelection = true;
- return true;
- }
- public void touchDragged (InputEvent event, float x, float y, int pointer) {
- super.touchDragged(event, x, y, pointer);
- setCursorPosition(x, y);
- }
- public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
- if (selectionStart == cursor) hasSelection = false;
- super.touchUp(event, x, y, pointer, button);
- }
- protected void setCursorPosition (float x, float y) {
- lastBlink = 0;
- cursorOn = false;
- cursor = letterUnderCursor(x);
- }
- protected void goHome (boolean jump) {
- cursor = 0;
- }
- protected void goEnd (boolean jump) {
- cursor = text.length();
- }
- public boolean keyDown (InputEvent event, int keycode) {
- if (disabled) return false;
- lastBlink = 0;
- cursorOn = false;
- Stage stage = getStage();
- if (stage == null || stage.getKeyboardFocus() != SearchField.this) return false;
- boolean repeat = false;
- boolean ctrl = UIUtils.ctrl();
- boolean jump = ctrl && !passwordMode;
- if (ctrl) {
- if (keycode == Input.Keys.V) {
- paste(clipboard.getContents(), true);
- repeat = true;
- }
- if (keycode == Input.Keys.C || keycode == Input.Keys.INSERT) {
- copy();
- return true;
- }
- if (keycode == Input.Keys.X) {
- cut(true);
- return true;
- }
- if (keycode == Input.Keys.A) {
- selectAll();
- return true;
- }
- if (keycode == Input.Keys.Z) {
- String oldText = text;
- setText(undoText);
- undoText = oldText;
- updateDisplayText();
- return true;
- }
- }
- if (UIUtils.shift()) {
- if (keycode == Input.Keys.INSERT) paste(clipboard.getContents(), true);
- if (keycode == Input.Keys.FORWARD_DEL) cut(true);
- selection:
- {
- int temp = cursor;
- keys:
- {
- if (keycode == Input.Keys.LEFT) {
- moveCursor(false, jump);
- repeat = true;
- break keys;
- }
- if (keycode == Input.Keys.RIGHT) {
- moveCursor(true, jump);
- repeat = true;
- break keys;
- }
- if (keycode == Input.Keys.HOME) {
- goHome(jump);
- break keys;
- }
- if (keycode == Input.Keys.END) {
- goEnd(jump);
- break keys;
- }
- break selection;
- }
- if (!hasSelection) {
- selectionStart = temp;
- hasSelection = true;
- }
- }
- } else {
- // Cursor movement or other keys (kills selection).
- if (keycode == Input.Keys.LEFT) {
- moveCursor(false, jump);
- clearSelection();
- repeat = true;
- }
- if (keycode == Input.Keys.RIGHT) {
- moveCursor(true, jump);
- clearSelection();
- repeat = true;
- }
- if (keycode == Input.Keys.HOME) {
- goHome(jump);
- clearSelection();
- }
- if (keycode == Input.Keys.END) {
- goEnd(jump);
- clearSelection();
- }
- }
- cursor = MathUtils.clamp(cursor, 0, text.length());
- if (repeat) {
- scheduleKeyRepeatTask(keycode);
- }
- return true;
- }
- protected void scheduleKeyRepeatTask (int keycode) {
- if (!keyRepeatTask.isScheduled() || keyRepeatTask.keycode != keycode) {
- keyRepeatTask.keycode = keycode;
- keyRepeatTask.cancel();
- Timer.schedule(keyRepeatTask, keyRepeatInitialTime, keyRepeatTime);
- }
- }
- public boolean keyUp (InputEvent event, int keycode) {
- if (disabled) return false;
- keyRepeatTask.cancel();
- return true;
- }
- public boolean keyTyped (InputEvent event, char character) {
- if (disabled) return false;
- // Disallow "typing" most ASCII control characters, which would show up as a space when onlyFontChars is true.
- switch (character) {
- case BACKSPACE:
- case TAB:
- case ENTER_ANDROID:
- case ENTER_DESKTOP:
- break;
- default:
- if (character < 32) return false;
- }
- Stage stage = getStage();
- if (stage == null || stage.getKeyboardFocus() != SearchField.this) return false;
- if (UIUtils.isMac && Gdx.input.isKeyPressed(Input.Keys.SYM)) return true;
- if ((character == TAB || character == ENTER_ANDROID) && focusTraversal) {
- next(UIUtils.shift());
- } else {
- boolean delete = character == DELETE;
- boolean backspace = character == BACKSPACE;
- boolean enter = character == ENTER_DESKTOP || character == ENTER_ANDROID;
- boolean add = enter ? writeEnters : (!onlyFontChars || style.font.getData().hasGlyph(character));
- boolean remove = backspace || delete;
- if (add || remove) {
- String oldText = text;
- int oldCursor = cursor;
- if (hasSelection)
- cursor = delete(false);
- else {
- if (backspace && cursor > 0) {
- text = text.substring(0, cursor - 1) + text.substring(cursor--);
- renderOffset = 0;
- }
- if (delete && cursor < text.length()) {
- text = text.substring(0, cursor) + text.substring(cursor + 1);
- }
- }
- if (add && !remove) {
- // Character may be added to the text.
- if (!enter && filter != null && !filter.acceptChar(SearchField.this, character)) return true;
- if (!withinMaxLength(text.length())) return true;
- String insertion = enter ? "\n" : String.valueOf(character);
- text = insert(cursor++, insertion, text);
- }
- String tempUndoText = undoText;
- if (changeText(oldText, text)) {
- long time = System.currentTimeMillis();
- if (time - 750 > lastChangeTime) undoText = oldText;
- lastChangeTime = time;
- } else
- cursor = oldCursor;
- updateDisplayText();
- }
- }
- if (listener != null) listener.keyTyped(SearchField.this, character);
- return true;
- }
- }
- /* Methods from SearchField */
- /** Set the backing Array that makes up the choices available in the SearchField */
- public void setItems (T... newItems) {
- if (newItems == null) throw new IllegalArgumentException("newItems cannot be null.");
- float oldPrefWidth = getPrefWidth();
- items.clear();
- items.addAll(newItems);
- selection.validate();
- searchFieldList.list.setItems(items);
- invalidate();
- if (oldPrefWidth != getPrefWidth()) invalidateHierarchy();
- }
- /** Sets the items visible in the select box. */
- public void setItems (Array<T> newItems) {
- if (newItems == null) throw new IllegalArgumentException("newItems cannot be null.");
- float oldPrefWidth = getPrefWidth();
- items.clear();
- items.addAll(newItems);
- selection.validate();
- searchFieldList.list.setItems(items);
- invalidate();
- if (oldPrefWidth != getPrefWidth()) invalidateHierarchy();
- }
- public void clearItems () {
- if (items.size == 0) return;
- items.clear();
- selection.clear();
- invalidateHierarchy();
- }
- /** Returns the internal items array. If modified, {@link #setItems(Array)} must be called to reflect the changes. */
- public Array<T> getItems () {
- return items;
- }
- /** Get the set of selected items, useful when multiple items are selected
- * @return a Selection object containing the selected elements */
- public ArraySelection<T> getItemSelection () {
- return selection;
- }
- /** Returns the first selected item, or null. For multiple selections use {@link SearchField#getSelection()}. */
- public T getSelected () {
- return selection.first();
- }
- /** Sets the selection to only the passed item and fire a ChangedEvent. */
- public void setSelected (T item) {
- if (items.contains(item, false)) {
- selection.set(item);
- SelectListener.SelectEvent event = Pools.obtain(SelectListener.SelectEvent.class);
- boolean cancelled = fire(event);
- if(!cancelled){
- setText(item.toString());
- }
- Pools.free(event);
- } else
- selection.clear();
- }
- /** @return The index of the first selected item. The top item has an index of 0. Nothing selected has an index of -1. */
- public int getSelectedIndex () {
- ObjectSet<T> selected = selection.items();
- return selected.size == 0 ? -1 : items.indexOf(selected.first(), false);
- }
- /** Sets the selection to only the selected index. */
- public void setSelectedIndex (int index) {
- selection.set(items.get(index));
- }
- public void showList () {
- if (items.size == 0) return;
- searchFieldList.show(getStage());
- }
- public void hideList () {
- searchFieldList.hide();
- }
- /** Returns the list shown when the select box is open. */
- public List<T> getList () {
- return searchFieldList.list;
- }
- /** Disables scrolling of the list shown when the select box is open. */
- public void setScrollingDisabled (boolean y) {
- searchFieldList.setScrollingDisabled(true, y);
- invalidateHierarchy();
- }
- /** Returns the scroll pane containing the list that is shown when the select box is open. */
- public ScrollPane getScrollPane () {
- return searchFieldList;
- }
- protected void onShow (Actor searchFieldList, boolean below) {
- searchFieldList.getColor().a = 0;
- searchFieldList.addAction(fadeIn(0.3f, Interpolation.fade));
- }
- protected void onHide (Actor searchFieldList) {
- searchFieldList.getColor().a = 1;
- searchFieldList.addAction(sequence(fadeOut(0.15f, Interpolation.fade), removeActor()));
- }
- protected String toString (T obj) {
- return obj.toString();
- }
- public float getPrefWidth () {
- validate();
- return prefWidth;
- }
- public float getPrefHeight () {
- validate();
- return prefHeight;
- }
- @Override
- public void layout () {
- Drawable bg = style.background;
- BitmapFont font = style.font;
- if (bg != null) {
- prefHeight = Math.max(bg.getTopHeight() + bg.getBottomHeight() + font.getCapHeight() - font.getDescent() * 2,
- bg.getMinHeight());
- } else
- prefHeight = font.getCapHeight() - font.getDescent() * 2;
- float maxItemWidth = 0;
- Pool<GlyphLayout> layoutPool = Pools.get(GlyphLayout.class);
- GlyphLayout layout = layoutPool.obtain();
- for (int i = 0; i < items.size; i++) {
- layout.setText(font, toString(items.get(i)));
- maxItemWidth = Math.max(layout.width, maxItemWidth);
- }
- layoutPool.free(layout);
- prefWidth = maxItemWidth;
- if (bg != null) prefWidth += bg.getLeftWidth() + bg.getRightWidth();
- List.ListStyle listStyle = style.listStyle;
- ScrollPane.ScrollPaneStyle scrollStyle = style.scrollStyle;
- float listWidth = maxItemWidth + listStyle.selection.getLeftWidth() + listStyle.selection.getRightWidth();
- if (scrollStyle.background != null)
- listWidth += scrollStyle.background.getLeftWidth() + scrollStyle.background.getRightWidth();
- if (searchFieldList == null || !searchFieldList.isScrollingDisabledY())
- listWidth += Math.max(style.scrollStyle.vScroll != null ? style.scrollStyle.vScroll.getMinWidth() : 0,
- style.scrollStyle.vScrollKnob != null ? style.scrollStyle.vScrollKnob.getMinWidth() : 0);
- prefWidth = Math.max(prefWidth, listWidth);
- }
- /** @author Nathan Sweet
- * @author mellotanica */
- static class SearchFieldList<T> extends ScrollPane {
- private final SearchField<T> searchField;
- int maxListCount;
- private final Vector2 screenPosition = new Vector2();
- final List<T> list;
- private InputListener hideListener;
- private Actor previousScrollFocus;
- private boolean forceBelow;
- public SearchFieldList (final SearchField<T> searchField) {
- super(null, searchField.style.scrollStyle);
- this.searchField = searchField;
- setOverscroll(false, false);
- setFadeScrollBars(false);
- setScrollingDisabled(true, false);
- this.forceBelow = searchField.style.forceBelow;
- list = new List<T>(searchField.style.listStyle) {
- @Override
- protected String toString (T obj) {
- return searchField.toString(obj);
- }
- };
- list.setTouchable(Touchable.disabled);
- setWidget(list);
- list.addListener(new ClickListener() {
- public void clicked (InputEvent event, float x, float y) {
- searchField.setSelected(list.getSelected());
- hide();
- }
- public boolean mouseMoved (InputEvent event, float x, float y) {
- list.setSelectedIndex(Math.min(searchField.items.size - 1, (int)((list.getHeight() - y) / list.getItemHeight())));
- return true;
- }
- });
- addListener(new InputListener() {
- public void exit (InputEvent event, float x, float y, int pointer, Actor toActor) {
- if (toActor == null || !isAscendantOf(toActor)) list.setSelected(searchField.getSelected());
- }
- });
- hideListener = new InputListener() {
- public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
- Actor target = event.getTarget();
- if (isAscendantOf(target)) return false;
- list.setSelected(searchField.getSelected());
- hide();
- return false;
- }
- public boolean keyDown (InputEvent event, int keycode) {
- if (keycode == Input.Keys.ESCAPE) hide();
- return false;
- }
- };
- }
- public void show (Stage stage) {
- if (list.isTouchable()) return;
- stage.removeCaptureListener(hideListener);
- stage.addCaptureListener(hideListener);
- stage.addActor(this);
- searchField.localToStageCoordinates(screenPosition.set(0, 0));
- // Show the list above or below the select box, limited to a number of items and the available height in the stage.
- float itemHeight = list.getItemHeight();
- float height = itemHeight * (maxListCount <= 0 ? searchField.items.size : Math.min(maxListCount, searchField.items.size));
- Drawable scrollPaneBackground = getStyle().background;
- if (scrollPaneBackground != null) height += scrollPaneBackground.getTopHeight() + scrollPaneBackground.getBottomHeight();
- Drawable listBackground = list.getStyle().background;
- if (listBackground != null) height += listBackground.getTopHeight() + listBackground.getBottomHeight();
- float heightBelow = screenPosition.y;
- float heightAbove = stage.getCamera().viewportHeight - screenPosition.y - searchField.getHeight();
- boolean below = true;
- if (height > heightBelow) {
- if (heightAbove > heightBelow && !forceBelow) {
- below = false;
- height = Math.min(height, heightAbove);
- } else
- height = heightBelow;
- }
- if (below)
- setY(screenPosition.y - height);
- else
- setY(screenPosition.y + searchField.getHeight());
- setX(screenPosition.x);
- setHeight(height);
- validate();
- float innerWidth = getPrefWidth() + ((getPrefHeight() > height && !isScrollingDisabledY())? getScrollBarWidth() : 0);
- float width = Math.max(innerWidth, searchField.getWidth());
- setWidth(width);
- validate();
- scrollTo(0, list.getHeight() - searchField.getSelectedIndex() * itemHeight - itemHeight / 2, 0, 0, true, true);
- updateVisualScroll();
- previousScrollFocus = null;
- Actor actor = stage.getScrollFocus();
- if (actor != null && !actor.isDescendantOf(this)) previousScrollFocus = actor;
- stage.setScrollFocus(this);
- list.setSelected(searchField.getSelected());
- list.setTouchable(Touchable.enabled);
- clearActions();
- searchField.onShow(this, below);
- }
- public void hide () {
- if (!list.isTouchable() || !hasParent()) return;
- list.setTouchable(Touchable.disabled);
- Stage stage = getStage();
- if (stage != null) {
- stage.removeCaptureListener(hideListener);
- if (previousScrollFocus != null && previousScrollFocus.getStage() == null) previousScrollFocus = null;
- Actor actor = stage.getScrollFocus();
- if (actor == null || isAscendantOf(actor)) stage.setScrollFocus(previousScrollFocus);
- }
- clearActions();
- searchField.onHide(this);
- }
- public void draw (Batch batch, float parentAlpha) {
- searchField.localToStageCoordinates(temp.set(0, 0));
- if (!temp.equals(screenPosition)) hide();
- super.draw(batch, parentAlpha);
- }
- public void act (float delta) {
- super.act(delta);
- toFront();
- }
- }
- /** The style for a text field, see {@link SearchField}.
- * @author mzechner
- * @author Nathan Sweet
- * @author mellotanica */
- static public class SearchFieldStyle {
- public BitmapFont font;
- public ScrollPane.ScrollPaneStyle scrollStyle;
- public List.ListStyle listStyle;
- public Color fontColor;
- /** Optional. */
- public Color focusedFontColor, disabledFontColor;
- /** Optional. */
- public Drawable background, focusedBackground, disabledBackground, cursor, selection;
- /** Optional. */
- public BitmapFont messageFont;
- /** Optional. */
- public Color messageFontColor;
- /** Optional. */
- public boolean forceBelow;
- public SearchFieldStyle () {
- }
- public SearchFieldStyle (BitmapFont font, Color fontColor, Drawable cursor, Drawable selection, Drawable background,
- ScrollPane.ScrollPaneStyle scrollPaneStyle, List.ListStyle listStyle) {
- this.background = background;
- this.cursor = cursor;
- this.font = font;
- this.fontColor = fontColor;
- this.selection = selection;
- this.scrollStyle = scrollPaneStyle;
- this.listStyle = listStyle;
- }
- public SearchFieldStyle (SearchField.SearchFieldStyle style) {
- this.messageFont = style.messageFont;
- if (style.messageFontColor != null) this.messageFontColor = new Color(style.messageFontColor);
- this.background = style.background;
- this.focusedBackground = style.focusedBackground;
- this.disabledBackground = style.disabledBackground;
- this.cursor = style.cursor;
- this.font = style.font;
- if (style.fontColor != null) this.fontColor = new Color(style.fontColor);
- if (style.focusedFontColor != null) this.focusedFontColor = new Color(style.focusedFontColor);
- if (style.disabledFontColor != null) this.disabledFontColor = new Color(style.disabledFontColor);
- this.selection = style.selection;
- this.scrollStyle = new ScrollPane.ScrollPaneStyle(style.scrollStyle);
- this.listStyle = new List.ListStyle(style.listStyle);
- this.forceBelow = style.forceBelow;
- }
- }
- public interface SearchFieldHelper<T> {
- Array<T> search(CharSequence query);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment