Skip to content

Commit

Permalink
Widget positional/z-index/mouse improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ATPStorages committed Aug 31, 2024
1 parent 53a7406 commit 614fde5
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 72 deletions.
13 changes: 11 additions & 2 deletions src/main/java/breadmod/mixin/client/MixinGameRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,17 @@

@Mixin(net.minecraft.client.renderer.GameRenderer.class)
abstract class MixinGameRenderer {
@Inject(method = "render", at = @At(value = "INVOKE", target = " net.minecraft.client.renderer.LevelRenderer.doEntityOutline()V"))
void processRegistryShaders(float pPartialTicks, long pNanoTime, boolean pRenderLevel, CallbackInfo ci) {
@SuppressWarnings({"MethodMayBeStatic", "ChainedMethodCall", "NestedMethodCall"})
@Inject(
method = "render", at = @At(value = "INVOKE",
target = "net.minecraft.client.renderer.LevelRenderer.doEntityOutline()V")
)
private void processRegistryShaders(
final float pPartialTicks,
final long pNanoTime,
final boolean pRenderLevel,
final CallbackInfo ci
) {
RenderSystem.disableBlend();
RenderSystem.disableDepthTest();
RenderSystem.resetTextureMatrix();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,9 @@ internal object SoundBlockScreenFactory : SerializedScreenFactory<SoundBlockMenu
0, 0,
176, 166,
modLocation("textures", "gui", "container", "sound_block.png")
) to (0.0 to "background"),
// EditBox(
// rgMinecraft.font, 4, 7,
// 160, 10,
// Component.empty()
// ).also { editBox ->
// editBox.value = "sound input WIP"
// editBox.setResponder {
// println(editBox.value)
// }
// } to (0.1 to null)
)
) to (0.0 to "background")
),
true
)

override fun create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package breadmod.client.screen.sound_block
import breadmod.menu.block.SoundBlockMenu
import breadmod.util.gui.SerializedScreen
import breadmod.util.gui.widget.*
import breadmod.util.gui.widget.ready.InventoryWidget
import breadmod.util.render.rgMinecraft
import com.mojang.blaze3d.platform.InputConstants
import net.minecraft.client.gui.GuiGraphics
import net.minecraft.network.chat.Component
import net.minecraft.world.entity.player.Inventory
import java.awt.Color
import java.util.*

internal class SoundBlockSerializedScreen(
pMenu: SoundBlockMenu,
Expand Down Expand Up @@ -49,15 +51,13 @@ internal class SoundBlockSerializedScreen(
)

rootWidget.addWidget(
title,
200.0
title
).addWidget(
InventoryWidget(8, 88, pInventory, pMenu),
0.0
InventoryWidget(8, 88, pInventory, pMenu)
).addWidget(
ItemContainerWidget(10, 10, 16, 16, pMenu.slots[36].container, pMenu).also {
it.addWidget(SlotWidget(0, 0, it, 36), 0.0)
}, 0.0
it.addWidget(SlotWidget(0, 0, it, 36))
}
).addWidget(
ListContainerWidget(
image.x + 7, title.y + title.height, image.width - 14, 70, 6,
Expand All @@ -66,32 +66,41 @@ internal class SoundBlockSerializedScreen(
BackgroundWidget.SolidColorBackgroundWidget(
0, 0, it.width, it.height,
Color.DARK_GRAY
) to (1.0 to null),
) to (0.0 to null),
BackgroundWidget.SolidColorBackgroundWidget(
2, 2, it.width, it.height - 4,
Color.BLACK
) to (2.0 to null)
) to (1.0 to null)
)
}, {
mutableMapOf(
BackgroundWidget.SolidColorBackgroundWidget(
0, 0, it.width, it.height,
Color.DARK_GRAY
) to (3.0 to null),
) to (2.0 to null),
BackgroundWidget.SolidColorBackgroundWidget(
1, 2, it.width - 3, it.height - 4,
Color.BLACK
) to (4.0 to null),
) to (3.0 to null),
ScrollBarWidget(it) to (0.0 to "scrollbar")
)
}
), 0.0, "list"
), pTag = "list"
)

val list = rootWidget.getTaggedWidget("list") as ListContainerWidget
val newRow = RowContainerWidget(0, 0, rgMinecraft.font.lineHeight + 2)
newRow.addWidget(BackgroundWidget.SolidColorBackgroundWidget(0, 0, list.width, newRow.height, Color.BLUE), 10000.0)
val random = Random()

list.addWidget(newRow, 300.0)
repeat(30) {
val newRow = RowContainerWidget(0, 0, rgMinecraft.font.lineHeight + 2)
newRow.addWidget(
BackgroundWidget.SolidColorBackgroundWidget(
0, 0,
list.width, newRow.height,
Color(random.nextFloat(), random.nextFloat(), random.nextFloat(), 1f)
)
)
list.addWidget(newRow)
}
}
}
66 changes: 44 additions & 22 deletions src/main/kotlin/breadmod/util/gui/widget/ContainerWidget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ import org.joml.Matrix4f
import kotlin.reflect.KClass
import kotlin.reflect.full.isSubclassOf

/**
* A map of widgets usually contained in a [ContainerWidget].
* @author Miko Elbrecht
* @since 1.0.0
*/
typealias ContainedWidgets = MutableMap<AbstractWidget, Pair<Double, String?>>

/**
Expand All @@ -38,7 +43,8 @@ open class ContainerWidget(
pWidth: Int, pHeight: Int,
private val pTilt: Float,
pComponent: Component,
private val childrenWidgets: ContainedWidgets
private val childrenWidgets: ContainedWidgets,
private val rootWidget: Boolean = false
) : AbstractWidget(pX, pY, pWidth, pHeight, pComponent), IWidgetOffsetAware {
private val associationMap: MutableMap<String, AbstractWidget> = mutableMapOf()

Expand Down Expand Up @@ -92,7 +98,7 @@ open class ContainerWidget(
* @author Miko Elbrecht
* @since 1.0
*/
open fun addWidget(pWidget: AbstractWidget, pZIndex: Double, pTag: String? = null): ContainerWidget = this.also {
open fun addWidget(pWidget: AbstractWidget, pZIndex: Double = 0.0, pTag: String? = null): ContainerWidget = this.also {
childrenWidgets[pWidget] = pZIndex to pTag
if (pTag != null) associationMap[pTag] = pWidget
}
Expand All @@ -108,6 +114,36 @@ open class ContainerWidget(
fun getTaggedWidget(pTag: String): AbstractWidget = associationMap[pTag]
?: throw NoSuchElementException("No widget with tag $pTag")

/**
* Facilitates the rendering of this [ContainerWidget].
* @see renderWidget
* @author Miko Elbrecht
* @since 1.0.0
*/
override fun render(pGuiGraphics: GuiGraphics, pMouseX: Int, pMouseY: Int, pPartialTick: Float) {
if (this.visible) {
val pose = pGuiGraphics.pose()
pose.pushPose()

pose.mulPoseMatrix(Matrix4f().rotateX(pTilt))
if (rootWidget) {
pose.translate(x.toDouble(), y.toDouble(), 1.0)
absoluteX = x
absoluteY = y
} else pose.translate(0.0, 0.0, 1.0)

isHovered = (pMouseX >= x) &&
(pMouseY >= y) &&
(pMouseX < x + width) &&
(pMouseY < y + height)

renderWidget(pGuiGraphics, pMouseX - x, pMouseY - y, pPartialTick)
updateTooltip()

pose.popPose()
}
}

/**
* Renders this [ContainerWidget] along with its children.
* @param pGuiGraphics The graphics context to render with.
Expand All @@ -119,32 +155,18 @@ open class ContainerWidget(
*/
override fun renderWidget(pGuiGraphics: GuiGraphics, pMouseX: Int, pMouseY: Int, pPartialTick: Float) {
val pose = pGuiGraphics.pose()
pose.pushPose()
var aX = absoluteX + this.x
var aY = absoluteY + this.y
if (this.x > 0 && this.y > 0) pose.translate(this.x.toDouble(), this.y.toDouble(), 0.0)
pose.mulPoseMatrix(Matrix4f().rotateX(pTilt))
childrenWidgets.forEach { (widget, pair) ->
pose.pushPose()
// NOTE: This code is quite messy. We should probably clean it up later.
if (widget !is ContainerWidget) {
aX += widget.x
aY += widget.y
pose.translate(widget.x.toDouble(), widget.y.toDouble(), pair.first)
}
pose.translate(widget.x.toDouble(), widget.y.toDouble(), pair.first)

if (widget is IWidgetOffsetAware) {
widget.absoluteX = aX
widget.absoluteY = aY
widget.absoluteX = absoluteX + widget.x
widget.absoluteY = absoluteY + widget.y
}
widget.render(
pGuiGraphics,
pMouseX - this.x,
pMouseY - this.y,
pPartialTick
)

widget.render(pGuiGraphics, pMouseX, pMouseY, pPartialTick)
pose.popPose()
}
pose.popPose()
}

private fun ContainerWidget.iterateLowHigh(
Expand Down
38 changes: 17 additions & 21 deletions src/main/kotlin/breadmod/util/gui/widget/ListContainerWidget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import breadmod.util.gui.widget.marker.IWidgetOffsetAware
import net.minecraft.client.gui.GuiGraphics
import net.minecraft.client.gui.components.AbstractWidget
import net.minecraft.network.chat.Component
import java.awt.Color
import kotlin.math.max

/**
Expand Down Expand Up @@ -66,38 +67,30 @@ class ListContainerWidget(
throw UnsupportedOperationException("ListContainerWidget only supports RowContainerWidgets")
pWidget.width = width
scrollContentsContainer.height += pWidget.height
pWidget.y = scrollContentsContainer.height
scrollBar.height = scrollBarContainer.height *
(scrollBarContainer.height / max(scrollContentsContainer.height, scrollBarContainer.height))
return scrollContentsContainer.addWidget(pWidget, pZIndex, pTag)
return this
}

override fun renderWidget(pGuiGraphics: GuiGraphics, pMouseX: Int, pMouseY: Int, pPartialTick: Float) {
val pose = pGuiGraphics.pose()
pose.pushPose()
pose.translate(x.toDouble(), y.toDouble(), 0.0)
val pMx = pMouseX - x
val pMy = pMouseX - x

fun positioning(widget: ContainerWidget, localZ: Double) {
fun positioning(widget: ContainerWidget) {
pose.pushPose()
pose.translate(widget.x.toDouble(), widget.y.toDouble(), localZ)
widget.render(pGuiGraphics, pMx - widget.x, pMy - widget.y, pPartialTick)
pose.translate(widget.x.toDouble(), widget.y.toDouble(), 0.0)
widget.render(
pGuiGraphics,
pMouseX, pMouseX,
pPartialTick
)
pGuiGraphics.fill(0, 0, widget.width, widget.height, Color(0.5f, 0f, 0f, 0.5f).rgb)
pose.popPose()
}

positioning(scrollContentsBackgroundContainer, 1.0)

val sccAbsX = absoluteX + scrollContentsContainer.x
val sccAbsY = absoluteY + scrollContentsContainer.y
// pGuiGraphics.enableScissor(
// sccAbsX, sccAbsY,
// sccAbsX + scrollContentsContainer.width, sccAbsY + scrollContentsContainer.height
// )
positioning(scrollContentsContainer, 2.0)
// pGuiGraphics.disableScissor()

positioning(scrollBarContainer, 3.0)
pose.popPose()
positioning(scrollContentsBackgroundContainer)
positioning(scrollContentsContainer)
positioning(scrollBarContainer)
}

init {
Expand All @@ -109,8 +102,11 @@ class ListContainerWidget(
scrollContentsBackgroundContainer.addWidget(widget, pair.first, pair.second)
}

@Suppress("RedundantValueArgument")
super.addWidget(scrollContentsBackgroundContainer, 0.0, "background")
@Suppress("RedundantValueArgument")
super.addWidget(scrollContentsContainer, 0.0, "contents")
@Suppress("RedundantValueArgument")
super.addWidget(scrollBarContainer, 0.0, "scroll")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package breadmod.util.gui.widget
package breadmod.util.gui.widget.ready

import breadmod.util.gui.widget.ItemContainerWidget
import breadmod.util.gui.widget.SlotWidget
import net.minecraft.world.entity.player.Inventory
import net.minecraft.world.inventory.AbstractContainerMenu

Expand Down

0 comments on commit 614fde5

Please sign in to comment.