11package korlibs.korge.gradle.util
22
33data 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
0 commit comments