input_util.cpp 2.76 KB
Newer Older
1
2
3
4
//****************************************
// by Benjamin 'Albsi' Albsmeier
// input/input.cpp for GDW SS15
//****************************************
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <input/input_util.hpp>

namespace gdw{
	input_util::input_util(input *input):input_(input){}

	input_util::~input_util(){}

	std::string input_util::to_lower_case(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_util::to_upper_case(std::string inString){
		std::string outString;
		for(auto s:inString){
			if(s >= 'a' && s <= 'z'){
				outString += s - 'a' + 'A';
			}else{
				outString += s;
			}
		}
		return outString;
	}

//NAME <> SDL_KEYCODE
	std::string input_util::get_key_name(SDL_Keycode key){
		return SDL_GetKeyName(key);
	}

	SDL_Keycode input_util::get_key_from_name(std::string name){
		return SDL_GetKeyFromName(name.c_str());
	}

//NAME <> SDL_MOUSE_BUTTON
	int input_util::get_mouse_button_from_name(std::string name){
		name = to_lower_case(name);
		if(name == "left"){
			return SDL_BUTTON_LEFT;
		}else if(name == "right"){
			return SDL_BUTTON_RIGHT;
		}else if(name == "middle"){
			return SDL_BUTTON_MIDDLE;
		}else if(name == "mouse4"){
			return SDL_BUTTON_X1;
		}else if(name == "mouse5"){
			return SDL_BUTTON_X2;
		}else{
			return -1;
		}
	}

	std::string input_util::get_mouse_button_name(int button){
		switch(button){
			case SDL_BUTTON_LEFT: return "left";
			case SDL_BUTTON_RIGHT: return "right";
			case SDL_BUTTON_MIDDLE: return "middle";
			case SDL_BUTTON_X1: return "mouse4";
			case SDL_BUTTON_X2: return "mouse5";
		}
		return "none";
	}

//NAME <> SDL_CONTROLLER_BUTTON/AXIS
	std::string input_util::get_controller_button_name(SDL_GameControllerButton button){
		return SDL_GameControllerGetStringForButton(button);
	}

	std::string input_util::get_controller_axis_name(SDL_GameControllerAxis axis){
		return SDL_GameControllerGetStringForAxis(axis);
	}

	SDL_GameControllerButton input_util::get_controller_button_from_name(std::string name){
		return SDL_GameControllerGetButtonFromString(name.c_str());
	}

	SDL_GameControllerAxis input_util::get_controller_axis_from_name(std::string name){
		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_util::controller_negativ_axis(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;
	}
}