diff options
author | Thunderblade73 <85900443+Thunderblade73@users.noreply.github.com> | 2024-04-07 11:14:09 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-07 11:14:09 +0200 |
commit | 03c416202ccdec17cc46f6d07aae111fa977771a (patch) | |
tree | a2454448f9df68f70902519d1da352c22905398a /src/main/java/at/hannibal2 | |
parent | c9a96559bd9b017978e39758d63ee15b348275b0 (diff) | |
download | skyhanni-03c416202ccdec17cc46f6d07aae111fa977771a.tar.gz skyhanni-03c416202ccdec17cc46f6d07aae111fa977771a.tar.bz2 skyhanni-03c416202ccdec17cc46f6d07aae111fa977771a.zip |
Backend: Renderable table (#822)
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
Co-authored-by: Walker Selby <git@walkerselby.com>
Co-authored-by: appable <enzospiacitelli@gmail.com>
Co-authored-by: alexia <me@alexia.lol>
Co-authored-by: CalMWolfs <94038482+CalMWolfs@users.noreply.github.com>
Co-authored-by: Empa <42304516+ItsEmpa@users.noreply.github.com>
Co-authored-by: Obsidian <108832807+Obsidianninja11@users.noreply.github.com>
Co-authored-by: Linnea Gräf <nea@nea.moe>
Co-authored-by: J10a1n15 <45315647+j10a1n15@users.noreply.github.com>
Co-authored-by: Dylan <61059252+DylanBruner@users.noreply.github.com>
Co-authored-by: Matthias <40476420+byWambo@users.noreply.github.com>
Diffstat (limited to 'src/main/java/at/hannibal2')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt | 58 | ||||
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderableUtils.kt | 25 |
2 files changed, 73 insertions, 10 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt b/src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt index 410a00cc8..92d5a4cf9 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt @@ -13,7 +13,10 @@ import at.hannibal2.skyhanni.utils.LorenzLogger import at.hannibal2.skyhanni.utils.NEUItems.renderOnScreen import at.hannibal2.skyhanni.utils.RenderUtils.HorizontalAlignment import at.hannibal2.skyhanni.utils.RenderUtils.VerticalAlignment +import at.hannibal2.skyhanni.utils.renderables.RenderableUtils.calculateTableXOffsets +import at.hannibal2.skyhanni.utils.renderables.RenderableUtils.calculateTableYOffsets import at.hannibal2.skyhanni.utils.renderables.RenderableUtils.renderXAligned +import at.hannibal2.skyhanni.utils.renderables.RenderableUtils.renderXYAligned import at.hannibal2.skyhanni.utils.renderables.RenderableUtils.renderYAligned import io.github.moulberry.moulconfig.gui.GuiScreenElementWrapper import io.github.moulberry.notenoughupdates.util.Utils @@ -314,16 +317,6 @@ interface Renderable { } } - fun placeholder(width: Int, height: Int = 10) = object : Renderable { - override val width = width - override val height = height - override val horizontalAlign = HorizontalAlignment.LEFT - override val verticalAlign = VerticalAlignment.TOP - - override fun render(posX: Int, posY: Int) { - } - } - fun wrappedString( text: String, width: Int, @@ -365,6 +358,51 @@ interface Renderable { } } + fun placeholder(width: Int, height: Int = 10) = object : Renderable { + override val width = width + override val height = height + override val horizontalAlign = HorizontalAlignment.LEFT + override val verticalAlign = VerticalAlignment.TOP + + override fun render(posX: Int, posY: Int) { + } + } + + /** + * @param content the list of rows the table should render + */ + fun table( + content: List<List<Renderable?>>, + xPadding: Int = 1, + yPadding: Int = 0, + horizontalAlign: HorizontalAlignment = HorizontalAlignment.LEFT, + verticalAlign: VerticalAlignment = VerticalAlignment.TOP, + ) = object : Renderable { + val xOffsets: List<Int> = calculateTableXOffsets(content, xPadding) + val yOffsets: List<Int> = calculateTableYOffsets(content, yPadding) + override val horizontalAlign = horizontalAlign + override val verticalAlign = verticalAlign + + override val width = xOffsets.last() - xPadding + override val height = yOffsets.last() - yPadding + + override fun render(posX: Int, posY: Int) { + content.forEachIndexed { rowIndex, row -> + row.forEachIndexed { index, renderable -> + GlStateManager.pushMatrix() + GlStateManager.translate(xOffsets[index].toFloat(), yOffsets[rowIndex].toFloat(), 0F) + renderable?.renderXYAligned( + posX + xOffsets[index], + posY + yOffsets[rowIndex], + xOffsets[index + 1] - xOffsets[index], + yOffsets[rowIndex + 1] - yOffsets[rowIndex] + ) + GlStateManager.popMatrix() + } + } + } + } + fun progressBar( percent: Double, startColor: Color = Color(255, 0, 0), diff --git a/src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderableUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderableUtils.kt index 9c6b94a55..70969d8fd 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderableUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderableUtils.kt @@ -6,6 +6,31 @@ import net.minecraft.client.renderer.GlStateManager internal object RenderableUtils { + /** Calculates the absolute x position of the columns in a table*/ + fun calculateTableXOffsets(content: List<List<Renderable?>>, xPadding: Int) = run { + var buffer = 0 + var index = 0 + buildList { + add(0) + while (true) { + buffer += content.map { it.getOrNull(index) }.takeIf { it.any { it != null } }?.maxOf { + it?.width ?: 0 + }?.let { it + xPadding } ?: break + add(buffer) + index++ + } + } + } + + /** Calculates the absolute y position of the rows in a table*/ + fun calculateTableYOffsets(content: List<List<Renderable?>>, yPadding: Int) = run { + var buffer = 0 + listOf(0) + content.map { row -> + buffer += row.maxOf { it?.height ?: 0 } + yPadding + buffer + } + } + private fun calculateAlignmentXOffset(renderable: Renderable, xSpace: Int) = when (renderable.horizontalAlign) { HorizontalAlignment.LEFT -> 0 HorizontalAlignment.CENTER -> (xSpace - renderable.width) / 2 |