Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
package korlibs.korge.gradle.util

data class ASEInfo(
val pixelWidth: Int = 0,
val pixelHeight: Int = 0,
val slices: List<AseSlice> = emptyList(),
val tags: List<AseTag> = emptyList(),
val layers: List<AseLayer> = emptyList(),
val frames: List<Frame> = emptyList(),
) {

val tagsByName: Map<String, AseTag> by lazy { tags.associateBy { it.tagName } }
val layersByName: Map<String, AseLayer> by lazy { layers.associateBy { it.layerName } }

data class AseSlice(
val sliceName: String,
val hasNinePatch: Boolean,
val hasPivotInfo: Boolean,
)
val keys: List<SliceKey>
) {
data class SliceKey(
val frameNumber: Int,
val x: Int,
val y: Int,
val width: Int,
val height: Int,
val ninePatch: NinePatchInfo? = null,
val pivot: PivotInfo? = null
)
data class NinePatchInfo(
val centerX: Int,
val centerY: Int,
val centerWidth: Int,
val centerHeight: Int
)
data class PivotInfo(
val pivotX: Int,
val pivotY: Int
)
}

data class AseTag(
val fromFrame: Int,
Expand Down Expand Up @@ -65,6 +93,11 @@ data class ASEInfo(
}
}

data class Frame(
val index: Int,
val duration: Int
)

companion object {
fun getAseInfo(file: SFile): ASEInfo {
return getAseInfo(file.readBytes())
Expand All @@ -80,6 +113,7 @@ data class ASEInfo(
val slices = arrayListOf<AseSlice>()
val tags = arrayListOf<AseTag>()
val layers = arrayListOf<AseLayer>()
val frames = arrayListOf<Frame>()

val fileSize = s.readS32LE()
if (s.length < fileSize) error("File too short s.length=${s.length} < fileSize=${fileSize}")
Expand Down Expand Up @@ -118,6 +152,7 @@ data class ASEInfo(
val frameDuration = fs.readU16LE()
fs.skip(2)
val numChunks = fs.readS32LE()
frames += Frame(frameIndex, frameDuration)

//println(" - $numChunks")

Expand Down Expand Up @@ -156,7 +191,38 @@ data class ASEInfo(
val sliceName = cs.readAseString()
val hasNinePatch = sliceFlags.hasBitSet(0)
val hasPivotInfo = sliceFlags.hasBitSet(1)
val aslice = AseSlice(sliceName, hasNinePatch, hasPivotInfo)

// Read 9-patch and pivot info
val keys = mutableListOf<AseSlice.SliceKey>()
for (key in 0 until numSliceKeys) {
val frameNumber = cs.readS32LE()
val x = cs.readU32LE()
val y = cs.readU32LE()
val width = cs.readS32LE()
val height = cs.readS32LE()

var ninePatch: AseSlice.NinePatchInfo? = null
var pivot: AseSlice.PivotInfo? = null

if (hasNinePatch) {
val centerX = cs.readU32LE()
val centerY = cs.readU32LE()
val centerWidth = cs.readS32LE()
val centerHeight = cs.readS32LE()
ninePatch = AseSlice.NinePatchInfo(centerX.toInt(), centerY.toInt(), centerWidth, centerHeight)
}
if (hasPivotInfo) {
val pivotX = cs.readU32LE()
val pivotY = cs.readU32LE()
pivot = AseSlice.PivotInfo(pivotX.toInt(), pivotY.toInt())
}

keys += AseSlice.SliceKey(
frameNumber, x.toInt(), y.toInt(), width, height, ninePatch, pivot
)
}

val aslice = AseSlice(sliceName, hasNinePatch, hasPivotInfo, keys)
slices += aslice
}
0x2018 -> { // TAGS
Expand Down Expand Up @@ -186,9 +252,12 @@ data class ASEInfo(
}

return ASEInfo(
pixelWidth = imageWidth,
pixelHeight = imageHeight,
slices = slices,
tags = tags,
layers = layers
layers = layers,
frames = frames
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,43 @@ class AseInfoTest {
Assert.assertEquals(0, info.slices.size)
Assert.assertEquals(listOf("TestNum", "FireTrail", "FireTrail2"), info.tags.map { it.tagName })
}

@Test
fun testSliceInfo() {
val info = ASEInfo.getAseInfo(getResourceBytes("sprite-slices.ase"))
Assert.assertEquals("Expected 2 slices", 2, info.slices.size)
Assert.assertEquals("Slice 1 name wrong", "Slice 1", info.slices[0].sliceName)
Assert.assertEquals("Slice 2 name wrong","Slice 2", info.slices[1].sliceName)
Assert.assertEquals("Expected 2 keys in slice 0", 2, info.slices[0].keys.size)
Assert.assertEquals("Expected 3 keys in slice 1", 3, info.slices[1].keys.size)

Assert.assertEquals("Image pixel width wrong", 32, info.pixelWidth)
Assert.assertEquals("Image pixel height wrong", 64, info.pixelHeight)
Assert.assertEquals("Frame number of slice 0 key 0 wrong", 0, info.slices[0].keys[0].frameNumber)
Assert.assertEquals("Frame number of slice 0 key 1 wrong", 2, info.slices[0].keys[1].frameNumber)
Assert.assertEquals("Frame number of slice 1 key 0 wrong", 0, info.slices[1].keys[0].frameNumber)
Assert.assertEquals("Frame number of slice 1 key 1 wrong", 1, info.slices[1].keys[1].frameNumber)
Assert.assertEquals("Frame number of slice 1 key 2 wrong", 2, info.slices[1].keys[2].frameNumber)

Assert.assertEquals("Slice x of slice 0 key 0 wrong", 3, info.slices[0].keys[0].x)
Assert.assertEquals("Slice y of slice 0 key 0 wrong", 4, info.slices[0].keys[0].y)
Assert.assertEquals("Slice width of slice 0 key 0 wrong", 24, info.slices[0].keys[0].width)
Assert.assertEquals("Slice height of slice 0 key 0 wrong", 20, info.slices[0].keys[0].height)
Assert.assertEquals("Slice nine-patch centerX of slice 0 key 0 wrong", 4, info.slices[0].keys[0].ninePatch?.centerX)
Assert.assertEquals("Slice nine-patch centerY of slice 0 key 0 wrong", 5, info.slices[0].keys[0].ninePatch?.centerY)
Assert.assertEquals("Slice nine-patch centerWidth of slice 0 key 0 wrong", 6, info.slices[0].keys[0].ninePatch?.centerWidth)
Assert.assertEquals("Slice nine-patch centerHeight of slice 0 key 0 wrong", 7, info.slices[0].keys[0].ninePatch?.centerHeight)
Assert.assertEquals("Slice pivotX of slice 0 key 0 wrong", 12, info.slices[0].keys[0].pivot?.pivotX)
Assert.assertEquals("Slice pivotY of slice 0 key 0 wrong", 34, info.slices[0].keys[0].pivot?.pivotY)

Assert.assertEquals("Slice pivotX of slice 0 key 1 wrong", 16, info.slices[0].keys[1].pivot?.pivotX)
Assert.assertEquals("Slice pivotY of slice 0 key 1 wrong", 24, info.slices[0].keys[1].pivot?.pivotY)

Assert.assertEquals("Frame 0 index wrong", 0, info.frames[0].index)
Assert.assertEquals("Frame 0 duration wrong", 100, info.frames[0].duration)
Assert.assertEquals("Frame 1 index wrong", 1, info.frames[1].index)
Assert.assertEquals("Frame 1 duration wrong", 80, info.frames[1].duration)
Assert.assertEquals("Frame 2 index wrong", 2, info.frames[2].index)
Assert.assertEquals("Frame 2 duration wrong", 42, info.frames[2].duration)
}
}
Binary file not shown.
Loading