From b1852f743e66ca4bd517da66412f0a0c48c80733 Mon Sep 17 00:00:00 2001 From: Username404 Date: Thu, 27 May 2021 20:57:18 +0200 Subject: [PATCH] Sort buttons alphabetically --- .../kotlin/fr/username404/snowygui/Snowy.kt | 3 +- .../snowygui/gui/feature/ButtonImpl.kt | 45 +++++++++++-------- .../username404/snowygui/fabric/FabricInit.kt | 3 +- .../username404/snowygui/forge/ForgeInit.kt | 3 +- 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt b/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt index a004421..2e17773 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt @@ -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> + lateinit var annotatedButtons: Set> fun onEvent(e: String, lambda: argsLambda) = useKey(e).add(lambda) @JvmField val logs: Logger = LogManager.getLogger() diff --git a/common/src/main/kotlin/fr/username404/snowygui/gui/feature/ButtonImpl.kt b/common/src/main/kotlin/fr/username404/snowygui/gui/feature/ButtonImpl.kt index 400a82c..924025a 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/gui/feature/ButtonImpl.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/gui/feature/ButtonImpl.kt @@ -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) { + buttons.groupBy { ClickGui.components.filterIsInstance().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() ?: throw Exception("Missing @ButtonInfo annotaton") diff --git a/fabric/src/main/kotlin/fr/username404/snowygui/fabric/FabricInit.kt b/fabric/src/main/kotlin/fr/username404/snowygui/fabric/FabricInit.kt index 22176ac..7e32691 100644 --- a/fabric/src/main/kotlin/fr/username404/snowygui/fabric/FabricInit.kt +++ b/fabric/src/main/kotlin/fr/username404/snowygui/fabric/FabricInit.kt @@ -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> } } \ No newline at end of file diff --git a/forge/src/main/kotlin/fr/username404/snowygui/forge/ForgeInit.kt b/forge/src/main/kotlin/fr/username404/snowygui/forge/ForgeInit.kt index 3b25b8e..2045b46 100644 --- a/forge/src/main/kotlin/fr/username404/snowygui/forge/ForgeInit.kt +++ b/forge/src/main/kotlin/fr/username404/snowygui/forge/ForgeInit.kt @@ -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 } .collect(Collectors.toSet()) with(MOD_BUS) { addListener(this@ForgeInit::initSetup)