64 lines
2.2 KiB
Java
64 lines
2.2 KiB
Java
package wtf.hak.survivalfabric.features;
|
|
|
|
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
|
|
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
|
|
import net.minecraft.client.MinecraftClient;
|
|
import net.minecraft.client.option.KeyBinding;
|
|
import net.minecraft.client.util.InputUtil;
|
|
import net.minecraft.text.Text;
|
|
import org.lwjgl.glfw.GLFW;
|
|
import org.spongepowered.asm.mixin.Unique;
|
|
|
|
import static wtf.hak.survivalfabric.config.client.ClientConfigManager.getConfig;
|
|
|
|
public class Zoom {
|
|
|
|
private static final KeyBinding ZOOM_BIND = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.survivalfabric.zoom", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_C, "category.survivalfabric.survivalfabric"));
|
|
|
|
private static boolean SHOULD_ZOOM = false;
|
|
private static int ZOOM_STEP = 0;
|
|
|
|
@Unique
|
|
private static boolean initialSmoothZoom;
|
|
|
|
public static void register() {
|
|
ClientTickEvents.END_CLIENT_TICK.register(client -> {
|
|
|
|
if (ZOOM_BIND.isPressed() && !SHOULD_ZOOM) {
|
|
if(getConfig().smoothCamera) {
|
|
initialSmoothZoom = MinecraftClient.getInstance().options.smoothCameraEnabled;
|
|
MinecraftClient.getInstance().options.smoothCameraEnabled = true;
|
|
}
|
|
SHOULD_ZOOM = true;
|
|
} else if (!ZOOM_BIND.isPressed() && SHOULD_ZOOM) {
|
|
SHOULD_ZOOM = false;
|
|
ZOOM_STEP = 0;
|
|
if(getConfig().smoothCamera) {
|
|
MinecraftClient.getInstance().options.smoothCameraEnabled = initialSmoothZoom;
|
|
}
|
|
}
|
|
|
|
});
|
|
}
|
|
|
|
public static boolean isZooming() {
|
|
return SHOULD_ZOOM;
|
|
}
|
|
|
|
public static float getZoomFov() {
|
|
return getConfig().initialZoom - -ZOOM_STEP * getConfig().zoomStep;
|
|
}
|
|
|
|
public static void modifyStep(int step) {
|
|
ZOOM_STEP += step;
|
|
|
|
// Clamp the zoom level so the FOV stays within [1, 110]
|
|
float zoomFov = getZoomFov();
|
|
if (zoomFov < 1) {
|
|
ZOOM_STEP = Math.round((1 - getConfig().initialZoom) / getConfig().zoomStep) + 1;
|
|
} else if (zoomFov > 110) {
|
|
ZOOM_STEP = Math.round((110 - getConfig().initialZoom) / getConfig().zoomStep);
|
|
}
|
|
}
|
|
}
|