From 5bfcf23fbf62709e03eb65c6a10e6ea10abf4793 Mon Sep 17 00:00:00 2001 From: soywiz Date: Thu, 4 Mar 2021 11:24:59 +0100 Subject: [PATCH] Sync from next --- gradle.properties | 4 +-- .../kotlin/com/soywiz/korim/atlas/Atlas.kt | 6 ++++- .../com/soywiz/korim/bitmap/BitmapSlice.kt | 25 ++++++++++++++++--- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/gradle.properties b/gradle.properties index 222ed16..17a465f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,8 +9,8 @@ group=com.soywiz.korlibs.korim version=2.0.0-SNAPSHOT # kotlinx -korioVersion=2.0.8 -kormaVersion=2.0.7 +korioVersion=2.0.9 +kormaVersion=2.0.8 # bintray location project.bintray.org=korlibs diff --git a/korim/src/commonMain/kotlin/com/soywiz/korim/atlas/Atlas.kt b/korim/src/commonMain/kotlin/com/soywiz/korim/atlas/Atlas.kt index 605c6d7..6420740 100644 --- a/korim/src/commonMain/kotlin/com/soywiz/korim/atlas/Atlas.kt +++ b/korim/src/commonMain/kotlin/com/soywiz/korim/atlas/Atlas.kt @@ -3,6 +3,7 @@ package com.soywiz.korim.atlas import com.soywiz.korim.bitmap.* import com.soywiz.korim.format.* import com.soywiz.korio.file.* +import com.soywiz.korma.geom.RectangleInt class Atlas(val textures: Map>, val info: AtlasInfo = AtlasInfo()) { constructor(texture: BitmapSlice, info: AtlasInfo = AtlasInfo()) : this(mapOf(info.pages.first().fileName to texture), info) @@ -14,7 +15,10 @@ class Atlas(val textures: Map>, val info: AtlasInfo val texture = textures[page.fileName] ?: error("Can't find '${page.fileName}' in ${textures.keys}") val slice = texture.slice(info.frame.rect).let { - BitmapSlice(it.bmpBase, it.bounds, info.name, info.rotated) + // Define virtual frame with offsets "info.spriteSourceSize.x" and "info.spriteSourceSize.y" + // and original texture size "info.sourceSize.width" and "info.sourceSize.height" + BitmapSlice(it.bmpBase, it.bounds, info.name, info.rotated, + virtFrame = if (info.trimmed) RectangleInt(info.spriteSourceSize.x, info.spriteSourceSize.y, info.sourceSize.width, info.sourceSize.height) else null) } val name get() = info.name // @TODO: Use name instead diff --git a/korim/src/commonMain/kotlin/com/soywiz/korim/bitmap/BitmapSlice.kt b/korim/src/commonMain/kotlin/com/soywiz/korim/bitmap/BitmapSlice.kt index c5e27da..a2fce89 100644 --- a/korim/src/commonMain/kotlin/com/soywiz/korim/bitmap/BitmapSlice.kt +++ b/korim/src/commonMain/kotlin/com/soywiz/korim/bitmap/BitmapSlice.kt @@ -5,11 +5,18 @@ import com.soywiz.kmem.* import com.soywiz.korio.resources.* import com.soywiz.korma.geom.* +/** + * @property virtFrame This defines a virtual frame [RectangleInt] which surrounds the bounds [RectangleInt] of the [Bitmap]. + * It is used in a trimmed texture atlas to specify the original size of a single texture. + * X and y of virtFrame is the offset of the virtual frame to the top left edge of + * the bounds rectangle. Width and height defines the size of the virtual frame. + */ abstract class BmpSlice( val bmpBase: Bitmap, val bounds: RectangleInt, val name: String? = null, - val rotated: Boolean = false + val rotated: Boolean = false, + val virtFrame: RectangleInt? = null ) : Extra, Resourceable { override fun getOrNull() = this override suspend fun get() = this @@ -37,6 +44,12 @@ abstract class BmpSlice( val right get() = bounds.right val bottom get() = bounds.bottom + val trimmed: Boolean = virtFrame != null + val frameOffsetX: Int = virtFrame?.x ?: 0 + val frameOffsetY: Int = virtFrame?.y ?: 0 + val frameWidth: Int = virtFrame?.width ?: bounds.width + val frameHeight : Int = virtFrame?.height ?: bounds.height + var parent: Any? = null val tl_x = p0.x.toFloat() @@ -61,7 +74,13 @@ fun BmpSlice.getIntBounds(out: RectangleInt = RectangleInt()) = out.setTo(left, fun BmpSlice.extract(): Bitmap = bmpBase.extract(left, top, width, height) -class BitmapSlice(override val bmp: T, bounds: RectangleInt, name: String? = null, rotated: Boolean = false) : BmpSlice(bmp, bounds, name, rotated), Extra by Extra.Mixin() { +class BitmapSlice( + override val bmp: T, + bounds: RectangleInt, + name: String? = null, + rotated: Boolean = false, + virtFrame: RectangleInt? = null +) : BmpSlice(bmp, bounds, name, rotated, virtFrame), Extra by Extra.Mixin() { val premultiplied get() = bmp.premultiplied fun extract(): T = bmp.extract(bounds.x, bounds.y, bounds.width, bounds.height) @@ -86,7 +105,7 @@ class BitmapSlice(override val bmp: T, bounds: RectangleInt, nam } } - fun withName(name: String? = null) = BitmapSlice(bmp, bounds, name, rotated) + fun withName(name: String? = null) = BitmapSlice(bmp, bounds, name, rotated, virtFrame) override fun toString(): String = "BitmapSlice($name:${SizeInt(bounds.width, bounds.height)})" }