From cc01e089d361f56382eb43560aee952674211dd2 Mon Sep 17 00:00:00 2001 From: LopyMine <127966446+LopyMine@users.noreply.github.com> Date: Sat, 9 Sep 2023 18:52:08 +0500 Subject: Paint WScrollBar using textures (#212) --- .../cottonmc/cotton/gui/widget/WScrollBar.java | 89 +++++++++------------ .../textures/widget/scroll_bar/background_dark.png | Bin 0 -> 165 bytes .../widget/scroll_bar/background_light.png | Bin 0 -> 159 bytes .../libgui/textures/widget/scroll_bar/focus.png | Bin 0 -> 156 bytes .../textures/widget/scroll_bar/scroll_bar_dark.png | Bin 0 -> 164 bytes .../widget/scroll_bar/scroll_bar_hovered_dark.png | Bin 0 -> 164 bytes .../widget/scroll_bar/scroll_bar_hovered_light.png | Bin 0 -> 164 bytes .../widget/scroll_bar/scroll_bar_light.png | Bin 0 -> 162 bytes .../widget/scroll_bar/scroll_bar_pressed_dark.png | Bin 0 -> 164 bytes .../widget/scroll_bar/scroll_bar_pressed_light.png | Bin 0 -> 161 bytes .../cottonmc/test/client/LibGuiTestClient.java | 1 + .../cottonmc/test/client/ScrollBarTestGui.java | 44 ++++++++++ 12 files changed, 85 insertions(+), 49 deletions(-) create mode 100644 src/main/resources/assets/libgui/textures/widget/scroll_bar/background_dark.png create mode 100644 src/main/resources/assets/libgui/textures/widget/scroll_bar/background_light.png create mode 100644 src/main/resources/assets/libgui/textures/widget/scroll_bar/focus.png create mode 100644 src/main/resources/assets/libgui/textures/widget/scroll_bar/scroll_bar_dark.png create mode 100644 src/main/resources/assets/libgui/textures/widget/scroll_bar/scroll_bar_hovered_dark.png create mode 100644 src/main/resources/assets/libgui/textures/widget/scroll_bar/scroll_bar_hovered_light.png create mode 100644 src/main/resources/assets/libgui/textures/widget/scroll_bar/scroll_bar_light.png create mode 100644 src/main/resources/assets/libgui/textures/widget/scroll_bar/scroll_bar_pressed_dark.png create mode 100644 src/main/resources/assets/libgui/textures/widget/scroll_bar/scroll_bar_pressed_light.png create mode 100644 src/testMod/java/io/github/cottonmc/test/client/ScrollBarTestGui.java diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java index 0846d68..2b7bb0c 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java @@ -5,11 +5,17 @@ import net.fabricmc.api.Environment; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder; import net.minecraft.client.gui.screen.narration.NarrationPart; +import net.minecraft.util.Identifier; -import io.github.cottonmc.cotton.gui.client.ScreenDrawing; +import io.github.cottonmc.cotton.gui.client.BackgroundPainter; +import io.github.cottonmc.cotton.gui.impl.LibGuiCommon; import io.github.cottonmc.cotton.gui.impl.client.NarrationMessages; +import io.github.cottonmc.cotton.gui.impl.client.NinePatchTextureRendererImpl; import io.github.cottonmc.cotton.gui.widget.data.Axis; import io.github.cottonmc.cotton.gui.widget.data.InputResult; +import juuxel.libninepatch.NinePatch; + +import static io.github.cottonmc.cotton.gui.client.BackgroundPainter.createNinePatch; public class WScrollBar extends WWidget { private static final int SCROLLING_SPEED = 4; @@ -41,63 +47,40 @@ public class WScrollBar extends WWidget { @Environment(EnvType.CLIENT) @Override public void paint(DrawContext context, int x, int y, int mouseX, int mouseY) { + var matrices = context.getMatrices(); boolean darkMode = shouldRenderInDarkMode(); - if (darkMode) { - ScreenDrawing.drawBeveledPanel(context, x, y, width, height, 0xFF_212121, 0xFF_2F2F2F, 0xFF_5D5D5D); - } else { - ScreenDrawing.drawBeveledPanel(context, x, y, width, height, 0xFF_373737, 0xFF_8B8B8B, 0xFF_FFFFFF); - } - if (maxValue<=0) return; + Painters.BACKGROUND.paintBackground(context, x, y, this); + + NinePatch painter = (darkMode ? Painters.SCROLL_BAR_DARK : Painters.SCROLL_BAR); - // Handle colors - int top, middle, bottom; + if (maxValue <= 0) return; if (sliding) { - if (darkMode) { - top = 0xFF_6C6C6C; - middle = 0xFF_2F2F2F; - bottom = 0xFF_212121; - } else { - top = 0xFF_FFFFFF; - middle = 0xFF_8B8B8B; - bottom = 0xFF_555555; - } + painter = (darkMode ? Painters.SCROLL_BAR_PRESSED_DARK : Painters.SCROLL_BAR_PRESSED); } else if (isWithinBounds(mouseX, mouseY)) { - if (darkMode) { - top = 0xFF_5F6A9D; - middle = 0xFF_323F6E; - bottom = 0xFF_0B204A; - } else { - top = 0xFF_CFD0F7; - middle = 0xFF_8791C7; - bottom = 0xFF_343E75; - } - } else { - if (darkMode) { - top = 0xFF_6C6C6C; - middle = 0xFF_414141; - bottom = 0xFF_212121; - } else { - top = 0xFF_FFFFFF; - middle = 0xFF_C6C6C6; - bottom = 0xFF_555555; - } + painter = (darkMode ? Painters.SCROLL_BAR_HOVERED_DARK : Painters.SCROLL_BAR_HOVERED); } - if (axis==Axis.HORIZONTAL) { - ScreenDrawing.drawBeveledPanel(context, x+1+getHandlePosition(), y+1, getHandleSize(), height-2, top, middle, bottom); + matrices.push(); + + if (axis == Axis.HORIZONTAL) { + matrices.translate(x + 1 + getHandlePosition(), y + 1, 0); + painter.draw(NinePatchTextureRendererImpl.INSTANCE, context, getHandleSize(), height - 2); if (isFocused()) { - drawBeveledOutline(context, x+1+getHandlePosition(), y+1, getHandleSize(), height-2, 0xFF_FFFFA7, 0xFF_8C8F39); + Painters.FOCUS.draw(NinePatchTextureRendererImpl.INSTANCE, context, getHandleSize(), height - 2); } } else { - ScreenDrawing.drawBeveledPanel(context, x+1, y+1+getHandlePosition(), width-2, getHandleSize(), top, middle, bottom); + matrices.translate(x + 1, y + 1 + getHandlePosition(), 0); + painter.draw(NinePatchTextureRendererImpl.INSTANCE, context, width - 2, getHandleSize()); if (isFocused()) { - drawBeveledOutline(context, x+1, y+1+getHandlePosition(), width-2, getHandleSize(), 0xFF_FFFFA7, 0xFF_8C8F39); + Painters.FOCUS.draw(NinePatchTextureRendererImpl.INSTANCE, context, width - 2, getHandleSize()); } } + + matrices.pop(); } @Override @@ -110,13 +93,6 @@ public class WScrollBar extends WWidget { return true; } - private static void drawBeveledOutline(DrawContext context, int x, int y, int width, int height, int topleft, int bottomright) { - ScreenDrawing.coloredRect(context, x, y, width, 1, topleft); //Top shadow - ScreenDrawing.coloredRect(context, x, y + 1, 1, height - 1, topleft); //Left shadow - ScreenDrawing.coloredRect(context, x + width - 1, y + 1, 1, height - 1, bottomright); //Right hilight - ScreenDrawing.coloredRect(context, x + 1, y + height - 1, width - 1, 1, bottomright); //Bottom hilight - } - /** * Gets the on-axis size of the scrollbar handle in gui pixels */ @@ -279,4 +255,19 @@ public class WScrollBar extends WWidget { builder.put(NarrationPart.TITLE, NarrationMessages.SCROLL_BAR_TITLE); builder.put(NarrationPart.USAGE, NarrationMessages.SLIDER_USAGE); } + + @Environment(EnvType.CLIENT) + static final class Painters { + static final NinePatch SCROLL_BAR = NinePatch.builder(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/scroll_bar/scroll_bar_light.png")).cornerSize(4).cornerUv(0.25f).build(); + static final NinePatch SCROLL_BAR_DARK = NinePatch.builder(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/scroll_bar/scroll_bar_dark.png")).cornerSize(4).cornerUv(0.25f).build(); + static final NinePatch SCROLL_BAR_PRESSED = NinePatch.builder(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/scroll_bar/scroll_bar_pressed_light.png")).cornerSize(4).cornerUv(0.25f).build(); + static final NinePatch SCROLL_BAR_PRESSED_DARK = NinePatch.builder(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/scroll_bar/scroll_bar_pressed_dark.png")).cornerSize(4).cornerUv(0.25f).build(); + static final NinePatch SCROLL_BAR_HOVERED = NinePatch.builder(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/scroll_bar/scroll_bar_hovered_light.png")).cornerSize(4).cornerUv(0.25f).build(); + static final NinePatch SCROLL_BAR_HOVERED_DARK = NinePatch.builder(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/scroll_bar/scroll_bar_hovered_dark.png")).cornerSize(4).cornerUv(0.25f).build(); + static final BackgroundPainter BACKGROUND = BackgroundPainter.createLightDarkVariants( + createNinePatch(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/scroll_bar/background_light.png")), + createNinePatch(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/scroll_bar/background_dark.png")) + ); + static final NinePatch FOCUS = NinePatch.builder(new Identifier(LibGuiCommon.MOD_ID, "textures/widget/scroll_bar/focus.png")).cornerSize(4).cornerUv(0.25f).build(); + } } diff --git a/src/main/resources/assets/libgui/textures/widget/scroll_bar/background_dark.png b/src/main/resources/assets/libgui/textures/widget/scroll_bar/background_dark.png new file mode 100644 index 0000000..3df12e3 Binary files /dev/null and b/src/main/resources/assets/libgui/textures/widget/scroll_bar/background_dark.png differ diff --git a/src/main/resources/assets/libgui/textures/widget/scroll_bar/background_light.png b/src/main/resources/assets/libgui/textures/widget/scroll_bar/background_light.png new file mode 100644 index 0000000..38c6ef8 Binary files /dev/null and b/src/main/resources/assets/libgui/textures/widget/scroll_bar/background_light.png differ diff --git a/src/main/resources/assets/libgui/textures/widget/scroll_bar/focus.png b/src/main/resources/assets/libgui/textures/widget/scroll_bar/focus.png new file mode 100644 index 0000000..955aa7e Binary files /dev/null and b/src/main/resources/assets/libgui/textures/widget/scroll_bar/focus.png differ diff --git a/src/main/resources/assets/libgui/textures/widget/scroll_bar/scroll_bar_dark.png b/src/main/resources/assets/libgui/textures/widget/scroll_bar/scroll_bar_dark.png new file mode 100644 index 0000000..4c2aab0 Binary files /dev/null and b/src/main/resources/assets/libgui/textures/widget/scroll_bar/scroll_bar_dark.png differ diff --git a/src/main/resources/assets/libgui/textures/widget/scroll_bar/scroll_bar_hovered_dark.png b/src/main/resources/assets/libgui/textures/widget/scroll_bar/scroll_bar_hovered_dark.png new file mode 100644 index 0000000..472db90 Binary files /dev/null and b/src/main/resources/assets/libgui/textures/widget/scroll_bar/scroll_bar_hovered_dark.png differ diff --git a/src/main/resources/assets/libgui/textures/widget/scroll_bar/scroll_bar_hovered_light.png b/src/main/resources/assets/libgui/textures/widget/scroll_bar/scroll_bar_hovered_light.png new file mode 100644 index 0000000..b0695ed Binary files /dev/null and b/src/main/resources/assets/libgui/textures/widget/scroll_bar/scroll_bar_hovered_light.png differ diff --git a/src/main/resources/assets/libgui/textures/widget/scroll_bar/scroll_bar_light.png b/src/main/resources/assets/libgui/textures/widget/scroll_bar/scroll_bar_light.png new file mode 100644 index 0000000..6d802e7 Binary files /dev/null and b/src/main/resources/assets/libgui/textures/widget/scroll_bar/scroll_bar_light.png differ diff --git a/src/main/resources/assets/libgui/textures/widget/scroll_bar/scroll_bar_pressed_dark.png b/src/main/resources/assets/libgui/textures/widget/scroll_bar/scroll_bar_pressed_dark.png new file mode 100644 index 0000000..8835712 Binary files /dev/null and b/src/main/resources/assets/libgui/textures/widget/scroll_bar/scroll_bar_pressed_dark.png differ diff --git a/src/main/resources/assets/libgui/textures/widget/scroll_bar/scroll_bar_pressed_light.png b/src/main/resources/assets/libgui/textures/widget/scroll_bar/scroll_bar_pressed_light.png new file mode 100644 index 0000000..9df19b0 Binary files /dev/null and b/src/main/resources/assets/libgui/textures/widget/scroll_bar/scroll_bar_pressed_light.png differ diff --git a/src/testMod/java/io/github/cottonmc/test/client/LibGuiTestClient.java b/src/testMod/java/io/github/cottonmc/test/client/LibGuiTestClient.java index e15eb83..57c2790 100644 --- a/src/testMod/java/io/github/cottonmc/test/client/LibGuiTestClient.java +++ b/src/testMod/java/io/github/cottonmc/test/client/LibGuiTestClient.java @@ -53,6 +53,7 @@ public class LibGuiTestClient implements ClientModInitializer { .then(literal("config").executes(openScreen(client -> new ConfigGui(client.currentScreen)))) .then(literal("tab").executes(openScreen(client -> new TabTestGui()))) .then(literal("scrolling").executes(openScreen(client -> new ScrollingTestGui()))) + .then(literal("scrollbar").executes(openScreen(client -> new ScrollBarTestGui()))) .then(literal("insets").executes(openScreen(client -> new InsetsTestGui()))) .then(literal("textfield").executes(openScreen(client -> new TextFieldTestGui()))) .then(literal("paddings") diff --git a/src/testMod/java/io/github/cottonmc/test/client/ScrollBarTestGui.java b/src/testMod/java/io/github/cottonmc/test/client/ScrollBarTestGui.java new file mode 100644 index 0000000..efac9d5 --- /dev/null +++ b/src/testMod/java/io/github/cottonmc/test/client/ScrollBarTestGui.java @@ -0,0 +1,44 @@ +package io.github.cottonmc.test.client; + +import net.fabricmc.fabric.api.util.TriState; + +import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription; +import io.github.cottonmc.cotton.gui.widget.WPlainPanel; +import io.github.cottonmc.cotton.gui.widget.WScrollBar; +import io.github.cottonmc.cotton.gui.widget.WToggleButton; +import io.github.cottonmc.cotton.gui.widget.data.Axis; +import io.github.cottonmc.cotton.gui.widget.data.Insets; + +public class ScrollBarTestGui extends LightweightGuiDescription { + private boolean darkMode = false; + + public ScrollBarTestGui() { + WPlainPanel root = new WPlainPanel(); + setRootPanel(root); + root.setSize(256, 240); + root.setInsets(Insets.ROOT_PANEL); + + WScrollBar scrollBar1 = new WScrollBar(Axis.HORIZONTAL); + root.add(scrollBar1, 0, 0, 256, 16); + + WScrollBar scrollBar2 = new WScrollBar(Axis.HORIZONTAL); + root.add(scrollBar2, 0, 240 - scrollBar2.getHeight(), 256, 8); + + WScrollBar scrollBar3 = new WScrollBar(Axis.VERTICAL); + root.add(scrollBar3, 0, 18, 16, 202); + + WScrollBar scrollBar4 = new WScrollBar(Axis.VERTICAL); + root.add(scrollBar4, 248, 18, 8, 202); + + WToggleButton toggleButton = new WToggleButton(); + toggleButton.setOnToggle(on -> darkMode = on); + root.add(toggleButton, 128 - (toggleButton.getWidth() / 2), 120 - (toggleButton.getHeight() / 2)); + + root.validate(this); + } + + @Override + public TriState isDarkMode() { + return TriState.of(darkMode); + } +} -- cgit