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 final boolean[] mouseButtonPressed = new boolean[3]; // 現在押されているか
private final boolean[] mouseButtonDown = new boolean[3]; // 押された瞬間
private final 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;
}
}