Applied coding style fixes

This commit is contained in:
kalaposfos13 2024-10-12 17:28:52 +02:00
parent 3dda62fcba
commit 98200221f5
3 changed files with 136 additions and 135 deletions

View File

@ -4,18 +4,17 @@
// new includes
#include <map>
// #include <unordered_map>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <SDL3/SDL_events.h>
#include <SDL3/SDL_init.h>
#include <SDL3/SDL_properties.h>
#include <SDL3/SDL_video.h>
#include <SDL3/SDL_timer.h>
#include <SDL3/SDL_video.h>
#include "common/assert.h"
#include "common/config.h"
#include "common/version.h"
@ -38,8 +37,10 @@ bool KeyBinding::operator<(const KeyBinding& other) const {
// modifiers are bitwise or-d together, so we need to check if ours is in that
template <typename T>
typename std::map<KeyBinding, T>::const_iterator FindKeyAllowingPartialModifiers(const std::map<KeyBinding, T>& map, KeyBinding binding) {
for (typename std::map<KeyBinding, T>::const_iterator it = map.cbegin(); it != map.cend(); it++) {
typename std::map<KeyBinding, T>::const_iterator FindKeyAllowingPartialModifiers(
const std::map<KeyBinding, T>& map, KeyBinding binding) {
for (typename std::map<KeyBinding, T>::const_iterator it = map.cbegin(); it != map.cend();
it++) {
if ((it->first.key == binding.key) && (it->first.modifier & binding.modifier) != 0) {
return it;
}
@ -47,8 +48,10 @@ typename std::map<KeyBinding, T>::const_iterator FindKeyAllowingPartialModifiers
return map.end(); // Return end if no match is found
}
template <typename T>
typename std::map<KeyBinding, T>::const_iterator FindKeyAllowingOnlyNoModifiers(const std::map<KeyBinding, T>& map, KeyBinding binding) {
for (typename std::map<KeyBinding, T>::const_iterator it = map.cbegin(); it != map.cend(); it++) {
typename std::map<KeyBinding, T>::const_iterator FindKeyAllowingOnlyNoModifiers(
const std::map<KeyBinding, T>& map, KeyBinding binding) {
for (typename std::map<KeyBinding, T>::const_iterator it = map.cbegin(); it != map.cend();
it++) {
if (it->first.key == binding.key && it->first.modifier == SDL_KMOD_NONE) {
return it;
}
@ -62,7 +65,6 @@ struct AxisMapping {
int value; // Value to set for key press (+127 or -127 for movement)
};
// i strongly suggest you collapse these maps
std::map<std::string, u32> string_to_cbutton_map = {
{"triangle", OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_TRIANGLE},
@ -80,8 +82,7 @@ std::map<std::string, u32> string_to_cbutton_map = {
{"up", OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_UP},
{"down", OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_DOWN},
{"left", OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_LEFT},
{"right", OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_RIGHT}
};
{"right", OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_RIGHT}};
std::map<std::string, AxisMapping> string_to_axis_map = {
{"axis_left_x_plus", {Input::Axis::LeftX, 127}},
{"axis_left_x_minus", {Input::Axis::LeftX, -127}},
@ -167,25 +168,14 @@ std::map<std::string, u32> string_to_keyboard_key_map = {
{"middlebutton", SDL_BUTTON_MIDDLE},
};
std::map<std::string, u32> string_to_keyboard_mod_key_map = {
{"lshift", SDL_KMOD_LSHIFT},
{"rshift", SDL_KMOD_RSHIFT},
{"lctrl", SDL_KMOD_LCTRL},
{"rctrl", SDL_KMOD_RCTRL},
{"lalt", SDL_KMOD_LALT},
{"ralt", SDL_KMOD_RALT},
{"shift", SDL_KMOD_SHIFT},
{"ctrl", SDL_KMOD_CTRL},
{"alt", SDL_KMOD_ALT},
{"l_meta", SDL_KMOD_LGUI},
{"r_meta", SDL_KMOD_RGUI},
{"meta", SDL_KMOD_GUI},
{"lwin", SDL_KMOD_LGUI},
{"rwin", SDL_KMOD_RGUI},
{"win", SDL_KMOD_GUI},
{"lshift", SDL_KMOD_LSHIFT}, {"rshift", SDL_KMOD_RSHIFT}, {"lctrl", SDL_KMOD_LCTRL},
{"rctrl", SDL_KMOD_RCTRL}, {"lalt", SDL_KMOD_LALT}, {"ralt", SDL_KMOD_RALT},
{"shift", SDL_KMOD_SHIFT}, {"ctrl", SDL_KMOD_CTRL}, {"alt", SDL_KMOD_ALT},
{"l_meta", SDL_KMOD_LGUI}, {"r_meta", SDL_KMOD_RGUI}, {"meta", SDL_KMOD_GUI},
{"lwin", SDL_KMOD_LGUI}, {"rwin", SDL_KMOD_RGUI}, {"win", SDL_KMOD_GUI},
{"none", SDL_KMOD_NONE}, // if you want to be fancy
};
// Button map: maps key+modifier to controller button
std::map<KeyBinding, u32> button_map = {
/*
@ -286,7 +276,8 @@ void WindowSDL::parseInputConfig(const std::string& filename) {
auto key_it = string_to_keyboard_key_map.find(key);
auto mod_it = string_to_keyboard_mod_key_map.find(mod);
if (key_it != string_to_keyboard_key_map.end() && mod_it != string_to_keyboard_mod_key_map.end()) {
if (key_it != string_to_keyboard_key_map.end() &&
mod_it != string_to_keyboard_mod_key_map.end()) {
binding.key = key_it->second;
binding.modifier = mod_it->second;
} else {
@ -312,7 +303,8 @@ void WindowSDL::parseInputConfig(const std::string& filename) {
} else if (button_it != string_to_cbutton_map.end()) {
button_map[binding] = button_it->second;
} else {
std::cerr << "Syntax error while parsing controller inputs at line " << lineCount << "\n";
std::cerr << "Syntax error while parsing controller inputs at line " << lineCount
<< "\n";
continue; // skip
}
}
@ -351,9 +343,8 @@ void WindowSDL::updateMouse() {
}
}
WindowSDL::WindowSDL(s32 width_, s32 height_, Input::GameController* controller_, std::string_view window_title)
WindowSDL::WindowSDL(s32 width_, s32 height_, Input::GameController* controller_,
std::string_view window_title)
: width{width_}, height{height_}, controller{controller_} {
if (!SDL_Init(SDL_INIT_VIDEO)) {
UNREACHABLE_MSG("Failed to initialize SDL video subsystem: {}", SDL_GetError());
@ -443,7 +434,7 @@ void WindowSDL::waitEvent() {
case SDL_EVENT_KEY_UP:
case SDL_EVENT_MOUSE_BUTTON_UP:
case SDL_EVENT_MOUSE_BUTTON_DOWN:
SDL_AddTimer(33, keyRepeatCallback, (void*)payload_to_timer); // this is intentional passthrough
SDL_AddTimer(33, keyRepeatCallback, (void*)payload_to_timer);
onKeyPress(&event);
break;
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
@ -474,15 +465,16 @@ void WindowSDL::updateButton(KeyBinding& binding, u32 button, bool is_pressed) {
switch (button) {
case OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_L2:
case OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_R2:
axis = (button == OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_R2) ?
Input::Axis::TriggerRight : Input::Axis::TriggerLeft;
axis = (button == OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_R2) ? Input::Axis::TriggerRight
: Input::Axis::TriggerLeft;
// int axis_value = is_pressed ? 255 : 0;
// int ax = Input::GetAxis(0, 0x80, is_pressed ? 255 : 0);
controller->Axis(0, axis, Input::GetAxis(0, 0x80, is_pressed ? 255 : 0));
break;
case OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_TOUCH_PAD:
x = Config::getBackButtonBehavior() == "left" ? 0.25f
: Config::getBackButtonBehavior() == "right" ? 0.75f : 0.5f;
: Config::getBackButtonBehavior() == "right" ? 0.75f
: 0.5f;
controller->SetTouchpadState(0, true, x, 0.5f);
// button = OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_TOUCH_PAD;
controller->CheckButton(0, button, is_pressed);
@ -498,10 +490,12 @@ void WindowSDL::onKeyPress(const SDL_Event* event) {
KeyBinding binding = {0, SDL_GetModState()};
if (event->type == SDL_EVENT_KEY_DOWN || event->type == SDL_EVENT_KEY_UP) {
binding.key = event->key.key; // For keyboard events
} else if (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN || event->type == SDL_EVENT_MOUSE_BUTTON_UP) {
} else if (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN ||
event->type == SDL_EVENT_MOUSE_BUTTON_UP) {
binding.key = event->button.button; // For mouse button events
} else {
std::cout << "Bro something is very wrong with the waitevent switch case as this is the only 4 possible cases\n";
std::cout << "Bro something is very wrong with the waitevent switch case as this is the "
"only 4 possible cases\n";
return;
}
@ -513,7 +507,8 @@ void WindowSDL::onKeyPress(const SDL_Event* event) {
if (event->type == SDL_EVENT_KEY_DOWN) {
// Toggle capture of the mouse
if (binding.key == SDLK_F9) {
SDL_SetWindowRelativeMouseMode(this->GetSdlWindow(), !SDL_GetWindowRelativeMouseMode(this->GetSdlWindow()));
SDL_SetWindowRelativeMouseMode(this->GetSdlWindow(),
!SDL_GetWindowRelativeMouseMode(this->GetSdlWindow()));
}
// Reparse kbm inputs
if (binding.key == SDLK_F8) {
@ -531,7 +526,6 @@ void WindowSDL::onKeyPress(const SDL_Event* event) {
}
}
// Check if the current key+modifier is a button mapping
bool button_found = false;
auto button_it = FindKeyAllowingPartialModifiers(button_map, binding);
@ -541,7 +535,9 @@ void WindowSDL::onKeyPress(const SDL_Event* event) {
if (button_it != button_map.end()) {
button_found = true;
button = button_it->second;
WindowSDL::updateButton(binding, button, event->type == SDL_EVENT_KEY_DOWN || event->type == SDL_EVENT_MOUSE_BUTTON_DOWN);
WindowSDL::updateButton(binding, button,
event->type == SDL_EVENT_KEY_DOWN ||
event->type == SDL_EVENT_MOUSE_BUTTON_DOWN);
}
// Check if the current key+modifier is an axis mapping
auto axis_it = FindKeyAllowingPartialModifiers(axis_map, binding);
@ -550,7 +546,10 @@ void WindowSDL::onKeyPress(const SDL_Event* event) {
}
if (axis_it != axis_map.end()) {
axis = axis_it->second.axis;
axis_value = (event->type == SDL_EVENT_KEY_DOWN || event->type == SDL_EVENT_MOUSE_BUTTON_DOWN) ? axis_it->second.value : 0;
axis_value =
(event->type == SDL_EVENT_KEY_DOWN || event->type == SDL_EVENT_MOUSE_BUTTON_DOWN)
? axis_it->second.value
: 0;
int ax = Input::GetAxis(-0x80, 0x80, axis_value);
controller->Axis(0, axis, ax);
}
@ -579,9 +578,11 @@ void WindowSDL::updateModKeyedInputsManually(Frontend::KeyBinding& binding) {
}
}
}
// if both non mod keyed and mod keyed inputs are used and you press the key and then the mod key in a single frame,
// both will activate but the simple one will not deactivate, unless i use this stupid looking workaround
if(!mod_keyed_input_found) return; // in this case the fix for the fix for the wrong update order is not needed
// if both non mod keyed and mod keyed inputs are used and you press the key and then the mod
// key in a single frame, both will activate but the simple one will not deactivate, unless i
// use this stupid looking workaround
if (!mod_keyed_input_found)
return; // in this case the fix for the fix for the wrong update order is not needed
for (auto input : button_map) {
if (input.first.modifier == SDL_KMOD_NONE) {
WindowSDL::updateButton(binding, input.second, false);
@ -592,8 +593,8 @@ void WindowSDL::updateModKeyedInputsManually(Frontend::KeyBinding& binding) {
controller->Axis(0, input.second.axis, Input::GetAxis(-0x80, 0x80, 0));
}
}
// also this sometimes leads to janky inputs but whoever decides to intentionally create a state where this is needed
// should not deserve a smooth experience anyway
// also this sometimes leads to janky inputs but whoever decides to intentionally create a state
// where this is needed should not deserve a smooth experience anyway
}
void WindowSDL::onGamepadEvent(const SDL_Event* event) {

View File

@ -18,7 +18,6 @@ class GameController;
namespace Frontend {
class KeyBinding {
public:
Uint32 key;
@ -52,8 +51,6 @@ struct WindowSystemInfo {
WindowSystemType type = WindowSystemType::Headless;
};
class WindowSDL {
public:
explicit WindowSDL(s32 width, s32 height, Input::GameController* controller,
@ -84,7 +81,6 @@ public:
void updateMouse();
private:
void onResize();
void onKeyPress(const SDL_Event* event);
void onGamepadEvent(const SDL_Event* event);

View File

@ -21,13 +21,17 @@ cross=e
square = r
#emergency extra bullets
up=w,lalt
up = w,
lalt
#change quick item
down=s,lalt
down = s,
lalt
#change weapon in left hand
left=a,lalt
left = a,
lalt
#change weapon in right hand
right=d,lalt
right = d,
lalt
#menu
options = escape
@ -35,13 +39,15 @@ options=escape
touchpad = g
#transform
l1=rightbutton,lshift
l1 = rightbutton,
lshift
#shoot
r1 = leftbutton
#light attack
l2 = rightbutton
#heavy attack
r2=leftbutton,lshift
r2 = leftbutton,
lshift
#does nothing
l3 = x
@ -50,10 +56,8 @@ r3=q
#axis mappings
#move
axis_left_x_minus=a
axis_left_x_plus=d
axis_left_y_minus=w
axis_left_y_plus=s
axis_left_x_minus = a axis_left_x_plus = d axis_left_y_minus =
w axis_left_y_plus = s
#og cam input
#axis_right_x_minus = j
#axis_right_x_plus = l