| 1 | package com.mojang.blaze3d.platform;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import org.jspecify.annotations.Nullable;
|
| 6 | import org.lwjgl.sdl.SDLKeyboard;
|
| 7 | import org.lwjgl.sdl.SDL_Rect;
|
| 8 | import org.lwjgl.sdl.SDL_Rect.Buffer;
|
| 9 | import org.lwjgl.system.MemoryStack;
|
| 10 |
|
| 11 | @Environment(EnvType.CLIENT)
|
| 12 | public class TextInputManager {
|
| 13 | private final Window window;
|
| 14 | private boolean textInputEnabled;
|
| 15 | private @Nullable Object owner;
|
| 16 | private boolean hasTextInputArea;
|
| 17 | private int areaX;
|
| 18 | private int areaY;
|
| 19 | private int areaWidth;
|
| 20 | private int areaHeight;
|
| 21 |
|
| 22 | public TextInputManager(final Window window) {
|
| 23 | this.window = window;
|
| 24 | }
|
| 25 |
|
| 26 | public void setTextInputArea(final int x0, final int y0, final int x1, final int y1) {
|
| 27 | double windowScale = (double)this.window.getGuiScale() / this.window.getPixelDensity();
|
| 28 | int x = (int)Math.round(x0 * windowScale);
|
| 29 | int y = (int)Math.round(y0 * windowScale);
|
| 30 | int width = Math.max(1, (int)Math.round((x1 - x0) * windowScale));
|
| 31 | int height = Math.max(1, (int)Math.round((y1 - y0) * windowScale));
|
| 32 | if (!this.hasTextInputArea || x != this.areaX || y != this.areaY || width != this.areaWidth || height != this.areaHeight) {
|
| 33 | this.areaX = x;
|
| 34 | this.areaY = y;
|
| 35 | this.areaWidth = width;
|
| 36 | this.areaHeight = height;
|
| 37 | this.hasTextInputArea = true;
|
| 38 | this.applyTextInputArea();
|
| 39 | }
|
| 40 | }
|
| 41 |
|
| 42 | private void applyTextInputArea() {
|
| 43 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 44 | Buffer rect = SDL_Rect.malloc(1, stack).x(this.areaX).y(this.areaY).w(this.areaWidth).h(this.areaHeight);
|
| 45 | SDLKeyboard.SDL_SetTextInputArea(this.window.handle(), rect, -1);
|
| 46 | }
|
| 47 | }
|
| 48 |
|
| 49 | public void startTextInput(final Object owner) {
|
| 50 | this.owner = owner;
|
| 51 | if (!this.textInputEnabled) {
|
| 52 | if (this.hasTextInputArea) {
|
| 53 | this.applyTextInputArea();
|
| 54 | }
|
| 55 |
|
| 56 | this.textInputEnabled = true;
|
| 57 | SDLKeyboard.SDL_StartTextInput(this.window.handle());
|
| 58 | SDLKeyboard.SDL_ClearComposition(this.window.handle());
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | public void stopTextInput(final Object owner) {
|
| 63 | if (this.owner == owner) {
|
| 64 | this.stopTextInput();
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | public void stopTextInput() {
|
| 69 | this.owner = null;
|
| 70 | if (this.textInputEnabled) {
|
| 71 | this.textInputEnabled = false;
|
| 72 | SDLKeyboard.SDL_StopTextInput(this.window.handle());
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | public void onTextInputFocusChange(final Object owner, final boolean focused) {
|
| 77 | if (focused) {
|
| 78 | this.startTextInput(owner);
|
| 79 | } else {
|
| 80 | this.stopTextInput(owner);
|
| 81 | }
|
| 82 | }
|
| 83 | }
|
| 84 |
|