Merge pull request #12 from TopchetoEU/TopchetoEU/Duration-config

Add duration config
This commit is contained in:
TopchetoEU 2022-09-22 12:30:39 +03:00 committed by GitHub
commit dcdc583e18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 315 additions and 43 deletions

View File

@ -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<Animation> manager) {
manager.register(new Descriptor<>(new RiseAnimation(), "rise")
@ -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);
@ -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);
};
}
}

View File

@ -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> animation;
private final Manager<Ease> 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> animation, Manager<Ease> ease) {
public ConfigManager(File configFile, Manager<Animation> animation, Manager<Ease> ease, ProgressManager progress) {
this.configFile = configFile;
this.animation = animation;
this.ease = ease;
this.progress = progress;
reload();
}

View File

@ -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> animation;
private final Manager<Ease> 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,7 +48,6 @@ public class AnimatedChunksScreen extends Screen {
mainSection.order = OrderType.Justified;
mainSection.children.addSelectableChild(selectionsSection());
mainSection.children.addSelectableChild(previewSection());
// setZOffset(parent.getZOffset() + 1);
}
@Override
@ -96,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, progress.getDuration(), (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> animation, Manager<Ease> ease, ConfigManager config) {
public AnimatedChunksScreen(Screen parent, Manager<Animation> animation, Manager<Ease> 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;

View File

@ -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;
}

View File

@ -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();
@ -246,9 +245,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();
}

View File

@ -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) {

View File

@ -0,0 +1,207 @@
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 interface InputAction {
void onInput(Input sender, String val);
}
public final MinecraftClient client;
private boolean clicked = false;
private boolean hovered = false;
private boolean isFocused() {
return parent.getFocused() == this;
}
private int index = 0;
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;
}
public float getX() {
return x;
}
public float getY() {
return y;
}
public float getWidth() {
return width;
}
public float getHeight() {
return 1 + paddingY * 2 + client.textRenderer.fontHeight;
}
@Override
public void appendNarrations(NarrationMessageBuilder msgBuilder) {
msgBuilder.put(NarrationPart.HINT, "Input");
}
@Override
public SelectionType getType() {
if (isFocused()) return SelectionType.FOCUSED;
if (hovered) return SelectionType.HOVERED;
return SelectionType.NONE;
}
@Override
public boolean changeFocus(boolean lookForwards) {
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
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));
}
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);
drawHorizontalLine(matrices, 0, (int)getWidth() - 1, (int)getHeight() - 1, white);
// if (focused) {
// }
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 = 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 && 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
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(ParentElement parent, int x, int y, InputAction input) {
this.parent = parent;
this.action = input;
this.client = MinecraftClient.getInstance();
this.x = x;
this.y = y;
}
}

View File

@ -0,0 +1,38 @@
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, float value, 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(Float.toString(value));
}
public NumberInput(ParentElement parent, int x, int y, InputAction action) {
this(parent, x, y, 0, action);
}
}

View File

@ -181,7 +181,7 @@ public abstract class Section extends AbstractParentElement implements Drawable,
@Override
public Optional<Element> 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;
}