generated from isXander/FabricKotlinTemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
switch to cloth config due to annoying issues with vigilance
- Loading branch information
Showing
8 changed files
with
223 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 63 additions & 37 deletions
100
src/main/kotlin/dev/isxander/zoomify/config/ZoomifySettings.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,87 @@ | ||
package dev.isxander.zoomify.config | ||
|
||
import dev.isxander.settxi.Setting | ||
import dev.isxander.settxi.impl.boolean | ||
import dev.isxander.settxi.impl.int | ||
import dev.isxander.settxi.impl.option | ||
import dev.isxander.zoomify.utils.SettxiGuiWrapper | ||
import dev.isxander.zoomify.utils.TransitionType | ||
import dev.isxander.zoomify.utils.formatEnum | ||
import dev.isxander.zoomify.utils.mc | ||
import gg.essential.vigilance.Vigilant | ||
import net.minecraft.text.TranslatableText | ||
import java.io.File | ||
|
||
object ZoomifySettings : Vigilant(File(mc.runDirectory, "config/zoomify.toml"), "Zoomify") { | ||
var initialZoom = 4f | ||
object ZoomifySettings : SettxiGuiWrapper(TranslatableText("zoomify.gui.title"), File(mc.runDirectory, "config/zoomify.json")) { | ||
override val settings = mutableListOf<Setting<*>>() | ||
|
||
var maxScrollZoom = 0.75f | ||
private var _scrollZoomTransition = TransitionType.LINEAR.ordinal | ||
var initialZoom by int(4) { | ||
name = "zoomify.gui.initialZoom.name" | ||
description = "zoomify.gui.initialZoom.description" | ||
category = "zoomify.gui.category.behaviour" | ||
range = 1..10 | ||
} | ||
|
||
var maxScrollZoom by int(75) { | ||
name = "zoomify.gui.maxScrollZoom.name" | ||
description = "zoomify.gui.maxScrollZoom.description" | ||
category = "zoomify.gui.category.behaviour" | ||
range = 1..100 | ||
} | ||
|
||
var _scrollZoomTransition by option(TransitionType.values().toOptionContainer { it.translationKey }.options[0]) { | ||
name = "zoomify.gui.scrollZoomTransition.name" | ||
description = "zoomify.gui.scrollZoomTransition.description" | ||
category = "zoomify.gui.category.scrolling" | ||
} | ||
var scrollZoomTransition: TransitionType | ||
get() = TransitionType.values()[this._scrollZoomTransition] | ||
get() = TransitionType.values()[this._scrollZoomTransition.ordinal] | ||
set(value) { | ||
this._scrollZoomTransition = value.ordinal | ||
this._scrollZoomTransition = _scrollZoomTransition.container.options[value.ordinal] | ||
} | ||
var scrollZoomSpeed = 0.5f | ||
|
||
var zoomSpeed = 0.5f | ||
private var _zoomTransition = TransitionType.LINEAR.ordinal | ||
var scrollZoomSpeed by int(50) { | ||
name = "zoomify.gui.scrollZoomSpeed.name" | ||
description = "zoomify.gui.scrollZoomSpeed.description" | ||
category = "zoomify.gui.category.scrolling" | ||
range = 1..100 | ||
} | ||
|
||
var zoomSpeed by int(50) { | ||
name = "zoomify.gui.zoomSpeed.name" | ||
description = "zoomify.gui.zoomSpeed.description" | ||
category = "zoomify.gui.category.behaviour" | ||
range = 1..100 | ||
} | ||
|
||
var _zoomTransition by option(TransitionType.values().toOptionContainer { it.translationKey }.options[0]) { | ||
name = "zoomify.gui.zoomTransition.name" | ||
description = "zoomify.gui.zoomTransition.description" | ||
category = "zoomify.gui.category.behaviour" | ||
} | ||
var zoomTransition: TransitionType | ||
get() = TransitionType.values()[this._zoomTransition] | ||
get() = TransitionType.values()[this._zoomTransition.ordinal] | ||
set(value) { | ||
this._zoomTransition = value.ordinal | ||
this._zoomTransition = _zoomTransition.container.options[value.ordinal] | ||
} | ||
|
||
|
||
private var _zoomKeyBehaviour = ZoomKeyBehaviour.HOLD.ordinal | ||
var _zoomKeyBehaviour by option(ZoomKeyBehaviour.values().toOptionContainer { it.translationKey }.options[0]) { | ||
name = "zoomify.gui.zoomKeyBehaviour.name" | ||
description = "zoomify.gui.zoomKeyBehaviour.description" | ||
category = "zoomify.gui.category.controls" | ||
} | ||
var zoomKeyBehaviour: ZoomKeyBehaviour | ||
get() = ZoomKeyBehaviour.values()[this._zoomKeyBehaviour] | ||
get() = ZoomKeyBehaviour.values()[this._zoomKeyBehaviour.ordinal] | ||
set(value) { | ||
this._zoomKeyBehaviour = value.ordinal | ||
this._zoomKeyBehaviour = _zoomKeyBehaviour.container.options[value.ordinal] | ||
} | ||
|
||
var cinematicCam = false | ||
var cinematicCam by boolean(false) { | ||
name = "zoomify.gui.cinematicCam.name" | ||
description = "zoomify.gui.cinematicCam.description" | ||
category = "zoomify.gui.category.controls" | ||
} | ||
|
||
init { | ||
category("zoomify.gui.category.behaviour") { | ||
decimalSlider(::initialZoom, "zoomify.gui.initialZoom.name", "zoomify.gui.initialZoom.description", min = 1f, max = 10f) | ||
percentSlider(::zoomSpeed, "zoomify.gui.zoomSpeed.name", "zoomify.gui.zoomSpeed.description") | ||
selector(::_zoomTransition, "zoomify.gui.zoomTransition.name", "zoomify.gui.zoomTransition.description", TransitionType.values().map { it.translationKey }) | ||
|
||
subcategory("zoomify.gui.subcategory.scrolling") { | ||
percentSlider(::maxScrollZoom, "zoomify.gui.maxScrollZoom.name", "zoomify.gui.maxScrollZoom.description") | ||
percentSlider(::scrollZoomSpeed, "zoomify.gui.scrollZoomSpeed.name", "zoomify.gui.scrollZoomSpeed.description") | ||
selector(::_scrollZoomTransition, "zoomify.gui.scrollZoomTransition.name", "zoomify.gui.scrollZoomTransition.description", TransitionType.values().map { it.translationKey }) | ||
} | ||
} | ||
|
||
category("zoomify.gui.category.controls") { | ||
selector(::_zoomKeyBehaviour, "zoomify.gui.zoomKeyBehaviour.name", "zoomify.gui.zoomKeyBehaviour.description", ZoomKeyBehaviour.values().map { it.translationKey }) | ||
switch(::cinematicCam, "zoomify.gui.cinematicCam.name", "zoomify.gui.cinematicCam.description") | ||
} | ||
|
||
initialize() | ||
|
||
hidePropertyIf(::zoomSpeed) { zoomTransition == TransitionType.INSTANT } | ||
load() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
src/main/kotlin/dev/isxander/zoomify/utils/SettxiGuiWrapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package dev.isxander.zoomify.utils | ||
|
||
import dev.isxander.settxi.Setting | ||
import dev.isxander.settxi.impl.* | ||
import dev.isxander.settxi.serialization.ConfigProcessor | ||
import dev.isxander.settxi.serialization.asJson | ||
import dev.isxander.settxi.serialization.populateFromJson | ||
import kotlinx.serialization.decodeFromString | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
import me.shedaniel.clothconfig2.api.ConfigBuilder | ||
import me.shedaniel.clothconfig2.impl.builders.DropdownMenuBuilder | ||
import net.minecraft.client.gui.screen.Screen | ||
import net.minecraft.text.Text | ||
import net.minecraft.text.TranslatableText | ||
import java.io.File | ||
import java.util.function.Supplier | ||
|
||
abstract class SettxiGuiWrapper(val title: Text, val file: File) : ConfigProcessor { | ||
override val settings = mutableListOf<Setting<*>>() | ||
|
||
fun load() { | ||
if (!file.exists()) { | ||
save() | ||
return | ||
} | ||
|
||
settings.populateFromJson(Json.decodeFromString(file.readText())) | ||
} | ||
|
||
fun save() { | ||
file.writeText(Json.encodeToString(settings.asJson())) | ||
} | ||
|
||
fun clothGui(parent: Screen? = null): Screen = | ||
ConfigBuilder.create().apply { | ||
parentScreen = parent | ||
this.title = this@SettxiGuiWrapper.title | ||
|
||
for (setting in settings) { | ||
val category = getOrCreateCategory(TranslatableText(setting.category)) | ||
category.addEntry(clothEntry(setting)) | ||
} | ||
|
||
setSavingRunnable { save() } | ||
}.build() | ||
|
||
private fun ConfigBuilder.clothEntry(setting: Setting<*>) = | ||
when (setting) { | ||
is BooleanSetting -> | ||
entryBuilder().startBooleanToggle(TranslatableText(setting.name), setting.get()).apply { | ||
defaultValue = Supplier { setting.default } | ||
setTooltip(TranslatableText(setting.description)) | ||
setSaveConsumer { setting.set(it) } | ||
} | ||
is DoubleSetting -> | ||
entryBuilder().startDoubleField(TranslatableText(setting.name), setting.get()).apply { | ||
defaultValue = Supplier { setting.default } | ||
setTooltip(TranslatableText(setting.description)) | ||
setSaveConsumer { setting.set(it) } | ||
setMin(setting.range.start) | ||
setMax(setting.range.endInclusive) | ||
} | ||
is FloatSetting -> | ||
entryBuilder().startFloatField(TranslatableText(setting.name), setting.get()).apply { | ||
defaultValue = Supplier { setting.default } | ||
setTooltip(TranslatableText(setting.description)) | ||
setSaveConsumer { setting.set(it) } | ||
setMin(setting.range.start) | ||
setMax(setting.range.endInclusive) | ||
} | ||
is LongSetting -> | ||
entryBuilder().startLongSlider(TranslatableText(setting.name), setting.get(), setting.range.first, setting.range.last).apply { | ||
defaultValue = Supplier { setting.default } | ||
setTooltip(TranslatableText(setting.description)) | ||
setSaveConsumer { setting.set(it) } | ||
} | ||
is IntSetting -> | ||
entryBuilder().startIntSlider(TranslatableText(setting.name), setting.get(), setting.range.first, setting.range.last).apply { | ||
defaultValue = Supplier { setting.default } | ||
setTooltip(TranslatableText(setting.description)) | ||
setSaveConsumer { setting.set(it) } | ||
} | ||
is StringSetting -> | ||
entryBuilder().startStrField(TranslatableText(setting.name), setting.get()).apply { | ||
defaultValue = Supplier { setting.default } | ||
setTooltip(TranslatableText(setting.description)) | ||
setSaveConsumer { setting.set(it) } | ||
} | ||
is OptionSetting -> | ||
entryBuilder().startStringDropdownMenu(TranslatableText(setting.name), setting.get().name) { TranslatableText(it) }.apply { | ||
defaultValue = Supplier { setting.default.name } | ||
setTooltip(TranslatableText(setting.description)) | ||
isSuggestionMode = false | ||
setSelections(setting.options.map { TranslatableText(it.name).string }) | ||
setSaveConsumer { setting.set(setting.options.first { option -> | ||
TranslatableText(option.name).string == it | ||
})} | ||
} | ||
is FileSetting -> | ||
throw UnsupportedOperationException("Cloth config does not support file settings") | ||
|
||
else -> throw IllegalArgumentException("Unknown setting type: ${setting.javaClass}") | ||
}.build() | ||
|
||
protected fun <T> Array<T>.toOptionContainer(nameProvider: (T) -> String): OptionContainer { | ||
return object : OptionContainer() { | ||
init { | ||
for (value in this@toOptionContainer) { | ||
option(nameProvider(value)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.