Added compound scroll button to Inventory GUI builder

This commit is contained in:
bluefireoly
2020-10-21 22:54:32 +02:00
parent c20bf0a1a6
commit 25d8e5fd41

View File

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