From 7139ddb3cfe5d5f9d6a13a132c97b562979d0da4 Mon Sep 17 00:00:00 2001 From: bluefireoly Date: Sun, 27 Sep 2020 18:35:55 +0200 Subject: [PATCH] Added new page change effects --- .../inventory/InventoryGUIPageChange.kt | 101 ++++++++++++++---- 1 file changed, 82 insertions(+), 19 deletions(-) diff --git a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIPageChange.kt b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIPageChange.kt index 1d2d0a5f..e5a9020f 100644 --- a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIPageChange.kt +++ b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIPageChange.kt @@ -1,5 +1,6 @@ package net.axay.kspigot.inventory +import net.axay.kspigot.main.KSpigot import net.axay.kspigot.runnables.task abstract class InventoryGUIPageChangeCalculator { @@ -21,7 +22,11 @@ abstract class InventoryGUIPageChangeCalculator { } enum class InventoryGUIPageChangeEffect { - INSTANT, SLIDE_HORIZONTALLY + INSTANT, + SLIDE_HORIZONTALLY, + SLIDE_VERTICALLY, + SWIPE_HORIZONTALLY, + SWIPE_VERTICALLY, } class InventoryGUIPageChanger(private val effect: InventoryGUIPageChangeEffect) { @@ -29,40 +34,98 @@ class InventoryGUIPageChanger(private val effect: InventoryGUIPageChangeEffect) fun changePage(gui: InventoryGUI, fromPage: Int, toPage: Int) { when (effect) { + InventoryGUIPageChangeEffect.INSTANT -> gui.loadPage(toPage) + InventoryGUIPageChangeEffect.SLIDE_HORIZONTALLY -> { val width = gui.data.inventoryGUIType.dimensions.width - val ifInverted = fromPage >= toPage - - var currentOffset = 1 - gui.data.plugin.task( - sync = true, - period = 1, - howOften = width.toLong() - ) { - + changePageEffect(gui.data.plugin, fromPage, toPage, width) { currentOffset, ifInverted -> if (ifInverted) { - gui.loadPage(fromPage, currentOffset) - gui.loadPage(toPage, - (width - currentOffset)) + gui.loadPage(fromPage, offsetVertically = currentOffset) + gui.loadPage(toPage, offsetVertically = -(width - currentOffset)) } else { - gui.loadPage(fromPage, -currentOffset) - gui.loadPage(toPage, width - currentOffset) + gui.loadPage(fromPage, offsetVertically = -currentOffset) + gui.loadPage(toPage, offsetVertically = width - currentOffset) } - - currentOffset++ - } } - InventoryGUIPageChangeEffect.INSTANT -> { + InventoryGUIPageChangeEffect.SLIDE_VERTICALLY -> { - gui.loadPage(toPage) + val height = gui.data.inventoryGUIType.dimensions.heigth + + changePageEffect(gui.data.plugin, fromPage, toPage, height) { currentOffset, ifInverted -> + if (ifInverted) { + gui.loadPage(fromPage, offsetVertically = currentOffset) + gui.loadPage(toPage, offsetVertically = -(height - currentOffset)) + } else { + gui.loadPage(fromPage, offsetVertically = -currentOffset) + gui.loadPage(toPage, offsetVertically = height - currentOffset) + } + } + + } + + InventoryGUIPageChangeEffect.SWIPE_HORIZONTALLY -> { + + val width = gui.data.inventoryGUIType.dimensions.width + + changePageEffect(gui.data.plugin, fromPage, toPage, width) { currentOffset, ifInverted -> + if (ifInverted) { + gui.loadPage(toPage, offsetVertically = -(width - currentOffset)) + } else { + gui.loadPage(toPage, offsetVertically = width - currentOffset) + } + } + + } + + InventoryGUIPageChangeEffect.SWIPE_VERTICALLY -> { + + val height = gui.data.inventoryGUIType.dimensions.heigth + + changePageEffect(gui.data.plugin, fromPage, toPage, height) { currentOffset, ifInverted -> + if (ifInverted) { + gui.loadPage(toPage, offsetVertically = -(height - currentOffset)) + } else { + gui.loadPage(toPage, offsetVertically = height - currentOffset) + } + } } } } + companion object { + + private inline fun changePageEffect( + kSpigot: KSpigot, + fromPage: Int, + toPage: Int, + doFor: Int, + crossinline effect: (currentOffset: Int, ifInverted: Boolean) -> Unit, + ) { + + val ifInverted = fromPage >= toPage + + var currentOffset = 1 + kSpigot.task( + sync = true, + period = 1, + howOften = doFor.toLong() + ) { + + effect.invoke(currentOffset, ifInverted) + + currentOffset++ + + } + + } + + } + } \ No newline at end of file