input_mapping.cpp 4.22 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
110
111
112
#include <input/input_mapping.hpp>

namespace gdw{
	input_mapping::input_mapping(input *input, engine& engine):input_(input), engine_(engine),mappings_(){}

	input_mapping::~input_mapping(){}

//ADD
	bool input_mapping::add(std::string name){
		bool ok = false;
		mapping ims;
		std::string s = engine_.getConfig().get<std::string>(name + "_key1","none");
		if(s != "none"){
			ims.key_1 = input_util::get_key_from_name(s);
			if(ims.key_1 != SDLK_UNKNOWN){
				ims.type += key_1_;
				ok = true;
			}
		}
		s = engine_.getConfig().get<std::string>(name + "_key2","none");
		if(s != "none"){
			ims.key_2 = input_util::get_key_from_name(s);
			if(ims.key_2 != SDLK_UNKNOWN){
				ims.type += key_2_;
				ok = true;
			}
		}
		s = engine_.getConfig().get<std::string>(name + "_mouse_button","none");
		if(s != "none"){
			ims.mouse_button = input_util::get_mouse_button_from_name(s);
			if(ims.mouse_button != -1){
				ims.type += mouse_button_;
				ok = true;
			}
		}
		s = engine_.getConfig().get<std::string>(name + "_controller_button","none");
		if(s != "none"){
			ims.controller_button = input_util::get_controller_button_from_name(s);
			if(ims.controller_button != SDL_CONTROLLER_BUTTON_INVALID){
				ims.type += controller_button_;
				ok = true;
			}
		}
		s = engine_.getConfig().get<std::string>(name + "_controller_axis","none");
		if(s != "none"){
			ims.controller_axis = input_util::get_controller_axis_from_name(s);
			if(ims.controller_axis != SDL_CONTROLLER_AXIS_INVALID){
				ims.type += controller_axis_;
				ims.negativ = input_util::controller_negativ_axis(s);
				if(ims.controller_axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT || ims.controller_axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT){
					ims.negativ = false;
				}
				ok = true;
			}
		}
		mappings_.emplace(name, ims);
		return ok;
	}

//RETURN
	SDL_Keycode input_mapping::key_1(std::string id){
		auto it = mappings_.find(id);
		if(it == mappings_.end())return SDLK_UNKNOWN;
		return (mappings_.at(it->first).type & key_1_) !=0? mappings_.at(it->first).key_1 : SDLK_UNKNOWN;
	}

	SDL_Keycode input_mapping::key_2(std::string id){
		auto it = mappings_.find(id);
		if(it == mappings_.end())return SDLK_UNKNOWN;
		return (mappings_.at(it->first).type & key_2_) !=0? mappings_.at(it->first).key_2 : SDLK_UNKNOWN;
	}

	int input_mapping::mouse_button(std::string id){
		auto it = mappings_.find(id);
		if(it == mappings_.end())return -1;
		return (mappings_.at(it->first).type & mouse_button_) !=0? mappings_.at(it->first).mouse_button : -1;
	}

	SDL_GameControllerButton input_mapping::controller_button(std::string id){
		auto it = mappings_.find(id);
		if(it == mappings_.end())return SDL_CONTROLLER_BUTTON_INVALID;
		return (mappings_.at(it->first).type & controller_button_) !=0? mappings_.at(it->first).controller_button : SDL_CONTROLLER_BUTTON_INVALID;
	}

	SDL_GameControllerAxis input_mapping::controller_axis(std::string id){
		auto it = mappings_.find(id);
		if(it == mappings_.end())return SDL_CONTROLLER_AXIS_INVALID;
		return (mappings_.at(it->first).type & controller_axis_) !=0? mappings_.at(it->first).controller_axis : SDL_CONTROLLER_AXIS_INVALID;
	}

	bool input_mapping::is_mapping(std::string id){
		return mappings_.find(id) != mappings_.end();
	}

//PRINT
	void input_mapping::print(){
		log << "input: print mappings:" << std::endl;
		for(auto it:mappings_){
			log << "input: " << it.first << std::endl;
			if(it.second.key_1 != 0)log << "input:    key1: " << input_util::get_key_name(it.second.key_1) << std::endl;
			if(it.second.key_2 != 0)log << "input:    key2: " << input_util::get_key_name(it.second.key_2) << std::endl;
			if(it.second.mouse_button != -1)log << "input:    mouse button: " << input_util::get_mouse_button_name(it.second.mouse_button) << std::endl;
			if(it.second.controller_button != -1)log << "input:    controller button: " << input_util::get_controller_button_name(it.second.controller_button) << std::endl;
			if(it.second.controller_axis != -1)log << "input:    controller axis: " << input_util::get_controller_axis_name(it.second.controller_axis) << std::endl;
			if(it.second.negativ)log << "input:    negativ axis" << std::endl;
		}
	}
}