Add a Zoom button

This commit is contained in:
Username404 2021-06-27 19:17:52 +02:00
parent 05dcbee261
commit 35775b98f2
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
12 changed files with 98 additions and 10 deletions

View File

@ -18,7 +18,7 @@ abstract class Snowy {
@JvmStatic @JvmStatic
protected val FeaturePackage: String = "fr.username404.snowygui.gui.feature" protected val FeaturePackage: String = "fr.username404.snowygui.gui.feature"
lateinit var annotatedButtons: Set<Class<out ButtonImpl>> lateinit var annotatedButtons: Set<Class<out ButtonImpl>>
fun onEvent(e: String, lambda: argsLambda) = useKey(e).add(lambda) fun onEvent(e: Any, lambda: argsLambda) = useKey(e.toString()).add(lambda)
@JvmField @JvmField
val logs: Logger = LogManager.getLogger() val logs: Logger = LogManager.getLogger()
} }

View File

@ -10,6 +10,7 @@ import fr.username404.snowygui.gui.feature.Category
import fr.username404.snowygui.gui.feature.Colors import fr.username404.snowygui.gui.feature.Colors
import fr.username404.snowygui.gui.feature.Macro import fr.username404.snowygui.gui.feature.Macro
import fr.username404.snowygui.gui.feature.riskyCheats import fr.username404.snowygui.gui.feature.riskyCheats
import fr.username404.snowygui.gui.feature.Zoom
import net.minecraft.client.gui.screens.Screen import net.minecraft.client.gui.screens.Screen
import net.minecraft.network.chat.Component import net.minecraft.network.chat.Component
import net.minecraft.network.chat.TextComponent import net.minecraft.network.chat.TextComponent
@ -48,9 +49,18 @@ val SnowyConfigScreen: Screen = object: Screen(translationComponent) {
.setTitle(translationComponent).apply { .setTitle(translationComponent).apply {
with(entryBuilder()) { with(entryBuilder()) {
getOrCreateCategory(TranslatableComponent("$confPrefix.general")).addEntry(startSubCategory(TranslatableComponent("$confPrefix.behavior")).apply { getOrCreateCategory(TranslatableComponent("$confPrefix.general")).addEntry(startSubCategory(TranslatableComponent("$confPrefix.behavior")).apply {
add(startBooleanToggle(TranslatableComponent("$confPrefix.behavior.sortalphabetically"), sortAlphabetically) addAll(
.setDefaultValue(true).requireRestart() setOf(
.setSaveConsumer { sortAlphabetically = it }.build() startBooleanToggle(TranslatableComponent("$confPrefix.behavior.sortalphabetically"), sortAlphabetically)
.setDefaultValue(true).requireRestart()
.setSaveConsumer { sortAlphabetically = it }.build(),
startDoubleField(TranslatableComponent("$confPrefix.behavior.zoom.factor"), Zoom.zoomFactor).setSaveConsumer {
Zoom.zoomFactor = it
}.setMin(1.1).build(),
startBooleanToggle(TranslatableComponent("$confPrefix.behavior.zoom.smoothcamera"), Zoom.smoothCameraOnZoom).setSaveConsumer {
Zoom.smoothCameraOnZoom = it
}.build()
)
) )
}.build()) }.build())
.addEntry( .addEntry(

View File

@ -27,6 +27,7 @@ import kotlinx.coroutines.runBlocking
import kotlinx.serialization.decodeFromString import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import kotlin.reflect.full.isSuperclassOf
object Configuration { object Configuration {
@Deprecated("Use the getValue or setValue methods instead", level = DeprecationLevel.ERROR) @Deprecated("Use the getValue or setValue methods instead", level = DeprecationLevel.ERROR)
@ -65,9 +66,7 @@ object Configuration {
parseString( parseString(
""" """
|Snowy { |Snowy {
| displayInitMessage = false
| macros = [] | macros = []
| riskyCheats = false
| sortAlphabetically = true | sortAlphabetically = true
|} |}
""".trimMargin() """.trimMargin()
@ -136,7 +135,20 @@ object Configuration {
@Suppress("DEPRECATION_ERROR") @Suppress("DEPRECATION_ERROR")
if (ModifiableValues.containsKey(property.name)) if (ModifiableValues.containsKey(property.name))
ModifiableValues[property.name] as T ModifiableValues[property.name] as T
else invoke().getValue(ref, property) else try {
invoke().getValue(ref, property)
} catch (e: ConfigException) {
when {
Number::class.isSuperclassOf(T::class) -> when (T::class) {
Double::class -> 2.0
Int::class, UInt::class, Short::class, UShort::class -> 2
Float::class -> 2F
else -> throw e
} as T
T::class == Boolean::class -> false as T
else -> throw e
}
}
operator fun <T> setValue(ref: Any?, property: KProperty<*>, value: T) = @Suppress("DEPRECATION_ERROR") operator fun <T> setValue(ref: Any?, property: KProperty<*>, value: T) = @Suppress("DEPRECATION_ERROR")
ModifiableValues.setValue(ref, property, value) ModifiableValues.setValue(ref, property, value)

View File

@ -0,0 +1,15 @@
package fr.username404.snowygui.gui.feature
import fr.username404.snowygui.config.Configuration
import net.minecraft.client.Minecraft
@ButtonInfo(Category.MISC)
object Zoom: ButtonImpl() {
internal var smoothCameraOnZoom: Boolean by Configuration
internal var zoomFactor: Double by Configuration
@JvmStatic
fun getNewZoom(fov: Double): Double = fov / zoomFactor
override fun execAction() {
Minecraft.getInstance().options.smoothCamera = toggled && smoothCameraOnZoom
}
}

View File

@ -12,6 +12,8 @@
"screen.snowy.config.colors": "Box colors", "screen.snowy.config.colors": "Box colors",
"screen.snowy.config.behavior": "Behavior", "screen.snowy.config.behavior": "Behavior",
"screen.snowy.config.behavior.sortalphabetically": "Sort buttons alphabetically", "screen.snowy.config.behavior.sortalphabetically": "Sort buttons alphabetically",
"screen.snowy.config.behavior.zoom.factor": "Zoom factor",
"screen.snowy.config.behavior.zoom.smoothcamera": "Smooth camera on zoom",
"category.snowy.keycategory": "SnowyGUI", "category.snowy.keycategory": "SnowyGUI",
"key.snowy.opengui": "Open the snowy gui", "key.snowy.opengui": "Open the snowy gui",
"key.snowy.configkey": "Open the snowy configuration screen", "key.snowy.configkey": "Open the snowy configuration screen",

View File

@ -11,6 +11,8 @@
"screen.snowy.config.colors": "Couleurs des boîtes", "screen.snowy.config.colors": "Couleurs des boîtes",
"screen.snowy.config.behavior": "Comportement", "screen.snowy.config.behavior": "Comportement",
"screen.snowy.config.behavior.sortalphabetically": "Trier les boutons alphabétiquement", "screen.snowy.config.behavior.sortalphabetically": "Trier les boutons alphabétiquement",
"screen.snowy.config.behavior.zoom.factor": "Facteur de zoom",
"screen.snowy.config.behavior.zoom.smoothcamera": "Caméra fluide pendant le zoom",
"category.snowy.keycategory": "SnowyGUI", "category.snowy.keycategory": "SnowyGUI",
"key.snowy.opengui": "Ouvrir l'interface de snowy", "key.snowy.opengui": "Ouvrir l'interface de snowy",
"key.snowy.configkey": "Ouvrir l'écran de configuration de snowy", "key.snowy.configkey": "Ouvrir l'écran de configuration de snowy",

View File

@ -0,0 +1,20 @@
package fr.username404.snowygui.mixins;
import fr.username404.snowygui.gui.feature.Zoom;
import net.minecraft.client.Options;
import net.minecraft.client.renderer.GameRenderer;
import org.objectweb.asm.Opcodes;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
@Mixin(GameRenderer.class)
abstract class ZoomMixin {
@Redirect(
at = @At(value = "FIELD", target = "Lnet/minecraft/client/Options;fov:D", opcode = Opcodes.GETFIELD, ordinal = 0),
method = "getFov(Lnet/minecraft/client/Camera;FZ)D"
)
private double getFov(Options options) {
return Zoom.INSTANCE.getToggled() ? Zoom.getNewZoom(options.fov) : options.fov;
}
}

View File

@ -29,7 +29,8 @@
] ]
}, },
"mixins": [ "mixins": [
"snowygui-mixins.json" "snowygui-mixins.json",
"snowygui-mixins-fabric.json"
], ],
"depends": { "depends": {
"fabricloader": ">=0.11.0", "fabricloader": ">=0.11.0",

View File

@ -0,0 +1,12 @@
{
"required": true,
"package": "fr.username404.snowygui.mixins",
"compatibilityLevel": "JAVA_8",
"client": [
"ZoomMixin"
],
"injectors": {
"defaultRequire": 1
},
"minVersion": "0.8"
}

View File

@ -49,6 +49,9 @@ class ForgeInit: Snowy() {
addListener(this@ForgeInit::initSetup) addListener(this@ForgeInit::initSetup)
addListener(this@ForgeInit::configSetup) addListener(this@ForgeInit::configSetup)
} }
FORGE_BUS.register(HudHandler) with(FORGE_BUS) {
register(HudHandler)
register(ZoomHandler)
}
} }
} }

View File

@ -1,4 +1,3 @@
@file:Suppress("UNUSED")
package fr.username404.snowygui.forge package fr.username404.snowygui.forge
import fr.username404.snowygui.EventSnowy import fr.username404.snowygui.EventSnowy

View File

@ -0,0 +1,12 @@
package fr.username404.snowygui.forge
import fr.username404.snowygui.gui.feature.Zoom
import net.minecraftforge.client.event.EntityViewRenderEvent
import net.minecraftforge.eventbus.api.SubscribeEvent
object ZoomHandler {
@SubscribeEvent
fun onFOVEvent(event: EntityViewRenderEvent.FOVModifier) {
if (Zoom.toggled) event.fov = Zoom.getNewZoom(event.fov)
}
}