Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Commit

Permalink
better text builders (and documentation)
Browse files Browse the repository at this point in the history
  • Loading branch information
homchom committed Oct 27, 2023
1 parent c64d582 commit e56c3fd
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ package io.github.homchom.recode.feature.social

import io.github.homchom.recode.MOD_NAME
import io.github.homchom.recode.render.ColorPalette
import io.github.homchom.recode.ui.text
import io.github.homchom.recode.ui.style
import io.github.homchom.recode.ui.translateText
import io.github.homchom.recode.util.regex.regex
import net.minecraft.client.GuiMessageTag

Expand All @@ -18,9 +19,7 @@ private val stackRegex = regex {
fun stackedMessageTag(amount: Int) = GuiMessageTag(
ColorPalette.AQUA.hex,
GuiMessageTag.Icon.CHAT_MODIFIED,
text {
color(aqua) { translate("chat.tag.recode.stacked", amount) }
},
translateText("chat.tag.recode.stacked", style().red(), arrayOf(amount)),
"$stackTagPrefix$amount"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.github.homchom.recode.feature.feature
import io.github.homchom.recode.id
import io.github.homchom.recode.render.IntegralColor
import io.github.homchom.recode.render.toColor
import io.github.homchom.recode.ui.style
import io.github.homchom.recode.ui.text
import net.fabricmc.fabric.api.resource.ResourceManagerHelper
import net.fabricmc.fabric.api.resource.ResourcePackActivationType
Expand All @@ -24,7 +25,7 @@ private fun registerBuiltInResourcePack(
Recode,
text {
literal("[$MOD_ID] ")
color(displayColor) { translate("resourcePack.recode.$id") }
translate("resourcePack.recode.$id", style().color(displayColor))
},
activationType
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import io.github.homchom.recode.hypercube.DFValueMeta
import io.github.homchom.recode.hypercube.dfValueMeta
import io.github.homchom.recode.mixin.render.chat.CommandSuggestionsAccessor
import io.github.homchom.recode.render.HexColor
import io.github.homchom.recode.ui.TextBuilder
import io.github.homchom.recode.ui.deserializeToNative
import io.github.homchom.recode.ui.style
import io.github.homchom.recode.util.Computation
import io.github.homchom.recode.util.map
import io.github.homchom.recode.util.regex.regex
import net.kyori.adventure.text.minimessage.MiniMessage
import net.minecraft.network.chat.Component
import net.minecraft.network.chat.Style
import net.minecraft.world.item.ItemStack

typealias HighlightedExpression = Computation<Component, String>
Expand Down Expand Up @@ -139,14 +140,13 @@ object ExpressionHighlighter {
}
} else {
object : HighlightBuilder {
private val builder = Component.empty()
private val builder = TextBuilder()

override fun append(text: String, depth: Int, depthIncreased: Boolean) {
val style = Style.EMPTY.withColor(colors[depth % colors.size])
builder.append(Component.literal(text).withStyle(style))
builder.literal(text, style().color(colors[depth % colors.size]))
}

override fun build(): Component = builder
override fun build() = builder.result
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/github/homchom/recode/ui/ChatUI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ operator fun GuiMessageTag.plus(other: GuiMessageTag): GuiMessageTag {
if (first.ordinal > second.ordinal) first else second
}
val newText = combineIfNotNull(text, other.text) { first, second ->
text { append(first); space(); append(second) }
first.copy().append(" ").append(second)
}
val newLogTag = combineIfNotNull(logTag, other.logTag) { first, second ->
"$first, $second"
Expand Down
98 changes: 0 additions & 98 deletions src/main/java/io/github/homchom/recode/ui/TextBuilder.kt

This file was deleted.

165 changes: 165 additions & 0 deletions src/main/java/io/github/homchom/recode/ui/TextBuilders.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package io.github.homchom.recode.ui

import io.github.homchom.recode.render.ColorPalette
import io.github.homchom.recode.render.IntegralColor
import net.minecraft.network.chat.*
import net.minecraft.network.chat.HoverEvent.EntityTooltipInfo
import net.minecraft.network.chat.HoverEvent.ItemStackInfo
import net.minecraft.resources.ResourceLocation

typealias TextScope = TextBuilder.() -> Unit

/**
* Creates a translated [Component] with [key], [args], and [style].
*/
fun translateText(
key: String,
style: StyleWrapper = style(),
args: Array<out Any> = emptyArray()
): Component {
return Component.translatable(key, *args).withStyle(style.result)
}

/**
* Creates a [Component] with the literal [string] and [style].
*/
fun literalText(string: String, style: StyleWrapper = style()): Component =
Component.literal(string).withStyle(style.result)

/**
* Builds a [Component] by adding [style] to [root] and applying [builder].
*
* use [translateText] and [literalText] when applicable, as their output is more optimized.
*
* @see TextBuilder
*/
inline fun text(
style: StyleWrapper = style(),
root: MutableComponent = Component.empty(),
builder: TextScope
): Component {
return root.withStyle(style.result)
.let(::TextBuilder)
.apply(builder)
.result
}

/**
* Creates a [StyleWrapper].
*/
fun style(initial: Style = Style.EMPTY) = StyleWrapper(initial)

/**
* A builder class for text [Component] objects.
*
* @see translate
* @see literal
*/
@JvmInline
value class TextBuilder(val result: MutableComponent = Component.empty()) {
/**
* Appends [translateText] with [key], [args], and [style] and applies [builder] to it.
*/
inline fun translate(
key: String,
style: StyleWrapper = style(),
args: Array<out Any> = emptyArray(),
builder: TextScope = {}
) {
result.append(text(style, Component.translatable(key, *args), builder))
}

/**
* Appends [literalText] with [string] and [style] and applies [builder] to it.
*/
inline fun literal(
string: String,
style: StyleWrapper = style(),
builder: TextScope = {}
) {
result.append(text(style, Component.literal(string), builder))
}
}

/**
* A wrapper class for idiomatic [Style] creation.
*/
@Suppress("unused")
@JvmInline
value class StyleWrapper(val result: Style) {
private inline fun map(transform: Style.() -> Style) = StyleWrapper(result.transform())

fun black() = color(ColorPalette.BLACK)

fun darkBlue() = color(ColorPalette.DARK_BLUE)

fun darkGreen() = color(ColorPalette.DARK_GREEN)

fun darkAqua() = color(ColorPalette.DARK_AQUA)

fun darkRed() = color(ColorPalette.DARK_RED)

fun darkPurple() = color(ColorPalette.DARK_PURPLE)

fun gold() = color(ColorPalette.GOLD)

fun gray() = color(ColorPalette.GRAY)

fun darkGray() = color(ColorPalette.DARK_GRAY)

fun blue() = color(ColorPalette.BLUE)

fun green() = color(ColorPalette.GREEN)

fun aqua() = color(ColorPalette.AQUA)

fun red() = color(ColorPalette.RED)

fun lightPurple() = color(ColorPalette.LIGHT_PURPLE)

fun yellow() = color(ColorPalette.YELLOW)

fun white() = color(ColorPalette.WHITE)

fun color(color: IntegralColor) = color(color.toInt())

fun color(color: Int) = map { withColor(color) }

fun bold() = map { withBold(true) }

fun underlined() = map { withUnderlined(true) }

fun italic() = map { withItalic(true) }

fun strikethrough() = map { withStrikethrough(true) }

fun obfuscated() = map { withObfuscated(true) }

fun onClick(action: ClickEvent.Action, value: String) =
map { withClickEvent(ClickEvent(action, value)) }

val openUrl get() = ClickEvent.Action.OPEN_URL

val openFile get() = ClickEvent.Action.OPEN_FILE

val runCommand get() = ClickEvent.Action.RUN_COMMAND

val suggestCommand get() = ClickEvent.Action.SUGGEST_COMMAND

val changePage get() = ClickEvent.Action.CHANGE_PAGE

val copyToClipboard get() = ClickEvent.Action.COPY_TO_CLIPBOARD

fun <T : Any> onHover(action: HoverEvent.Action<T>, value: T) =
map { withHoverEvent(HoverEvent(action, value)) }

val showText: HoverEvent.Action<Component> get() = HoverEvent.Action.SHOW_TEXT

val showItem: HoverEvent.Action<ItemStackInfo> get() = HoverEvent.Action.SHOW_ITEM

val showEntity: HoverEvent.Action<EntityTooltipInfo> get() = HoverEvent.Action.SHOW_ENTITY

fun insert(string: String) = map { withInsertion(string) }

fun font(id: ResourceLocation) = map { withFont(id) }
}
4 changes: 2 additions & 2 deletions src/main/java/io/github/homchom/recode/util/Matcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ fun interface Matcher<in T, out R : Any> {
}

/**
* Creates and returns a [MatcherList] with [initialPredicates].
* Creates a [MatcherList] with [initialPredicates].
*/
fun <T, R : Any> matcherOf(vararg initialPredicates: Matcher<T, R>) =
MatcherList<T, R>().apply { addAll(initialPredicates) }

/**
* Creates and returns a [MatcherList] with [initialPredicates].
* Creates a [MatcherList] with [initialPredicates].
*/
fun <T, R : Matcher<T, R>> matcherOf(initialPredicates: Collection<R>) =
MatcherList<T, R>().apply { addAll(initialPredicates) }
Expand Down

0 comments on commit e56c3fd

Please sign in to comment.