Added dynamic-size components

This commit is contained in:
Auxilor
2022-09-30 13:29:50 +01:00
parent 27d2b5c8a4
commit e1c063d5f4
4 changed files with 29 additions and 5 deletions

View File

@@ -25,6 +25,24 @@ public interface GUIComponent {
*/
int getColumns();
/**
* Initialize the component.
* <p>
* This is called before getRows / getColumns is queried,
* and allows for dynamically sized components.
* <p>
* getRows and getColumns can return values bigger than this,
* it will simply prevent the component from being added at
* this position (for minimum-sized components).
*
* @param maxRows The maximum number of rows.
* @param maxColumns The maximum number of columns.
*/
default void init(final int maxRows,
final int maxColumns) {
// Most components will not require initialization.
}
/**
* Get the slot at a certain position in the component.
* <p>

View File

@@ -161,7 +161,7 @@ public interface MenuBuilder extends PageBuilder {
* @param action The action.
* @return THe builder.
*/
default MenuBuilder onSignalReceive(@NotNull final SignalHandler action) {
default MenuBuilder onSignal(@NotNull final SignalHandler action) {
return this;
}

View File

@@ -142,9 +142,9 @@ fun MenuBuilder.addPage(page: Int, creation: PageBuilder.() -> Unit): MenuBuilde
return this.addPage(Page(page, builder.build()))
}
/** @see MenuBuilder.onSignalReceive */
inline fun <reified T : Signal> MenuBuilder.onSignalReceive(crossinline handler: (Player, Menu, T) -> Unit): MenuBuilder {
return this.onSignalReceive(object : SignalHandler<T>(T::class.java) {
/** @see MenuBuilder.onSignal */
inline fun <reified T : Signal> MenuBuilder.onSignal(crossinline handler: (Player, Menu, T) -> Unit): MenuBuilder {
return this.onSignal(object : SignalHandler<T>(T::class.java) {
override fun handle(player: Player, menu: Menu, signal: T) =
handler(player, menu, signal)
})

View File

@@ -30,6 +30,12 @@ class EcoMenuBuilder(private val rows: Int) : MenuBuilder {
override fun addComponent(layer: MenuLayer, row: Int, column: Int, component: GUIComponent): MenuBuilder {
require(row in 1..rows) { "Invalid row number!" }
require(column in 1..9) { "Invalid column number!" }
val maxRows = 1 + rows - row
val maxColumns = 10 - column
component.init(maxRows, maxColumns)
require(column + component.columns - 1 <= 9) { "Component is too large to be placed here!" }
require(row + component.rows - 1 <= getRows()) { "Component is too large to be placed here!" }
@@ -60,7 +66,7 @@ class EcoMenuBuilder(private val rows: Int) : MenuBuilder {
return this
}
override fun onSignalReceive(action: SignalHandler<*>): MenuBuilder {
override fun onSignal(action: SignalHandler<*>): MenuBuilder {
signalHandlers += action
return this
}