Commit cce433b2 authored by Benjamin 'Albsi' Albsmeier's avatar Benjamin 'Albsi' Albsmeier
Browse files

now again with the controller axis and some small changes

parent 099b2942
......@@ -19,14 +19,14 @@ test_ui : false
mapGenDebug : false
move_left_key1 : a
move_left_key2 : left
move_left_controller_axis : leftx_positiv
move_left_controller_axis : leftx
move_right_key1 : d
move_right_key2 : right
move_right_controller_axis : leftx_negativ
move_right_controller_axis : leftx_negative
beam_normal_mouse_button : left
beam_normal_controller_axis : triggerleft
beam_normal_controller_axis : lefttrigger
beam_inverse_mouse_button : right
beam_inverse_controller_axis : TRIGGERRIGHT
beam_inverse_controller_axis : righttrigger
place_item_key1 : Space
place_item_mouse_button : Middle
place_item_controller_button : A
......@@ -34,7 +34,7 @@ test_key1 : t
test_key2 : p
test_mouse_button : middle
test_controller_button : back
test_controller_axis : righty_positiv
test_controller_axis : righty
state_switch_key1 : Escape
state_switch_controller_button : START
button_click_mouse_button : left
......
......@@ -90,12 +90,14 @@ namespace gdw{
* example: test_mouse_button : left
*
* https://wiki.libsdl.org/SDL_GameControllerButton?highlight=%28%5CbCategoryGameController%5Cb%29%7C%28CategoryEnum%29
* without 'SDL_CONTROLLER_BUTTON_' use for the dpad 'up', 'down', 'left' or 'right'
* without 'SDL_CONTROLLER_BUTTON_'
* dpad is 'dp' + direction dpad_up is 'dpup'
* controller button: <name>_controller_button : <button>
* example: test_controller_button: start
*
* https://wiki.libsdl.org/SDL_GameControllerAxis?highlight=%28%5CbCategoryGameController%5Cb%29%7C%28CategoryEnum%29
* without 'SDL_CONTROLLER_AXIS_'
* and trigger got the inverted name 'lefttrigger' is the name for SDL_CONTROLLER_AXIS_TRIGGERLEFT
* controller axis: <name>_controller_axis : <axis> + '_positiv' or '_negativ' (ignored if trigger)
* example: test_controller_axis : leftx_positiv
*/
......@@ -184,8 +186,14 @@ namespace gdw{
bool isControllerButtonReleased(SDL_GameControllerButton button, int nr = 0);
float controllerAxis(SDL_GameControllerAxis axis, int nr = 0);
std::string makeStringLowerCase(std::string str);
std::string makeStringUpperCase(std::string inString);
//
std::string getMouseButtonName(int id);
std::string getMouseButtonName(int button);
std::string getKeyName(SDL_Keycode key);
std::string getControllerButtonName(SDL_GameControllerButton button);
std::string getControllerAxisName(SDL_GameControllerAxis axis);
SDL_Keycode getKeyCodeByMapping(std::string id, int nr);
int getMButtonCodeByMapping(std::string id);
SDL_GameControllerButton getCButtonCodeByMapping(std::string id);
......@@ -195,6 +203,7 @@ namespace gdw{
SDL_Keycode getKeyFromName(std::string name);
SDL_GameControllerButton getCButtonFromName(std::string name);
SDL_GameControllerAxis getCAxisFromName(std::string name);
bool isNegativAxis(std::string name);
int firstController();
controllerCursor getCCursorFromName(std::string name);
......
......@@ -261,6 +261,8 @@ namespace gdw{
if(s != "none"){
ims.caxisCode = getCAxisFromName(s);
ims.type += caxis;
ims.negativAxis = isNegativAxis(s);
if(ims.caxisCode == SDL_CONTROLLER_AXIS_TRIGGERLEFT || ims.caxisCode == SDL_CONTROLLER_AXIS_TRIGGERRIGHT)ims.negativAxis = false;
ok = true;
}
mappings_.emplace(name, ims);
......@@ -270,18 +272,18 @@ namespace gdw{
void input::printMapping(){
log << "input: print mappings:" << std::endl;
for(auto it:mappings_){
log << "input: name: " << it.first << std::endl;
if(it.second.keyCode1 != 0)log << "input: key1: " << SDL_GetKeyName(it.second.keyCode1) << std::endl;
if(it.second.keyCode2 != 0)log << "input: key2: " << SDL_GetKeyName(it.second.keyCode2) << std::endl;
if(it.second.mbuttonCode != -1)log << "input: mouse button: " << getMouseButtonName(it.second.mbuttonCode) << std::endl;
if(it.second.cbuttonCode != -1)log << "input: controller button: " << SDL_GameControllerGetStringForButton(it.second.cbuttonCode) << std::endl;
if(it.second.caxisCode != -1)log << "input: controller axis: " << SDL_GameControllerGetStringForAxis(it.second.caxisCode) << std::endl;
if(it.second.negativAxis)log << "input: negativ axis" << std::endl;
log << "input: " << it.first << std::endl;
if(it.second.keyCode1 != 0)log << "input: key1: " << getKeyName(it.second.keyCode1) << std::endl;
if(it.second.keyCode2 != 0)log << "input: key2: " << getKeyName(it.second.keyCode2) << std::endl;
if(it.second.mbuttonCode != -1)log << "input: mouse button: " << getMouseButtonName(it.second.mbuttonCode) << std::endl;
if(it.second.cbuttonCode != -1)log << "input: controller button: " << getControllerButtonName(it.second.cbuttonCode) << std::endl;
if(it.second.caxisCode != -1)log << "input: controller axis: " << getControllerAxisName(it.second.caxisCode) << std::endl;
if(it.second.negativAxis)log << "input: negativ axis" << std::endl;
}
}
std::string input::getMouseButtonName(int id){
switch(id){
std::string input::getMouseButtonName(int button){
switch(button){
case SDL_BUTTON_LEFT: return "left";
case SDL_BUTTON_RIGHT: return "right";
case SDL_BUTTON_MIDDLE: return "middle";
......@@ -291,6 +293,17 @@ namespace gdw{
return "none";
}
std::string input::getKeyName(SDL_Keycode key){
return SDL_GetKeyName(key);
}
std::string input::getControllerButtonName(SDL_GameControllerButton button){
return SDL_GameControllerGetStringForButton(button);
}
std::string input::getControllerAxisName(SDL_GameControllerAxis axis){
return SDL_GameControllerGetStringForAxis(axis);
}
int input::getKeyCodeByMapping(std::string id, int nr){
auto it = mappings_.find(id);
......@@ -322,15 +335,16 @@ namespace gdw{
}
int input::getMButtonFromName(std::string name){
if(name == "Left" || name == "left" || name == "LEFT"){
name = makeStringLowerCase(name);
if(name == "left"){
return SDL_BUTTON_LEFT;
}else if(name == "Right" || name == "right" || name == "RIGHT"){
}else if(name == "right"){
return SDL_BUTTON_RIGHT;
}else if(name == "Middle" || name == "middle" || name == "MIDDLE"){
}else if(name == "middle"){
return SDL_BUTTON_MIDDLE;
}else if(name == "Mouse4" || name == "mouse4" || name == "MOUSE4"){
}else if(name == "mouse4"){
return SDL_BUTTON_X1;
}else if(name == "Mouse5" || name == "mouse5" || name == "MOUSE5"){
}else if(name == "mouse5"){
return SDL_BUTTON_X2;
}else{
return -1;
......@@ -342,21 +356,30 @@ namespace gdw{
}
SDL_GameControllerButton input::getCButtonFromName(std::string name){
if(name == "LEFT" || name == "left" || name == "Left"){
return SDL_CONTROLLER_BUTTON_DPAD_LEFT;
}else if(name == "RIGHT" || name == "right" || name == "Right"){
return SDL_CONTROLLER_BUTTON_DPAD_RIGHT;
}else if(name == "UP" || name == "up" || name == "Up"){
return SDL_CONTROLLER_BUTTON_DPAD_UP;
}else if(name == "DOWN" || name == "down" || name == "Down"){
return SDL_CONTROLLER_BUTTON_DPAD_DOWN;
}
return SDL_GameControllerGetButtonFromString(name.c_str());
}
SDL_GameControllerAxis input::getCAxisFromName(std::string name){
//return SDL_GameControllerGetAxisFromString(name.c_str());
return SDL_CONTROLLER_AXIS_INVALID;
size_t sep = name.find("_");
if(sep != std::string::npos){
std::string first = name.substr(0, sep);
return SDL_GameControllerGetAxisFromString(first.c_str());
}else{
return SDL_GameControllerGetAxisFromString(name.c_str());
}
}
bool input::isNegativAxis(std::string name){
size_t sep = name.find("_");
if(sep != std::string::npos){
std::string second = name.substr(sep +1, name.length() -sep -1);
if(second != ""){
if(second == "negative" || second == "n"){
return true;
}
}
}
return false;
}
controllerCursor input::getCCursorFromName(std::string name){
......@@ -368,6 +391,30 @@ namespace gdw{
return controllerCursor::none;
}
std::string input::makeStringLowerCase(std::string inString){
std::string outString;
for(auto s:inString){
if(s >= 'A' && s <= 'Z'){
outString += s - 'A' + 'a';
}else{
outString += s;
}
}
return outString;
}
std::string input::makeStringUpperCase(std::string inString){
std::string outString;
for(auto s:inString){
if(s >= 'a' && s <= 'z'){
outString += s - 'a' + 'A';
}else{
outString += s;
}
}
return outString;
}
//RETURN
bool input::isPressed(std::string id){
auto it = mappings_.find(id);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment