From a3d7f9463bac1e52007aa6b762905b9c555bddd7 Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Thu, 22 Sep 2022 12:11:02 +0300 Subject: [PATCH] chore: Finish duration input and add it to config gui --- .../animatedchunks/AnimatedChunks.java | 2 +- .../gui/AnimatedChunksScreen.java | 25 +++- .../animatedchunks/gui/ChunkPreview.java | 3 +- .../animatedchunks/gui/HorizontalSection.java | 2 +- .../topchetoeu/animatedchunks/gui/Input.java | 125 ++++++++++++------ .../animatedchunks/gui/NumberInput.java | 35 +++++ 6 files changed, 146 insertions(+), 46 deletions(-) create mode 100644 src/main/java/me/topchetoeu/animatedchunks/gui/NumberInput.java diff --git a/src/main/java/me/topchetoeu/animatedchunks/AnimatedChunks.java b/src/main/java/me/topchetoeu/animatedchunks/AnimatedChunks.java index cef0e8a..6517aaf 100644 --- a/src/main/java/me/topchetoeu/animatedchunks/AnimatedChunks.java +++ b/src/main/java/me/topchetoeu/animatedchunks/AnimatedChunks.java @@ -148,7 +148,7 @@ public final class AnimatedChunks implements ClientModInitializer, ModMenuApi { public ConfigScreenFactory getModConfigScreenFactory() { return (Screen parent) -> { var _this = getInstance(); - return new AnimatedChunksScreen(parent, _this.animation, _this.ease, _this.config); + return new AnimatedChunksScreen(parent, _this.animation, _this.ease, _this.config, _this.progress); }; } } diff --git a/src/main/java/me/topchetoeu/animatedchunks/gui/AnimatedChunksScreen.java b/src/main/java/me/topchetoeu/animatedchunks/gui/AnimatedChunksScreen.java index f0d352f..c432f82 100644 --- a/src/main/java/me/topchetoeu/animatedchunks/gui/AnimatedChunksScreen.java +++ b/src/main/java/me/topchetoeu/animatedchunks/gui/AnimatedChunksScreen.java @@ -10,6 +10,7 @@ import net.minecraft.util.math.ColorHelper.Argb; import me.topchetoeu.animatedchunks.ConfigManager; import me.topchetoeu.animatedchunks.Manager; import me.topchetoeu.animatedchunks.animation.Animation; +import me.topchetoeu.animatedchunks.animation.ProgressManager; import me.topchetoeu.animatedchunks.easing.Ease; import me.topchetoeu.animatedchunks.gui.Section.OrderType; import net.minecraft.client.MinecraftClient; @@ -20,6 +21,7 @@ public class AnimatedChunksScreen extends Screen { private final Manager animation; private final Manager ease; private final ConfigManager config; + private final ProgressManager progress; public static void playClick() { MinecraftClient.getInstance().getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0f)); @@ -46,8 +48,6 @@ public class AnimatedChunksScreen extends Screen { mainSection.order = OrderType.Justified; mainSection.children.addSelectableChild(selectionsSection()); mainSection.children.addSelectableChild(previewSection()); - mainSection.children.addSelectableChild(new Input(0, 0, null)); - // setZOffset(parent.getZOffset() + 1); } @Override @@ -97,17 +97,36 @@ public class AnimatedChunksScreen extends Screen { return res; } + private Section durationSection() { + var res = new HorizontalSection(); + res.setTargetWidth(width / 2); + var input = new NumberInput(res, 5, 5, (sender, val) -> { + if (val <= 0) sender.invalid = true; + else { + progress.setDuration(val); + sender.invalid = false; + } + }); + input.width = (int)res.getTargetWidth(); + res.x = res.y = 5; + res.title = Text.of("Duration:"); + res.children.addSelectableChild(input); + return res; + } private Section selectionsSection() { var res = new HorizontalSection(); res.x = res.y = 5; res.title = Text.of("Animation config:"); res.children.addSelectableChild(selectionSection(animation, "Animation")); res.children.addSelectableChild(selectionSection(ease, "Ease")); + res.children.addSelectableChild(durationSection()); return res; } - public AnimatedChunksScreen(Screen parent, Manager animation, Manager ease, ConfigManager config) { + + public AnimatedChunksScreen(Screen parent, Manager animation, Manager ease, ConfigManager config, ProgressManager progress) { super(Text.of("Animated Chunks Config")); config.reload(); + this.progress = progress; this.animation = animation; this.ease = ease; this.config = config; diff --git a/src/main/java/me/topchetoeu/animatedchunks/gui/ChunkPreview.java b/src/main/java/me/topchetoeu/animatedchunks/gui/ChunkPreview.java index 7731df1..c774718 100644 --- a/src/main/java/me/topchetoeu/animatedchunks/gui/ChunkPreview.java +++ b/src/main/java/me/topchetoeu/animatedchunks/gui/ChunkPreview.java @@ -89,7 +89,7 @@ public class ChunkPreview extends DrawableHelper implements Drawable, Element, S return z < 3; } - private static void myFill(MatrixStack matrices, float x1, float y1, float x2, float y2, float a, float r, float g, float b) { + public static void myFill(MatrixStack matrices, float x1, float y1, float x2, float y2, float a, float r, float g, float b) { if (x1 < x2) { float tmp = x1; x1 = x2; @@ -121,7 +121,6 @@ public class ChunkPreview extends DrawableHelper implements Drawable, Element, S RenderSystem.enableBlend(); RenderSystem.disableCull(); RenderSystem.disableTexture(); - RenderSystem.defaultBlendFunc(); RenderSystem.setShader(GameRenderer::getPositionColorShader); bufferBuilder.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_COLOR); bufferBuilder.vertex(p1.getX(), p1.getY(), p1.getZ()).color(r, g, b, a).next(); diff --git a/src/main/java/me/topchetoeu/animatedchunks/gui/HorizontalSection.java b/src/main/java/me/topchetoeu/animatedchunks/gui/HorizontalSection.java index d4af6ad..d05d1a0 100644 --- a/src/main/java/me/topchetoeu/animatedchunks/gui/HorizontalSection.java +++ b/src/main/java/me/topchetoeu/animatedchunks/gui/HorizontalSection.java @@ -16,7 +16,7 @@ public final class HorizontalSection extends Section { private float targetWidth; - public float getTargetWidth(float width) { + public float getTargetWidth() { return this.targetWidth; } public void setTargetWidth(float width) { diff --git a/src/main/java/me/topchetoeu/animatedchunks/gui/Input.java b/src/main/java/me/topchetoeu/animatedchunks/gui/Input.java index 1eec4e5..64f6ae7 100644 --- a/src/main/java/me/topchetoeu/animatedchunks/gui/Input.java +++ b/src/main/java/me/topchetoeu/animatedchunks/gui/Input.java @@ -2,42 +2,50 @@ package me.topchetoeu.animatedchunks.gui; import org.lwjgl.glfw.GLFW; +import com.mojang.blaze3d.platform.GlStateManager.DstFactor; +import com.mojang.blaze3d.platform.GlStateManager.SrcFactor; +import com.mojang.blaze3d.systems.RenderSystem; + import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.Drawable; import net.minecraft.client.gui.DrawableHelper; import net.minecraft.client.gui.Element; +import net.minecraft.client.gui.ParentElement; import net.minecraft.client.gui.Selectable; import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder; import net.minecraft.client.gui.screen.narration.NarrationPart; +import net.minecraft.client.render.GameRenderer; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.util.math.ColorHelper.Argb; public class Input extends DrawableHelper implements Drawable, Element, Selectable, BoundboxProvider { - public static interface ClickAction { - void onClick(); + public interface InputAction { + void onInput(Input sender, String val); } - public ClickAction clickAction; public final MinecraftClient client; private boolean clicked = false; - private boolean focused = false; private boolean hovered = false; - - public String content = ""; - public int paddingX = 5, paddingY = 2; - public int x, y, width = 100; - public float scale = 1; - public float min = 0, max = 10; - private float value; - - public float getValue() { - return value; + private boolean isFocused() { + return parent.getFocused() == this; } - public Input setValue(float val) { - if (val < min) val = min; - if (val > max) val = max; + private int index = 0; - value = val; + private final ParentElement parent; + private String content = ""; + public int paddingX = 5, paddingY = 2; + private float time = 0; + public int x, y, width = 100; + public boolean invalid = false; + public InputAction action = null; + + public String getContent() { + return content; + } + public Input setContent(String val) { + if (index > val.length()) index = val.length(); + content = val; + action.onInput(this, val); return this; } @@ -52,11 +60,7 @@ public class Input extends DrawableHelper implements Drawable, Element, Selectab return width; } public float getHeight() { - return paddingY * 2 + client.textRenderer.fontHeight; - } - - public void click() { - if (clickAction != null) clickAction.onClick(); + return 1 + paddingY * 2 + client.textRenderer.fontHeight; } @Override @@ -66,14 +70,42 @@ public class Input extends DrawableHelper implements Drawable, Element, Selectab @Override public SelectionType getType() { - if (focused) return SelectionType.FOCUSED; + if (isFocused()) return SelectionType.FOCUSED; if (hovered) return SelectionType.HOVERED; return SelectionType.NONE; } @Override public boolean changeFocus(boolean lookForwards) { - focused = !focused; - return focused; + if (isFocused()) { + parent.setFocused(null); + return false; + } + else { + parent.setFocused(this); + return true; + } + } + + private void renderCursor(MatrixStack matrices, float delta) { + time += delta / 20; + + if ((int)(time * 2) % 2 != 0) return; + + // RenderSystem.enableBlend(); + RenderSystem.setShader(GameRenderer::getPositionColorShader); + RenderSystem.disableCull(); + RenderSystem.disableTexture(); + RenderSystem.blendFuncSeparate(SrcFactor.ONE_MINUS_DST_COLOR, DstFactor.ONE_MINUS_SRC_COLOR, SrcFactor.ONE, DstFactor.ZERO); + + float x1 = paddingX + client.textRenderer.getWidth(content.substring(0, index)) + 1; + float x2 = x1 + 1; + // if (index < content.length()) { + // x2 += client.textRenderer.getWidth("" + content.charAt(index)) - 2; + // } + float y1 = paddingY; + float y2 = y1 + client.textRenderer.fontHeight; + + ChunkPreview.myFill(matrices, x1, y1, x2, y2, 1, 1, 1, 1); } @Override @@ -91,6 +123,11 @@ public class Input extends DrawableHelper implements Drawable, Element, Selectab fill(matrices, 0, 0, (int)getWidth(), (int)getHeight(), Argb.getArgb(127, 255, 255, 255)); } + client.textRenderer.draw(matrices, content, paddingX + 1, paddingY + 1, white); + if (isFocused()) renderCursor(matrices, delta); + + if (invalid) white = 0xFFFF0000; + drawHorizontalLine(matrices, 0, (int)getWidth() - 1, 0, white); drawVerticalLine(matrices, 0, 0, (int)getHeight() - 1, white); drawVerticalLine(matrices, (int)getWidth() - 1, 0, (int)getHeight() - 1, white); @@ -99,8 +136,6 @@ public class Input extends DrawableHelper implements Drawable, Element, Selectab // if (focused) { // } - client.textRenderer.draw(matrices, content, paddingX + 1, paddingY + 1, white); - matrices.pop(); } @@ -113,21 +148,32 @@ public class Input extends DrawableHelper implements Drawable, Element, Selectab @Override public boolean charTyped(char chr, int modifiers) { - content += chr; + content = content.substring(0, index) + chr + content.substring(index); + action.onInput(this, content); + index++; + time = 0; return true; } @Override public boolean keyPressed(int keyCode, int scanCode, int modifiers) { - if (keyCode == GLFW.GLFW_KEY_BACKSPACE && content.length() > 0) content = content.substring(0, content.length() - 1); - return false; - } - @Override - public boolean keyReleased(int keyCode, int scanCode, int modifiers) { - if (keyCode == GLFW.GLFW_KEY_ENTER) { - clicked = false; - return true; + if (keyCode == GLFW.GLFW_KEY_BACKSPACE && index > 0) { + content = content.substring(0, index - 1) + content.substring(index); + action.onInput(this, content); + index--; } + if (keyCode == GLFW.GLFW_KEY_DELETE && index < content.length()) { + time = 0; + content = content.substring(0, index) + content.substring(index + 1); + action.onInput(this, content); + } + if (keyCode == GLFW.GLFW_KEY_RIGHT && index < content.length()) { + index++; + } + if (keyCode == GLFW.GLFW_KEY_LEFT && index > 0) { + index--; + } + time = 0; return false; } @Override @@ -151,8 +197,9 @@ public class Input extends DrawableHelper implements Drawable, Element, Selectab return false; } - public Input(int x, int y, ClickAction clickAction) { - this.clickAction = clickAction; + public Input(ParentElement parent, int x, int y, InputAction input) { + this.parent = parent; + this.action = input; this.client = MinecraftClient.getInstance(); this.x = x; this.y = y; diff --git a/src/main/java/me/topchetoeu/animatedchunks/gui/NumberInput.java b/src/main/java/me/topchetoeu/animatedchunks/gui/NumberInput.java new file mode 100644 index 0000000..92476b3 --- /dev/null +++ b/src/main/java/me/topchetoeu/animatedchunks/gui/NumberInput.java @@ -0,0 +1,35 @@ +package me.topchetoeu.animatedchunks.gui; + +import net.minecraft.client.gui.ParentElement; + +public class NumberInput extends Input { + public interface InputAction { + void input(NumberInput sender, float number); + } + private float value; + public InputAction action; + + public float getValue() { + return value; + } + public NumberInput setValue(float val) { + value = val; + action.input(this, val); + return this; + } + + public NumberInput(ParentElement parent, int x, int y, InputAction action) { + super(parent, x, y, null); + super.action = (sender, val) -> { + try { + invalid = false; + setValue(Float.parseFloat(val.trim())); + } + catch (NumberFormatException e) { + invalid = true; + } + }; + this.action = action; + setContent(""); + } +}