fork/GramFirmware
git clone https://git.julia.blue/fork/GramFirmware
818d962
gram
1#include "core/mode_selection.hpp" 2 3#include "core/state.hpp" 4#include "modes/CustomControllerMode.hpp" 5#include "modes/CustomKeyboardMode.hpp" 6#include "modes/FgcMode.hpp" 7#include "modes/Melee20Button.hpp" 8#include "modes/ProjectM.hpp" 9#include "modes/RivalsOfAether.hpp" 10#include "modes/Ultimate.hpp" 11#include "util/state_util.hpp" 12 13#include <config.pb.h> 14 15Melee20Button melee_mode ; 16ProjectM projectm_mode ; 17Ultimate ultimate_mode ; 18FgcMode fgc_mode ; 19RivalsOfAether rivals_mode ; 20CustomKeyboardMode keyboard_mode ; 21CustomControllerMode custom_mode ; 22 23uint64_t mode_activation_masks [10 ]; 24 25size_t current_mode_index = SIZE_MAX ; 26 27void set_mode (CommunicationBackend * backend ,ControllerMode * mode ) { 28// Delete keyboard mode in case one is set, so we don't end up getting both controller and 29// keyboard inputs. 30current_kb_mode = nullptr ; 31 32// Set new controller mode. 33backend -> SetGameMode (mode ); 34} 35 36void set_mode (CommunicationBackend * backend ,KeyboardMode * mode ) { 37// Only DInputBackend supports keyboard modes. 38if (backend -> BackendId ()!= COMMS_BACKEND_DINPUT ) { 39return ; 40 } 41 42// Delete and reassign current keyboard mode. 43current_kb_mode = mode ; 44 45// Unset the current controller mode so backend only gives neutral inputs. 46backend -> SetGameMode (mode ); 47} 48 49void set_mode (CommunicationBackend * backend ,GameModeConfig & mode_config ,Config & config ) { 50switch (mode_config .mode_id ) { 51case MODE_MELEE : 52melee_mode .SetConfig (mode_config ,config .melee_options ); 53set_mode (backend ,& melee_mode ); 54break ; 55case MODE_PROJECT_M : 56projectm_mode .SetConfig (mode_config ,config .project_m_options ); 57set_mode (backend ,& projectm_mode ); 58break ; 59case MODE_ULTIMATE : 60ultimate_mode .SetConfig (mode_config ); 61set_mode (backend ,& ultimate_mode ); 62break ; 63case MODE_FGC : 64fgc_mode .SetConfig (mode_config ); 65set_mode (backend ,& fgc_mode ); 66break ; 67case MODE_RIVALS_OF_AETHER : 68rivals_mode .SetConfig (mode_config ); 69set_mode (backend ,& rivals_mode ); 70break ; 71case MODE_KEYBOARD : 72if (backend -> BackendId ()!= COMMS_BACKEND_DINPUT || 73mode_config .keyboard_mode_config < 1 || 74mode_config .keyboard_mode_config > config .keyboard_modes_count ) { 75break ; 76 } 77keyboard_mode .SetConfig ( 78mode_config , 79config .keyboard_modes [mode_config .keyboard_mode_config - 1 ] 80 ); 81set_mode (backend ,& keyboard_mode ); 82break ; 83case MODE_CUSTOM : 84if (mode_config .custom_mode_config < 1 || 85mode_config .custom_mode_config > config .custom_modes_count ) { 86break ; 87 } 88custom_mode .SetConfig ( 89mode_config , 90config .custom_modes [mode_config .custom_mode_config - 1 ] 91 ); 92set_mode (backend ,& custom_mode ); 93break ; 94case MODE_UNSPECIFIED : 95default : 96break ; 97 } 98} 99 100// TODO: Maybe remove this overload in favour of looking up the gamemode outside of here using a 101// config_utils function. 102void set_mode (CommunicationBackend * backend ,GameModeId mode_id ,Config & config ) { 103// In this overload we only know the mode id so we need to find a mode config that matches this 104// ID. 105for (size_t i = 0 ;i < config .game_mode_configs_count ;i ++ ) { 106GameModeConfig & mode = config .game_mode_configs [i ]; 107if (mode .mode_id == mode_id ) { 108set_mode (backend ,mode ,config ); 109return ; 110 } 111 } 112} 113 114void select_mode (CommunicationBackend ** backends ,size_t backends_count ,Config & config ) { 115// TODO: Use a counter variable to only run the contents of this function every x iterations 116// rather than on every single poll. 117 118InputState & inputs = backends [0 ]-> GetInputs (); 119 120for (size_t i = 0 ;i < config .game_mode_configs_count ;i ++ ) { 121GameModeConfig & mode_config = config .game_mode_configs [i ]; 122if (all_buttons_held (inputs .buttons ,mode_activation_masks [i ])&& i != current_mode_index ) { 123current_mode_index = i ; 124for (size_t i = 0 ;i < backends_count ;i ++ ) { 125set_mode (backends [i ],mode_config ,config ); 126 } 127return ; 128 } 129 } 130} 131 132void setup_mode_activation_bindings (const GameModeConfig * mode_configs ,size_t mode_configs_count ) { 133// Build bit masks for checking for matching button holds. 134for (size_t i = 0 ;i < mode_configs_count ;i ++ ) { 135mode_activation_masks [i ]= make_button_mask ( 136mode_configs [i ].activation_binding , 137mode_configs [i ].activation_binding_count 138 ); 139 } 140}