Skip to content

Commit

Permalink
fix: Try to auto calculate Scrollable size, consider scroll direction…
Browse files Browse the repository at this point in the history
… as a parameter
  • Loading branch information
0ffz committed Aug 19, 2024
1 parent 554b441 commit 1e63fa9
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -42,7 +39,14 @@ class GuiyExamplePlugin : JavaPlugin() {
"pagination" {
playerExecutes {
guiy {
com.mineinabyss.guiy.example.gui.PaginatedMenu(player)
PaginatedMenu(player)
}
}
}
"scrolling" {
playerExecutes {
guiy {
ScrollingMenu(player)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T> Scrollable(
items: List<T>,
startLine: Int,
itemsPerLine: Int,
totalLines: Int,
line: Int,
scrollDirection: ScrollDirection,
nextButton: @Composable () -> Unit,
previousButton: @Composable () -> Unit,
navbarPosition: NavbarPosition = NavbarPosition.BOTTOM,
Expand All @@ -28,9 +31,11 @@ fun <T> Scrollable(
content: @Composable (page: List<T>) -> 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))
Expand All @@ -39,10 +44,10 @@ fun <T> 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 = {
Expand Down

0 comments on commit 1e63fa9

Please sign in to comment.