Re-opening GUIs no longer evaporate scrollbar due to already scrolled slots

Fixes #150
This commit is contained in:
DBotThePony 2022-10-24 18:04:01 +07:00
parent d3e2b1928a
commit 0cc753e035
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -31,6 +31,7 @@ import ru.dbotthepony.mc.otm.core.integerDivisionUp
import ru.dbotthepony.mc.otm.menu.MatteryMenu
import ru.dbotthepony.mc.otm.network.MenuNetworkChannel
import ru.dbotthepony.mc.otm.registry.AndroidFeatures
import java.util.WeakHashMap
import java.util.function.Consumer
fun onMovementInputUpdate(event: MovementInputUpdateEvent) {
@ -115,6 +116,9 @@ private class InventoryScrollbarPanel<S : Screen>(screen: S, matteryPlayer: Matt
}, isSlim = true
)
private data class ScreenScrollbarConfig(val x: Float, val y: Float, val height: Float)
private val foundScreenConfigurations = WeakHashMap<AbstractContainerScreen<*>, ScreenScrollbarConfig>()
private fun exosuitInventoryLogic(screen: Screen, addListener: (GuiEventListener) -> Unit) {
val player = minecraft.player ?: return
val matteryPlayer = player.matteryPlayer ?: return
@ -127,6 +131,7 @@ private fun exosuitInventoryLogic(screen: Screen, addListener: (GuiEventListener
return
}
val existingConfig = foundScreenConfigurations[screen]
val foundInventorySlots = Int2ObjectArrayMap<Slot>()
for (slot in screen.menu.slots) {
@ -135,34 +140,51 @@ private fun exosuitInventoryLogic(screen: Screen, addListener: (GuiEventListener
}
}
var passed = true
for (i in 9 .. 35) {
if (i !in foundInventorySlots) {
return
if (existingConfig == null) {
return
} else {
passed = false
break
}
}
}
val mostTop = foundInventorySlots.values
.stream()
.min { o1, o2 -> o1.y.compareTo(o2.y) }
.get().y.toFloat()
val config: ScreenScrollbarConfig
val mostBottom = foundInventorySlots.values
.stream()
.max { o1, o2 -> o1.y.compareTo(o2.y) }
.get().y.toFloat()
if (passed) {
val mostTop = foundInventorySlots.values
.stream()
.min { o1, o2 -> o1.y.compareTo(o2.y) }
.get().y.toFloat()
val mostLeft = foundInventorySlots.values
.stream()
.max { o1, o2 -> o1.x.compareTo(o2.x) }
.get().x.toFloat() + 26f
val mostBottom = foundInventorySlots.values
.stream()
.max { o1, o2 -> o1.y.compareTo(o2.y) }
.get().y.toFloat()
val mostLeft = foundInventorySlots.values
.stream()
.max { o1, o2 -> o1.x.compareTo(o2.x) }
.get().x.toFloat() + 26f
config = ScreenScrollbarConfig(mostLeft + screen.guiLeft - 9f, mostTop + screen.guiTop - 1f, mostBottom - mostTop + AbstractSlotPanel.SIZE)
foundScreenConfigurations[screen] = config
} else if (existingConfig != null) {
config = existingConfig
} else {
return
}
val findScrollbar = (screen.renderables.firstOrNull { it is Panel2Widget<*, *> && it.panel is InventoryScrollbarPanel } as Panel2Widget<*, *>?)?.panel as InventoryScrollbarPanel?
val scrollbar = findScrollbar ?: InventoryScrollbarPanel(screen, matteryPlayer)
// HOW
scrollbar.x = mostLeft + screen.guiLeft - 9f
scrollbar.y = mostTop + screen.guiTop - 1f
scrollbar.height = mostBottom - mostTop + AbstractSlotPanel.SIZE
scrollbar.x = config.x
scrollbar.y = config.y
scrollbar.height = config.height
if (findScrollbar == null) {
val widget = Panel2Widget(scrollbar)