diff --git a/GameEngine/src/main/java/Main.java b/GameEngine/src/main/java/Main.java index 6b57f28..d19dfc9 100644 --- a/GameEngine/src/main/java/Main.java +++ b/GameEngine/src/main/java/Main.java @@ -1,9 +1,8 @@ -import gameEngine.Window; +import gameEngine.*; public class Main { public static void main(String[] args) { Window window = Window.get(); - Swing.swing(); window.run(); } } \ No newline at end of file diff --git a/GameEngine/src/main/java/Swing.java b/GameEngine/src/main/java/Swing.java deleted file mode 100644 index c1de740..0000000 --- a/GameEngine/src/main/java/Swing.java +++ /dev/null @@ -1,63 +0,0 @@ -import gameEngine.ColorController; - -import java.awt.GridLayout; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.util.Objects; -import javax.swing.JButton; -import javax.swing.JComboBox; -import javax.swing.JFrame; -import javax.swing.JLabel; -import javax.swing.JPanel; - -public class Swing { - public Swing() { - } - - public static void swing() { - JFrame frame = new JFrame("Swing Example"); - frame.setDefaultCloseOperation(3); - frame.setSize(300, 200); - JPanel panel = new JPanel(); - panel.setLayout(new GridLayout(4, 2)); - JLabel keyInputLabel = new JLabel("KeyInput:"); - String[] keyInputOptions = new String[]{"KeyPress", "KeyDown", "KeyUp"}; - final JComboBox keyInputDropdown = new JComboBox(keyInputOptions); - JLabel keyCodeLabel = new JLabel("KeyCode:"); - String[] keyCodeOptions = new String[]{"A", "S", "D"}; - final JComboBox keyCodeDropdown = new JComboBox(keyCodeOptions); - JLabel colorLabel = new JLabel("Color:"); - String[] colorOptions = new String[]{"Red", "Blue", "Green", "Yellow"}; - final JComboBox colorDropdown = new JComboBox(colorOptions); - JButton updateButton = new JButton("Update"); - updateButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - String selectedKeyInput = (String)keyInputDropdown.getSelectedItem(); - String selectedKeyCode = (String)keyCodeDropdown.getSelectedItem(); - String selectedColor = (String)colorDropdown.getSelectedItem(); - if (Objects.equals(selectedKeyCode, "A")) { - ColorController.setCustomKeyCode(65); - } - - if (Objects.equals(selectedKeyCode, "S")) { - ColorController.setCustomKeyCode(83); - } - - if (Objects.equals(selectedKeyCode, "D")) { - ColorController.setCustomKeyCode(68); - } - - } - }); - panel.add(keyInputLabel); - panel.add(keyInputDropdown); - panel.add(keyCodeLabel); - panel.add(keyCodeDropdown); - panel.add(colorLabel); - panel.add(colorDropdown); - panel.add(new JLabel()); - panel.add(updateButton); - frame.add(panel); - frame.setVisible(true); - } -} \ No newline at end of file diff --git a/GameEngine/src/main/java/gameEngine/ColorController.java b/GameEngine/src/main/java/gameEngine/ColorController.java index 6e0eae8..f844d3d 100644 --- a/GameEngine/src/main/java/gameEngine/ColorController.java +++ b/GameEngine/src/main/java/gameEngine/ColorController.java @@ -1,6 +1,6 @@ package gameEngine; +import gameEngine.input.*; -import gameEngine.*; import static org.lwjgl.glfw.GLFW.*; import static org.lwjgl.opengl.GL11.*; @@ -8,7 +8,6 @@ private float red; private float green; private float blue; - private static int customKeyCode; public ColorController() { } @@ -18,29 +17,32 @@ } public void update() { - if (KeyInput.KeyDown(GLFW_KEY_R)) { + if (Input.GetKey(GLFW_KEY_R)) { this.setColor(1.0F, 0.0F, 0.0F); System.out.println("R"); } - if (KeyInput.KeyPress(GLFW_KEY_G)) { + if (Input.GetKeyUp(GLFW_KEY_G)) { this.setColor(0.0F, 1.0F, 0.0F); System.out.println("G"); } - if (KeyInput.KeyUp(GLFW_KEY_B)) { + if (Input.GetKeyDown(GLFW_KEY_B)) { this.setColor(0.0F, 0.0F, 1.0F); System.out.println("B"); } - if (KeyInput.KeyUp(GLFW_KEY_Y)) { - this.setColor(1.0F, 1.0F, 0.0F); - System.out.println("Y"); + if (Input.GetMouseButton(0)) { // Left mouse button + setColor(1.0f, 0.0f, 0.0f); // Set color to red + System.out.println("Left Mouse Button Pressed - Red"); } - - if (KeyInput.KeyPress(customKeyCode)) { - this.setColor(0.0F, 1.0F, 1.0F); - System.out.println("Custom"); + if (Input.GetMouseButtonUp(1)) { // Right mouse button + setColor(0.0f, 0.0f, 1.0f); // Set color to blue + System.out.println("Right Mouse Button Pressed - Blue"); + } + if (Input.GetMouseButtonDown(2)) { // Middle mouse button + setColor(0.0f, 1.0f, 0.0f); // Set color to green + System.out.println("Middle Mouse Button Pressed - Green"); } glClearColor(this.getRed(), this.getGreen(), this.getBlue(), 0.0F); @@ -64,8 +66,5 @@ this.blue = b; } - public static void setCustomKeyCode(int value) { - customKeyCode = value; - } } diff --git a/GameEngine/src/main/java/gameEngine/KeyInput.java b/GameEngine/src/main/java/gameEngine/KeyInput.java deleted file mode 100644 index f98dc47..0000000 --- a/GameEngine/src/main/java/gameEngine/KeyInput.java +++ /dev/null @@ -1,54 +0,0 @@ -package gameEngine; - -import static org.lwjgl.glfw.GLFW.*; - -public class KeyInput { - private static KeyInput instance; - private static long window; - private static final boolean[] lastKeyStates = new boolean[GLFW_KEY_LAST+1]; - private static final boolean[] currentKeyStates = new boolean[GLFW_KEY_LAST+1]; - - private KeyInput(long window) { - KeyInput.window = window; - } - - public static void init(long window) { - if (instance == null) { - instance = new KeyInput(window); - } - - } - - public static boolean KeyPress(int keyCode) { - return isValidKeyCode(keyCode) ? currentKeyStates[keyCode] : false; - } - - public static boolean KeyDown(int keyCode) { - if (!isValidKeyCode(keyCode)) { - return false; - } else { - return currentKeyStates[keyCode] && !lastKeyStates[keyCode]; - } - } - - public static boolean KeyUp(int keyCode) { - if (!isValidKeyCode(keyCode)) { - return false; - } else { - return !currentKeyStates[keyCode] && lastKeyStates[keyCode]; - } - } - - public static void updateKeyStates() { - System.arraycopy(currentKeyStates, 0, lastKeyStates, 0, currentKeyStates.length); - - for(int i = GLFW_KEY_SPACE; i <= GLFW_KEY_LAST; ++i) { - currentKeyStates[i] = glfwGetKey(window, i) == 1; - } - - } - - private static boolean isValidKeyCode(int keyCode) { - return keyCode >= GLFW_KEY_SPACE && keyCode <= GLFW_KEY_LAST; - } -} diff --git a/GameEngine/src/main/java/gameEngine/Window.java b/GameEngine/src/main/java/gameEngine/Window.java index 4b4e344..e4f36b0 100644 --- a/GameEngine/src/main/java/gameEngine/Window.java +++ b/GameEngine/src/main/java/gameEngine/Window.java @@ -1,5 +1,5 @@ - package gameEngine; +import gameEngine.input.*; import org.lwjgl.*; import org.lwjgl.glfw.*; @@ -59,10 +59,10 @@ if ( this.glfwWindow == NULL ) throw new RuntimeException("Failed to create the GLFW window"); - glfwSetKeyCallback(this.glfwWindow, (window, key, scancode, action, mods) -> { - if ( key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE ) - glfwSetWindowShouldClose(window, true); - }); + glfwSetCursorPosCallback(glfwWindow, MouseInput::mousePosCallback); + glfwSetMouseButtonCallback(glfwWindow, MouseInput::mouseButtonCallback); + glfwSetScrollCallback(glfwWindow, MouseInput::mouseScrollCallBack); + glfwSetKeyCallback(glfwWindow, KeyInput::keyCallback); // Get the thread stack and push a new frame try ( MemoryStack stack = stackPush() ) { @@ -89,7 +89,7 @@ glfwShowWindow(this.glfwWindow); //-------------------------------------------------------------- - KeyInput.init(this.glfwWindow); + iGameComponents.add(new ColorController()); for (IGameComponent gameComponent : iGameComponents) { gameComponent.init(); @@ -102,8 +102,7 @@ while (!glfwWindowShouldClose(this.glfwWindow)) { //--------------------------------------------------------------- - // 毎フレームのキー状態を更新 - KeyInput.updateKeyStates(); + glfwPollEvents(); // ウィンドウイベントをポーリング //GameComponentのupdate処理 for (IGameComponent gameComponents : iGameComponents) { @@ -113,7 +112,7 @@ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // フレームバッファをクリア glfwSwapBuffers(this.glfwWindow); // カラーバッファを交換 - glfwPollEvents(); // ウィンドウイベントをポーリング + } } diff --git a/GameEngine/src/main/java/gameEngine/input/Input.java b/GameEngine/src/main/java/gameEngine/input/Input.java new file mode 100644 index 0000000..0f47699 --- /dev/null +++ b/GameEngine/src/main/java/gameEngine/input/Input.java @@ -0,0 +1,34 @@ +package gameEngine.input; + +public class Input { + + // キーが押された瞬間 + public static boolean GetKeyDown(int keyCode) { + return KeyInput.isKeyDown(keyCode); + } + + // キーが押され続けているか + public static boolean GetKey(int keyCode) { + return KeyInput.isKeyPressed(keyCode); + } + + // キーが離された瞬間 + public static boolean GetKeyUp(int keyCode) { + return KeyInput.isKeyUp(keyCode); + } + + // マウスボタンが押された瞬間 + public static boolean GetMouseButtonDown(int mouseButton) { + return MouseInput.isMouseButtonDown(mouseButton); + } + + // マウスボタンが押され続けているか + public static boolean GetMouseButton(int mouseButton) { + return MouseInput.isMouseButtonPressed(mouseButton); + } + + // マウスボタンが離された瞬間 + public static boolean GetMouseButtonUp(int mouseButton) { + return MouseInput.isMouseButtonUp(mouseButton); + } +} diff --git a/GameEngine/src/main/java/gameEngine/input/KeyInput.java b/GameEngine/src/main/java/gameEngine/input/KeyInput.java new file mode 100644 index 0000000..47b4aa4 --- /dev/null +++ b/GameEngine/src/main/java/gameEngine/input/KeyInput.java @@ -0,0 +1,60 @@ +package gameEngine.input; + +import static org.lwjgl.glfw.GLFW.*; + +public class KeyInput { + private static KeyInput instance; + private boolean keyPressed[] = new boolean[GLFW_KEY_LAST + 1]; // 押されている状態を保持 + private boolean keyDown[] = new boolean[GLFW_KEY_LAST + 1]; // 押された瞬間を検知 + private boolean keyUp[] = new boolean[GLFW_KEY_LAST + 1]; // 離された瞬間を検知 + + private KeyInput() { + } + + public static KeyInput get() { + if (KeyInput.instance == null) { + KeyInput.instance = new KeyInput(); + } + return KeyInput.instance; + } + + public static void keyCallback(long window, int key, int scancode, int action, int mods) { + if (key <= GLFW_KEY_LAST && key >= 0) { + KeyInput input = get(); + if (action == GLFW_PRESS) { + if (!input.keyPressed[key]) { + input.keyDown[key] = true; // 押された瞬間 + } + input.keyPressed[key] = true; + } else if (action == GLFW_RELEASE) { + input.keyPressed[key] = false; + input.keyUp[key] = true; // 離された瞬間 + } + } + } + + protected static boolean isKeyDown(int keyCode) { + if (keyCode <= GLFW_KEY_LAST && keyCode >= 0) { + boolean result = get().keyDown[keyCode]; + get().keyDown[keyCode] = false; // 一度だけ検知するため、クリア + return result; + } + return false; + } + + protected static boolean isKeyPressed(int keyCode) { + if (keyCode <= GLFW_KEY_LAST && keyCode >= 0) { + return get().keyPressed[keyCode]; + } + return false; + } + + protected static boolean isKeyUp(int keyCode) { + if (keyCode <= GLFW_KEY_LAST && keyCode >= 0) { + boolean result = get().keyUp[keyCode]; + get().keyUp[keyCode] = false; // 一度だけ検知するため、クリア + return result; + } + return false; + } +} \ No newline at end of file diff --git a/GameEngine/src/main/java/gameEngine/input/MouseInput.java b/GameEngine/src/main/java/gameEngine/input/MouseInput.java new file mode 100644 index 0000000..793dd5b --- /dev/null +++ b/GameEngine/src/main/java/gameEngine/input/MouseInput.java @@ -0,0 +1,114 @@ +package gameEngine.input; + +import static org.lwjgl.glfw.GLFW.GLFW_PRESS; +import static org.lwjgl.glfw.GLFW.GLFW_RELEASE; + +public class MouseInput { + private static MouseInput instance; + private double scrollX,scrollY; + private double xPos,yPos,lastX,lastY; + private boolean mouseButtonPressed[] = new boolean[3]; // 現在押されているか + private boolean mouseButtonDown[] = new boolean[3]; // 押された瞬間 + private boolean mouseButtonUp[] = new boolean[3]; // 離された瞬間 + private boolean isDragging; + + private MouseInput(){ + this.scrollX = 0.0; + this.scrollY = 0.0; + this.xPos = 0.0; + this.yPos = 0.0; + this.lastX = 0.0; + this.lastY = 0.0; + } + + public static MouseInput get(){ + if(MouseInput.instance == null){ + MouseInput.instance = new MouseInput(); + } + return MouseInput.instance; + } + + public static void mousePosCallback(long windoow, double xpos, double ypos){ + get().lastX = get().xPos; + get().lastY = get().yPos; + get().xPos = xpos; + get().yPos = ypos; + get().isDragging = get().mouseButtonPressed[0] || get().mouseButtonPressed[1] || get().mouseButtonPressed[2]; + } + + public static void mouseButtonCallback(long window, int button, int action, int mods) { + MouseInput listener = get(); + if (button < listener.mouseButtonPressed.length) { + if (action == GLFW_PRESS) { + if (!listener.mouseButtonPressed[button]) { + listener.mouseButtonDown[button] = true; // 押された瞬間を記録 + } + listener.mouseButtonPressed[button] = true; // 押され続けている状態 + } else if (action == GLFW_RELEASE) { + listener.mouseButtonPressed[button] = false; + listener.mouseButtonUp[button] = true; // 離された瞬間を記録 + listener.isDragging = false; + } + } + } + public static void mouseScrollCallBack(long window, double xOffset, double yOffset){ + get().scrollX = xOffset; + get().scrollY = yOffset; + } + + public static void endFrame(){ + get().scrollX = 0; + get().scrollY = 0; + get().lastX = get().xPos; + get().lastY = get().yPos; + } + + public static float getX(){ + return (float)get().xPos; + } + public static float getY(){ + return (float)get().yPos; + } + public static float getDx(){ + return (float)(get().lastX - get().xPos); + } + + public static float getDy(){ + return (float)(get().lastY - get().yPos); + } + + public static float getScrollX(){ + return (float)get().scrollX; + } + public static float getScrollY(){ + return (float)get().scrollY; + } + public static boolean isDragging(){ + return get().isDragging; + } + + protected static boolean isMouseButtonDown(int button) { + if (button < get().mouseButtonDown.length) { + boolean result = get().mouseButtonDown[button]; + get().mouseButtonDown[button] = false; // 一度だけ検知するためにリセット + return result; + } + return false; + } + + protected static boolean isMouseButtonPressed(int button) { + if (button < get().mouseButtonPressed.length) { + return get().mouseButtonPressed[button]; + } + return false; + } + + protected static boolean isMouseButtonUp(int button) { + if (button < get().mouseButtonUp.length) { + boolean result = get().mouseButtonUp[button]; + get().mouseButtonUp[button] = false; // 一度だけ検知するためにリセット + return result; + } + return false; + } +}