diff --git a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIBuilder.kt b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIBuilder.kt index 1a287aaf..d084e948 100644 --- a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIBuilder.kt +++ b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIBuilder.kt @@ -4,6 +4,7 @@ package net.axay.kspigot.inventory import net.axay.kspigot.inventory.elements.* import org.bukkit.inventory.ItemStack +import kotlin.math.absoluteValue fun kSpigotGUI( type: InventoryType, @@ -59,6 +60,11 @@ class InventoryGUIPageBuilder( internal fun build() = InventoryGUIPage(page, guiSlots, transitionTo, transitionFrom) + private fun defineSlots(slots: InventorySlotCompound, element: InventoryGUISlot) = + slots.withInvType(type).forEach { curSlot -> + curSlot.realSlotIn(type.dimensions)?.let { guiSlots[it] = element } + } + /** * A button is an item protected from any player * actions. If clicked, the specified [onClick] @@ -87,12 +93,12 @@ class InventoryGUIPageBuilder( */ fun pageChanger( slots: InventorySlotCompound, - itemStack: ItemStack, + icon: ItemStack, toPage: Int, onChange: ((InventoryGUIClickEvent) -> Unit)? = null ) = defineSlots( slots, InventoryGUIButtonPageChange( - itemStack, + icon, InventoryGUIPageChangeCalculator.InventoryGUIConsistentPageCalculator(toPage), onChange ) @@ -105,11 +111,11 @@ class InventoryGUIPageBuilder( */ fun previousPage( slots: InventorySlotCompound, - itemStack: ItemStack, + icon: ItemStack, onChange: ((InventoryGUIClickEvent) -> Unit)? = null ) = defineSlots( slots, InventoryGUIButtonPageChange( - itemStack, + icon, InventoryGUIPageChangeCalculator.InventoryGUIPreviousPageCalculator, onChange ) @@ -122,11 +128,11 @@ class InventoryGUIPageBuilder( */ fun nextPage( slots: InventorySlotCompound, - itemStack: ItemStack, + icon: ItemStack, onChange: ((InventoryGUIClickEvent) -> Unit)? = null ) = defineSlots( slots, InventoryGUIButtonPageChange( - itemStack, + icon, InventoryGUIPageChangeCalculator.InventoryGUINextPageCalculator, onChange ) @@ -138,19 +144,28 @@ class InventoryGUIPageBuilder( */ fun changeGUI( slots: InventorySlotCompound, - itemStack: ItemStack, + icon: ItemStack, newGUI: () -> InventoryGUI<*>, newPage: Int? = null, onChange: ((InventoryGUIClickEvent) -> Unit)? = null ) = defineSlots( slots, InventoryGUIButtonInventoryChange( - itemStack, + icon, newGUI, newPage, onChange ) ) + /** + * Creates a new compound, holding data which can be displayed + * in any compound space. + */ + fun createCompound( + iconGenerator: (E) -> ItemStack, + onClick: (clickEvent: InventoryGUIClickEvent, element: E) -> Unit + ) = InventoryGUISpaceCompound(type, iconGenerator, onClick) + /** * Defines an area where the content of the given compound * is displayed. @@ -167,17 +182,25 @@ class InventoryGUIPageBuilder( } /** - * Creates a new compound, holding data which can be displayed - * in any compound space. + * By pressing this button, + * the user scrolls forward in the compound. */ - fun createCompound( - iconGenerator: (E) -> ItemStack, - onClick: (clickEvent: InventoryGUIClickEvent, element: E) -> Unit - ) = InventoryGUISpaceCompound(type, iconGenerator, onClick) + fun compoundScrollForwards( + slots: InventorySlotCompound, + icon: ItemStack, + compound: InventoryGUISpaceCompound, + scrollDistance: Int = compound.invType.dimensions.height + ) = defineSlots(slots, InventoryGUISpaceCompoundScrollButton(icon, compound, scrollDistance.absoluteValue)) - private fun defineSlots(slots: InventorySlotCompound, element: InventoryGUISlot) = - slots.withInvType(type).forEach { curSlot -> - curSlot.realSlotIn(type.dimensions)?.let { guiSlots[it] = element } - } + /** + * By pressing this button, + * the user scrolls backwards in the compound. + */ + fun compoundScrollBackwards( + slots: InventorySlotCompound, + icon: ItemStack, + compound: InventoryGUISpaceCompound, + scrollDistance: Int = compound.invType.dimensions.height + ) = defineSlots(slots, InventoryGUISpaceCompoundScrollButton(icon, compound, -scrollDistance.absoluteValue)) } \ No newline at end of file