fork/GramFirmware
git clone https://git.julia.blue/fork/GramFirmware
818d962
gram
1#include "modes/CustomControllerMode.hpp" 2 3#include "util/state_util.hpp" 4 5#define SIGNUM (x ) ((x > 0) - (x < 0)) 6#define ANALOG_STICK_NEUTRAL 128 7 8CustomControllerMode ::CustomControllerMode () :ControllerMode () {} 9 10void CustomControllerMode ::SetConfig ( 11GameModeConfig & config , 12const CustomModeConfig & custom_mode_config 13) { 14InputMode ::SetConfig (config ); 15_custom_mode_config = & custom_mode_config ; 16for (size_t i = 0 ;i < custom_mode_config .modifiers_count ;i ++ ) { 17_modifier_button_masks [i ]= make_button_mask ( 18custom_mode_config .modifiers [i ].buttons , 19custom_mode_config .modifiers [i ].buttons_count 20 ); 21 } 22for (size_t i = 0 ;i < custom_mode_config .button_combo_mappings_count ;i ++ ) { 23_button_combo_mappings_masks [i ]= make_button_mask ( 24custom_mode_config .button_combo_mappings [i ].buttons , 25custom_mode_config .button_combo_mappings [i ].buttons_count 26 ); 27 } 28} 29 30void CustomControllerMode ::UpdateDigitalOutputs (const InputState & inputs ,OutputState & outputs ) { 31if (_custom_mode_config == nullptr ) { 32return ; 33 } 34 35// First check for button combo -> single output mappings, and lock out the normal function of 36// the buttons in any buttons combos that are activated. 37_buttons_to_ignore = 0 ; 38const ButtonComboMapping * button_combo_mappings = _custom_mode_config -> button_combo_mappings ; 39for (size_t i = 0 ;i < _custom_mode_config -> button_combo_mappings_count ;i ++ ) { 40const ButtonComboMapping & button_combo_mapping = button_combo_mappings [i ]; 41if (!all_buttons_held (inputs .buttons ,_button_combo_mappings_masks [i ])) { 42continue ; 43 } 44 45set_output (outputs .buttons ,button_combo_mapping .digital_output , true); 46_buttons_to_ignore |=_button_combo_mappings_masks [i ]; 47 } 48_filtered_buttons = inputs .buttons & ~_buttons_to_ignore ; 49 50for (size_t output = 0 ;output < _custom_mode_config -> digital_button_mappings_count ;output ++ ) { 51Button input = _custom_mode_config -> digital_button_mappings [output ]; 52set_output ( 53outputs .buttons , 54 (DigitalOutput )(output + 1 ), 55get_button (_filtered_buttons ,input ) 56 ); 57 } 58 59if (inputs .nunchuk_connected ) { 60outputs .triggerLDigital |=inputs .nunchuk_z ; 61 } 62} 63 64void CustomControllerMode ::UpdateAnalogOutputs (const InputState & inputs ,OutputState & outputs ) { 65if (_custom_mode_config == nullptr ) { 66return ; 67 } 68 69const Button * direction_buttons = _custom_mode_config -> stick_direction_mappings ; 70uint8_t stick_range = _custom_mode_config -> stick_range ; 71UpdateDirections ( 72get_button (_filtered_buttons ,GetDirectionButton (direction_buttons ,SD_LSTICK_LEFT )), 73get_button (_filtered_buttons ,GetDirectionButton (direction_buttons ,SD_LSTICK_RIGHT )), 74get_button (_filtered_buttons ,GetDirectionButton (direction_buttons ,SD_LSTICK_DOWN )), 75get_button (_filtered_buttons ,GetDirectionButton (direction_buttons ,SD_LSTICK_UP )), 76get_button (_filtered_buttons ,GetDirectionButton (direction_buttons ,SD_RSTICK_LEFT )), 77get_button (_filtered_buttons ,GetDirectionButton (direction_buttons ,SD_RSTICK_RIGHT )), 78get_button (_filtered_buttons ,GetDirectionButton (direction_buttons ,SD_RSTICK_DOWN )), 79get_button (_filtered_buttons ,GetDirectionButton (direction_buttons ,SD_RSTICK_UP )), 80ANALOG_STICK_NEUTRAL - stick_range , 81ANALOG_STICK_NEUTRAL , 82ANALOG_STICK_NEUTRAL + stick_range , 83outputs 84 ); 85 86const AnalogModifier * modifiers = _custom_mode_config -> modifiers ; 87for (size_t i = 0 ;i < _custom_mode_config -> modifiers_count ;i ++ ) { 88const AnalogModifier & modifier = modifiers [i ]; 89if (modifier .axis == AXIS_UNSPECIFIED || modifier .axis > _AnalogAxis_MAX ) { 90continue ; 91 } 92if (!all_buttons_held (_filtered_buttons ,_modifier_button_masks [i ])) { 93continue ; 94 } 95 96uint8_t OutputState ::* axis = axis_pointer (modifier .axis ); 97if (axis != nullptr ) { 98int8_t sign = 0 ; 99switch (modifier .combination_mode ) { 100case COMBINATION_MODE_OVERRIDE : 101sign = SIGNUM (outputs .*axis ); 102outputs .*axis = ANALOG_STICK_NEUTRAL + 103_custom_mode_config -> stick_range * modifier .multiplier * sign ; 104break ; 105case COMBINATION_MODE_COMPOUND : 106case COMBINATION_MODE_UNSPECIFIED : 107default : 108outputs .*axis = ANALOG_STICK_NEUTRAL + 109 (outputs .*axis - ANALOG_STICK_NEUTRAL )* modifier .multiplier ; 110break ; 111 } 112 } 113 } 114 115const AnalogTriggerMapping * analog_trigger_mappings = 116_custom_mode_config -> analog_trigger_mappings ; 117for (size_t i = 0 ;i < _custom_mode_config -> analog_trigger_mappings_count ;i ++ ) { 118const AnalogTriggerMapping & mapping = analog_trigger_mappings [i ]; 119if (get_button (_filtered_buttons ,mapping .button )) { 120switch (mapping .trigger ) { 121case TRIGGER_LT : 122outputs .triggerLAnalog = mapping .value ; 123break ; 124case TRIGGER_RT : 125outputs .triggerRAnalog = mapping .value ; 126break ; 127default : 128break ; 129 } 130 } 131 } 132 133if (outputs .triggerLDigital ) { 134outputs .triggerLAnalog = 255 ; 135 } 136if (outputs .triggerRDigital ) { 137outputs .triggerRAnalog = 255 ; 138 } 139 140// Nunchuk overrides left stick. 141if (inputs .nunchuk_connected ) { 142outputs .leftStickX = inputs .nunchuk_x ; 143outputs .leftStickY = inputs .nunchuk_y ; 144 } 145} 146 147Button CustomControllerMode ::GetDirectionButton ( 148const Button * direction_buttons , 149StickDirectionButton direction 150) { 151if (direction == SD_UNSPECIFIED || direction > _StickDirectionButton_MAX ) { 152return BTN_UNSPECIFIED ; 153 } 154return direction_buttons [direction - 1 ]; 155}