Added new page change effects

This commit is contained in:
bluefireoly
2020-09-27 18:35:55 +02:00
parent b180b232a7
commit 7139ddb3cf

View File

@@ -1,5 +1,6 @@
package net.axay.kspigot.inventory package net.axay.kspigot.inventory
import net.axay.kspigot.main.KSpigot
import net.axay.kspigot.runnables.task import net.axay.kspigot.runnables.task
abstract class InventoryGUIPageChangeCalculator { abstract class InventoryGUIPageChangeCalculator {
@@ -21,7 +22,11 @@ abstract class InventoryGUIPageChangeCalculator {
} }
enum class InventoryGUIPageChangeEffect { enum class InventoryGUIPageChangeEffect {
INSTANT, SLIDE_HORIZONTALLY INSTANT,
SLIDE_HORIZONTALLY,
SLIDE_VERTICALLY,
SWIPE_HORIZONTALLY,
SWIPE_VERTICALLY,
} }
class InventoryGUIPageChanger(private val effect: InventoryGUIPageChangeEffect) { class InventoryGUIPageChanger(private val effect: InventoryGUIPageChangeEffect) {
@@ -29,40 +34,98 @@ class InventoryGUIPageChanger(private val effect: InventoryGUIPageChangeEffect)
fun changePage(gui: InventoryGUI, fromPage: Int, toPage: Int) { fun changePage(gui: InventoryGUI, fromPage: Int, toPage: Int) {
when (effect) { when (effect) {
InventoryGUIPageChangeEffect.INSTANT -> gui.loadPage(toPage)
InventoryGUIPageChangeEffect.SLIDE_HORIZONTALLY -> { InventoryGUIPageChangeEffect.SLIDE_HORIZONTALLY -> {
val width = gui.data.inventoryGUIType.dimensions.width val width = gui.data.inventoryGUIType.dimensions.width
val ifInverted = fromPage >= toPage changePageEffect(gui.data.plugin, fromPage, toPage, width) { currentOffset, ifInverted ->
var currentOffset = 1
gui.data.plugin.task(
sync = true,
period = 1,
howOften = width.toLong()
) {
if (ifInverted) { if (ifInverted) {
gui.loadPage(fromPage, currentOffset) gui.loadPage(fromPage, offsetVertically = currentOffset)
gui.loadPage(toPage, - (width - currentOffset)) gui.loadPage(toPage, offsetVertically = -(width - currentOffset))
} else { } else {
gui.loadPage(fromPage, -currentOffset) gui.loadPage(fromPage, offsetVertically = -currentOffset)
gui.loadPage(toPage, width - 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++
}
}
}
} }