<commit_before>#include "photon_core.h"

namespace photon{

namespace input{

glm::vec2 WindowToWorldCoord(photon_instance &instance, int x, int y){
    float widthfac = instance.window.width / 2.0f;
    float heightfac = instance.window.height / 2.0f;

    glm::vec2 location((x - widthfac), (heightfac - y));
    location /= std::min(widthfac, heightfac);
    location *= instance.camera_offset.z;
    location.x += instance.player.location.x + instance.camera_offset.x;
    location.y += instance.player.location.y + instance.camera_offset.y;

    return location;
}

void DoInputSingle(photon_input_state &state, photon_input &input){
    state.last_state = state.current_state;
    switch(state.type){
    case photon_input_state::keyboard:{
        Uint8* keyboard = SDL_GetKeyboardState(nullptr);
        SDL_Keymod modifiers = SDL_GetModState();
        state.current_state = keyboard[state.key] && (modifiers == state.modifiers || modifiers & state.modifiers);
        break;
    }
    case photon_input_state::joystick_axis:{
        if(state.joystick_input_index > -1){
            state.current_state = SDL_JoystickGetAxis(input.joystick, state.joystick_input_index) / 32768.0f;
            if(state.axis_input_negate){
                state.current_state = -state.current_state;
            }
        }
        if(state.current_state < input.deadzone){
            state.current_state = 0.0f;
        }
        break;
    }
    case photon_input_state::joystick_button:{
        state.current_state = SDL_JoystickGetButton(input.joystick, state.joystick_input_index);
        break;
    }
    case photon_input_state::gamecontroller_axis:{
        state.current_state = SDL_GameControllerGetAxis(input.controller, state.controller_axis) / 32768.0f;
        if(state.axis_input_negate){
            state.current_state = -state.current_state;
        }
        if(state.current_state < input.deadzone){
            state.current_state = 0.0f;
        }
        break;
    }
    case photon_input_state::gamecontroller_button:{
        state.current_state = SDL_GameControllerGetButton(input.controller, state.controller_button);
        break;
    }
    case photon_input_state::none:
        break;
    }
}

bool IsActivated(photon_input_state &state){
    return state.current_state > 0.9f && state.last_state < 0.9f;
}

void DoInput(photon_instance &instance, float time){
    photon_input &input = instance.input;

    DoInputSingle(input.left, input);
    DoInputSingle(input.right, input);
    DoInputSingle(input.up, input);
    DoInputSingle(input.down, input);
    DoInputSingle(input.select, input);
    DoInputSingle(input.back, input);

    DoInputSingle(input.pause, input);

    DoInputSingle(input.interact, input);
    DoInputSingle(input.move_right, input);
    DoInputSingle(input.move_left, input);
    DoInputSingle(input.move_up, input);
    DoInputSingle(input.move_down, input);
    DoInputSingle(input.camera_right, input);
    DoInputSingle(input.camera_left, input);
    DoInputSingle(input.camera_up, input);
    DoInputSingle(input.camera_down, input);
    DoInputSingle(input.rotate_clockwise, input);
    DoInputSingle(input.rotate_counter_clockwise, input);
    DoInputSingle(input.zoom_in, input);
    DoInputSingle(input.zoom_out, input);
    DoInputSingle(input.next_item, input);
    DoInputSingle(input.previous_item, input);

    if(instance.gui.load_save_menu.loading || instance.gui.load_save_menu.saving){
        photon_gui_load_save_menu &load_save_menu = instance.gui.load_save_menu;
        if(IsActivated(input.select)){
            gui::ConfirmLoadSave(instance);
        }
        if(IsActivated(input.back)){
            gui::CancelLoadSave(instance);
        }
        if(IsActivated(input.left)){
            if(--load_save_menu.cursor < 0){
                load_save_menu.cursor = 0;
            }
        }
        if(IsActivated(input.right)){
            if(++load_save_menu.cursor > load_save_menu.filename.length()){
                load_save_menu.cursor = load_save_menu.filename.length();
            }
        }
        if(IsActivated(input.up)){
            if(--load_save_menu.current_file_index < 0){
                load_save_menu.current_file_index = load_save_menu.file_list.size() - 1;
            }
            load_save_menu.filename = load_save_menu.file_list[load_save_menu.current_file_index];
        }
        if(IsActivated(input.down)){
            if(++load_save_menu.current_file_index >= load_save_menu.file_list.size()){
                load_save_menu.current_file_index = 0;
            }
            load_save_menu.filename = load_save_menu.file_list[load_save_menu.current_file_index];
        }

    }else if(!instance.level.is_valid){
        photon_gui_button_list &menu = instance.gui.main_menu;
        if(IsActivated(input.select)){
            gui::ActivateButton(instance, menu, menu.highlighted);
        }
        if(IsActivated(input.up)){
            if(--menu.highlighted < 0){
                menu.highlighted = menu.buttons.size() - 1;
            }
        }
        if(IsActivated(input.down)){
            if(++menu.highlighted >= menu.buttons.size()){
                menu.highlighted = 0;
            }
        }
    }else if(instance.paused){
        photon_gui_button_list &menu = instance.gui.pause_menu;
        if(IsActivated(input.select)){
            gui::ActivateButton(instance, menu, menu.highlighted);
        }
        if(IsActivated(input.back) || IsActivated(input.pause)){
            instance.paused = false;
        }
        if(IsActivated(input.up)){
            if(--menu.highlighted < 0){
                menu.highlighted = menu.buttons.size() - 1;
            }
        }
        if(IsActivated(input.down)){
            if(++menu.highlighted >= menu.buttons.size()){
                menu.highlighted = 0;
            }
        }
    }else{
        instance.player.location.x += (input.move_right.current_state - input.move_left.current_state) * time * instance.camera_offset.z;
        instance.player.location.y += (input.move_up.current_state - input.move_down.current_state) * time * instance.camera_offset.z;

        glm::vec2 cam_offset(input.camera_right.current_state - input.camera_left.current_state,
                             input.camera_up.current_state - input.camera_down.current_state);

        // TODO - make a setting that enables/disables screen edges.
        // if only SDL_GetMouseState worked when the cursor is outside the window... oh well...
        if(SDL_GetMouseFocus() == instance.window.window_SDL){
            glm::ivec2 mouse;
            uint32_t buttons = SDL_GetMouseState(&mouse.x, &mouse.y);

            if(instance.settings.screen_edges){
                cam_offset.x += (std::max(80 + mouse.x - int(instance.window.width),  0) - std::max(80 - mouse.x, 0)) / 80.0f;
                cam_offset.y += (std::max(80 - mouse.y, 0) - std::max(80 + mouse.y - int(instance.window.height), 0)) / 80.0f;
            }

            if(input.enable_mouse){
                glm::vec2 delta = WindowToWorldCoord(instance, mouse.x, mouse.y) - instance.player.location;
                if(buttons & SDL_BUTTON_LMASK){
                    if(glm::length(delta) > 0.5f){
                        instance.player.location += delta * time;
                    }
                }
                if(buttons & SDL_BUTTON_RMASK){
                    float angle = glm::degrees(glm::atan(delta.y, delta.x));

                    blocks::OnRotate(glm::uvec2(instance.player.location + 0.5f), instance.level, angle);
                }
            }
        }

        cam_offset *= instance.camera_offset.z * 0.5f * time * 2.0f;<commit_msg>fixed top left screen edges always being activated on startup no matter where the cursor is.<commit_after>#include "photon_core.h"

namespace photon{

namespace input{

glm::vec2 WindowToWorldCoord(photon_instance &instance, int x, int y){
    float widthfac = instance.window.width / 2.0f;
    float heightfac = instance.window.height / 2.0f;

    glm::vec2 location((x - widthfac), (heightfac - y));
    location /= std::min(widthfac, heightfac);
    location *= instance.camera_offset.z;
    location.x += instance.player.location.x + instance.camera_offset.x;
    location.y += instance.player.location.y + instance.camera_offset.y;

    return location;
}

void DoInputSingle(photon_input_state &state, photon_input &input){
    state.last_state = state.current_state;
    switch(state.type){
    case photon_input_state::keyboard:{
        Uint8* keyboard = SDL_GetKeyboardState(nullptr);
        SDL_Keymod modifiers = SDL_GetModState();
        state.current_state = keyboard[state.key] && (modifiers == state.modifiers || modifiers & state.modifiers);
        break;
    }
    case photon_input_state::joystick_axis:{
        if(state.joystick_input_index > -1){
            state.current_state = SDL_JoystickGetAxis(input.joystick, state.joystick_input_index) / 32768.0f;
            if(state.axis_input_negate){
                state.current_state = -state.current_state;
            }
        }
        if(state.current_state < input.deadzone){
            state.current_state = 0.0f;
        }
        break;
    }
    case photon_input_state::joystick_button:{
        state.current_state = SDL_JoystickGetButton(input.joystick, state.joystick_input_index);
        break;
    }
    case photon_input_state::gamecontroller_axis:{
        state.current_state = SDL_GameControllerGetAxis(input.controller, state.controller_axis) / 32768.0f;
        if(state.axis_input_negate){
            state.current_state = -state.current_state;
        }
        if(state.current_state < input.deadzone){
            state.current_state = 0.0f;
        }
        break;
    }
    case photon_input_state::gamecontroller_button:{
        state.current_state = SDL_GameControllerGetButton(input.controller, state.controller_button);
        break;
    }
    case photon_input_state::none:
        break;
    }
}

bool IsActivated(photon_input_state &state){
    return state.current_state > 0.9f && state.last_state < 0.9f;
}

void DoInput(photon_instance &instance, float time){
    photon_input &input = instance.input;

    DoInputSingle(input.left, input);
    DoInputSingle(input.right, input);
    DoInputSingle(input.up, input);
    DoInputSingle(input.down, input);
    DoInputSingle(input.select, input);
    DoInputSingle(input.back, input);

    DoInputSingle(input.pause, input);

    DoInputSingle(input.interact, input);
    DoInputSingle(input.move_right, input);
    DoInputSingle(input.move_left, input);
    DoInputSingle(input.move_up, input);
    DoInputSingle(input.move_down, input);
    DoInputSingle(input.camera_right, input);
    DoInputSingle(input.camera_left, input);
    DoInputSingle(input.camera_up, input);
    DoInputSingle(input.camera_down, input);
    DoInputSingle(input.rotate_clockwise, input);
    DoInputSingle(input.rotate_counter_clockwise, input);
    DoInputSingle(input.zoom_in, input);
    DoInputSingle(input.zoom_out, input);
    DoInputSingle(input.next_item, input);
    DoInputSingle(input.previous_item, input);

    if(instance.gui.load_save_menu.loading || instance.gui.load_save_menu.saving){
        photon_gui_load_save_menu &load_save_menu = instance.gui.load_save_menu;
        if(IsActivated(input.select)){
            gui::ConfirmLoadSave(instance);
        }
        if(IsActivated(input.back)){
            gui::CancelLoadSave(instance);
        }
        if(IsActivated(input.left)){
            if(--load_save_menu.cursor < 0){
                load_save_menu.cursor = 0;
            }
        }
        if(IsActivated(input.right)){
            if(++load_save_menu.cursor > load_save_menu.filename.length()){
                load_save_menu.cursor = load_save_menu.filename.length();
            }
        }
        if(IsActivated(input.up)){
            if(--load_save_menu.current_file_index < 0){
                load_save_menu.current_file_index = load_save_menu.file_list.size() - 1;
            }
            load_save_menu.filename = load_save_menu.file_list[load_save_menu.current_file_index];
        }
        if(IsActivated(input.down)){
            if(++load_save_menu.current_file_index >= load_save_menu.file_list.size()){
                load_save_menu.current_file_index = 0;
            }
            load_save_menu.filename = load_save_menu.file_list[load_save_menu.current_file_index];
        }

    }else if(!instance.level.is_valid){
        photon_gui_button_list &menu = instance.gui.main_menu;
        if(IsActivated(input.select)){
            gui::ActivateButton(instance, menu, menu.highlighted);
        }
        if(IsActivated(input.up)){
            if(--menu.highlighted < 0){
                menu.highlighted = menu.buttons.size() - 1;
            }
        }
        if(IsActivated(input.down)){
            if(++menu.highlighted >= menu.buttons.size()){
                menu.highlighted = 0;
            }
        }
    }else if(instance.paused){
        photon_gui_button_list &menu = instance.gui.pause_menu;
        if(IsActivated(input.select)){
            gui::ActivateButton(instance, menu, menu.highlighted);
        }
        if(IsActivated(input.back) || IsActivated(input.pause)){
            instance.paused = false;
        }
        if(IsActivated(input.up)){
            if(--menu.highlighted < 0){
                menu.highlighted = menu.buttons.size() - 1;
            }
        }
        if(IsActivated(input.down)){
            if(++menu.highlighted >= menu.buttons.size()){
                menu.highlighted = 0;
            }
        }
    }else{
        instance.player.location.x += (input.move_right.current_state - input.move_left.current_state) * time * instance.camera_offset.z;
        instance.player.location.y += (input.move_up.current_state - input.move_down.current_state) * time * instance.camera_offset.z;

        glm::vec2 cam_offset(input.camera_right.current_state - input.camera_left.current_state,
                             input.camera_up.current_state - input.camera_down.current_state);

        // TODO - make a setting that enables/disables screen edges.
        // if only SDL_GetMouseState worked when the cursor is outside the window... oh well...
        if(SDL_GetMouseFocus() == instance.window.window_SDL){
            glm::ivec2 mouse;
            uint32_t buttons = SDL_GetMouseState(&mouse.x, &mouse.y);

            // if the mouse position is (0,0) chances are it hasn't recieved the first event yet... I should try to find a better way to fix this, but this will do for now...
            if(instance.settings.screen_edges && !(mouse == glm::ivec2(0))){
                cam_offset.x += (std::max(80 + mouse.x - int(instance.window.width),  0) - std::max(80 - mouse.x, 0)) / 80.0f;
                cam_offset.y += (std::max(80 - mouse.y, 0) - std::max(80 + mouse.y - int(instance.window.height), 0)) / 80.0f;
            }

            if(input.enable_mouse){
                glm::vec2 delta = WindowToWorldCoord(instance, mouse.x, mouse.y) - instance.player.location;
                if(buttons & SDL_BUTTON_LMASK){
                    if(glm::length(delta) > 0.5f){
                        instance.player.location += delta * time;
                    }
                }
                if(buttons & SDL_BUTTON_RMASK){
                    float angle = glm::degrees(glm::atan(delta.y, delta.x));

                    blocks::OnRotate(glm::uvec2(instance.player.location + 0.5f), instance.level, angle);
                }
            }
        }

        cam_offset *= instance.camera_offset.z * 0.5f * time * 2.0f;
{'fraction_non_alphanumeric': 0.11706539074960128, 'fraction_numerical': 0.009314194577352473, 'mean_word_length': 2.4574327304808117, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5857039', 'n_tokens_mistral': 4999, 'n_tokens_neox': 4741, 'n_words': 724}