Skip to content

Commit

Permalink
Auto-generated R object containing sprites (for now, just them); new …
Browse files Browse the repository at this point in the history
…main menu icon.
  • Loading branch information
Mnemotechnician committed Oct 26, 2023
1 parent 2d0f04f commit dce8d08
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions minchat-client/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.io.OutputStream
import java.security.MessageDigest
import java.util.*
Expand All @@ -12,6 +13,13 @@ val jarName = "minchat-client"
val mindustryVersion: String by rootProject
val ktorVersion: String by rootProject

val spritesFolder = File("$projectDir/assets/sprites")
val generatedCodeFolder = File("$buildDir/generated/")

sourceSets.main {
kotlin.srcDir(generatedCodeFolder)
}

dependencies {
implementation(project(":minchat-common"))
implementation(project(":minchat-rest"))
Expand Down Expand Up @@ -198,10 +206,59 @@ task<Jar>("release") {
}
}

val generateResources by tasks.registering {
val outputFile = generatedCodeFolder.resolve("R.kt")

inputs.dir(spritesFolder)
outputs.file(outputFile)

doLast {
val sprites = spritesFolder.walkTopDown()
.filter { it.isFile }
.filter { it.extension == "png" }
.map { it.nameWithoutExtension }
.map { name ->
// Trasnform kebab-case and snake_case into camelCase
buildString {
var isCapital = false
name.forEach {
if (it == '-' || it == '_' || it == ' ') {
isCapital = true
} else {
append(if (isCapital) it.uppercase() else it.lowercase())
isCapital = false
}
}
} to "minchat-$name"
}

val result = """
|package io.minchat.client
|
|import arc.Core
|import arc.scene.style.TextureRegionDrawable
|
|object R {
| ${sprites.joinToString("\n|\t") {
"val ${it.first} by lazy { Core.atlas.find(\"${it.second}\", \"error\").let(::TextureRegionDrawable) }"
}}
|}
""".trimMargin()

outputFile.writeText(result)
}
}

tasks.withType<KotlinCompile> {
dependsOn(generateResources)
}

tasks.jar {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
archiveFileName.set("${jarName}.jar")

dependsOn(generateResources)

from(rootDir) {
include("mod.hjson")
include("icon.png")
Expand Down
7 changes: 5 additions & 2 deletions minchat-client/src/main/kotlin/io/minchat/client/Minchat.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.minchat.client

import arc.Core
import arc.Events
import arc.scene.ui.Label
import com.github.mnemotechnician.mkui.delegates.setting
Expand All @@ -10,6 +11,7 @@ import com.github.mnemotechnician.mkui.extensions.runUi
import io.minchat.client.config.*
import io.minchat.client.misc.*
import io.minchat.client.plugin.MinchatPluginHandler
import io.minchat.client.ui.*
import io.minchat.client.ui.chat.ChatFragment
import io.minchat.client.ui.managers.GuiChatButtonManager
import io.minchat.common.MINCHAT_VERSION
Expand All @@ -23,6 +25,7 @@ import mindustry.mod.Mod
import mindustry.ui.Styles
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import io.minchat.client.R

private var minchatInstance: MinchatMod? = null
/** The only instance of this mod. */
Expand Down Expand Up @@ -94,7 +97,7 @@ class MinchatMod : Mod(), CoroutineScope {
}

Events.on(EventType.ClientLoadEvent::class.java) {
Vars.ui.menufrag.addButton("MinChat", Icon.terminal) {
Vars.ui.menufrag.addButton("MinChat", R.chat) {
showChatDialog()
}

Expand Down Expand Up @@ -131,7 +134,7 @@ class MinchatMod : Mod(), CoroutineScope {
}.marginBottom(60f).row()

check("don't show again") {
dontShowInfoAgain = true
dontShowInfoAgain = it
}.row()
}.show()
}
Expand Down

0 comments on commit dce8d08

Please sign in to comment.