Skip to content

Commit

Permalink
add narration to tooltip components
Browse files Browse the repository at this point in the history
  • Loading branch information
deirn committed Jun 4, 2024
1 parent 09df45c commit b87ea0f
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 9 deletions.
9 changes: 9 additions & 0 deletions src/api/java/mcp/mobius/waila/api/ITooltipComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import mcp.mobius.waila.api.__internal__.ApiSide;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

/**
* The base type of all Waila tooltip components.
Expand Down Expand Up @@ -34,6 +36,13 @@ public interface ITooltipComponent {
*/
void render(GuiGraphics ctx, int x, int y, float delta);

/**
* Returns the message that will be read out loud when TTS is enabled.
*/
default @Nullable Component getNarration() {
return null;
}

/**
* A component that will grow in size relative to overall tooltip width.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import mcp.mobius.waila.api.ITooltipComponent;
import mcp.mobius.waila.api.__internal__.ApiSide;
import mcp.mobius.waila.buildconst.Tl;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import org.jetbrains.annotations.Nullable;

/**
* Component that renders an armor bar.
Expand Down Expand Up @@ -39,6 +42,11 @@ public int getHeight() {
return (Mth.positiveCeilDiv(iconCount, lineWidth) * 3) + 6;
}

@Override
public @Nullable Component getNarration() {
return Component.translatable(Tl.Tts.Component.ARMOR, armor);
}

@Override
public void render(GuiGraphics ctx, int x, int y, float delta) {
var filled = armor / 2 - 1;
Expand Down
6 changes: 6 additions & 0 deletions src/api/java/mcp/mobius/waila/api/component/BarComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.Nullable;

/**
* Component that renders a colored bar.
Expand Down Expand Up @@ -74,6 +75,11 @@ public int getHeight() {
return HEIGHT;
}

@Override
public @Nullable Component getNarration() {
return text;
}

@Override
public void render(GuiGraphics ctx, int x, int y, float delta) {
renderBar(ctx.pose(), x, y, WIDTH, V0_BG, U1, V1_BG, color);
Expand Down
11 changes: 10 additions & 1 deletion src/api/java/mcp/mobius/waila/api/component/HealthComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import mcp.mobius.waila.api.ITooltipComponent;
import mcp.mobius.waila.api.__internal__.ApiSide;
import mcp.mobius.waila.buildconst.Tl;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import org.jetbrains.annotations.Nullable;

/**
* Component that renders a health bar.
Expand All @@ -26,12 +29,13 @@ public class HealthComponent implements ITooltipComponent {
*/
public HealthComponent(float health, float maxHealth, int maxPerLine, boolean absorption) {
this.health = Mth.ceil(health);
this.maxHealth = Mth.ceil(maxHealth);
this.iconCount = Mth.positiveCeilDiv(Mth.ceil(Math.max(health, maxHealth)), 2);
this.lineWidth = Math.min(iconCount, maxPerLine);
this.absorption = absorption;
}

private final int health;
private final int health, maxHealth;
private final int iconCount;
private final int lineWidth;
private final boolean absorption;
Expand All @@ -46,6 +50,11 @@ public int getHeight() {
return (Mth.positiveCeilDiv(iconCount, lineWidth) * 3) + 6;
}

@Override
public @Nullable Component getNarration() {
return Component.translatable(Tl.Tts.Component.HEALTH, health, maxHealth);
}

@Override
public void render(GuiGraphics ctx, int x, int y, float delta) {
var filled = health / 2 - 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import mcp.mobius.waila.api.ITooltipComponent;
import mcp.mobius.waila.api.WailaHelper;
import mcp.mobius.waila.api.__internal__.ApiSide;
import mcp.mobius.waila.buildconst.Tl;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.ItemLike;
import org.jetbrains.annotations.Nullable;

/**
* Component that renders an {@link ItemStack}.
Expand Down Expand Up @@ -37,6 +40,11 @@ public int getHeight() {
return stack.isEmpty() ? 0 : 18;
}

@Override
public @Nullable Component getNarration() {
return Component.translatable(Tl.Tts.Component.ITEM, stack.getCount(), stack.getDisplayName());
}

@Override
public void render(GuiGraphics ctx, int x, int y, float delta) {
ctx.renderItem(stack, x + 1, y + 1);
Expand Down
16 changes: 16 additions & 0 deletions src/api/java/mcp/mobius/waila/api/component/ItemListComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

import mcp.mobius.waila.api.ITooltipComponent;
import mcp.mobius.waila.api.__internal__.ApiSide;
import mcp.mobius.waila.buildconst.Tl;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;
import net.minecraft.util.Mth;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.Nullable;

/**
* Component that renders items that dynamically grow based on available space.
Expand Down Expand Up @@ -42,6 +45,19 @@ public void setGrownWidth(int grownWidth) {
maxIndex = gridWidth * gridHeight - 1;
}

@Override
public @Nullable Component getNarration() {
if (items.isEmpty()) return null;

var narration = Component.empty();
for (var item : items) {
narration.append(Component.translatable(Tl.Tts.Component.ITEM, item.getCount(), item.getDisplayName()));
narration.append("\n");
}
narration.getSiblings().removeLast();
return narration;
}

@Override
public int getHeight() {
return gridHeight * 18;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
import mcp.mobius.waila.api.WailaHelper;
import mcp.mobius.waila.api.__internal__.ApiSide;
import mcp.mobius.waila.api.__internal__.IApiService;
import mcp.mobius.waila.buildconst.Tl;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.ItemLike;
import org.jetbrains.annotations.Nullable;

/**
* Component that renders an {@link ItemStack} with its name.
Expand Down Expand Up @@ -43,6 +46,11 @@ public int getHeight() {
return getFont().lineHeight;
}

@Override
public @Nullable Component getNarration() {
return Component.translatable(Tl.Tts.Component.ITEM, stack.getCount(), stack.getDisplayName());
}

@Override
public void render(GuiGraphics ctx, int x, int y, float delta) {
var pose = ctx.pose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.Nullable;

/**
* Component that renders items with their names.
Expand Down Expand Up @@ -56,6 +58,21 @@ public int getHeight() {
return height;
}

@Override
public @Nullable Component getNarration() {
if (components.isEmpty()) return null;

var narration = Component.empty();
for (var component : components) {
var childNarration = component.getNarration();
if (childNarration == null) continue;
narration.append(childNarration);
narration.append("\n");
}
narration.getSiblings().removeLast();
return narration;
}

@Override
public void render(GuiGraphics ctx, int x, int y, float delta) {
var iy = y;
Expand Down
12 changes: 12 additions & 0 deletions src/api/java/mcp/mobius/waila/api/component/PairComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.Nullable;

/**
* Component that renders key-value pair that would be aligned at the colon.
Expand Down Expand Up @@ -38,6 +39,17 @@ public int getHeight() {
return height;
}

@Override
public @Nullable Component getNarration() {
var keyNarration = key.getNarration();
var valueNarration = value.getNarration();
if (keyNarration != null) {
if (valueNarration != null) return keyNarration.copy().append(valueNarration);
else return keyNarration;
}
return valueNarration;
}

@Override
public void render(GuiGraphics ctx, int x, int y, float delta) {
var offset = key.getHeight() < height ? (height - key.getHeight()) / 2 : 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;

/**
* Component that renders a bar with a texture as the foreground.
Expand Down Expand Up @@ -53,6 +54,11 @@ public int getHeight() {
return BarComponent.HEIGHT;
}

@Override
public @Nullable Component getNarration() {
return text;
}

@Override
public void render(GuiGraphics ctx, int x, int y, float delta) {
var matrices = ctx.pose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.Nullable;

/**
* Component that renders a vanilla {@link Component}.
Expand Down Expand Up @@ -36,6 +37,11 @@ public int getHeight() {
return getFont().lineHeight;
}

@Override
public @Nullable Component getNarration() {
return component;
}

@Override
public void render(GuiGraphics ctx, int x, int y, float delta) {
ctx.drawString(getFont(), component, x, y, IApiService.INSTANCE.getFontColor());
Expand Down
19 changes: 11 additions & 8 deletions src/main/java/mcp/mobius/waila/gui/hud/TooltipRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import mcp.mobius.waila.api.WailaConstants;
import mcp.mobius.waila.api.component.EmptyComponent;
import mcp.mobius.waila.api.component.PairComponent;
import mcp.mobius.waila.api.component.WrappedComponent;
import mcp.mobius.waila.config.PluginConfig;
import mcp.mobius.waila.event.EventCanceller;
import mcp.mobius.waila.mixin.BossHealthOverlayAccess;
Expand All @@ -43,6 +42,7 @@ public class TooltipRenderer {
private static final Supplier<Rectangle> RECT = Suppliers.memoize(Rectangle::new);

private static boolean started;
private static StringBuilder narrationBuilder = new StringBuilder();
private static String lastNarration = "";
private static ITooltipComponent icon = EmptyComponent.INSTANCE;
private static int topOffset;
Expand All @@ -60,6 +60,7 @@ public static void beginBuild(State state) {
started = true;
TooltipRenderer.state = state;
TOOLTIP.clear();
narrationBuilder = new StringBuilder();
icon = EmptyComponent.INSTANCE;
topOffset = 0;
colonOffset = 0;
Expand All @@ -83,6 +84,11 @@ public static void add(Line line) {
}

for (var component : line.components) {
if (state.enableTextToSpeech() && !WailaConstants.MOD_NAME_TAG.equals(line.tag)) {
var narration = component.getNarration();
if (narration != null) narrationBuilder.append("\n").append(narration.getString().replaceAll("§[a-z0-9]", ""));
}

if (component instanceof PairComponent pair) {
colonOffset = Math.max(pair.key.getWidth(), colonOffset);
break;
Expand Down Expand Up @@ -332,13 +338,10 @@ private static void narrateObjectName(Minecraft client) {
return;
}

var objectName = TOOLTIP.getLine(WailaConstants.OBJECT_NAME_TAG);
if (objectName != null && objectName.components.get(0) instanceof WrappedComponent component) {
var narrate = component.component.getString().replaceAll("§[a-z0-9]", "");
if (!lastNarration.equalsIgnoreCase(narrate)) {
CompletableFuture.runAsync(() -> narrator.say(narrate, true));
lastNarration = narrate;
}
var narration = narrationBuilder.toString();
if (!lastNarration.equalsIgnoreCase(narration)) {
CompletableFuture.runAsync(() -> narrator.say(narration, true));
lastNarration = narration;
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/resources/resources/assets/waila/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@
"tooltip.waila.harvest.tier.diamond" : "Diamond",
"tooltip.waila.harvest.tier.netherite" : "Netherite",

"tts.waila.component.armor" : "Armor: %s",
"tts.waila.component.health" : "Health: %s/%s",
"tts.waila.component.item" : "%s of %s",

"gui.waila.configuration" : "%s Configuration",
"gui.waila.waila_settings" : "%s Settings",

Expand Down

0 comments on commit b87ea0f

Please sign in to comment.