Skip to content

Commit aef38fd

Browse files
Totally unneeded but cool GraphWidget along with a change to DynamicValueRegistry and fixed option name rendering in ClassicSkin.
1 parent d20247e commit aef38fd

File tree

12 files changed

+592
-85
lines changed

12 files changed

+592
-85
lines changed

src/main/java/com/tanishisherewith/dynamichud/IntegrationTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44
import com.tanishisherewith.dynamichud.integration.DynamicHudIntegration;
55
import com.tanishisherewith.dynamichud.screens.AbstractMoveableScreen;
66
import com.tanishisherewith.dynamichud.utils.DynamicValueRegistry;
7+
import com.tanishisherewith.dynamichud.widget.Widget;
8+
import com.tanishisherewith.dynamichud.widgets.GraphWidget;
79
import com.tanishisherewith.dynamichud.widgets.TextWidget;
10+
import net.minecraft.client.MinecraftClient;
811
import net.minecraft.client.gui.screen.TitleScreen;
912
import net.minecraft.text.Text;
1013

14+
import java.awt.*;
15+
1116
public class IntegrationTest implements DynamicHudIntegration {
1217
TextWidget FPSWidget;
1318
TextWidget HelloWidget;
1419
TextWidget DynamicHUDWidget;
20+
GraphWidget graphWidget;
1521
DynamicValueRegistry registry;
1622

1723
@Override
@@ -58,13 +64,36 @@ public void init() {
5864
.shouldScale(true)
5965
.build();
6066

67+
graphWidget = new GraphWidget.GraphWidgetBuilder()
68+
.setX(250)
69+
.setY(100)
70+
.setGraphColor(Color.CYAN)
71+
.setAnchor(Widget.Anchor.CENTER)
72+
.setHeight(100)
73+
.setWidth(150)
74+
.setGridLines(10)
75+
.setBackgroundColor(Color.DARK_GRAY)
76+
.setLineThickness(1f)
77+
.setMaxDataPoints(100)
78+
.setMaxValue(120)
79+
.setMinValue(30)
80+
.setModID(DynamicHUD.MOD_ID)
81+
.setDraggable(true)
82+
.setDisplay(true)
83+
.setShowGrid(true)
84+
.setLabel("FPS Chart")
85+
.build();
86+
87+
88+
graphWidget.addDataPointEveryTick(()-> (float) MinecraftClient.getInstance().getCurrentFps());
6189
}
6290

6391
@Override
6492
public DynamicHudConfigurator configure(DynamicHudConfigurator configurator) {
6593
configurator.addWidget(FPSWidget)
6694
.addWidget(HelloWidget)
6795
.addWidget(DynamicHUDWidget)
96+
.addWidget(graphWidget)
6897
.configureRenderer(renderer -> {
6998
//Already true by default
7099
//renderer.shouldRenderInGameHud(true);

src/main/java/com/tanishisherewith/dynamichud/integration/DefaultIntegrationImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.tanishisherewith.dynamichud.integration;
22

33
import com.tanishisherewith.dynamichud.widget.WidgetManager;
4+
import com.tanishisherewith.dynamichud.widgets.GraphWidget;
45
import com.tanishisherewith.dynamichud.widgets.ItemWidget;
56
import com.tanishisherewith.dynamichud.widgets.TextWidget;
67

@@ -21,7 +22,8 @@ public void init() {}
2122
public void registerCustomWidgets() {
2223
WidgetManager.registerCustomWidgets(
2324
TextWidget.DATA,
24-
ItemWidget.DATA
25+
ItemWidget.DATA,
26+
GraphWidget.DATA
2527
);
2628
}
2729
}

src/main/java/com/tanishisherewith/dynamichud/integration/DynamicHudConfigurator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public final AbstractMoveableScreen getMovableScreen() {
9797
}
9898

9999
/**
100-
* Internal method to save these widgets using fabric API events.
100+
* Internal method to save these widgets using fabric API events. Should not be called anywhere else except when loading the DHIntegration on startup.
101101
*/
102102
@ApiStatus.Internal
103103
public void setupSaveEvents(File widgetsFile){
@@ -131,5 +131,4 @@ private void saveWidgetsSafely(File widgetsFile, List<Widget> widgets) {
131131
throw new RuntimeException(e);
132132
}
133133
}
134-
135134
}

src/main/java/com/tanishisherewith/dynamichud/integration/IntegrationManager.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public static void integrate(){
8080
}
8181
}
8282

83-
List<ModError> invalid_implementations = new ArrayList<>();
83+
List<ModError> bad_implementations = new ArrayList<>();
8484

8585
integrations.forEach(container -> {
8686
//Register custom widget data's by WidgetManager.registerCustomWidgets() first for every entrypoint
@@ -148,22 +148,22 @@ public static void integrate(){
148148
} else {
149149
DynamicHUD.logger.error("Mod {} has improper implementation of DynamicHUD", modId, e);
150150
}
151-
invalid_implementations.add(new ModError(modId, e.getLocalizedMessage().trim()));
151+
bad_implementations.add(new ModError(modId, e.getLocalizedMessage().trim()));
152152
}
153153
}
154154
printInfo("(DynamicHUD) Integration of supported mods was successful");
155155

156156

157157
// Sheesh
158-
if(!invalid_implementations.isEmpty()){
159-
BooleanPool.put("WarningScreen", false);
158+
if(!bad_implementations.isEmpty()){
159+
BooleanPool.put("WarningScreenFlag", false);
160160

161161
ClientTickEvents.START_CLIENT_TICK.register((client)->{
162-
if(BooleanPool.get("WarningScreen")) return;
162+
if(BooleanPool.get("WarningScreenFlag")) return;
163163

164164
if(DynamicHUD.MC.currentScreen instanceof TitleScreen) {
165-
DynamicHUD.MC.setScreen(new WarningScreen(invalid_implementations));
166-
BooleanPool.put("WarningScreen", true);
165+
DynamicHUD.MC.setScreen(new WarningScreen(bad_implementations));
166+
BooleanPool.put("WarningScreenFlag", true);
167167
}
168168
});
169169
}

src/main/java/com/tanishisherewith/dynamichud/internal/WarningScreen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void render(DrawContext context, int mouseX, int mouseY, float delta) {
5050
super.render(context, mouseX, mouseY, delta);
5151

5252
context.drawCenteredTextWithShadow(this.textRenderer, this.title, this.width / 2, 20, 0xFFFFFF);
53-
context.drawCenteredTextWithShadow(this.textRenderer, "Mods with invalid implementation of DynamicHUD found!", this.width / 2, 35, Color.ORANGE.getRGB());
53+
context.drawCenteredTextWithShadow(this.textRenderer, "Mods with bad implementation of DynamicHUD found!", this.width / 2, 35, Color.ORANGE.getRGB());
5454

5555
int y = 60;
5656
for (ModError error : modErrors) {

src/main/java/com/tanishisherewith/dynamichud/utils/DynamicValueRegistry.java

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import java.util.function.Supplier;
77

88
/**
9-
* This class is responsible for managing dynamic values for widgets.
10-
* It maintains a global and local registry of suppliers for these dynamic values.
9+
* A type-safe registry for managing dynamic values for widgets.
10+
* Supports both global and local registries with unique identifiers.
1111
* <p>
1212
* To use a local registry, simple create an object of the class.
1313
* <pre>
@@ -20,103 +20,107 @@
2020
* </p>
2121
*/
2222
public class DynamicValueRegistry {
23-
/**
24-
* A map that holds the global registry of suppliers.
25-
*
26-
* @see #localRegistry
27-
*/
23+
private static final Map<String, DynamicValueRegistry> REGISTRY_BY_ID = new HashMap<>();
2824
private static final Map<String, Supplier<?>> GLOBAL_REGISTRY = new HashMap<>();
2925

30-
/**
31-
* A map that holds the local registry of suppliers.
32-
*
33-
* @see #GLOBAL_REGISTRY
34-
*/
26+
private final String id; // Unique identifier for this registry instance
3527
private final Map<String, Supplier<?>> localRegistry = new HashMap<>();
3628

3729
/**
38-
* Constructor for the DynamicValueRegistry class.
39-
*
40-
* @param modId The ID of the mod for which this registry is being created. Doesn't need to be modId, it can simply be used as a standard unique identifier string.
30+
* Constructor for a local registry with a unique ID.
31+
* @param modId The mod ID or unique identifier for grouping registries.
32+
* @param registryId A unique ID for this registry instance.
33+
*/
34+
public DynamicValueRegistry(String modId, String registryId) {
35+
this.id = registryId;
36+
System.registerInstance(this, modId);
37+
REGISTRY_BY_ID.put(registryId, this);
38+
}
39+
/**
40+
* Constructor for a local registry using with registryId as modID.
41+
* @param modId The mod ID or unique identifier for grouping registries.
4142
*/
4243
public DynamicValueRegistry(String modId) {
43-
System.registerInstance(this,modId);
44+
this.id = modId;
45+
System.registerInstance(this, modId);
46+
REGISTRY_BY_ID.put(modId, this);
4447
}
4548

4649
/**
4750
* Registers a supplier in the global registry.
48-
*
49-
* @param key The key under which the supplier is to be registered.
50-
* @param supplier The supplier to be registered.
51+
* @param key The key for the supplier.
52+
* @param supplier The supplier providing values of type T.
5153
*/
52-
public static void registerGlobal(String key, Supplier<?> supplier) {
54+
public static <T> void registerGlobal(String key, Supplier<T> supplier) {
5355
GLOBAL_REGISTRY.put(key, supplier);
5456
}
5557

5658
/**
5759
* Retrieves a supplier from the global registry.
58-
*
59-
* @param key The key of the supplier to be retrieved.
60-
* @return The supplier registered under the given key, or null if no such supplier exists.
60+
* @param key The key of the supplier.
61+
* @return The supplier, or null if not found.
6162
*/
6263
public static Supplier<?> getGlobal(String key) {
6364
return GLOBAL_REGISTRY.get(key);
6465
}
6566

6667
/**
6768
* Registers a supplier in the local registry.
68-
*
69-
* @param key The key under which the supplier is to be registered.
70-
* @param supplier The supplier to be registered.
69+
* @param key The key for the supplier.
70+
* @param supplier The supplier providing values of type T.
7171
*/
7272
public void registerLocal(String key, Supplier<?> supplier) {
7373
localRegistry.put(key, supplier);
7474
}
7575

7676
/**
77-
* Retrieves a supplier from the local registry, falling back to the global registry if necessary.
78-
*
79-
* @param key The key of the supplier to be retrieved.
80-
* @return The supplier registered under the given key, or null if no such supplier exists.
77+
* Retrieves a supplier from the local or global registry.
78+
* @param key The key of the supplier.
79+
* @return The supplier, or null if not found.
8180
*/
8281
public Supplier<?> get(String key) {
83-
return localRegistry.getOrDefault(key, GLOBAL_REGISTRY.get(key));
82+
return localRegistry.getOrDefault(key, null);
8483
}
8584

8685
/**
87-
* Sets the local registry to the given map.
88-
*
89-
* @param map The map to be set as the local registry.
86+
* Gets the registry instance by its unique ID.
87+
* @param registryId The unique ID of the registry.
88+
* @return The registry instance, or null if not found.
9089
*/
91-
public void setLocalRegistry(Map<String, Supplier<?>> map) {
92-
localRegistry.clear();
93-
localRegistry.putAll(map);
90+
public static DynamicValueRegistry getById(String registryId) {
91+
return REGISTRY_BY_ID.get(registryId);
9492
}
93+
9594
/**
96-
* Retrieves all instances of DynamicValueRegistry for a specific mod ID.
97-
*
98-
* @param modId The mod ID to search for.
99-
* @return A list of DynamicValueRegistry instances, or an empty list if none exist.
95+
* Retrieves all registry instances for a mod ID.
96+
* @param modId The mod ID.
97+
* @return A list of registries for the mod.
10098
*/
10199
public static List<DynamicValueRegistry> getInstances(String modId) {
102100
return System.getInstances(DynamicValueRegistry.class, modId);
103101
}
104102

105103
/**
106104
* Removes a supplier from the global registry.
107-
*
108-
* @param key The key of the supplier to remove.
105+
* @param key The key of the supplier.
109106
*/
110107
public static void removeGlobal(String key) {
111108
GLOBAL_REGISTRY.remove(key);
112109
}
113110

114111
/**
115112
* Removes a supplier from the local registry.
116-
*
117-
* @param key The key of the supplier to remove.
113+
* @param key The key of the supplier.
118114
*/
119115
public void removeLocal(String key) {
120116
localRegistry.remove(key);
121117
}
122-
}
118+
119+
/**
120+
* Gets the unique ID of this registry.
121+
* @return The registry ID.
122+
*/
123+
public String getId() {
124+
return id;
125+
}
126+
}

src/main/java/com/tanishisherewith/dynamichud/utils/contextmenu/ContextMenu.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
import com.tanishisherewith.dynamichud.utils.contextmenu.contextmenuscreen.ContextMenuScreenRegistry;
99
import com.tanishisherewith.dynamichud.utils.contextmenu.contextmenuscreen.DefaultContextMenuScreenFactory;
1010
import com.tanishisherewith.dynamichud.utils.contextmenu.options.Option;
11+
import com.tanishisherewith.dynamichud.widget.WidgetBox;
1112
import net.minecraft.client.gui.DrawContext;
1213
import net.minecraft.client.gui.screen.Screen;
1314
import net.minecraft.util.math.MathHelper;
1415
import org.jetbrains.annotations.NotNull;
1516
import org.jetbrains.annotations.Nullable;
17+
import org.lwjgl.glfw.GLFW;
1618

1719
import java.awt.*;
1820
import java.util.ArrayList;
@@ -132,6 +134,12 @@ public void toggleDisplay() {
132134
}
133135
}
134136

137+
public void toggleDisplay(WidgetBox widgetBox,double mouseX, double mouseY, int button){
138+
if (button == GLFW.GLFW_MOUSE_BUTTON_RIGHT && widgetBox.isMouseOver(mouseX, mouseY)) {
139+
toggleDisplay();
140+
}
141+
}
142+
135143
public void resetAllOptions(){
136144
for(Option<?> option: options){
137145
option.reset();

src/main/java/com/tanishisherewith/dynamichud/utils/contextmenu/options/ColorOption.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class ColorOption extends Option<Color> {
1313
private ContextMenu<?> parentMenu = null;
1414
private ColorGradient colorGradient = null;
1515

16-
public ColorOption(Text name, ContextMenu<?> parentMenu, Supplier<Color> getter, Consumer<Color> setter) {
16+
public ColorOption(Text name, Supplier<Color> getter, Consumer<Color> setter,ContextMenu<?> parentMenu) {
1717
super(name,getter, setter);
1818
this.parentMenu = parentMenu;
1919
this.colorGradient = new ColorGradient(x + this.parentMenu.getWidth(), y - 10, get(), this::set, 50, 100);

0 commit comments

Comments
 (0)