The space compound can now be rendered

This commit is contained in:
bluefireoly
2020-10-21 15:34:08 +02:00
parent 3c05f54bdb
commit d7a7ef6914
5 changed files with 56 additions and 28 deletions

View File

@@ -146,6 +146,10 @@ class InventoryGUIPageBuilder<T : ForInventory>(
)
)
/**
* Defines an area where the content of the given compound
* is displayed.
*/
fun <E> compoundSpace(
slots: InventorySlotCompound<T>,
compound: InventoryGUISpaceCompound<T, E>
@@ -157,6 +161,15 @@ class InventoryGUIPageBuilder<T : ForInventory>(
)
}
/**
* 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)
private fun defineSlots(slots: InventorySlotCompound<T>, element: InventoryGUISlot<T>) =
slots.withInvType(type).forEach { curSlot ->
curSlot.realSlotIn(type.dimensions)?.let { guiSlots[it] = element }

View File

@@ -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) {

View File

@@ -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<InventorySlot>().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<Int>().apply {
invSlots.forEach { curSlot ->
curSlot.realSlotIn(this@InventoryDimensions)?.let { this += it }
}
}
}
val invSlotsWithRealSlots by lazy {
HashMap<InventorySlot, Int>().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<Inventor
fun realSlotIn(inventoryDimensions: InventoryDimensions): Int? {
if (!isInDimension(inventoryDimensions)) return null
val realRow = inventoryDimensions.heigth - (row - 1)
val realRow = inventoryDimensions.height - (row - 1)
val rowsUnder = if (realRow - 1 >= 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<T : ForInventory> internal constructor(
) : InventorySlotCompound<T> {
override fun withInvType(invType: InventoryType<T>) = HashSet<InventorySlot>().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<T : ForInventory> 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<T : ForInventory> 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)
}

View File

@@ -12,8 +12,6 @@ class InventoryType<in T : ForInventory>(
val bukkitType: InventoryType? = null
) {
private val size = dimensions.width * dimensions.heigth
companion object {
val ONE_BY_NINE = InventoryType<ForInventoryOneByNine>(InventoryDimensions(9, 1))
@@ -33,7 +31,7 @@ class InventoryType<in T : ForInventory>(
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)
}
}

View File

@@ -25,14 +25,32 @@ class InventoryGUISpaceCompound<T : ForInventory, E> internal constructor(
) {
private val content = ArrayList<E>()
private val internalSlots = ArrayList<Int>()
private val realInternalSlots = ArrayList<Int>()
private val currentInternalSlots: List<Int> 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<T : ForInventory, E> internal constructor(
}
internal fun addSlots(slots: InventorySlotCompound<T>) {
internalSlots += slots.realSlotsWithInvType(invType)
internalSlots.sort()
slots.realSlotsWithInvType(invType).forEach {
if (!realInternalSlots.contains(it))
realInternalSlots.add(it)
}
realInternalSlots.sort()
}
/**