Skip to content

Commit

Permalink
feat: font shifts
Browse files Browse the repository at this point in the history
  • Loading branch information
andantet committed Dec 14, 2024
1 parent 70f83b7 commit 5d1980c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ import net.mcbrawls.packin.resource.PackResource
import net.mcbrawls.packin.resource.pack.PackinResourcePack
import net.mcbrawls.packin.resource.pack.ResourceCollector
import net.minecraft.util.Identifier
import net.minecraft.util.math.Vec2f

// TODO: add support for a list of shifts
/**
* A resource provider to add a font.
*/
class FontProvider(
fontId: Identifier,
val fontId: Identifier,
val size: Float,
val oversample: Float,
vararg val shifts: Vec2f,
) : ResourceProvider {
/**
* The file path of the font JSON file.
Expand Down Expand Up @@ -45,21 +46,45 @@ class FontProvider(

val json = createJson()
collector.collect(fontJsonPath, json.toString().encodeToByteArray())

shifts.forEach { shift ->
val shiftedJson = createJson(shift)
val shiftedFontId = createShiftedFontId(fontId, shift)
val shiftedFontJsonPath = shiftedFontId.withPath { "font/$it.json" }
collector.collect(shiftedFontJsonPath, shiftedJson.toString().encodeToByteArray())
}
}

/**
* Creates the font JSON file as an object.
*/
fun createJson(): JsonObject {
fun createJson(shift: Vec2f? = null): JsonObject {
return JsonObject().apply {
add("providers", JsonArray().apply {
add(JsonObject().apply {
addProperty("type", "ttf")
addProperty("file", fontFilePathInJson)
addProperty("size", size)
addProperty("oversample", oversample)

shift?.also { shift ->
add(
"shift",
JsonArray().apply {
add(shift.x)
add(shift.y)
}
)
}
})
})
}
}

companion object {
fun createShiftedFontId(fontId: Identifier, shift: Vec2f): Identifier {
val shiftString = "${shift.x}_${shift.y}"
return fontId.withPath { "${it}_shift_$shiftString" }
}
}
}
3 changes: 2 additions & 1 deletion src/test/kotlin/net/mcbrawls/packin/test/PackinTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import net.minecraft.command.argument.IdentifierArgumentType
import net.minecraft.server.command.CommandManager
import net.minecraft.text.Text
import net.minecraft.util.Identifier
import net.minecraft.util.math.Vec2f
import kotlin.io.path.Path
import kotlin.io.path.writeBytes
import kotlin.math.floor
Expand All @@ -48,7 +49,7 @@ object PackinTest : ModInitializer {
val packBytes = PackinResourcePack.create(PackMetadata("Test", Text.literal("Test"))) {
addProvider(FontProvider(Identifier.of("brawls", "pinch"), 7.0f, 4.0f))
addProvider(FontProvider(Identifier.of("brawls", "love_bug"), 9.0f, 8.0f))
addProvider(FontProvider(Identifier.of("brawls", "chocolate"), 11.0f, 8.0f))
addProvider(FontProvider(Identifier.of("brawls", "chocolate"), 11.0f, 8.0f, Vec2f(0.0f, 1.0f)))

addProvider(
SoundProvider(
Expand Down

0 comments on commit 5d1980c

Please sign in to comment.