Skip to content

Commit e7e8dbf

Browse files
committed
Implement a bunch of new iota renderers, close #48
1 parent 36a9108 commit e7e8dbf

File tree

16 files changed

+292
-57
lines changed

16 files changed

+292
-57
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaRenderers
1111
import gay.`object`.hexdebug.blocks.focusholder.FocusHolderBlock
1212
import gay.`object`.hexdebug.config.HexDebugClientConfig
1313
import gay.`object`.hexdebug.config.HexDebugServerConfig
14-
import gay.`object`.hexdebug.gui.splicing.renderers.ItemRendererProvider
15-
import gay.`object`.hexdebug.gui.splicing.renderers.ListRendererProvider
16-
import gay.`object`.hexdebug.gui.splicing.renderers.PatternRenderer
17-
import gay.`object`.hexdebug.gui.splicing.renderers.TextureRendererProvider
14+
import gay.`object`.hexdebug.gui.splicing.renderers.*
15+
import gay.`object`.hexdebug.gui.splicing.renderers.conditional.IfPathExistsRendererProvider
1816
import gay.`object`.hexdebug.registry.HexDebugBlocks
1917
import gay.`object`.hexdebug.resources.splicing.SplicingTableIotasResourceReloadListener
2018
import gay.`object`.hexdebug.utils.styledHoverName
@@ -97,9 +95,12 @@ object HexDebugClient {
9795

9896
private fun registerSplicingTableIotaRenderers() {
9997
for ((name, parser) in arrayOf(
98+
"conditional/if_path_exists" to IfPathExistsRendererProvider.PARSER,
10099
"item" to ItemRendererProvider.PARSER,
100+
"layers" to LayersRendererProvider.PARSER,
101101
"list" to ListRendererProvider.PARSER,
102102
"pattern" to PatternRenderer.PARSER,
103+
"sub_iota" to SubIotaRendererProvider.PARSER,
103104
"texture" to TextureRendererProvider.PARSER,
104105
)) {
105106
SplicingTableIotaRenderers.register(HexDebug.id(name), parser)

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

File renamed without changes.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package gay.`object`.hexdebug.gui.splicing.renderers
2+
3+
import at.petrak.hexcasting.api.casting.iota.IotaType
4+
import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaRenderer
5+
import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaRendererParser
6+
import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaRendererProvider
7+
import gay.`object`.hexdebug.api.splicing.SplicingTableIotaClientView
8+
import gay.`object`.hexdebug.utils.getAsIotaRendererProvider
9+
import gay.`object`.hexdebug.utils.pushPose
10+
import gay.`object`.hexdebug.utils.scale
11+
import net.minecraft.client.gui.GuiGraphics
12+
import net.minecraft.client.gui.components.Tooltip
13+
import net.minecraft.util.GsonHelper
14+
15+
class LayersRenderer(
16+
type: IotaType<*>,
17+
iota: SplicingTableIotaClientView,
18+
x: Int,
19+
y: Int,
20+
private val layers: List<RendererLayer<SplicingTableIotaRenderer>>,
21+
) : SplicingTableIotaRenderer(type, iota, x, y) {
22+
override fun render(guiGraphics: GuiGraphics, mouseX: Int, mouseY: Int, partialTick: Float) {
23+
val ps = guiGraphics.pose()
24+
for (layer in layers) {
25+
ps.pushPose {
26+
ps.translate(x.toFloat(), y.toFloat(), 0f)
27+
if (layer.scale != 1f) {
28+
ps.translate(18f / 2f, 21f / 2f, 0f)
29+
ps.scale(layer.scale)
30+
ps.translate(-18f / 2f, -21f / 2f, 0f)
31+
}
32+
layer.renderer.render(guiGraphics, mouseX, mouseY, partialTick)
33+
}
34+
}
35+
}
36+
37+
override fun createTooltip(): Tooltip {
38+
return layers.lastOrNull { it.useTooltip }
39+
?.renderer
40+
?.createTooltip()
41+
?: super.createTooltip()
42+
}
43+
}
44+
45+
class LayersRendererProvider(
46+
private val layers: List<RendererLayer<SplicingTableIotaRendererProvider>>,
47+
) : SplicingTableIotaRendererProvider {
48+
override fun createRenderer(
49+
type: IotaType<*>,
50+
iota: SplicingTableIotaClientView,
51+
x: Int,
52+
y: Int
53+
): SplicingTableIotaRenderer? {
54+
val rendererLayers = layers.mapNotNull { layer ->
55+
layer.mapNotNull { it.createRenderer(type, iota, 0, 0) }
56+
}
57+
if (rendererLayers.isEmpty()) return null
58+
return LayersRenderer(type, iota, x, y, rendererLayers)
59+
}
60+
61+
companion object {
62+
val PARSER = SplicingTableIotaRendererParser<LayersRendererProvider> { _, jsonObject, _ ->
63+
LayersRendererProvider(
64+
layers = GsonHelper.getAsJsonArray(jsonObject, "layers").map { layer ->
65+
val layerObject = GsonHelper.convertToJsonObject(layer, "layers")
66+
RendererLayer(
67+
renderer = layerObject.getAsIotaRendererProvider("renderer"),
68+
scale = GsonHelper.getAsFloat(layerObject, "scale", 1f),
69+
useTooltip = GsonHelper.getAsBoolean(layerObject, "useTooltip", true),
70+
)
71+
},
72+
)
73+
}
74+
}
75+
}
76+
77+
data class RendererLayer<T>(
78+
val renderer: T,
79+
val scale: Float,
80+
val useTooltip: Boolean,
81+
) {
82+
fun <U> mapNotNull(f: (T) -> U?): RendererLayer<U>? {
83+
return RendererLayer(
84+
renderer = f(renderer) ?: return null,
85+
scale = scale,
86+
useTooltip = useTooltip,
87+
)
88+
}
89+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package gay.`object`.hexdebug.gui.splicing.renderers
2+
3+
import at.petrak.hexcasting.api.casting.iota.IotaType
4+
import at.petrak.hexcasting.api.utils.downcast
5+
import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaRenderer
6+
import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaRendererParser
7+
import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaRendererProvider
8+
import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaTooltipBuilder
9+
import gay.`object`.hexdebug.api.splicing.SplicingTableIotaClientView
10+
import gay.`object`.hexdebug.gui.splicing.SplicingTableScreen
11+
import gay.`object`.hexdebug.utils.getAsIotaRendererProvider
12+
import net.minecraft.client.gui.GuiGraphics
13+
import net.minecraft.nbt.ListTag
14+
15+
class ListRenderer(
16+
type: IotaType<*>,
17+
iota: SplicingTableIotaClientView,
18+
x: Int,
19+
y: Int,
20+
private val inner: SplicingTableIotaRenderer?,
21+
) : SplicingTableIotaRenderer(type, iota, x, y) {
22+
override fun render(guiGraphics: GuiGraphics, mouseX: Int, mouseY: Int, partialTick: Float) {
23+
inner?.render(guiGraphics, mouseX, mouseY, partialTick)
24+
}
25+
26+
override fun buildTooltip(): SplicingTableIotaTooltipBuilder {
27+
val listTag = iota.data!!.downcast(ListTag.TYPE)
28+
return super.buildTooltip()
29+
.addAdvancedLine(SplicingTableScreen.tooltipText("length", listTag.size))
30+
}
31+
}
32+
33+
class ListRendererProvider(private val inner: SplicingTableIotaRendererProvider) : SplicingTableIotaRendererProvider {
34+
override fun createRenderer(
35+
type: IotaType<*>,
36+
iota: SplicingTableIotaClientView,
37+
x: Int,
38+
y: Int
39+
): SplicingTableIotaRenderer {
40+
return ListRenderer(type, iota, x, y, inner.createRenderer(type, iota, x, y))
41+
}
42+
43+
companion object {
44+
val PARSER = SplicingTableIotaRendererParser<ListRendererProvider> { _, jsonObject, _ ->
45+
ListRendererProvider(
46+
inner = jsonObject.getAsIotaRendererProvider("renderer"),
47+
)
48+
}
49+
}
50+
}

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

Lines changed: 0 additions & 47 deletions
This file was deleted.

Common/src/main/kotlin/gay/object/hexdebug/gui/splicing/renderers/PatternRenderer.kt renamed to Common/src/main/kotlin/gay/object/hexdebug/gui/splicing/renderers/Pattern.kt

File renamed without changes.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package gay.`object`.hexdebug.gui.splicing.renderers
2+
import at.petrak.hexcasting.api.casting.iota.IotaType
3+
import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaRenderer
4+
import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaRendererParser
5+
import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaRendererProvider
6+
import gay.`object`.hexdebug.api.splicing.SplicingTableIotaClientView
7+
import gay.`object`.hexdebug.resources.splicing.SplicingTableIotasResourceReloadListener
8+
import gay.`object`.hexdebug.utils.getAsNbtPath
9+
import gay.`object`.hexdebug.utils.getOrNull
10+
import net.minecraft.commands.arguments.NbtPathArgument.NbtPath
11+
import net.minecraft.nbt.CompoundTag
12+
import net.minecraft.network.chat.Component
13+
14+
class SubIotaRendererProvider(private val path: NbtPath) : SplicingTableIotaRendererProvider {
15+
override fun createRenderer(
16+
type: IotaType<*>,
17+
iota: SplicingTableIotaClientView,
18+
x: Int,
19+
y: Int
20+
): SplicingTableIotaRenderer? {
21+
val data = iota.data ?: return null
22+
val subIotaTag = path.getOrNull(data)?.first() as? CompoundTag ?: return null
23+
val subIotaType = IotaType.getTypeFromTag(subIotaTag) ?: return null
24+
val provider = SplicingTableIotasResourceReloadListener.getProvider(subIotaType) ?: return null
25+
val subIota = SplicingTableIotaClientView(
26+
subIotaTag,
27+
Component.empty(),
28+
"",
29+
0,
30+
0,
31+
)
32+
return provider.createRenderer(subIotaType, subIota, x, y)
33+
}
34+
35+
companion object {
36+
val PARSER = SplicingTableIotaRendererParser<SubIotaRendererProvider> { _, jsonObject, _ ->
37+
SubIotaRendererProvider(path = jsonObject.getAsNbtPath("path"))
38+
}
39+
}
40+
}

Common/src/main/kotlin/gay/object/hexdebug/gui/splicing/renderers/TextureRendererProvider.kt renamed to Common/src/main/kotlin/gay/object/hexdebug/gui/splicing/renderers/Texture.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import net.minecraft.resources.ResourceLocation
1212
import net.minecraft.util.GsonHelper
1313
import java.awt.Color
1414

15-
open class TextureRendererProvider(
15+
data class TextureRendererProvider(
1616
val texture: ResourceLocation,
1717
val xOffset: Int,
1818
val yOffset: Int,
@@ -33,7 +33,7 @@ open class TextureRendererProvider(
3333
return TextureRenderer(type, iota, x, y)
3434
}
3535

36-
open inner class TextureRenderer(
36+
inner class TextureRenderer(
3737
type: IotaType<*>,
3838
iota: SplicingTableIotaClientView,
3939
x: Int,
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package gay.`object`.hexdebug.gui.splicing.renderers.conditional
2+
3+
import at.petrak.hexcasting.api.casting.iota.IotaType
4+
import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaRenderer
5+
import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaRendererParser
6+
import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaRendererProvider
7+
import gay.`object`.hexdebug.api.splicing.SplicingTableIotaClientView
8+
import gay.`object`.hexdebug.utils.getAsIotaRendererProvider
9+
import gay.`object`.hexdebug.utils.getAsNbtPath
10+
import net.minecraft.commands.arguments.NbtPathArgument.NbtPath
11+
12+
class IfPathExistsRendererProvider(
13+
private val path: NbtPath,
14+
private val providerIf: SplicingTableIotaRendererProvider,
15+
private val providerElse: SplicingTableIotaRendererProvider,
16+
) : SplicingTableIotaRendererProvider {
17+
override fun createRenderer(
18+
type: IotaType<*>,
19+
iota: SplicingTableIotaClientView,
20+
x: Int,
21+
y: Int
22+
): SplicingTableIotaRenderer? {
23+
val condition = iota.data?.let { path.countMatching(it) > 0 } == true
24+
val provider = if (condition) providerIf else providerElse
25+
return provider.createRenderer(type, iota, x, y)
26+
}
27+
28+
companion object {
29+
val PARSER = SplicingTableIotaRendererParser<IfPathExistsRendererProvider> { _, jsonObject, _ ->
30+
IfPathExistsRendererProvider(
31+
path = jsonObject.getAsNbtPath("path"),
32+
providerIf = jsonObject.getAsIotaRendererProvider("if"),
33+
providerElse = jsonObject.getAsIotaRendererProvider("else"),
34+
)
35+
}
36+
}
37+
}

Common/src/main/kotlin/gay/object/hexdebug/utils/GsonHelper.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import com.google.gson.JsonObject
66
import com.google.gson.JsonSyntaxException
77
import com.mojang.brigadier.StringReader
88
import com.mojang.brigadier.exceptions.CommandSyntaxException
9+
import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaRendererProvider
10+
import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaRenderers
911
import net.minecraft.commands.arguments.NbtPathArgument
1012
import net.minecraft.commands.arguments.NbtPathArgument.NbtPath
1113
import net.minecraft.resources.ResourceLocation
@@ -36,3 +38,15 @@ fun JsonObject.getAsNbtPath(memberName: String, fallback: NbtPath? = null): NbtP
3638
fallback ?: throw JsonSyntaxException("Invalid $memberName, expected a valid NBT path", e)
3739
}
3840
}
41+
42+
fun JsonObject.getAsIotaRendererProvider(memberName: String): SplicingTableIotaRendererProvider {
43+
return when {
44+
GsonHelper.isStringValue(this, memberName) -> {
45+
SplicingTableIotaRenderers.loadProvider(getAsResourceLocation(memberName))
46+
}
47+
GsonHelper.isObjectNode(this, memberName) -> {
48+
SplicingTableIotaRenderers.parseProvider(getAsJsonObject(memberName))
49+
}
50+
else -> throw JsonSyntaxException("Invalid $memberName, expected a valid renderer ID or object")
51+
}
52+
}

0 commit comments

Comments
 (0)