The space compound can now be rendered
This commit is contained in:
@@ -146,6 +146,10 @@ class InventoryGUIPageBuilder<T : ForInventory>(
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines an area where the content of the given compound
|
||||||
|
* is displayed.
|
||||||
|
*/
|
||||||
fun <E> compoundSpace(
|
fun <E> compoundSpace(
|
||||||
slots: InventorySlotCompound<T>,
|
slots: InventorySlotCompound<T>,
|
||||||
compound: InventoryGUISpaceCompound<T, E>
|
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>) =
|
private fun defineSlots(slots: InventorySlotCompound<T>, element: InventoryGUISlot<T>) =
|
||||||
slots.withInvType(type).forEach { curSlot ->
|
slots.withInvType(type).forEach { curSlot ->
|
||||||
curSlot.realSlotIn(type.dimensions)?.let { guiSlots[it] = element }
|
curSlot.realSlotIn(type.dimensions)?.let { guiSlots[it] = element }
|
||||||
|
@@ -67,7 +67,7 @@ internal fun InventoryGUI<*>.changePage(
|
|||||||
|
|
||||||
PageChangeEffect.SLIDE_VERTICALLY -> {
|
PageChangeEffect.SLIDE_VERTICALLY -> {
|
||||||
|
|
||||||
val height = data.inventoryType.dimensions.heigth
|
val height = data.inventoryType.dimensions.height
|
||||||
|
|
||||||
changePageEffect(fromPageInt, toPageInt, height) { currentOffset, ifInverted ->
|
changePageEffect(fromPageInt, toPageInt, height) { currentOffset, ifInverted ->
|
||||||
if (ifInverted) {
|
if (ifInverted) {
|
||||||
@@ -97,7 +97,7 @@ internal fun InventoryGUI<*>.changePage(
|
|||||||
|
|
||||||
PageChangeEffect.SWIPE_VERTICALLY -> {
|
PageChangeEffect.SWIPE_VERTICALLY -> {
|
||||||
|
|
||||||
val height = data.inventoryType.dimensions.heigth
|
val height = data.inventoryType.dimensions.height
|
||||||
|
|
||||||
changePageEffect(fromPageInt, toPageInt, height) { currentOffset, ifInverted ->
|
changePageEffect(fromPageInt, toPageInt, height) { currentOffset, ifInverted ->
|
||||||
if (ifInverted) {
|
if (ifInverted) {
|
||||||
|
@@ -6,11 +6,13 @@ import net.axay.kspigot.languageextensions.kotlinextensions.MinMaxPair
|
|||||||
|
|
||||||
// INVENTORY
|
// 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 {
|
val invSlots by lazy {
|
||||||
ArrayList<InventorySlot>().apply {
|
ArrayList<InventorySlot>().apply {
|
||||||
(1..heigth).forEach { row ->
|
(1..height).forEach { row ->
|
||||||
(1..width).forEach { slotInRow ->
|
(1..width).forEach { slotInRow ->
|
||||||
this += InventorySlot(row, 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 {
|
val invSlotsWithRealSlots by lazy {
|
||||||
HashMap<InventorySlot, Int>().apply {
|
HashMap<InventorySlot, Int>().apply {
|
||||||
invSlots.forEach { curSlot ->
|
invSlots.forEach { curSlot ->
|
||||||
@@ -34,6 +28,8 @@ data class InventoryDimensions(val width: Int, val heigth: Int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val realSlots by lazy { invSlotsWithRealSlots.values }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SLOTS
|
// SLOTS
|
||||||
@@ -57,13 +53,13 @@ data class InventorySlot(val row: Int, val slotInRow: Int) : Comparable<Inventor
|
|||||||
|
|
||||||
fun realSlotIn(inventoryDimensions: InventoryDimensions): Int? {
|
fun realSlotIn(inventoryDimensions: InventoryDimensions): Int? {
|
||||||
if (!isInDimension(inventoryDimensions)) return null
|
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
|
val rowsUnder = if (realRow - 1 >= 0) realRow - 1 else 0
|
||||||
return ((rowsUnder * inventoryDimensions.width) + slotInRow) - 1
|
return ((rowsUnder * inventoryDimensions.width) + slotInRow) - 1
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isInDimension(inventoryDimensions: InventoryDimensions) =
|
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(
|
fun add(offsetHorizontally: Int, offsetVertically: Int) = InventorySlot(
|
||||||
row + offsetVertically,
|
row + offsetVertically,
|
||||||
@@ -182,7 +178,7 @@ class InventoryColumnSlots<T : ForInventory> internal constructor(
|
|||||||
) : InventorySlotCompound<T> {
|
) : InventorySlotCompound<T> {
|
||||||
|
|
||||||
override fun withInvType(invType: InventoryType<T>) = HashSet<InventorySlot>().apply {
|
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)
|
this += InventorySlot(row, column)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -199,9 +195,9 @@ class InventoryBorderSlots<T : ForInventory> internal constructor(
|
|||||||
for (currentPadding in 0 until padding) {
|
for (currentPadding in 0 until padding) {
|
||||||
for (slotInRow in 1 + currentPadding..dimensions.width - currentPadding) {
|
for (slotInRow in 1 + currentPadding..dimensions.width - currentPadding) {
|
||||||
this += InventorySlot(1, slotInRow)
|
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, 1)
|
||||||
this += InventorySlot(row, dimensions.width)
|
this += InventorySlot(row, dimensions.width)
|
||||||
}
|
}
|
||||||
@@ -224,8 +220,8 @@ class InventoryCornerSlots<T : ForInventory> internal constructor(
|
|||||||
|
|
||||||
if (ifBottomLeft) this += InventorySlot(1, 1)
|
if (ifBottomLeft) this += InventorySlot(1, 1)
|
||||||
if (ifBottomRight) this += InventorySlot(1, dimensions.width)
|
if (ifBottomRight) this += InventorySlot(1, dimensions.width)
|
||||||
if (ifTopLeft) this += InventorySlot(dimensions.heigth, 1)
|
if (ifTopLeft) this += InventorySlot(dimensions.height, 1)
|
||||||
if (ifTopRight) this += InventorySlot(dimensions.heigth, dimensions.width)
|
if (ifTopRight) this += InventorySlot(dimensions.height, dimensions.width)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -12,8 +12,6 @@ class InventoryType<in T : ForInventory>(
|
|||||||
val bukkitType: InventoryType? = null
|
val bukkitType: InventoryType? = null
|
||||||
) {
|
) {
|
||||||
|
|
||||||
private val size = dimensions.width * dimensions.heigth
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|
||||||
val ONE_BY_NINE = InventoryType<ForInventoryOneByNine>(InventoryDimensions(9, 1))
|
val ONE_BY_NINE = InventoryType<ForInventoryOneByNine>(InventoryDimensions(9, 1))
|
||||||
@@ -33,7 +31,7 @@ class InventoryType<in T : ForInventory>(
|
|||||||
val realTitle = title ?: ""
|
val realTitle = title ?: ""
|
||||||
return when {
|
return when {
|
||||||
bukkitType != null -> Bukkit.createInventory(holder, bukkitType, realTitle)
|
bukkitType != null -> Bukkit.createInventory(holder, bukkitType, realTitle)
|
||||||
else -> Bukkit.createInventory(holder, size, realTitle)
|
else -> Bukkit.createInventory(holder, dimensions.slotAmount, realTitle)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -25,14 +25,32 @@ class InventoryGUISpaceCompound<T : ForInventory, E> internal constructor(
|
|||||||
) {
|
) {
|
||||||
|
|
||||||
private val content = ArrayList<E>()
|
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 = { }
|
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(
|
||||||
private fun contentAtSlot(slot: Int) = content.getOrNull(internalSlots.indexOf(slot))
|
realInternalSlots.indexOf(translateSlot(slot))
|
||||||
|
)
|
||||||
|
|
||||||
internal fun getItemStack(slot: Int): ItemStack {
|
internal fun getItemStack(slot: Int): ItemStack {
|
||||||
return contentAtSlot(slot)?.let { return@let iconGenerator.invoke(it) }
|
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>) {
|
internal fun addSlots(slots: InventorySlotCompound<T>) {
|
||||||
internalSlots += slots.realSlotsWithInvType(invType)
|
slots.realSlotsWithInvType(invType).forEach {
|
||||||
internalSlots.sort()
|
if (!realInternalSlots.contains(it))
|
||||||
|
realInternalSlots.add(it)
|
||||||
|
}
|
||||||
|
realInternalSlots.sort()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user