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
Expand Up @@ -7,6 +7,7 @@ import kotlin.system.*

open class KorgeTexturePacker : KorgeResourceProcessor {
override fun processFolder(context: KorgeResourceProcessorContext) {
context.resourceFolders
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this line should be removed?

Copy link

Copilot AI May 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The standalone invocation of 'context.resourceFolders' is redundant and does not contribute to the processing logic; it is recommended to remove it to avoid confusion.

Copilot uses AI. Check for mistakes.
for (folder in context.resourceFolders) {
val files = folder.listFiles()?.toList() ?: emptyList()
for (file in files) {
Expand All @@ -18,17 +19,32 @@ open class KorgeTexturePacker : KorgeResourceProcessor {
generate(context.logger, atlasJsonFile, arrayOf(file))
}
file.isFile -> {
val sources = file.readLines().filter { it.isNotBlank() }.map { File(folder, it) }.toTypedArray()
val settings = parseAtlasSettingsFile(file)
val sources = settings.files.map { File(folder, it) }.toTypedArray()
context.skipFiles(file, *sources)
generate(context.logger, atlasJsonFile, sources)
generate(
context.logger,
atlasJsonFile,
sources,
enableRotation = settings.enableRotation,
enableTrimming = settings.enableTrimming,
padding = settings.padding,
)
}
}
}
}
}
}

fun generate(logger: org.slf4j.Logger, outputFile: File, imageFolders: Array<File>) {
private fun generate(
logger: org.slf4j.Logger,
outputFile: File,
imageFolders: Array<File>,
enableRotation: Boolean = true,
enableTrimming: Boolean = true,
padding: Int = 2,
) {
val involvedFiles = NewTexturePacker.getAllFiles(*imageFolders)
//val maxLastModifiedTime = involvedFiles.maxOfOrNull { it.file.lastModified() } ?: System.currentTimeMillis()
val involvedString = involvedFiles.map { it.relative.name + ":" + it.file.length() + ":" + it.file.lastModified() }.sorted().joinToString("\n")
Expand All @@ -37,7 +53,12 @@ open class KorgeTexturePacker : KorgeResourceProcessor {
//if (!outputFile.exists() || involvedFile.takeIfExists()?.readText() != involvedString) {
if (involvedFile.takeIfExists()?.readText() != involvedString) {
val time = measureTimeMillis {
val results = NewTexturePacker.packImages(*imageFolders, enableRotation = true, enableTrimming = true)
val results = NewTexturePacker.packImages(
*imageFolders,
enableRotation = enableRotation,
enableTrimming = enableTrimming,
padding = padding
)
for (result in results) {
val imageOut = result.write(outputFile)
}
Expand All @@ -50,4 +71,63 @@ open class KorgeTexturePacker : KorgeResourceProcessor {
logger.info("KorgeTexturePacker.CACHED: $involvedFile")
}
}

private fun parseAtlasSettingsFile(atlasFile: File): AtlasGenerationSettings {
Copy link

Copilot AI May 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The atlas settings parsing in this module duplicates functionality found in the main module; consider refactoring this into a common utility to improve maintainability.

Copilot uses AI. Check for mistakes.
var enableRotation = true
var enableTrimming = true
var padding = 2
val files = mutableListOf<String>()

atlasFile.forEachLine { line ->
val trimmedLine = line.trim()
if (trimmedLine.isBlank()) return@forEachLine

val enableRotationSetting = tryExtractSetting(trimmedLine, "#enable-rotation") { it.toBoolean() }
if (enableRotationSetting != null) {
enableRotation = enableRotationSetting
return@forEachLine
}

val enableTrimmingSetting = tryExtractSetting(trimmedLine, "#enable-trimming") { it.toBoolean() }
if (enableTrimmingSetting != null) {
enableTrimming = enableTrimmingSetting
return@forEachLine
}

val paddingSetting = tryExtractSetting(trimmedLine, "#padding") { it.toInt() }
if (paddingSetting != null) {
padding = paddingSetting
return@forEachLine
}

files.add(trimmedLine)
}

return AtlasGenerationSettings(
files = files,
enableRotation = enableRotation,
enableTrimming = enableTrimming,
padding = padding,
)
}

private inline fun <T> tryExtractSetting(line: String, key: String, valueParser: (String) -> T): T? {
return if (line.startsWith(key)) {
try {
val value = line.substringAfter("=").trim()
valueParser(value)
} catch (e: Exception) {
null
}
} else {
null
}
}
}

private data class AtlasGenerationSettings(
val files: List<String>,
val enableRotation: Boolean,
val enableTrimming: Boolean,
val padding: Int,
)
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ object NewTexturePacker {
vararg folders: File,
enableRotation: Boolean = true,
enableTrimming: Boolean = true,
padding: Int = 2,
): List<AtlasInfo> {
val images: List<Pair<File, SimpleBitmap>> = getAllFiles(*folders).mapNotNull {
try {
Expand All @@ -54,16 +55,14 @@ object NewTexturePacker {
}
}

val PADDING = 2

val packer = NewBinPacker.MaxRectsPacker(4096, 4096, PADDING * 2, NewBinPacker.IOption(
val packer = NewBinPacker.MaxRectsPacker(4096, 4096, padding * 2, NewBinPacker.IOption(
smart = true,
pot = true,
square = false,
allowRotation = enableRotation,
//allowRotation = false,
tag = false,
border = PADDING
border = padding
))

packer.addArray(images.map { (file, image) ->
Expand Down Expand Up @@ -94,7 +93,7 @@ object NewTexturePacker {
//println("$rect :: info=$info")

val chunk = if (rect.rot) info.trimmedImage.flipY().rotate90() else info.trimmedImage
out.put(rect.x - PADDING, rect.y - PADDING, chunk.extrude(PADDING))
out.put(rect.x - padding, rect.y - padding, chunk.extrude(padding))
//out.put(rect.x, rect.y, chunk)

val obj = LinkedHashMap<String, Any?>()
Expand Down Expand Up @@ -139,3 +138,4 @@ object NewTexturePacker {
return outAtlases
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import kotlin.system.*

open class KorgeTexturePacker : KorgeResourceProcessor {
override fun processFolder(context: KorgeResourceProcessorContext) {
context.resourceFolders
Copy link

Copilot AI May 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of 'context.resourceFolders' as a standalone expression does not affect the control flow and appears to be a no-op; consider removing it for clarity.

Suggested change
context.resourceFolders

Copilot uses AI. Check for mistakes.
for (folder in context.resourceFolders) {
val files = folder.listFiles()?.toList() ?: emptyList()
for (file in files) {
Expand All @@ -18,17 +19,32 @@ open class KorgeTexturePacker : KorgeResourceProcessor {
generate(context.logger, atlasJsonFile, arrayOf(file))
}
file.isFile -> {
val sources = file.readLines().filter { it.isNotBlank() }.map { File(folder, it) }.toTypedArray()
val settings = parseAtlasSettingsFile(file)
val sources = settings.files.map { File(folder, it) }.toTypedArray()
context.skipFiles(file, *sources)
generate(context.logger, atlasJsonFile, sources)
generate(
context.logger,
atlasJsonFile,
sources,
enableRotation = settings.enableRotation,
enableTrimming = settings.enableTrimming,
padding = settings.padding,
)
}
}
}
}
}
}

fun generate(logger: org.slf4j.Logger, outputFile: File, imageFolders: Array<File>) {
private fun generate(
logger: org.slf4j.Logger,
outputFile: File,
imageFolders: Array<File>,
enableRotation: Boolean = true,
enableTrimming: Boolean = true,
padding: Int = 2,
) {
val involvedFiles = NewTexturePacker.getAllFiles(*imageFolders)
//val maxLastModifiedTime = involvedFiles.maxOfOrNull { it.file.lastModified() } ?: System.currentTimeMillis()
val involvedString = involvedFiles.map { it.relative.name + ":" + it.file.length() + ":" + it.file.lastModified() }.sorted().joinToString("\n")
Expand All @@ -37,7 +53,12 @@ open class KorgeTexturePacker : KorgeResourceProcessor {
//if (!outputFile.exists() || involvedFile.takeIfExists()?.readText() != involvedString) {
if (involvedFile.takeIfExists()?.readText() != involvedString) {
val time = measureTimeMillis {
val results = NewTexturePacker.packImages(*imageFolders, enableRotation = true, enableTrimming = true)
val results = NewTexturePacker.packImages(
*imageFolders,
enableRotation = enableRotation,
enableTrimming = enableTrimming,
padding = padding
)
for (result in results) {
val imageOut = result.write(outputFile)
}
Expand All @@ -50,4 +71,63 @@ open class KorgeTexturePacker : KorgeResourceProcessor {
logger.info("KorgeTexturePacker.CACHED: $involvedFile")
}
}

private fun parseAtlasSettingsFile(atlasFile: File): AtlasGenerationSettings {
Copy link

Copilot AI May 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since similar atlas settings parsing logic exists in both the main and buildSrc processor files, consider extracting this logic into a shared utility to reduce duplication and ease future maintenance.

Copilot uses AI. Check for mistakes.
var enableRotation = true
var enableTrimming = true
var padding = 2
val files = mutableListOf<String>()

atlasFile.forEachLine { line ->
val trimmedLine = line.trim()
if (trimmedLine.isBlank()) return@forEachLine

val enableRotationSetting = tryExtractSetting(trimmedLine, "#enable-rotation") { it.toBoolean() }
if (enableRotationSetting != null) {
enableRotation = enableRotationSetting
return@forEachLine
}

val enableTrimmingSetting = tryExtractSetting(trimmedLine, "#enable-trimming") { it.toBoolean() }
if (enableTrimmingSetting != null) {
enableTrimming = enableTrimmingSetting
return@forEachLine
}

val paddingSetting = tryExtractSetting(trimmedLine, "#padding") { it.toInt() }
if (paddingSetting != null) {
padding = paddingSetting
return@forEachLine
}

files.add(trimmedLine)
}

return AtlasGenerationSettings(
files = files,
enableRotation = enableRotation,
enableTrimming = enableTrimming,
padding = padding,
)
}

private inline fun <T> tryExtractSetting(line: String, key: String, valueParser: (String) -> T): T? {
return if (line.startsWith(key)) {
try {
val value = line.substringAfter("=").trim()
valueParser(value)
} catch (e: Exception) {
null
}
} else {
null
}
}
}

private data class AtlasGenerationSettings(
val files: List<String>,
val enableRotation: Boolean,
val enableTrimming: Boolean,
val padding: Int,
)
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ object NewTexturePacker {
vararg folders: File,
enableRotation: Boolean = true,
enableTrimming: Boolean = true,
padding: Int = 2,
): List<AtlasInfo> {
val images: List<Pair<File, SimpleBitmap>> = getAllFiles(*folders).mapNotNull {
try {
Expand All @@ -54,16 +55,14 @@ object NewTexturePacker {
}
}

val PADDING = 2

val packer = NewBinPacker.MaxRectsPacker(4096, 4096, PADDING * 2, NewBinPacker.IOption(
val packer = NewBinPacker.MaxRectsPacker(4096, 4096, padding * 2, NewBinPacker.IOption(
smart = true,
pot = true,
square = false,
allowRotation = enableRotation,
//allowRotation = false,
tag = false,
border = PADDING
border = padding
))

packer.addArray(images.map { (file, image) ->
Expand Down Expand Up @@ -94,7 +93,7 @@ object NewTexturePacker {
//println("$rect :: info=$info")

val chunk = if (rect.rot) info.trimmedImage.flipY().rotate90() else info.trimmedImage
out.put(rect.x - PADDING, rect.y - PADDING, chunk.extrude(PADDING))
out.put(rect.x - padding, rect.y - padding, chunk.extrude(padding))
//out.put(rect.x, rect.y, chunk)

val obj = LinkedHashMap<String, Any?>()
Expand Down
Loading