Sort buttons alphabetically

This commit is contained in:
Username404 2021-05-27 20:57:18 +02:00
parent 6552adbf70
commit b1852f743e
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
4 changed files with 33 additions and 21 deletions

View File

@ -4,6 +4,7 @@ import com.google.gson.Gson
import com.google.gson.GsonBuilder
import fr.username404.snowygui.EventSnowy.Companion.useKey
import fr.username404.snowygui.config.Configuration.obtained
import fr.username404.snowygui.gui.feature.ButtonImpl
import fr.username404.snowygui.gui.feature.Ignored
import fr.username404.snowygui.misc.AddKeyMaps
import io.github.config4k.getValue
@ -18,7 +19,7 @@ abstract class Snowy {
val Gson: Gson = GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()
@JvmStatic
protected val FeaturePackage: String = "fr.username404.snowygui.gui.feature"
lateinit var annotatedButtons: Set<Class<*>>
lateinit var annotatedButtons: Set<Class<ButtonImpl>>
fun onEvent(e: String, lambda: argsLambda) = useKey(e).add(lambda)
@JvmField
val logs: Logger = LogManager.getLogger()

View File

@ -17,30 +17,39 @@ sealed class ButtonImpl: ColoredElement(0.0, 0.0, 73, 8, opacity = 0.60F) {
var done: Boolean = false; private set
fun initialize() {
done = true
Snowy.annotatedButtons.forEach { // Initializes every button
fun addToButtons(buttonImpl: ButtonImpl) {
fun addToButtons(buttons: Collection<ButtonImpl>) {
buttons.groupBy {
ClickGui.components.filterIsInstance<ClickBox>().find { box ->
box.name!!.key == buttonImpl.info.parent.translationKey
}?.buttons!!.add(buttonImpl)
}
((try {
it.getConstructor().newInstance()
} catch (e: NoSuchMethodException) {
it.getDeclaredField("INSTANCE").get(null)
}) as ButtonImpl).let { impl ->
with(impl) {
if (info.kind == ButtonInfo.Companion.Type.TOGGLE) {
Configuration.enabledFeatures[title]?.let { bool ->
toggled = bool
it.let { impl ->
with(impl) {
if (info.kind == Type.TOGGLE) {
Configuration.enabledFeatures[title]?.let { bool ->
toggled = bool
}
}
}
box.name!!.key == impl.info.parent.translationKey
}
}
}.entries.forEach { entry ->
with(entry) {
entry.value.sortedBy {
it.title
}.forEach {
key?.buttons!!.add(it)
}
}
addToButtons(impl)
}
Configuration.macros.forEach { macro ->
addToButtons(macro)
}
}
addToButtons(
Snowy.annotatedButtons.map {
((try {
it.getConstructor().newInstance()
} catch (e: NoSuchMethodException) {
it.getDeclaredField("INSTANCE").get(null)
}) as ButtonImpl)
}.plus(Configuration.macros)
)
}
}
private val info = this::class.findAnnotation<ButtonInfo>() ?: throw Exception("Missing @ButtonInfo annotaton")

View File

@ -3,6 +3,7 @@ package fr.username404.snowygui.fabric
import com.mojang.blaze3d.vertex.PoseStack
import fr.username404.snowygui.EventSnowy
import fr.username404.snowygui.Snowy
import fr.username404.snowygui.gui.feature.ButtonImpl
import fr.username404.snowygui.gui.feature.ButtonInfo
import net.fabricmc.api.ClientModInitializer
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback
@ -29,6 +30,6 @@ class FabricInit: Snowy(), ClientModInitializer {
.setUrls(ClasspathHelper.forPackage(FeaturePackage))
.setScanners(SubTypesScanner(false), TypeAnnotationsScanner())
.useParallelExecutor()
).getTypesAnnotatedWith(ButtonInfo::class.java).filter { it.isValidForButtonCollection() }.toSet()
).getTypesAnnotatedWith(ButtonInfo::class.java).filter { it.isValidForButtonCollection() }.toSet() as Set<Class<ButtonImpl>>
}
}

View File

@ -2,6 +2,7 @@ package fr.username404.snowygui.forge
import fr.username404.snowygui.Snowy
import fr.username404.snowygui.config.SnowyConfigScreen
import fr.username404.snowygui.gui.feature.ButtonImpl
import fr.username404.snowygui.gui.feature.ButtonInfo
import net.minecraftforge.fml.ExtensionPoint
import net.minecraftforge.fml.ModList
@ -49,7 +50,7 @@ class ForgeInit: Snowy() {
}
}
}
.map { Class.forName((it!!.javaClass.getDeclaredField("clazz").apply { isAccessible = true }.get(it) as Type).className) }
.map { Class.forName((it!!.javaClass.getDeclaredField("clazz").apply { isAccessible = true }.get(it) as Type).className) as Class<ButtonImpl> }
.collect(Collectors.toSet())
with(MOD_BUS) {
addListener(this@ForgeInit::initSetup)