Skip to content

Commit 654251e

Browse files
committed
Refactor iota tooltip generation
1 parent 40aa826 commit 654251e

File tree

5 files changed

+137
-82
lines changed

5 files changed

+137
-82
lines changed

Common/src/main/java/gay/object/hexdebug/api/client/splicing/SplicingTableIotaRendererProvider.java

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,20 @@
22

33
import at.petrak.hexcasting.api.casting.iota.IotaType;
44
import at.petrak.hexcasting.common.lib.hex.HexIotaTypes;
5-
import com.google.common.collect.Lists;
65
import gay.object.hexdebug.api.splicing.SplicingTableIotaClientView;
76
import gay.object.hexdebug.gui.splicing.SplicingTableScreen;
7+
import net.minecraft.client.gui.components.Tooltip;
88
import net.minecraft.network.chat.Component;
99
import org.jetbrains.annotations.NotNull;
1010

11-
import java.util.ArrayList;
12-
import java.util.List;
13-
11+
/**
12+
* A factory for {@link SplicingTableIotaRenderer} instances.
13+
* <br>
14+
* Every method in this interface is called every time the splicing table changes which iotas are
15+
* currently visible, so don't do anything too laggy in here.
16+
*/
1417
public interface SplicingTableIotaRendererProvider {
15-
/**
16-
* Creates and returns a new renderer for the provided iota.
17-
* <br>
18-
* This is called every time the splicing table changes which iotas are currently visible, so
19-
* don't do anything too laggy in here.
20-
*/
18+
/** Creates and returns a new {@link SplicingTableIotaRenderer} for the provided iota. */
2119
@NotNull
2220
SplicingTableIotaRenderer createRenderer(
2321
@NotNull IotaType<?> type,
@@ -27,40 +25,42 @@ SplicingTableIotaRenderer createRenderer(
2725
);
2826

2927
/**
30-
* Creates and returns a new tooltip for the provided iota.
28+
* Creates and returns a new {@link Tooltip} for the provided iota.
3129
* <br>
32-
* This is called every time the splicing table changes which iotas are currently visible, so
33-
* don't do anything too laggy in here.
30+
* In most cases, you'll likely want to override
31+
* {@link SplicingTableIotaRendererProvider#getTooltipBuilder} instead.
3432
*/
3533
@NotNull
36-
default SplicingTableIotaTooltip createTooltip(
34+
default Tooltip createTooltip(
3735
@NotNull IotaType<?> type,
3836
@NotNull SplicingTableIotaClientView iota
3937
) {
40-
ArrayList<Component> advanced = new ArrayList<>();
38+
return getTooltipBuilder(type, iota).build();
39+
}
40+
41+
/**
42+
* Creates and returns a new {@link SplicingTableIotaTooltipBuilder} for the provided iota.
43+
* <br>
44+
* If you want to provide a tooltip directly instead of using this builder, you can override
45+
* {@link SplicingTableIotaRendererProvider#createTooltip} instead.
46+
*/
47+
@NotNull
48+
default SplicingTableIotaTooltipBuilder getTooltipBuilder(
49+
@NotNull IotaType<?> type,
50+
@NotNull SplicingTableIotaClientView iota
51+
) {
52+
var builder = new SplicingTableIotaTooltipBuilder(iota.name())
53+
.addDetailsLine(SplicingTableScreen.tooltipText("index", iota.index()));
54+
4155
var typeKey = HexIotaTypes.REGISTRY.getKey(type);
4256
if (typeKey != null) {
43-
advanced.add(Component.literal(typeKey.toString()));
57+
builder.addAdvancedLine(Component.literal(typeKey.toString()));
4458
}
45-
advanced.add(SplicingTableScreen.tooltipText("depth", iota.depth()));
4659

47-
return new SplicingTableIotaTooltip(
48-
iota.name(),
49-
new ArrayList<>(),
50-
Lists.newArrayList(
51-
SplicingTableScreen.tooltipText("index", iota.index())
52-
),
53-
advanced,
54-
null
55-
);
60+
return builder.addAdvancedLine(SplicingTableScreen.tooltipText("depth", iota.depth()));
5661
}
5762

58-
/**
59-
* Returns the background type for this renderer.
60-
* <br>
61-
* This is called every time the splicing table changes which iotas are currently visible, so
62-
* don't do anything too laggy in here.
63-
*/
63+
/** Returns the background type for this renderer. */
6464
@NotNull
6565
default SplicingTableIotaBackgroundType getBackgroundType(
6666
@NotNull IotaType<?> type,

Common/src/main/java/gay/object/hexdebug/api/client/splicing/SplicingTableIotaTooltip.java

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package gay.object.hexdebug.api.client.splicing;
2+
3+
import net.minecraft.ChatFormatting;
4+
import net.minecraft.client.Minecraft;
5+
import net.minecraft.client.gui.components.Tooltip;
6+
import net.minecraft.network.chat.Component;
7+
import net.minecraft.network.chat.ComponentUtils;
8+
import org.jetbrains.annotations.NotNull;
9+
import org.jetbrains.annotations.Nullable;
10+
11+
import java.util.ArrayList;
12+
13+
public class SplicingTableIotaTooltipBuilder {
14+
private final Component name;
15+
private final ArrayList<Component> body;
16+
private final ArrayList<Component> details;
17+
private final ArrayList<Component> advanced;
18+
@Nullable
19+
private Component narration;
20+
21+
public SplicingTableIotaTooltipBuilder(@NotNull Component name) {
22+
this.name = name;
23+
body = new ArrayList<>();
24+
details = new ArrayList<>();
25+
advanced = new ArrayList<>();
26+
}
27+
28+
@NotNull
29+
public Component getName() {
30+
return name;
31+
}
32+
33+
@NotNull
34+
public ArrayList<Component> getBodyLines() {
35+
return body;
36+
}
37+
38+
@NotNull
39+
public ArrayList<Component> getDetailsLines() {
40+
return details;
41+
}
42+
43+
@NotNull
44+
public ArrayList<Component> getAdvancedLines() {
45+
return advanced;
46+
}
47+
48+
@Nullable
49+
public Component getNarration() {
50+
return narration;
51+
}
52+
53+
/** Append a line to the tooltip's body. */
54+
public SplicingTableIotaTooltipBuilder addBodyLine(@NotNull Component line) {
55+
body.add(line);
56+
return this;
57+
}
58+
59+
/** Append a line to the tooltip's details. */
60+
public SplicingTableIotaTooltipBuilder addDetailsLine(@NotNull Component line) {
61+
details.add(line);
62+
return this;
63+
}
64+
65+
/** Append a line to the tooltip's "advanced tooltips" section. */
66+
public SplicingTableIotaTooltipBuilder addAdvancedLine(@NotNull Component line) {
67+
advanced.add(line);
68+
return this;
69+
}
70+
71+
/** Set the tooltip's narration. */
72+
public SplicingTableIotaTooltipBuilder setNarration(@Nullable Component narration) {
73+
this.narration = narration;
74+
return this;
75+
}
76+
77+
public Tooltip build() {
78+
var lines = new ArrayList<Component>();
79+
lines.add(name);
80+
lines.addAll(body);
81+
for (var line : details) {
82+
lines.add(line.copy().withStyle(ChatFormatting.GRAY));
83+
}
84+
if (Minecraft.getInstance().options.advancedItemTooltips) {
85+
for (var line : advanced) {
86+
lines.add(line.copy().withStyle(ChatFormatting.DARK_GRAY));
87+
}
88+
}
89+
90+
var tooltipText = ComponentUtils.formatList(lines, Component.literal("\n"));
91+
if (narration != null) {
92+
return Tooltip.create(tooltipText, narration);
93+
}
94+
return Tooltip.create(tooltipText);
95+
}
96+
}

Common/src/main/kotlin/gay/object/hexdebug/gui/splicing/renderers/ListRendererProvider.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import at.petrak.hexcasting.api.casting.iota.IotaType
44
import at.petrak.hexcasting.api.utils.downcast
55
import gay.`object`.hexdebug.HexDebug
66
import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaRendererParser
7-
import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaTooltip
7+
import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaTooltipBuilder
88
import gay.`object`.hexdebug.api.splicing.SplicingTableIotaClientView
99
import gay.`object`.hexdebug.gui.splicing.SplicingTableScreen
1010
import net.minecraft.nbt.ListTag
@@ -20,14 +20,13 @@ object ListRendererProvider : TextureRendererProvider(
2020
textureWidth = 512,
2121
textureHeight = 512,
2222
) {
23-
override fun createTooltip(
23+
override fun getTooltipBuilder(
2424
type: IotaType<*>,
2525
iota: SplicingTableIotaClientView,
26-
): SplicingTableIotaTooltip {
27-
val tooltip = super.createTooltip(type, iota)
26+
): SplicingTableIotaTooltipBuilder {
2827
val listTag = iota.data!!.downcast(ListTag.TYPE)
29-
tooltip.advanced += SplicingTableScreen.tooltipText("length", listTag.size)
30-
return tooltip
28+
return super.getTooltipBuilder(type, iota)
29+
.addAdvancedLine(SplicingTableScreen.tooltipText("length", listTag.size))
3130
}
3231

3332
val PARSER = SplicingTableIotaRendererParser.of(this)

Common/src/main/kotlin/gay/object/hexdebug/gui/splicing/renderers/PatternRendererProvider.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,13 @@ object PatternRendererProvider : SplicingTableIotaRendererProvider {
8686
}
8787
}
8888

89-
override fun createTooltip(
89+
override fun getTooltipBuilder(
9090
type: IotaType<*>,
9191
iota: SplicingTableIotaClientView,
92-
): SplicingTableIotaTooltip {
93-
val tooltip = super.createTooltip(type, iota)
92+
): SplicingTableIotaTooltipBuilder {
9493
val pattern = iota.deserializePattern()!!
95-
tooltip.advanced += SplicingTableScreen.tooltipText("signature", pattern.simpleString())
96-
return tooltip
94+
return super.getTooltipBuilder(type, iota)
95+
.addAdvancedLine(SplicingTableScreen.tooltipText("signature", pattern.simpleString()))
9796
}
9897

9998
override fun getBackgroundType(

0 commit comments

Comments
 (0)