| 109 | } |
| 110 | |
| 111 | void Input::Update() { |
| 112 | wait_input = false; // clear each frame |
| 113 | |
| 114 | source->Update(); |
| 115 | auto& pressed_buttons = source->GetPressedButtons(); |
| 116 | |
| 117 | // Check button states |
| 118 | for (unsigned i = 0; i < BUTTON_COUNT; ++i) { |
| 119 | bool pressed = pressed_buttons[i]; |
| 120 | UpdateButton(i, pressed); |
| 121 | } |
| 122 | |
| 123 | auto& directions = source->GetDirectionMappings(); |
| 124 | |
| 125 | // Press time for directional buttons, the less they have been pressed, the higher their priority will be |
| 126 | int dirpress[Direction::NUM_DIRECTIONS] = {}; |
| 127 | |
| 128 | // Get max pressed time for each directional button |
| 129 | for (auto& dm: directions) { |
| 130 | if (dirpress[dm.first] < press_time[dm.second]) { |
| 131 | dirpress[dm.first] = press_time[dm.second]; |
| 132 | }; |
| 133 | } |
| 134 | |
| 135 | // Calculate diagonal directions pressed time by dir4 combinations |
| 136 | dirpress[Direction::DOWNLEFT] += (dirpress[Direction::DOWN] > 0 && dirpress[Direction::LEFT] > 0) ? dirpress[Direction::DOWN] + dirpress[Direction::LEFT] : 0; |
| 137 | dirpress[Direction::DOWNRIGHT] += (dirpress[Direction::DOWN] > 0 && dirpress[Direction::RIGHT] > 0) ? dirpress[Direction::DOWN] + dirpress[Direction::RIGHT] : 0; |
| 138 | dirpress[Direction::UPLEFT] += (dirpress[Direction::UP] > 0 && dirpress[Direction::LEFT] > 0) ? dirpress[Direction::UP] + dirpress[Direction::LEFT] : 0; |
| 139 | dirpress[Direction::UPRIGHT] += (dirpress[Direction::UP] > 0 && dirpress[Direction::RIGHT] > 0) ? dirpress[Direction::UP] + dirpress[Direction::RIGHT] : 0; |
| 140 | |
| 141 | dir4 = Direction::NONE; |
| 142 | dir8 = Direction::NONE; |
| 143 | |
| 144 | // Check if no opposed keys are being pressed at the same time |
| 145 | if (!(dirpress[Direction::DOWN] > 0 && dirpress[Direction::UP] > 0) && !(dirpress[Direction::LEFT] > 0 && dirpress[Direction::RIGHT] > 0)) { |
| 146 | |
| 147 | // Get dir4 by the with lowest press time (besides 0 frames) |
| 148 | int min_press_time = 0; |
| 149 | for (int i = 2; i <= 8; i += 2) { |
| 150 | if (dirpress[i] > 0) { |
| 151 | if (min_press_time == 0 || dirpress[i] < min_press_time) { |
| 152 | dir4 = i; |
| 153 | min_press_time = dirpress[i]; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // Dir8 will be at least equal to Dir4 |
| 159 | dir8 = dir4; |
| 160 | |
| 161 | // Check diagonal directions (There is a priority order) |
| 162 | if (dirpress[Direction::UPRIGHT] > 0) dir8 = Direction::UPRIGHT; |
| 163 | else if (dirpress[Direction::UPLEFT] > 0) dir8 = Direction::UPLEFT; |
| 164 | else if (dirpress[Direction::DOWNRIGHT] > 0) dir8 = Direction::DOWNRIGHT; |
| 165 | else if (dirpress[Direction::DOWNLEFT] > 0) dir8 = Direction::DOWNLEFT; |
| 166 | } |
| 167 | |
| 168 | // Determine pressed & released keys from raw keystate |
nothing calls this directly
no test coverage detected