Skip to content

Commit

Permalink
fixed tabs bug and improved scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
Thunderblade73 committed Jan 26, 2024
1 parent bc71a16 commit eec77ba
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ abstract class GuideScrollPage(
renderable = Renderable.scrollTable(
content = content,
height = sizeY - paddingY * 2,
velocity = 1.0,
xPadding = Renderable.calculateStretchXPadding(content, sizeX - paddingX * 3),
yPadding = 5,
hasHeader = hasHeader,
Expand Down
73 changes: 35 additions & 38 deletions src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import at.hannibal2.skyhanni.config.core.config.gui.GuiPositionEditor
import at.hannibal2.skyhanni.data.ToolTipData
import at.hannibal2.skyhanni.utils.ColorUtils
import at.hannibal2.skyhanni.utils.LorenzColor
import at.hannibal2.skyhanni.utils.LorenzDebug
import at.hannibal2.skyhanni.utils.LorenzLogger
import at.hannibal2.skyhanni.utils.NEUItems.renderOnScreen
import at.hannibal2.skyhanni.utils.guide.GuideGUI
import at.hannibal2.skyhanni.utils.renderables.RenderableUtils.calculateTableXOffsets
import at.hannibal2.skyhanni.utils.renderables.RenderableUtils.calculateTableYOffsets
import at.hannibal2.skyhanni.utils.renderables.RenderableUtils.renderXAligned
Expand All @@ -18,37 +20,35 @@ import net.minecraft.client.gui.GuiChat
import net.minecraft.client.gui.inventory.GuiEditSign
import net.minecraft.client.renderer.GlStateManager
import net.minecraft.item.ItemStack
import net.minecraftforge.client.event.MouseEvent
import org.lwjgl.input.Mouse
import java.awt.Color
import java.util.Collections
import kotlin.math.max

interface Renderable {

val width: Int
val height: Int

val horizontalAlign: HorizontalAlignment
val verticalAlign: VerticalAlignment
fun isHovered(posX: Int, posY: Int) = currentRenderPassMousePosition?.let { (x, y) ->
x in (posX..posX + width)
&& y in (posY..posY + height) // TODO: adjust for variable height?
x in (posX .. posX + width)
&& y in (posY .. posY + height)
} ?: false

fun scrollInput(scrollOld: Int, posX: Int, posY: Int, button: Int?, minHeight: Int, maxHeight: Int, velocity: Double) =
if (isHovered(posX, posY)) {
var scroll = scrollOld
if (button != null && Mouse.isButtonDown(button)) {
scroll += (Mouse.getEventDY() * 0.5 * velocity).toInt()
// LorenzDebug.log(Mouse.getEventDY().toString())
}
scroll += (Mouse.getEventDWheel() * velocity * 0.02).toInt()

when {
scroll > maxHeight -> maxHeight
scroll < minHeight -> minHeight
else -> scroll
}
} else scrollOld
fun scrollInput(scrollOld: Float, posX: Int, posY: Int, button: Int?, minHeight: Int, maxHeight: Int, velocity: Double) =
if (maxHeight < minHeight) minHeight.toFloat() else
if (isHovered(posX, posY)) {
var scroll = scrollOld
if (button != null && Mouse.isButtonDown(button)) {
scroll += (Mouse.getEventDY() * velocity * 1/20).toFloat()
// LorenzDebug.log(Mouse.getEventDY().toString())
}
scroll += (Mouse.getEventDWheel() * velocity * 1 / (120 * 20)).toFloat()
scroll.coerceIn(minHeight.toFloat(), maxHeight.toFloat())
} else scrollOld

/**
* Pos x and pos y are relative to the mouse position.
Expand All @@ -57,6 +57,7 @@ interface Renderable {
fun render(posX: Int, posY: Int)

companion object {

val logger = LorenzLogger("debug/renderable")
val list = mutableMapOf<Pair<Int, Int>, List<Int>>()

Expand All @@ -76,7 +77,6 @@ interface Renderable {
enum class HorizontalAlignment { Left, Center, Right }
enum class VerticalAlignment { Top, Center, Bottom }


fun fromAny(any: Any?, itemScale: Double = 1.0): Renderable? = when (any) {
null -> placeholder(12)
is Renderable -> any
Expand Down Expand Up @@ -200,17 +200,19 @@ interface Renderable {
}

private fun shouldAllowLink(debug: Boolean = false, bypassChecks: Boolean): Boolean {
val isGuiScreen = Minecraft.getMinecraft().currentScreen != null
val guiScreen = Minecraft.getMinecraft().currentScreen

val isGuiScreen = guiScreen != null
if (bypassChecks) {
return isGuiScreen
}
val isGuiPositionEditor = Minecraft.getMinecraft().currentScreen !is GuiPositionEditor
val isNotInSignAndOnSlot = if (Minecraft.getMinecraft().currentScreen !is GuiEditSign) {
val isGuiPositionEditor = guiScreen !is GuiPositionEditor
val isNotInSignAndOnSlot = if (guiScreen !is GuiEditSign && guiScreen !is GuideGUI<*>) {
ToolTipData.lastSlot == null
} else true
val isConfigScreen = Minecraft.getMinecraft().currentScreen !is GuiScreenElementWrapper
val isConfigScreen = guiScreen !is GuiScreenElementWrapper

val openGui = Minecraft.getMinecraft().currentScreen?.javaClass?.name ?: "none"
val openGui = guiScreen?.javaClass?.name ?: "none"
val isInNeuPv = openGui == "io.github.moulberry.notenoughupdates.profileviewer.GuiProfileViewer"
val isInSkyTilsPv = openGui == "gg.skytils.skytilsmod.gui.profile.ProfileGui"

Expand Down Expand Up @@ -273,7 +275,6 @@ interface Renderable {
unhovered.render(posX, posY)
isHovered = false
}

}
}

Expand Down Expand Up @@ -348,16 +349,17 @@ interface Renderable {

private val virtualHeight = list.maxOf { it.height }

var scroll = 0
var scroll = 0f
val scrollInt get() = scroll.toInt()

private val end get() = scroll + height
private val end get() = scrollInt + height

override fun render(posX: Int, posY: Int) {
scroll = scrollInput(scroll, posX, posY, button, 0, virtualHeight - height, velocity)
var renderY = 0
var virtualY = 0
list.forEach {
if (virtualY in scroll..end) {
if (virtualY in scrollInt .. end) {
it.renderXAligned(posX, posY + renderY, width)
GlStateManager.translate(0f, it.height.toFloat(), 0f)
renderY += it.height
Expand All @@ -366,7 +368,6 @@ interface Renderable {
}
GlStateManager.translate(0f, -renderY.toFloat(), 0f)
}

}

fun scrollTable(
Expand All @@ -391,15 +392,15 @@ interface Renderable {

private val virtualHeight = yOffsets.last() - yPadding

private val end get() = scroll + height - yPadding - 2 // TODO fix the -2 "fix"
private val end get() = scrollInt + height - yPadding - 2 // TODO fix the -2 "fix"
private val minHeight = if (hasHeader) yOffsets[1] else 0

var scroll = minHeight
var scroll = minHeight.toFloat()

val scrollInt get() = scroll.toInt()

override fun render(posX: Int, posY: Int) {
scroll = if (virtualHeight > height)
scrollInput(scroll, posX, posY, button, minHeight, virtualHeight - height, velocity)
else minHeight
scroll = scrollInput(scroll, posX, posY, button, minHeight, virtualHeight - height, velocity)
var renderY = 0
if (hasHeader) {
content[0].forEachIndexed { index, renderable ->
Expand All @@ -411,7 +412,7 @@ interface Renderable {
GlStateManager.translate(0f, yShift.toFloat(), 0f)
renderY += yShift
}
val range = yOffsets.indexOfFirst { it >= scroll }..<(yOffsets.indexOfFirst { it >= end }.takeIf { it > 0 }
val range = yOffsets.indexOfFirst { it >= scrollInt } ..< (yOffsets.indexOfFirst { it >= end }.takeIf { it > 0 }
?: (yOffsets.size - 1))
for (rowIndex in range) {
content[rowIndex].forEachIndexed { index, renderable ->
Expand All @@ -431,7 +432,6 @@ interface Renderable {
}
GlStateManager.translate(0f, -renderY.toFloat(), 0f)
}

}

/**
Expand Down Expand Up @@ -486,7 +486,6 @@ interface Renderable {
Gui.drawRect(1, 1, width - 1, height - 1, color.darker().rgb)
Gui.drawRect(1, 1, progress, height - 1, color.rgb)
}

}

fun wrappedString(
Expand Down Expand Up @@ -517,7 +516,6 @@ interface Renderable {
}
}


fun calculateStretchXPadding(content: List<List<Renderable?>>, xSpace: Int): Int {
if (content.isEmpty()) return xSpace
val xWidth = content.maxOf { it.sumOf { it?.width ?: 0 } }
Expand Down Expand Up @@ -581,6 +579,5 @@ interface Renderable {
GlStateManager.translate(0f, -yOffset.toFloat(), 0f)
}
}

}
}

0 comments on commit eec77ba

Please sign in to comment.