From c8a64488be822122f5e36bf359927e5ae79180a0 Mon Sep 17 00:00:00 2001 From: Beomjun Kim Date: Mon, 18 Mar 2024 15:16:10 +0900 Subject: [PATCH 1/4] Add item, text component tooltips --- .../calcite/screen/CalciteCommandScreen.java | 101 ++++++++++---- .../calcite/screen/CalciteInputSuggestor.java | 10 +- .../widget/CalciteTextFieldWidget.java | 126 ++++++++++++------ 3 files changed, 171 insertions(+), 66 deletions(-) diff --git a/src/client/java/works/nuty/calcite/screen/CalciteCommandScreen.java b/src/client/java/works/nuty/calcite/screen/CalciteCommandScreen.java index aa6a560..6ce4de1 100644 --- a/src/client/java/works/nuty/calcite/screen/CalciteCommandScreen.java +++ b/src/client/java/works/nuty/calcite/screen/CalciteCommandScreen.java @@ -37,6 +37,8 @@ public class CalciteCommandScreen extends Screen { private final BlockPos blockPos; private boolean isInitialized = false; private CommandListWidget commandListWidget; + private ButtonWidget doneButton; + private ButtonWidget cancelButton; @Nullable private Runnable commandSuggestorRenderer; @@ -100,7 +102,7 @@ public List getConnectedCommandBlocks() { } @Nullable - private CommandWidget getFocusedCommandWidget() { + private AbstractCommandWidget getFocusedCommandWidget() { return this.commandListWidget.getFocused(); } @@ -113,6 +115,7 @@ protected void init() { for (CommandBlockBlockEntity blockEntity : connectedCommandBlocks) { this.commandListWidget.addCommandBlock(blockEntity); } + this.commandListWidget.addBlank(); this.commandListWidget.setFocused(this.commandListWidget.positionedWidgets.get(this.blockPos)); this.commandListWidget.scrollToFocused(); @@ -127,16 +130,12 @@ protected void init() { } } - this.addDrawableChild( - ButtonWidget.builder(ScreenTexts.DONE, button -> this.commitAndClose()) - .dimensions(this.width - 10 - 200 - 5, this.height - 25, 100, 20) - .build() - ); - this.addDrawableChild( - ButtonWidget.builder(ScreenTexts.CANCEL, button -> this.close()) - .dimensions(this.width - 10 - 100, this.height - 25, 100, 20) - .build() - ); + this.doneButton = ButtonWidget.builder(ScreenTexts.DONE, button -> this.commitAndClose()) + .dimensions(this.width - 10 - 200 - 5, this.height - 25, 100, 20) + .build(); + this.cancelButton = ButtonWidget.builder(ScreenTexts.CANCEL, button -> this.close()) + .dimensions(this.width - 10 - 100, this.height - 25, 100, 20) + .build(); this.isInitialized = true; } @@ -149,7 +148,7 @@ public void resize(MinecraftClient client, int width, int height) { @Override public boolean shouldCloseOnEsc() { - return this.commandListWidget.children().stream().noneMatch(CommandWidget::isModified); + return this.commandListWidget.children().stream().noneMatch(AbstractCommandWidget::isModified); } private void commitAndClose() { @@ -174,7 +173,7 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) { return true; } if (keyCode == GLFW.GLFW_KEY_ESCAPE) { - CommandWidget focused = this.commandListWidget.getFocused(); + AbstractCommandWidget focused = this.commandListWidget.getFocused(); if (focused != null) { this.commandListWidget.setFocused(null); return true; @@ -193,12 +192,20 @@ public boolean mouseScrolled(double mouseX, double mouseY, double horizontalAmou @Override public boolean mouseClicked(double mouseX, double mouseY, int button) { - for (CommandWidget widget : this.commandListWidget.children()) { - if (widget.modeButton.mouseClicked(mouseX, mouseY, button)) { - return true; - } - if (widget.autoActivateButton.mouseClicked(mouseX, mouseY, button)) { - return true; + if (this.doneButton.mouseClicked(mouseX, mouseY, button)) { + return true; + } + if (this.cancelButton.mouseClicked(mouseX, mouseY, button)) { + return true; + } + for (AbstractCommandWidget widget : this.commandListWidget.children()) { + if (widget instanceof CommandWidget cw) { + if (cw.modeButton.mouseClicked(mouseX, mouseY, button)) { + return true; + } + if (cw.autoActivateButton.mouseClicked(mouseX, mouseY, button)) { + return true; + } } } if (this.commandListWidget.mouseClicked(mouseX, mouseY, button)) { @@ -223,15 +230,49 @@ public void render(DrawContext context, int mouseX, int mouseY, float delta) { commandSuggestorRenderer.run(); commandSuggestorRenderer = null; } + + doneButton.render(context, mouseX, mouseY, delta); + cancelButton.render(context, mouseX, mouseY, delta); + } + + @Environment(EnvType.CLIENT) + public abstract static class AbstractCommandWidget extends ElementListWidget.Entry { + abstract public boolean isModified(); + } + + @Environment(EnvType.CLIENT) + public static class BlankCommandWidget extends AbstractCommandWidget { + public BlankCommandWidget() { + } + + @Override + public List selectableChildren() { + return List.of(); + } + + @Override + public List children() { + return List.of(); + } + + @Override + public void render(DrawContext context, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) { + + } + + public boolean isModified() { + return false; + } } @Environment(EnvType.CLIENT) - public class CommandListWidget extends ElementListWidget { + public class CommandListWidget extends ElementListWidget { private List indexedWidgets; private Map positionedWidgets; public CommandListWidget() { - super(CalciteCommandScreen.this.client, CalciteCommandScreen.this.width, CalciteCommandScreen.this.height - 34, 5, 24); + super(CalciteCommandScreen.this.client, CalciteCommandScreen.this.width, CalciteCommandScreen.this.height, 0, 24); + this.headerHeight = 5; this.indexedWidgets = new ArrayList<>(); this.positionedWidgets = new HashMap<>(); this.setRenderBackground(false); @@ -242,13 +283,15 @@ protected void apply(CommandListWidget clw) { this.positionedWidgets = clw.positionedWidgets; for (var child : clw.children()) { this.addEntry(child); - child.commandEdit.setWidth(CalciteCommandScreen.this.width - 70); + if (child instanceof CommandWidget cw) { + cw.commandEdit.setWidth(CalciteCommandScreen.this.width - 70); + } } } @Override protected int getScrollbarPositionX() { - return this.width - 10; + return this.width - 6; } @Override @@ -263,11 +306,15 @@ public void addCommandBlock(CommandBlockBlockEntity blockEntity) { this.addEntry(widget); } + public void addBlank() { + this.addEntry(new BlankCommandWidget()); + } + @Override public boolean mouseClicked(double mouseX, double mouseY, int button) { - CommandWidget widget = this.getFocused(); - if (widget != null) { - if (widget.commandSuggestor.mouseClicked(mouseX, mouseY, button)) { + AbstractCommandWidget widget = this.getFocused(); + if (widget instanceof CommandWidget cw) { + if (cw.commandSuggestor.mouseClicked(mouseX, mouseY, button)) { return true; } } @@ -296,7 +343,7 @@ public void scrollToFocused() { } @Environment(EnvType.CLIENT) - public class CommandWidget extends ElementListWidget.Entry { + public class CommandWidget extends AbstractCommandWidget { protected final CalciteInputSuggestor commandSuggestor; protected final List children; private final CommandBlockBlockEntity blockEntity; diff --git a/src/client/java/works/nuty/calcite/screen/CalciteInputSuggestor.java b/src/client/java/works/nuty/calcite/screen/CalciteInputSuggestor.java index f813975..be2680c 100644 --- a/src/client/java/works/nuty/calcite/screen/CalciteInputSuggestor.java +++ b/src/client/java/works/nuty/calcite/screen/CalciteInputSuggestor.java @@ -34,6 +34,8 @@ import net.minecraft.util.Formatting; import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.Vec2f; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.Nullable; import works.nuty.calcite.widget.CalciteTextFieldWidget; @@ -49,6 +51,7 @@ public class CalciteInputSuggestor { private static final Style ERROR_STYLE = Style.EMPTY.withColor(Formatting.RED); private static final Style INFO_STYLE = Style.EMPTY.withColor(Formatting.GRAY); private static final List