From c20bf0a1a6fa071aa78725fdd72b00eb9851b3c3 Mon Sep 17 00:00:00 2001 From: bluefireoly Date: Wed, 21 Oct 2020 22:43:08 +0200 Subject: [PATCH] Inventory GUI compound scroll functionality --- .../elements/InventoryGUISpaceCompound.kt | 41 +++++++++++-------- .../InventoryGUISpaceCompoundScrollButton.kt | 28 +++++++++++++ 2 files changed, 52 insertions(+), 17 deletions(-) create mode 100644 src/main/kotlin/net/axay/kspigot/inventory/elements/InventoryGUISpaceCompoundScrollButton.kt diff --git a/src/main/kotlin/net/axay/kspigot/inventory/elements/InventoryGUISpaceCompound.kt b/src/main/kotlin/net/axay/kspigot/inventory/elements/InventoryGUISpaceCompound.kt index fd50d03f..3a95296a 100644 --- a/src/main/kotlin/net/axay/kspigot/inventory/elements/InventoryGUISpaceCompound.kt +++ b/src/main/kotlin/net/axay/kspigot/inventory/elements/InventoryGUISpaceCompound.kt @@ -32,28 +32,35 @@ class InventoryGUISpaceCompound( private val realInternalSlots = ArrayList() - private val currentInternalSlots: List get() { + private val currentInternalSlots: List + get() { - val result = ArrayList(realInternalSlots) + val result = ArrayList(realInternalSlots) - var more = 1 - while (content.size > result.size) { - result += realInternalSlots.mapTo(ArrayList()) { it + (more * invType.dimensions.slotAmount) } - more++ - } - - return result - - } - - internal var scrolledLines: Int = 0 - set(value) { - if (((value - 1) * invType.dimensions.width) < content.size) { - field = value - onChange() + var more = 1 + while (content.size > result.size) { + result += realInternalSlots.mapTo(ArrayList()) { it + (more * invType.dimensions.slotAmount) } + more++ } + + return result + } + internal var scrolledLines: Int = 0; private set + + fun scroll(distance: Int): Boolean { + val value = scrolledLines + distance + return if ( + value >= 0 && + ((value - 1) * invType.dimensions.width) < content.size + ) { + scrolledLines = value + onChange() + true + } else false + } + private var contentSort: () -> Unit = { } private val registeredGUIs = HashSet>() diff --git a/src/main/kotlin/net/axay/kspigot/inventory/elements/InventoryGUISpaceCompoundScrollButton.kt b/src/main/kotlin/net/axay/kspigot/inventory/elements/InventoryGUISpaceCompoundScrollButton.kt new file mode 100644 index 00000000..1ffa5848 --- /dev/null +++ b/src/main/kotlin/net/axay/kspigot/inventory/elements/InventoryGUISpaceCompoundScrollButton.kt @@ -0,0 +1,28 @@ +package net.axay.kspigot.inventory.elements + +import net.axay.kspigot.inventory.ForInventory +import net.axay.kspigot.inventory.InventoryGUIClickEvent +import net.axay.kspigot.inventory.InventoryGUIElement +import net.axay.kspigot.runnables.task +import org.bukkit.inventory.ItemStack + +class InventoryGUISpaceCompoundScrollButton( + private val icon: ItemStack, + private val compound: InventoryGUISpaceCompound, + private val scrollDistance: Int = compound.invType.dimensions.height, + private val reverse: Boolean = false +) : InventoryGUIElement() { + + override fun getItemStack(slot: Int) = icon + + override fun onClickElement(clickEvent: InventoryGUIClickEvent) { + task( + period = 1, + howOften = scrollDistance.toLong() + ) { + val ifScrolled = if (reverse) compound.scroll(-1) else compound.scroll(1) + if (!ifScrolled) it.cancel() + } + } + +} \ No newline at end of file