From 976e91c83711d12cc4532d5285814c5d78e8c69e Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Thu, 22 Sep 2022 10:55:35 +0300 Subject: [PATCH 1/5] fix: Offests in GUI now work correctly --- .../topchetoeu/animatedchunks/gui/Button.java | 7 ++---- .../animatedchunks/gui/ChunkPreview.java | 3 --- .../animatedchunks/gui/Section.java | 22 +++++++++---------- 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/src/main/java/me/topchetoeu/animatedchunks/gui/Button.java b/src/main/java/me/topchetoeu/animatedchunks/gui/Button.java index 69a33f6..6fb3187 100644 --- a/src/main/java/me/topchetoeu/animatedchunks/gui/Button.java +++ b/src/main/java/me/topchetoeu/animatedchunks/gui/Button.java @@ -83,7 +83,7 @@ public class Button extends DrawableHelper implements Drawable, Element, Selecta matrices.push(); matrices.translate(this.x, this.y, getZOffset()); - hovered = isMouseOver(x, y); + hovered = isMouseOver(x - this.x, y - this.y); if (hovered) { fill(matrices, 0, 0, (int)getWidth(), (int)getHeight(), Argb.getArgb(32, 255, 255, 255)); @@ -109,9 +109,6 @@ public class Button extends DrawableHelper implements Drawable, Element, Selecta public boolean isMouseOver(double x, double y) { if (clicked) return true; - x -= this.x; - y -= this.y; - return x >= 0 && x < getWidth() && y >= 0 && y < getHeight(); } @@ -136,7 +133,7 @@ public class Button extends DrawableHelper implements Drawable, Element, Selecta @Override public boolean mouseClicked(double mouseX, double mouseY, int button) { if (button != 0) return false; - if (isMouseOver(mouseX, mouseY)) { + if (isMouseOver(mouseX - this.x, mouseY - this.x)) { clicked = true; hovered = true; } diff --git a/src/main/java/me/topchetoeu/animatedchunks/gui/ChunkPreview.java b/src/main/java/me/topchetoeu/animatedchunks/gui/ChunkPreview.java index c1441be..7731df1 100644 --- a/src/main/java/me/topchetoeu/animatedchunks/gui/ChunkPreview.java +++ b/src/main/java/me/topchetoeu/animatedchunks/gui/ChunkPreview.java @@ -246,9 +246,6 @@ public class ChunkPreview extends DrawableHelper implements Drawable, Element, S public boolean isMouseOver(double x, double y) { if (clicked) return true; - x -= this.x; - y -= this.y; - return x >= 0 && x < getWidth() && y >= 0 && y < getHeight(); } diff --git a/src/main/java/me/topchetoeu/animatedchunks/gui/Section.java b/src/main/java/me/topchetoeu/animatedchunks/gui/Section.java index a9752a4..c6cd797 100644 --- a/src/main/java/me/topchetoeu/animatedchunks/gui/Section.java +++ b/src/main/java/me/topchetoeu/animatedchunks/gui/Section.java @@ -181,7 +181,7 @@ public abstract class Section extends AbstractParentElement implements Drawable, @Override public Optional hoveredElement(double mouseX, double mouseY) { for (Selectable element : this.children.selectables) { - var offset = children.getOffsetAndPos(element); + var offset = children.getOffset(element); if (element.getType() != SelectionType.HOVERED || !((Element)element).isMouseOver(mouseX - offset.x, mouseY - offset.y)) continue; return Optional.of((Element)element); } @@ -191,8 +191,8 @@ public abstract class Section extends AbstractParentElement implements Drawable, @Override public boolean mouseScrolled(double mouseX, double mouseY, double delta) { for (Element element : this.children()) { - var offset = children.getOffsetAndPos(element); - if (!element.mouseScrolled(mouseX - offset.x, mouseY - offset.y, delta)) continue; + var offset = children.getOffset(element); + if (!element.mouseScrolled(mouseX - offset.x - x, mouseY - offset.y - y, delta)) continue; return true; } return false; @@ -200,8 +200,8 @@ public abstract class Section extends AbstractParentElement implements Drawable, @Override public boolean mouseClicked(double mouseX, double mouseY, int button) { for (Element element : this.children()) { - var offset = children.getOffsetAndPos(element); - if (!element.mouseClicked(mouseX - offset.x, mouseY - offset.y, button)) continue; + var offset = children.getOffset(element); + if (!element.mouseClicked(mouseX - offset.x - x, mouseY - offset.y - y, button)) continue; this.setFocused(element); if (button == 0) { this.setDragging(true); @@ -214,8 +214,8 @@ public abstract class Section extends AbstractParentElement implements Drawable, @Override public void mouseMoved(double mouseX, double mouseY) { for (Element element : this.children()) { - var offset = children.getOffsetAndPos(element); - element.mouseMoved(mouseX - offset.x, mouseY - offset.y); + var offset = children.getOffset(element); + element.mouseMoved(mouseX - offset.x - x, mouseY - offset.y - y); } } @@ -224,8 +224,8 @@ public abstract class Section extends AbstractParentElement implements Drawable, this.setDragging(false); for (Element element : children.get()) { - var offset = children.getOffsetAndPos(element); - if (element.mouseReleased(mouseX - (int)offset.x, mouseY - (int)offset.y, button)) return true; + var offset = children.getOffset(element); + if (element.mouseReleased(mouseX - (int)offset.x - x, mouseY - (int)offset.y - y, button)) return true; } return false; @@ -234,8 +234,8 @@ public abstract class Section extends AbstractParentElement implements Drawable, @Override public boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) { if (this.getFocused() != null && this.isDragging() && button == 0) { - var offset = children.getOffsetAndPos(getFocused()); - return this.getFocused().mouseDragged(mouseX - offset.x, mouseY - offset.y, button, deltaX, deltaY); + var offset = children.getOffset(getFocused()); + return this.getFocused().mouseDragged(mouseX - offset.x - x, mouseY - offset.y - y, button, deltaX, deltaY); } return false; } From 2507e806f373b732bdda10b5910d41912e8ad09f Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Thu, 22 Sep 2022 10:55:57 +0300 Subject: [PATCH 2/5] chore: Change default ease to sine --- src/main/java/me/topchetoeu/animatedchunks/AnimatedChunks.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/me/topchetoeu/animatedchunks/AnimatedChunks.java b/src/main/java/me/topchetoeu/animatedchunks/AnimatedChunks.java index ab7d9a9..cef0e8a 100644 --- a/src/main/java/me/topchetoeu/animatedchunks/AnimatedChunks.java +++ b/src/main/java/me/topchetoeu/animatedchunks/AnimatedChunks.java @@ -91,7 +91,7 @@ public final class AnimatedChunks implements ClientModInitializer, ModMenuApi { .description("Animation takes off very quickly, overshoots, then undershoots, until it reaches the end.") ); - manager.set("elastic"); + manager.set("sine"); } private static void registerAnimations(Manager manager) { manager.register(new Descriptor<>(new RiseAnimation(), "rise") From 766a9740d6a91d24c558f5263364085917c93557 Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Thu, 22 Sep 2022 10:56:17 +0300 Subject: [PATCH 3/5] feat: Add basic input element --- .../gui/AnimatedChunksScreen.java | 1 + .../topchetoeu/animatedchunks/gui/Input.java | 160 ++++++++++++++++++ 2 files changed, 161 insertions(+) create mode 100644 src/main/java/me/topchetoeu/animatedchunks/gui/Input.java diff --git a/src/main/java/me/topchetoeu/animatedchunks/gui/AnimatedChunksScreen.java b/src/main/java/me/topchetoeu/animatedchunks/gui/AnimatedChunksScreen.java index 7572c27..f0d352f 100644 --- a/src/main/java/me/topchetoeu/animatedchunks/gui/AnimatedChunksScreen.java +++ b/src/main/java/me/topchetoeu/animatedchunks/gui/AnimatedChunksScreen.java @@ -46,6 +46,7 @@ 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); } diff --git a/src/main/java/me/topchetoeu/animatedchunks/gui/Input.java b/src/main/java/me/topchetoeu/animatedchunks/gui/Input.java new file mode 100644 index 0000000..1eec4e5 --- /dev/null +++ b/src/main/java/me/topchetoeu/animatedchunks/gui/Input.java @@ -0,0 +1,160 @@ +package me.topchetoeu.animatedchunks.gui; + +import org.lwjgl.glfw.GLFW; + +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.Selectable; +import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder; +import net.minecraft.client.gui.screen.narration.NarrationPart; +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 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; + } + public Input setValue(float val) { + if (val < min) val = min; + if (val > max) val = max; + + value = val; + return this; + } + + public float getX() { + return x; + } + public float getY() { + return y; + } + + public float getWidth() { + return width; + } + public float getHeight() { + return paddingY * 2 + client.textRenderer.fontHeight; + } + + public void click() { + if (clickAction != null) clickAction.onClick(); + } + + @Override + public void appendNarrations(NarrationMessageBuilder msgBuilder) { + msgBuilder.put(NarrationPart.HINT, "Input"); + } + + @Override + public SelectionType getType() { + if (focused) return SelectionType.FOCUSED; + if (hovered) return SelectionType.HOVERED; + return SelectionType.NONE; + } + @Override + public boolean changeFocus(boolean lookForwards) { + focused = !focused; + return focused; + } + + @Override + public void render(MatrixStack matrices, int x, int y, float delta) { + int white = Argb.getArgb(255, 255, 255, 255); + matrices.push(); + matrices.translate(this.x, this.y, getZOffset()); + + hovered = isMouseOver(x, y); + + // if (hovered) { + // fill(matrices, 0, 0, (int)getWidth(), (int)getHeight(), Argb.getArgb(32, 255, 255, 255)); + // } + if (clicked) { + fill(matrices, 0, 0, (int)getWidth(), (int)getHeight(), Argb.getArgb(127, 255, 255, 255)); + } + + 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); + drawHorizontalLine(matrices, 0, (int)getWidth() - 1, (int)getHeight() - 1, white); + + // if (focused) { + // } + + client.textRenderer.draw(matrices, content, paddingX + 1, paddingY + 1, white); + + matrices.pop(); + } + + @Override + public boolean isMouseOver(double x, double y) { + if (clicked) return true; + + return x >= 0 && x < getWidth() && y >= 0 && y < getHeight(); + } + + @Override + public boolean charTyped(char chr, int modifiers) { + content += chr; + 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; + } + return false; + } + @Override + public boolean mouseClicked(double mouseX, double mouseY, int button) { + if (button != 0) return false; + if (isMouseOver(mouseX - this.x, mouseY - this.y)) { + clicked = true; + hovered = true; + return true; + } + return false; + // return true; + } + @Override + public boolean mouseReleased(double mouseX, double mouseY, int button) { + if (button != 0) return false; + if (clicked) { + clicked = false; + return true; + } + return false; + } + + public Input(int x, int y, ClickAction clickAction) { + this.clickAction = clickAction; + this.client = MinecraftClient.getInstance(); + this.x = x; + this.y = y; + } +} \ No newline at end of file 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 4/5] 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(""); + } +} From 23d289f9f7d99ead59ee61f90745adf0b4d84218 Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Thu, 22 Sep 2022 12:30:02 +0300 Subject: [PATCH 5/5] chore: Save and load duration from config file --- .../animatedchunks/AnimatedChunks.java | 2 +- .../animatedchunks/ConfigManager.java | 46 ++++++++++++------- .../gui/AnimatedChunksScreen.java | 2 +- .../animatedchunks/gui/NumberInput.java | 7 ++- 4 files changed, 37 insertions(+), 20 deletions(-) diff --git a/src/main/java/me/topchetoeu/animatedchunks/AnimatedChunks.java b/src/main/java/me/topchetoeu/animatedchunks/AnimatedChunks.java index 6517aaf..57b5545 100644 --- a/src/main/java/me/topchetoeu/animatedchunks/AnimatedChunks.java +++ b/src/main/java/me/topchetoeu/animatedchunks/AnimatedChunks.java @@ -134,7 +134,7 @@ public final class AnimatedChunks implements ClientModInitializer, ModMenuApi { registerEases(ease); registerAnimations(animation); - config = new ConfigManager(new File("config/animated-chunks.dat"), animation, ease); + config = new ConfigManager(new File("config/animated-chunks.dat"), animation, ease, progress); EASES_REGISTERING.invoker().register(ease); ANIMATIONS_REGISTERING.invoker().register(animation); diff --git a/src/main/java/me/topchetoeu/animatedchunks/ConfigManager.java b/src/main/java/me/topchetoeu/animatedchunks/ConfigManager.java index a9bbfe0..d64ca11 100644 --- a/src/main/java/me/topchetoeu/animatedchunks/ConfigManager.java +++ b/src/main/java/me/topchetoeu/animatedchunks/ConfigManager.java @@ -1,22 +1,25 @@ package me.topchetoeu.animatedchunks; +import java.io.EOFException; import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.FileWriter; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.ByteBuffer; import me.topchetoeu.animatedchunks.animation.Animation; +import me.topchetoeu.animatedchunks.animation.ProgressManager; import me.topchetoeu.animatedchunks.easing.Ease; public class ConfigManager { public final File configFile; private final Manager animation; private final Manager ease; + private final ProgressManager progress; - private String readString(InputStreamReader reader) throws IOException { + private String readString(InputStream reader) throws IOException { String res = ""; int i; @@ -27,36 +30,46 @@ public class ConfigManager { return res; } - private void writeString(OutputStreamWriter writer, String str) throws IOException { + private void writeString(OutputStream writer, String str) throws IOException { for (int i = 0; i < str.length(); i++) { - writer.write((char)str.charAt(i)); + writer.write((byte)str.charAt(i)); + } + writer.write((byte)0); + } + + private float readFloat(InputStream reader) throws IOException { + try { + var bytes = reader.readNBytes(4); + return ByteBuffer.wrap(bytes).getFloat(); + } + catch (IndexOutOfBoundsException e) { + throw new EOFException(); } - writer.write(0); } public void reload() { try { - var reader = new FileReader(configFile); + var reader = new FileInputStream(configFile); String animation = readString(reader); String ease = readString(reader); + float duration = readFloat(reader); reader.close(); this.animation.set(animation); this.ease.set(ease); - } - catch (FileNotFoundException e) { - save(); + this.progress.setDuration(duration); } catch (IOException e) { - throw new RuntimeException(e); + save(); } } public void save() { try { - var writer = new FileWriter(configFile); + var writer = new FileOutputStream(configFile); writeString(writer, animation.get().getName()); writeString(writer, ease.get().getName()); + writer.write(ByteBuffer.allocate(4).putFloat(progress.getDuration()).array()); writer.close(); } catch (IOException e) { @@ -64,10 +77,11 @@ public class ConfigManager { } } - public ConfigManager(File configFile, Manager animation, Manager ease) { + public ConfigManager(File configFile, Manager animation, Manager ease, ProgressManager progress) { this.configFile = configFile; this.animation = animation; this.ease = ease; + this.progress = progress; reload(); } diff --git a/src/main/java/me/topchetoeu/animatedchunks/gui/AnimatedChunksScreen.java b/src/main/java/me/topchetoeu/animatedchunks/gui/AnimatedChunksScreen.java index c432f82..ffec4a8 100644 --- a/src/main/java/me/topchetoeu/animatedchunks/gui/AnimatedChunksScreen.java +++ b/src/main/java/me/topchetoeu/animatedchunks/gui/AnimatedChunksScreen.java @@ -100,7 +100,7 @@ public class AnimatedChunksScreen extends Screen { private Section durationSection() { var res = new HorizontalSection(); res.setTargetWidth(width / 2); - var input = new NumberInput(res, 5, 5, (sender, val) -> { + var input = new NumberInput(res, 5, 5, progress.getDuration(), (sender, val) -> { if (val <= 0) sender.invalid = true; else { progress.setDuration(val); diff --git a/src/main/java/me/topchetoeu/animatedchunks/gui/NumberInput.java b/src/main/java/me/topchetoeu/animatedchunks/gui/NumberInput.java index 92476b3..5393b1d 100644 --- a/src/main/java/me/topchetoeu/animatedchunks/gui/NumberInput.java +++ b/src/main/java/me/topchetoeu/animatedchunks/gui/NumberInput.java @@ -18,7 +18,7 @@ public class NumberInput extends Input { return this; } - public NumberInput(ParentElement parent, int x, int y, InputAction action) { + public NumberInput(ParentElement parent, int x, int y, float value, InputAction action) { super(parent, x, y, null); super.action = (sender, val) -> { try { @@ -30,6 +30,9 @@ public class NumberInput extends Input { } }; this.action = action; - setContent(""); + setContent(Float.toString(value)); + } + public NumberInput(ParentElement parent, int x, int y, InputAction action) { + this(parent, x, y, 0, action); } }