Add Component and Container classes for UI structure and implement scissor method in GraphicsContext
This commit is contained in:
@@ -66,7 +66,7 @@ public class DiceOS extends ApplicationAdapter {
|
||||
|
||||
cursorService.setScale(scaling);
|
||||
|
||||
gc = new GraphicsContext(display.getBatch(), displayHeight, displayWidth);
|
||||
gc = new GraphicsContext(screenViewport.getCamera(), display.getBatch(), displayHeight, displayWidth);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -84,7 +84,7 @@ public class DiceOS extends ApplicationAdapter {
|
||||
// Render background
|
||||
gc.draw(clouds, 0, 0);
|
||||
|
||||
windowService.paint(gc);
|
||||
gc.scissor(0, 0, gc.getWidth(), gc.getHeight(), windowService::paint);
|
||||
|
||||
// Render borders
|
||||
var param = new GraphicsContext.Parameters();
|
||||
|
||||
@@ -24,6 +24,6 @@ public class WindowService implements OnStartup {
|
||||
|
||||
void paint(GraphicsContext gc) {
|
||||
// Render menubar
|
||||
gc.draw(menubar, 0, 0, gc.getWidth(), 13);
|
||||
gc.draw(menubar, 0, 0, gc.getWidth(), 14);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,23 @@
|
||||
package be.seeseemelk.diceos.system.gfx;
|
||||
|
||||
import com.badlogic.gdx.graphics.Camera;
|
||||
import com.badlogic.gdx.graphics.Texture;
|
||||
import com.badlogic.gdx.graphics.g2d.Batch;
|
||||
import com.badlogic.gdx.graphics.g2d.NinePatch;
|
||||
import com.badlogic.gdx.math.Rectangle;
|
||||
import com.badlogic.gdx.scenes.scene2d.utils.ScissorStack;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Graphics context and rendering utilities.
|
||||
*/
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public class GraphicsContext {
|
||||
private final Camera camera;
|
||||
private final Batch batch;
|
||||
private final int height;
|
||||
private final int width;
|
||||
@@ -31,6 +37,18 @@ public class GraphicsContext {
|
||||
params.flipX, params.flipY);
|
||||
}
|
||||
|
||||
public void scissor(int x, int y, int width, int height, Consumer<GraphicsContext> callback) {
|
||||
var scissors = new Rectangle();
|
||||
var clipBounds = new Rectangle(x, getHeight() - y - height, width, height);
|
||||
ScissorStack.calculateScissors(camera, batch.getTransformMatrix(), clipBounds, scissors);
|
||||
batch.flush();
|
||||
if (ScissorStack.pushScissors(scissors)) {
|
||||
callback.accept(this);
|
||||
batch.flush();
|
||||
ScissorStack.popScissors();
|
||||
}
|
||||
}
|
||||
|
||||
public static class Parameters {
|
||||
public final static Parameters DEFAULT = new Parameters();
|
||||
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package be.seeseemelk.diceos.system.toolkit;
|
||||
|
||||
import be.seeseemelk.diceos.system.gfx.GraphicsContext;
|
||||
|
||||
public abstract class Component {
|
||||
public abstract void paint(GraphicsContext gc);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package be.seeseemelk.diceos.system.toolkit;
|
||||
|
||||
import be.seeseemelk.diceos.system.gfx.GraphicsContext;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class Container extends Component {
|
||||
private final List<Component> components = new ArrayList<>();
|
||||
|
||||
public void addComponent(Component component) {
|
||||
components.add(component);
|
||||
}
|
||||
|
||||
public void removeComponent(Component component) {
|
||||
components.remove(component);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paint(GraphicsContext gc) {
|
||||
for (Component component : components) {
|
||||
component.paint(gc);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package be.seeseemelk.diceos.system.toolkit;
|
||||
|
||||
import be.seeseemelk.diceos.system.gfx.GraphicsContext;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
public class Window extends Container {
|
||||
private String title;
|
||||
|
||||
@Override
|
||||
public void paint(GraphicsContext gc) {
|
||||
super.paint(gc);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user