| 1 | package com.mojang.blaze3d.platform;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 | import java.util.List;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.client.Minecraft;
|
| 8 | import net.minecraft.client.gui.screens.options.controls.KeyBindsScreen;
|
| 9 | import net.minecraft.client.input.InputQuirks;
|
| 10 | import net.minecraft.client.input.KeyEvent;
|
| 11 | import net.minecraft.client.input.MouseButtonInfo;
|
| 12 | import net.minecraft.client.input.PreeditEvent;
|
| 13 | import org.lwjgl.sdl.SDLEvents;
|
| 14 | import org.lwjgl.sdl.SDLKeyboard;
|
| 15 | import org.lwjgl.sdl.SDL_Event;
|
| 16 | import org.lwjgl.sdl.SDL_KeyboardEvent;
|
| 17 | import org.lwjgl.sdl.SDL_MouseButtonEvent;
|
| 18 | import org.lwjgl.sdl.SDL_MouseMotionEvent;
|
| 19 | import org.lwjgl.sdl.SDL_MouseWheelEvent;
|
| 20 | import org.lwjgl.sdl.SDL_TextEditingEvent;
|
| 21 |
|
| 22 | @Environment(EnvType.CLIENT)
|
| 23 | public class SDLEventHandler {
|
| 24 | private final List<String> dropFiles = new ArrayList<>();
|
| 25 | private final Minecraft minecraft;
|
| 26 | private final Window window;
|
| 27 |
|
| 28 | public SDLEventHandler(final Minecraft minecraft, final Window window) {
|
| 29 | this.minecraft = minecraft;
|
| 30 | this.window = window;
|
| 31 | }
|
| 32 |
|
| 33 | private static long getWindowHandle(final SDL_Event event) {
|
| 34 | return SDLEvents.SDL_GetWindowFromEvent(event);
|
| 35 | }
|
| 36 |
|
| 37 | public void pollEvents() {
|
| 38 | try (SDL_Event event = SDL_Event.malloc()) {
|
| 39 | while (SDLEvents.SDL_PollEvent(event)) {
|
| 40 | switch (event.type()) {
|
| 41 | case 768:
|
| 42 | case 769:
|
| 43 | this.handleKeyEvent(event);
|
| 44 | break;
|
| 45 | case 770:
|
| 46 | this.handleTextEditingEvent(event);
|
| 47 | break;
|
| 48 | case 771:
|
| 49 | this.handleTextInputEvent(event);
|
| 50 | break;
|
| 51 | case 772:
|
| 52 | this.handleKeymapChangedEvent();
|
| 53 | break;
|
| 54 | case 1024:
|
| 55 | this.handleMouseMotionEvent(event);
|
| 56 | break;
|
| 57 | case 1025:
|
| 58 | case 1026:
|
| 59 | this.handleMouseButtonEvent(event);
|
| 60 | break;
|
| 61 | case 1027:
|
| 62 | this.handleMouseWheelEvent(event);
|
| 63 | break;
|
| 64 | case 4096:
|
| 65 | this.handleDropFileEvent(event);
|
| 66 | break;
|
| 67 | case 4098:
|
| 68 | this.handleDropBeginEvent();
|
| 69 | break;
|
| 70 | case 4099:
|
| 71 | this.handleDropCompleteEvent(event);
|
| 72 | break;
|
| 73 | default:
|
| 74 | this.window.handleEvent(event);
|
| 75 | }
|
| 76 | }
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | public void pumpEvents() {
|
| 81 | this.flushInputEvents();
|
| 82 | this.pollEvents();
|
| 83 | }
|
| 84 |
|
| 85 | public void flushInputEvents() {
|
| 86 | SDLEvents.SDL_PumpEvents();
|
| 87 | SDLEvents.SDL_FlushEvents(768, 4871);
|
| 88 | }
|
| 89 |
|
| 90 | private void handleKeymapChangedEvent() {
|
| 91 | this.minecraft.execute(() -> {
|
| 92 | if (this.minecraft.gui.screen() instanceof KeyBindsScreen keyBindsScreen) {
|
| 93 | keyBindsScreen.refreshKeybindLabels();
|
| 94 | }
|
| 95 | });
|
| 96 | }
|
| 97 |
|
| 98 | private void handleKeyEvent(final SDL_Event event) {
|
| 99 | SDL_KeyboardEvent keyEvent = event.key();
|
| 100 | int action = event.type() == 769 ? 0 : (keyEvent.repeat() ? -1 : 1);
|
| 101 | KeyEvent key = new KeyEvent(keyEvent.scancode(), keyEvent.key(), keyEvent.mod());
|
| 102 | this.minecraft.execute(() -> this.minecraft.keyboardHandler.keyPress(getWindowHandle(event), action, key));
|
| 103 | }
|
| 104 |
|
| 105 | private void handleTextInputEvent(final SDL_Event event) {
|
| 106 | String text = event.text().textString();
|
| 107 | if (text != null) {
|
| 108 | long handle = getWindowHandle(event);
|
| 109 | this.minecraft.execute(() -> this.minecraft.keyboardHandler.textInput(handle, text));
|
| 110 | }
|
| 111 | }
|
| 112 |
|
| 113 | private void handleTextEditingEvent(final SDL_Event event) {
|
| 114 | SDL_TextEditingEvent edit = event.edit();
|
| 115 | PreeditEvent preedit = PreeditEvent.fromSdlTextEditing(edit.textString(), edit.start(), edit.length());
|
| 116 | long handle = getWindowHandle(event);
|
| 117 | this.minecraft.execute(() -> this.minecraft.keyboardHandler.textEditing(handle, preedit));
|
| 118 | }
|
| 119 |
|
| 120 | private void handleMouseMotionEvent(final SDL_Event event) {
|
| 121 | SDL_MouseMotionEvent motion = event.motion();
|
| 122 | long handle = getWindowHandle(event);
|
| 123 | this.minecraft.execute(() -> this.minecraft.mouseHandler.onMove(handle, motion.x(), motion.y(), motion.xrel(), motion.yrel()));
|
| 124 | }
|
| 125 |
|
| 126 | private void handleMouseButtonEvent(final SDL_Event event) {
|
| 127 | SDL_MouseButtonEvent buttonEvent = event.button();
|
| 128 | int action = event.type() == 1025 ? 1 : 0;
|
| 129 | MouseButtonInfo buttonInfo = new MouseButtonInfo(buttonEvent.button(), SDLKeyboard.SDL_GetModState());
|
| 130 | long handle = getWindowHandle(event);
|
| 131 | this.minecraft.execute(() -> this.minecraft.mouseHandler.onButton(handle, buttonInfo, action));
|
| 132 | }
|
| 133 |
|
| 134 | private void handleDropFileEvent(final SDL_Event event) {
|
| 135 | String data = event.drop().dataString();
|
| 136 | if (data != null) {
|
| 137 | this.dropFiles.add(data);
|
| 138 | }
|
| 139 | }
|
| 140 |
|
| 141 | private void handleDropBeginEvent() {
|
| 142 | this.dropFiles.clear();
|
| 143 | }
|
| 144 |
|
| 145 | private void handleMouseWheelEvent(final SDL_Event event) {
|
| 146 | SDL_MouseWheelEvent wheel = event.wheel();
|
| 147 | Long handle = getWindowHandle(event);
|
| 148 | boolean invertedScroll = isShiftInvertedScroll(wheel.y(), wheel.x());
|
| 149 | double scrollX = invertedScroll ? 0.0 : wheel.x();
|
| 150 | double scrollY = invertedScroll ? -wheel.x() : wheel.y();
|
| 151 | this.minecraft.execute(() -> this.minecraft.mouseHandler.onScroll(handle, scrollX, scrollY));
|
| 152 | }
|
| 153 |
|
| 154 | private static boolean isShiftInvertedScroll(final double y, final double x) {
|
| 155 | return InputQuirks.SHIFT_INVERTS_SCROLL_AXIS && y == 0.0 && x != 0.0 && (SDLKeyboard.SDL_GetModState() & 3) != 0;
|
| 156 | }
|
| 157 |
|
| 158 | private void handleDropCompleteEvent(final SDL_Event event) {
|
| 159 | if (!this.dropFiles.isEmpty()) {
|
| 160 | long handle = getWindowHandle(event);
|
| 161 | List<String> files = List.copyOf(this.dropFiles);
|
| 162 | this.minecraft.execute(() -> this.minecraft.mouseHandler.onDrop(handle, files));
|
| 163 | }
|
| 164 |
|
| 165 | this.dropFiles.clear();
|
| 166 | }
|
| 167 | }
|
| 168 |
|