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));
}
}
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);
}
}
}
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);
}
}
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));
}
}
Aggregations