11package com.operationpotato.itemlist.gui.recipe
22
33import com.operationpotato.itemlist.Keybinds
4- import com.operationpotato.itemlist.favorites.FavoritesManager
54import com.operationpotato.itemlist.SkyBlockItemList.logger
65import com.operationpotato.itemlist.api.impl.PluginManager
6+ import com.operationpotato.itemlist.favorites.FavoritesManager
7+ import com.operationpotato.itemlist.gui.SpacerTextWidget
78import com.operationpotato.itemlist.utils.SkyBlockRecipeAPI
9+ import net.minecraft.client.Minecraft
810import net.minecraft.client.gui.components.AbstractContainerWidget
9- import net.minecraft.client.gui.components.ScrollableLayout
11+ import net.minecraft.client.gui.components.AbstractWidget.playButtonClickSound
12+ import net.minecraft.client.gui.components.Button
1013import net.minecraft.client.gui.layouts.FrameLayout
11- import net.minecraft.client.gui.layouts.Layout
1214import net.minecraft.client.gui.layouts.LinearLayout
1315import net.minecraft.client.gui.screens.Screen
16+ import net.minecraft.client.gui.screens.inventory.PageButton
1417import net.minecraft.client.input.KeyEvent
1518import net.minecraft.client.input.MouseButtonEvent
1619import net.minecraft.world.item.ItemStack
@@ -29,15 +32,54 @@ import tech.thatgravyboat.skyblockapi.utils.text.TextColor
2932import tech.thatgravyboat.skyblockapi.utils.text.TextStyle.color
3033import kotlin.jvm.optionals.getOrNull
3134
32- class RecipeScreen (val parent : Screen ? , val recipes : List <AbstractRecipeWidget >) : Screen(Text .of("Recipe Screen ")) {
33- var isOversized = false
35+ class RecipeScreen (val parent : Screen ? , val recipes : List <AbstractRecipeWidget >, val pageIndex : Int = 0 ) : Screen(Text .of("Recipe Screen ")) {
36+
37+ var pageAmount: Int = 0
38+
39+ val prevPageButton: Button = PageButton (0 , 0 , false , { _ ->
40+ goBackward()
41+ playButtonClickSound(Minecraft .getInstance().soundManager)
42+ }, false )
43+ val nextPageButton: Button = PageButton (0 , 0 , true , { _ ->
44+ goForward()
45+ playButtonClickSound(Minecraft .getInstance().soundManager)
46+ }, false )
47+ var topLayout: LinearLayout = LinearLayout .horizontal()
3448
3549 override fun init () {
3650 super .init ()
3751
38- var layout: Layout = LinearLayout .vertical().spacing(10 ).apply { recipes.forEach { addChild(it) } }
39- isOversized = recipes.sumOf { it.height + 10 } > height
40- if (isOversized) layout = ScrollableLayout (McClient .self, layout, height)
52+ val pages = mutableListOf (mutableListOf<AbstractRecipeWidget >())
53+ val allowedSize = this @RecipeScreen.height / 5 * 4
54+
55+ recipes.forEach { recipe ->
56+ if (pages.last().sumOf { it.height + 5 } + recipe.height > allowedSize) {
57+ pages.add(mutableListOf (recipe))
58+ } else {
59+ pages.last().add(recipe)
60+ }
61+ }
62+
63+ pageAmount = pages.size - 1
64+ val pageIndex = pageIndex.coerceIn(0 , pageAmount)
65+
66+ topLayout = LinearLayout .horizontal()
67+ val layout = LinearLayout .vertical().spacing(5 ).apply {
68+ topLayout.addChild(prevPageButton) { it.alignHorizontallyLeft() }
69+ topLayout.addChild(SpacerTextWidget (
70+ 0 ,
71+ 0 ,
72+ pages[pageIndex].maxBy { it.width }.width - 46 ,
73+ Text .of(" ${pageIndex + 1 } / ${pages.size} " ),
74+ font
75+ ))
76+ topLayout.addChild(nextPageButton) { it.alignHorizontallyRight() }
77+ topLayout.arrangeElements()
78+ addChild(topLayout)
79+
80+ pages[pageIndex].forEach(::addChild)
81+ }
82+
4183 layout.apply {
4284 arrangeElements()
4385 FrameLayout .centerInRectangle(this , this @RecipeScreen.rectangle)
@@ -59,7 +101,6 @@ class RecipeScreen(val parent: Screen?, val recipes: List<AbstractRecipeWidget>)
59101 recipes.forEach {
60102 if (it.right > right) right = it.right
61103 }
62- if (isOversized) right + = 10
63104 return right
64105 }
65106
@@ -97,6 +138,23 @@ class RecipeScreen(val parent: Screen?, val recipes: List<AbstractRecipeWidget>)
97138 return super .keyPressed(event)
98139 }
99140
141+ override fun mouseScrolled (x : Double , y : Double , scrollX : Double , scrollY : Double ): Boolean {
142+ if (super .mouseScrolled(x, y, scrollX, scrollY)) return true
143+ if (x >= topLayout.x && x <= topLayout.x + topLayout.width) {
144+ if (scrollY < 0 ) goForward() else goBackward()
145+ return true
146+ }
147+ return false
148+ }
149+
150+ fun goForward () {
151+ McClient .setScreen(RecipeScreen (parent, recipes, if (pageIndex != pageAmount) pageIndex + 1 else 0 ))
152+ }
153+
154+ fun goBackward () {
155+ McClient .setScreen(RecipeScreen (parent, recipes, if (pageIndex != 0 ) pageIndex - 1 else pageAmount))
156+ }
157+
100158 companion object {
101159 fun openRecipeForItem (stack : ItemStack , parent : Screen ? = null) {
102160 val targetId = stack.getSkyBlockId() ? : return
0 commit comments