Skip to content

Commit

Permalink
feat: LayerPageSourceListener
Browse files Browse the repository at this point in the history
  • Loading branch information
andantet committed Nov 12, 2024
1 parent 260c5eb commit befdbdc
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 30 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ kotlin_version=2.0.21
fabric_kotlin_version=1.12.3

# mod properties
mod_version=1.6
mod_version=1.7
maven_group=net.mcbrawls
mod_id=slate
2 changes: 1 addition & 1 deletion src/main/kotlin/net/mcbrawls/slate/Slate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import net.mcbrawls.slate.callback.SlateCloseCallback
import net.mcbrawls.slate.callback.SlateOpenCallback
import net.mcbrawls.slate.callback.SlateTickCallback
import net.mcbrawls.slate.callback.handler.SlateCallbackHandler
import net.mcbrawls.slate.layer.PagedSlateLayer
import net.mcbrawls.slate.layer.SlateLayer
import net.mcbrawls.slate.layer.paged.PagedSlateLayer
import net.mcbrawls.slate.screen.SlateScreenHandler
import net.mcbrawls.slate.screen.SlateScreenHandlerFactory
import net.mcbrawls.slate.screen.slot.TileClickContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ open class SlateLayerCallbackHandler {
callbacks.add(callback)
}

/**
* Adds a callback invoked on the layer if a page changes.
*/
fun onPageChange(callback: SlateLayerPageChangeCallback) {
callbacks.add(callback)
}

/**
* Combines all callbacks for the given type into one callable object.
*/
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.mcbrawls.slate.layer.paged

/**
* Listens to modifications to the internal collection and updates the layer accordingly.
*/
class LayerPageSourceListener<T>(
val layer: PagedSlateLayer,
private val _collection: MutableCollection<T> = mutableListOf(),
) {
val collection: Collection<T> get() = _collection.toList()

/**
* Modify and update the internal collection.
*/
fun modify(action: MutableCollection<T>.() -> Unit) {
synchronized(_collection) {
action.invoke(_collection)
layer.slotCount = _collection.size
layer.updateTileGrid()
}
}

companion object {
/**
* Creates a page source listener from this collection.
*/
fun <T> MutableCollection<T>.layerPageListener(layer: PagedSlateLayer) = LayerPageSourceListener(layer, this)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.mcbrawls.slate.layer
package net.mcbrawls.slate.layer.paged

import net.mcbrawls.slate.layer.SlateLayer
import net.mcbrawls.slate.tile.StackTile
import net.mcbrawls.slate.tile.Tile
import net.mcbrawls.slate.tile.Tile.Companion.tile
Expand All @@ -24,13 +25,7 @@ abstract class PagedSlateLayer(
var slotCount: Int = slotCount
set(value) {
field = value

// update page
val oldPage = currentPage
currentPage = oldPage
if (currentPage == oldPage) {
updateTileGrid()
}
currentPage = currentPage
}

/**
Expand All @@ -43,16 +38,7 @@ abstract class PagedSlateLayer(
*/
var currentPage: Int = 0
set(value) {
val page = wrapPage(value, maxPage)

if (field == page) {
return
}

field = page
updateTileGrid()

// callbackHandler.collectCallbacks<SlateLayerPageChangeCallback>().invoke()
field = wrapPage(value, maxPage)
}

init {
Expand Down Expand Up @@ -100,6 +86,7 @@ abstract class PagedSlateLayer(
onClick { slate, tile, context ->
callback?.onClick(slate, tile, context)
currentPage = modifier.invoke(currentPage)
updateTileGrid()
}
}
}
Expand Down

0 comments on commit befdbdc

Please sign in to comment.