diff options
author | Juuxel <6596629+Juuxel@users.noreply.github.com> | 2021-06-27 14:10:13 +0300 |
---|---|---|
committer | Juuxel <6596629+Juuxel@users.noreply.github.com> | 2021-06-27 14:10:13 +0300 |
commit | 0b62c15c6cbc3a3440002bd705e2e499fb29df66 (patch) | |
tree | 568a6ad37ebc3a1e4da665d43807ac23304d0e48 /src | |
parent | dc69df9c4e4787aa4d7025ac64b41290a52d79f9 (diff) | |
download | LibGui-0b62c15c6cbc3a3440002bd705e2e499fb29df66.tar.gz LibGui-0b62c15c6cbc3a3440002bd705e2e499fb29df66.tar.bz2 LibGui-0b62c15c6cbc3a3440002bd705e2e499fb29df66.zip |
Add a "visual logger" for showing error messages in a dev env
Diffstat (limited to 'src')
6 files changed, 93 insertions, 10 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/ValidatedSlot.java b/src/main/java/io/github/cottonmc/cotton/gui/ValidatedSlot.java index f51bcad..b7bdfcb 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/ValidatedSlot.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/ValidatedSlot.java @@ -7,15 +7,14 @@ import net.minecraft.screen.slot.Slot; import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; +import io.github.cottonmc.cotton.gui.impl.VisualLogger; import io.github.cottonmc.cotton.gui.widget.WItemSlot; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import java.util.Objects; import java.util.function.Predicate; public class ValidatedSlot extends Slot { - private static final Logger LOGGER = LogManager.getLogger(); + private static final VisualLogger LOGGER = new VisualLogger(ValidatedSlot.class); private final int slotNumber; // Original positions that will be restored when this slot is reshown private final int originalX, originalY; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonClientScreen.java b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonClientScreen.java index d6928ee..cb80707 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonClientScreen.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonClientScreen.java @@ -7,6 +7,7 @@ import net.minecraft.text.Style; import net.minecraft.text.Text; import io.github.cottonmc.cotton.gui.GuiDescription; +import io.github.cottonmc.cotton.gui.impl.VisualLogger; import io.github.cottonmc.cotton.gui.impl.client.CottonScreenImpl; import io.github.cottonmc.cotton.gui.impl.client.MouseInputHandler; import io.github.cottonmc.cotton.gui.widget.WPanel; @@ -65,6 +66,7 @@ public class CottonClientScreen extends Screen implements CottonScreenImpl { public void removed() { super.removed(); this.client.keyboard.setRepeatEvents(false); + VisualLogger.reset(); } @Nullable @@ -137,6 +139,8 @@ public class CottonClientScreen extends Screen implements CottonScreenImpl { if (hitChild!=null) hitChild.renderTooltip(matrices, left, top, mouseX-left, mouseY-top); } } + + VisualLogger.render(matrices); } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonInventoryScreen.java b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonInventoryScreen.java index 6341894..824cbc2 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonInventoryScreen.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonInventoryScreen.java @@ -9,6 +9,7 @@ import net.minecraft.text.Style; import net.minecraft.text.Text; import io.github.cottonmc.cotton.gui.SyncedGuiDescription; +import io.github.cottonmc.cotton.gui.impl.VisualLogger; import io.github.cottonmc.cotton.gui.impl.client.CottonScreenImpl; import io.github.cottonmc.cotton.gui.impl.client.MouseInputHandler; import io.github.cottonmc.cotton.gui.widget.WPanel; @@ -79,6 +80,7 @@ public class CottonInventoryScreen<T extends SyncedGuiDescription> extends Handl public void removed() { super.removed(); this.client.keyboard.setRepeatEvents(false); + VisualLogger.reset(); } @Nullable @@ -265,6 +267,7 @@ public class CottonInventoryScreen<T extends SyncedGuiDescription> extends Handl } drawMouseoverTooltip(matrices, mouseX, mouseY); //Draws the itemstack tooltips + VisualLogger.render(matrices); } @Override diff --git a/src/main/java/io/github/cottonmc/cotton/gui/impl/VisualLogger.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/VisualLogger.java new file mode 100644 index 0000000..3f17aa4 --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/VisualLogger.java @@ -0,0 +1,80 @@ +package io.github.cottonmc.cotton.gui.impl; + +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.fabricmc.loader.api.FabricLoader; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.text.LiteralText; +import net.minecraft.text.OrderedText; +import net.minecraft.text.Text; +import net.minecraft.util.Formatting; + +import io.github.cottonmc.cotton.gui.client.ScreenDrawing; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.message.ParameterizedMessage; + +import java.util.ArrayList; +import java.util.List; + +/** + * A "logger" that renders its messages on the screen in dev envs. + */ +public final class VisualLogger { + private static final List<Text> WARNINGS = new ArrayList<>(); + + private final Logger logger; + private final Class<?> clazz; + + public VisualLogger(Class<?> clazz) { + logger = LogManager.getLogger(clazz); + this.clazz = clazz; + } + + public void error(String message, Object... params) { + log(message, params, Level.ERROR, Formatting.RED); + } + + public void warn(String message, Object... params) { + log(message, params, Level.WARN, Formatting.GOLD); + } + + private void log(String message, Object[] params, Level level, Formatting formatting) { + logger.log(level, message, params); + + if (FabricLoader.getInstance().isDevelopmentEnvironment()) { + var text = new LiteralText(clazz.getSimpleName() + '/'); + text.append(new LiteralText(level.name()).formatted(formatting)); + text.append(new LiteralText(": " + ParameterizedMessage.format(message, params))); + + WARNINGS.add(text); + } + } + + @Environment(EnvType.CLIENT) + public static void render(MatrixStack matrices) { + var client = MinecraftClient.getInstance(); + var textRenderer = client.textRenderer; + int width = client.getWindow().getScaledWidth(); + List<OrderedText> lines = new ArrayList<>(); + + for (Text warning : WARNINGS) { + lines.addAll(textRenderer.wrapLines(warning, width)); + } + + int fontHeight = textRenderer.fontHeight; + int y = 0; + + for (var line : lines) { + ScreenDrawing.coloredRect(matrices, 2, 2 + y, textRenderer.getWidth(line), fontHeight, 0x88_000000); + ScreenDrawing.drawString(matrices, line, 2, 2 + y, 0xFF_FFFFFF); + y += fontHeight; + } + } + + public static void reset() { + WARNINGS.clear(); + } +} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java index f25a2f2..a99028c 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java @@ -12,9 +12,8 @@ import net.minecraft.screen.slot.SlotActionType; import io.github.cottonmc.cotton.gui.GuiDescription; import io.github.cottonmc.cotton.gui.ValidatedSlot; import io.github.cottonmc.cotton.gui.client.BackgroundPainter; +import io.github.cottonmc.cotton.gui.impl.VisualLogger; import io.github.cottonmc.cotton.gui.widget.icon.Icon; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.Nullable; import java.util.ArrayList; @@ -60,7 +59,7 @@ import java.util.function.Predicate; * </pre> */ public class WItemSlot extends WWidget { - private static final Logger LOGGER = LogManager.getLogger(); + private static final VisualLogger LOGGER = new VisualLogger(WItemSlot.class); private static final Predicate<ItemStack> DEFAULT_FILTER = stack -> true; private final List<ValidatedSlot> peers = new ArrayList<>(); @Nullable @@ -175,7 +174,6 @@ public class WItemSlot extends WWidget { this.icon = icon; if (icon != null && (slotsWide * slotsHigh) > 1) { - // TODO: Should these types of warnings be visible in the screen itself in a dev env? LOGGER.warn("Setting icon {} for item slot {} with more than 1 slot ({})", icon, this, slotsWide * slotsHigh); } 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 cd3fb68..5a43a0a 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,9 +8,8 @@ import net.minecraft.client.util.math.MatrixStack; import com.google.common.annotations.Beta; import io.github.cottonmc.cotton.gui.GuiDescription; +import io.github.cottonmc.cotton.gui.impl.VisualLogger; import io.github.cottonmc.cotton.gui.widget.data.InputResult; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.Nullable; import org.lwjgl.glfw.GLFW; @@ -18,7 +17,7 @@ import org.lwjgl.glfw.GLFW; * The base class for all widgets. */ public class WWidget { - private static final Logger LOGGER = LogManager.getLogger(); + private static final VisualLogger LOGGER = new VisualLogger(WWidget.class); /** * The containing panel of this widget. |