Skip to content

Commit

Permalink
Few changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tanishisherewithhh committed May 29, 2024
1 parent 0a9c7c2 commit 3038f79
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import org.jetbrains.annotations.NotNull;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.system.NonnullDefault;

import java.io.File;

Expand Down Expand Up @@ -85,6 +85,9 @@ public interface DynamicHudIntegration {
* WidgetManager.registerCustomWidget(TextWidget.DATA);
* }
* </pre>
*
* Custom widgets can be registered in any method in the interface
* but to avoid any errors and mishaps it is recommended you add them here
*/
default void registerCustomWidgets() {
}
Expand Down Expand Up @@ -118,10 +121,15 @@ default KeyBinding getKeyBind() {

/**
* Returns the movable screen for the DynamicHud.
* <p>
* <h3>
* !! Should never be null !!
* </h3>
*</p>
*
* @return The movable screen.
*/
@NotNull AbstractMoveableScreen getMovableScreen();
AbstractMoveableScreen getMovableScreen();

/**
* To return a {@link WidgetRenderer} object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
import com.tanishisherewith.dynamichud.widget.WidgetRenderer;
import com.tanishisherewith.dynamichud.widgets.TextWidget;
import net.minecraft.client.gui.screen.TitleScreen;
import net.minecraft.client.gui.screen.multiplayer.MultiplayerScreen;
import net.minecraft.text.Text;
import org.jetbrains.annotations.NotNull;

import java.util.List;

Expand All @@ -27,7 +25,7 @@ public void init() {

//Local registry
registry = new DynamicValueRegistry(DynamicHUD.MOD_ID);
registry.registerLocal("Hello", () -> "Hello!");
registry.registerLocal("Hello", () -> "Hello " + DynamicHUD.MC.getSession().getUsername() + "!");
registry.registerLocal("DynamicHUD", () -> "DynamicHUD");


Expand Down Expand Up @@ -82,11 +80,13 @@ public void initAfter() {

renderer = new WidgetRenderer(widgets);
renderer.shouldRenderInGameHud(true);

//This will make widgets render in the titlescreen as well.
renderer.addScreen(TitleScreen.class);
}

@Override
public @NotNull AbstractMoveableScreen getMovableScreen() {
public AbstractMoveableScreen getMovableScreen() {
return new AbstractMoveableScreen(Text.literal("Editor Screen"), renderer) {
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public final class GlobalConfig {
@SerialEntry
private float scale = 1.0f;

@SerialEntry
private boolean displayDescriptions = false;

@SerialEntry
private boolean showColorPickerPreview = true;

Expand Down Expand Up @@ -54,6 +57,12 @@ public final Screen createYACLGUI() {
.binding(true, () -> this.showColorPickerPreview, newVal -> this.showColorPickerPreview = newVal)
.controller(booleanOption -> BooleanControllerBuilder.create(booleanOption).yesNoFormatter())
.build())
.option(Option.<Boolean>createBuilder()
.name(Text.literal("Show widget descriptions/tooltips"))
.description(OptionDescription.of(Text.literal("Shows the description of widgets as tooltips.")))
.binding(true, () -> this.displayDescriptions, newVal -> this.displayDescriptions = newVal)
.controller(booleanOption -> BooleanControllerBuilder.create(booleanOption).yesNoFormatter())
.build())
.build())
.build())
.build()
Expand All @@ -66,4 +75,8 @@ public float getScale(){
public boolean showColorPickerPreview() {
return showColorPickerPreview;
}

public boolean shouldDisplayDescriptions() {
return displayDescriptions;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tanishisherewith.dynamichud.screens;

import com.tanishisherewith.dynamichud.config.GlobalConfig;
import com.tanishisherewith.dynamichud.widget.Widget;
import com.tanishisherewith.dynamichud.widget.WidgetRenderer;
import net.minecraft.client.gui.DrawContext;
Expand All @@ -9,8 +10,6 @@
public abstract class AbstractMoveableScreen extends Screen {
public final WidgetRenderer widgetRenderer;
public int snapSize = 100;
protected boolean shouldPause = false; // To pause if the screen is opened or not

/**
* Constructs a AbstractMoveableScreen object.
*/
Expand Down Expand Up @@ -81,6 +80,10 @@ public void render(DrawContext drawContext, int mouseX, int mouseY, float delta)

// Draw each widget
widgetRenderer.renderWidgets(drawContext, mouseX, mouseY);

if(widgetRenderer.selectedWidget != null && GlobalConfig.get().shouldDisplayDescriptions()){
drawContext.drawTooltip(client.textRenderer,Text.of(widgetRenderer.selectedWidget.DATA.description()),mouseX,mouseY);
}
}
public void handleClickOnWidget(Widget widget, double mouseX, double mouseY, int button){

Expand All @@ -93,13 +96,9 @@ public void close() {
super.close();
}

public void setShouldPause(boolean shouldPause) {
this.shouldPause = shouldPause;
}

@Override
public boolean shouldPause() {
return shouldPause;
return false;
}

public void setSnapSize(int size) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ContextMenu {
public int finalWidth = 0;
public int height = 0;
public Color backgroundColor = new Color(107, 112, 126, 124);
private Color darkerBorderColor = backgroundColor.darker().darker().darker().darker().darker().darker();
private final Color darkerBorderColor = backgroundColor.darker().darker().darker().darker().darker().darker();
//Todo: Add padding around the rectangle instead of just one side.
public int padding = 5; // The amount of padding around the rectangle
public int heightOffset = 4; // Height offset from the widget
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public ColorOption(String name, ContextMenu parentMenu, Supplier<Color> getter,
super(getter, setter);
this.name = name;
this.parentMenu = parentMenu;
System.out.println(get());
colorPicker = new ColorGradientPicker(x + this.parentMenu.finalWidth, y - 10, get(), this::set, 50, 100);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.tanishisherewith.dynamichud.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;
Expand All @@ -24,7 +23,7 @@ public class SubMenuOption extends Option<Boolean> {
public String name = "Empty";


public SubMenuOption(String name, @NotNull ContextMenu parentMenu, Supplier<Boolean> getter, Consumer<Boolean> setter) {
public SubMenuOption(String name, ContextMenu parentMenu, Supplier<Boolean> getter, Consumer<Boolean> setter) {
super(getter, setter);
Objects.requireNonNull(parentMenu, "Parent Menu cannot be null");
this.name = name;
Expand Down
15 changes: 4 additions & 11 deletions src/main/java/com/tanishisherewith/dynamichud/widget/Widget.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ public final void render(DrawContext drawContext, int mouseX, int mouseY) {
* Renders the widget on the editor screen.
*/
public final void renderInEditor(DrawContext drawContext, int mouseX, int mouseY) {
if(!isInEditor) return;

displayBg(drawContext);

if (shouldScale) {
Expand Down Expand Up @@ -255,7 +257,6 @@ public void onClose() {
* Displays a faint grayish background if enabled or faint reddish background if disabled.
* Drawn with 2 pixel offset to all sides
*
* @param context
*/
protected void displayBg(DrawContext context) {
int backgroundColor = this.shouldDisplay() ? ColorHelper.getColor(0, 0, 0, 128) : ColorHelper.getColor(255, 0, 0, 128);
Expand Down Expand Up @@ -346,23 +347,15 @@ public abstract static class WidgetBuilder<T, S> {


/**
* X Position of the widget relative to the screen.
* Should be between 0f - 1f
*
* @param x
* @return Builder
* X Position of the widget of the scaled screen.
*/
public T setX(int x) {
this.x = x;
return self();
}

/**
* Y Position of the widget relative to the screen.
* Should be between 0f - 1f
*
* @param y
* @return
* Y Position of the widget of the scaled screen.
*/
public T setY(int y) {
this.y = y;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public static void removeWidget(Widget widget) {
*/
public static void onScreenResized(int newWidth, int newHeight, int previousWidth, int previousHeight) {
for (Widget widget : widgets) {
// To ensure that infinite coordinates is not returned
// To ensure that infinite coordinates is not returned for the first time its resized.
if (widget.xPercent <= 0.0f) {
widget.xPercent = (float) widget.getX() / previousWidth;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,24 @@ public void renderWidgets(DrawContext context, int mouseX, int mouseY) {

Screen currentScreen = DynamicHUD.MC.currentScreen;

//Render in game hud
if (currentScreen == null && renderInGameHud) {
for (Widget widget : widgets) {
widget.isInEditor = false;
widget.render(context, 0, 0);
}
return;
}

//Render in editing screen
if (currentScreen instanceof AbstractMoveableScreen) {
for (Widget widget : widgets) {
widget.isInEditor = true;
widget.renderInEditor(context, mouseX, mouseY);
}
return;
}
//Render in any other screen
if (currentScreen != null && allowedScreens.contains(DynamicHUD.MC.currentScreen.getClass()) && !this.isInEditor) {
for (Widget widget : widgets) {
widget.isInEditor = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public void readFromTag(NbtCompound tag) {
public void setItemStack(ItemStack item) {
this.item = item;
}

public static class Builder extends WidgetBuilder<Builder,ItemWidget>{
ItemStack itemStack;
public Builder setItemStack(ItemStack itemStack) {
Expand Down

0 comments on commit 3038f79

Please sign in to comment.