Skip to content

Commit

Permalink
SubMenuOption, some changes and javadocs
Browse files Browse the repository at this point in the history
  • Loading branch information
tanishisherewithhh committed Apr 11, 2024
1 parent 3ed5a37 commit 9ac1100
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ public void onInitializeClient() {
printInfo("Initialising DynamicHud");

// Add WidgetData of included widgets
WidgetManager.addWidgetDatas(
WidgetManager.registerCustomWidgets(
TextWidget.DATA
);

//YACL load
GlobalConfig.HANDLER.load();

Expand All @@ -81,12 +82,16 @@ public void onInitializeClient() {
.forEach(entrypoint -> {
ModMetadata metadata = entrypoint.getProvider().getMetadata();
String modId = metadata.getId();
printInfo(String.format("Supported mod with id %s was found!", modId));
AbstractMoveableScreen screen;
KeyBinding binding;
WidgetRenderer widgetRenderer;
File widgetsFile;
try {
DynamicHudIntegration DHIntegration = entrypoint.getEntrypoint();
DHIntegration.init();

File widgetsFile = DHIntegration.getWidgetsFile();
widgetsFile = DHIntegration.getWidgetsFile();

if (widgetsFile.exists()) {
WidgetManager.loadWidgets(widgetsFile);
Expand All @@ -97,11 +102,11 @@ public void onInitializeClient() {

screen = DHIntegration.getMovableScreen();

KeyBinding binding = DHIntegration.getKeyBind();
binding = DHIntegration.getKeyBind();

DHIntegration.registerCustomWidgets();

WidgetRenderer widgetRenderer = DHIntegration.getWidgetRenderer();
widgetRenderer = DHIntegration.getWidgetRenderer();
addWidgetRenderer(widgetRenderer);

List<Widget> widgets = fileMap.get(widgetsFile.getName());
Expand Down Expand Up @@ -139,7 +144,6 @@ public void onInitializeClient() {
ServerLifecycleEvents.END_DATA_PACK_RELOAD.register((server, resourceManager, s) -> GlobalConfig.HANDLER.save());
ServerPlayConnectionEvents.DISCONNECT.register((handler, packetSender) -> GlobalConfig.HANDLER.save());
Runtime.getRuntime().addShutdownHook(new Thread(() -> GlobalConfig.HANDLER.save()));
GlobalConfig.HANDLER.save();

HudRenderCallback.EVENT.register(new HudRender());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public void addWidgets() {
WidgetManager.addWidget(Example2Widget);
}

@Override
public void registerCustomWidgets() {
//WidgetManager.addWidgetData(MyWidget.DATA);
}

public void initAfter() {
List<Widget> widgets = WidgetManager.getWidgetsForMod(DynamicHUD.MOD_ID);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import java.io.File;
import java.util.List;

public class DynamicHudTestTWO implements DynamicHudIntegration {
public class DynamicHudTestTwo implements DynamicHudIntegration {
TextWidget exampleWidget;
WidgetRenderer renderer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
import net.minecraft.client.gui.DrawContext;

/**
* Using the fabric event {@link HudRenderCallback} to render widgets in the game HUD.
* Mouse positions are passed in the negatives even though theoretically it's in the centre of the screen.
*/
public class HudRender implements HudRenderCallback {
@Override
public void onHudRender(DrawContext drawContext, float tickDelta) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.options;

import com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.ContextMenu;
import com.tanishisherewith.dynamichud.newTrial.utils.contextmenu.Option;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.text.Text;
import org.jetbrains.annotations.NotNull;

import java.awt.*;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
* SubMenu option displays a sub menu beside a boolean-like button.
* <p>
* The {@link #getter} gets a boolean value to display/close the subMenu by default.
* <p>
* The {@link #setter} returns a boolean value depending on if the subMenu is visible or not
*/
public class SubMenuOption extends Option<Boolean> {
private final ContextMenu subMenu;
private final ContextMenu parentMenu;
public String name = "Empty";


public SubMenuOption(String name, @NotNull ContextMenu parentMenu, Supplier<Boolean> getter, Consumer<Boolean> setter) {
super(getter, setter);
Objects.requireNonNull(parentMenu,"Parent Menu cannot be null");
this.name = name;
this.parentMenu = parentMenu;
this.subMenu = new ContextMenu(parentMenu.x + parentMenu.width,this.y);
this.subMenu.heightOffset = 0;
this.subMenu.shouldDisplay = get();
}

@Override
public void render(DrawContext drawContext, int x, int y) {
super.render(drawContext, x, y);

int color = value ? Color.GREEN.getRGB() : Color.RED.getRGB();
drawContext.drawText(mc.textRenderer, Text.of(name), x, y, color, false);

subMenu.render(drawContext, this.x + parentMenu.width, this.y, 0);
}

@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
if (super.mouseClicked(mouseX, mouseY, button)) {
subMenu.toggleDisplay();
set(subMenu.shouldDisplay);
return true;
}
subMenu.mouseClicked(mouseX, mouseY, button);
return false;
}

@Override
public boolean mouseReleased(double mouseX, double mouseY, int button) {
subMenu.mouseReleased(mouseX, mouseY, button);
return super.mouseReleased(mouseX, mouseY, button);
}

@Override
public boolean mouseDragged(double mouseX, double mouseY, int button) {
subMenu.mouseDragged(mouseX, mouseY, button);
return super.mouseDragged(mouseX, mouseY, button);
}

public SubMenuOption getOption(){
return this;
}

public ContextMenu getSubMenu() {
return subMenu;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,32 @@ public void render(DrawContext drawContext, int x1, int y1) {
}
gradientSlider.render(drawContext, x + 30, y + client.textRenderer.fontHeight + 4);
gradientBox.render(drawContext, x + 30, y + client.textRenderer.fontHeight + gradientSlider.getHeight() + 10);
colorPickerButton.render(drawContext, x + 55 + boxSize, y + client.textRenderer.fontHeight + gradientSlider.getHeight() + 8);
colorPickerButton.render(drawContext, x + 54 + boxSize, y + client.textRenderer.fontHeight + gradientSlider.getHeight() + 8);
alphaSlider.render(drawContext, x + 40 + boxSize, y + client.textRenderer.fontHeight + gradientSlider.getHeight() + 10);

if (colorPickerButton.isPicking()) {
// Draw the cursor
// Draw the preview box near cursor

//Translate cursor screen position to minecraft's scaled window
double mouseX = client.mouse.getX() * client.getWindow().getScaledWidth() / (double) client.getWindow().getWidth();
double mouseY = client.mouse.getY() * client.getWindow().getScaledHeight() / (double) client.getWindow().getHeight();

Framebuffer framebuffer = client.getFramebuffer();
int x = (int) (mouseX * framebuffer.textureWidth / client.getWindow().getScaledWidth());
int y = (int) ((client.getWindow().getScaledHeight() - mouseY) * framebuffer.textureHeight / client.getWindow().getScaledHeight());

//Read the pixel color at x,y pos to buffer
ByteBuffer buffer = GlAllocationUtils.allocateByteBuffer(4);
GL11.glReadPixels(x, y, 1, 1, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);
int red = buffer.get(0) & 0xFF;
int green = buffer.get(1) & 0xFF;
int blue = buffer.get(2) & 0xFF;

drawContext.getMatrices().push();
drawContext.getMatrices().translate(0,0,500);
drawContext.fill((int) mouseX + 10, (int) mouseY, (int) mouseX + 26, (int) mouseY + 16, -1);
drawContext.fill((int) mouseX + 11, (int) mouseY + 1, (int) mouseX + 25, (int) mouseY + 15, (red << 16) | (green << 8) | blue | 0xFF000000);
drawContext.getMatrices().pop();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class WidgetManager {
*
* @param data The WidgetData object to add.
*/
public static void addWidgetData(WidgetData<?> data) {
public static void registerCustomWidget(WidgetData<?> data) {
widgetDataMap.put(data.name(), data);
}

Expand All @@ -46,7 +46,7 @@ public static void addWidgetData(WidgetData<?> data) {
*
* @param widgetDatas The WidgetData objects to add.
*/
public static void addWidgetDatas(WidgetData<?>... widgetDatas) {
public static void registerCustomWidgets(WidgetData<?>... widgetDatas) {
for (WidgetData<?> data : widgetDatas) {
widgetDataMap.put(data.name(), data);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import java.util.function.Supplier;

public class TextWidget extends Widget {
public Color textColor; public static WidgetData<TextWidget> DATA = new WidgetData<>("TextWidget", "Display Text on screen", TextWidget::new);
public Color textColor;
public static WidgetData<TextWidget> DATA = new WidgetData<>("TextWidget", "Display Text on screen", TextWidget::new);
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
Expand Down Expand Up @@ -95,7 +96,7 @@ public void renderWidget(DrawContext drawContext, int mouseX, int mouseY) {
drawContext.drawText(mc.textRenderer, text, getX() + 2, getY() + 2, color, shadow);
widgetBox.setSizeAndPosition(getX(), getY(), mc.textRenderer.getWidth(text) + 3, mc.textRenderer.fontHeight + 2, this.shouldScale, GlobalConfig.get().scale);
}
menu.render(drawContext, getX() - 2, getY(), (int) Math.ceil(getHeight()));
menu.render(drawContext, getX(), getY(), (int) Math.ceil(getHeight()));
}

@Override
Expand Down Expand Up @@ -149,9 +150,10 @@ public void readFromTag(NbtCompound tag) {

// If true then it means that we should use local registry and if false (i.e. null) then use global registry
boolean dvrObj = tag.getBoolean("DynamicValueRegistry");

System.out.println(dvrObj);
if (!dvrObj) {
this.textSupplier = (Supplier<String>) DynamicValueRegistry.getGlobal(dynamicRegistryKey);
System.out.println(textSupplier);
return;
}

Expand Down

0 comments on commit 9ac1100

Please sign in to comment.