Skip to content

Commit 2fd29aa

Browse files
committed
Refactor iota rendering API to move most functions out of Provider
1 parent 03f26c6 commit 2fd29aa

File tree

13 files changed

+256
-198
lines changed

13 files changed

+256
-198
lines changed
Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,88 @@
11
package gay.object.hexdebug.api.client.splicing;
22

3+
import at.petrak.hexcasting.api.casting.iota.IotaType;
4+
import at.petrak.hexcasting.common.lib.hex.HexIotaTypes;
5+
import gay.object.hexdebug.api.splicing.SplicingTableIotaClientView;
6+
import gay.object.hexdebug.gui.splicing.SplicingTableScreen;
37
import net.minecraft.client.gui.GuiGraphics;
8+
import net.minecraft.client.gui.components.Tooltip;
9+
import net.minecraft.network.chat.Component;
410
import org.jetbrains.annotations.NotNull;
511

6-
@FunctionalInterface
7-
public interface SplicingTableIotaRenderer {
8-
void render(@NotNull GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick);
12+
public abstract class SplicingTableIotaRenderer {
13+
@NotNull
14+
private final IotaType<?> type;
15+
@NotNull
16+
private final SplicingTableIotaClientView iota;
17+
private final int x;
18+
private final int y;
19+
20+
public SplicingTableIotaRenderer(
21+
@NotNull IotaType<?> type,
22+
@NotNull SplicingTableIotaClientView iota,
23+
int x,
24+
int y
25+
) {
26+
this.type = type;
27+
this.iota = iota;
28+
this.x = x;
29+
this.y = y;
30+
}
31+
32+
@NotNull
33+
public final IotaType<?> getType() {
34+
return type;
35+
}
36+
37+
@NotNull
38+
public final SplicingTableIotaClientView getIota() {
39+
return iota;
40+
}
41+
42+
public final int getX() {
43+
return x;
44+
}
45+
46+
public final int getY() {
47+
return y;
48+
}
49+
50+
/** Renders one frame of this iota. */
51+
public abstract void render(@NotNull GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick);
52+
53+
/** Returns the background type for this renderer. */
54+
@NotNull
55+
public SplicingTableIotaBackgroundType getBackgroundType() {
56+
return SplicingTableIotaBackgroundType.GOLD;
57+
}
58+
59+
/**
60+
* Creates and returns a new {@link Tooltip} for the provided iota.
61+
* <br>
62+
* In most cases, you'll likely want to override
63+
* {@link SplicingTableIotaRenderer#buildTooltip} instead.
64+
*/
65+
@NotNull
66+
public Tooltip createTooltip() {
67+
return buildTooltip().build();
68+
}
69+
70+
/**
71+
* Creates and returns a new {@link SplicingTableIotaTooltipBuilder} for the provided iota.
72+
* <br>
73+
* If you want to provide a tooltip directly instead of using this builder, you can override
74+
* {@link SplicingTableIotaRenderer#createTooltip} instead.
75+
*/
76+
@NotNull
77+
protected SplicingTableIotaTooltipBuilder buildTooltip() {
78+
var builder = new SplicingTableIotaTooltipBuilder(iota.name())
79+
.addDetailsLine(SplicingTableScreen.tooltipText("index", iota.index()));
80+
81+
var typeKey = HexIotaTypes.REGISTRY.getKey(type);
82+
if (typeKey != null) {
83+
builder.addAdvancedLine(Component.literal(typeKey.toString()));
84+
}
85+
86+
return builder.addAdvancedLine(SplicingTableScreen.tooltipText("depth", iota.depth()));
87+
}
988
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.google.gson.Gson;
44
import com.google.gson.JsonObject;
5+
import com.mojang.datafixers.util.Function3;
6+
import com.mojang.datafixers.util.Function4;
57
import org.jetbrains.annotations.NotNull;
68
import org.jetbrains.annotations.Nullable;
79

@@ -15,11 +17,9 @@ public interface SplicingTableIotaRendererParser<T extends SplicingTableIotaRend
1517
@NotNull
1618
T parse(@NotNull Gson gson, @NotNull JsonObject jsonObject, @Nullable T parent);
1719

18-
/**
19-
* Creates a parser that always returns the given instance.
20-
*/
20+
/** Creates a parser that always returns the given provider. */
2121
@NotNull
22-
static <T extends SplicingTableIotaRendererProvider> SplicingTableIotaRendererParser<T> of(T instance) {
23-
return (gson, jsonObject, parent) -> instance;
22+
static SplicingTableIotaRendererParser<?> simple(SplicingTableIotaRendererProvider provider) {
23+
return (gson, jsonObject, parent) -> provider;
2424
}
2525
}

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

Lines changed: 5 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,17 @@
99
import org.jetbrains.annotations.NotNull;
1010
import org.jetbrains.annotations.Nullable;
1111

12-
/**
13-
* A factory for {@link SplicingTableIotaRenderer} instances.
14-
* <br>
15-
* Every method in this interface is called every time the splicing table changes which iotas are
16-
* currently visible, so don't do anything too laggy in here.
17-
*/
12+
/** A factory for {@link SplicingTableIotaRenderer} instances. */
13+
@FunctionalInterface
1814
public interface SplicingTableIotaRendererProvider {
1915
/**
2016
* Creates and returns a new {@link SplicingTableIotaRenderer} for the provided iota.
2117
* <br>
2218
* May return null if unable to create a renderer for the given iota; in that case, the default
2319
* renderer will be used instead.
20+
* <br>
21+
* This is called every time the splicing table changes which iotas are currently visible, so
22+
* don't do anything too laggy in here.
2423
*/
2524
@Nullable
2625
SplicingTableIotaRenderer createRenderer(
@@ -29,49 +28,4 @@ SplicingTableIotaRenderer createRenderer(
2928
int x,
3029
int y
3130
);
32-
33-
/**
34-
* Creates and returns a new {@link Tooltip} for the provided iota.
35-
* <br>
36-
* In most cases, you'll likely want to override
37-
* {@link SplicingTableIotaRendererProvider#getTooltipBuilder} instead.
38-
*/
39-
@NotNull
40-
default Tooltip createTooltip(
41-
@NotNull IotaType<?> type,
42-
@NotNull SplicingTableIotaClientView iota
43-
) {
44-
return getTooltipBuilder(type, iota).build();
45-
}
46-
47-
/**
48-
* Creates and returns a new {@link SplicingTableIotaTooltipBuilder} for the provided iota.
49-
* <br>
50-
* If you want to provide a tooltip directly instead of using this builder, you can override
51-
* {@link SplicingTableIotaRendererProvider#createTooltip} instead.
52-
*/
53-
@NotNull
54-
default SplicingTableIotaTooltipBuilder getTooltipBuilder(
55-
@NotNull IotaType<?> type,
56-
@NotNull SplicingTableIotaClientView iota
57-
) {
58-
var builder = new SplicingTableIotaTooltipBuilder(iota.name())
59-
.addDetailsLine(SplicingTableScreen.tooltipText("index", iota.index()));
60-
61-
var typeKey = HexIotaTypes.REGISTRY.getKey(type);
62-
if (typeKey != null) {
63-
builder.addAdvancedLine(Component.literal(typeKey.toString()));
64-
}
65-
66-
return builder.addAdvancedLine(SplicingTableScreen.tooltipText("depth", iota.depth()));
67-
}
68-
69-
/** Returns the background type for this renderer. */
70-
@NotNull
71-
default SplicingTableIotaBackgroundType getBackgroundType(
72-
@NotNull IotaType<?> type,
73-
@NotNull SplicingTableIotaClientView iota
74-
) {
75-
return SplicingTableIotaBackgroundType.GOLD;
76-
}
7731
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import java.util.ArrayList;
1212

13-
public class SplicingTableIotaTooltipBuilder {
13+
public final class SplicingTableIotaTooltipBuilder {
1414
private final Component name;
1515
private final ArrayList<Component> body;
1616
private final ArrayList<Component> details;
@@ -51,29 +51,34 @@ public Component getNarration() {
5151
}
5252

5353
/** Append a line to the tooltip's body. */
54+
@NotNull
5455
public SplicingTableIotaTooltipBuilder addBodyLine(@NotNull Component line) {
5556
body.add(line);
5657
return this;
5758
}
5859

5960
/** Append a line to the tooltip's details. */
61+
@NotNull
6062
public SplicingTableIotaTooltipBuilder addDetailsLine(@NotNull Component line) {
6163
details.add(line);
6264
return this;
6365
}
6466

6567
/** Append a line to the tooltip's "advanced tooltips" section. */
68+
@NotNull
6669
public SplicingTableIotaTooltipBuilder addAdvancedLine(@NotNull Component line) {
6770
advanced.add(line);
6871
return this;
6972
}
7073

7174
/** Set the tooltip's narration. */
75+
@NotNull
7276
public SplicingTableIotaTooltipBuilder setNarration(@Nullable Component narration) {
7377
this.narration = narration;
7478
return this;
7579
}
7680

81+
@NotNull
7782
public Tooltip build() {
7883
var lines = new ArrayList<Component>();
7984
lines.add(name);

Common/src/main/kotlin/gay/object/hexdebug/HexDebugClient.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import gay.`object`.hexdebug.config.HexDebugClientConfig
1313
import gay.`object`.hexdebug.config.HexDebugServerConfig
1414
import gay.`object`.hexdebug.gui.splicing.renderers.ItemRendererProvider
1515
import gay.`object`.hexdebug.gui.splicing.renderers.ListRendererProvider
16-
import gay.`object`.hexdebug.gui.splicing.renderers.PatternRendererProvider
16+
import gay.`object`.hexdebug.gui.splicing.renderers.PatternRenderer
1717
import gay.`object`.hexdebug.gui.splicing.renderers.TextureRendererProvider
1818
import gay.`object`.hexdebug.registry.HexDebugBlocks
1919
import gay.`object`.hexdebug.resources.splicing.SplicingTableIotasResourceReloadListener
@@ -99,7 +99,7 @@ object HexDebugClient {
9999
for ((name, parser) in arrayOf(
100100
"item" to ItemRendererProvider.PARSER,
101101
"list" to ListRendererProvider.PARSER,
102-
"pattern" to PatternRendererProvider.PARSER,
102+
"pattern" to PatternRenderer.PARSER,
103103
"texture" to TextureRendererProvider.PARSER,
104104
)) {
105105
SplicingTableIotaRenderers.register(HexDebug.id(name), parser)

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaRendererProvid
77
import gay.`object`.hexdebug.api.splicing.SplicingTableIotaClientView
88
import gay.`object`.hexdebug.utils.*
99
import net.minecraft.client.Minecraft
10+
import net.minecraft.client.gui.GuiGraphics
1011
import net.minecraft.commands.arguments.NbtPathArgument.NbtPath
1112
import net.minecraft.core.registries.BuiltInRegistries
1213
import net.minecraft.nbt.CompoundTag
@@ -41,9 +42,18 @@ class ItemRendererProvider(
4142
val stack = ItemStack(item, count)
4243
stack.tag = tag
4344

44-
return SplicingTableIotaRenderer { guiGraphics, _, _, _ ->
45-
val ps = guiGraphics.pose()
46-
ps.pushPose {
45+
return ItemRenderer(type, iota, x, y, stack)
46+
}
47+
48+
inner class ItemRenderer(
49+
type: IotaType<*>,
50+
iota: SplicingTableIotaClientView,
51+
x: Int,
52+
y: Int,
53+
val stack: ItemStack,
54+
) : SplicingTableIotaRenderer(type, iota, x, y) {
55+
override fun render(guiGraphics: GuiGraphics, mouseX: Int, mouseY: Int, partialTick: Float) {
56+
guiGraphics.pose().letPushPose { ps ->
4757
// align to center of iota display
4858
ps.translate(x + (18f / 2f) + xOffset, y + (21f / 2f) + yOffset, 0f)
4959
ps.scale(scale)

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package gay.`object`.hexdebug.gui.splicing.renderers
33
import at.petrak.hexcasting.api.casting.iota.IotaType
44
import at.petrak.hexcasting.api.utils.downcast
55
import gay.`object`.hexdebug.HexDebug
6+
import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaRenderer
67
import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaRendererParser
78
import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaTooltipBuilder
89
import gay.`object`.hexdebug.api.splicing.SplicingTableIotaClientView
@@ -20,14 +21,27 @@ object ListRendererProvider : TextureRendererProvider(
2021
textureWidth = 512,
2122
textureHeight = 512,
2223
) {
23-
override fun getTooltipBuilder(
24+
override fun createRenderer(
2425
type: IotaType<*>,
2526
iota: SplicingTableIotaClientView,
26-
): SplicingTableIotaTooltipBuilder {
27-
val listTag = iota.data!!.downcast(ListTag.TYPE)
28-
return super.getTooltipBuilder(type, iota)
29-
.addAdvancedLine(SplicingTableScreen.tooltipText("length", listTag.size))
27+
x: Int,
28+
y: Int
29+
): SplicingTableIotaRenderer {
30+
return ListRenderer(type, iota, x, y)
3031
}
3132

32-
val PARSER = SplicingTableIotaRendererParser.of(this)
33+
class ListRenderer(
34+
type: IotaType<*>,
35+
iota: SplicingTableIotaClientView,
36+
x: Int,
37+
y: Int,
38+
) : TextureRenderer(type, iota, x, y) {
39+
override fun buildTooltip(): SplicingTableIotaTooltipBuilder {
40+
val listTag = iota.data!!.downcast(ListTag.TYPE)
41+
return super.buildTooltip()
42+
.addAdvancedLine(SplicingTableScreen.tooltipText("length", listTag.size))
43+
}
44+
}
45+
46+
val PARSER = SplicingTableIotaRendererParser.simple(::ListRenderer)
3347
}

0 commit comments

Comments
 (0)