-
Notifications
You must be signed in to change notification settings - Fork 137
Configurable atlas textures packing #2328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ import kotlin.system.* | |
|
|
||
| open class KorgeTexturePacker : KorgeResourceProcessor { | ||
| override fun processFolder(context: KorgeResourceProcessorContext) { | ||
| context.resourceFolders | ||
|
||
| for (folder in context.resourceFolders) { | ||
| val files = folder.listFiles()?.toList() ?: emptyList() | ||
| for (file in files) { | ||
|
|
@@ -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") | ||
|
|
@@ -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) | ||
| } | ||
|
|
@@ -50,4 +71,63 @@ open class KorgeTexturePacker : KorgeResourceProcessor { | |
| logger.info("KorgeTexturePacker.CACHED: $involvedFile") | ||
| } | ||
| } | ||
|
|
||
| private fun parseAtlasSettingsFile(atlasFile: File): AtlasGenerationSettings { | ||
|
||
| 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 | ||
|---|---|---|---|---|
|
|
@@ -7,6 +7,7 @@ import kotlin.system.* | |||
|
|
||||
| open class KorgeTexturePacker : KorgeResourceProcessor { | ||||
| override fun processFolder(context: KorgeResourceProcessorContext) { | ||||
| context.resourceFolders | ||||
|
||||
| context.resourceFolders |
Copilot
AI
May 15, 2025
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?