From d7a7ef69146f54da0feed83ab037f2f1b3dd9ab3 Mon Sep 17 00:00:00 2001 From: bluefireoly Date: Wed, 21 Oct 2020 15:34:08 +0200 Subject: [PATCH] The space compound can now be rendered --- .../kspigot/inventory/InventoryGUIBuilder.kt | 13 ++++++++ .../inventory/InventoryGUIPageChange.kt | 4 +-- .../kspigot/inventory/InventoryGUISlots.kt | 30 ++++++++--------- .../axay/kspigot/inventory/InventoryType.kt | 4 +-- .../elements/InventoryGUISpaceCompound.kt | 33 +++++++++++++++---- 5 files changed, 56 insertions(+), 28 deletions(-) diff --git a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIBuilder.kt b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIBuilder.kt index b7344c12..6c29f6f9 100644 --- a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIBuilder.kt +++ b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIBuilder.kt @@ -146,6 +146,10 @@ class InventoryGUIPageBuilder( ) ) + /** + * Defines an area where the content of the given compound + * is displayed. + */ fun compoundSpace( slots: InventorySlotCompound, compound: InventoryGUISpaceCompound @@ -157,6 +161,15 @@ class InventoryGUIPageBuilder( ) } + /** + * 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) + private fun defineSlots(slots: InventorySlotCompound, element: InventoryGUISlot) = slots.withInvType(type).forEach { curSlot -> curSlot.realSlotIn(type.dimensions)?.let { guiSlots[it] = element } diff --git a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIPageChange.kt b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIPageChange.kt index b777da7b..e18597c6 100644 --- a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIPageChange.kt +++ b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIPageChange.kt @@ -67,7 +67,7 @@ internal fun InventoryGUI<*>.changePage( PageChangeEffect.SLIDE_VERTICALLY -> { - val height = data.inventoryType.dimensions.heigth + val height = data.inventoryType.dimensions.height changePageEffect(fromPageInt, toPageInt, height) { currentOffset, ifInverted -> if (ifInverted) { @@ -97,7 +97,7 @@ internal fun InventoryGUI<*>.changePage( PageChangeEffect.SWIPE_VERTICALLY -> { - val height = data.inventoryType.dimensions.heigth + val height = data.inventoryType.dimensions.height changePageEffect(fromPageInt, toPageInt, height) { currentOffset, ifInverted -> if (ifInverted) { diff --git a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUISlots.kt b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUISlots.kt index f0732fd7..e82bc7b9 100644 --- a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUISlots.kt +++ b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUISlots.kt @@ -6,11 +6,13 @@ import net.axay.kspigot.languageextensions.kotlinextensions.MinMaxPair // INVENTORY -data class InventoryDimensions(val width: Int, val heigth: Int) { +data class InventoryDimensions(val width: Int, val height: Int) { + + val slotAmount = width * height val invSlots by lazy { ArrayList().apply { - (1..heigth).forEach { row -> + (1..height).forEach { row -> (1..width).forEach { slotInRow -> this += InventorySlot(row, slotInRow) } @@ -18,14 +20,6 @@ data class InventoryDimensions(val width: Int, val heigth: Int) { } } - val realSlots by lazy { - ArrayList().apply { - invSlots.forEach { curSlot -> - curSlot.realSlotIn(this@InventoryDimensions)?.let { this += it } - } - } - } - val invSlotsWithRealSlots by lazy { HashMap().apply { invSlots.forEach { curSlot -> @@ -34,6 +28,8 @@ data class InventoryDimensions(val width: Int, val heigth: Int) { } } + val realSlots by lazy { invSlotsWithRealSlots.values } + } // SLOTS @@ -57,13 +53,13 @@ data class InventorySlot(val row: Int, val slotInRow: Int) : Comparable= 0) realRow - 1 else 0 return ((rowsUnder * inventoryDimensions.width) + slotInRow) - 1 } fun isInDimension(inventoryDimensions: InventoryDimensions) = - (1..inventoryDimensions.width).contains(slotInRow) && (1..inventoryDimensions.heigth).contains(row) + (1..inventoryDimensions.width).contains(slotInRow) && (1..inventoryDimensions.height).contains(row) fun add(offsetHorizontally: Int, offsetVertically: Int) = InventorySlot( row + offsetVertically, @@ -182,7 +178,7 @@ class InventoryColumnSlots internal constructor( ) : InventorySlotCompound { override fun withInvType(invType: InventoryType) = HashSet().apply { - for (row in 1..invType.dimensions.heigth) + for (row in 1..invType.dimensions.height) this += InventorySlot(row, column) } @@ -199,9 +195,9 @@ class InventoryBorderSlots internal constructor( for (currentPadding in 0 until padding) { for (slotInRow in 1 + currentPadding..dimensions.width - currentPadding) { this += InventorySlot(1, slotInRow) - this += InventorySlot(dimensions.heigth, slotInRow) + this += InventorySlot(dimensions.height, slotInRow) } - for (row in 2 + currentPadding until dimensions.heigth - currentPadding) { + for (row in 2 + currentPadding until dimensions.height - currentPadding) { this += InventorySlot(row, 1) this += InventorySlot(row, dimensions.width) } @@ -224,8 +220,8 @@ class InventoryCornerSlots internal constructor( if (ifBottomLeft) this += InventorySlot(1, 1) if (ifBottomRight) this += InventorySlot(1, dimensions.width) - if (ifTopLeft) this += InventorySlot(dimensions.heigth, 1) - if (ifTopRight) this += InventorySlot(dimensions.heigth, dimensions.width) + if (ifTopLeft) this += InventorySlot(dimensions.height, 1) + if (ifTopRight) this += InventorySlot(dimensions.height, dimensions.width) } diff --git a/src/main/kotlin/net/axay/kspigot/inventory/InventoryType.kt b/src/main/kotlin/net/axay/kspigot/inventory/InventoryType.kt index 8510a25d..8204120f 100644 --- a/src/main/kotlin/net/axay/kspigot/inventory/InventoryType.kt +++ b/src/main/kotlin/net/axay/kspigot/inventory/InventoryType.kt @@ -12,8 +12,6 @@ class InventoryType( val bukkitType: InventoryType? = null ) { - private val size = dimensions.width * dimensions.heigth - companion object { val ONE_BY_NINE = InventoryType(InventoryDimensions(9, 1)) @@ -33,7 +31,7 @@ class InventoryType( val realTitle = title ?: "" return when { bukkitType != null -> Bukkit.createInventory(holder, bukkitType, realTitle) - else -> Bukkit.createInventory(holder, size, realTitle) + else -> Bukkit.createInventory(holder, dimensions.slotAmount, realTitle) } } 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 bb3a1527..cc373ebb 100644 --- a/src/main/kotlin/net/axay/kspigot/inventory/elements/InventoryGUISpaceCompound.kt +++ b/src/main/kotlin/net/axay/kspigot/inventory/elements/InventoryGUISpaceCompound.kt @@ -25,14 +25,32 @@ class InventoryGUISpaceCompound internal constructor( ) { private val content = ArrayList() - private val internalSlots = ArrayList() + + private val realInternalSlots = ArrayList() + + private val currentInternalSlots: List get() { + + val result = ArrayList(realInternalSlots) + + var more = 1 + while (content.size > result.size) { + result += realInternalSlots.mapTo(ArrayList()) { it + (more * invType.dimensions.slotAmount) } + more++ + } + + return result + + } + + private var scrolledLines: Int = 0 private var contentSort: () -> Unit = { } - // TODO add "scrolling" functionality + private fun translateSlot(slot: Int) = (scrolledLines * invType.dimensions.width) + slot - // TODO take current scroll progress into account - private fun contentAtSlot(slot: Int) = content.getOrNull(internalSlots.indexOf(slot)) + private fun contentAtSlot(slot: Int) = content.getOrNull( + realInternalSlots.indexOf(translateSlot(slot)) + ) internal fun getItemStack(slot: Int): ItemStack { return contentAtSlot(slot)?.let { return@let iconGenerator.invoke(it) } @@ -45,8 +63,11 @@ class InventoryGUISpaceCompound internal constructor( } internal fun addSlots(slots: InventorySlotCompound) { - internalSlots += slots.realSlotsWithInvType(invType) - internalSlots.sort() + slots.realSlotsWithInvType(invType).forEach { + if (!realInternalSlots.contains(it)) + realInternalSlots.add(it) + } + realInternalSlots.sort() } /**