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