From 667553fb3d1c86ddb4d143b8f239cea2640fc661 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sat, 22 Oct 2022 00:51:51 +0700 Subject: [PATCH] Fix inventory scrollbar in regular guis --- .../mc/otm/client/ClientEventHandler.kt | 4 +- .../mc/otm/client/screen/DriveViewerScreen.kt | 4 +- .../client/screen/ExoSuitInventoryScreen.kt | 4 +- .../mc/otm/client/screen/ItemMonitorScreen.kt | 4 +- .../mc/otm/client/screen/MatterPanelScreen.kt | 7 ++- .../mc/otm/client/screen/MatteryScreen.kt | 4 +- .../client/screen/panels/EffectListPanel.kt | 5 +-- .../kotlin/ru/dbotthepony/mc/otm/core/Math.kt | 43 ++++++++++++++++++- 8 files changed, 57 insertions(+), 18 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/ClientEventHandler.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/ClientEventHandler.kt index 8c61aaeeb..f117717ba 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/ClientEventHandler.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/ClientEventHandler.kt @@ -23,7 +23,7 @@ import ru.dbotthepony.mc.otm.client.screen.panels.Panel2Widget import ru.dbotthepony.mc.otm.compat.InventoryScrollPacket import ru.dbotthepony.mc.otm.compat.cos.isCosmeticArmorScreen import ru.dbotthepony.mc.otm.core.TranslatableComponent -import ru.dbotthepony.mc.otm.core.maxScrollDivision +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 @@ -146,7 +146,7 @@ private fun exosuitInventoryLogic(event: ScreenEvent.Init.Post) { .max { o1, o2 -> o1.x.compareTo(o2.x) } .get().x.toFloat() + 26f - val scrollbar = DiscreteScrollBarPanel(screen, null, { maxScrollDivision(matteryPlayer.exoSuitContainer.containerSize, 9) }, { _, _, newScroll -> + val scrollbar = DiscreteScrollBarPanel(screen, null, { integerDivisionUp(matteryPlayer.exoSuitContainer.containerSize, 9) }, { _, _, newScroll -> inventoryScroll = newScroll MenuNetworkChannel.sendToServer(InventoryScrollPacket(newScroll).also { it.play(player) }) }, isSlim = true) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/DriveViewerScreen.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/DriveViewerScreen.kt index e1c0e931a..2eada543a 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/DriveViewerScreen.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/DriveViewerScreen.kt @@ -8,7 +8,7 @@ import ru.dbotthepony.mc.otm.core.TranslatableComponent import ru.dbotthepony.mc.otm.client.screen.panels.* import ru.dbotthepony.mc.otm.client.screen.panels.buttons.CheckBoxLabelInputPanel import ru.dbotthepony.mc.otm.client.screen.widget.PowerGaugePanel -import ru.dbotthepony.mc.otm.core.maxScrollDivision +import ru.dbotthepony.mc.otm.core.integerDivisionDown import ru.dbotthepony.mc.otm.item.PortableCondensationDriveItem import ru.dbotthepony.mc.otm.menu.DriveViewerMenu import yalter.mousetweaks.api.MouseTweaksDisableWheelTweak @@ -49,7 +49,7 @@ class DriveViewerScreen(menu: DriveViewerMenu, inventory: Inventory, title: Comp val grid = GridPanel(this, frame, 28f, 16f, GRID_WIDTH * 18f, GRID_HEIGHT * 18f, GRID_WIDTH, GRID_HEIGHT) - val scrollBar = DiscreteScrollBarPanel(this, frame, { maxScrollDivision(menu.networkedItemView.itemCount, GRID_WIDTH) }, { _, _, _ -> }, 192f, 14f, 92f) + val scrollBar = DiscreteScrollBarPanel(this, frame, { integerDivisionDown(menu.networkedItemView.itemCount, GRID_WIDTH) }, { _, _, _ -> }, 192f, 14f, 92f) views.add(grid) views.add(scrollBar) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/ExoSuitInventoryScreen.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/ExoSuitInventoryScreen.kt index 5fc8f7b66..536c0050d 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/ExoSuitInventoryScreen.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/ExoSuitInventoryScreen.kt @@ -11,7 +11,7 @@ import ru.dbotthepony.mc.otm.client.screen.panels.* import ru.dbotthepony.mc.otm.client.screen.panels.buttons.LargeRectangleButtonPanel import ru.dbotthepony.mc.otm.client.setMousePos import ru.dbotthepony.mc.otm.client.shouldOpenVanillaInventory -import ru.dbotthepony.mc.otm.core.maxScrollDivision +import ru.dbotthepony.mc.otm.core.integerDivisionDown import ru.dbotthepony.mc.otm.menu.ExoSuitInventoryMenu import ru.dbotthepony.mc.otm.network.ExoSuitMenuOpen import ru.dbotthepony.mc.otm.network.MatteryPlayerNetworkChannel @@ -49,7 +49,7 @@ class ExoSuitInventoryScreen(menu: ExoSuitInventoryMenu) : MatteryScreen diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/ItemMonitorScreen.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/ItemMonitorScreen.kt index cd31a536b..9e8cdc170 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/ItemMonitorScreen.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/ItemMonitorScreen.kt @@ -17,7 +17,7 @@ import ru.dbotthepony.mc.otm.client.screen.panels.buttons.SmallEnumRectangleButt import ru.dbotthepony.mc.otm.client.screen.widget.ProgressGaugePanel import ru.dbotthepony.mc.otm.client.screen.widget.WidePowerGaugePanel import ru.dbotthepony.mc.otm.core.asGetterSetter -import ru.dbotthepony.mc.otm.core.maxScrollDivision +import ru.dbotthepony.mc.otm.core.integerDivisionDown import ru.dbotthepony.mc.otm.core.formatReadableNumber import ru.dbotthepony.mc.otm.core.formatSiComponent import ru.dbotthepony.mc.otm.menu.ItemMonitorMenu @@ -44,7 +44,7 @@ class ItemMonitorScreen(menu: ItemMonitorMenu, inventory: Inventory, title: Comp frame.width = 178f + frame.dockPadding.left + frame.dockPadding.right val viewScrollBar = DiscreteScrollBarPanel(this, topPanel, - { maxScrollDivision(menu.networkedItemView.itemCount, ITEM_GRID_WIDTH) }, + { integerDivisionDown(menu.networkedItemView.itemCount, ITEM_GRID_WIDTH) }, { _, _, _ -> }, 28f + ITEM_GRID_WIDTH * 18f + 2f, 16f, ITEM_GRID_HEIGHT * 18f) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/MatterPanelScreen.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/MatterPanelScreen.kt index ec4572c8f..0453dc22f 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/MatterPanelScreen.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/MatterPanelScreen.kt @@ -8,12 +8,11 @@ import net.minecraft.world.item.ItemStack import ru.dbotthepony.mc.otm.capability.matter.IPatternState import ru.dbotthepony.mc.otm.capability.matter.IReplicationTask import ru.dbotthepony.mc.otm.client.render.WidgetLocation -import ru.dbotthepony.mc.otm.client.render.element import ru.dbotthepony.mc.otm.client.render.subElement import ru.dbotthepony.mc.otm.client.screen.panels.* import ru.dbotthepony.mc.otm.client.screen.panels.buttons.ButtonPanel import ru.dbotthepony.mc.otm.core.TranslatableComponent -import ru.dbotthepony.mc.otm.core.maxScrollDivision +import ru.dbotthepony.mc.otm.core.integerDivisionDown import ru.dbotthepony.mc.otm.menu.MatterPanelMenu import ru.dbotthepony.mc.otm.menu.ReplicationRequestPacket import ru.dbotthepony.mc.otm.network.MenuNetworkChannel @@ -32,9 +31,9 @@ class MatterPanelScreen( val scrollBar = DiscreteScrollBarPanel(this, frame, { if (isPatternView) { - maxScrollDivision(menu.patterns.size, GRID_WIDTH) + integerDivisionDown(menu.patterns.size, GRID_WIDTH) } else { - maxScrollDivision(menu.tasks.size, GRID_WIDTH) + integerDivisionDown(menu.tasks.size, GRID_WIDTH) } }, { _, _, _ -> }) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/MatteryScreen.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/MatteryScreen.kt index 0468f706d..30fe8b175 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/MatteryScreen.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/MatteryScreen.kt @@ -20,7 +20,7 @@ import ru.dbotthepony.mc.otm.ClientConfig import ru.dbotthepony.mc.otm.client.moveMousePosScaled import ru.dbotthepony.mc.otm.client.screen.panels.* import ru.dbotthepony.mc.otm.compat.cos.CosmeticToggleButton -import ru.dbotthepony.mc.otm.core.maxScrollDivision +import ru.dbotthepony.mc.otm.core.integerDivisionDown import ru.dbotthepony.mc.otm.menu.MatteryMenu import java.util.Collections @@ -138,7 +138,7 @@ abstract class MatteryScreen(menu: T, inventory: Inventory, tit } else { inventoryFrame = FramePanel>(this, null, 0f, 0f, INVENTORY_FRAME_WIDTH_EXTENDED, BASE_INVENTORY_FRAME_HEIGHT + AbstractSlotPanel.SIZE * inventoryRows, inventory.displayName).also(this::addPanel) - inventoryScrollbar = DiscreteScrollBarPanel(this, inventoryFrame, { maxScrollDivision(menu.playerCombinedInventorySlots.size, 9) }, { + inventoryScrollbar = DiscreteScrollBarPanel(this, inventoryFrame, { integerDivisionDown(menu.playerCombinedInventorySlots.size, 9) }, { _, old, new -> for (i in old until old + inventoryRows) { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/EffectListPanel.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/EffectListPanel.kt index e890f537d..cb7f6de5c 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/EffectListPanel.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/EffectListPanel.kt @@ -8,7 +8,6 @@ import net.minecraft.client.gui.screens.Screen import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen import net.minecraft.world.effect.MobEffect import net.minecraft.world.effect.MobEffectInstance -import net.minecraft.world.effect.MobEffectUtil import net.minecraft.world.entity.LivingEntity import ru.dbotthepony.mc.otm.client.minecraft import ru.dbotthepony.mc.otm.client.render.TextAlign @@ -20,7 +19,7 @@ import ru.dbotthepony.mc.otm.client.render.render import ru.dbotthepony.mc.otm.core.RGBAColor import ru.dbotthepony.mc.otm.core.TranslatableComponent import ru.dbotthepony.mc.otm.core.formatTickDuration -import ru.dbotthepony.mc.otm.core.maxScrollDivision +import ru.dbotthepony.mc.otm.core.integerDivisionDown import java.util.stream.Collectors open class EffectListPanel @JvmOverloads constructor( @@ -156,7 +155,7 @@ open class EffectListPanel @JvmOverloads constructor( protected val effectButtons: MutableMap = Object2ObjectArrayMap() fun calculateMaxScroll(scrollBarPanel: DiscreteScrollBarPanel<*>): Int { - return maxScrollDivision(entity.activeEffects.size, gridWidth) + return integerDivisionDown(entity.activeEffects.size, gridWidth) } fun onScrolled(scrollBarPanel: DiscreteScrollBarPanel<*>, oldScroll: Int, newScroll: Int) { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/Math.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/Math.kt index 5ffd435ed..8903ae352 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/Math.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/Math.kt @@ -31,11 +31,52 @@ fun BigInteger.toIntSafe(): Int { return toInt() } +/** + * 0, 9 -> 0 + * 1, 9 -> 0 + * 8, 9 -> 0 + * 9, 9 -> 0 + * 10, 9 -> 1 + * 17, 9 -> 1 + * 18, 9 -> 1 + * 19, 9 -> 2 + */ @Suppress("SameParameterValue") -fun maxScrollDivision(elements: Int, elementsPerRow: Int): Int { +fun integerDivisionDown(elements: Int, elementsPerRow: Int): Int { + require(elements >= 0) { "Invalid amount of elements $elements" } + + if (elements == 0) { + return 0 + } + if (elements % elementsPerRow == 0) { return elements / elementsPerRow - 1 } return elements / elementsPerRow } + +/** + * 0, 9 -> 0 + * 1, 9 -> 1 + * 8, 9 -> 1 + * 9, 9 -> 1 + * 10, 9 -> 2 + * 17, 9 -> 2 + * 18, 9 -> 2 + * 19, 9 -> 3 + */ +@Suppress("SameParameterValue") +fun integerDivisionUp(elements: Int, elementsPerRow: Int): Int { + require(elements >= 0) { "Invalid amount of elements $elements" } + + if (elements == 0) { + return 0 + } + + if (elements % elementsPerRow == 0) { + return elements / elementsPerRow + } + + return elements / elementsPerRow + 1 +}