diff options
author | LopyMine <127966446+LopyMine@users.noreply.github.com> | 2023-09-09 18:52:08 +0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-09 16:52:08 +0300 |
commit | cc01e089d361f56382eb43560aee952674211dd2 (patch) | |
tree | d19318599d6467395bd576e0df0f4946e23e56c0 /src | |
parent | 54ad50bc3499a4b489801c15a540917f74878e74 (diff) | |
download | LibGui-cc01e089d361f56382eb43560aee952674211dd2.tar.gz LibGui-cc01e089d361f56382eb43560aee952674211dd2.tar.bz2 LibGui-cc01e089d361f56382eb43560aee952674211dd2.zip |
Paint WScrollBar using textures (#212)
Diffstat (limited to 'src')
12 files changed, 85 insertions, 49 deletions
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<Identifier> 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<Identifier> 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<Identifier> 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<Identifier> 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<Identifier> 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<Identifier> 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<Identifier> 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<Identifier> 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 Binary files differnew file mode 100644 index 0000000..3df12e3 --- /dev/null +++ b/src/main/resources/assets/libgui/textures/widget/scroll_bar/background_dark.png 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 Binary files differnew file mode 100644 index 0000000..38c6ef8 --- /dev/null +++ b/src/main/resources/assets/libgui/textures/widget/scroll_bar/background_light.png 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 Binary files differnew file mode 100644 index 0000000..955aa7e --- /dev/null +++ b/src/main/resources/assets/libgui/textures/widget/scroll_bar/focus.png 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 Binary files differnew file mode 100644 index 0000000..4c2aab0 --- /dev/null +++ b/src/main/resources/assets/libgui/textures/widget/scroll_bar/scroll_bar_dark.png 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 Binary files differnew file mode 100644 index 0000000..472db90 --- /dev/null +++ b/src/main/resources/assets/libgui/textures/widget/scroll_bar/scroll_bar_hovered_dark.png 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 Binary files differnew file mode 100644 index 0000000..b0695ed --- /dev/null +++ b/src/main/resources/assets/libgui/textures/widget/scroll_bar/scroll_bar_hovered_light.png 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 Binary files differnew file mode 100644 index 0000000..6d802e7 --- /dev/null +++ b/src/main/resources/assets/libgui/textures/widget/scroll_bar/scroll_bar_light.png 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 Binary files differnew file mode 100644 index 0000000..8835712 --- /dev/null +++ b/src/main/resources/assets/libgui/textures/widget/scroll_bar/scroll_bar_pressed_dark.png 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 Binary files differnew file mode 100644 index 0000000..9df19b0 --- /dev/null +++ b/src/main/resources/assets/libgui/textures/widget/scroll_bar/scroll_bar_pressed_light.png 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); + } +} |