Goggle Tooltip code cleanup and ModernUI compat (#6477)

* Goggle Tooltip code cleanup and ModernUI compat
fixes Creators-of-Create#6404

* Calculate indents based on font
thanks @Tidy-Bear

* exactPositioning workaround

---------

Co-authored-by: zelophed <github@zelophed.me>
This commit is contained in:
attackeight 2024-07-15 11:14:56 -04:00 committed by GitHub
parent 3c9c668b56
commit a5dc53ba5f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 105 additions and 61 deletions

View File

@ -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")

View File

@ -25,7 +25,8 @@ public enum Mods {
SOPHISTICATEDSTORAGE,
STORAGEDRAWERS,
TCONSTRUCT,
XLPACKETS;
XLPACKETS,
MODERNUI;
private final String id;

View File

@ -22,15 +22,13 @@ public interface IDisplayAssemblyExceptions {
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;
}

View File

@ -27,14 +27,14 @@ public class ElevatorContactEditPacket extends BlockEntityConfigurationPacket<El
@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)];
}

View File

@ -112,8 +112,7 @@ public class WhistleBlockEntity extends SmartBlockEntity implements IHaveGoggleI
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;
}

View File

@ -4,10 +4,12 @@ import java.util.ArrayList;
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;
@ -16,6 +18,7 @@ import com.simibubi.create.foundation.blockEntity.behaviour.ValueBox;
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;
@ -26,6 +29,7 @@ import com.simibubi.create.infrastructure.config.AllConfigs;
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;
@ -154,9 +158,8 @@ public class GoggleOverlayRenderer {
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()) {
@ -205,14 +208,44 @@ public class GoggleOverlayRenderer {
colorBorderBot.scaleAlpha(fade);
}
RemovedGuiUtils.drawHoveringText(poseStack, tooltip, posX, posY, width, height, -1, colorBackground.getRGB(),
colorBorderTop.getRGB(), colorBorderBot.getRGB(), mc.font);
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);
poseStack.popPose();
}
public static BlockPos proxiedOverlayPosition(Level level, BlockPos pos) {

View File

@ -147,20 +147,13 @@ public class BoilerData {
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;

View File

@ -48,7 +48,7 @@ public abstract class GaugeBlockEntity extends KineticBlockEntity implements IHa
@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;
}

View File

@ -784,20 +784,13 @@ public class BrassTunnelBlockEntity extends BeltTunnelBlockEntity implements IHa
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;
}

View File

@ -79,7 +79,7 @@ public class AnalogLeverBlockEntity extends SmartBlockEntity implements IHaveGog
@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;
}

View File

@ -6,7 +6,6 @@ import java.util.LinkedList;
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;
@ -30,15 +29,11 @@ public class TooltipHelper {
}
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) {
@ -54,7 +49,7 @@ public class TooltipHelper {
public static Style styleFromColor(ChatFormatting color) {
return Style.EMPTY.applyFormat(color);
}
public static Style styleFromColor(int hex) {
return Style.EMPTY.withColor(hex);
}

View File

@ -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);
}

View File

@ -2,10 +2,15 @@ package com.simibubi.create.foundation.utility;
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 {
@ -29,7 +34,7 @@ public class LangBuilder {
* Appends a localised component<br>
* To add an independently formatted localised component, use add() and a nested
* builder
*
*
* @param langKey
* @param args
* @return
@ -40,7 +45,7 @@ public class LangBuilder {
/**
* Appends a text component
*
*
* @param literalText
* @return
*/
@ -50,7 +55,7 @@ public class LangBuilder {
/**
* Appends a colored text component
*
*
* @param format
* @param literalText
* @return
@ -61,7 +66,7 @@ public class LangBuilder {
/**
* Appends a colored text component
*
*
* @param color
* @param literalText
* @return
@ -72,7 +77,7 @@ public class LangBuilder {
/**
* Appends the contents of another builder
*
*
* @param otherBuilder
* @return
*/
@ -82,7 +87,7 @@ public class LangBuilder {
/**
* Appends a component
*
*
* @param customComponent
* @return
*/
@ -95,7 +100,7 @@ public class LangBuilder {
/**
* Applies the format to all added components
*
*
* @param format
* @return
*/
@ -107,7 +112,7 @@ public class LangBuilder {
/**
* Applies the color to all added components
*
*
* @param color
* @return
*/
@ -150,11 +155,20 @@ public class LangBuilder {
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() {
@ -162,4 +176,4 @@ public class LangBuilder {
throw new IllegalStateException("No components were added to builder");
}
}
}

View File

@ -29,6 +29,7 @@
"accessor.AgeableListModelAccessor",
"accessor.GameRendererAccessor",
"accessor.HumanoidArmorLayerAccessor",
"accessor.MouseHandlerAccessor",
"accessor.ParticleEngineAccessor",
"client.BlockDestructionProgressMixin",
"client.CameraMixin",