Search in sources :

Example 1 with TextInput

use of com.spinyowl.legui.component.TextInput in project legui by SpinyOwl.

the class TextInputCharEventListener method process.

/**
 * Used to handle {@link CharEvent}.
 *
 * @param event event to handle.
 */
@Override
public void process(CharEvent event) {
    TextInput textInput = (TextInput) event.getTargetComponent();
    if (textInput.isFocused() && textInput.isEditable() && !MOUSE_BUTTON_LEFT.isPressed()) {
        String str = cpToStr(event.getCodepoint());
        TextState textState = textInput.getTextState();
        String oldText = textState.getText();
        int start = textInput.getStartSelectionIndex();
        int end = textInput.getEndSelectionIndex();
        if (start > end) {
            start = textInput.getEndSelectionIndex();
            end = textInput.getStartSelectionIndex();
        }
        if (start != end) {
            StringBuilder t = new StringBuilder(textState.getText());
            t.delete(start, end);
            textState.setText(t.toString());
            textInput.setCaretPosition(start);
            textInput.setStartSelectionIndex(start);
            textInput.setEndSelectionIndex(start);
        }
        int caretPosition = textInput.getCaretPosition();
        StringBuilder t = new StringBuilder(textState.getText());
        t.insert(caretPosition, str);
        textState.setText(t.toString());
        int newCaretPosition = caretPosition + str.length();
        textInput.setCaretPosition(newCaretPosition);
        textInput.setEndSelectionIndex(newCaretPosition);
        textInput.setStartSelectionIndex(newCaretPosition);
        String newText = textState.getText();
        EventProcessorProvider.getInstance().pushEvent(new TextInputContentChangeEvent(textInput, event.getContext(), event.getFrame(), oldText, newText));
    }
}
Also used : TextState(com.spinyowl.legui.component.optional.TextState) TextInputContentChangeEvent(com.spinyowl.legui.component.event.textinput.TextInputContentChangeEvent) TextInput(com.spinyowl.legui.component.TextInput)

Example 2 with TextInput

use of com.spinyowl.legui.component.TextInput in project legui by SpinyOwl.

the class TextInputMouseClickEventListener method process.

/**
 * Used to handle {@link MouseClickEvent}.
 *
 * @param event event to handle.
 */
@Override
public void process(MouseClickEvent event) {
    TextInput gui = (TextInput) event.getTargetComponent();
    int mouseCaretPosition = gui.getMouseCaretPosition();
    if (event.getAction() == MouseClickEvent.MouseClickAction.PRESS) {
        gui.setCaretPosition(mouseCaretPosition);
        gui.setEndSelectionIndex(mouseCaretPosition);
        if (!event.isModShift()) {
            gui.setStartSelectionIndex(mouseCaretPosition);
        }
    }
}
Also used : TextInput(com.spinyowl.legui.component.TextInput)

Example 3 with TextInput

use of com.spinyowl.legui.component.TextInput in project legui by SpinyOwl.

the class TextInputDragEventListener method process.

@Override
public void process(MouseDragEvent event) {
    TextInput textInput = (TextInput) event.getTargetComponent();
    if (MOUSE_BUTTON_LEFT.isPressed()) {
        int mouseCaretPosition = textInput.getMouseCaretPosition();
        textInput.setCaretPosition(mouseCaretPosition);
        textInput.setEndSelectionIndex(mouseCaretPosition);
    }
}
Also used : TextInput(com.spinyowl.legui.component.TextInput)

Example 4 with TextInput

use of com.spinyowl.legui.component.TextInput in project legui by SpinyOwl.

the class TextInputKeyEventListener method process.

/**
 * Used to handle {@link KeyEvent}.
 *
 * @param event event to handle.
 */
@Override
public void process(KeyboardEvent event) {
    TextInput gui = (TextInput) event.getTargetComponent();
    boolean pressed = event.getAction() != KeyAction.RELEASE;
    if (!pressed) {
        return;
    }
    String oldText = gui.getTextState().getText();
    processKeys(event, gui);
    String newText = gui.getTextState().getText();
    if (!oldText.equals(newText)) {
        EventProcessorProvider.getInstance().pushEvent(new TextInputContentChangeEvent<>(gui, event.getContext(), event.getFrame(), oldText, newText));
    }
}
Also used : TextInput(com.spinyowl.legui.component.TextInput)

Aggregations

TextInput (com.spinyowl.legui.component.TextInput)4 TextInputContentChangeEvent (com.spinyowl.legui.component.event.textinput.TextInputContentChangeEvent)1 TextState (com.spinyowl.legui.component.optional.TextState)1