Skip to content

Commit

Permalink
Rename and clean up a bunch of things
Browse files Browse the repository at this point in the history
  • Loading branch information
trainb0y committed Sep 24, 2023
1 parent c0810e0 commit 7246d5a
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ public class GameRendererMixin {
method = "getFov(Lnet/minecraft/client/render/Camera;FZ)D",
cancellable = true
)
private double getZoomedFov(Camera camera, float tickDelta, boolean changingFov, CallbackInfoReturnable<Double> info) {
double result = ZoomLogic.getFov(info.getReturnValue(), tickDelta);
info.setReturnValue(result);
return result;
private void getZoomedFov(Camera camera, float tickDelta, boolean changingFov, CallbackInfoReturnable<Double> info) {
info.setReturnValue(ZoomLogic.getFov(info.getReturnValue(), tickDelta));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.github.trainb0y.fabrizoom.mixin;

import io.github.trainb0y.fabrizoom.ZoomLogic;
import io.github.trainb0y.fabrizoom.config.Config;
import io.github.trainb0y.fabrizoom.config.ConfigHandler;
import io.github.trainb0y.fabrizoom.config.ZoomOverlay;
import io.github.trainb0y.fabrizoom.config.ZoomTransition;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.hud.InGameHud;
import net.minecraft.util.Identifier;
Expand All @@ -27,23 +29,19 @@ private void renderSpyglassOverlay(DrawContext context, float scale) {}
@Shadow
private void renderOverlay(DrawContext context, Identifier texture, float opacity) {}


/**
* Renders the zoom vignette
*/
@Inject(
at = @At(value = "INVOKE", target = "net/minecraft/entity/player/PlayerInventory.getArmorStack(I)Lnet/minecraft/item/ItemStack;"),
method = "render(Lnet/minecraft/client/gui/DrawContext;F)V"
)
public void injectZoomOverlay(DrawContext context, float tickDelta, CallbackInfo info) {
var overlay = Config.getValues().getZoomOverlay();
var overlay = ConfigHandler.getValues().getZoomOverlay();

if (overlay == Config.ZoomOverlay.NONE || ZoomLogic.INSTANCE.getCurrentZoomFovMultiplier() >= 0.99) return;
if (overlay == ZoomOverlay.NONE || ZoomLogic.INSTANCE.getCurrentZoomFovMultiplier() >= 0.99) return;

switch (overlay) {
case VIGNETTE -> {
float alpha;
if (Config.getValues().getTransition() != Config.Transition.NONE) {
if (ConfigHandler.getValues().getTransition() != ZoomTransition.NONE) {
// smooth and linear transition
alpha = 1 - MathHelper.lerp(tickDelta, ZoomLogic.getLastZoomOverlayAlpha(), ZoomLogic.getZoomOverlayAlpha());
} else {
Expand All @@ -54,7 +52,7 @@ public void injectZoomOverlay(DrawContext context, float tickDelta, CallbackInfo
renderOverlay(context, ZOOM_OVERLAY, alpha);
}
case SPYGLASS -> {
if (ZoomLogic.getZooming()) {
if (ZoomLogic.isZooming()) {
float scale = (1f / (float) ZoomLogic.getZoomDivisor()) / ZoomLogic.INSTANCE.getCurrentZoomFovMultiplier();
scale += 0.125f; // this is jank, but the vanilla spyglass lerps from 0.5 to 1.125
scale = MathHelper.clamp(scale, 0.5f, 1.125f);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.trainb0y.fabrizoom.mixin;

import io.github.trainb0y.fabrizoom.ZoomLogic;
import io.github.trainb0y.fabrizoom.config.Config;
import io.github.trainb0y.fabrizoom.config.ConfigHandler;
import net.minecraft.client.Mouse;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand Down Expand Up @@ -71,7 +71,7 @@ public class MouseMixin {
)
public void applyZoomChanges(CallbackInfo ci, double d, double e, double k, double l, double f, double g, double h, int m) {
this.modifyMouse = false;
if (ZoomLogic.getZooming()) {
if (ZoomLogic.isZooming()) {
k = ZoomLogic.applyMouseXModifier(k, h, e);
l = ZoomLogic.applyMouseYModifier(l, h, e);
this.modifyMouse = true;
Expand Down Expand Up @@ -138,7 +138,7 @@ private double modifyFinalCursorDeltaY(double l) {
cancellable = true
)
private void onMouseScroll(CallbackInfo info) {
if (this.eventDeltaVerticalWheel == 0.0 || !ZoomLogic.getZooming() || !Config.getValues().getZoomScroll()) return;
if (this.eventDeltaVerticalWheel == 0.0 || !ZoomLogic.isZooming() || !ConfigHandler.getValues().getZoomScroll()) return;

ZoomLogic.changeZoomDivisor(this.eventDeltaVerticalWheel > 0.0);
info.cancel();
Expand Down
8 changes: 3 additions & 5 deletions src/main/kotlin/io/github/trainb0y/fabrizoom/FabriZoom.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package io.github.trainb0y.fabrizoom

import io.github.trainb0y.fabrizoom.config.Config
import io.github.trainb0y.fabrizoom.config.ConfigHandler
import net.fabricmc.api.ClientModInitializer
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents
import org.slf4j.Logger
import org.slf4j.LoggerFactory

/**
* Main mod entrypoint
*/
/** Main mod entrypoint */
@Suppress("Unused")
class FabriZoom : ClientModInitializer {
override fun onInitializeClient() {
Config.loadConfig()
ConfigHandler.loadConfig()
Keybinds.register()
ClientTickEvents.END_CLIENT_TICK.register(
ClientTickEvents.EndTick { Keybinds.onTick() }
Expand Down
21 changes: 10 additions & 11 deletions src/main/kotlin/io/github/trainb0y/fabrizoom/Keybinds.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.trainb0y.fabrizoom

import io.github.trainb0y.fabrizoom.config.Config
import io.github.trainb0y.fabrizoom.config.Config.values
import io.github.trainb0y.fabrizoom.config.ConfigHandler.values
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper
import net.minecraft.client.MinecraftClient
import net.minecraft.client.option.KeyBinding
Expand All @@ -15,7 +14,7 @@ import org.lwjgl.glfw.GLFW
*/
object Keybinds {
/** Translation key for the keybind category */
private const val category = "category.fabrizoom.keybinds"
private const val CATEGORY = "category.fabrizoom.keybinds"

/** The primary key for zooming */
private lateinit var zoomKey: KeyBinding
Expand All @@ -35,16 +34,16 @@ object Keybinds {
fun register() {
// can't register and initialize at the same time, because they won't appear in the keybind menu
zoomKey = KeyBindingHelper.registerKeyBinding(
KeyBinding("key.fabrizoom.zoom", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_C, category)
KeyBinding("key.fabrizoom.zoom", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_C, CATEGORY)
)
increaseKey = KeyBindingHelper.registerKeyBinding(
KeyBinding("key.fabrizoom.increase", InputUtil.Type.KEYSYM, InputUtil.UNKNOWN_KEY.code, category)
KeyBinding("key.fabrizoom.increase", InputUtil.Type.KEYSYM, InputUtil.UNKNOWN_KEY.code, CATEGORY)
)
decreaseKey = KeyBindingHelper.registerKeyBinding(
KeyBinding("key.fabrizoom.decrease", InputUtil.Type.KEYSYM, InputUtil.UNKNOWN_KEY.code, category)
KeyBinding("key.fabrizoom.decrease", InputUtil.Type.KEYSYM, InputUtil.UNKNOWN_KEY.code, CATEGORY)
)
resetKey = KeyBindingHelper.registerKeyBinding(
KeyBinding("key.fabrizoom.reset", InputUtil.Type.KEYSYM, InputUtil.UNKNOWN_KEY.code, category)
KeyBinding("key.fabrizoom.reset", InputUtil.Type.KEYSYM, InputUtil.UNKNOWN_KEY.code, CATEGORY)
)
}

Expand All @@ -54,17 +53,17 @@ object Keybinds {
* @see io.github.trainb0y.fabrizoom.mixin.MouseMixin.onMouseScroll
*/
fun onTick() {
if (ZoomLogic.zooming) {
if (ZoomLogic.isZooming) {
if (decreaseKey.isPressed) ZoomLogic.changeZoomDivisor(false)
if (increaseKey.isPressed) ZoomLogic.changeZoomDivisor(true)
if (resetKey.isPressed || !zoomKey.isPressed) ZoomLogic.zoomDivisor = values.zoomDivisor
}

if (values.zoomSound) { // todo: move this somewhere else, it doesn't belong with keybinds
if (!ZoomLogic.zooming && zoomKey.isPressed) MinecraftClient.getInstance().player?.playSound(SoundEvents.ITEM_SPYGLASS_USE, 1f, 1f)
if (ZoomLogic.zooming && !zoomKey.isPressed) MinecraftClient.getInstance().player?.playSound(SoundEvents.ITEM_SPYGLASS_STOP_USING, 1f, 1f)
if (!ZoomLogic.isZooming && zoomKey.isPressed) MinecraftClient.getInstance().player?.playSound(SoundEvents.ITEM_SPYGLASS_USE, 1f, 1f)
if (ZoomLogic.isZooming && !zoomKey.isPressed) MinecraftClient.getInstance().player?.playSound(SoundEvents.ITEM_SPYGLASS_STOP_USING, 1f, 1f)
}

ZoomLogic.zooming = zoomKey.isPressed
ZoomLogic.isZooming = zoomKey.isPressed
}
}
29 changes: 15 additions & 14 deletions src/main/kotlin/io/github/trainb0y/fabrizoom/ZoomLogic.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package io.github.trainb0y.fabrizoom

import io.github.trainb0y.fabrizoom.config.Config
import io.github.trainb0y.fabrizoom.config.Config.values
import io.github.trainb0y.fabrizoom.config.ConfigHandler
import io.github.trainb0y.fabrizoom.config.ConfigHandler.values
import io.github.trainb0y.fabrizoom.config.ZoomTransition
import net.minecraft.client.util.SmoothUtil
import net.minecraft.util.math.MathHelper

Expand All @@ -27,7 +28,7 @@ object ZoomLogic {

/** Whether we are currently zooming */
@JvmStatic
var zooming = false
var isZooming = false

/** The current zoom divisor (amount to zoom in) */
@JvmStatic
Expand All @@ -39,7 +40,7 @@ object ZoomLogic {
var currentZoomFovMultiplier = 1.0f
private set

var lastZoomFovMultiplier = 1.0f
private var lastZoomFovMultiplier = 1.0f
private set

/**
Expand Down Expand Up @@ -95,41 +96,41 @@ object ZoomLogic {
*/
@JvmStatic
fun tick() {
if (!zooming) {
if (!isZooming) {
this.cursorXZoomSmoother.clear()
this.cursorYZoomSmoother.clear()
}

// calculate zoom fov multiplier
lastZoomFovMultiplier = currentZoomFovMultiplier

val zoomMultiplier = if (zooming) {
val zoomMultiplier = if (isZooming) {
1.0 / zoomDivisor
} else {
1.0
}

currentZoomFovMultiplier = when (values.transition) {
Config.Transition.NONE -> lastZoomFovMultiplier
Config.Transition.LINEAR -> MathHelper.stepTowards(
ZoomTransition.NONE -> lastZoomFovMultiplier
ZoomTransition.LINEAR -> MathHelper.stepTowards(
currentZoomFovMultiplier,
zoomMultiplier.toFloat(),
zoomMultiplier.coerceIn(values.minimumLinearStep, values.maximumLinearStep).toFloat()
)

Config.Transition.SMOOTH -> currentZoomFovMultiplier + (zoomMultiplier - currentZoomFovMultiplier).toFloat() * values.smoothMultiplier
ZoomTransition.SMOOTH -> currentZoomFovMultiplier + (zoomMultiplier - currentZoomFovMultiplier).toFloat() * values.smoothMultiplier
}

// Calculate zoom overlay alpha
lastZoomOverlayAlpha = zoomOverlayAlpha
zoomOverlayAlpha = when (values.transition) {
Config.Transition.SMOOTH -> zoomOverlayAlpha + (zoomMultiplier - zoomOverlayAlpha).toFloat() * values.smoothMultiplier
Config.Transition.LINEAR -> {
ZoomTransition.SMOOTH -> zoomOverlayAlpha + (zoomMultiplier - zoomOverlayAlpha).toFloat() * values.smoothMultiplier
ZoomTransition.LINEAR -> {
val linearStep = (1.0f / zoomDivisor).coerceIn(values.minimumLinearStep, values.maximumLinearStep)
MathHelper.stepTowards(zoomOverlayAlpha, zoomMultiplier.toFloat(), linearStep.toFloat())
}

Config.Transition.NONE -> if (zooming) {
ZoomTransition.NONE -> if (isZooming) {
1f
} else {
0f
Expand All @@ -142,7 +143,7 @@ object ZoomLogic {
* Used by the keybinds and mouse zoom scrolling
*
* @param increase whether to increase or decrease the zoom
* @see Config.values
* @see ConfigHandler.values
* @see io.github.trainb0y.fabrizoom.config.ConfigurableValues.scrollStep
*/
@JvmStatic
Expand All @@ -162,7 +163,7 @@ object ZoomLogic {
@JvmStatic
fun getFov(fov: Double, delta: Float): Double =
when (values.transition) {
Config.Transition.NONE -> if (zooming) {
ZoomTransition.NONE -> if (isZooming) {
fov / zoomDivisor
} else {
fov
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,25 @@ import java.nio.file.Path
* Handles loading and saving of the mod configuration
*/
@ConfigSerializable
object Config {
object ConfigHandler {

/** The path to the config file */
private val configPath: Path = FabricLoader.getInstance().configDir.resolve("fabrizoom.conf")
private val configFilePath: Path = FabricLoader.getInstance().configDir.resolve("fabrizoom.conf")

/** The config version, doesn't necessarily match mod version */
@Suppress("Unused")
private const val version = "1"
private const val VERSION = "2"

/** The current configuration values */
@JvmStatic
var values = Presets.DEFAULT.values!!.copy() // this should get overwritten

/** A type of Zoom Transition */
enum class Transition(
/** Translation key for the name of this zoom transition */
val key: String
) {
LINEAR("transition.fabrizoom.linear"),
SMOOTH("transition.fabrizoom.smooth"),
NONE("transition.fabrizoom.none");
}

enum class ZoomOverlay(
val key: String
) {
NONE("overlay.fabrizoom.none"),
VIGNETTE("overlay.fabrizoom.vignette"),
SPYGLASS("overlay.fabrizoom.spyglass");
}

/**
* Save the current configuration [values] to [configPath]
*/
fun saveConfig() {

val loader = HoconConfigurationLoader.builder()
.path(configPath)
.path(configFilePath)
.build()
try {
val root = loader.load()
root.node("version").set(version)
root.node("version").set(VERSION)
root.node("values").set(values)
loader.save(root)
FabriZoom.logger.info("Saved configuration")
Expand All @@ -63,18 +40,15 @@ object Config {

}

/**
* Load configuration [values] from the file at [configPath]
*/
fun loadConfig() {
val loader = HoconConfigurationLoader.builder()
.path(configPath)
.path(configFilePath)
.build()
try {
val root = loader.load()
val configVersion = root.node("version").string!!
if (version != configVersion) {
FabriZoom.logger.warn("Found config version: $configVersion, current version: $version")
if (VERSION != configVersion) {
FabriZoom.logger.warn("Found config version: $configVersion, current version: $VERSION")
FabriZoom.logger.warn("Attempting to load anyway")
}

Expand All @@ -91,11 +65,6 @@ object Config {
applyDefaultConfig()
}

/**
* Apply the default preset config [values]
*
* @see Presets.DEFAULT
*/
fun applyDefaultConfig() {
values = Presets.DEFAULT.values!!.copy()
}
Expand Down
Loading

0 comments on commit 7246d5a

Please sign in to comment.