/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   key_change_scene.c                                 :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: bszilas <bszilas@student.42vienna.com>     +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/11/26 18:12:48 by vvobis            #+#    #+#             */
/*   Updated: 2025/05/30 12:44:03 by victor           ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "../../minirt.h"

uint    key_change_res_minus(t_scene *scene)
{
    if (scene->resolution_x == 1 && scene->resolution_y == 1)
        return (scene->anti_aliasing = ANTI_ALIASING_FACTOR, 1);
    if (scene->resolution_x == RESOLUTION_SCALE_X \
    && scene->resolution_y == RESOLUTION_SCALE_Y)
        return (scene->resolution_x = 1, scene->resolution_y = 1, 1);
    if (scene->resolution_x > 1 && scene->resolution_y > 1)
        return (scene->resolution_x /= 2, scene->resolution_y /= 2, 1);
    return (1);
}

uint    key_change_res(int keycode, t_scene *scene)
{
    if (keycode == KEY_MINUS)
    {
        if (scene->anti_aliasing)
            return (scene->anti_aliasing = false, 1);
        if (scene->resolution_x == 1 && scene->resolution_y == 1)
            return (scene->resolution_x = RESOLUTION_SCALE_X, \
                    scene->resolution_y = RESOLUTION_SCALE_Y, 1);
        if (scene->resolution_x >= RESOLUTION_SCALE_X * \
                SCENE_START_RESOLUTION_CAP || scene->resolution_y >= \
                RESOLUTION_SCALE_Y * SCENE_START_RESOLUTION_CAP)
            return (false);
        scene->resolution_x *= 2;
        scene->resolution_y *= 2;
    }
    else if (keycode == KEY_EQUAL)
        key_change_res_minus(scene);
    else if (keycode == KEY_ENTER)
        return (scene->resolution_x = scene->resolution_y = 1, 1);
    else if (keycode == KEY_BACKSPACE)
        return (scene->camera.position = (t_vector){0, 0, 0}, \
                scene->camera.fov = 1, 1);
    return (false);
}

bool    key_move_focused(int keycode, t_vector *focus)
{
    if (keycode == KEY_D)
        return (focus->x += 0.1, true);
    else if (keycode == KEY_A)
        return (focus->x -= 0.1, true);
    else if (keycode == KEY_E)
        return (focus->y += 0.1, true);
    else if (keycode == KEY_Q)
        return (focus->y -= 0.1, true);
    else if (keycode == KEY_W)
        return (focus->z += .2, true);
    else if (keycode == KEY_S)
        return (focus->z -= .2, true);
    return (false);
}

bool    key_change_fov(int keycode, t_camera *camera)
{
    if (keycode == KEY_SLASH)
    {
        if (camera->fov < 175)
        {
            camera->fov += 5;
        }
        return (camera->fov_f = tan(camera->fov / 2 * M_PI / 180));
    }
    else if (keycode == KEY_PERIOD)
    {
        if (camera->fov > 5)
        {
            camera->fov -= 5;
        }
        return (camera->fov_f = tan(camera->fov / 2 * M_PI / 180));
    }
    return (false);
}
