|
| 1 | +/* |
| 2 | + * Minecraft Forge, Patchwork Project |
| 3 | + * Copyright (c) 2016-2020, 2019-2020 |
| 4 | + * |
| 5 | + * This library is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU Lesser General Public |
| 7 | + * License as published by the Free Software Foundation version 2.1 |
| 8 | + * of the License. |
| 9 | + * |
| 10 | + * This library is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + * Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public |
| 16 | + * License along with this library; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | + */ |
| 19 | + |
| 20 | +package net.minecraftforge.client.event; |
| 21 | + |
| 22 | +import net.minecraftforge.eventbus.api.Event; |
| 23 | +import org.lwjgl.glfw.GLFW; |
| 24 | + |
| 25 | +public class InputEvent extends Event { |
| 26 | + /** |
| 27 | + * A cancellable mouse event fired before key bindings are updated. |
| 28 | + */ |
| 29 | + public static class RawMouseEvent extends InputEvent { |
| 30 | + private final int action; |
| 31 | + private final int button; |
| 32 | + private final int mods; |
| 33 | + |
| 34 | + // For EventBus |
| 35 | + public RawMouseEvent() { |
| 36 | + this(-1, -1, -1); |
| 37 | + } |
| 38 | + |
| 39 | + public RawMouseEvent(int button, int action, int mods) { |
| 40 | + this.button = button; |
| 41 | + this.action = action; |
| 42 | + this.mods = mods; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * The mouse button that triggered this event. |
| 47 | + * https://www.glfw.org/docs/latest/group__buttons.html |
| 48 | + * |
| 49 | + * @see GLFW mouse constants starting with "GLFW_MOUSE_BUTTON_" |
| 50 | + */ |
| 51 | + public int getButton() { |
| 52 | + return this.button; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Integer representing the mouse button's action. |
| 57 | + * |
| 58 | + * @see GLFW#GLFW_PRESS |
| 59 | + * @see GLFW#GLFW_RELEASE |
| 60 | + */ |
| 61 | + public int getAction() { |
| 62 | + return this.action; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Bit field representing the modifier keys pressed. |
| 67 | + * https://www.glfw.org/docs/latest/group__mods.html |
| 68 | + * |
| 69 | + * @see GLFW#GLFW_MOD_SHIFT |
| 70 | + * @see GLFW#GLFW_MOD_CONTROL |
| 71 | + * @see GLFW#GLFW_MOD_ALT |
| 72 | + * @see GLFW#GLFW_MOD_SUPER |
| 73 | + */ |
| 74 | + public int getMods() { |
| 75 | + return this.mods; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public boolean isCancelable() { |
| 80 | + return true; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * This event fires when a mouse input is detected. |
| 86 | + */ |
| 87 | + public static class MouseInputEvent extends InputEvent { |
| 88 | + private final int button; |
| 89 | + private final int action; |
| 90 | + private final int mods; |
| 91 | + |
| 92 | + // For EventBus |
| 93 | + public MouseInputEvent() { |
| 94 | + this(-1, -1, -1); |
| 95 | + } |
| 96 | + |
| 97 | + public MouseInputEvent(int button, int action, int mods) { |
| 98 | + this.button = button; |
| 99 | + this.action = action; |
| 100 | + this.mods = mods; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * The mouse button that triggered this event. |
| 105 | + * https://www.glfw.org/docs/latest/group__buttons.html |
| 106 | + * |
| 107 | + * @see GLFW mouse constants starting with "GLFW_MOUSE_BUTTON_" |
| 108 | + */ |
| 109 | + public int getButton() { |
| 110 | + return this.button; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Integer representing the mouse button's action. |
| 115 | + * |
| 116 | + * @see GLFW#GLFW_PRESS |
| 117 | + * @see GLFW#GLFW_RELEASE |
| 118 | + */ |
| 119 | + public int getAction() { |
| 120 | + return this.action; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Bit field representing the modifier keys pressed. |
| 125 | + * https://www.glfw.org/docs/latest/group__mods.html |
| 126 | + * |
| 127 | + * @see GLFW#GLFW_MOD_SHIFT |
| 128 | + * @see GLFW#GLFW_MOD_CONTROL |
| 129 | + * @see GLFW#GLFW_MOD_ALT |
| 130 | + * @see GLFW#GLFW_MOD_SUPER |
| 131 | + */ |
| 132 | + public int getMods() { |
| 133 | + return this.mods; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * This event fires when the mouse scroll wheel is used outside of a gui. |
| 139 | + */ |
| 140 | + public static class MouseScrollEvent extends InputEvent { |
| 141 | + private final double scrollDelta; |
| 142 | + private final double mouseX; |
| 143 | + private final double mouseY; |
| 144 | + private final boolean leftDown; |
| 145 | + private final boolean middleDown; |
| 146 | + private final boolean rightDown; |
| 147 | + |
| 148 | + // For EventBus |
| 149 | + public MouseScrollEvent() { |
| 150 | + this(-1, false, false, false, -1, -1); |
| 151 | + } |
| 152 | + |
| 153 | + public MouseScrollEvent(double scrollDelta, boolean leftDown, boolean middleDown, boolean rightDown, double mouseX, double mouseY) { |
| 154 | + this.scrollDelta = scrollDelta; |
| 155 | + this.leftDown = leftDown; |
| 156 | + this.middleDown = middleDown; |
| 157 | + this.rightDown = rightDown; |
| 158 | + this.mouseX = mouseX; |
| 159 | + this.mouseY = mouseY; |
| 160 | + } |
| 161 | + |
| 162 | + public double getScrollDelta() { |
| 163 | + return this.scrollDelta; |
| 164 | + } |
| 165 | + |
| 166 | + public boolean isLeftDown() { |
| 167 | + return this.leftDown; |
| 168 | + } |
| 169 | + |
| 170 | + public boolean isRightDown() { |
| 171 | + return this.rightDown; |
| 172 | + } |
| 173 | + |
| 174 | + public boolean isMiddleDown() { |
| 175 | + return this.middleDown; |
| 176 | + } |
| 177 | + |
| 178 | + public double getMouseX() { |
| 179 | + return this.mouseX; |
| 180 | + } |
| 181 | + |
| 182 | + public double getMouseY() { |
| 183 | + return this.mouseY; |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public boolean isCancelable() { |
| 188 | + return true; |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * This event fires when a keyboard input is detected. |
| 194 | + */ |
| 195 | + public static class KeyInputEvent extends InputEvent { |
| 196 | + private final int key; |
| 197 | + private final int scanCode; |
| 198 | + private final int action; |
| 199 | + private final int modifiers; |
| 200 | + |
| 201 | + // For EventBus |
| 202 | + public KeyInputEvent() { |
| 203 | + this(-1, -1, -1, -1); |
| 204 | + } |
| 205 | + |
| 206 | + public KeyInputEvent(int key, int scanCode, int action, int modifiers) { |
| 207 | + this.key = key; |
| 208 | + this.scanCode = scanCode; |
| 209 | + this.action = action; |
| 210 | + this.modifiers = modifiers; |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * The keyboard key that triggered this event. |
| 215 | + * https://www.glfw.org/docs/latest/group__keys.html |
| 216 | + * |
| 217 | + * @see GLFW key constants starting with "GLFW_KEY_" |
| 218 | + */ |
| 219 | + public int getKey() { |
| 220 | + return this.key; |
| 221 | + } |
| 222 | + |
| 223 | + /** |
| 224 | + * Platform-specific scan code. |
| 225 | + * Used for {@link InputMappings#getInputByCode(int, int)} |
| 226 | + * |
| 227 | + * <p>The scan code is unique for every key, regardless of whether it has a key code. |
| 228 | + * Scan codes are platform-specific but consistent over time, so keys will have different scan codes depending |
| 229 | + * on the platform but they are safe to save to disk as custom key bindings. |
| 230 | + */ |
| 231 | + public int getScanCode() { |
| 232 | + return this.scanCode; |
| 233 | + } |
| 234 | + |
| 235 | + /** |
| 236 | + * Integer representing the key's action. |
| 237 | + * |
| 238 | + * @see GLFW#GLFW_PRESS |
| 239 | + * @see GLFW#GLFW_RELEASE |
| 240 | + * @see GLFW#GLFW_REPEAT |
| 241 | + */ |
| 242 | + public int getAction() { |
| 243 | + return this.action; |
| 244 | + } |
| 245 | + |
| 246 | + /** |
| 247 | + * Bit field representing the modifier keys pressed. |
| 248 | + * https://www.glfw.org/docs/latest/group__mods.html |
| 249 | + * |
| 250 | + * @see GLFW#GLFW_MOD_SHIFT |
| 251 | + * @see GLFW#GLFW_MOD_CONTROL |
| 252 | + * @see GLFW#GLFW_MOD_ALT |
| 253 | + * @see GLFW#GLFW_MOD_SUPER |
| 254 | + */ |
| 255 | + public int getModifiers() { |
| 256 | + return this.modifiers; |
| 257 | + } |
| 258 | + } |
| 259 | +} |
0 commit comments