From a71f6254e9287b9949c2297e2bdb04084dca08f5 Mon Sep 17 00:00:00 2001 From: rugmj Date: Tue, 2 Jan 2024 13:08:25 +0000 Subject: [PATCH] fixes some ui stuff --- .../macros/gui/screen/MacroEditScreen.java | 23 ++-- .../gui/widget/commandlist/CommandEntry.java | 118 ----------------- .../commandlist/CommandEntryPlaceHolder.java | 10 +- .../widget/commandlist/CommandListWidget.java | 123 +++++++++++++++++- 4 files changed, 131 insertions(+), 143 deletions(-) delete mode 100644 src/main/java/tools/redstone/redstonetools/macros/gui/widget/commandlist/CommandEntry.java diff --git a/src/main/java/tools/redstone/redstonetools/macros/gui/screen/MacroEditScreen.java b/src/main/java/tools/redstone/redstonetools/macros/gui/screen/MacroEditScreen.java index 6918d673..0cdbb8cb 100644 --- a/src/main/java/tools/redstone/redstonetools/macros/gui/screen/MacroEditScreen.java +++ b/src/main/java/tools/redstone/redstonetools/macros/gui/screen/MacroEditScreen.java @@ -1,13 +1,11 @@ package tools.redstone.redstonetools.macros.gui.screen; +import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.screen.ConfirmScreen; -import net.minecraft.client.gui.widget.ClickableWidget; -import net.minecraft.util.math.MathHelper; import tools.redstone.redstonetools.macros.Macro; import tools.redstone.redstonetools.macros.MacroManager; import tools.redstone.redstonetools.macros.actions.Action; import tools.redstone.redstonetools.macros.actions.CommandAction; -import tools.redstone.redstonetools.macros.gui.widget.commandlist.CommandEntry; import tools.redstone.redstonetools.macros.gui.widget.commandlist.CommandListWidget; import tools.redstone.redstonetools.macros.gui.widget.macrolist.MacroListWidget; import net.minecraft.client.MinecraftClient; @@ -19,7 +17,6 @@ import net.minecraft.client.util.InputUtil; import net.minecraft.client.util.InputUtil.Key; import net.minecraft.client.util.InputUtil.Type; -import net.minecraft.client.util.math.MatrixStack; import net.minecraft.screen.ScreenTexts; import net.minecraft.text.Text; import net.minecraft.util.Formatting; @@ -102,7 +99,7 @@ public void init() { this.addDrawableChild(keyBindButton); int widgetWidth = 339; - List entries = null; + List entries = null; double scrollAmount = 0; if (commandList != null) { entries = commandList.children(); @@ -115,7 +112,7 @@ public void init() { if (entries != null) { commandList.children().clear(); - for (CommandEntry entry : entries) { + for (CommandListWidget.CommandEntry entry : entries) { entry.setOwner(commandList); commandList.children().add(entry); } @@ -136,20 +133,20 @@ private boolean canClickDone() { } @Override - public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) { + public void render(DrawContext context, int mouseX, int mouseY, float delta) { if (overlapped) { mouseX = -1; mouseY = -1; } this.renderBackgroundTexture(0); - commandList.render(matrices, mouseX, mouseY, delta); - super.render(matrices, mouseX, mouseY, delta); + commandList.render(context, mouseX, mouseY, delta); + super.render(context, mouseX, mouseY, delta); - drawCenteredTextWithShadow(matrices, this.textRenderer, this.title, this.width / 2, 8, 16777215); + drawCenteredTextWithShadow(context, this.textRenderer, this.title, this.width / 2, 8, 16777215); - drawCenteredTextWithShadow(matrices, this.textRenderer, "Key Bind", width / 2 - (99 - textRenderer.getWidth("Key Bind") / 2), 55 + textRenderer.fontHeight / 2, 16777215); - nameField.render(matrices, mouseX, mouseY, delta); + drawCenteredTextWithShadow(context, this.textRenderer, "Key Bind", width / 2 - (99 - textRenderer.getWidth("Key Bind") / 2), 55 + textRenderer.fontHeight / 2, 16777215); + nameField.render(context, mouseX, mouseY, delta); if (nameField.getText().isEmpty() && !nameField.isFocused()) { nameField.setSuggestion("Name"); @@ -244,7 +241,7 @@ private boolean updateKeybinding(Key key) { detectingKeycodeKey = false; Text text = key.getLocalizedText(); if (key == InputUtil.UNKNOWN_KEY) text = Text.of(""); - if ( KeyBindingUtils.isKeyAlreadyBound(key) ) { text = new LiteralText(text.getString()).formatted(Formatting.RED); } + if ( KeyBindingUtils.isKeyAlreadyBound(key) ) { text = Text.literal(text.getString()).formatted(Formatting.RED); } keyBindButton.setMessage(text); macro.setKey(key); diff --git a/src/main/java/tools/redstone/redstonetools/macros/gui/widget/commandlist/CommandEntry.java b/src/main/java/tools/redstone/redstonetools/macros/gui/widget/commandlist/CommandEntry.java deleted file mode 100644 index 5258f904..00000000 --- a/src/main/java/tools/redstone/redstonetools/macros/gui/widget/commandlist/CommandEntry.java +++ /dev/null @@ -1,118 +0,0 @@ -package tools.redstone.redstonetools.macros.gui.widget.commandlist; - -import net.minecraft.client.gui.DrawContext; -import tools.redstone.redstonetools.macros.gui.MacroCommandSuggestor; -import tools.redstone.redstonetools.macros.gui.widget.IconButtonWidget; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.widget.ButtonWidget; -import net.minecraft.client.gui.widget.EntryListWidget; -import net.minecraft.client.gui.widget.TextFieldWidget; -import net.minecraft.client.util.math.MatrixStack; -import net.minecraft.text.Text; - -public class CommandEntry extends EntryListWidget.Entry { - - - protected CommandListWidget owner; - - public final TextFieldWidget command; - protected final ButtonWidget deleteButton; - - - public CommandEntry(MinecraftClient client, CommandListWidget owner, String text) { - this.owner = owner; - - command = new TextFieldWidget(client.textRenderer, 0, 0, 300, 20, Text.of("")); - command.setMaxLength(255); - command.setText(text); - - deleteButton = new IconButtonWidget(IconButtonWidget.CROSS_ICON,0, 0, 20, 20, Text.of(""), (button) -> { - this.owner.removeCommand(this); - }); - - MacroCommandSuggestor commandMacroCommandSuggestor = new MacroCommandSuggestor(client, owner.getParent(), command,client.textRenderer,true,false, 0,0,0); - commandMacroCommandSuggestor.setWindowActive(false); - commandMacroCommandSuggestor.refresh(); - commandMacroCommandSuggestor.close(); - } - - - @Override - public void render(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) { - command.x = owner.getParent().width/2-owner.getWidth()/2+5; - command.y = y; - command.render(matrices,mouseX,mouseY,tickDelta); - - - deleteButton.x = command.x + command.getWidth()+5; - deleteButton.y = y; - deleteButton.render(matrices,mouseX,mouseY,tickDelta); - - if (edit) { - edit = false; - owner.getParent().editCommandField(command); - } - } - - public void tick() { - command.tick(); - } - private boolean edit = false; - - public void setFocused(boolean focused){ - command.setTextFieldFocused(focused); - if (focused){ - owner.centerScrollOn(this); - edit = true; - } - owner.focusOn(this); - } - - @Override - public void render(DrawContext context, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) { - return; - } - - protected String getText() { - return command.getText(); - } - - public void setOwner(CommandListWidget owner) { - this.owner = owner; - } - - @Override - public boolean mouseClicked(double mouseX, double mouseY, int button) { - if (command.mouseClicked(mouseX,mouseY,button)) { - owner.centerScrollOn(this); - edit = true; - return true; - } - deleteButton.mouseClicked(mouseX,mouseY,button); - - return super.mouseClicked(mouseX, mouseY, button); - } - - @Override - public boolean charTyped(char chr, int modifiers) { - if (command.isFocused()) return command.charTyped(chr,modifiers); - - return super.charTyped(chr, modifiers); - } - - @Override - public boolean keyPressed(int keyCode, int scanCode, int modifiers) { - if (command.isFocused()) { - return command.keyPressed(keyCode, scanCode, modifiers); - } - - return super.keyPressed(keyCode, scanCode, modifiers); - } - - @Override - public boolean keyReleased(int keyCode, int scanCode, int modifiers) { - if (command.isFocused()) return command.keyReleased(keyCode, scanCode, modifiers); - - return super.keyReleased(keyCode, scanCode, modifiers); - } -} diff --git a/src/main/java/tools/redstone/redstonetools/macros/gui/widget/commandlist/CommandEntryPlaceHolder.java b/src/main/java/tools/redstone/redstonetools/macros/gui/widget/commandlist/CommandEntryPlaceHolder.java index ac8103de..cb300a91 100644 --- a/src/main/java/tools/redstone/redstonetools/macros/gui/widget/commandlist/CommandEntryPlaceHolder.java +++ b/src/main/java/tools/redstone/redstonetools/macros/gui/widget/commandlist/CommandEntryPlaceHolder.java @@ -1,23 +1,23 @@ package tools.redstone.redstonetools.macros.gui.widget.commandlist; import net.minecraft.client.MinecraftClient; -import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.client.gui.DrawContext; +import tools.redstone.redstonetools.macros.gui.widget.commandlist.CommandListWidget.CommandEntry; -public class CommandEntryPlaceHolder extends CommandEntry{ +public class CommandEntryPlaceHolder extends CommandEntry { public CommandEntryPlaceHolder(MinecraftClient client, CommandListWidget owner, String text) { super(client, owner, text); super.deleteButton.visible = false; } - @Override - public void render(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) { + public void render(DrawContext context, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) { command.setSuggestion("Add new command"); if (!super.command.getText().isEmpty()) { super.owner.addCommandFromPlaceHolder(command.getText(),this); command.setText(""); } - super.render(matrices, index, y, x, entryWidth, entryHeight, mouseX, mouseY, hovered, tickDelta); + super.render(context, index, y, x, entryWidth, entryHeight, mouseX, mouseY, hovered, tickDelta); } } diff --git a/src/main/java/tools/redstone/redstonetools/macros/gui/widget/commandlist/CommandListWidget.java b/src/main/java/tools/redstone/redstonetools/macros/gui/widget/commandlist/CommandListWidget.java index a62653b3..e855aa4f 100644 --- a/src/main/java/tools/redstone/redstonetools/macros/gui/widget/commandlist/CommandListWidget.java +++ b/src/main/java/tools/redstone/redstonetools/macros/gui/widget/commandlist/CommandListWidget.java @@ -1,19 +1,25 @@ package tools.redstone.redstonetools.macros.gui.widget.commandlist; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.client.gui.widget.TextFieldWidget; +import net.minecraft.text.Text; +import tools.redstone.redstonetools.macros.gui.MacroCommandSuggestor; import tools.redstone.redstonetools.macros.gui.screen.MacroEditScreen; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder; import net.minecraft.client.gui.widget.EntryListWidget; +import tools.redstone.redstonetools.macros.gui.widget.IconButtonWidget; import java.util.ArrayList; import java.util.List; -public class CommandListWidget extends EntryListWidget { +public class CommandListWidget extends EntryListWidget { private final MacroEditScreen parent; - public CommandListWidget(MinecraftClient client, MacroEditScreen parent, int width, int height, int top, int bottom, int itemHeight) { - super(client, width, height, top, bottom, itemHeight); + public CommandListWidget(MinecraftClient client, MacroEditScreen parent, int width, int height, int y, int itemHeight) { + super(client, width, height, y, itemHeight); this.parent = parent; addEntry(new CommandEntryPlaceHolder(client,this,"")); } @@ -27,7 +33,7 @@ public void tick() { public CommandEntry addCommand(String command) { - CommandEntry entry = new CommandEntry(client,this,command); + CommandEntry entry = new CommandEntry(client, this, command); List entries = children(); entries.add(entries.size()-1,entry); @@ -39,8 +45,8 @@ protected void addCommandFromPlaceHolder(String command,CommandEntryPlaceHolder CommandEntry entry = addCommand(command); placeHolder.setFocused(false); - entry.command.x = placeHolder.command.x; - entry.command.y = placeHolder.command.y; + entry.command.setX(placeHolder.command.getX()); + entry.command.setY(placeHolder.command.getY()); entry.setFocused(true); } @@ -123,5 +129,108 @@ public int getWidth() { } @Override - public void appendNarrations(NarrationMessageBuilder builder) {} + protected void appendClickableNarrations(NarrationMessageBuilder builder) { + + } + + public static class CommandEntry extends EntryListWidget.Entry { + + protected CommandListWidget owner; + + public final TextFieldWidget command; + protected final ButtonWidget deleteButton; + + + public CommandEntry(MinecraftClient client, CommandListWidget owner, String text) { + this.owner = owner; + + command = new TextFieldWidget(client.textRenderer, 0, 0, 300, 20, Text.of("")); + command.setMaxLength(255); + command.setText(text); + + deleteButton = new IconButtonWidget(IconButtonWidget.CROSS_ICON,0, 0, 20, 20, Text.of(""), (button) -> { + this.owner.removeCommand(this); + }); + + MacroCommandSuggestor commandMacroCommandSuggestor = new MacroCommandSuggestor(client, owner.getParent(), command,client.textRenderer,true,false, 0,0,0); + commandMacroCommandSuggestor.setWindowActive(false); + commandMacroCommandSuggestor.refresh(); + commandMacroCommandSuggestor.close(); + } + + + @Override + public void render(DrawContext context, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) { + command.setX(owner.getParent().width / 2 - owner.getWidth() / 2 + 5); + command.setY(y); + command.render(context,mouseX,mouseY,tickDelta); + + + deleteButton.setX(command.getX() + command.getWidth() + 5); + deleteButton.setY(y); + deleteButton.render(context,mouseX,mouseY,tickDelta); + + if (edit) { + edit = false; + owner.getParent().editCommandField(command); + } + } + + public void tick() { + command.tick(); + } + private boolean edit = false; + + public void setFocused(boolean focused){ + command.setFocused(focused); + if (focused){ + owner.centerScrollOn(this); + edit = true; + } + owner.focusOn(this); + } + + protected String getText() { + return command.getText(); + } + + public void setOwner(CommandListWidget owner) { + this.owner = owner; + } + + @Override + public boolean mouseClicked(double mouseX, double mouseY, int button) { + if (command.mouseClicked(mouseX,mouseY,button)) { + owner.centerScrollOn(this); + edit = true; + return true; + } + deleteButton.mouseClicked(mouseX,mouseY,button); + + return super.mouseClicked(mouseX, mouseY, button); + } + + @Override + public boolean charTyped(char chr, int modifiers) { + if (command.isFocused()) return command.charTyped(chr,modifiers); + + return super.charTyped(chr, modifiers); + } + + @Override + public boolean keyPressed(int keyCode, int scanCode, int modifiers) { + if (command.isFocused()) { + return command.keyPressed(keyCode, scanCode, modifiers); + } + + return super.keyPressed(keyCode, scanCode, modifiers); + } + + @Override + public boolean keyReleased(int keyCode, int scanCode, int modifiers) { + if (command.isFocused()) return command.keyReleased(keyCode, scanCode, modifiers); + + return super.keyReleased(keyCode, scanCode, modifiers); + } + } }