Skip to content

Commit

Permalink
feat: TooltipChunk mutability methods
Browse files Browse the repository at this point in the history
  • Loading branch information
andantet committed Nov 11, 2024
1 parent dcd94bf commit 006aae1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
13 changes: 1 addition & 12 deletions src/main/kotlin/net/mcbrawls/slate/tile/Tile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,7 @@ abstract class Tile {
fun tooltip(chunks: List<TooltipChunk>) {
val lastIndex = chunks.lastIndex
chunks.forEachIndexed { index, chunk ->
val texts = chunk.texts
if (texts.isNotEmpty()) {
// append chunk
texts.forEach { text ->
tooltip.add(text.copy().fillStyle(chunk.style))
}

// append break
if (index != lastIndex) {
tooltip.add(Text.empty())
}
}
chunk.modifyTooltip(tooltip, index, lastIndex)
}
}

Expand Down
28 changes: 28 additions & 0 deletions src/main/kotlin/net/mcbrawls/slate/tooltip/TooltipChunk.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,34 @@ class TooltipChunk(
style = styler.invoke(style)
}

/**
* Adds a text component to this chunk.
*/
fun add(text: Text) {
texts.add(text)
}

/**
* Adds a text component to this chunk.
*/
fun add(text: String) {
add(Text.literal(text))
}

internal fun modifyTooltip(tooltip: MutableList<Text>, index: Int, lastIndex: Int) {
if (texts.isNotEmpty()) {
// append chunk
texts.forEach { text ->
tooltip.add(text.copy().fillStyle(style))
}

// append break
if (index != lastIndex) {
tooltip.add(Text.empty())
}
}
}

companion object {
/**
* Builds a tooltip chunk.
Expand Down

0 comments on commit 006aae1

Please sign in to comment.