Skip to content

Commit

Permalink
Allow string type config editing
Browse files Browse the repository at this point in the history
  • Loading branch information
Attack8 committed Nov 3, 2024
1 parent d48a504 commit af8bd3d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.mojang.blaze3d.platform.Window;
import com.mojang.blaze3d.systems.RenderSystem;
import com.simibubi.create.foundation.config.ui.entries.NumberEntry;
import com.simibubi.create.foundation.config.ui.entries.StringEntry;
import com.simibubi.create.foundation.gui.RemovedGuiUtils;
import com.simibubi.create.foundation.gui.Theme;
import com.simibubi.create.foundation.gui.TickableGuiEventListener;
Expand Down Expand Up @@ -66,6 +67,7 @@ protected void renderList(GuiGraphics graphics, int p_239229_, int p_239230_, fl
@Override
public boolean mouseClicked(double x, double y, int button) {
children().stream().filter(e -> e instanceof NumberEntry<?>).forEach(e -> e.mouseClicked(x, y, button));
children().stream().filter(e -> e instanceof StringEntry).forEach(e -> e.mouseClicked(x, y, button));

return super.mouseClicked(x, y, button);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import com.simibubi.create.Create;
import com.simibubi.create.foundation.config.ui.entries.StringEntry;

import org.lwjgl.glfw.GLFW;

import com.electronwill.nightconfig.core.AbstractConfig;
Expand Down Expand Up @@ -280,6 +283,8 @@ protected void init() {
entry = new EnumEntry(humanKey, (ForgeConfigSpec.ConfigValue<Enum<?>>) configValue, valueSpec);
} else if (value instanceof Number) {
entry = NumberEntry.create(value, humanKey, configValue, valueSpec);
} else if (value instanceof String) {
entry = new StringEntry(humanKey, (ForgeConfigSpec.ConfigValue<String>) configValue, valueSpec);
}

if (entry == null)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.simibubi.create.foundation.config.ui.entries;

import com.simibubi.create.foundation.config.ui.ConfigTextField;

import com.simibubi.create.foundation.gui.Theme;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.EditBox;
import net.minecraftforge.common.ForgeConfigSpec;

public class StringEntry extends ValueEntry<String> {

protected EditBox textField;

public StringEntry(String label, ForgeConfigSpec.ConfigValue<String> value, ForgeConfigSpec.ValueSpec spec) {
super(label, value, spec);
textField = new ConfigTextField(Minecraft.getInstance().font, 0, 0, 200, 20);
textField.setValue(value.get());
textField.setTextColor(Theme.i(Theme.Key.TEXT));

textField.setResponder(this::setValue);

textField.moveCursorToStart();
listeners.add(textField);
onReset();
}

@Override
public void render(GuiGraphics graphics, int index, int y, int x, int width, int height, int mouseX, int mouseY, boolean p_230432_9_, float partialTicks) {
super.render(graphics, index, y, x, width, height, mouseX, mouseY, p_230432_9_, partialTicks);

textField.setX(x + width - 82 - resetWidth);
textField.setY(y + 8);
textField.setWidth(Math.min(width - getLabelWidth(width) - resetWidth, 60));
textField.setHeight(20);
textField.render(graphics, mouseX, mouseY, partialTicks);

}

@Override
protected void setEditable(boolean b) {
super.setEditable(b);
textField.setEditable(b);
}

@Override
public void tick() {
super.tick();
textField.tick();
}
}

0 comments on commit af8bd3d

Please sign in to comment.