aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt58
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderableUtils.kt25
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