Add support for skinning in Rhubarb for Spine

This commit is contained in:
Daniel Wolf 2021-11-21 11:16:07 +01:00
parent 7a04b7ed41
commit a86ec43f23
2 changed files with 18 additions and 12 deletions

View File

@ -2,6 +2,8 @@
## Unreleased ## Unreleased
* **Added** support for skinning in Rhubarb for Spine ([issue #108](https://github.com/DanielSWolf/rhubarb-lip-sync/issues/108))
## Version 1.11.0 ## Version 1.11.0
* **Added** support for more WAVE file features ([issue #101](https://github.com/DanielSWolf/rhubarb-lip-sync/issues/101)) * **Added** support for more WAVE file features ([issue #101](https://github.com/DanielSWolf/rhubarb-lip-sync/issues/101))

View File

@ -10,7 +10,6 @@ class SpineJson(private val filePath: Path) {
private val fileDirectoryPath: Path = filePath.parent private val fileDirectoryPath: Path = filePath.parent
private val json: JsonObject private val json: JsonObject
private val skeleton: JsonObject private val skeleton: JsonObject
private val defaultSkin: JsonObject
init { init {
if (!Files.exists(filePath)) { if (!Files.exists(filePath)) {
@ -22,12 +21,6 @@ class SpineJson(private val filePath: Path) {
throw EndUserException("Wrong file format. This is not a valid JSON file.") throw EndUserException("Wrong file format. This is not a valid JSON file.")
} }
skeleton = json.obj("skeleton") ?: throw EndUserException("JSON file is corrupted.") skeleton = json.obj("skeleton") ?: throw EndUserException("JSON file is corrupted.")
val skins = json["skins"] ?: throw EndUserException("JSON file doesn't contain skins.")
defaultSkin = when (skins) {
is JsonObject -> skins.obj("default")
is JsonArray<*> -> (skins as JsonArray<JsonObject>).find { it.string("name") == "default" }
else -> null
} ?: throw EndUserException("JSON file doesn't have a default skin.")
validateProperties() validateProperties()
} }
@ -96,10 +89,21 @@ class SpineJson(private val filePath: Path) {
} }
fun getSlotAttachmentNames(slotName: String): List<String> { fun getSlotAttachmentNames(slotName: String): List<String> {
val attachments = defaultSkin.obj(slotName) @Suppress("UNCHECKED_CAST")
?: defaultSkin.obj("attachments")?.obj(slotName) val skins: Collection<JsonObject> = when (val skinsObject = json["skins"]) {
?: JsonObject() is JsonObject -> skinsObject.values as Collection<JsonObject>
return attachments.map { it.key } is JsonArray<*> -> skinsObject as Collection<JsonObject>
else -> emptyList()
}
// Get attachment names for all skins
return skins
.flatMap { skin ->
skin.obj(slotName)?.keys?.toList()
?: skin.obj("attachments")?.obj(slotName)?.keys?.toList()
?: emptyList<String>()
}
.distinct()
} }
val animationNames = observableSet<String>( val animationNames = observableSet<String>(
@ -156,4 +160,4 @@ class SpineJson(private val filePath: Path) {
fun save() { fun save() {
Files.write(filePath, listOf(toString()), StandardCharsets.UTF_8) Files.write(filePath, listOf(toString()), StandardCharsets.UTF_8)
} }
} }