Skip to content

Commit

Permalink
Goggle Tooltip code cleanup and ModernUI compat (#6477)
Browse files Browse the repository at this point in the history
* Goggle Tooltip code cleanup and ModernUI compat
fixes #6404

* Calculate indents based on font
thanks @Tidy-Bear

* exactPositioning workaround

---------

Co-authored-by: zelophed <github@zelophed.me>
  • Loading branch information
Attack8 and zelophed authored Jul 15, 2024
1 parent 3c9c668 commit a5dc53b
Show file tree
Hide file tree
Showing 14 changed files with 104 additions and 60 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ dependencies {
// runtimeOnly fg.deobf("maven.modrinth:spark:1.10.38-forge")
//runtimeOnly fg.deobf("curse.maven:forbidden-arcanus-309858:4729924")
//runtimeOnly fg.deobf("curse.maven:valhelsia-core-416935:3886212")
// implementation fg.deobf("curse.maven:modern-ui-352491:5229350")
// runtimeOnly fg.deobf("curse.maven:sophisticated-storage-619320:5194748")
// runtimeOnly fg.deobf("curse.maven:sophisticated-core-618298:5296312")
// runtimeOnly fg.deobf("curse.maven:functional-storage-556861:5271589")
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/simibubi/create/compat/Mods.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public enum Mods {
SOPHISTICATEDSTORAGE,
STORAGEDRAWERS,
TCONSTRUCT,
XLPACKETS;
XLPACKETS,
MODERNUI;

private final String id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@ default boolean addExceptionToTooltip(List<Component> tooltip) {
if (!tooltip.isEmpty())
tooltip.add(Components.immutableEmpty());

tooltip.add(IHaveGoggleInformation.componentSpacing.plainCopy()
.append(Lang.translateDirect("gui.assembly.exception")
.withStyle(ChatFormatting.GOLD)));
Lang.translate("gui.assembly.exception").style(ChatFormatting.GOLD)
.forGoggles(tooltip);

String text = e.component.getString();
Arrays.stream(text.split("\n"))
.forEach(l -> TooltipHelper.cutStringTextComponent(l, Palette.GRAY_AND_WHITE)
.forEach(c -> tooltip.add(IHaveGoggleInformation.componentSpacing.plainCopy()
.append(c))));
.forEach(c -> Lang.text(c.getString()).forGoggles(tooltip)));

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ public ElevatorContactEditPacket(FriendlyByteBuf buffer) {
@Override
protected void writeSettings(FriendlyByteBuf buffer) {
buffer.writeUtf(shortName, 4);
buffer.writeUtf(longName, 30);
buffer.writeUtf(longName, 90);
buffer.writeVarInt(doorControl.ordinal());
}

@Override
protected void readSettings(FriendlyByteBuf buffer) {
shortName = buffer.readUtf(4);
longName = buffer.readUtf(30);
longName = buffer.readUtf(90);
doorControl = DoorControl.values()[Mth.clamp(buffer.readVarInt(), 0, DoorControl.values().length)];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ public boolean addToGoggleTooltip(List<Component> tooltip, boolean isPlayerSneak
String[] pitches = Lang.translateDirect("generic.notes")
.getString()
.split(";");
MutableComponent textComponent = Components.literal(spacing);
tooltip.add(textComponent.append(Lang.translateDirect("generic.pitch", pitches[pitch % pitches.length])));
Lang.translate("generic.pitch", pitches[pitch % pitches.length]).forGoggles(tooltip);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import java.util.List;
import java.util.Map;

import com.mojang.blaze3d.platform.Window;
import com.mojang.blaze3d.vertex.PoseStack;
import com.simibubi.create.AllBlocks;
import com.simibubi.create.AllItems;
import com.simibubi.create.CreateClient;
import com.simibubi.create.compat.Mods;
import com.simibubi.create.content.contraptions.IDisplayAssemblyExceptions;
import com.simibubi.create.content.contraptions.piston.MechanicalPistonBlock;
import com.simibubi.create.content.contraptions.piston.PistonExtensionPoleBlock;
Expand All @@ -16,6 +18,7 @@
import com.simibubi.create.foundation.gui.RemovedGuiUtils;
import com.simibubi.create.foundation.gui.Theme;
import com.simibubi.create.foundation.gui.element.GuiGameElement;
import com.simibubi.create.foundation.mixin.accessor.MouseHandlerAccessor;
import com.simibubi.create.foundation.outliner.Outline;
import com.simibubi.create.foundation.outliner.Outliner.OutlineEntry;
import com.simibubi.create.foundation.utility.Color;
Expand All @@ -26,6 +29,7 @@
import com.simibubi.create.infrastructure.config.CClient;

import net.minecraft.client.Minecraft;
import net.minecraft.client.MouseHandler;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
Expand Down Expand Up @@ -154,9 +158,8 @@ public static void renderOverlay(ForgeIngameGui gui, PoseStack poseStack, float
if (!tooltip.isEmpty())
tooltip.add(Components.immutableEmpty());

tooltip.add(IHaveGoggleInformation.componentSpacing.plainCopy()
.append(Lang.translateDirect("gui.goggles.pole_length"))
.append(Components.literal(" " + poles)));
Lang.translate("gui.goggles.pole_length").text(" " + poles)
.forGoggles(tooltip);
}

if (tooltip.isEmpty()) {
Expand Down Expand Up @@ -205,14 +208,44 @@ public static void renderOverlay(ForgeIngameGui gui, PoseStack poseStack, float
colorBorderBot.scaleAlpha(fade);
}

GuiGameElement.of(item)
.at(posX + 10, posY - 16, 450)
.render(poseStack);

if (!Mods.MODERNUI.isLoaded()) {
// default tooltip rendering when modernUI is not loaded
RemovedGuiUtils.drawHoveringText(poseStack, tooltip, posX, posY, width, height, -1, colorBackground.getRGB(),
colorBorderTop.getRGB(), colorBorderBot.getRGB(), mc.font);

poseStack.popPose();

return;
}

/*
* special handling for modernUI
*
* their tooltip handler causes the overlay to jiggle each frame,
* if the mouse is moving, guiScale is anything but 1 and exactPositioning is enabled
*
* this is a workaround to fix this behavior
*/
MouseHandler mouseHandler = Minecraft.getInstance().mouseHandler;
Window window = Minecraft.getInstance().getWindow();
double guiScale = window.getGuiScale();
double cursorX = mouseHandler.xpos();
double cursorY = mouseHandler.ypos();
((MouseHandlerAccessor) mouseHandler).create$setXPos(Math.round(cursorX / guiScale) * guiScale);
((MouseHandlerAccessor) mouseHandler).create$setYPos(Math.round(cursorY / guiScale) * guiScale);

RemovedGuiUtils.drawHoveringText(poseStack, tooltip, posX, posY, width, height, -1, colorBackground.getRGB(),
colorBorderTop.getRGB(), colorBorderBot.getRGB(), mc.font);

((MouseHandlerAccessor) mouseHandler).create$setXPos(cursorX);
((MouseHandlerAccessor) mouseHandler).create$setYPos(cursorY);

GuiGameElement.of(item)
.at(posX + 10, posY - 16, 450)
.render(poseStack);
poseStack.popPose();

}

public static BlockPos proxiedOverlayPosition(Level level, BlockPos pos) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,13 @@ public boolean addToGoggleTooltip(List<Component> tooltip, boolean isPlayerSneak
if (!isActive())
return false;

Component indent = Components.literal(IHaveGoggleInformation.spacing);
Component indent2 = Components.literal(IHaveGoggleInformation.spacing + " ");

calcMinMaxForSize(boilerSize);

tooltip.add(indent.plainCopy()
.append(
Lang.translateDirect("boiler.status", getHeatLevelTextComponent().withStyle(ChatFormatting.GREEN))));
tooltip.add(indent2.plainCopy()
.append(getSizeComponent(true, false)));
tooltip.add(indent2.plainCopy()
.append(getWaterComponent(true, false)));
tooltip.add(indent2.plainCopy()
.append(getHeatComponent(true, false)));
Lang.translate("boiler.status", getHeatLevelTextComponent().withStyle(ChatFormatting.GREEN))
.forGoggles(tooltip);
Lang.text(getSizeComponent(true, false).getString()).forGoggles(tooltip, 1);
Lang.text(getWaterComponent(true, false).getString()).forGoggles(tooltip, 1);
Lang.text(getHeatComponent(true, false).getString()).forGoggles(tooltip, 1);

if (attachedEngines == 0)
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void tick() {

@Override
public boolean addToGoggleTooltip(List<Component> tooltip, boolean isPlayerSneaking) {
tooltip.add(componentSpacing.plainCopy().append(Lang.translateDirect("gui.gauge.info_header")));
Lang.translate("gui.gauge.info_header").forGoggles(tooltip);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -784,20 +784,13 @@ public boolean addToGoggleTooltip(List<Component> tooltip, boolean isPlayerSneak
if (allStacks.isEmpty())
return false;

tooltip.add(componentSpacing.plainCopy()
.append(Lang.translateDirect("tooltip.brass_tunnel.contains"))
.withStyle(ChatFormatting.WHITE));
Lang.translate("tooltip.brass_tunnel.contains").style(ChatFormatting.WHITE).forGoggles(tooltip);
for (ItemStack item : allStacks) {
tooltip.add(componentSpacing.plainCopy()
.append(Lang.translateDirect("tooltip.brass_tunnel.contains_entry",
Components.translatable(item.getDescriptionId())
.getString(),
item.getCount()))
.withStyle(ChatFormatting.GRAY));
Lang.translate("tooltip.brass_tunnel.contains_entry",
Components.translatable(item.getDescriptionId()).getString(), item.getCount())
.style(ChatFormatting.GRAY).forGoggles(tooltip);
}
tooltip.add(componentSpacing.plainCopy()
.append(Lang.translateDirect("tooltip.brass_tunnel.retrieve"))
.withStyle(ChatFormatting.DARK_GRAY));
Lang.translate("tooltip.brass_tunnel.retrieve").style(ChatFormatting.DARK_GRAY).forGoggles(tooltip);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void changeState(boolean back) {

@Override
public boolean addToGoggleTooltip(List<Component> tooltip, boolean isPlayerSneaking) {
tooltip.add(componentSpacing.plainCopy().append(Lang.translateDirect("tooltip.analogStrength", this.state)));
Lang.translate("tooltip.analogStrength", this.state).forGoggles(tooltip);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.List;

import com.google.common.base.Strings;
import com.simibubi.create.content.equipment.goggles.IHaveGoggleInformation;
import com.simibubi.create.foundation.utility.Components;
import com.simibubi.create.foundation.utility.Couple;
import com.simibubi.create.foundation.utility.Lang;
Expand All @@ -30,15 +29,11 @@ public static MutableComponent holdShift(Palette palette, boolean highlighted) {
}

public static void addHint(List<Component> tooltip, String hintKey, Object... messageParams) {
Component spacing = IHaveGoggleInformation.componentSpacing;
tooltip.add(spacing.plainCopy()
.append(Lang.translateDirect(hintKey + ".title"))
.withStyle(ChatFormatting.GOLD));
Lang.translate(hintKey + ".title").style(ChatFormatting.GOLD).forGoggles(tooltip);
Component hint = Lang.translateDirect(hintKey);
List<Component> cutComponent = cutTextComponent(hint, Palette.GRAY_AND_WHITE);
for (Component component : cutComponent)
tooltip.add(spacing.plainCopy()
.append(component));
Lang.text(component.getString()).forGoggles(tooltip);
}

public static String makeProgressBar(int length, int filledLength) {
Expand All @@ -54,7 +49,7 @@ public static String makeProgressBar(int length, int filledLength) {
public static Style styleFromColor(ChatFormatting color) {
return Style.EMPTY.applyFormat(color);
}

public static Style styleFromColor(int hex) {
return Style.EMPTY.withColor(hex);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.simibubi.create.foundation.mixin.accessor;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

import net.minecraft.client.MouseHandler;

@Mixin(MouseHandler.class)
public interface MouseHandlerAccessor {

@Accessor("xpos")
void create$setXPos(double xPos);

@Accessor("ypos")
void create$setYPos(double yPos);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

import java.util.List;

import com.simibubi.create.compat.Mods;

import joptsimple.internal.Strings;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.player.Player;

public class LangBuilder {
Expand All @@ -29,7 +34,7 @@ public LangBuilder newLine() {
* Appends a localised component<br>
* To add an independently formatted localised component, use add() and a nested
* builder
*
*
* @param langKey
* @param args
* @return
Expand All @@ -40,7 +45,7 @@ public LangBuilder translate(String langKey, Object... args) {

/**
* Appends a text component
*
*
* @param literalText
* @return
*/
Expand All @@ -50,7 +55,7 @@ public LangBuilder text(String literalText) {

/**
* Appends a colored text component
*
*
* @param format
* @param literalText
* @return
Expand All @@ -61,7 +66,7 @@ public LangBuilder text(ChatFormatting format, String literalText) {

/**
* Appends a colored text component
*
*
* @param color
* @param literalText
* @return
Expand All @@ -72,7 +77,7 @@ public LangBuilder text(int color, String literalText) {

/**
* Appends the contents of another builder
*
*
* @param otherBuilder
* @return
*/
Expand All @@ -82,7 +87,7 @@ public LangBuilder add(LangBuilder otherBuilder) {

/**
* Appends a component
*
*
* @param customComponent
* @return
*/
Expand All @@ -95,7 +100,7 @@ public LangBuilder add(MutableComponent customComponent) {

/**
* Applies the format to all added components
*
*
* @param format
* @return
*/
Expand All @@ -107,7 +112,7 @@ public LangBuilder style(ChatFormatting format) {

/**
* Applies the color to all added components
*
*
* @param color
* @return
*/
Expand Down Expand Up @@ -150,16 +155,25 @@ public void forGoggles(List<? super MutableComponent> tooltip) {

public void forGoggles(List<? super MutableComponent> tooltip, int indents) {
tooltip.add(Lang.builder()
.text(Strings.repeat(' ', 4 + indents))
.text(Strings.repeat(' ', getIndents(Minecraft.getInstance().font, 4 + indents)))
.add(this)
.component());
}

public static final float DEFAULT_SPACE_WIDTH = 4.0F; // space width in vanilla's default font
static int getIndents(Font font, int defaultIndents) {
int spaceWidth = font.width(" ");
if (DEFAULT_SPACE_WIDTH == spaceWidth) {
return defaultIndents;
}
return Mth.ceil(DEFAULT_SPACE_WIDTH * defaultIndents / spaceWidth);
}

//

private void assertComponent() {
if (component == null)
throw new IllegalStateException("No components were added to builder");
}

}
}
1 change: 1 addition & 0 deletions src/main/resources/create.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"accessor.AgeableListModelAccessor",
"accessor.GameRendererAccessor",
"accessor.HumanoidArmorLayerAccessor",
"accessor.MouseHandlerAccessor",
"accessor.ParticleEngineAccessor",
"client.BlockDestructionProgressMixin",
"client.CameraMixin",
Expand Down

0 comments on commit a5dc53b

Please sign in to comment.