refactored mouse
This commit is contained in:
parent
5aa96f2b7f
commit
4ee489c1e6
|
@ -0,0 +1,9 @@
|
||||||
|
package com.danitheskunk.skunkworks;
|
||||||
|
|
||||||
|
public interface IMouse {
|
||||||
|
Vec2i getPos();
|
||||||
|
|
||||||
|
boolean isClicked(int button);
|
||||||
|
|
||||||
|
boolean isDown(int button);
|
||||||
|
}
|
|
@ -13,16 +13,12 @@ public interface IWindow {
|
||||||
|
|
||||||
BaseGamepad getGamepad(int id);
|
BaseGamepad getGamepad(int id);
|
||||||
|
|
||||||
Vec2i getMousePos();
|
IMouse getMouse();
|
||||||
|
|
||||||
IPipeline<IRenderContext> getPipeline2D();
|
IPipeline<IRenderContext> getPipeline2D();
|
||||||
|
|
||||||
IPipeline<IRenderContext3D> getPipeline3D();
|
IPipeline<IRenderContext3D> getPipeline3D();
|
||||||
|
|
||||||
boolean isMouseClicked(int button);
|
|
||||||
|
|
||||||
boolean isMouseDown(int button);
|
|
||||||
|
|
||||||
IFont loadFontTTF(String path, float size);
|
IFont loadFontTTF(String path, float size);
|
||||||
|
|
||||||
IFont loadFontTileset(String path);
|
IFont loadFontTileset(String path);
|
||||||
|
|
|
@ -63,11 +63,12 @@ public class Test extends BaseGame {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void update(double delta) {
|
protected void update(double delta) {
|
||||||
var mouse = window.getMousePos();
|
var mouse = window.getMouse();
|
||||||
|
var mousePos = mouse.getPos();
|
||||||
term.clear(Color.TRANS_BLACK);
|
term.clear(Color.TRANS_BLACK);
|
||||||
var col = window.isMouseDown(0) ? Color.RED : Color.GREEN;
|
var col = mouse.isDown(0) ? Color.RED : Color.GREEN;
|
||||||
term.setBackgroundColor(Vec2i.div(mouse, term.getFullCharSize()), col);
|
term.setBackgroundColor(Vec2i.div(mousePos, term.getFullCharSize()), col);
|
||||||
if(window.isMouseClicked(1)) {
|
if(mouse.isClicked(1)) {
|
||||||
System.out.println("right click");
|
System.out.println("right click");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,68 @@
|
||||||
|
package com.danitheskunk.skunkworks.backends.gl;
|
||||||
|
|
||||||
|
import com.danitheskunk.skunkworks.IMouse;
|
||||||
|
import com.danitheskunk.skunkworks.Vec2i;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import static org.lwjgl.glfw.GLFW.*;
|
||||||
|
|
||||||
|
public class Mouse implements IMouse {
|
||||||
|
private final boolean[] stateMouseClicked;
|
||||||
|
private final boolean[] stateMouseDown;
|
||||||
|
private final long window;
|
||||||
|
private Filter scaler;
|
||||||
|
|
||||||
|
public Mouse(long window, Filter scaler) {
|
||||||
|
this.window = window;
|
||||||
|
stateMouseClicked = new boolean[8];
|
||||||
|
stateMouseDown = new boolean[8];
|
||||||
|
glfwSetMouseButtonCallback(window, this::mouseButtonCallback);
|
||||||
|
this.scaler = scaler;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vec2i getPos() {
|
||||||
|
double[] x = {0.0};
|
||||||
|
double[] y = {0.0};
|
||||||
|
glfwGetCursorPos(window, x, y);
|
||||||
|
var mousePos = new Vec2i((int) x[0], (int) y[0]);
|
||||||
|
var scaledMousePos = scaler.translateScreenCoordinate(mousePos);
|
||||||
|
return Vec2i.max(
|
||||||
|
Vec2i.ZERO,
|
||||||
|
Vec2i.min(
|
||||||
|
Vec2i.sub(scaler.input.getSize(), new Vec2i(1, 1)),
|
||||||
|
scaledMousePos
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void mouseButtonCallback(
|
||||||
|
@SuppressWarnings("unused") long window, int button, int action, @SuppressWarnings(
|
||||||
|
"unused") int mods
|
||||||
|
) {
|
||||||
|
switch(action) {
|
||||||
|
case GLFW_PRESS -> {
|
||||||
|
stateMouseClicked[button] = true;
|
||||||
|
stateMouseDown[button] = true;
|
||||||
|
}
|
||||||
|
case GLFW_RELEASE -> stateMouseDown[button] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setScaler(Filter scaler) {
|
||||||
|
this.scaler = scaler;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void tick() {
|
||||||
|
Arrays.fill(stateMouseClicked, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isClicked(int button) {
|
||||||
|
return stateMouseClicked[button];
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isDown(int button) {
|
||||||
|
return stateMouseDown[button];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -8,7 +8,10 @@ import org.lwjgl.glfw.GLFWErrorCallback;
|
||||||
import org.lwjgl.opengl.GL;
|
import org.lwjgl.opengl.GL;
|
||||||
import org.lwjgl.opengl.GL11;
|
import org.lwjgl.opengl.GL11;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import static org.lwjgl.glfw.GLFW.*;
|
import static org.lwjgl.glfw.GLFW.*;
|
||||||
import static org.lwjgl.opengl.GL46.*;
|
import static org.lwjgl.opengl.GL46.*;
|
||||||
|
@ -17,16 +20,15 @@ import static org.lwjgl.system.MemoryUtil.NULL;
|
||||||
public class Window extends BaseWindow {
|
public class Window extends BaseWindow {
|
||||||
|
|
||||||
private final Framebuffer framebuffer;
|
private final Framebuffer framebuffer;
|
||||||
|
private final Mouse mouse;
|
||||||
|
private final Pipeline2D pipeline2D;
|
||||||
|
private final Pipeline3D pipeline3D;
|
||||||
|
private final Map<String, Filter> scalers;
|
||||||
private final Vec2i size;
|
private final Vec2i size;
|
||||||
private final boolean[] stateMouseClicked;
|
|
||||||
private final boolean[] stateMouseDown;
|
|
||||||
private final TextureAtlas textureAtlas;
|
private final TextureAtlas textureAtlas;
|
||||||
private final long window;
|
private final long window;
|
||||||
private boolean shouldClose;
|
private boolean shouldClose;
|
||||||
private Vec2i windowSize;
|
private Vec2i windowSize;
|
||||||
private final Pipeline2D pipeline2D;
|
|
||||||
private final Pipeline3D pipeline3D;
|
|
||||||
private final Map<String, Filter> scalers;
|
|
||||||
|
|
||||||
//private final FilterFXAA fxaa;
|
//private final FilterFXAA fxaa;
|
||||||
|
|
||||||
|
@ -40,8 +42,6 @@ public class Window extends BaseWindow {
|
||||||
this.size = size;
|
this.size = size;
|
||||||
windowSize = size;
|
windowSize = size;
|
||||||
|
|
||||||
stateMouseClicked = new boolean[8];
|
|
||||||
stateMouseDown = new boolean[8];
|
|
||||||
|
|
||||||
window = glfwCreateWindow(size.getX(), size.getY(), title, NULL, NULL);
|
window = glfwCreateWindow(size.getX(), size.getY(), title, NULL, NULL);
|
||||||
if(window == NULL) throw new RuntimeException(
|
if(window == NULL) throw new RuntimeException(
|
||||||
|
@ -61,7 +61,6 @@ public class Window extends BaseWindow {
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
|
||||||
glfwSetWindowSizeCallback(window, this::windowSizeCallback);
|
glfwSetWindowSizeCallback(window, this::windowSizeCallback);
|
||||||
glfwSetMouseButtonCallback(window, this::mouseButtonCallback);
|
|
||||||
|
|
||||||
framebuffer = new Framebuffer(size);
|
framebuffer = new Framebuffer(size);
|
||||||
textureAtlas = new TextureAtlas();
|
textureAtlas = new TextureAtlas();
|
||||||
|
@ -75,8 +74,9 @@ public class Window extends BaseWindow {
|
||||||
scalers.put("aspect", new ScalerAspect(framebuffer));
|
scalers.put("aspect", new ScalerAspect(framebuffer));
|
||||||
scalers.put("integer", new ScalerInteger(framebuffer));
|
scalers.put("integer", new ScalerInteger(framebuffer));
|
||||||
scaler = "aspect";
|
scaler = "aspect";
|
||||||
|
mouse = new Mouse(window, scalers.get(scaler));
|
||||||
|
|
||||||
// = new ScalerInteger(framebuffer);
|
// = new ScalerInteger(framebuffer);
|
||||||
|
|
||||||
shouldClose = false;
|
shouldClose = false;
|
||||||
|
|
||||||
|
@ -97,17 +97,11 @@ public class Window extends BaseWindow {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Vec2i getMousePos() {
|
public IMouse getMouse() {
|
||||||
double[] x = {0.0};
|
return mouse;
|
||||||
double[] y = {0.0};
|
|
||||||
glfwGetCursorPos(window, x, y);
|
|
||||||
var mousePos = new Vec2i((int) x[0], (int) y[0]);
|
|
||||||
var scaledMousePos = scalers.get(scaler).translateScreenCoordinate(mousePos);
|
|
||||||
return Vec2i.max(Vec2i.ZERO,
|
|
||||||
Vec2i.min(Vec2i.sub(size, new Vec2i(1, 1)), scaledMousePos)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IPipeline getPipeline2D() {
|
public IPipeline getPipeline2D() {
|
||||||
return pipeline2D;
|
return pipeline2D;
|
||||||
|
@ -118,16 +112,6 @@ public class Window extends BaseWindow {
|
||||||
return pipeline3D;
|
return pipeline3D;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isMouseClicked(int button) {
|
|
||||||
return stateMouseClicked[button];
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isMouseDown(int button) {
|
|
||||||
return stateMouseDown[button];
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ITexture loadTexture(Image image) {
|
public ITexture loadTexture(Image image) {
|
||||||
return textureAtlas.addTexture(image);
|
return textureAtlas.addTexture(image);
|
||||||
|
@ -164,20 +148,6 @@ public class Window extends BaseWindow {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void mouseButtonCallback(
|
|
||||||
@SuppressWarnings("unused") long window, int button, int action, @SuppressWarnings(
|
|
||||||
"unused") int mods
|
|
||||||
) {
|
|
||||||
switch(action) {
|
|
||||||
case GLFW_PRESS -> {
|
|
||||||
stateMouseClicked[button] = true;
|
|
||||||
stateMouseDown[button] = true;
|
|
||||||
}
|
|
||||||
case GLFW_RELEASE -> stateMouseDown[button] = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void runScaler() {
|
public void runScaler() {
|
||||||
//fxaa.apply();
|
//fxaa.apply();
|
||||||
|
@ -191,6 +161,12 @@ public class Window extends BaseWindow {
|
||||||
pipeline2D.setDebug(on);
|
pipeline2D.setDebug(on);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setScaler(String scaler) {
|
||||||
|
super.setScaler(scaler);
|
||||||
|
mouse.setScaler(scalers.get(scaler));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean shouldClose() {
|
public boolean shouldClose() {
|
||||||
return shouldClose;
|
return shouldClose;
|
||||||
|
@ -211,7 +187,7 @@ public class Window extends BaseWindow {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Arrays.fill(stateMouseClicked, false);
|
mouse.tick();
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue