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);
|
||||
|
||||
Vec2i getMousePos();
|
||||
IMouse getMouse();
|
||||
|
||||
IPipeline<IRenderContext> getPipeline2D();
|
||||
|
||||
IPipeline<IRenderContext3D> getPipeline3D();
|
||||
|
||||
boolean isMouseClicked(int button);
|
||||
|
||||
boolean isMouseDown(int button);
|
||||
|
||||
IFont loadFontTTF(String path, float size);
|
||||
|
||||
IFont loadFontTileset(String path);
|
||||
|
|
|
@ -63,11 +63,12 @@ public class Test extends BaseGame {
|
|||
|
||||
@Override
|
||||
protected void update(double delta) {
|
||||
var mouse = window.getMousePos();
|
||||
var mouse = window.getMouse();
|
||||
var mousePos = mouse.getPos();
|
||||
term.clear(Color.TRANS_BLACK);
|
||||
var col = window.isMouseDown(0) ? Color.RED : Color.GREEN;
|
||||
term.setBackgroundColor(Vec2i.div(mouse, term.getFullCharSize()), col);
|
||||
if(window.isMouseClicked(1)) {
|
||||
var col = mouse.isDown(0) ? Color.RED : Color.GREEN;
|
||||
term.setBackgroundColor(Vec2i.div(mousePos, term.getFullCharSize()), col);
|
||||
if(mouse.isClicked(1)) {
|
||||
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.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.opengl.GL46.*;
|
||||
|
@ -17,16 +20,15 @@ import static org.lwjgl.system.MemoryUtil.NULL;
|
|||
public class Window extends BaseWindow {
|
||||
|
||||
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 boolean[] stateMouseClicked;
|
||||
private final boolean[] stateMouseDown;
|
||||
private final TextureAtlas textureAtlas;
|
||||
private final long window;
|
||||
private boolean shouldClose;
|
||||
private Vec2i windowSize;
|
||||
private final Pipeline2D pipeline2D;
|
||||
private final Pipeline3D pipeline3D;
|
||||
private final Map<String, Filter> scalers;
|
||||
|
||||
//private final FilterFXAA fxaa;
|
||||
|
||||
|
@ -40,8 +42,6 @@ public class Window extends BaseWindow {
|
|||
this.size = size;
|
||||
windowSize = size;
|
||||
|
||||
stateMouseClicked = new boolean[8];
|
||||
stateMouseDown = new boolean[8];
|
||||
|
||||
window = glfwCreateWindow(size.getX(), size.getY(), title, NULL, NULL);
|
||||
if(window == NULL) throw new RuntimeException(
|
||||
|
@ -61,7 +61,6 @@ public class Window extends BaseWindow {
|
|||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
glfwSetWindowSizeCallback(window, this::windowSizeCallback);
|
||||
glfwSetMouseButtonCallback(window, this::mouseButtonCallback);
|
||||
|
||||
framebuffer = new Framebuffer(size);
|
||||
textureAtlas = new TextureAtlas();
|
||||
|
@ -75,6 +74,7 @@ public class Window extends BaseWindow {
|
|||
scalers.put("aspect", new ScalerAspect(framebuffer));
|
||||
scalers.put("integer", new ScalerInteger(framebuffer));
|
||||
scaler = "aspect";
|
||||
mouse = new Mouse(window, scalers.get(scaler));
|
||||
|
||||
// = new ScalerInteger(framebuffer);
|
||||
|
||||
|
@ -97,17 +97,11 @@ public class Window extends BaseWindow {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Vec2i getMousePos() {
|
||||
double[] x = {0.0};
|
||||
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)
|
||||
);
|
||||
public IMouse getMouse() {
|
||||
return mouse;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public IPipeline getPipeline2D() {
|
||||
return pipeline2D;
|
||||
|
@ -118,16 +112,6 @@ public class Window extends BaseWindow {
|
|||
return pipeline3D;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMouseClicked(int button) {
|
||||
return stateMouseClicked[button];
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMouseDown(int button) {
|
||||
return stateMouseDown[button];
|
||||
}
|
||||
|
||||
@Override
|
||||
public ITexture loadTexture(Image 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
|
||||
public void runScaler() {
|
||||
//fxaa.apply();
|
||||
|
@ -191,6 +161,12 @@ public class Window extends BaseWindow {
|
|||
pipeline2D.setDebug(on);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setScaler(String scaler) {
|
||||
super.setScaler(scaler);
|
||||
mouse.setScaler(scalers.get(scaler));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldClose() {
|
||||
return shouldClose;
|
||||
|
@ -211,7 +187,7 @@ public class Window extends BaseWindow {
|
|||
return;
|
||||
}
|
||||
|
||||
Arrays.fill(stateMouseClicked, false);
|
||||
mouse.tick();
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue