Bulk commit
This commit is contained in:
19
src/main/kotlin/moe/nea/firmament/gui/WSpacer.kt
Normal file
19
src/main/kotlin/moe/nea/firmament/gui/WSpacer.kt
Normal file
@@ -0,0 +1,19 @@
|
||||
package moe.nea.firmament.gui
|
||||
|
||||
import io.github.cottonmc.cotton.gui.widget.WPanel
|
||||
import io.github.cottonmc.cotton.gui.widget.WWidget
|
||||
|
||||
class WSpacer(val child: WWidget, val spaceLeft: Int) : WPanel() {
|
||||
init {
|
||||
children.add(child)
|
||||
child.setLocation(spaceLeft, 0)
|
||||
}
|
||||
|
||||
override fun getWidth(): Int {
|
||||
return child.width + spaceLeft
|
||||
}
|
||||
|
||||
override fun getHeight(): Int {
|
||||
return child.height
|
||||
}
|
||||
}
|
||||
@@ -18,14 +18,11 @@
|
||||
|
||||
package moe.nea.firmament.gui.config
|
||||
|
||||
import io.github.cottonmc.cotton.gui.widget.WLabel
|
||||
import io.github.cottonmc.cotton.gui.widget.WToggleButton
|
||||
import io.github.cottonmc.cotton.gui.widget.data.VerticalAlignment
|
||||
import kotlinx.serialization.json.JsonElement
|
||||
import kotlinx.serialization.json.JsonPrimitive
|
||||
import kotlinx.serialization.json.boolean
|
||||
import kotlinx.serialization.json.jsonPrimitive
|
||||
import net.minecraft.text.Text
|
||||
|
||||
class BooleanHandler(val config: ManagedConfig) : ManagedConfig.OptionHandler<Boolean> {
|
||||
override fun toJson(element: Boolean): JsonElement? {
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package moe.nea.firmament.gui.config
|
||||
|
||||
import io.github.cottonmc.cotton.gui.widget.WLabel
|
||||
import io.github.cottonmc.cotton.gui.widget.WSlider
|
||||
import io.github.cottonmc.cotton.gui.widget.data.Axis
|
||||
import io.github.cottonmc.cotton.gui.widget.data.VerticalAlignment
|
||||
import java.util.function.IntConsumer
|
||||
import kotlinx.serialization.json.JsonElement
|
||||
import kotlinx.serialization.json.JsonPrimitive
|
||||
import kotlinx.serialization.json.jsonPrimitive
|
||||
import kotlinx.serialization.json.long
|
||||
import kotlin.time.Duration
|
||||
import kotlin.time.Duration.Companion.milliseconds
|
||||
import kotlin.time.DurationUnit
|
||||
import kotlin.time.toDuration
|
||||
import net.minecraft.text.Text
|
||||
import moe.nea.firmament.util.FirmFormatters
|
||||
|
||||
class DurationHandler(val config: ManagedConfig, val min: Duration, val max: Duration) :
|
||||
ManagedConfig.OptionHandler<Duration> {
|
||||
override fun toJson(element: Duration): JsonElement? {
|
||||
return JsonPrimitive(element.inWholeMilliseconds)
|
||||
}
|
||||
|
||||
override fun fromJson(element: JsonElement): Duration {
|
||||
return element.jsonPrimitive.long.toDuration(DurationUnit.MILLISECONDS)
|
||||
}
|
||||
|
||||
override fun emitGuiElements(opt: ManagedConfig.Option<Duration>, guiAppender: GuiAppender) {
|
||||
val lw = guiAppender.width / 2
|
||||
guiAppender.set(
|
||||
0, 0, lw, 1,
|
||||
WLabel(opt.labelText).setVerticalAlignment(VerticalAlignment.CENTER)
|
||||
)
|
||||
val label =
|
||||
WLabel(Text.literal(FirmFormatters.formatTimespan(opt.value))).setVerticalAlignment(VerticalAlignment.CENTER)
|
||||
guiAppender.set(lw, 0, 2, 1, label)
|
||||
guiAppender.set(
|
||||
lw + 2,
|
||||
0,
|
||||
lw - 2,
|
||||
1,
|
||||
WSlider(min.inWholeMilliseconds.toInt(), max.inWholeMilliseconds.toInt(), Axis.HORIZONTAL).apply {
|
||||
valueChangeListener = IntConsumer {
|
||||
opt.value = it.milliseconds
|
||||
label.text = Text.literal(FirmFormatters.formatTimespan(opt.value))
|
||||
config.save()
|
||||
}
|
||||
guiAppender.onReload {
|
||||
value = opt.value.inWholeMilliseconds.toInt()
|
||||
label.text = Text.literal(FirmFormatters.formatTimespan(opt.value))
|
||||
}
|
||||
})
|
||||
guiAppender.skipRows(1)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -29,7 +29,7 @@ class GuiAppender(val width: Int) {
|
||||
internal val panel = WGridPanel().also { it.setGaps(4, 4) }
|
||||
internal val reloadables = mutableListOf<(() -> Unit)>()
|
||||
fun set(x: Int, y: Int, w: Int, h: Int, widget: WWidget) {
|
||||
panel.add(widget, x, y, w, h)
|
||||
panel.add(widget, x, y + row, w, h)
|
||||
}
|
||||
|
||||
|
||||
@@ -50,13 +50,13 @@ class GuiAppender(val width: Int) {
|
||||
|
||||
fun appendSplitRow(left: WWidget, right: WWidget) {
|
||||
val lw = width / 2
|
||||
set(0, row, lw, 1, left)
|
||||
set(lw, row, width - lw, 1, right)
|
||||
set(0, 0, lw, 1, left)
|
||||
set(lw, 0, width - lw, 1, right)
|
||||
skipRows(1)
|
||||
}
|
||||
|
||||
fun appendFullRow(widget: WWidget) {
|
||||
set(0, row, width, 1, widget)
|
||||
set(0, 0, width, 1, widget)
|
||||
skipRows(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package moe.nea.firmament.gui.config
|
||||
|
||||
import io.github.cottonmc.cotton.gui.widget.WLabel
|
||||
import io.github.cottonmc.cotton.gui.widget.WSlider
|
||||
import io.github.cottonmc.cotton.gui.widget.data.Axis
|
||||
import io.github.cottonmc.cotton.gui.widget.data.VerticalAlignment
|
||||
import java.util.function.IntConsumer
|
||||
import kotlinx.serialization.json.JsonElement
|
||||
import kotlinx.serialization.json.JsonPrimitive
|
||||
import kotlinx.serialization.json.int
|
||||
import kotlinx.serialization.json.jsonPrimitive
|
||||
import net.minecraft.text.Text
|
||||
|
||||
class IntegerHandler(val config: ManagedConfig, val min: Int, val max: Int) : ManagedConfig.OptionHandler<Int> {
|
||||
override fun toJson(element: Int): JsonElement? {
|
||||
return JsonPrimitive(element)
|
||||
}
|
||||
|
||||
override fun fromJson(element: JsonElement): Int {
|
||||
return element.jsonPrimitive.int
|
||||
}
|
||||
|
||||
override fun emitGuiElements(opt: ManagedConfig.Option<Int>, guiAppender: GuiAppender) {
|
||||
val lw = guiAppender.width / 2
|
||||
guiAppender.set(
|
||||
0, 0, lw, 1,
|
||||
WLabel(opt.labelText).setVerticalAlignment(VerticalAlignment.CENTER)
|
||||
)
|
||||
val label =
|
||||
WLabel(Text.literal(opt.value.toString())).setVerticalAlignment(VerticalAlignment.CENTER)
|
||||
guiAppender.set(lw, 0, 2, 1, label)
|
||||
guiAppender.set(
|
||||
lw + 2,
|
||||
0,
|
||||
lw - 2,
|
||||
1,
|
||||
WSlider(min, max, Axis.HORIZONTAL).apply {
|
||||
valueChangeListener = IntConsumer {
|
||||
opt.value = it
|
||||
label.text = Text.literal(opt.value.toString())
|
||||
config.save()
|
||||
}
|
||||
guiAppender.onReload {
|
||||
value = opt.value
|
||||
label.text = Text.literal(opt.value.toString())
|
||||
}
|
||||
})
|
||||
guiAppender.skipRows(1)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -30,6 +30,7 @@ import kotlin.io.path.readText
|
||||
import kotlin.io.path.writeText
|
||||
import kotlin.properties.ReadWriteProperty
|
||||
import kotlin.reflect.KProperty
|
||||
import kotlin.time.Duration
|
||||
import net.minecraft.client.gui.screen.Screen
|
||||
import net.minecraft.text.Text
|
||||
import moe.nea.firmament.Firmament
|
||||
@@ -139,6 +140,25 @@ abstract class ManagedConfig(val name: String) {
|
||||
return option(propertyName, default, BooleanHandler(this))
|
||||
}
|
||||
|
||||
protected fun duration(
|
||||
propertyName: String,
|
||||
min: Duration,
|
||||
max: Duration,
|
||||
default: () -> Duration,
|
||||
): Option<Duration> {
|
||||
return option(propertyName, default, DurationHandler(this, min, max))
|
||||
}
|
||||
|
||||
|
||||
protected fun integer(
|
||||
propertyName: String,
|
||||
min: Int,
|
||||
max: Int,
|
||||
default: () -> Int,
|
||||
): Option<Int> {
|
||||
return option(propertyName, default, IntegerHandler(this, min, max))
|
||||
}
|
||||
|
||||
protected fun button(propertyName: String, runnable: () -> Unit): Option<Unit> {
|
||||
return option(propertyName, { }, ClickHandler(this, runnable))
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ class StringHandler(val config: ManagedConfig) : ManagedConfig.OptionHandler<Str
|
||||
guiAppender.appendLabeledRow(
|
||||
opt.labelText,
|
||||
WTextField(opt.labelText).apply {
|
||||
maxLength = 1000
|
||||
suggestion = Text.translatableWithFallback(opt.rawLabelText + ".hint", "")
|
||||
guiAppender.onReload { text = opt.value }
|
||||
setChangedListener {
|
||||
|
||||
Reference in New Issue
Block a user