mouse pos implemented, with scaling for stretchmodes

This commit is contained in:
DaniTheSkunk 2022-11-07 22:53:14 +00:00
parent bc706369d0
commit 4a54b21136
5 changed files with 92 additions and 10 deletions

View File

@ -37,5 +37,9 @@ public interface IWindow {
void setStretchMode(WindowStretchMode mode); void setStretchMode(WindowStretchMode mode);
Vec2i getMousePos();
boolean isMouseClicked(int button);
void tick(); void tick();
} }

View File

@ -8,7 +8,7 @@ public class Test extends BaseGame {
private Terminal term; private Terminal term;
public Test() { public Test() {
super(new Vec2i(1280/2, 720/2), "Skunkworks"); super(new Vec2i(40*12, 22*12), "Skunkworks");
} }
public static void main(String[] args) { public static void main(String[] args) {
@ -19,8 +19,9 @@ public class Test extends BaseGame {
protected void init() { protected void init() {
var fontThin = window.loadFontTileset("fonts\\thin-6x12.png"); var fontThin = window.loadFontTileset("fonts\\thin-6x12.png");
var fontThin2 = window.loadFontTileset("fonts\\thin-12x12.png"); var fontThin2 = window.loadFontTileset("fonts\\thin-12x12.png");
term = new Terminal(new Vec2i(80/2, 45/2), fontThin2, fontThin); term = new Terminal(new Vec2i(40, 22), fontThin2, fontThin);
window.setStretchMode(WindowStretchMode.INTEGER);
var c1 = Color.GREEN; var c1 = Color.GREEN;
var c2 = new Color(0, 128, 0); var c2 = new Color(0, 128, 0);
@ -57,4 +58,11 @@ public class Test extends BaseGame {
protected void render(IRenderContext rc) { protected void render(IRenderContext rc) {
rc.drawTerminal(term); rc.drawTerminal(term);
} }
@Override
protected void update(double delta) {
var mouse = window.getMousePos();
term.clear(Color.BLACK);
term.setBackgroundColor(Vec2i.div(mouse, term.getFullCharSize()), Color.GREEN);
}
} }

View File

@ -34,6 +34,13 @@ public final class Vec2f {
); );
} }
public static Vec2f div(Vec2i a, Vec2i b) {
return new Vec2f(
(double) a.getX() / (double) b.getX(),
(double) a.getY() / (double) b.getY()
);
}
//getters and setters //getters and setters
public double getX() { public double getX() {
return x; return x;

View File

@ -65,12 +65,30 @@ public final class Vec2i {
return new Vec2f(a.x / (double) b.x, a.y / (double) b.y); return new Vec2f(a.x / (double) b.x, a.y / (double) b.y);
} }
public static Vec2i min(Vec2i a, Vec2i b) {
return new Vec2i(Math.min(a.x, b.x), Math.min(a.y, b.y));
}
public static Vec2i max(Vec2i a, Vec2i b) {
return new Vec2i(Math.max(a.x, b.x), Math.max(a.y, b.y));
}
public static Vec2i tween(Vec2i a, Vec2i b, double amount) { public static Vec2i tween(Vec2i a, Vec2i b, double amount) {
return new Vec2i(Util.tweenInt(a.x, b.x, amount), return new Vec2i(Util.tweenInt(a.x, b.x, amount),
Util.tweenInt(a.y, b.y, amount) Util.tweenInt(a.y, b.y, amount)
); );
} }
public static Vec2i mul(Vec2f a, Vec2i b) {
return new Vec2i((int) (a.getX() * b.getX()),
(int) (a.getY() * b.getY())
);
}
public static Vec2i mul(Vec2i a, double b) {
return new Vec2i((int)(a.x / b), (int)(a.y / b));
}
public int getX() { public int getX() {
return x; return x;

View File

@ -1,9 +1,6 @@
package com.danitheskunk.skunkworks.backends.gl; package com.danitheskunk.skunkworks.backends.gl;
import com.danitheskunk.skunkworks.BaseWindow; import com.danitheskunk.skunkworks.*;
import com.danitheskunk.skunkworks.Engine;
import com.danitheskunk.skunkworks.Recti;
import com.danitheskunk.skunkworks.Vec2i;
import com.danitheskunk.skunkworks.gfx.IRenderContext; import com.danitheskunk.skunkworks.gfx.IRenderContext;
import com.danitheskunk.skunkworks.gfx.ITexture; import com.danitheskunk.skunkworks.gfx.ITexture;
import com.danitheskunk.skunkworks.gfx.Image; import com.danitheskunk.skunkworks.gfx.Image;
@ -242,13 +239,17 @@ public class Window extends BaseWindow {
float scaley = (float) windowSize.getY() / (float) size.getY(); float scaley = (float) windowSize.getY() / (float) size.getY();
float scale = Math.min(scalex, scaley); float scale = Math.min(scalex, scaley);
int xoff = (int)((windowSize.getX() - size.getX() * scale) / 2); int xoff = (int) (
int yoff = (int)((windowSize.getY() - size.getY() * scale) / 2); (windowSize.getX() - size.getX() * scale) / 2
);
int yoff = (int) (
(windowSize.getY() - size.getY() * scale) / 2
);
tlx1 = xoff; tlx1 = xoff;
tly1 = yoff; tly1 = yoff;
tlx2 = (int)(xoff + size.getX() * scale); tlx2 = (int) (xoff + size.getX() * scale);
tly2 = (int)(yoff + size.getY() * scale); tly2 = (int) (yoff + size.getY() * scale);
break; break;
} }
case INTEGER: { case INTEGER: {
@ -314,6 +315,50 @@ public class Window extends BaseWindow {
return shouldClose; return shouldClose;
} }
@Override
public Vec2i getMousePos() {
//todo: scale mouse to stretchMode
double[] x = {0.0};
double[] y = {0.0};
glfwGetCursorPos(window, x, y);
var mousePos = new Vec2i((int) x[0], (int) y[0]);
var scaledMousePos = switch(this.stretchMode) {
case STRETCH -> {
var stretch = Vec2f.div(this.size, this.windowSize);
yield Vec2i.mul(stretch, mousePos);
}
case ASPECT -> {
var scale = Vec2f.div(this.size, this.windowSize);
var scalef = Math.max(scale.getX(), scale.getY());
var off = Vec2f.div(Vec2i.sub(windowSize,
Vec2f.mul(size.toVec2f(), 1.0 / scalef).toVec2i()
).toVec2f(), 2).toVec2i();
yield Vec2i.mul(Vec2i.sub(mousePos, off), 1.0 / scalef);
}
case INTEGER -> {
var scale = Vec2i.div(this.windowSize, this.size);
var scalei = Math.max(1, Math.min(scale.getX(), scale.getY()));
var off = Vec2i.div(Vec2i.sub(windowSize,
Vec2i.mul(size, scalei)
), 2);
yield Vec2i.div(Vec2i.sub(mousePos, off), scalei);
}
};
return Vec2i.max(Vec2i.ZERO,
Vec2i.min(Vec2i.sub(size, new Vec2i(1, 1)), scaledMousePos)
);
}
@Override
public boolean isMouseClicked(int button) {
return false;
}
@Override @Override
public void tick() { public void tick() {
glfwMakeContextCurrent(window); glfwMakeContextCurrent(window);