diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/EnumOption.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/EnumOption.java new file mode 100644 index 0000000..7c49a8f --- /dev/null +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/EnumOption.java @@ -0,0 +1,62 @@ +package com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.options; + +import com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.Option; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.text.Text; + +import java.awt.Color; +import java.util.function.Consumer; +import java.util.function.Supplier; + +public class EnumOption> extends Option { + private final E[] values; + private int currentIndex = 0; + public String name = "Empty"; + + public EnumOption(String name, Supplier getter, Consumer setter, E[] values) { + super(getter, setter); + this.name = name; + this.values = values; + this.value = get(); + for (int i = 0; i < values.length; i++) { + if (values[i] == value) { + currentIndex = i; + break; + } + } + } + + @Override + public void render(DrawContext drawContext, int x, int y) { + super.render(drawContext, x, y); + + value = get(); + this.height = mc.textRenderer.fontHeight + 1; + this.width = mc.textRenderer.getWidth(name + ": " + value.name()) + 1; + + int color = Color.WHITE.getRGB(); + drawContext.drawText(mc.textRenderer, Text.of(name + ": " + value.name()), x, y, color, false); + } + + @Override + public boolean mouseClicked(double mouseX, double mouseY, int button) { + super.mouseClicked(mouseX, mouseY, button); + if (isMouseOver(mouseX, mouseY)) { + if(button == 0) { + currentIndex = (currentIndex + 1) % values.length; + if(currentIndex > values.length - 1){ + currentIndex = 0; + } + value = values[currentIndex]; + }else if(button == 1){ + currentIndex = (currentIndex - 1) % values.length; + if(currentIndex < 0){ + currentIndex = values.length - 1; + } + value = values[currentIndex]; + } + set(value); + } + return true; + } +} diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/ListOption.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/ListOption.java new file mode 100644 index 0000000..80f605a --- /dev/null +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/utils/contextmenu/options/ListOption.java @@ -0,0 +1,64 @@ +package com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.options; + +import com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.Option; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.text.Text; + +import java.awt.*; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; +import java.util.function.Supplier; + +public class ListOption extends Option { + private final List values; + private int currentIndex = 0; + public String name = "Empty"; + + public ListOption(String name, Supplier getter, Consumer setter, List values) { + super(getter, setter); + this.name = name; + this.values = values; + this.value = getter.get(); + for (int i = 0; i < values.size(); i++) { + if (values.get(i).toString().equals(value)) { + currentIndex = i; + break; + } + } + } + + @Override + public void render(DrawContext drawContext, int x, int y) { + super.render(drawContext, x, y); + + value = get(); + this.height = mc.textRenderer.fontHeight; + this.width = mc.textRenderer.getWidth(name + ": " + value.toString()) + 1; + + int color = Color.WHITE.getRGB(); + drawContext.drawText(mc.textRenderer, Text.of(name + ": " + value.toString()), x, y, color, false); + } + + @Override + public boolean mouseClicked(double mouseX, double mouseY, int button) { + super.mouseClicked(mouseX, mouseY, button); + if (isMouseOver(mouseX, mouseY)) { + if(button == 0) { + currentIndex = (currentIndex + 1) % values.size(); + if(currentIndex > values.size() - 1){ + currentIndex = 0; + } + value = values.get(currentIndex); + }else if(button == 1){ + currentIndex = (currentIndex - 1) % values.size(); + if(currentIndex < 0){ + currentIndex = values.size() - 1; + } + value = values.get(currentIndex); + } + set(value); + } + return true; + } +} diff --git a/src/main/java/com/tanishisherewith/dynamichud/newTrial/widgets/TextWidget.java b/src/main/java/com/tanishisherewith/dynamichud/newTrial/widgets/TextWidget.java index 0d276a9..ef6f1b2 100644 --- a/src/main/java/com/tanishisherewith/dynamichud/newTrial/widgets/TextWidget.java +++ b/src/main/java/com/tanishisherewith/dynamichud/newTrial/widgets/TextWidget.java @@ -5,13 +5,17 @@ import com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.ContextMenu; import com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.options.BooleanOption; import com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.options.DoubleOption; +import com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.options.EnumOption; +import com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.options.ListOption; import com.tanishisherewith.dynamichud.newTrial.widget.Widget; import com.tanishisherewith.dynamichud.newTrial.widget.WidgetData; import net.minecraft.client.gui.DrawContext; import net.minecraft.nbt.NbtCompound; import org.lwjgl.glfw.GLFW; -import java.awt.*; +import java.awt.Color; +import java.util.*; +import java.util.concurrent.atomic.AtomicReference; import java.util.function.Supplier; public class TextWidget extends Widget { @@ -48,6 +52,16 @@ public void createMenu(){ menu.addOption(new BooleanOption("Shadow",()->this.shadow,value-> this.shadow = value)); menu.addOption(new BooleanOption("Rainbow",()->this.rainbow,value-> this.rainbow = value)); menu.addOption(new DoubleOption("RainbowSpeed",1,4,1.0f, ()->(double)this.rainbowSpeed, value-> this.rainbowSpeed = value.intValue())); + AtomicReference testss = new AtomicReference<>(test.TEST); + List options = Arrays.asList("TEST","TEST2","TEST23"); + AtomicReference option = new AtomicReference<>("TEST"); + menu.addOption(new EnumOption("Test", testss::get, testss::set,test.values())); + menu.addOption(new ListOption("New", option::get, option::set,options )); + } + public enum test{ + TEST, + TEST_2, + TEST_3 } /** @@ -109,8 +123,9 @@ public void onClose() { public void writeToTag(NbtCompound tag) { super.writeToTag(tag); tag.putString("DynamicRegistryKey",dynamicRegistryKey); - tag.putBoolean("shadow",shadow); - tag.putBoolean("rainbow",rainbow); + tag.putBoolean("Shadow",shadow); + tag.putBoolean("Rainbow",rainbow); + tag.putInt("RainbowSpeed",rainbowSpeed); // If true then it means that we should use local registry and if false (i.e. null) then use global registry tag.putBoolean("DynamicValueRegistry", dynamicValueRegistry != null); @@ -119,8 +134,10 @@ public void writeToTag(NbtCompound tag) { @Override public void readFromTag(NbtCompound tag) { super.readFromTag(tag); - shadow = tag.getBoolean("shadow"); - rainbow = tag.getBoolean("rainbow"); + shadow = tag.getBoolean("Shadow"); + rainbow = tag.getBoolean("Rainbow"); + rainbowSpeed = tag.getInt("RainbowSpeed"); + dynamicRegistryKey = tag.getString("DynamicRegistryKey"); // If true then it means that we should use local registry and if false (i.e. null) then use global registry