Gimbal/src/main/java/net/moonleay/gimbal/client/screen/GimbalSettingsGui.kt
moonleay b4f46ee703
feat: added option and config for player fly speed
Signed-off-by: moonleay <contact@moonleay.net>
2024-05-16 03:31:41 +02:00

218 lines
8.6 KiB
Kotlin

/*
* Gimbal
* Copyright (C) 2024 moonleay
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.moonleay.gimbal.client.screen
import net.minecraft.client.gui.screen.Screen
import net.minecraft.client.gui.widget.ButtonWidget
import net.minecraft.client.gui.widget.CyclingButtonWidget
import net.minecraft.client.util.math.MatrixStack
import net.minecraft.screen.ScreenTexts
import net.minecraft.text.Text
import net.moonleay.gimbal.client.config.ClientConfigHolder
import net.moonleay.gimbal.client.config.GimbalClientConfig
import net.moonleay.gimbal.client.config.GimbalGuiSettings
import net.moonleay.gimbal.client.config.enums.HudOptions
import net.moonleay.gimbal.client.config.enums.ToastSettings
import net.moonleay.gimbal.client.screen.widgets.GimbalSliderWidget
import net.moonleay.gimbal.client.util.NumberUtil
import net.moonleay.gimbal.constants.TranslationKeys
class GimbalSettingsGui(private val parent: Screen, private val cfg: ClientConfigHolder) :
Screen(Text.translatable(TranslationKeys.Gui.Config.SCREEN_NAME)) {
private var hudOptions: HudOptions
private var toastSettings: ToastSettings
private var playerFlySpeed: Int
init {
this.hudOptions = cfg.config.guiSettings.hudOptions
this.toastSettings = cfg.config.toastSettings
this.playerFlySpeed = cfg.config.playerFlySpeed
}
override fun init() {
this.addDrawableChild(CyclingButtonWidget.onOffBuilder(
// Text.translatable("gimbal.gui.enabled"),
// Text.translatable("gimbal.gui.disabled")
)
.initially(cfg.config.guiSettings.showHud)
.build(
this.width / 2 - 155,
this.height / 6 + 24 * 0,
150,
20,
Text.translatable(TranslationKeys.Gui.Config.Hud.SHOW_HUD)
) { _: CyclingButtonWidget<Boolean?>?, isEnabled: Boolean? ->
val oldGui = cfg.config.guiSettings
val newGui = GimbalGuiSettings(
showHud = isEnabled ?: true,
offset = oldGui.offset,
horizontalAnchor = oldGui.horizontalAnchor,
verticalAnchor = oldGui.verticalAnchor,
hudOptions = oldGui.hudOptions
)
val newConf = GimbalClientConfig(
guiSettings = newGui,
toastSettings = cfg.config.toastSettings,
playerFlySpeed = cfg.config.playerFlySpeed
)
cfg.updateConfig(newConf)
})
this.addDrawableChild(ButtonWidget(
this.width / 2 - 155 + 160,
this.height / 6 + 24 * 0,
150,
20,
Text.translatable(TranslationKeys.Gui.Config.Hud.EDIT_HUD)
) { _: ButtonWidget? ->
this.client!!.setScreen(GimbalEditHudGui(this, cfg))
})
this.addDrawableChild<CyclingButtonWidget<HudOptions>>(
CyclingButtonWidget.builder<HudOptions> { value: HudOptions? ->
when (value) {
HudOptions.ALL -> {
return@builder HudOptions.ALL.translatableText
}
HudOptions.NUMBER -> {
return@builder HudOptions.NUMBER.translatableText
}
HudOptions.INITIAL -> {
return@builder HudOptions.INITIAL.translatableText
}
HudOptions.ONLY_MODE -> {
return@builder HudOptions.ONLY_MODE.translatableText
}
else -> return@builder Text.translatable(TranslationKeys.Gui.Config.Hud.Modifiers.SHOW_ALL)
}
}
.values(HudOptions.entries)
.initially(this.hudOptions)
.build(
this.width / 2 - 155,
this.height / 6 + 24 * 1,
150,
20,
Text.translatable(TranslationKeys.Gui.Config.Hud.Modifiers.SHOW_MODIFIERS)
) { _: CyclingButtonWidget<HudOptions>?, hudDO: HudOptions ->
this.hudOptions = hudDO
val oldConfig = cfg.config
val oldGuiConfig = oldConfig.guiSettings
val newConfig = GimbalClientConfig(
guiSettings = GimbalGuiSettings(
horizontalAnchor = oldGuiConfig.horizontalAnchor,
verticalAnchor = oldGuiConfig.verticalAnchor,
showHud = oldGuiConfig.showHud,
offset = oldGuiConfig.offset,
hudOptions = this.hudOptions
),
toastSettings = oldConfig.toastSettings,
playerFlySpeed = oldConfig.playerFlySpeed
)
cfg.updateConfig(newConfig)
}
)
this.addDrawableChild<CyclingButtonWidget<ToastSettings>>(
CyclingButtonWidget.builder<ToastSettings> { value: ToastSettings? ->
when (value) {
ToastSettings.ALL -> {
return@builder ToastSettings.ALL.translatableText
}
ToastSettings.ONLY_TOGGLE -> {
return@builder ToastSettings.ONLY_TOGGLE.translatableText
}
ToastSettings.ONLY_SYSTEM -> {
return@builder ToastSettings.ONLY_SYSTEM.translatableText
}
ToastSettings.NONE -> {
return@builder ToastSettings.NONE.translatableText
}
else -> return@builder Text.translatable(TranslationKeys.Gui.Config.Toasts.SHOW_ALL)
}
}
.values(ToastSettings.entries)
.initially(this.toastSettings)
.build(
this.width / 2 - 155 + 160,
this.height / 6 + 24 * 1,
150,
20,
Text.translatable(TranslationKeys.Gui.Config.Toasts.SHOW_TOASTS)
) { _: CyclingButtonWidget<ToastSettings>?, toastStng: ToastSettings ->
this.toastSettings = toastStng
val oldConfig = cfg.config
val newConfig = GimbalClientConfig(
guiSettings = oldConfig.guiSettings,
toastSettings = this.toastSettings,
playerFlySpeed = oldConfig.playerFlySpeed
)
cfg.updateConfig(newConfig)
}
)
this.addDrawableChild(GimbalSliderWidget(
this.width / 2 - 155,
this.height / 6 + 24 * 2,
150,
20,
Text.translatable(TranslationKeys.Gui.Config.PLAYER_FLY_SPEED),
NumberUtil.interpolate(this.playerFlySpeed.toDouble(), 50.0, 500.0, 0.0, 1.0),
50.0,
500.0,
) { value ->
this.playerFlySpeed = NumberUtil.interpolate(value, 0.0, 1.0, 50.0, 500.0).toInt()
val oldConfig = cfg.config
val newConfig = GimbalClientConfig(
guiSettings = oldConfig.guiSettings,
toastSettings = this.toastSettings,
playerFlySpeed = this.playerFlySpeed
)
cfg.updateConfig(newConfig)
})
// Done button
this.addDrawableChild(ButtonWidget(
this.width / 2 - 100, this.height / 6 + 168, 200, 20, ScreenTexts.DONE
) { _: ButtonWidget? ->
this.client!!.setScreen(
this.parent
)
})
}
override fun render(matrices: MatrixStack?, mouseX: Int, mouseY: Int, delta: Float) {
this.renderBackground(matrices)
drawCenteredText(matrices, this.textRenderer, this.title, this.width / 2, 15, 16777215)
super.render(matrices, mouseX, mouseY, delta)
}
}