diff options
author | Juuz <6596629+Juuxel@users.noreply.github.com> | 2023-05-27 13:35:57 +0300 |
---|---|---|
committer | Juuz <6596629+Juuxel@users.noreply.github.com> | 2023-05-27 13:35:57 +0300 |
commit | 25174bab0e004f10ba332f06186792c5bd69eebb (patch) | |
tree | b8e32b07c2e6526234861ec4396dcde3d03ff35b /src/main | |
parent | 5d702bcabc5d3973b3fb142f716a952a6eced94b (diff) | |
download | LibGui-25174bab0e004f10ba332f06186792c5bd69eebb.tar.gz LibGui-25174bab0e004f10ba332f06186792c5bd69eebb.tar.bz2 LibGui-25174bab0e004f10ba332f06186792c5bd69eebb.zip |
Add GUI- and widget-level dark mode toggles
Diffstat (limited to 'src/main')
15 files changed, 62 insertions, 29 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/GuiDescription.java b/src/main/java/io/github/cottonmc/cotton/gui/GuiDescription.java index 6435651..6a7b06e 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/GuiDescription.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/GuiDescription.java @@ -2,6 +2,7 @@ package io.github.cottonmc.cotton.gui; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; +import net.fabricmc.fabric.api.util.TriState; import net.minecraft.screen.PropertyDelegate; import io.github.cottonmc.cotton.gui.widget.WPanel; @@ -143,4 +144,19 @@ public interface GuiDescription { * @since 4.0.0 */ void setTitlePos(Vec2i titlePos); + + /** + * Checks whether dark mode is used for this GUI description. + * + * @return <ul> + * <li>{@link TriState#TRUE} to force dark mode + * <li>{@link TriState#FALSE} to force light mode + * <li>{@link TriState#DEFAULT} to use the {@linkplain io.github.cottonmc.cotton.gui.client.LibGui#isDarkMode() + * global dark mode setting} + * </ul> + * @since 7.1.0 + */ + default TriState isDarkMode() { + return TriState.DEFAULT; + } } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/SyncedGuiDescription.java b/src/main/java/io/github/cottonmc/cotton/gui/SyncedGuiDescription.java index 6e6a3cd..2abc643 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/SyncedGuiDescription.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/SyncedGuiDescription.java @@ -99,7 +99,7 @@ public class SyncedGuiDescription extends ScreenHandler implements GuiDescriptio } public int getTitleColor() { - return (world.isClient && LibGui.isDarkMode()) ? darkTitleColor : titleColor; + return (world.isClient && isDarkMode().orElse(LibGui.isDarkMode())) ? darkTitleColor : titleColor; } public SyncedGuiDescription setRootPanel(WPanel panel) { diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/BackgroundPainter.java b/src/main/java/io/github/cottonmc/cotton/gui/client/BackgroundPainter.java index 654442c..77b7268 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/BackgroundPainter.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/BackgroundPainter.java @@ -143,7 +143,7 @@ public interface BackgroundPainter { /** * Creates a background painter that uses either the {@code light} or the {@code dark} background painter - * depending on the current setting. + * depending on the {@linkplain WWidget#shouldRenderInDarkMode current setting}. * * @param light the light mode background painter * @param dark the dark mode background painter @@ -152,7 +152,7 @@ public interface BackgroundPainter { */ public static BackgroundPainter createLightDarkVariants(BackgroundPainter light, BackgroundPainter dark) { return (matrices, left, top, panel) -> { - if (LibGui.isDarkMode()) dark.paintBackground(matrices, left, top, panel); + if (panel.shouldRenderInDarkMode()) dark.paintBackground(matrices, left, top, panel); else light.paintBackground(matrices, left, top, panel); }; } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/LightweightGuiDescription.java b/src/main/java/io/github/cottonmc/cotton/gui/client/LightweightGuiDescription.java index 1c23da8..7a4714d 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/LightweightGuiDescription.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/LightweightGuiDescription.java @@ -35,7 +35,7 @@ public class LightweightGuiDescription implements GuiDescription { @Override public int getTitleColor() { - return (LibGui.isDarkMode()) ? darkmodeTitleColor : titleColor; + return isDarkMode().orElse(LibGui.isDarkMode()) ? darkmodeTitleColor : titleColor; } @Override diff --git a/src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/WKirbSprite.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/WKirbSprite.java index 9bed0d1..5d9473f 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/WKirbSprite.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/WKirbSprite.java @@ -5,7 +5,6 @@ import net.fabricmc.api.Environment; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.util.Identifier; -import io.github.cottonmc.cotton.gui.client.LibGui; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; import io.github.cottonmc.cotton.gui.impl.LibGuiCommon; import io.github.cottonmc.cotton.gui.widget.WWidget; @@ -31,7 +30,7 @@ public class WKirbSprite extends WWidget { private long lastFrame; public WKirbSprite() { - state = (LibGui.isDarkMode()) ? State.ASLEEP : State.AWAKE; + state = (shouldRenderInDarkMode()) ? State.ASLEEP : State.AWAKE; } public void schedule(int[] frames) { @@ -59,7 +58,7 @@ public class WKirbSprite extends WWidget { long now = System.nanoTime() / 1_000_000L; if (pendingFrames.isEmpty()) { - if (LibGui.isDarkMode()) { + if (shouldRenderInDarkMode()) { state = switch (state) { case AWAKE -> State.FALLING_ASLEEP; case FALLING_ASLEEP -> State.ASLEEP; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java index de212fb..d865452 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java @@ -12,7 +12,6 @@ import net.minecraft.sound.SoundEvents; import net.minecraft.text.Text; import net.minecraft.util.Identifier; -import io.github.cottonmc.cotton.gui.client.LibGui; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; import io.github.cottonmc.cotton.gui.impl.client.NarrationMessages; import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment; @@ -108,7 +107,7 @@ public class WButton extends WWidget { float buttonEndLeft = (200-(getWidth()/2)) * px; - Identifier texture = getTexture(); + Identifier texture = getTexture(this); ScreenDrawing.texturedRect(matrices, x, y, getWidth()/2, 20, texture, buttonLeft, buttonTop, buttonLeft+buttonWidth, buttonTop+buttonHeight, 0xFFFFFFFF); ScreenDrawing.texturedRect(matrices, x+(getWidth()/2), y, getWidth()/2, 20, texture, buttonEndLeft, buttonTop, 200*px, buttonTop+buttonHeight, 0xFFFFFFFF); @@ -269,7 +268,7 @@ public class WButton extends WWidget { } @Environment(EnvType.CLIENT) - static Identifier getTexture() { - return LibGui.isDarkMode() ? DARK_WIDGETS_LOCATION : ClickableWidget.WIDGETS_TEXTURE; + static Identifier getTexture(WWidget widget) { + return widget.shouldRenderInDarkMode() ? DARK_WIDGETS_LOCATION : ClickableWidget.WIDGETS_TEXTURE; } } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WDynamicLabel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WDynamicLabel.java index 6099022..0b81a59 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WDynamicLabel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WDynamicLabel.java @@ -4,7 +4,6 @@ import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.util.math.MatrixStack; -import io.github.cottonmc.cotton.gui.client.LibGui; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment; @@ -40,7 +39,7 @@ public class WDynamicLabel extends WWidget { @Override public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) { String tr = text.get(); - ScreenDrawing.drawString(matrices, tr, alignment, x, y, this.getWidth(), LibGui.isDarkMode() ? darkmodeColor : color); + ScreenDrawing.drawString(matrices, tr, alignment, x, y, this.getWidth(), shouldRenderInDarkMode() ? darkmodeColor : color); } @Override diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java index 752d919..f340717 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java @@ -11,7 +11,6 @@ import net.minecraft.client.util.math.MatrixStack; import net.minecraft.text.Style; import net.minecraft.text.Text; -import io.github.cottonmc.cotton.gui.client.LibGui; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; import io.github.cottonmc.cotton.gui.impl.client.LibGuiConfig; import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment; @@ -72,7 +71,7 @@ public class WLabel extends WWidget { case TOP -> 0; }; - ScreenDrawing.drawString(matrices, text.asOrderedText(), horizontalAlignment, x, y + yOffset, this.getWidth(), LibGui.isDarkMode() ? darkmodeColor : color); + ScreenDrawing.drawString(matrices, text.asOrderedText(), horizontalAlignment, x, y + yOffset, this.getWidth(), shouldRenderInDarkMode() ? darkmodeColor : color); Style hoveredTextStyle = getTextStyleAt(mouseX, mouseY); ScreenDrawing.drawTextHover(matrices, hoveredTextStyle, x + mouseX, y + mouseY); diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabeledSlider.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabeledSlider.java index c750036..59adde7 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabeledSlider.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabeledSlider.java @@ -210,7 +210,7 @@ public class WLabeledSlider extends WAbstractSlider { float buttonHeight = 20 * px; float buttonEndLeft = (200 - halfWidth) * px; - Identifier texture = WButton.getTexture(); + Identifier texture = WButton.getTexture(this); ScreenDrawing.texturedRect(matrices, x, y, halfWidth, 20, texture, buttonLeft, buttonTop, buttonLeft + buttonWidth, buttonTop + buttonHeight, 0xFFFFFFFF); ScreenDrawing.texturedRect(matrices, x + halfWidth, y, halfWidth, 20, texture, buttonEndLeft, buttonTop, 200 * px, buttonTop + buttonHeight, 0xFFFFFFFF); } 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 4e2ddb4..aab0db8 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 @@ -6,7 +6,6 @@ import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder; import net.minecraft.client.gui.screen.narration.NarrationPart; import net.minecraft.client.util.math.MatrixStack; -import io.github.cottonmc.cotton.gui.client.LibGui; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; import io.github.cottonmc.cotton.gui.impl.client.NarrationMessages; import io.github.cottonmc.cotton.gui.widget.data.Axis; @@ -42,7 +41,9 @@ public class WScrollBar extends WWidget { @Environment(EnvType.CLIENT) @Override public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) { - if (LibGui.isDarkMode()) { + boolean darkMode = shouldRenderInDarkMode(); + + if (darkMode) { ScreenDrawing.drawBeveledPanel(matrices, x, y, width, height, 0xFF_212121, 0xFF_2F2F2F, 0xFF_5D5D5D); } else { ScreenDrawing.drawBeveledPanel(matrices, x, y, width, height, 0xFF_373737, 0xFF_8B8B8B, 0xFF_FFFFFF); @@ -53,7 +54,7 @@ public class WScrollBar extends WWidget { int top, middle, bottom; if (sliding) { - if (LibGui.isDarkMode()) { + if (darkMode) { top = 0xFF_6C6C6C; middle = 0xFF_2F2F2F; bottom = 0xFF_212121; @@ -63,7 +64,7 @@ public class WScrollBar extends WWidget { bottom = 0xFF_555555; } } else if (isWithinBounds(mouseX, mouseY)) { - if (LibGui.isDarkMode()) { + if (darkMode) { top = 0xFF_5F6A9D; middle = 0xFF_323F6E; bottom = 0xFF_0B204A; @@ -73,7 +74,7 @@ public class WScrollBar extends WWidget { bottom = 0xFF_343E75; } } else { - if (LibGui.isDarkMode()) { + if (darkMode) { top = 0xFF_6C6C6C; middle = 0xFF_414141; bottom = 0xFF_212121; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java index 4765199..f0ad49d 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java @@ -6,7 +6,6 @@ import net.minecraft.client.util.math.MatrixStack; import net.minecraft.util.Identifier; import io.github.cottonmc.cotton.gui.client.BackgroundPainter; -import io.github.cottonmc.cotton.gui.client.LibGui; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; import io.github.cottonmc.cotton.gui.impl.LibGuiCommon; import io.github.cottonmc.cotton.gui.widget.data.Axis; @@ -58,7 +57,7 @@ public class WSlider extends WAbstractSlider { int thumbX, thumbY; // thumbXOffset: thumb texture x offset in pixels int thumbXOffset; - Identifier texture = LibGui.isDarkMode() ? DARK_TEXTURE : LIGHT_TEXTURE; + Identifier texture = shouldRenderInDarkMode() ? DARK_TEXTURE : LIGHT_TEXTURE; if (axis == Axis.VERTICAL) { int trackX = x + width / 2 - TRACK_WIDTH / 2; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WTabPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WTabPanel.java index 1104929..729c6dd 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WTabPanel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WTabPanel.java @@ -13,7 +13,6 @@ import net.minecraft.text.Text; import net.minecraft.util.Identifier; import io.github.cottonmc.cotton.gui.client.BackgroundPainter; -import io.github.cottonmc.cotton.gui.client.LibGui; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; import io.github.cottonmc.cotton.gui.impl.LibGuiCommon; import io.github.cottonmc.cotton.gui.impl.client.NarrationMessages; @@ -404,7 +403,7 @@ public class WTabPanel extends WPanel { HorizontalAlignment align = (icon != null) ? HorizontalAlignment.LEFT : HorizontalAlignment.CENTER; int color; - if (LibGui.isDarkMode()) { + if (shouldRenderInDarkMode()) { color = WLabel.DEFAULT_DARKMODE_TEXT_COLOR; } else { color = selected ? WLabel.DEFAULT_TEXT_COLOR : 0xEEEEEE; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java index 8a4f03e..a38ce74 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java @@ -11,7 +11,6 @@ import net.minecraft.text.OrderedText; import net.minecraft.text.Style; import net.minecraft.text.Text; -import io.github.cottonmc.cotton.gui.client.LibGui; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment; import io.github.cottonmc.cotton.gui.widget.data.InputResult; @@ -102,7 +101,7 @@ public class WText extends WWidget { for (int i = 0; i < wrappedLines.size(); i++) { OrderedText line = wrappedLines.get(i); - int c = LibGui.isDarkMode() ? darkmodeColor : color; + int c = shouldRenderInDarkMode() ? darkmodeColor : color; ScreenDrawing.drawString(matrices, line, horizontalAlignment, x, y + yOffset + i * font.fontHeight, width, c); } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java index 47f013e..3ad8cd6 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java @@ -11,7 +11,6 @@ import net.minecraft.sound.SoundEvents; import net.minecraft.text.Text; import net.minecraft.util.Identifier; -import io.github.cottonmc.cotton.gui.client.LibGui; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; import io.github.cottonmc.cotton.gui.impl.LibGuiCommon; import io.github.cottonmc.cotton.gui.impl.client.NarrationMessages; @@ -112,7 +111,7 @@ public class WToggleButton extends WWidget { } if (label!=null) { - ScreenDrawing.drawString(matrices, label.asOrderedText(), x + 22, y+6, LibGui.isDarkMode() ? darkmodeColor : color); + ScreenDrawing.drawString(matrices, label.asOrderedText(), x + 22, y+6, shouldRenderInDarkMode() ? darkmodeColor : color); } } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java index a240cd6..f008310 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java @@ -8,6 +8,7 @@ import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder; import net.minecraft.client.util.math.MatrixStack; import io.github.cottonmc.cotton.gui.GuiDescription; +import io.github.cottonmc.cotton.gui.client.LibGui; import io.github.cottonmc.cotton.gui.impl.VisualLogger; import io.github.cottonmc.cotton.gui.widget.data.InputResult; import io.github.cottonmc.cotton.gui.widget.data.ObservableProperty; @@ -569,4 +570,27 @@ public class WWidget { public static boolean isActivationKey(int ch) { return ch == GLFW.GLFW_KEY_ENTER || ch == GLFW.GLFW_KEY_KP_ENTER || ch == GLFW.GLFW_KEY_SPACE; } + + /** + * Checks if this widget should be rendered in dark mode. + * + * <p>If the widget has a host that {@linkplain GuiDescription#isDarkMode() forces dark mode}, + * the forced value is used. Otherwise, this method returns {@link LibGui#isDarkMode()}. + * + * <p>{@linkplain #paint Painting} should respect this value for general-purpose widgets + * intended for use in multiple different GUIs. + * + * @return {@code true} if this widget should be rendered in dark mode, {@code false} otherwise + * @since 7.1.0 + */ + @Environment(EnvType.CLIENT) + public boolean shouldRenderInDarkMode() { + var globalDarkMode = LibGui.isDarkMode(); + + if (host != null) { + return host.isDarkMode().orElse(globalDarkMode); + } + + return globalDarkMode; + } } |