Skip to content

Commit a8f1326

Browse files
committed
Add nine-patch and pivot slice info and update unit test
1 parent fa6f762 commit a8f1326

File tree

3 files changed

+111
-3
lines changed

3 files changed

+111
-3
lines changed

korge-gradle-plugin/src/main/kotlin/korlibs/korge/gradle/util/ASEInfo.kt

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,43 @@
11
package korlibs.korge.gradle.util
22

33
data class ASEInfo(
4+
val pixelWidth: Int = 0,
5+
val pixelHeight: Int = 0,
46
val slices: List<AseSlice> = emptyList(),
57
val tags: List<AseTag> = emptyList(),
68
val layers: List<AseLayer> = emptyList(),
9+
val frames: List<Frame> = emptyList(),
710
) {
11+
12+
val tagsByName: Map<String, AseTag> by lazy { tags.associateBy { it.tagName } }
13+
val layersByName: Map<String, AseLayer> by lazy { layers.associateBy { it.layerName } }
14+
815
data class AseSlice(
916
val sliceName: String,
1017
val hasNinePatch: Boolean,
1118
val hasPivotInfo: Boolean,
12-
)
19+
val keys: List<SliceKey>
20+
) {
21+
data class SliceKey(
22+
val frameNumber: Int,
23+
val x: Int,
24+
val y: Int,
25+
val width: Int,
26+
val height: Int,
27+
val ninePatch: NinePatchInfo? = null,
28+
val pivot: PivotInfo? = null
29+
)
30+
data class NinePatchInfo(
31+
val centerX: Int,
32+
val centerY: Int,
33+
val centerWidth: Int,
34+
val centerHeight: Int
35+
)
36+
data class PivotInfo(
37+
val pivotX: Int,
38+
val pivotY: Int
39+
)
40+
}
1341

1442
data class AseTag(
1543
val fromFrame: Int,
@@ -65,6 +93,11 @@ data class ASEInfo(
6593
}
6694
}
6795

96+
data class Frame(
97+
val index: Int,
98+
val duration: Int
99+
)
100+
68101
companion object {
69102
fun getAseInfo(file: SFile): ASEInfo {
70103
return getAseInfo(file.readBytes())
@@ -80,6 +113,7 @@ data class ASEInfo(
80113
val slices = arrayListOf<AseSlice>()
81114
val tags = arrayListOf<AseTag>()
82115
val layers = arrayListOf<AseLayer>()
116+
val frames = arrayListOf<Frame>()
83117

84118
val fileSize = s.readS32LE()
85119
if (s.length < fileSize) error("File too short s.length=${s.length} < fileSize=${fileSize}")
@@ -118,6 +152,7 @@ data class ASEInfo(
118152
val frameDuration = fs.readU16LE()
119153
fs.skip(2)
120154
val numChunks = fs.readS32LE()
155+
frames += Frame(frameIndex, frameDuration)
121156

122157
//println(" - $numChunks")
123158

@@ -156,7 +191,38 @@ data class ASEInfo(
156191
val sliceName = cs.readAseString()
157192
val hasNinePatch = sliceFlags.hasBitSet(0)
158193
val hasPivotInfo = sliceFlags.hasBitSet(1)
159-
val aslice = AseSlice(sliceName, hasNinePatch, hasPivotInfo)
194+
195+
// Read 9-patch and pivot info
196+
val keys = mutableListOf<AseSlice.SliceKey>()
197+
for (key in 0 until numSliceKeys) {
198+
val frameNumber = cs.readS32LE()
199+
val x = cs.readU32LE()
200+
val y = cs.readU32LE()
201+
val width = cs.readS32LE()
202+
val height = cs.readS32LE()
203+
204+
var ninePatch: AseSlice.NinePatchInfo? = null
205+
var pivot: AseSlice.PivotInfo? = null
206+
207+
if (hasNinePatch) {
208+
val centerX = cs.readU32LE()
209+
val centerY = cs.readU32LE()
210+
val centerWidth = cs.readS32LE()
211+
val centerHeight = cs.readS32LE()
212+
ninePatch = AseSlice.NinePatchInfo(centerX.toInt(), centerY.toInt(), centerWidth, centerHeight)
213+
}
214+
if (hasPivotInfo) {
215+
val pivotX = cs.readU32LE()
216+
val pivotY = cs.readU32LE()
217+
pivot = AseSlice.PivotInfo(pivotX.toInt(), pivotY.toInt())
218+
}
219+
220+
keys += AseSlice.SliceKey(
221+
frameNumber, x.toInt(), y.toInt(), width, height, ninePatch, pivot
222+
)
223+
}
224+
225+
val aslice = AseSlice(sliceName, hasNinePatch, hasPivotInfo, keys)
160226
slices += aslice
161227
}
162228
0x2018 -> { // TAGS
@@ -186,9 +252,12 @@ data class ASEInfo(
186252
}
187253

188254
return ASEInfo(
255+
pixelWidth = imageWidth,
256+
pixelHeight = imageHeight,
189257
slices = slices,
190258
tags = tags,
191-
layers = layers
259+
layers = layers,
260+
frames = frames
192261
)
193262
}
194263

korge-gradle-plugin/src/test/kotlin/korlibs/korge/gradle/util/AseInfoTest.kt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,43 @@ class AseInfoTest {
1111
Assert.assertEquals(0, info.slices.size)
1212
Assert.assertEquals(listOf("TestNum", "FireTrail", "FireTrail2"), info.tags.map { it.tagName })
1313
}
14+
15+
@Test
16+
fun testSliceInfo() {
17+
val info = ASEInfo.getAseInfo(getResourceBytes("sprite-slices.ase"))
18+
Assert.assertEquals("Expected 2 slices", 2, info.slices.size)
19+
Assert.assertEquals("Slice 1 name wrong", "Slice 1", info.slices[0].sliceName)
20+
Assert.assertEquals("Slice 2 name wrong","Slice 2", info.slices[1].sliceName)
21+
Assert.assertEquals("Expected 2 keys in slice 0", 2, info.slices[0].keys.size)
22+
Assert.assertEquals("Expected 3 keys in slice 1", 3, info.slices[1].keys.size)
23+
24+
Assert.assertEquals("Image pixel width wrong", 32, info.pixelWidth)
25+
Assert.assertEquals("Image pixel height wrong", 64, info.pixelHeight)
26+
Assert.assertEquals("Frame number of slice 0 key 0 wrong", 0, info.slices[0].keys[0].frameNumber)
27+
Assert.assertEquals("Frame number of slice 0 key 1 wrong", 2, info.slices[0].keys[1].frameNumber)
28+
Assert.assertEquals("Frame number of slice 1 key 0 wrong", 0, info.slices[1].keys[0].frameNumber)
29+
Assert.assertEquals("Frame number of slice 1 key 1 wrong", 1, info.slices[1].keys[1].frameNumber)
30+
Assert.assertEquals("Frame number of slice 1 key 2 wrong", 2, info.slices[1].keys[2].frameNumber)
31+
32+
Assert.assertEquals("Slice x of slice 0 key 0 wrong", 3, info.slices[0].keys[0].x)
33+
Assert.assertEquals("Slice y of slice 0 key 0 wrong", 4, info.slices[0].keys[0].y)
34+
Assert.assertEquals("Slice width of slice 0 key 0 wrong", 24, info.slices[0].keys[0].width)
35+
Assert.assertEquals("Slice height of slice 0 key 0 wrong", 20, info.slices[0].keys[0].height)
36+
Assert.assertEquals("Slice nine-patch centerX of slice 0 key 0 wrong", 4, info.slices[0].keys[0].ninePatch?.centerX)
37+
Assert.assertEquals("Slice nine-patch centerY of slice 0 key 0 wrong", 5, info.slices[0].keys[0].ninePatch?.centerY)
38+
Assert.assertEquals("Slice nine-patch centerWidth of slice 0 key 0 wrong", 6, info.slices[0].keys[0].ninePatch?.centerWidth)
39+
Assert.assertEquals("Slice nine-patch centerHeight of slice 0 key 0 wrong", 7, info.slices[0].keys[0].ninePatch?.centerHeight)
40+
Assert.assertEquals("Slice pivotX of slice 0 key 0 wrong", 12, info.slices[0].keys[0].pivot?.pivotX)
41+
Assert.assertEquals("Slice pivotY of slice 0 key 0 wrong", 34, info.slices[0].keys[0].pivot?.pivotY)
42+
43+
Assert.assertEquals("Slice pivotX of slice 0 key 1 wrong", 16, info.slices[0].keys[1].pivot?.pivotX)
44+
Assert.assertEquals("Slice pivotY of slice 0 key 1 wrong", 24, info.slices[0].keys[1].pivot?.pivotY)
45+
46+
Assert.assertEquals("Frame 0 index wrong", 0, info.frames[0].index)
47+
Assert.assertEquals("Frame 0 duration wrong", 100, info.frames[0].duration)
48+
Assert.assertEquals("Frame 1 index wrong", 1, info.frames[1].index)
49+
Assert.assertEquals("Frame 1 duration wrong", 80, info.frames[1].duration)
50+
Assert.assertEquals("Frame 2 index wrong", 2, info.frames[2].index)
51+
Assert.assertEquals("Frame 2 duration wrong", 42, info.frames[2].duration)
52+
}
1453
}
964 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)