Add Component and Container classes for UI structure and implement scissor method in GraphicsContext

This commit is contained in:
2025-12-19 21:04:39 +01:00
parent 209501b4c1
commit 30e5b8bd02
6 changed files with 69 additions and 3 deletions

View File

@@ -66,7 +66,7 @@ public class DiceOS extends ApplicationAdapter {
cursorService.setScale(scaling); cursorService.setScale(scaling);
gc = new GraphicsContext(display.getBatch(), displayHeight, displayWidth); gc = new GraphicsContext(screenViewport.getCamera(), display.getBatch(), displayHeight, displayWidth);
} }
@Override @Override
@@ -84,7 +84,7 @@ public class DiceOS extends ApplicationAdapter {
// Render background // Render background
gc.draw(clouds, 0, 0); gc.draw(clouds, 0, 0);
windowService.paint(gc); gc.scissor(0, 0, gc.getWidth(), gc.getHeight(), windowService::paint);
// Render borders // Render borders
var param = new GraphicsContext.Parameters(); var param = new GraphicsContext.Parameters();

View File

@@ -24,6 +24,6 @@ public class WindowService implements OnStartup {
void paint(GraphicsContext gc) { void paint(GraphicsContext gc) {
// Render menubar // Render menubar
gc.draw(menubar, 0, 0, gc.getWidth(), 13); gc.draw(menubar, 0, 0, gc.getWidth(), 14);
} }
} }

View File

@@ -1,17 +1,23 @@
package be.seeseemelk.diceos.system.gfx; package be.seeseemelk.diceos.system.gfx;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.Texture; 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.NinePatch; 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.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import java.util.function.Consumer;
/** /**
* Graphics context and rendering utilities. * Graphics context and rendering utilities.
*/ */
@Getter @Getter
@RequiredArgsConstructor @RequiredArgsConstructor
public class GraphicsContext { public class GraphicsContext {
private final Camera camera;
private final Batch batch; private final Batch batch;
private final int height; private final int height;
private final int width; private final int width;
@@ -31,6 +37,18 @@ public class GraphicsContext {
params.flipX, params.flipY); 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 static class Parameters {
public final static Parameters DEFAULT = new Parameters(); public final static Parameters DEFAULT = new Parameters();

View File

@@ -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);
}

View File

@@ -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);
}
}
}

View File

@@ -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);
}
}