Add MouseButtonCallback
This commit is contained in:
parent
6dc0566fed
commit
0b4502c3ce
2 changed files with 98 additions and 0 deletions
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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");
|
||||
});
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue