aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/gui/config/ChoiceHandler.kt
blob: 25e885a2038d142841373eb1dca9be2c69d46862 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package moe.nea.firmament.gui.config

import io.github.notenoughupdates.moulconfig.gui.HorizontalAlign
import io.github.notenoughupdates.moulconfig.gui.VerticalAlign
import io.github.notenoughupdates.moulconfig.gui.component.AlignComponent
import io.github.notenoughupdates.moulconfig.gui.component.RowComponent
import io.github.notenoughupdates.moulconfig.gui.component.TextComponent
import kotlinx.serialization.json.JsonElement
import kotlin.jvm.optionals.getOrNull
import net.minecraft.util.StringIdentifiable
import moe.nea.firmament.gui.CheckboxComponent
import moe.nea.firmament.util.ErrorUtil
import moe.nea.firmament.util.json.KJsonOps

class ChoiceHandler<E>(
	val universe: List<E>,
) : ManagedConfig.OptionHandler<E> where E : Enum<E>, E : StringIdentifiable {
	val codec = StringIdentifiable.createCodec {
		@Suppress("UNCHECKED_CAST", "PLATFORM_CLASS_MAPPED_TO_KOTLIN")
		(universe as java.util.List<*>).toArray(arrayOfNulls<Enum<E>>(0)) as Array<E>
	}
	val renderer = EnumRenderer.default<E>()

	override fun toJson(element: E): JsonElement? {
		return codec.encodeStart(KJsonOps.INSTANCE, element)
			.promotePartial { ErrorUtil.softError("Failed to encode json element '$element': $it") }.result()
			.getOrNull()
	}

	override fun fromJson(element: JsonElement): E {
		return codec.decode(KJsonOps.INSTANCE, element)
			.promotePartial { ErrorUtil.softError("Failed to decode json element '$element': $it") }
			.result()
			.get()
			.first
	}

	override fun emitGuiElements(opt: ManagedOption<E>, guiAppender: GuiAppender) {
		guiAppender.appendFullRow(TextComponent(opt.labelText.string))
		for (e in universe) {
			guiAppender.appendFullRow(RowComponent(
				AlignComponent(CheckboxComponent(opt, e), { HorizontalAlign.LEFT }, { VerticalAlign.CENTER }),
				TextComponent(renderer.getName(opt, e).string)
			))
		}
	}
}