From 8f9aeb260817ed3a5956c67979b71ebf1a8e09ca Mon Sep 17 00:00:00 2001 From: Sebastiaan de Schaetzen Date: Fri, 15 May 2026 21:44:23 +0200 Subject: [PATCH] Enhance GraphicsContext: add RGB color setting method, adjust clipping logic, and update Menu to return Window --- .../seeseemelk/diceos/system/WindowService.java | 4 ++-- .../diceos/system/gfx/GraphicsContext.java | 10 +++++++++- .../diceos/system/toolkit/menu/Menu.java | 17 ++++++++++------- .../diceos/system/toolkit/menu/MenuPopup.java | 16 ---------------- .../diceos/system/gfx/GraphicsContextTest.java | 6 ++++++ 5 files changed, 27 insertions(+), 26 deletions(-) delete mode 100644 src/main/java/be/seeseemelk/diceos/system/toolkit/menu/MenuPopup.java diff --git a/src/main/java/be/seeseemelk/diceos/system/WindowService.java b/src/main/java/be/seeseemelk/diceos/system/WindowService.java index 3045901..2b7b9ee 100644 --- a/src/main/java/be/seeseemelk/diceos/system/WindowService.java +++ b/src/main/java/be/seeseemelk/diceos/system/WindowService.java @@ -69,13 +69,13 @@ public class WindowService implements OnStartup { // Render menubar items gc.save(); - gc.translate(5, 0); + gc.translate(8, 0); for (var submenu : menu.getItems()) { gc.save(); gc.clip(0, 0, submenu.getWidth(), submenu.getHeight()); submenu.paint(gc); gc.restore(); - gc.translate(submenu.getWidth() + 4, 0); + gc.translate(submenu.getWidth(), 0); } gc.restore(); } diff --git a/src/main/java/be/seeseemelk/diceos/system/gfx/GraphicsContext.java b/src/main/java/be/seeseemelk/diceos/system/gfx/GraphicsContext.java index e32d6dc..b9554eb 100644 --- a/src/main/java/be/seeseemelk/diceos/system/gfx/GraphicsContext.java +++ b/src/main/java/be/seeseemelk/diceos/system/gfx/GraphicsContext.java @@ -110,6 +110,14 @@ public class GraphicsContext { batch.setColor(r / 255f, g / 255f, b / 255f, 1f); } + /** + * Sets the color for subsequent drawing operations using an RGB integer. + * @param rgb The RGB color integer. + */ + public void setColour(int rgb) { + setColour((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF); + } + /** * Shifts the context to the right and down by some amount. * Negative values move towards the opposite direction. @@ -154,7 +162,7 @@ public class GraphicsContext { * @param height The height of the clipping area. */ public void clip(int x, int y, int width, int height) { - var scissors = new Rectangle(x, getHeight() - y - height, width - getXTranslation(), height - getYTranslation()); + var scissors = new Rectangle(x + getXTranslation(), getHeight() - (y + getYTranslation()) - height, width, height); ScissorStack.calculateScissors(camera, batch.getTransformMatrix(), scissors, scissors); batch.flush(); if (ScissorStack.pushScissors(scissors)) { diff --git a/src/main/java/be/seeseemelk/diceos/system/toolkit/menu/Menu.java b/src/main/java/be/seeseemelk/diceos/system/toolkit/menu/Menu.java index ab86f86..bfe75af 100644 --- a/src/main/java/be/seeseemelk/diceos/system/toolkit/menu/Menu.java +++ b/src/main/java/be/seeseemelk/diceos/system/toolkit/menu/Menu.java @@ -3,6 +3,7 @@ package be.seeseemelk.diceos.system.toolkit.menu; import be.seeseemelk.diceos.system.DiceOS; import be.seeseemelk.diceos.system.font.Font; import be.seeseemelk.diceos.system.gfx.GraphicsContext; +import be.seeseemelk.diceos.system.toolkit.Window; import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -14,6 +15,8 @@ import java.util.List; @Getter @Slf4j public class Menu implements MenuItem { + private final static int HORIZONTAL_PADDING = 2; + private final String title; private final List items = new ArrayList<>(); private final Font font = DiceOS.getResourceLoader().getDefaultFont(); @@ -30,7 +33,7 @@ public class Menu implements MenuItem { @Override public int getWidth() { - return font.getTextWidth(title); + return font.getTextWidth(title) + HORIZONTAL_PADDING * 2; } @Override @@ -43,23 +46,23 @@ public class Menu implements MenuItem { * * @param x The x-coordinate. * @param y The y-coordinate. - * @return The created MenuPopup. + * @return The created Window for the menu. */ - public MenuPopup show(int x, int y) { - return new MenuPopup(this, x, y); + public Window show(int x, int y) { + return new Window(x, y, getWidth(), getHeight(), Window.Style.NONE); } @Override public void paint(GraphicsContext gc) { if (DiceOS.getInputService().mouseInBounds(gc.getXTranslation(), gc.getYTranslation(), getWidth(), getHeight())) { log.info("Mouse in bounds"); - gc.setColour(0, 0, 255); + gc.setColour(0x9f7445); + gc.fillRect(-100, -100, 500, 500); } - gc.fillRect(-100, -100, 500, 500); gc.setColour(255, 255, 255); // Renders the menu as a simple menu button - gc.draw(title, 2, 5); + gc.draw(title, HORIZONTAL_PADDING, 5); } } diff --git a/src/main/java/be/seeseemelk/diceos/system/toolkit/menu/MenuPopup.java b/src/main/java/be/seeseemelk/diceos/system/toolkit/menu/MenuPopup.java deleted file mode 100644 index ab220d8..0000000 --- a/src/main/java/be/seeseemelk/diceos/system/toolkit/menu/MenuPopup.java +++ /dev/null @@ -1,16 +0,0 @@ -package be.seeseemelk.diceos.system.toolkit.menu; - -import be.seeseemelk.diceos.system.toolkit.Window; - -/** - * A visisble menu popup. - */ -public class MenuPopup { - private final Menu menu; - private final Window window; - - public MenuPopup(Menu menu, int x, int y) { - this.menu = menu; - this.window = new Window(x, y, menu.getWidth(), menu.getHeight(), Window.Style.NONE); - } -} diff --git a/src/test/java/be/seeseemelk/diceos/system/gfx/GraphicsContextTest.java b/src/test/java/be/seeseemelk/diceos/system/gfx/GraphicsContextTest.java index 4cdbd4b..c4dca1d 100644 --- a/src/test/java/be/seeseemelk/diceos/system/gfx/GraphicsContextTest.java +++ b/src/test/java/be/seeseemelk/diceos/system/gfx/GraphicsContextTest.java @@ -57,6 +57,12 @@ class GraphicsContextTest { verify(batch).setColor(1f, 0f, 0f, 1f); } + @Test + void testSetColourRGB() { + gc.setColour(0xFF00FF); + verify(batch).setColor(1f, 0f, 1f, 1f); + } + @Test void testTranslate() { gc.translate(10, 20);