controller.cpp 3.21 KB
Newer Older
1
2
3
4
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
#include <input/controller.hpp>

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

	controller::~controller(){}

//EVENTS
	void controller::down_event(const SDL_ControllerButtonEvent &e){//TODO
		/*auto it = controllers_.find(e.which);
		if(it != controllers_.end()){
			controllers_.at(it->first).controllerMap.emplace(e.button, true);
		}*/
	}

	void controller::up_event(const SDL_ControllerButtonEvent &e){//TODO
		/*auto it = controllers_.find(e.which);
		if(it != controllers_.end()){
			controllers_.at(it->first).controllerReleasedMap.emplace(e.button, true);
		}*/
	}

	void controller::added(const SDL_ControllerDeviceEvent &e){//TODO
		/*if(SDL_IsGameController(e.which)){
			controller c;
			c.sdlController = SDL_GameControllerOpen(e.which);
			c.controllerNr = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(c.sdlController));
			usedControllers_.emplace(c.controllerNr, true);
			c.active = true;
			if(controllerDB_){
				std::vector<char> controllerDBVec(controllerDB_->content());
				if(SDL_GameControllerAddMapping(&controllerDBVec[0]) == -1){
					log << "controller: can't load controllerDB" << std::endl << SDL_GetError() << std::endl;
				}
			}
			controllers_.emplace(c.controllerNr, c);
		}*/
	}

	void controller::removed(const SDL_ControllerDeviceEvent &e){//TODO
		/*std::unordered_map<int, bool>::iterator usedIt = usedControllers_.find(e.which);
		if(usedIt != usedControllers_.end()){
			usedControllers_.at(usedIt->first) = false;
		}else{
			usedControllers_.emplace(e.which, false);
		}

		std::unordered_map<int, controller>::iterator it = controllers_.find(e.which);
		if(it != controllers_.end()){
			SDL_GameControllerClose(controllers_.at(it->first).sdlController);
			controllers_.erase(it);
		}*/
	}

//RETURN
	SDL_GameController* controller::get(){
		return sdl_controller_;
	}

	bool controller::pressed(SDL_GameControllerButton button){
		/*if(nr == -1)nr = firstController();
		if(nr == -1)return false;
		auto it = controllers_.find(nr);
		if(controllers_.size()>0){
			auto btnIt = controllers_.at(it->first).controllerMap.find(button);
			return controllers_.at(it->first).controllerMap.at(btnIt->first);
		}*/
		return false;
	}

	bool controller::down(SDL_GameControllerButton button){
		/*if(nr == -1)nr = firstController();
		if(nr == -1)return false;
		auto it = controllers_.find(nr);
		if(controllers_.size()>0){
			return SDL_GameControllerGetButton(controllers_.at(it->first).sdlController, button);
		}*/
		return false;
	}

	bool controller::released(SDL_GameControllerButton button){
		/*if(nr == -1)nr = firstController();
		if(nr == -1)return false;
		auto it = controllers_.find(nr);
		if(controllers_.size()>0){
			auto btnIt = controllers_.at(it->first).controllerReleasedMap.find(button);
			return btnIt->second;
		}*/
		return false;
	}

	float controller::axis(SDL_GameControllerAxis axis){
		/*if(nr == -1)nr = firstController();
		if(nr == -1)return 0.f;
		auto it = controllers_.find(nr);
		if(controllers_.size() > 0){
			if(controllers_.at(it->first).active){
				float f = SDL_GameControllerGetAxis(controllers_.at(it->first).sdlController, axis) / controllerAxisMax_;
				return !(f < controllerAxisDeadZone_ && f > -controllerAxisDeadZone_)? f:0.f;;
			}
		}*/
		return 0.f;
	}
}