From 1e63fa95a919277b79f36dae508982dbcccc6ff2 Mon Sep 17 00:00:00 2001 From: Danielle Voznyy Date: Mon, 19 Aug 2024 18:38:31 -0400 Subject: [PATCH] fix: Try to auto calculate Scrollable size, consider scroll direction as a parameter --- .../guiy/example/GuiyExamplePlugin.kt | 14 ++++-- .../guiy/example/gui/ScrollingMenu.kt | 47 +++++++++++++++++++ .../guiy/components/lists/Scrollable.kt | 21 +++++---- 3 files changed, 69 insertions(+), 13 deletions(-) create mode 100644 guiy-example/src/main/kotlin/com/mineinabyss/guiy/example/gui/ScrollingMenu.kt diff --git a/guiy-example/src/main/kotlin/com/mineinabyss/guiy/example/GuiyExamplePlugin.kt b/guiy-example/src/main/kotlin/com/mineinabyss/guiy/example/GuiyExamplePlugin.kt index 8780e66..4749475 100644 --- a/guiy-example/src/main/kotlin/com/mineinabyss/guiy/example/GuiyExamplePlugin.kt +++ b/guiy-example/src/main/kotlin/com/mineinabyss/guiy/example/GuiyExamplePlugin.kt @@ -1,9 +1,6 @@ package com.mineinabyss.guiy.example -import com.mineinabyss.guiy.example.gui.AnimatedTitle -import com.mineinabyss.guiy.example.gui.ArrangementMenu -import com.mineinabyss.guiy.example.gui.CreativeMenu -import com.mineinabyss.guiy.example.gui.Cursor +import com.mineinabyss.guiy.example.gui.* import com.mineinabyss.guiy.inventory.guiy import com.mineinabyss.idofront.commands.brigadier.commands import org.bukkit.plugin.java.JavaPlugin @@ -42,7 +39,14 @@ class GuiyExamplePlugin : JavaPlugin() { "pagination" { playerExecutes { guiy { - com.mineinabyss.guiy.example.gui.PaginatedMenu(player) + PaginatedMenu(player) + } + } + } + "scrolling" { + playerExecutes { + guiy { + ScrollingMenu(player) } } } diff --git a/guiy-example/src/main/kotlin/com/mineinabyss/guiy/example/gui/ScrollingMenu.kt b/guiy-example/src/main/kotlin/com/mineinabyss/guiy/example/gui/ScrollingMenu.kt new file mode 100644 index 0000000..f98d44a --- /dev/null +++ b/guiy-example/src/main/kotlin/com/mineinabyss/guiy/example/gui/ScrollingMenu.kt @@ -0,0 +1,47 @@ +package com.mineinabyss.guiy.example.gui + +import androidx.compose.runtime.* +import com.mineinabyss.guiy.components.Item +import com.mineinabyss.guiy.components.VerticalGrid +import com.mineinabyss.guiy.components.canvases.Chest +import com.mineinabyss.guiy.components.lists.NavbarPosition +import com.mineinabyss.guiy.components.lists.ScrollDirection +import com.mineinabyss.guiy.components.lists.Scrollable +import com.mineinabyss.guiy.inventory.LocalGuiyOwner +import com.mineinabyss.guiy.modifiers.Modifier +import com.mineinabyss.guiy.modifiers.click.clickable +import com.mineinabyss.guiy.modifiers.fillMaxSize +import org.bukkit.Material +import org.bukkit.entity.Player +import org.bukkit.inventory.ItemStack + +@Composable +fun ScrollingMenu(player: Player) { + val owner = LocalGuiyOwner.current + Chest( + setOf(player), + "Pagination example", + onClose = { owner.exit() }, + modifier = Modifier.fillMaxSize() + ) { + val items = remember { + val materials = Material.entries + (1..100).map { ItemStack(materials[it]) } + } + var line by remember { mutableStateOf(0) } + Scrollable( + items, + line = line, + scrollDirection = ScrollDirection.VERTICAL, + navbarPosition = NavbarPosition.END, + previousButton = { Item(Material.RED_CONCRETE, "Previous", modifier = Modifier.clickable { line-- }) }, + nextButton = { Item(Material.BLUE_CONCRETE, "Next", modifier = Modifier.clickable { line++ }) }, + ) { pageItems -> + VerticalGrid { + pageItems.forEach { item -> + Item(item) + } + } + } + } +} diff --git a/src/main/kotlin/com/mineinabyss/guiy/components/lists/Scrollable.kt b/src/main/kotlin/com/mineinabyss/guiy/components/lists/Scrollable.kt index c19ea0e..1bf097b 100644 --- a/src/main/kotlin/com/mineinabyss/guiy/components/lists/Scrollable.kt +++ b/src/main/kotlin/com/mineinabyss/guiy/components/lists/Scrollable.kt @@ -11,12 +11,15 @@ import com.mineinabyss.idofront.items.editItemMeta import org.bukkit.Material import org.bukkit.inventory.ItemStack +enum class ScrollDirection { + VERTICAL, HORIZONTAL; +} + @Composable fun Scrollable( items: List, - startLine: Int, - itemsPerLine: Int, - totalLines: Int, + line: Int, + scrollDirection: ScrollDirection, nextButton: @Composable () -> Unit, previousButton: @Composable () -> Unit, navbarPosition: NavbarPosition = NavbarPosition.BOTTOM, @@ -28,9 +31,11 @@ fun Scrollable( content: @Composable (page: List) -> Unit, ) { var size by remember { mutableStateOf(Size(0, 0)) } + val itemsPerLine = if (scrollDirection == ScrollDirection.VERTICAL) size.width else size.height + val totalLines = if (scrollDirection == ScrollDirection.VERTICAL) size.height else size.width Box(Modifier.fillMaxSize()) { - val start = startLine * itemsPerLine - val end = (startLine + 1) * itemsPerLine * totalLines + val start = line * itemsPerLine + val end = (line + 1) * itemsPerLine * totalLines val pageItems = remember(start, end) { if (start < 0) emptyList() else items.subList(start, end.coerceAtMost(items.size)) @@ -39,10 +44,10 @@ fun Scrollable( position = navbarPosition, navbar = { NavbarButtons(navbarPosition, navbarBackground) { - if (startLine > 0) previousButton() - //else Spacer(1, 1) + if (line > 0) previousButton() + else Spacer(1, 1) if (end < items.size) nextButton() - //else Spacer(1, 1) + else Spacer(1, 1) } }, content = {