Skip to content

Commit

Permalink
More changes and fixes. Also DoubleOption. Enum option will be next a…
Browse files Browse the repository at this point in the history
…nd final.
  • Loading branch information
tanishisherewithhh committed Apr 6, 2024
1 parent b338fd5 commit ea86f31
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 61 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ private void render(DrawContext context, int mouseX, int mouseY, float delta, Ca
private void onScreenResize(MinecraftClient client,int width, int height, CallbackInfo ci) {
WidgetManager.onScreenResized(width,height,this.width,this.height);
}
@Inject(at = @At("TAIL"), method = "close")
private void render(CallbackInfo ci) {
@Inject(at = @At("HEAD"), method = "close")
private void onClose(CallbackInfo ci) {
for(WidgetRenderer widgetRenderer: DynamicHUD.getWidgetRenderers()){
widgetRenderer.onCloseScreen();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@
public class DynamicHudTest implements DynamicHudIntegration {
TextWidget textWidget;
TextWidget Example2Widget;
DynamicValueRegistry registry = new DynamicValueRegistry(DynamicHUD.MOD_ID);
DynamicValueRegistry registry;
WidgetRenderer renderer;
@Override
public void init() {
//Global registry
DynamicValueRegistry.registerGlobal("FPS",() -> "FPS: "+ DynamicHUD.MC.getCurrentFps());

//Local registry
registry = new DynamicValueRegistry(DynamicHUD.MOD_ID);
registry.registerLocal("FPS",()-> "FPS C-DVR: "+ DynamicHUD.MC.getCurrentFps());

textWidget = new TextWidget.Builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ public void render(DrawContext drawContext, int mouseX, int mouseY, float delta)

@Override
public void close() {
super.close();
widgetRenderer.isInEditor = false;
widgetRenderer.onCloseScreen();
super.close();
}

public void setShouldPause(boolean shouldPause) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ public class ContextMenu {
public int heightOffset = 4; // Height offset from the widget
public boolean shouldDisplay = false;

public ContextMenu(int x,int y, int width, int height, Widget selectedWidget){
public ContextMenu(int x,int y){
this.x = x;
this.y = y + heightOffset;
this.width = width;
this.height = height;
this.selectedWidget = selectedWidget;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ protected T get(){
return getter.get();
}
protected void set(T value){
this.value = value;
setter.accept(value);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.options;

import com.tanishisherewith.dynamichud.helpers.DrawHelper;
import com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.Option;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import org.apache.commons.lang3.Validate;

import javax.xml.validation.Validator;
import java.util.function.Consumer;
import java.util.function.Supplier;

public class DoubleOption extends Option<Double> {
public String name = "Empty";
private boolean isDragging = false;
private double minValue = 0.0,maxValue = 0.0;
float step = 0.1f;
public DoubleOption(String name,double minValue,double maxValue,float step,Supplier<Double> getter, Consumer<Double> setter) {
super(getter, setter);
this.name = name;
this.value = get();
this.minValue = minValue;
this.maxValue = maxValue;
this.width = 30;
this.height = 16;
this.step = step;
Validate.isTrue(this.step>0.0f,"Step cannot be less than or equal to 0 (zero)");
}
@Override
public void render(DrawContext drawContext, int x, int y) {
super.render(drawContext, x, y);

value = get();

this.width = 30;
this.height = 16;
// Draw the label
TextRenderer textRenderer = mc.textRenderer;
DrawHelper.scaleAndPosition(drawContext.getMatrices(),x,y,0.7f);
String labelText = name + ": " + String.format("%.1f", value);
int labelWidth = textRenderer.getWidth(labelText);
this.width = Math.max(this.width,labelWidth);
drawContext.drawTextWithShadow(textRenderer, labelText, x, y + 1, 0xFFFFFFFF);
DrawHelper.stopScaling(drawContext.getMatrices());

// Draw the slider
drawSlider(drawContext, x, y + textRenderer.fontHeight + 1, width);

// Draw the handle
float handleWidth = 3;
float handleHeight = 8;
double handleX = x + (value - minValue) / (maxValue - minValue) * (width - handleWidth);
double handleY = y + textRenderer.fontHeight + 1 + ((2 - handleHeight) / 2);

DrawHelper.fillRoundedRect(drawContext, (int) handleX, (int) handleY, (int) (handleX + handleWidth), (int) (handleY + handleHeight), 0xFFFFFFFF);
}

private void drawSlider(DrawContext drawContext, int sliderX, int sliderY, int sliderWidth) {
DrawHelper.fill(drawContext, sliderX, sliderY, sliderX + sliderWidth, sliderY + 2, 0xFFFFFFFF);
}

@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
super.mouseClicked(mouseX, mouseY, button);
if(isMouseOver(mouseX, mouseY)) {
step(mouseX);
isDragging = true;
}
return true;
}

@Override
public boolean mouseReleased(double mouseX, double mouseY, int button) {
isDragging = false;
return super.mouseReleased(mouseX, mouseY, button);
}
private void step(double mouseX){
double newValue = minValue + (float) (mouseX - x) / width * (maxValue - minValue);
// Round the new value to the nearest step
newValue = Math.round(newValue / step) * step;
set(newValue);
}

@Override
public boolean mouseDragged(double mouseX, double mouseY, int button) {
if(isMouseOver(mouseX, mouseY) && isDragging) {
step(mouseX);
}
return super.mouseDragged(mouseX, mouseY, button);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ public abstract class Widget {

protected MinecraftClient mc = MinecraftClient.getInstance();

//This is the UID of the widget used to identify during loading and saving
/**
* This is the UID of the widget used to identify during loading and saving.
* <p>
* It's different from modID because this is unique to each widget.
* @see #modId
*/
public UID uid = UID.generate();
public boolean isInEditor = false;
// Whether the widget is enabled and should be displayed.
Expand All @@ -41,6 +46,14 @@ public abstract class Widget {
// The y position of the widget as a percentage of the screen height, i.e. the relative y position of the widget for resizing and scaling
protected float yPercent;
public boolean shouldScale = true;

/**
* An identifier for widgets to group them under one ID.
* <p>
* Doesn't necessarily have to be the mod ID of mod, but it's preferred to use mod ID if you are only grouping widgets under one ID.
* Can be any string if wanted.
* @see #uid
*/
public String modId = "unknown";

//Dimensions of the widget
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ public static void loadWidgets(File file) throws IOException {
NbtCompound widgetTag = widgetList.getCompound(i);
WidgetData<?> widgetData = widgetDataMap.get(widgetTag.getString("name"));
Widget widget = widgetData.createWidget();
widget.readFromTag(widgetTag);
printInfo("Loading Widget: " + widget);
widget.readFromTag(widgetTag);
widgets.add(widget);
}
}else{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.tanishisherewith.dynamichud.DynamicHUD;
import com.tanishisherewith.dynamichud.newTrial.screens.AbstractMoveableScreen;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.GameMenuScreen;
import net.minecraft.client.gui.screen.Screen;
import org.lwjgl.glfw.GLFW;

Expand All @@ -15,8 +16,17 @@ public class WidgetRenderer {
public boolean isInEditor = false;
List<Widget> widgets;
public Widget selectedWidget = null;

/**
* Add the list of widgets the widgetRenderer should render
* <p>
* By default, it adds the {@link GameMenuScreen} to allow rendering of the widgets in the pause/main menu screen.
*
* @param widgets List of widgets to render
*/
public WidgetRenderer(List<Widget> widgets){
this.widgets = widgets;
addScreen(GameMenuScreen.class);
}
public void addWidget(Widget widget){
widgets.add(widget);
Expand Down Expand Up @@ -63,6 +73,8 @@ public void mouseClicked(double mouseX, double mouseY, int button){
}
if(currentScreen instanceof AbstractMoveableScreen){
for (Widget widget : widgets) {
// This essentially acts as a Z - layer where the widget first in the list is moved and dragged
// if they are overlapped on each other.
if(widget.mouseClicked(mouseX,mouseY,button)){
selectedWidget = widget;
return;
Expand All @@ -77,6 +89,8 @@ public void mouseDragged(double mouseX, double mouseY, int button, int snapSize)
}
if(currentScreen instanceof AbstractMoveableScreen){
for (Widget widget : widgets) {
// This essentially acts as a Z - layer where the widget first in the list is moved and dragged
// if they are overlapped on each other.
if(widget.mouseDragged(mouseX,mouseY,button,snapSize)){
selectedWidget = widget;
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.tanishisherewith.dynamichud.newTrial.utils.DynamicValueRegistry;
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.widget.Widget;
import com.tanishisherewith.dynamichud.newTrial.widget.WidgetData;
import net.minecraft.client.gui.DrawContext;
Expand All @@ -20,6 +21,7 @@ public class TextWidget extends Widget {
DynamicValueRegistry dynamicValueRegistry = null;
protected boolean shadow; // Whether to draw a shadow behind the text
protected boolean rainbow; // Whether to apply a rainbow effect to the text
protected int rainbowSpeed = 2; //Speed of the rainbow effect
private ContextMenu menu;

public TextWidget() {
Expand All @@ -39,9 +41,13 @@ public TextWidget(String dynamicRegistryKey, boolean shadow, boolean rainbow,Str
textSupplier = (Supplier<String>) DynamicValueRegistry.getGlobal(dynamicRegistryKey);
this.shadow = shadow;
this.rainbow = rainbow;
menu = new ContextMenu(getX(),getY(),20,20,null);
createMenu();
}
public void createMenu(){
menu = new ContextMenu(getX(),getY());
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()));
}

/**
Expand All @@ -58,19 +64,17 @@ public TextWidget(DynamicValueRegistry dynamicValueRegistry,String dynamicRegist
textSupplier = (Supplier<String>) dynamicValueRegistry.get(dynamicRegistryKey);
this.shadow = shadow;
this.rainbow = rainbow;
menu = new ContextMenu(getX(),getY(),20,20,null);
menu.addOption(new BooleanOption("Shadow",()->this.shadow,value-> this.shadow = value));
menu.addOption(new BooleanOption("Rainbow",()->this.rainbow,value-> this.rainbow = value));
createMenu();
}

@Override
public void renderWidget(DrawContext drawContext,int mouseX, int mouseY) {
menu.render(drawContext, getX() - 2,getY(), (int) Math.ceil(getHeight()));
int color = rainbow ? ColorHelper.getColorFromHue((System.currentTimeMillis() % 10000) / 10000f) : Color.WHITE.getRGB();
if(textSupplier != null) {
menu.render(drawContext, getX() - 2, getY(), (int) Math.ceil(getHeight()));
int color = rainbow ? ColorHelper.getColorFromHue((System.currentTimeMillis() % (5000 * rainbowSpeed) / (float) (5000f * rainbowSpeed))) : Color.WHITE.getRGB();
if (textSupplier != null) {
String text = textSupplier.get();
drawContext.drawText(mc.textRenderer, text, (int) getX(), (int)getY(), color, shadow);
widgetBox.setSizeAndPosition(getX() - 2,getY() - 2,mc.textRenderer.getWidth(text) + 2,mc.textRenderer.fontHeight + 2);
drawContext.drawText(mc.textRenderer, text, (int) getX(), (int) getY(), color, shadow);
widgetBox.setSizeAndPosition(getX() - 2, getY() - 2, mc.textRenderer.getWidth(text) + 2, mc.textRenderer.fontHeight + 2);
}
}

Expand Down Expand Up @@ -135,10 +139,8 @@ public void readFromTag(NbtCompound tag) {
System.out.println(DynamicValueRegistry.getInstances(modId));
return;
}
menu = new ContextMenu(getX(),getY(),20,20,null);
menu.addOption(new BooleanOption("Shadow",()->this.shadow,value-> this.shadow = value));
menu.addOption(new BooleanOption("Rainbow",()->this.rainbow,value-> this.rainbow = value));
}
createMenu();
}
public static class Builder extends WidgetBuilder<Builder,TextWidget> {
protected boolean shadow = false;
protected boolean rainbow = false;
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/dynamichud.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"defaultRequire": 1
},
"client": [
"OptionsScreenMixin",
"ScreenMixin"
]
}

0 comments on commit ea86f31

Please sign in to comment.