diff options
| author | Shnupbups <shnupbups@gmail.com> | 2021-12-07 01:38:45 +1100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-12-06 22:38:45 +0800 |
| commit | ea48061cccb7fe91cf54478829d45798d39e2633 (patch) | |
| tree | 6f7938663a3f51e7e77cf728815c28cf10ffa2b8 | |
| parent | 2095e5b24ec4e1b6b79b0ade6bbea095f3ab1543 (diff) | |
| download | RoughlyEnoughItems-ea48061cccb7fe91cf54478829d45798d39e2633.tar.gz RoughlyEnoughItems-ea48061cccb7fe91cf54478829d45798d39e2633.tar.bz2 RoughlyEnoughItems-ea48061cccb7fe91cf54478829d45798d39e2633.zip | |
Make Label tooltips use Components instead of Strings (#680)
Attempted to keep all old methods for backwards compatibility reasons, but marked as Deprecated and ScheduledForRemoval... and thus had to choose a not-so-ideal method name or two for erasure conflict reasons
5 files changed, 118 insertions, 19 deletions
diff --git a/api/src/main/java/me/shedaniel/rei/api/client/gui/widgets/Label.java b/api/src/main/java/me/shedaniel/rei/api/client/gui/widgets/Label.java index 0c634b114..13822f61f 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/gui/widgets/Label.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/gui/widgets/Label.java @@ -26,12 +26,16 @@ package me.shedaniel.rei.api.client.gui.widgets; import com.mojang.blaze3d.vertex.PoseStack; import me.shedaniel.math.Point; import me.shedaniel.rei.api.client.REIRuntime; +import net.minecraft.network.chat.Component; import net.minecraft.network.chat.FormattedText; +import net.minecraft.network.chat.TextComponent; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Function; +import java.util.stream.Stream; public abstract class Label extends WidgetWithBounds { public static final int LEFT_ALIGNED = -1; @@ -143,47 +147,141 @@ public abstract class Label extends WidgetWithBounds { /** * @return the tooltip from the current tooltip function, null if no tooltip. + * @deprecated use {@link #getTooltipLines} */ @Nullable - public abstract String getTooltip(); - + @Deprecated + @ApiStatus.ScheduledForRemoval + public String getTooltip() { + Component[] lines = getTooltipLines(); + String tooltip = null; + if(lines != null) { + StringBuilder tooltipBuilder = new StringBuilder(); + for (Component line:lines) { + tooltipBuilder.append(line.getContents()).append("\n"); + } + tooltip = tooltipBuilder.toString(); + } + return tooltip; + } + + /** + * @return the tooltip from the current tooltip function, null if no tooltip. + */ + @Nullable + public abstract Component[] getTooltipLines(); + + /** + * Sets the tooltip function used to get the tooltip. + * + * @param tooltip the tooltip function used to get the tooltip. + * @deprecated use {@link #setTooltipFunction(Function)} + */ + @Deprecated + @ApiStatus.ScheduledForRemoval + public void setTooltip(@Nullable Function<Label, @Nullable String> tooltip) { + if(tooltip != null) setTooltipFunction((label) -> { + String text = tooltip.apply(label); + if(text != null) return Stream.of(text.split("\n")).map(TextComponent::new).toArray(Component[]::new); + return null; + }); + } + /** * Sets the tooltip function used to get the tooltip. * * @param tooltip the tooltip function used to get the tooltip. */ - public abstract void setTooltip(@Nullable Function<Label, @Nullable String> tooltip); + public abstract void setTooltipFunction(@Nullable Function<Label, @Nullable Component[]> tooltip); + + /** + * Sets the tooltip. + * + * @param tooltip the lines of tooltip. + */ + public void setTooltip(Component... tooltip) { + setTooltipFunction((label) -> tooltip); + } + + /** + * Sets the tooltip. + * + * @param tooltip the tooltip. + */ + public void setTooltip(String... tooltip) { + setTooltipFunction((label) -> Stream.of(tooltip).map(TextComponent::new).toArray(Component[]::new)); + } /** * Sets the tooltip. * * @param tooltip the lines of tooltip. * @return the label itself. + * @deprecated use {@link #tooltip(String...)} */ + @Deprecated + @ApiStatus.ScheduledForRemoval public final Label tooltipLines(String... tooltip) { - return tooltipLine(String.join("\n", tooltip)); + return tooltip(tooltip); } - + /** * Sets the tooltip. * * @param tooltip the line of tooltip. * @return the label itself. + * @deprecated use {@link #tooltip(String...)} */ + @Deprecated + @ApiStatus.ScheduledForRemoval public final Label tooltipLine(String tooltip) { - return tooltipSupplier(label -> tooltip); + return tooltip(tooltip); } - + + /** + * Sets the tooltip. + * + * @param tooltip the lines of tooltip. + * @return the label itself. + */ + public final Label tooltip(String... tooltip) { + return tooltipFunction(label -> Stream.of(tooltip).map(TextComponent::new).toArray(Component[]::new)); + } + + /** + * Sets the tooltip. + * + * @param tooltip the lines of tooltip. + * @return the label itself. + */ + public final Label tooltip(Component... tooltip) { + return tooltipFunction(label -> tooltip); + } + /** * Sets the tooltip function. * * @param tooltip the tooltip function used to get the tooltip. * @return the label itself. + * @deprecated use {@link #tooltipFunction} */ + @Deprecated + @ApiStatus.ScheduledForRemoval(inVersion = "8.0.0") public final Label tooltipSupplier(@Nullable Function<Label, @Nullable String> tooltip) { setTooltip(tooltip); return this; } + + /** + * Sets the tooltip function. + * + * @param tooltip the tooltip function used to get the tooltip. + * @return the label itself. + */ + public final Label tooltipFunction(@Nullable Function<Label, @Nullable Component[]> tooltip) { + setTooltipFunction(tooltip); + return this; + } /** * Gets the horizontal alignment of the label, defaulted as centered. diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/ScreenOverlayImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/ScreenOverlayImpl.java index f95afd48b..0c71a7cbd 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/ScreenOverlayImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/ScreenOverlayImpl.java @@ -68,6 +68,7 @@ import me.shedaniel.rei.impl.client.gui.modules.Menu; import me.shedaniel.rei.impl.client.gui.widget.*; import me.shedaniel.rei.impl.client.gui.widget.search.OverlaySearchField; import me.shedaniel.rei.impl.common.util.Weather; +import net.minecraft.ChatFormatting; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.chat.NarratorChatListener; import net.minecraft.client.gui.components.events.GuiEventListener; @@ -337,7 +338,7 @@ public class ScreenOverlayImpl extends ScreenOverlay { ENTRY_LIST_WIDGET.updateEntriesPosition(); }, ENTRY_LIST_WIDGET.getPage(), ENTRY_LIST_WIDGET.getTotalPages()); } - }).tooltipLine(I18n.get("text.rei.go_back_first_page") + "\n \n§7" + I18n.get("text.rei.shift_click_to", I18n.get("text.rei.choose_page"))).focusable(false).onRender((matrices, label) -> { + }).tooltip(new TranslatableComponent("text.rei.go_back_first_page"), new TextComponent(" "), new TranslatableComponent("text.rei.shift_click_to", new TranslatableComponent("text.rei.choose_page")).withStyle(ChatFormatting.GRAY)).focusable(false).onRender((matrices, label) -> { label.setClickable(ENTRY_LIST_WIDGET.getTotalPages() > 1); label.setMessage(new TextComponent(String.format("%s/%s", ENTRY_LIST_WIDGET.getPage() + 1, Math.max(ENTRY_LIST_WIDGET.getTotalPages(), 1)))); }).rainbow(new Random().nextFloat() < 1.0E-4D || ClientHelperImpl.getInstance().isAprilFools.get())); diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/CompositeDisplayViewingScreen.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/CompositeDisplayViewingScreen.java index 27b6c3cb0..902a81b13 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/CompositeDisplayViewingScreen.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/CompositeDisplayViewingScreen.java @@ -54,7 +54,6 @@ import me.shedaniel.rei.impl.display.DisplaySpec; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.chat.NarratorChatListener; import net.minecraft.client.gui.components.events.GuiEventListener; -import net.minecraft.client.resources.language.I18n; import net.minecraft.client.resources.sounds.SimpleSoundInstance; import net.minecraft.network.chat.TextComponent; import net.minecraft.network.chat.TranslatableComponent; @@ -227,7 +226,7 @@ public class CompositeDisplayViewingScreen extends AbstractDisplayViewingScreen this.widgets.add(Widgets.createClickableLabel(new Point(bounds.x + 4 + scrollListBounds.width / 2, bounds.y + 6), categories.get(selectedCategoryIndex).getTitle(), label -> { ViewSearchBuilder.builder().addAllCategories().open(); - }).tooltipLine(I18n.get("text.rei.view_all_categories")).noShadow().color(0xFF404040, 0xFFBBBBBB).hoveredColor(0xFF0041FF, 0xFFFFBD4D)); + }).tooltip(new TranslatableComponent("text.rei.view_all_categories")).noShadow().color(0xFF404040, 0xFFBBBBBB).hoveredColor(0xFF0041FF, 0xFFFFBD4D)); this._children().addAll(buttonList); this.widgets.addAll(tabs); diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/DefaultDisplayViewingScreen.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/DefaultDisplayViewingScreen.java index 5ee3ece56..fc16ca16b 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/DefaultDisplayViewingScreen.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/DefaultDisplayViewingScreen.java @@ -58,12 +58,12 @@ import me.shedaniel.rei.impl.client.gui.widget.InternalWidgets; import me.shedaniel.rei.impl.client.gui.widget.TabWidget; import me.shedaniel.rei.impl.client.gui.widget.basewidgets.PanelWidget; import me.shedaniel.rei.impl.display.DisplaySpec; +import net.minecraft.ChatFormatting; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.chat.NarratorChatListener; import net.minecraft.client.gui.components.events.GuiEventListener; import net.minecraft.client.gui.screens.Screen; import net.minecraft.client.renderer.MultiBufferSource; -import net.minecraft.client.resources.language.I18n; import net.minecraft.client.resources.sounds.SimpleSoundInstance; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.TextComponent; @@ -187,7 +187,7 @@ public class DefaultDisplayViewingScreen extends AbstractDisplayViewingScreen { .onClick(button -> previousCategory()).tooltipLine(new TranslatableComponent("text.rei.previous_category"))); this.widgets.add(Widgets.createClickableLabel(new Point(bounds.getCenterX(), bounds.getY() + 7), getCurrentCategory().getTitle(), clickableLabelWidget -> { ViewSearchBuilder.builder().addAllCategories().open(); - }).tooltipLine(I18n.get("text.rei.view_all_categories"))); + }).tooltip(new TranslatableComponent("text.rei.view_all_categories"))); this.widgets.add(categoryNext = Widgets.createButton(new Rectangle(bounds.getMaxX() - 17, bounds.getY() + 5, 12, 12), new TranslatableComponent("text.rei.right_arrow")) .onClick(button -> nextCategory()).tooltipLine(new TranslatableComponent("text.rei.next_category"))); this.categoryBack.setEnabled(categories.size() > 1); @@ -213,7 +213,7 @@ public class DefaultDisplayViewingScreen extends AbstractDisplayViewingScreen { }).onRender((matrices, label) -> { label.setMessage(new ImmutableTextComponent(String.format("%d/%d", page + 1, getCurrentTotalPages()))); label.setClickable(getCurrentTotalPages() > 1); - }).tooltipSupplier(label -> label.isClickable() ? I18n.get("text.rei.go_back_first_page") + "\n \n§7" + I18n.get("text.rei.shift_click_to", I18n.get("text.rei.choose_page")) : null)); + }).tooltipFunction(label -> label.isClickable() ? new Component[]{new TranslatableComponent("text.rei.go_back_first_page"), new TextComponent(" "), new TranslatableComponent("text.rei.shift_click_to", new TranslatableComponent("text.rei.choose_page")).withStyle(ChatFormatting.GRAY)} : null)); this.widgets.add(recipeNext = Widgets.createButton(new Rectangle(bounds.getMaxX() - 17, bounds.getY() + 19, 12, 12), new TranslatableComponent("text.rei.right_arrow")) .onClick(button -> { page++; diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/basewidgets/LabelWidget.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/basewidgets/LabelWidget.java index 9566b7de0..f8a3d1120 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/basewidgets/LabelWidget.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/basewidgets/LabelWidget.java @@ -37,6 +37,7 @@ import me.shedaniel.rei.api.client.gui.widgets.Widgets; import me.shedaniel.rei.impl.client.gui.text.TextTransformations; import net.minecraft.client.gui.components.events.GuiEventListener; import net.minecraft.locale.Language; +import net.minecraft.network.chat.Component; import net.minecraft.network.chat.FormattedText; import net.minecraft.network.chat.TextComponent; import net.minecraft.util.FormattedCharSequence; @@ -70,7 +71,7 @@ public final class LabelWidget extends Label { return hoveredColor.value(); }, ValueAnimator.typicalTransitionTime() / 2); private Point point; - @Nullable private Function<Label, @Nullable String> tooltip; + @Nullable private Function<Label, @Nullable Component[]> tooltip; @Nullable private Consumer<Label> onClick; @Nullable private BiConsumer<PoseStack, Label> onRender; private FormattedText text; @@ -126,14 +127,14 @@ public final class LabelWidget extends Label { @Override @Nullable - public final String getTooltip() { + public final Component[] getTooltipLines() { if (tooltip == null) return null; return tooltip.apply(this); } @Override - public final void setTooltip(@Nullable Function<Label, @Nullable String> tooltip) { + public final void setTooltipFunction(@Nullable Function<Label, @Nullable Component[]> tooltip) { this.tooltip = tooltip; } @@ -251,12 +252,12 @@ public final class LabelWidget extends Label { break; } if (isHovered(mouseX, mouseY)) { - String tooltip = getTooltip(); + Component[] tooltip = getTooltipLines(); if (tooltip != null) { if (!focused && containsMouse(mouseX, mouseY)) - Tooltip.create(Stream.of(tooltip.split("\n")).map(TextComponent::new).collect(Collectors.toList())).queue(); + Tooltip.create(tooltip).queue(); else if (focused) - Tooltip.create(point, Stream.of(tooltip.split("\n")).map(TextComponent::new).collect(Collectors.toList())).queue(); + Tooltip.create(point, tooltip).queue(); } } } |
