69 lines
1.7 KiB
Kotlin
69 lines
1.7 KiB
Kotlin
package ru.dbotthepony.kstarbound.io.json
|
|
|
|
import com.google.gson.TypeAdapter
|
|
import com.google.gson.stream.JsonReader
|
|
import com.google.gson.stream.JsonWriter
|
|
import ru.dbotthepony.kvector.util2d.AABB
|
|
import ru.dbotthepony.kvector.util2d.AABBi
|
|
import ru.dbotthepony.kvector.vector.Vector2d
|
|
import ru.dbotthepony.kvector.vector.Vector2i
|
|
|
|
object AABBTypeAdapter : TypeAdapter<AABB>() {
|
|
override fun write(out: JsonWriter, value: AABB) {
|
|
`out`.beginArray()
|
|
`out`.value(value.mins.x)
|
|
`out`.value(value.mins.y)
|
|
`out`.value(value.maxs.x)
|
|
`out`.value(value.maxs.y)
|
|
`out`.endArray()
|
|
}
|
|
|
|
override fun read(`in`: JsonReader): AABB {
|
|
`in`.beginArray()
|
|
val (x1, x2) = Vector2d(`in`.nextDouble(), `in`.nextDouble())
|
|
val (y1, y2) = Vector2d(`in`.nextDouble(), `in`.nextDouble())
|
|
`in`.endArray()
|
|
|
|
val xMins = x1.coerceAtMost(x2)
|
|
val xMaxs = x1.coerceAtLeast(x2)
|
|
|
|
val yMins = y1.coerceAtMost(y2)
|
|
val yMaxs = y1.coerceAtLeast(y2)
|
|
|
|
return AABB(
|
|
Vector2d(xMins, yMins),
|
|
Vector2d(xMaxs, yMaxs),
|
|
)
|
|
}
|
|
}
|
|
|
|
object AABBiTypeAdapter : TypeAdapter<AABBi>() {
|
|
override fun write(out: JsonWriter, value: AABBi) {
|
|
`out`.beginArray()
|
|
`out`.value(value.mins.x)
|
|
`out`.value(value.mins.y)
|
|
`out`.value(value.maxs.x)
|
|
`out`.value(value.maxs.y)
|
|
`out`.endArray()
|
|
}
|
|
|
|
override fun read(`in`: JsonReader): AABBi {
|
|
`in`.beginArray()
|
|
val (x1, x2) = Vector2i(`in`.nextInt(), `in`.nextInt())
|
|
val (y1, y2) = Vector2i(`in`.nextInt(), `in`.nextInt())
|
|
`in`.endArray()
|
|
|
|
val xMins = x1.coerceAtMost(x2)
|
|
val xMaxs = x1.coerceAtLeast(x2)
|
|
|
|
val yMins = y1.coerceAtMost(y2)
|
|
val yMaxs = y1.coerceAtLeast(y2)
|
|
|
|
return AABBi(
|
|
Vector2i(xMins, yMins),
|
|
Vector2i(xMaxs, yMaxs),
|
|
)
|
|
}
|
|
}
|
|
|