| 7 | import io.github.humbleui.types.*; |
| 8 | |
| 9 | public class PanelTextInput extends Panel implements TextInputClient { |
| 10 | public List<String> keys = Collections.synchronizedList(new ArrayList<String>()); |
| 11 | |
| 12 | public String text = ""; |
| 13 | public EventTextInputMarked lastMarked = null; |
| 14 | public int lastInputHeight = 0; |
| 15 | public boolean cursorDraw = true; |
| 16 | public TimerTask timerTask; |
| 17 | public static Timer timer = new Timer(true); |
| 18 | public boolean _wasInside = false; |
| 19 | |
| 20 | public PanelTextInput(Window window) { |
| 21 | super(window); |
| 22 | this.drawBG = false; |
| 23 | window.setTextInputEnabled(true); |
| 24 | window.setTextInputClient(this); |
| 25 | timerTask = new TimerTask() { |
| 26 | public void run() { |
| 27 | cursorDraw = !cursorDraw; |
| 28 | App.runOnUIThread(() -> { if (!window.isClosed()) window.requestFrame(); }); |
| 29 | } |
| 30 | }; |
| 31 | timer.schedule(timerTask, 0, 500); |
| 32 | } |
| 33 | |
| 34 | @Override |
| 35 | public void accept(Event e) { |
| 36 | if (e instanceof EventTextInput ee) { |
| 37 | int start = ee.getReplacementStart() == -1 ? text.length() : ee.getReplacementStart(); |
| 38 | int end = start + ee.getReplacementEnd() - ee.getReplacementStart(); |
| 39 | text = text.substring(0, start) + ee.getText() + text.substring(end); |
| 40 | lastMarked = null; |
| 41 | window.requestFrame(); |
| 42 | } else if (e instanceof EventTextInputMarked ee) { |
| 43 | int start = ee.getReplacementStart() == -1 ? text.length() : ee.getReplacementStart(); |
| 44 | int end = start + ee.getReplacementEnd() - ee.getReplacementStart(); |
| 45 | text = text.substring(0, start) + text.substring(end); |
| 46 | lastMarked = ee; |
| 47 | window.requestFrame(); |
| 48 | } else if (e instanceof EventMouseButton ee) { |
| 49 | window.unmarkText(); |
| 50 | window.requestFrame(); |
| 51 | } else if (e instanceof EventMouseMove ee) { |
| 52 | boolean isInside = contains(ee.getX(), ee.getY()); |
| 53 | if (!_wasInside && isInside) |
| 54 | window.setMouseCursor(MouseCursor.IBEAM); |
| 55 | if (_wasInside && !isInside) |
| 56 | window.setMouseCursor(MouseCursor.ARROW); |
| 57 | _wasInside = isInside; |
| 58 | } else if (e instanceof EventKey ee) { |
| 59 | Key key = ee.getKey(); |
| 60 | KeyLocation loc = ee.getLocation(); |
| 61 | |
| 62 | var keyText = loc == KeyLocation.DEFAULT ? key.getName() : capitalize(loc.toString()) + " " + key.getName(); |
| 63 | if (ee.isPressed() && !keys.contains(keyText)) |
| 64 | keys.add(keyText); |
| 65 | else if (!ee.isPressed()) |
| 66 | keys.remove(keyText); |
nothing calls this directly
no outgoing calls
no test coverage detected