diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/Ext.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/Ext.kt index 4e45b1e5e..d28465c7e 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/Ext.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/Ext.kt @@ -20,6 +20,18 @@ fun setMousePos(x: Double, y: Double) { GLFW.glfwSetCursorPos(minecraft.window.window, x, y) } +fun setMousePosScaled(x: Double, y: Double) { + GLFW.glfwSetCursorPos(minecraft.window.window, x * minecraft.window.guiScale, y * minecraft.window.guiScale) +} + +fun setMousePos(x: Float, y: Float) { + GLFW.glfwSetCursorPos(minecraft.window.window, x.toDouble(), y.toDouble()) +} + +fun setMousePosScaled(x: Float, y: Float) { + GLFW.glfwSetCursorPos(minecraft.window.window, x * minecraft.window.guiScale, y * minecraft.window.guiScale) +} + private val cursorXPosBuf = ByteBuffer.allocateDirect(8).also { it.order(ByteOrder.LITTLE_ENDIAN) }.asDoubleBuffer() private val cursorYPosBuf = ByteBuffer.allocateDirect(8).also { it.order(ByteOrder.LITTLE_ENDIAN) }.asDoubleBuffer() 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 c0350dffa..49c293795 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 @@ -19,28 +19,14 @@ import yalter.mousetweaks.api.MouseTweaksDisableWheelTweak @MouseTweaksDisableWheelTweak class ExoSuitInventoryScreen(menu: ExoSuitInventoryMenu) : MatteryScreen(menu, TranslatableComponent("otm.gui.exosuit")) { - var inventoryRows = 3 - set(value) { - val newValue = value.coerceAtLeast(3).coerceAtMost(6) - val old = field - - if (field != newValue) { - field = newValue - - if (mainFrame != null) { - updateInventoryRows(old) - } - } - } - private lateinit var mainInventoryLine: EditablePanel private lateinit var scrollPanel: DiscreteScrollBarPanel - private fun updateInventoryRows(old: Int) { + override fun updateInventoryRows(old: Int) { mainFrame!!.height = FRAME_BASE_HEIGHT + inventoryRows * AbstractSlotPanel.SIZE mainInventoryLine.height = AbstractSlotPanel.SIZE * inventoryRows - for (i in scrollPanel.scroll until scrollPanel.scroll + old) { + for (i in scrollPanel.scroll + inventoryRows until scrollPanel.scroll + old) { getInventorySlotsRow(i).visible = false } @@ -78,7 +64,7 @@ class ExoSuitInventoryScreen(menu: ExoSuitInventoryMenu) : MatteryScreen(this@ExoSuitInventoryScreen, frame, height = AbstractSlotPanel.SIZE * inventoryRows) { @@ -87,7 +73,7 @@ class ExoSuitInventoryScreen(menu: ExoSuitInventoryMenu) : MatteryScreen frame.y += movePixels moveMousePosScaled(y = movePixels) - - HeightControls.Status.of(inventoryRows < 6, inventoryRows > 3) - }.also { - it.controlStatus = HeightControls.Status.of(inventoryRows < 6, inventoryRows > 3) } return frame 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 de57e9d97..4e1657e93 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 @@ -16,7 +16,7 @@ import net.minecraftforge.client.event.ContainerScreenEvent.Render.Foreground import net.minecraftforge.common.MinecraftForge import org.lwjgl.opengl.GL11 import org.lwjgl.opengl.GL13 -import ru.dbotthepony.mc.otm.capability.matteryPlayer +import ru.dbotthepony.mc.otm.client.moveMousePosScaled import ru.dbotthepony.mc.otm.client.screen.panels.* import ru.dbotthepony.mc.otm.menu.MatteryMenu @@ -34,8 +34,8 @@ abstract class MatteryScreen(menu: T, inventory: Inventory, tit protected val panels = ArrayDeque>() - var inventoryFrame: FramePanel>? = null - var mainFrame: FramePanel>? = null + var inventoryFrame: FramePanel>? = null + var mainFrame: FramePanel>? = null private var madeMainFrame = false var itemRenderer: ItemRenderer @@ -52,6 +52,53 @@ abstract class MatteryScreen(menu: T, inventory: Inventory, tit val isQuickCrafting get() = isQuickCrafting private val inventorySlotsRows = Int2ObjectAVLTreeMap>>() + private lateinit var slotListCanvas: EditablePanel> + private lateinit var inventoryScrollbar: DiscreteScrollBarPanel> + + var inventoryRows = lastRows + set(value) { + val newValue = value.coerceAtLeast(MIN_ROWS).coerceAtMost(MAX_ROWS) + val old = field + + if (field != newValue) { + field = newValue + updateInventoryRows(old) + lastRows = newValue + } + } + + protected open fun updateInventoryRows(old: Int) { + if (inventoryFrame == null) { + return + } + + slotListCanvas.height = AbstractSlotPanel.SIZE * inventoryRows + inventoryFrame!!.height = BASE_INVENTORY_FRAME_HEIGHT + slotListCanvas.height + + for (i in inventoryScrollbar.scroll + inventoryRows until inventoryScrollbar.scroll + old) { + getInventorySlotsRow(i).visible = false + } + + for (i in inventoryScrollbar.scroll until inventoryScrollbar.scroll + inventoryRows) { + getInventorySlotsRow(i).also { + it.visible = true + it.parent = slotListCanvas + it.y = (i - inventoryScrollbar.scroll) * AbstractSlotPanel.SIZE + } + } + + inventoryScrollbar.scroll = inventoryScrollbar.scroll + } + + protected fun makeInventoryRowsControls(parent: EditablePanel<*>, x: Float, y: Float, callback: (Float) -> Unit): HeightControls<*> { + return HeightControls(this, parent, x, y) { + inventoryRows += if (it) 1 else -1 + callback.invoke(if (it) -AbstractSlotPanel.SIZE / 2f else AbstractSlotPanel.SIZE / 2f) + HeightControls.Status.of(inventoryRows < 6, inventoryRows > 3) + }.also { + it.controlStatus = HeightControls.Status.of(inventoryRows < 6, inventoryRows > 3) + } + } init { if (menu.playerInventorySlots.isNotEmpty() && menu.autoCreateInventoryFrame) { @@ -77,18 +124,16 @@ abstract class MatteryScreen(menu: T, inventory: Inventory, tit } } } else { - inventoryFrame = FramePanel>(this, null, 0f, 0f, INVENTORY_FRAME_WIDTH_EXTENDED, INVENTORY_FRAME_HEIGHT, inventory.displayName).also(this::addPanel) + inventoryFrame = FramePanel>(this, null, 0f, 0f, INVENTORY_FRAME_WIDTH_EXTENDED, BASE_INVENTORY_FRAME_HEIGHT + AbstractSlotPanel.SIZE * inventoryRows, inventory.displayName).also(this::addPanel) - var slotListCanvas: EditablePanel>? = null - - val scrollbar = DiscreteScrollBarPanel(this, inventoryFrame, { ((menu.playerCombinedInventorySlots.size - 27) + 8) / 9 }, { + inventoryScrollbar = DiscreteScrollBarPanel(this, inventoryFrame, { ((menu.playerCombinedInventorySlots.size - inventoryRows * 9) + 8) / 9 }, { _, old, new -> - for (i in old .. old + 2) { + for (i in old until old + inventoryRows) { getInventorySlotsRow(i).visible = false } - for (i in new .. new + 2) { + for (i in new until new + inventoryRows) { getInventorySlotsRow(i).also { it.visible = true it.parent = slotListCanvas @@ -96,12 +141,12 @@ abstract class MatteryScreen(menu: T, inventory: Inventory, tit } } - menu.ply.matteryPlayer?.exoSuitMenu?.lastScroll = new + lastScroll = new }) - slotListCanvas = object : EditablePanel>(this@MatteryScreen, inventoryFrame, height = AbstractSlotPanel.SIZE * 3f) { + slotListCanvas = object : EditablePanel>(this@MatteryScreen, inventoryFrame, height = AbstractSlotPanel.SIZE * inventoryRows) { override fun mouseScrolledInner(x: Double, y: Double, scroll: Double): Boolean { - scrollbar.mouseScrolledInner(x, y, scroll) + inventoryScrollbar.mouseScrolledInner(x, y, scroll) return true } } @@ -115,17 +160,23 @@ abstract class MatteryScreen(menu: T, inventory: Inventory, tit SlotPanel(this, hotbarStrip, slot).also { it.dock = Dock.LEFT } } - scrollbar.parent = slotListCanvas - scrollbar.dock = Dock.RIGHT + inventoryScrollbar.parent = slotListCanvas + inventoryScrollbar.dock = Dock.RIGHT - scrollbar.scroll = menu.ply.matteryPlayer?.exoSuitMenu?.lastScroll ?: 0 + inventoryScrollbar.scroll = lastScroll - for (i in scrollbar.scroll .. scrollbar.scroll + 2) { + for (i in inventoryScrollbar.scroll until inventoryScrollbar.scroll + inventoryRows) { getInventorySlotsRow(i).also { it.parent = slotListCanvas - it.y = (i - scrollbar.scroll) * AbstractSlotPanel.SIZE + it.y = (i - inventoryScrollbar.scroll) * AbstractSlotPanel.SIZE } } + + makeInventoryRowsControls(inventoryFrame!!, inventoryFrame!!.width + 2f, 0f) { movePixels -> + mainFrame?.let { it.y += movePixels } + inventoryFrame?.let { it.y += movePixels } + moveMousePosScaled(y = movePixels) + } } } } @@ -170,7 +221,7 @@ abstract class MatteryScreen(menu: T, inventory: Inventory, tit movePanels() } - fun addPanel(panel: EditablePanel>): Boolean { + fun addPanel(panel: EditablePanel>): Boolean { if (!panels.contains(panel)) { panels.addFirst(panel) return true @@ -179,7 +230,7 @@ abstract class MatteryScreen(menu: T, inventory: Inventory, tit return false } - fun removePanel(panel: EditablePanel>): Boolean { + fun removePanel(panel: EditablePanel>): Boolean { val indexOf = panels.indexOf(panel) if (indexOf != -1) { @@ -195,7 +246,7 @@ abstract class MatteryScreen(menu: T, inventory: Inventory, tit * * @param panel panel to be pushed up */ - fun popup(panel: EditablePanel>) { + fun popup(panel: EditablePanel>) { val indexOf = panels.indexOf(panel) require(indexOf != -1) { "No such panel $panel" } @@ -215,7 +266,7 @@ abstract class MatteryScreen(menu: T, inventory: Inventory, tit * * @return FramePanel created, or null */ - protected open fun makeMainFrame(): FramePanel>? { + protected open fun makeMainFrame(): FramePanel>? { return FramePanel(this, null, 0f, 0f, DEFAULT_FRAME_WIDTH, DEFAULT_FRAME_HEIGHT, getTitle()) } @@ -473,6 +524,7 @@ abstract class MatteryScreen(menu: T, inventory: Inventory, tit const val INVENTORY_FRAME_WIDTH = DEFAULT_FRAME_WIDTH const val INVENTORY_FRAME_WIDTH_EXTENDED = DEFAULT_FRAME_WIDTH + ScrollBarConstants.WIDTH + 2f const val INVENTORY_FRAME_HEIGHT = 3f * AbstractSlotPanel.SIZE + AbstractSlotPanel.SIZE + 24f + const val BASE_INVENTORY_FRAME_HEIGHT = AbstractSlotPanel.SIZE + 24f const val GAUGE_TOP_WITH_SLOT = 17f const val GAUGE_TOP_WITHOUT_SLOT = 26f @@ -480,5 +532,11 @@ abstract class MatteryScreen(menu: T, inventory: Inventory, tit const val PROGRESS_ARROW_TOP = 42f const val PROGRESS_SLOT_TOP = 41f const val LEFT_MARGIN = 8f + + const val MIN_ROWS = 3 + const val MAX_ROWS = 6 + + var lastScroll = 0 + var lastRows = 3 } } \ No newline at end of file diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/EditablePanel.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/EditablePanel.kt index 9269549fa..3a26239bf 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/EditablePanel.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/EditablePanel.kt @@ -11,6 +11,7 @@ import net.minecraft.network.chat.Component import net.minecraft.world.inventory.Slot import org.apache.logging.log4j.LogManager import ru.dbotthepony.mc.otm.client.minecraft +import ru.dbotthepony.mc.otm.client.moveMousePosScaled import ru.dbotthepony.mc.otm.client.render.popScissorRect import ru.dbotthepony.mc.otm.client.render.pushScissorRect import ru.dbotthepony.mc.otm.client.screen.MatteryScreen @@ -1207,4 +1208,8 @@ open class EditablePanel @JvmOverloads constructor( setDockMargin(0f, 0f, 0f, 0f) return grid } + + fun mouseToCenter() { + moveMousePosScaled(absoluteX + width / 2f, absoluteY + height / 2f) + } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/ExoSuitInventoryMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/ExoSuitInventoryMenu.kt index 4e452cb8c..3bc2384e7 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/ExoSuitInventoryMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/ExoSuitInventoryMenu.kt @@ -16,7 +16,6 @@ import ru.dbotthepony.mc.otm.network.ExoSuitSlotPacket import ru.dbotthepony.mc.otm.network.MatteryPlayerNetworkChannel class ExoSuitInventoryMenu(val capability: MatteryPlayerCapability) : MatteryMenu(null, CONTAINER_ID, capability.ply.inventory) { - var lastScroll = 0 override val storageSlots: Collection get() = listOf() val craftingGrid: CraftingContainer