Skip to content

Commit

Permalink
Enum and List option.
Browse files Browse the repository at this point in the history
  • Loading branch information
tanishisherewithhh committed Apr 6, 2024
1 parent ea86f31 commit 6822259
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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<E extends Enum<E>> extends Option<E> {
private final E[] values;
private int currentIndex = 0;
public String name = "Empty";

public EnumOption(String name, Supplier<E> getter, Consumer<E> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<T> extends Option<T> {
private final List<T> values;
private int currentIndex = 0;
public String name = "Empty";

public ListOption(String name, Supplier<T> getter, Consumer<T> setter, List<T> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<test> testss = new AtomicReference<>(test.TEST);
List<String> options = Arrays.asList("TEST","TEST2","TEST23");
AtomicReference<String> option = new AtomicReference<>("TEST");
menu.addOption(new EnumOption<test>("Test", testss::get, testss::set,test.values()));
menu.addOption(new ListOption<String>("New", option::get, option::set,options ));
}
public enum test{
TEST,
TEST_2,
TEST_3
}

/**
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down

0 comments on commit 6822259

Please sign in to comment.