From 0b4502c3ce3908fe3b9bf8a2d98d8d1dfd35e7d4 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Thu, 25 Jul 2024 12:19:46 +0200 Subject: [PATCH] Add MouseButtonCallback --- .../glfw/callbacks/MouseButtonCallback.java | 86 +++++++++++++++++++ .../graphics/glfw/classes/GlfwWindow.java | 12 +++ 2 files changed, 98 insertions(+) create mode 100644 graphics/glfw/src/main/java/de/staropensource/sosengine/graphics/glfw/callbacks/MouseButtonCallback.java diff --git a/graphics/glfw/src/main/java/de/staropensource/sosengine/graphics/glfw/callbacks/MouseButtonCallback.java b/graphics/glfw/src/main/java/de/staropensource/sosengine/graphics/glfw/callbacks/MouseButtonCallback.java new file mode 100644 index 00000000..e6375352 --- /dev/null +++ b/graphics/glfw/src/main/java/de/staropensource/sosengine/graphics/glfw/callbacks/MouseButtonCallback.java @@ -0,0 +1,86 @@ +/* + * STAROPENSOURCE ENGINE SOURCE FILE + * Copyright (c) 2024 The StarOpenSource Engine Contributors + * Licensed under the GNU Affero General Public License v3 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.staropensource.sosengine.graphics.glfw.callbacks; + +import de.staropensource.sosengine.graphics.classes.Window; +import de.staropensource.sosengine.graphics.events.InputEvent; +import de.staropensource.sosengine.graphics.glfw.classes.WindowCallback; +import de.staropensource.sosengine.graphics.types.input.Key; +import de.staropensource.sosengine.graphics.types.input.KeyState; +import org.jetbrains.annotations.NotNull; +import org.lwjgl.glfw.GLFWMouseButtonCallbackI; + +import static org.lwjgl.glfw.GLFW.*; + +/** + * A {@link GLFWMouseButtonCallbackI} implementation. + * + * @since v1-alpha2 + */ +@SuppressWarnings({ "unused" }) +public class MouseButtonCallback extends WindowCallback implements GLFWMouseButtonCallbackI { + /** + * A {@link InputEvent} instance, to avoid creating too many objects + * and making too many allocations, which would potentially decrease + * performance. + * + * @since v1-alpha2 + */ + private final InputEvent event = new InputEvent(); + + /** + * Constructs this class. + * + * @param window {@link Window} class + * @since v1-alpha2 + */ + public MouseButtonCallback(@NotNull Window window) { + super(window); + } + + /** {@inheritDoc} */ + @Override + public void invoke(long window, int key, int action, int mods) { + // Ignore GLFW_REPEAT action + if (action == GLFW_REPEAT) + return; + + event.callEvent( + // Attached window + getAttachedWindow(), + + // Key + switch (key) { + case GLFW_MOUSE_BUTTON_LEFT -> Key.MOUSE_LEFT; + case GLFW_MOUSE_BUTTON_MIDDLE -> Key.MOUSE_MIDDLE; + case GLFW_MOUSE_BUTTON_RIGHT -> Key.MOUSE_RIGHT; + case GLFW_MOUSE_BUTTON_4, GLFW_MOUSE_BUTTON_5, + GLFW_MOUSE_BUTTON_6, GLFW_MOUSE_BUTTON_7, + GLFW_MOUSE_BUTTON_8 -> Key.UNKNOWN_MOUSE; + default -> throw new IllegalStateException("Mouse button " + key + " is invalid"); + }, + // Key state + switch (action) { + case GLFW_PRESS -> KeyState.PRESSED; + case GLFW_RELEASE -> KeyState.RELEASED; + default -> throw new IllegalStateException("Action " + action + " is invalid"); + }); + } +} diff --git a/graphics/glfw/src/main/java/de/staropensource/sosengine/graphics/glfw/classes/GlfwWindow.java b/graphics/glfw/src/main/java/de/staropensource/sosengine/graphics/glfw/classes/GlfwWindow.java index bddddaa5..83828953 100644 --- a/graphics/glfw/src/main/java/de/staropensource/sosengine/graphics/glfw/classes/GlfwWindow.java +++ b/graphics/glfw/src/main/java/de/staropensource/sosengine/graphics/glfw/classes/GlfwWindow.java @@ -28,6 +28,7 @@ import de.staropensource.sosengine.graphics.classes.Window; import de.staropensource.sosengine.graphics.events.GraphicsApiErrorEvent; import de.staropensource.sosengine.graphics.events.InputEvent; import de.staropensource.sosengine.graphics.glfw.callbacks.KeyCallback; +import de.staropensource.sosengine.graphics.glfw.callbacks.MouseButtonCallback; import de.staropensource.sosengine.graphics.glfw.exceptions.NotOnMainThreadException; import de.staropensource.sosengine.graphics.glfw.exceptions.WindowCreationFailureException; import de.staropensource.sosengine.graphics.types.window.VsyncMode; @@ -35,6 +36,7 @@ import de.staropensource.sosengine.graphics.types.window.WindowMode; import lombok.Getter; import org.jetbrains.annotations.NotNull; import org.lwjgl.glfw.GLFWKeyCallback; +import org.lwjgl.glfw.GLFWMouseButtonCallback; import org.lwjgl.system.MemoryStack; import org.lwjgl.system.MemoryUtil; @@ -70,6 +72,13 @@ public abstract class GlfwWindow extends Window { */ private GLFWKeyCallback keyCallback; + /** + * Contains the {@link GLFWMouseButtonCallback} used for emitting {@link InputEvent}s. + * + * @since v1-alpha2 + */ + private GLFWMouseButtonCallback mouseButtonCallback; + // ------------------------------------------------ [ Window (de)initialization ] ------------------------------------------------ // /** * Creates a new window. @@ -159,9 +168,11 @@ public abstract class GlfwWindow extends Window { // Create callbacks keyCallback = GLFWKeyCallback.create(new KeyCallback(this)); + mouseButtonCallback = GLFWMouseButtonCallback.create(new MouseButtonCallback(this)); // Set callback glfwSetKeyCallback(identifierLong, keyCallback); + glfwSetMouseButtonCallback(identifier, mouseButtonCallback); // Update the window state setSize(getSize()); @@ -182,6 +193,7 @@ public abstract class GlfwWindow extends Window { // Close callbacks keyCallback.close(); + mouseButtonCallback.close(); // Destroy the window glfwDestroyWindow(identifierLong);