Refactor GraphicsContext: implement translation handling and update drawing methods to use translation values
Build and Test / build (push) Has been cancelled
Build and Test / build (push) Has been cancelled
This commit is contained in:
@@ -6,7 +6,6 @@ import com.badlogic.gdx.graphics.Texture;
|
||||
import com.badlogic.gdx.graphics.g2d.Batch;
|
||||
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
||||
import com.badlogic.gdx.graphics.g2d.NinePatch;
|
||||
import com.badlogic.gdx.math.Matrix4;
|
||||
import com.badlogic.gdx.math.Rectangle;
|
||||
import com.badlogic.gdx.scenes.scene2d.utils.ScissorStack;
|
||||
import lombok.Getter;
|
||||
@@ -28,12 +27,17 @@ public class GraphicsContext {
|
||||
|
||||
private final Stack<GraphicsState> stateStack = new java.util.Stack<>();
|
||||
|
||||
private int translateX = 0;
|
||||
private int translateY = 0;
|
||||
|
||||
private static class GraphicsState {
|
||||
private final Matrix4 transform;
|
||||
private final int translateX;
|
||||
private final int translateY;
|
||||
private int scissorCount;
|
||||
|
||||
public GraphicsState(Matrix4 transform, int scissorCount) {
|
||||
this.transform = transform.cpy();
|
||||
public GraphicsState(int translateX, int translateY, int scissorCount) {
|
||||
this.translateX = translateX;
|
||||
this.translateY = translateY;
|
||||
this.scissorCount = scissorCount;
|
||||
}
|
||||
}
|
||||
@@ -47,7 +51,7 @@ public class GraphicsContext {
|
||||
* @param height The height.
|
||||
*/
|
||||
public void draw(NinePatch image, int x, int y, int width, int height) {
|
||||
image.draw(batch, x, getHeight() - height - y, width, height);
|
||||
image.draw(batch, x + translateX, getHeight() - height - (y + translateY), width, height);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,7 +61,7 @@ public class GraphicsContext {
|
||||
* @param y The y-coordinate.
|
||||
*/
|
||||
public void draw(Texture texture, int x, int y) {
|
||||
draw(texture, x, y, Parameters.DEFAULT);
|
||||
draw(texture, x + translateX, y + translateY, Parameters.DEFAULT);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -69,7 +73,7 @@ public class GraphicsContext {
|
||||
*/
|
||||
public void draw(Texture texture, int x, int y, Parameters params) {
|
||||
batch.draw(texture,
|
||||
x, getHeight() - y - texture.getHeight(), texture.getWidth(), texture.getHeight(),
|
||||
x + translateX, getHeight() - (y + translateY) - texture.getHeight(), texture.getWidth(), texture.getHeight(),
|
||||
0, 0, texture.getWidth(), texture.getHeight(),
|
||||
params.flipX, params.flipY);
|
||||
}
|
||||
@@ -81,7 +85,7 @@ public class GraphicsContext {
|
||||
* @param y The y coordinate to draw at.
|
||||
*/
|
||||
public void draw(String text, int x, int y) {
|
||||
font.draw(batch, text, x, getHeight() - (y + font.getLineHeight()));
|
||||
font.draw(batch, text, x + translateX, getHeight() - (y + translateY + font.getLineHeight()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -93,7 +97,7 @@ public class GraphicsContext {
|
||||
*/
|
||||
public void fillRect(int x, int y, int width, int height) {
|
||||
var texture = DiceOS.getResourceLoader().loadTexture("system/white.png");
|
||||
batch.draw(texture, x, getHeight() - height - y, width, height); // This is not ideal.
|
||||
batch.draw(texture, x + translateX, getHeight() - height - (y + translateY), width, height);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -114,7 +118,8 @@ public class GraphicsContext {
|
||||
* @param y The number of pixels to move down by.
|
||||
*/
|
||||
public void translate(int x, int y) {
|
||||
batch.setTransformMatrix(batch.getTransformMatrix().trn(x, -y, 0));
|
||||
translateX += x;
|
||||
translateY += y;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -123,11 +128,11 @@ public class GraphicsContext {
|
||||
* @return The total amount of X-translation.
|
||||
*/
|
||||
public int getXTranslation() {
|
||||
return (int) batch.getTransformMatrix().val[com.badlogic.gdx.math.Matrix4.M03];
|
||||
return translateX;
|
||||
}
|
||||
|
||||
public int getYTranslation() {
|
||||
return (int) batch.getTransformMatrix().val[com.badlogic.gdx.math.Matrix4.M13];
|
||||
return translateY;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -170,7 +175,7 @@ public class GraphicsContext {
|
||||
* The state can later be restored using {@link #restore()}.
|
||||
*/
|
||||
public void save() {
|
||||
stateStack.push(new GraphicsState(batch.getTransformMatrix(), 0));
|
||||
stateStack.push(new GraphicsState(translateX, translateY, 0));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -183,7 +188,8 @@ public class GraphicsContext {
|
||||
}
|
||||
batch.flush();
|
||||
GraphicsState state = stateStack.pop();
|
||||
batch.setTransformMatrix(state.transform);
|
||||
translateX = state.translateX;
|
||||
translateY = state.translateY;
|
||||
for (int i = 0; i < state.scissorCount; i++) {
|
||||
ScissorStack.popScissors();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user