mouse button input working

This commit is contained in:
DaniTheSkunk 2022-11-07 23:17:09 +00:00
parent 4a54b21136
commit 82dd72b15c
3 changed files with 36 additions and 3 deletions

View File

@ -41,5 +41,7 @@ public interface IWindow {
boolean isMouseClicked(int button);
boolean isMouseDown(int button);
void tick();
}

View File

@ -63,6 +63,10 @@ public class Test extends BaseGame {
protected void update(double delta) {
var mouse = window.getMousePos();
term.clear(Color.BLACK);
term.setBackgroundColor(Vec2i.div(mouse, term.getFullCharSize()), Color.GREEN);
var col = window.isMouseDown(0) ? Color.RED : Color.GREEN;
term.setBackgroundColor(Vec2i.div(mouse, term.getFullCharSize()), col);
if(window.isMouseClicked(1)) {
System.out.println("right click");
}
}
}

View File

@ -9,6 +9,7 @@ import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.lwjgl.glfw.GLFW.*;
@ -77,6 +78,8 @@ public class Window extends BaseWindow {
private boolean shouldClose;
private final int framebuffer;
private final int framebufferTex;
private boolean[] stateMouseClicked;
private boolean[] stateMouseDown;
public Window(Vec2i size, String title, Engine engine) {
super(engine);
@ -89,6 +92,9 @@ public class Window extends BaseWindow {
this.size = size;
this.windowSize = size;
this.stateMouseClicked = new boolean[8];
this.stateMouseDown = new boolean[8];
window = glfwCreateWindow(size.getX(), size.getY(), title, NULL, NULL);
if(window == NULL) throw new RuntimeException(
"Failed to create GLFW window");
@ -107,6 +113,7 @@ public class Window extends BaseWindow {
glEnable(GL_BLEND);
glfwSetWindowSizeCallback(window, this::windowSizeCallback);
glfwSetMouseButtonCallback(window, this::mouseButtonCallback);
framebuffer = glGenFramebuffers();
@ -156,6 +163,20 @@ public class Window extends BaseWindow {
System.out.println(GL11.glGetInteger(GL_MAX_TEXTURE_SIZE));
}
private void mouseButtonCallback(
long window, int button, int action, int mods
) {
switch(action) {
case GLFW_PRESS -> {
this.stateMouseClicked[button] = true;
this.stateMouseDown[button] = true;
}
case GLFW_RELEASE -> {
this.stateMouseDown[button] = false;
}
}
}
private void windowSizeCallback(long window, int width, int height) {
System.out.printf("new window size %d x %d\n", width, height);
windowSize = new Vec2i(width, height);
@ -356,17 +377,23 @@ public class Window extends BaseWindow {
@Override
public boolean isMouseClicked(int button) {
return false;
return this.stateMouseClicked[button];
}
@Override
public void tick() {
public boolean isMouseDown(int button) {
return this.stateMouseDown[button];
}
@Override
public void tick() { //one tick per update call?
glfwMakeContextCurrent(window);
if(glfwWindowShouldClose(window)) {
shouldClose = true;
return;
}
Arrays.fill(stateMouseClicked, false);
glfwPollEvents();
}
}