Skip to content

Commit

Permalink
Refactor TitleContainer and fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinthegreat1 committed Aug 21, 2024
1 parent 709657e commit 39eb3fb
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 180 deletions.
18 changes: 11 additions & 7 deletions src/main/java/de/hysky/skyblocker/config/HudConfigScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
* (i.e. one for dwarven mines, one for the end, etc.) See an implementation for an example.
*/
public abstract class HudConfigScreen extends Screen {
private final Screen parent;
private final List<HudWidget> widgets;
protected final Screen parent;
protected final List<HudWidget> widgets;

private HudWidget draggingWidget;
private double mouseClickRelativeX;
Expand Down Expand Up @@ -51,7 +51,7 @@ public HudConfigScreen(Text title, Screen parent, List<HudWidget> widgets) {
@Override
public final void render(DrawContext context, int mouseX, int mouseY, float delta) {
super.render(context, mouseX, mouseY, delta);
renderWidget(context, widgets);
renderWidget(context, widgets, delta);
context.drawCenteredTextWithShadow(textRenderer, "Right Click To Reset Position", width / 2, height / 2, Color.GRAY.getRGB());
}

Expand All @@ -60,7 +60,7 @@ public final void render(DrawContext context, int mouseX, int mouseY, float delt
* @param context the context to render in
* @param widgets the widgets to render
*/
protected void renderWidget(DrawContext context, List<HudWidget> widgets) {
protected void renderWidget(DrawContext context, List<HudWidget> widgets, float delta) {
for (HudWidget widget : widgets) {
widget.render(context);
}
Expand All @@ -69,7 +69,7 @@ protected void renderWidget(DrawContext context, List<HudWidget> widgets) {
@Override
public final boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) {
if (button == 0 && draggingWidget != null) {
draggingWidget.setX((int) Math.clamp(mouseX - mouseClickRelativeX, 0, this.width - draggingWidget.getWidth()));
draggingWidget.setX((int) Math.clamp(mouseX - mouseClickRelativeX, 0, this.width - draggingWidget.getWidth()) - getWidgetXOffset(draggingWidget));
draggingWidget.setY((int) Math.clamp(mouseY - mouseClickRelativeY, 0, this.height - draggingWidget.getHeight()));
}
return super.mouseDragged(mouseX, mouseY, button, deltaX, deltaY);
Expand All @@ -79,9 +79,9 @@ public final boolean mouseDragged(double mouseX, double mouseY, int button, doub
public final boolean mouseClicked(double mouseX, double mouseY, int button) {
if (button == 0) {
for (HudWidget widget : widgets) {
if (RenderHelper.pointIsInArea(mouseX, mouseY, widget.getX(), widget.getY(), widget.getX() + widget.getWidth(), widget.getY() + widget.getHeight())) {
if (RenderHelper.pointIsInArea(mouseX, mouseY, widget.getX() + getWidgetXOffset(widget), widget.getY(), widget.getX() + getWidgetXOffset(widget) + widget.getWidth(), widget.getY() + widget.getHeight())) {
draggingWidget = widget;
mouseClickRelativeX = mouseX - widget.getX();
mouseClickRelativeX = mouseX - widget.getX() - getWidgetXOffset(widget);
mouseClickRelativeY = mouseY - widget.getY();
break;
}
Expand All @@ -98,6 +98,10 @@ public final boolean mouseReleased(double mouseX, double mouseY, int button) {
return super.mouseReleased(mouseX, mouseY, button);
}

protected int getWidgetXOffset(HudWidget widget) {
return 0;
}

/**
* Resets the positions of the widgets to the positions in the config. Override to change the behavior.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,14 @@ public static class TitleContainer {
public int y = 10;

@SerialEntry
public Direction direction = Direction.HORIZONTAL;
public Direction direction = Direction.VERTICAL;

@SerialEntry
public Alignment alignment = Alignment.MIDDLE;

public float getRenderScale() {
return titleContainerScale * 0.03f;
}
}

public enum Direction {
Expand All @@ -113,7 +117,7 @@ public String toString() {
}

public enum Alignment {
LEFT, RIGHT, MIDDLE;
LEFT, MIDDLE, RIGHT;

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package de.hysky.skyblocker.skyblock.tabhud.widget;

import de.hysky.skyblocker.utils.Location;
import net.minecraft.client.gui.DrawContext;

public class EmptyWidget extends HudWidget {
public EmptyWidget() {
super("");
}

@Override
public boolean shouldRender(Location location) {
return false;
}

@Override
public void update() {}

@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.hysky.skyblocker.utils.render.title;

import com.demonwav.mcdev.annotations.Translatable;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
Expand All @@ -20,7 +21,7 @@ public class Title {
* @param textKey the translation key
* @param formatting the formatting to be applied to the text
*/
public Title(String textKey, Formatting formatting) {
public Title(@Translatable String textKey, Formatting formatting) {
this(Text.translatable(textKey).formatted(formatting));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,64 +87,46 @@ private static void render(DrawContext context, RenderTickCounter tickCounter) {
}

protected static void render(DrawContext context, Set<Title> titles, int xPos, int yPos, float tickDelta) {
var client = MinecraftClient.getInstance();
TextRenderer textRenderer = client.textRenderer;
TextRenderer textRenderer = MinecraftClient.getInstance().textRenderer;

// Calculate Scale to use
float scale = 3F * (SkyblockerConfigManager.get().uiAndVisuals.titleContainer.titleContainerScale / 100F);

float scale = SkyblockerConfigManager.get().uiAndVisuals.titleContainer.getRenderScale();
// Grab direction and alignment values
UIAndVisualsConfig.Direction direction = SkyblockerConfigManager.get().uiAndVisuals.titleContainer.direction;
UIAndVisualsConfig.Alignment alignment = SkyblockerConfigManager.get().uiAndVisuals.titleContainer.alignment;

// x/y refer to the starting position for the text
// If left or right aligned or middle aligned vertically, start at xPos, we will shift each text later
float x = xPos;
// y always starts at yPos
float x = 0;
float y = yPos;

//Calculate the width of combined text
float width = 0;
for (Title title : titles) {
width += textRenderer.getWidth(title.getText()) * scale + 10;
}

if (alignment == UIAndVisualsConfig.Alignment.MIDDLE) {
if (direction == UIAndVisualsConfig.Direction.HORIZONTAL) {
//If middle aligned horizontally, start the xPosition at half of the width to the left.
x = xPos - (width / 2);
} else {
//If middle aligned vertically, start at xPos, we will shift each text to the left later
x = xPos;
}
}
if (alignment == UIAndVisualsConfig.Alignment.LEFT || alignment == UIAndVisualsConfig.Alignment.RIGHT) {
//If left or right aligned, start at xPos, we will shift each text later
x = xPos;
// Calculate the width of combined text
float totalWidth = getWidth(textRenderer, titles);
if (alignment == UIAndVisualsConfig.Alignment.MIDDLE && direction == UIAndVisualsConfig.Direction.HORIZONTAL) {
// If middle aligned horizontally, start the xPosition at half of the width to the left.
x = xPos - totalWidth / 2;
}

for (Title title : titles) {

//Calculate which x the text should use
float xToUse;
if (direction == UIAndVisualsConfig.Direction.HORIZONTAL) {
xToUse = alignment == UIAndVisualsConfig.Alignment.RIGHT ?
x - (textRenderer.getWidth(title.getText()) * scale) : //if right aligned we need the text position to be aligned on the right side.
x;
} else {
xToUse = alignment == UIAndVisualsConfig.Alignment.MIDDLE ?
x - (textRenderer.getWidth(title.getText()) * scale) / 2 : //if middle aligned we need the text position to be aligned in the middle.
alignment == UIAndVisualsConfig.Alignment.RIGHT ?
x - (textRenderer.getWidth(title.getText()) * scale) : //if right aligned we need the text position to be aligned on the right side.
x;
float xTextLeft = x;
if (alignment == UIAndVisualsConfig.Alignment.RIGHT) {
//if right aligned we need the text position to be aligned on the right side.
xTextLeft = x - textRenderer.getWidth(title.getText()) * scale;
} else if (direction == UIAndVisualsConfig.Direction.VERTICAL && alignment == UIAndVisualsConfig.Alignment.MIDDLE) {
//if middle aligned we need the text position to be aligned in the middle.
xTextLeft = x - (textRenderer.getWidth(title.getText()) * scale) / 2;
}

//Start displaying the title at the correct position, not at the default position
if (title.isDefaultPos()) {
title.x = xToUse;
title.x = xTextLeft;
title.y = y;
}

//Lerp the texts x and y variables
title.x = MathHelper.lerp(tickDelta * 0.5F, title.x, xToUse);
title.x = MathHelper.lerp(tickDelta * 0.5F, title.x, xTextLeft);
title.y = MathHelper.lerp(tickDelta * 0.5F, title.y, y);

//Translate the matrix to the texts position and scale
Expand All @@ -160,17 +142,29 @@ protected static void render(DrawContext context, Set<Title> titles, int xPos, i
if (direction == UIAndVisualsConfig.Direction.HORIZONTAL) {
if (alignment == UIAndVisualsConfig.Alignment.MIDDLE || alignment == UIAndVisualsConfig.Alignment.LEFT) {
//Move to the right if middle or left aligned
x += textRenderer.getWidth(title.getText()) * scale + 10;
}

if (alignment == UIAndVisualsConfig.Alignment.RIGHT) {
x += (textRenderer.getWidth(title.getText()) + 10) * scale;
} else if (alignment == UIAndVisualsConfig.Alignment.RIGHT) {
//Move to the left if right aligned
x -= textRenderer.getWidth(title.getText()) * scale + 10;
x -= (textRenderer.getWidth(title.getText()) + 10) * scale;
}
} else {
//Y always moves by the same amount if vertical
y += textRenderer.fontHeight * scale + 10;
y += (textRenderer.fontHeight + 1) * scale;
}
}
}
}

protected static int getWidth(TextRenderer textRenderer, Set<Title> titles) {
float scale = SkyblockerConfigManager.get().uiAndVisuals.titleContainer.getRenderScale();
return SkyblockerConfigManager.get().uiAndVisuals.titleContainer.direction == UIAndVisualsConfig.Direction.HORIZONTAL ?
(int) ((titles.stream().map(Title::getText).mapToInt(textRenderer::getWidth).mapToDouble(width -> width + 10).sum() - 10) * scale) :
(int) (titles.stream().map(Title::getText).mapToInt(textRenderer::getWidth).max().orElse(0) * scale);
}

protected static int getHeight(TextRenderer textRenderer, Set<Title> titles) {
float scale = SkyblockerConfigManager.get().uiAndVisuals.titleContainer.getRenderScale();
return SkyblockerConfigManager.get().uiAndVisuals.titleContainer.direction == UIAndVisualsConfig.Direction.HORIZONTAL ?
(int) (textRenderer.fontHeight * scale) :
(int) ((textRenderer.fontHeight + 1) * titles.size() * scale);
}
}
Loading

0 comments on commit 39eb3fb

Please sign in to comment.