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.Batch;
|
||||||
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
||||||
import com.badlogic.gdx.graphics.g2d.NinePatch;
|
import com.badlogic.gdx.graphics.g2d.NinePatch;
|
||||||
import com.badlogic.gdx.math.Matrix4;
|
|
||||||
import com.badlogic.gdx.math.Rectangle;
|
import com.badlogic.gdx.math.Rectangle;
|
||||||
import com.badlogic.gdx.scenes.scene2d.utils.ScissorStack;
|
import com.badlogic.gdx.scenes.scene2d.utils.ScissorStack;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
@@ -28,12 +27,17 @@ public class GraphicsContext {
|
|||||||
|
|
||||||
private final Stack<GraphicsState> stateStack = new java.util.Stack<>();
|
private final Stack<GraphicsState> stateStack = new java.util.Stack<>();
|
||||||
|
|
||||||
|
private int translateX = 0;
|
||||||
|
private int translateY = 0;
|
||||||
|
|
||||||
private static class GraphicsState {
|
private static class GraphicsState {
|
||||||
private final Matrix4 transform;
|
private final int translateX;
|
||||||
|
private final int translateY;
|
||||||
private int scissorCount;
|
private int scissorCount;
|
||||||
|
|
||||||
public GraphicsState(Matrix4 transform, int scissorCount) {
|
public GraphicsState(int translateX, int translateY, int scissorCount) {
|
||||||
this.transform = transform.cpy();
|
this.translateX = translateX;
|
||||||
|
this.translateY = translateY;
|
||||||
this.scissorCount = scissorCount;
|
this.scissorCount = scissorCount;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -47,7 +51,7 @@ public class GraphicsContext {
|
|||||||
* @param height The height.
|
* @param height The height.
|
||||||
*/
|
*/
|
||||||
public void draw(NinePatch image, int x, int y, int width, int 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.
|
* @param y The y-coordinate.
|
||||||
*/
|
*/
|
||||||
public void draw(Texture texture, int x, int y) {
|
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) {
|
public void draw(Texture texture, int x, int y, Parameters params) {
|
||||||
batch.draw(texture,
|
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(),
|
0, 0, texture.getWidth(), texture.getHeight(),
|
||||||
params.flipX, params.flipY);
|
params.flipX, params.flipY);
|
||||||
}
|
}
|
||||||
@@ -81,7 +85,7 @@ public class GraphicsContext {
|
|||||||
* @param y The y coordinate to draw at.
|
* @param y The y coordinate to draw at.
|
||||||
*/
|
*/
|
||||||
public void draw(String text, int x, int y) {
|
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) {
|
public void fillRect(int x, int y, int width, int height) {
|
||||||
var texture = DiceOS.getResourceLoader().loadTexture("system/white.png");
|
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.
|
* @param y The number of pixels to move down by.
|
||||||
*/
|
*/
|
||||||
public void translate(int x, int y) {
|
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.
|
* @return The total amount of X-translation.
|
||||||
*/
|
*/
|
||||||
public int getXTranslation() {
|
public int getXTranslation() {
|
||||||
return (int) batch.getTransformMatrix().val[com.badlogic.gdx.math.Matrix4.M03];
|
return translateX;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getYTranslation() {
|
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()}.
|
* The state can later be restored using {@link #restore()}.
|
||||||
*/
|
*/
|
||||||
public void save() {
|
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();
|
batch.flush();
|
||||||
GraphicsState state = stateStack.pop();
|
GraphicsState state = stateStack.pop();
|
||||||
batch.setTransformMatrix(state.transform);
|
translateX = state.translateX;
|
||||||
|
translateY = state.translateY;
|
||||||
for (int i = 0; i < state.scissorCount; i++) {
|
for (int i = 0; i < state.scissorCount; i++) {
|
||||||
ScissorStack.popScissors();
|
ScissorStack.popScissors();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import org.junit.jupiter.api.BeforeEach;
|
|||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
@@ -59,7 +60,8 @@ class GraphicsContextTest {
|
|||||||
@Test
|
@Test
|
||||||
void testTranslate() {
|
void testTranslate() {
|
||||||
gc.translate(10, 20);
|
gc.translate(10, 20);
|
||||||
verify(batch).setTransformMatrix(any(Matrix4.class));
|
assertEquals(10, gc.getXTranslation());
|
||||||
|
assertEquals(20, gc.getYTranslation());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user