Updated transition system

This commit is contained in:
bluefireoly
2020-10-11 00:57:13 +02:00
parent b62f33008b
commit a9e54e707e
4 changed files with 52 additions and 34 deletions

View File

@@ -41,7 +41,9 @@ class InventoryGUIHolder(kSpigot: KSpigot) : AutoCloseable {
kSpigot.listen<InventoryClickEvent> { kSpigot.listen<InventoryClickEvent> {
val inv = registered.find { search -> search.isThisInv(it.inventory) } ?: return@listen val clickedInv = it.clickedInventory ?: return@listen
val inv = registered.find { search -> search.isThisInv(clickedInv) } ?: return@listen
val invPage = inv.currentPageInt val invPage = inv.currentPageInt
val slot = inv.data.pages[invPage]?.slots?.get(it.slot) val slot = inv.data.pages[invPage]?.slots?.get(it.slot)
@@ -74,13 +76,13 @@ class InventoryGUIClickEvent<T : ForInventory>(
private const val DEFAULT_PAGE = 1 private const val DEFAULT_PAGE = 1
class InventoryGUIData<T : ForInventory>( class InventoryGUIData<T : ForInventory>(
val plugin: KSpigot, val plugin: KSpigot,
val inventoryType: InventoryType<T>, val inventoryType: InventoryType<T>,
val title: String?, val title: String?,
internal val pages: Map<Int, InventoryGUIPage<T>>, internal val pages: Map<Int, InventoryGUIPage<T>>,
val transitionTo: InventoryGUIPageChangeEffect?, val transitionTo: InventoryChangeEffect?,
val transitionFrom: InventoryGUIPageChangeEffect?, val transitionFrom: InventoryChangeEffect?,
internal val generalOnClick: ((InventoryGUIClickEvent<T>) -> Unit)? internal val generalOnClick: ((InventoryGUIClickEvent<T>) -> Unit)?
) )
abstract class InventoryGUI<T : ForInventory>( abstract class InventoryGUI<T : ForInventory>(
@@ -93,7 +95,7 @@ abstract class InventoryGUI<T : ForInventory>(
internal abstract val bukkitInventory: Inventory internal abstract val bukkitInventory: Inventory
internal abstract fun loadPageUnsafe(page: InventoryGUIPage<*>?, offsetHorizontally: Int = 0, offsetVertically: Int = 0) internal abstract fun loadPageUnsafe(page: InventoryGUIPage<*>, offsetHorizontally: Int = 0, offsetVertically: Int = 0)
internal abstract fun loadPageUnsafe(page: Int, offsetHorizontally: Int = 0, offsetVertically: Int = 0) internal abstract fun loadPageUnsafe(page: Int, offsetHorizontally: Int = 0, offsetVertically: Int = 0)
/** /**
@@ -144,9 +146,7 @@ class InventoryGUIShared<T : ForInventory>(
override fun isThisInv(inventory: Inventory) = inventory == bukkitInventory override fun isThisInv(inventory: Inventory) = inventory == bukkitInventory
override fun loadPageUnsafe(page: InventoryGUIPage<*>?, offsetHorizontally: Int, offsetVertically: Int) { override fun loadPageUnsafe(page: InventoryGUIPage<*>, offsetHorizontally: Int, offsetVertically: Int) {
if (page == null) return
val ifOffset = offsetHorizontally != 0 || offsetVertically != 0 val ifOffset = offsetHorizontally != 0 || offsetVertically != 0
@@ -204,8 +204,8 @@ class InventoryGUIShared<T : ForInventory>(
} }
class InventoryGUIPage<T : ForInventory>( class InventoryGUIPage<T : ForInventory>(
val number: Int, val number: Int,
internal val slots: Map<Int, InventoryGUISlot<T>>, internal val slots: Map<Int, InventoryGUISlot<T>>,
val transitionTo: InventoryGUIPageChangeEffect?, val transitionTo: PageChangeEffect?,
val transitionFrom: InventoryGUIPageChangeEffect? val transitionFrom: PageChangeEffect?
) )

View File

@@ -17,8 +17,8 @@ class InventoryGUIBuilder<T : ForInventory>(
var title: String = "" var title: String = ""
var transitionTo: InventoryGUIPageChangeEffect? = null var transitionTo: InventoryChangeEffect? = null
var transitionFrom: InventoryGUIPageChangeEffect? = null var transitionFrom: InventoryChangeEffect? = null
private val guiSlots = HashMap<Int, InventoryGUIPage<T>>() private val guiSlots = HashMap<Int, InventoryGUIPage<T>>()
@@ -50,8 +50,8 @@ class InventoryGUIPageBuilder<T : ForInventory>(
private val guiSlots = HashMap<Int, InventoryGUISlot<T>>() private val guiSlots = HashMap<Int, InventoryGUISlot<T>>()
var transitionTo: InventoryGUIPageChangeEffect? = null var transitionTo: PageChangeEffect? = null
var transitionFrom: InventoryGUIPageChangeEffect? = null var transitionFrom: PageChangeEffect? = null
internal fun build() = InventoryGUIPage(page, guiSlots, transitionTo, transitionFrom) internal fun build() = InventoryGUIPage(page, guiSlots, transitionTo, transitionFrom)
@@ -117,7 +117,7 @@ class InventoryGUIPageBuilder<T : ForInventory>(
* By pressing this button, the player switches to another * By pressing this button, the player switches to another
* InventoryGUI. The transition effect is applied. * InventoryGUI. The transition effect is applied.
*/ */
fun changeGUI(slots: InventorySlotCompound<T>, itemStack: ItemStack, newGUI: InventoryGUI<*>, newPage: Int? = null, onChange: ((InventoryGUIClickEvent<T>) -> Unit)? = null) fun changeGUI(slots: InventorySlotCompound<T>, itemStack: ItemStack, newGUI: () -> InventoryGUI<*>, newPage: Int? = null, onChange: ((InventoryGUIClickEvent<T>) -> Unit)? = null)
= defineSlots(slots, InventoryGUIButtonInventoryChange( = defineSlots(slots, InventoryGUIButtonInventoryChange(
InventoryGUIElementData(itemStack), InventoryGUIElementData(itemStack),
newGUI, newGUI,

View File

@@ -61,7 +61,7 @@ class InventoryGUIButtonPageChange<T : ForInventory>(
if (newPage != null) { if (newPage != null) {
val effect = (newPage.transitionTo ?: currentPage.transitionFrom) val effect = (newPage.transitionTo ?: currentPage.transitionFrom)
?: InventoryGUIPageChangeEffect.INSTANT ?: PageChangeEffect.INSTANT
it.gui.changePage(effect, currentPage, newPage) it.gui.changePage(effect, currentPage, newPage)
@@ -73,18 +73,20 @@ class InventoryGUIButtonPageChange<T : ForInventory>(
class InventoryGUIButtonInventoryChange<T : ForInventory>( class InventoryGUIButtonInventoryChange<T : ForInventory>(
inventoryGUIElementData: InventoryGUIElementData, inventoryGUIElementData: InventoryGUIElementData,
changeToGUI: InventoryGUI<*>, changeToGUICallback: () -> InventoryGUI<*>,
changeToPageInt: Int?, changeToPageInt: Int?,
onChange: ((InventoryGUIClickEvent<T>) -> Unit)? onChange: ((InventoryGUIClickEvent<T>) -> Unit)?
) )
: InventoryGUIButton<T>(inventoryGUIElementData, { : InventoryGUIButton<T>(inventoryGUIElementData, {
val changeToGUI = changeToGUICallback.invoke()
val effect = (changeToGUI.data.transitionTo ?: it.gui.data.transitionFrom) val effect = (changeToGUI.data.transitionTo ?: it.gui.data.transitionFrom)
?: InventoryGUIPageChangeEffect.INSTANT ?: InventoryChangeEffect.INSTANT
val changeToPage = changeToGUI.getPage(changeToPageInt) ?: changeToGUI.currentPage val changeToPage = changeToGUI.getPage(changeToPageInt) ?: changeToGUI.currentPage
changeToGUI.changePage(effect, it.gui.currentPage, changeToPage) changeToGUI.changeGUI(effect, it.gui.currentPage, changeToPage)
it.bukkitEvent.whoClicked.openGUI(changeToGUI) it.bukkitEvent.whoClicked.openGUI(changeToGUI)

View File

@@ -23,7 +23,7 @@ abstract class InventoryGUIPageChangeCalculator {
} }
enum class InventoryGUIPageChangeEffect { enum class PageChangeEffect {
INSTANT, INSTANT,
SLIDE_HORIZONTALLY, SLIDE_HORIZONTALLY,
SLIDE_VERTICALLY, SLIDE_VERTICALLY,
@@ -31,16 +31,26 @@ enum class InventoryGUIPageChangeEffect {
SWIPE_VERTICALLY, SWIPE_VERTICALLY,
} }
internal fun InventoryGUI<*>.changePage(effect: InventoryGUIPageChangeEffect, fromPage: InventoryGUIPage<*>?, toPage: InventoryGUIPage<*>?) { enum class InventoryChangeEffect(
val effect: PageChangeEffect
) {
INSTANT(PageChangeEffect.INSTANT)
}
val fromPageInt = fromPage?.number ?: 0 internal fun InventoryGUI<*>.changePage(
val toPageInt = toPage?.number ?: 0 effect: PageChangeEffect,
fromPage: InventoryGUIPage<*>,
toPage: InventoryGUIPage<*>
) {
val fromPageInt = fromPage.number
val toPageInt = toPage.number
when (effect) { when (effect) {
InventoryGUIPageChangeEffect.INSTANT -> loadPageUnsafe(toPage) PageChangeEffect.INSTANT -> loadPageUnsafe(toPage)
InventoryGUIPageChangeEffect.SLIDE_HORIZONTALLY -> { PageChangeEffect.SLIDE_HORIZONTALLY -> {
val width = data.inventoryType.dimensions.width val width = data.inventoryType.dimensions.width
@@ -56,7 +66,7 @@ internal fun InventoryGUI<*>.changePage(effect: InventoryGUIPageChangeEffect, fr
} }
InventoryGUIPageChangeEffect.SLIDE_VERTICALLY -> { PageChangeEffect.SLIDE_VERTICALLY -> {
val height = data.inventoryType.dimensions.heigth val height = data.inventoryType.dimensions.heigth
@@ -72,7 +82,7 @@ internal fun InventoryGUI<*>.changePage(effect: InventoryGUIPageChangeEffect, fr
} }
InventoryGUIPageChangeEffect.SWIPE_HORIZONTALLY -> { PageChangeEffect.SWIPE_HORIZONTALLY -> {
val width = data.inventoryType.dimensions.width val width = data.inventoryType.dimensions.width
@@ -86,7 +96,7 @@ internal fun InventoryGUI<*>.changePage(effect: InventoryGUIPageChangeEffect, fr
} }
InventoryGUIPageChangeEffect.SWIPE_VERTICALLY -> { PageChangeEffect.SWIPE_VERTICALLY -> {
val height = data.inventoryType.dimensions.heigth val height = data.inventoryType.dimensions.heigth
@@ -103,6 +113,12 @@ internal fun InventoryGUI<*>.changePage(effect: InventoryGUIPageChangeEffect, fr
} }
} }
internal fun InventoryGUI<*>.changeGUI(
effect: InventoryChangeEffect,
fromPage: InventoryGUIPage<*>,
toPage: InventoryGUIPage<*>
) = changePage(effect.effect, fromPage, toPage)
private inline fun changePageEffect( private inline fun changePageEffect(
kSpigot: KSpigot, kSpigot: KSpigot,
fromPage: Int, fromPage: Int,