-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced a higher level wrapper for multi page screens with cramming options
- Loading branch information
Showing
5 changed files
with
160 additions
and
5 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
api/src/main/kotlin/com/mattmx/ktgui/components/screen/pagination/GuiCramMultiPageScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.mattmx.ktgui.components.screen.pagination | ||
|
||
import com.mattmx.ktgui.components.button.GuiButton | ||
import com.mattmx.ktgui.components.screen.GuiScreen | ||
import com.mattmx.ktgui.dsl.button | ||
import com.mattmx.ktgui.utils.not | ||
import net.kyori.adventure.text.Component | ||
import org.bukkit.Material | ||
|
||
class GuiCramMultiPageScreen( | ||
title: Component, | ||
rows: Int = 6 | ||
) : GuiMultiPageScreen(title, rows) { | ||
val extraReservedSlots = arrayListOf<Int>() | ||
|
||
infix fun reserve(slots: IntRange) = extraReservedSlots.addAll(slots) | ||
infix fun reserve(slots: List<Int>) = extraReservedSlots.addAll(slots) | ||
|
||
operator fun GuiButton<*>.unaryPlus() = cramAdd(this) | ||
operator fun Collection<GuiButton<*>>.unaryPlus() = forEach { cramAdd(it) } | ||
open fun cramAdd(child: GuiButton<*>) { | ||
var lastPage = pages.lastOrNull() | ||
|
||
// If it's full then make a new one | ||
if (lastPage == null || isFull(lastPage)) { | ||
lastPage = GuiScreen(Component.empty(), rows).apply { pages.add(this) } | ||
} | ||
|
||
val nextSlot = nextSlotToFill(lastPage) | ||
if (nextSlot == null) { | ||
GuiScreen(Component.empty(), rows).apply { pages.add(this) } | ||
return cramAdd(child) | ||
} | ||
|
||
child childOf lastPage slot nextSlot | ||
} | ||
|
||
fun nextSlotToFill(sub: GuiScreen): Int? { | ||
var nextSlot = sub.slotsUsed().max() + 1 | ||
|
||
while (nextSlot in reservedSlots() && nextSlot <= sub.totalSlots()) { | ||
nextSlot++ | ||
} | ||
|
||
if (nextSlot >= sub.totalSlots()) return null | ||
|
||
return nextSlot | ||
} | ||
|
||
fun isFull(sub: GuiScreen) = sub.slotsUsed().size >= totalSlots() - reservedSlots().size | ||
|
||
fun reservedSlots() = this.slotsUsed() + extraReservedSlots | ||
|
||
} | ||
|
||
fun cramMultiPageScreen(title: Component, rows: Int = 6, block: GuiCramMultiPageScreen.() -> Unit) = | ||
GuiCramMultiPageScreen(title, rows).apply(block) | ||
|
||
fun main() { | ||
val gui = cramMultiPageScreen(!"Materials") { | ||
reserve(last() - 8..last()) | ||
|
||
button(Material.SPECTRAL_ARROW) { | ||
named(!"&aNext") | ||
click.left { navigateNextPage() } | ||
} slot last() | ||
|
||
button(Material.SPECTRAL_ARROW) { | ||
named(!"&cLast") | ||
click.left { navigatePreviousPage() } | ||
} slot last() - 8 | ||
|
||
+Material.values().map { button(it) {} } | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
api/src/main/kotlin/com/mattmx/ktgui/components/screen/pagination/GuiMultiPageScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.mattmx.ktgui.components.screen.pagination | ||
|
||
import com.mattmx.ktgui.components.screen.GuiScreen | ||
import com.mattmx.ktgui.dsl.button | ||
import com.mattmx.ktgui.event.EventCallback | ||
import com.mattmx.ktgui.utils.not | ||
import net.kyori.adventure.text.Component | ||
import org.bukkit.Material | ||
import java.util.* | ||
import kotlin.math.max | ||
import kotlin.math.min | ||
|
||
open class GuiMultiPageScreen( | ||
title: Component, | ||
rows: Int = 6 | ||
) : GuiScreen(title, rows) { | ||
var currentPage = 0 | ||
set(value) { | ||
field = value | ||
refresh() | ||
pageChange.invoke(value) | ||
} | ||
val pageChange = EventCallback<Int>() | ||
val pages = Collections.synchronizedList(arrayListOf<GuiScreen>()) | ||
|
||
infix fun page(block: GuiScreen.() -> Unit) = page(null, block) | ||
open fun page(index: Int? = null, block: GuiScreen.() -> Unit) = apply { | ||
val sub = GuiScreen(Component.empty(), rows).apply(block) | ||
if (index == null) { | ||
pages.add(sub) | ||
} else { | ||
pages[index] = sub | ||
} | ||
} | ||
|
||
open fun navigatePreviousPage() { | ||
currentPage = max(0, currentPage - 1) | ||
} | ||
|
||
open fun navigateNextPage() { | ||
currentPage = min(pages.size, currentPage + 1) | ||
} | ||
} | ||
|
||
fun multiPageGui(title: Component, rows: Int = 6, block: GuiMultiPageScreen.() -> Unit) = | ||
GuiMultiPageScreen(title, rows).apply(block) | ||
|
||
fun main() { | ||
val gui = multiPageGui(!"Test") { | ||
button(Material.SPECTRAL_ARROW) { | ||
named(!"&aLast") | ||
click.left { navigatePreviousPage() } | ||
} slot last() | ||
|
||
button(Material.SPECTRAL_ARROW) { | ||
named(!"&aNext") | ||
click.left { navigateNextPage() } | ||
} slot last() | ||
|
||
page { | ||
|
||
} | ||
|
||
page { | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 13 additions & 1 deletion
14
plugin/src/main/kotlin/com/mattmx/ktgui/examples/SignalScoreboardExample.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters