Search in sources :

Example 1 with Focusable

use of de.johni0702.minecraft.gui.function.Focusable in project jGui by ReplayMod.

the class Utils method link.

/**
 * Link the specified focusable compontents in the order they're supplied.
 * @param focusables Focusables to link
 * @see Focusable#setNext(Focusable)
 * @see Focusable#setPrevious(Focusable)
 */
public static void link(Focusable... focusables) {
    checkArgument(new HashSet<>(Arrays.asList(focusables)).size() == focusables.length, "focusables must be unique and not null");
    for (int i = 0; i < focusables.length; i++) {
        Focusable next = focusables[(i + 1) % focusables.length];
        focusables[i].setNext(next);
        next.setPrevious(focusables[i]);
    }
}
Also used : Focusable(de.johni0702.minecraft.gui.function.Focusable)

Example 2 with Focusable

use of de.johni0702.minecraft.gui.function.Focusable in project jGui by ReplayMod.

the class AbstractGuiTextArea method typeKey.

@Override
public boolean typeKey(ReadablePoint mousePosition, int keyCode, char keyChar, boolean ctrlDown, boolean shiftDown) {
    if (keyCode == Keyboard.KEY_TAB) {
        Focusable other = shiftDown ? previous : next;
        if (other != null) {
            setFocused(false);
            other.setFocused(true);
        }
        return true;
    }
    if (!this.focused) {
        return false;
    }
    if (Screen.hasControlDown()) {
        switch(keyCode) {
            case // Select all
            Keyboard.KEY_A:
                cursorX = cursorY = 0;
                selectionY = text.length - 1;
                selectionX = text[selectionY].length();
                return true;
            case // Copy
            Keyboard.KEY_C:
                MCVer.setClipboardString(getSelectedText());
                return true;
            case // Paste
            Keyboard.KEY_V:
                if (isEnabled()) {
                    writeText(MCVer.getClipboardString());
                }
                return true;
            case // Cut
            Keyboard.KEY_X:
                if (isEnabled()) {
                    MCVer.setClipboardString(cutSelectedText());
                }
                return true;
        }
    }
    boolean words = Screen.hasControlDown();
    boolean select = Screen.hasShiftDown();
    switch(keyCode) {
        case Keyboard.KEY_HOME:
            cursorX = 0;
            break;
        case Keyboard.KEY_END:
            cursorX = text[cursorY].length();
            break;
        case Keyboard.KEY_LEFT:
            if (cursorX == 0) {
                if (cursorY > 0) {
                    cursorY--;
                    cursorX = text[cursorY].length();
                }
            } else {
                if (words) {
                    cursorX -= getPreviousWordLength();
                } else {
                    cursorX--;
                }
            }
            break;
        case Keyboard.KEY_RIGHT:
            if (cursorX == text[cursorY].length()) {
                if (cursorY < text.length - 1) {
                    cursorY++;
                    cursorX = 0;
                }
            } else {
                if (words) {
                    cursorX += getNextWordLength();
                } else {
                    cursorX++;
                }
            }
            break;
        case Keyboard.KEY_UP:
            if (cursorY > 0) {
                cursorY--;
                cursorX = Math.min(cursorX, text[cursorY].length());
            }
            break;
        case Keyboard.KEY_DOWN:
            if (cursorY + 1 < text.length) {
                cursorY++;
                cursorX = Math.min(cursorX, text[cursorY].length());
            }
            break;
        case Keyboard.KEY_BACK:
            if (isEnabled()) {
                if (getSelectedText().length() > 0) {
                    deleteSelectedText();
                } else if (words) {
                    deletePreviousWord();
                } else {
                    deletePreviousChar();
                }
            }
            return true;
        case Keyboard.KEY_DELETE:
            if (isEnabled()) {
                if (getSelectedText().length() > 0) {
                    deleteSelectedText();
                } else if (words) {
                    deleteNextWord();
                } else {
                    deleteNextChar();
                }
            }
            return true;
        default:
            if (isEnabled()) {
                if (keyChar == '\r') {
                    keyChar = '\n';
                }
                writeChar(keyChar);
            }
            return true;
    }
    if (!select) {
        selectionX = cursorX;
        selectionY = cursorY;
    }
    return true;
}
Also used : Focusable(de.johni0702.minecraft.gui.function.Focusable)

Example 3 with Focusable

use of de.johni0702.minecraft.gui.function.Focusable in project jGui by ReplayMod.

the class AbstractGuiTextField method typeKey.

@Override
public boolean typeKey(ReadablePoint mousePosition, int keyCode, char keyChar, boolean ctrlDown, boolean shiftDown) {
    if (!this.focused) {
        return false;
    }
    if (keyCode == Keyboard.KEY_TAB) {
        Focusable other = shiftDown ? previous : next;
        if (other != null) {
            setFocused(false);
            other.setFocused(true);
            // If the other field is a text field, by default select all its text (saves a Ctrl+A)
            if (other instanceof AbstractGuiTextField) {
                AbstractGuiTextField<?> field = (AbstractGuiTextField<?>) other;
                field.cursorPos = 0;
                field.selectionPos = field.text.length();
            }
        }
        return true;
    }
    if (keyCode == Keyboard.KEY_RETURN) {
        onEnter();
        return true;
    }
    String textBefore = text;
    try {
        if (Screen.hasControlDown()) {
            switch(keyCode) {
                case // Select all
                Keyboard.KEY_A:
                    cursorPos = 0;
                    selectionPos = text.length();
                    return true;
                case // Copy
                Keyboard.KEY_C:
                    MCVer.setClipboardString(getSelectedText());
                    return true;
                case // Paste
                Keyboard.KEY_V:
                    if (isEnabled()) {
                        writeText(MCVer.getClipboardString());
                    }
                    return true;
                case // Cut
                Keyboard.KEY_X:
                    if (isEnabled()) {
                        MCVer.setClipboardString(deleteSelectedText());
                    }
                    return true;
            }
        }
        boolean words = Screen.hasControlDown();
        boolean select = Screen.hasShiftDown();
        switch(keyCode) {
            case Keyboard.KEY_HOME:
                cursorPos = 0;
                break;
            case Keyboard.KEY_END:
                cursorPos = text.length();
                break;
            case Keyboard.KEY_LEFT:
                if (cursorPos != 0) {
                    if (words) {
                        cursorPos -= getPreviousWordLength();
                    } else {
                        cursorPos--;
                    }
                }
                break;
            case Keyboard.KEY_RIGHT:
                if (cursorPos != text.length()) {
                    if (words) {
                        cursorPos += getNextWordLength();
                    } else {
                        cursorPos++;
                    }
                }
                break;
            case Keyboard.KEY_BACK:
                if (isEnabled()) {
                    if (getSelectedText().length() > 0) {
                        deleteSelectedText();
                    } else if (words) {
                        deletePreviousWord();
                    } else {
                        deletePreviousChar();
                    }
                }
                return true;
            case Keyboard.KEY_DELETE:
                if (isEnabled()) {
                    if (getSelectedText().length() > 0) {
                        deleteSelectedText();
                    } else if (words) {
                        deleteNextWord();
                    } else {
                        deleteNextChar();
                    }
                }
                return true;
            default:
                if (isEnabled()) {
                    if (keyChar == '\r') {
                        keyChar = '\n';
                    }
                    writeChar(keyChar);
                }
                return true;
        }
        if (!select) {
            selectionPos = cursorPos;
        }
        return true;
    } finally {
        if (!textBefore.equals(text)) {
            onTextChanged(textBefore);
        }
    }
}
Also used : Focusable(de.johni0702.minecraft.gui.function.Focusable)

Aggregations

Focusable (de.johni0702.minecraft.gui.function.Focusable)3