Commit 1af81e5d authored by Benjamin 'Albsi' Albsmeier's avatar Benjamin 'Albsi' Albsmeier
Browse files

controller should now work again - exapt controller cursor

parent 12b23839
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include <unordered_map> #include <unordered_map>
#include <SDL.h> #include <SDL.h>
#include <input/input.hpp> #include <input/input.hpp>
#include <input/input_mapping.hpp>
namespace gdw{ namespace gdw{
struct axis_struct{ struct axis_struct{
...@@ -17,22 +18,26 @@ namespace gdw{ ...@@ -17,22 +18,26 @@ namespace gdw{
input *input_; input *input_;
static float constexpr axis_max_ = 32767.f; static float constexpr axis_max_ = 32767.f;
static float constexpr axis_dead_zone_ = .3f; static float constexpr axis_dead_zone_ = .3f;
static float constexpr speed_ = 800.f;
SDL_GameController *sdl_controller_; SDL_GameController *sdl_controller_;
std::unordered_map<int, bool> button_map_; std::unordered_map<int, bool> button_map_;
std::unordered_map<int, bool> button_released_map_; std::unordered_map<int, bool> button_released_map_;
std::unordered_map<int, axis_struct> axis_map_; std::shared_ptr<std::unordered_map<int, axis_struct>> axis_map_;
public: public:
controller(input *input); controller(input *input, SDL_GameController* sdl_controller);
~controller(); ~controller();
void reset(); void reset();
void update(float delta);
SDL_GameController* get(); SDL_GameController* sdl_controller();
std::shared_ptr<std::unordered_map<int, axis_struct>> axis_map();
bool down(SDL_GameControllerButton button); bool down(SDL_GameControllerButton button_id);
bool pressed(SDL_GameControllerButton button); bool pressed(SDL_GameControllerButton button_id);
bool released(SDL_GameControllerButton button); bool released(SDL_GameControllerButton button_id);
bool pressed_axis(SDL_GameControllerAxis axis_id);
bool down_axis(SDL_GameControllerAxis axis_id);
bool released_axis(SDL_GameControllerAxis axis_id);
float axis(SDL_GameControllerAxis axis); float axis(SDL_GameControllerAxis axis);
void up_event(const SDL_ControllerButtonEvent &e); void up_event(const SDL_ControllerButtonEvent &e);
......
...@@ -14,12 +14,13 @@ namespace gdw{ ...@@ -14,12 +14,13 @@ namespace gdw{
};*/ };*/
class controller_manager{ class controller_manager{
private: private:
std::string controller_database_path_ = "input/gamecontrollerdb.txt";
std::unordered_map<int, std::shared_ptr<gdw::controller>> controllers_;
std::shared_ptr<const asset> controllerDB_;
engine &engine_; engine &engine_;
input *input_; input *input_;
static float constexpr speed_ = 800.f;
std::string controller_database_path_ = "input/gamecontrollerdb.txt";
std::shared_ptr<const asset> controller_database_;
std::unordered_map<int, std::shared_ptr<gdw::controller>> controllers_;
//std::unordered_map<int, bool> used_controllers_; //std::unordered_map<int, bool> used_controllers_;
//int controllerNr = -1; //int controllerNr = -1;
...@@ -28,29 +29,27 @@ namespace gdw{ ...@@ -28,29 +29,27 @@ namespace gdw{
//controllerCursor getCCursorFromName(std::string name); //controllerCursor getCCursorFromName(std::string name);
int first_controller(); int first_controller();
bool is_controller(int id);
public: public:
controller_manager(input *input, engine& engine); controller_manager(input *input, engine& engine);
~controller_manager(); ~controller_manager();
void update(float delta); void update(float delta);
void reset(); void reset();
bool pressed_button(SDL_GameControllerButton button_id, int controller_id); //RETURN
bool down_button(SDL_GameControllerButton button_id, int controller_id); bool pressed(SDL_GameControllerButton button_id, int controller_id);
bool released_button(SDL_GameControllerButton button_id, int controller_id); bool down(SDL_GameControllerButton button_id, int controller_id);
bool released(SDL_GameControllerButton button_id, int controller_id);
bool pressed_axis(SDL_GameControllerAxis axis_id, int controller_id); bool pressed_axis(SDL_GameControllerAxis axis_id, int controller_id);
bool down_axis(SDL_GameControllerAxis axis_id, int controller_id); bool down_axis(SDL_GameControllerAxis axis_id, int controller_id);
bool released_axis(SDL_GameControllerAxis axis_id, int controller_id); bool released_axis(SDL_GameControllerAxis axis_id, int controller_id);
float axis(SDL_GameControllerAxis axis, int controller_id); float axis(SDL_GameControllerAxis axis, int controller_id);
int controller_count(); int controller_count();
bool is_controller(int controller_id);
std::shared_ptr<gdw::controller> controller(int id); std::shared_ptr<gdw::controller> controller(int controller_id);
SDL_GameController* get(int controller_id);
//EVENT
void up_event(const SDL_ControllerButtonEvent &e); void up_event(const SDL_ControllerButtonEvent &e);
void down_event(const SDL_ControllerButtonEvent &e); void down_event(const SDL_ControllerButtonEvent &e);
void added(const SDL_ControllerDeviceEvent &e); void added(const SDL_ControllerDeviceEvent &e);
......
#include <input/controller.hpp> #include <input/controller.hpp>
namespace gdw{ namespace gdw{
controller::controller(input *input):input_(input){} controller::controller(input *input, SDL_GameController* sdl_controller):input_(input),sdl_controller_(sdl_controller){
axis_map_ = std::make_shared<std::unordered_map<int, axis_struct>>();
for(int i=SDL_CONTROLLER_AXIS_INVALID+1; i<SDL_CONTROLLER_AXIS_MAX; ++i){
axis_struct as;
axis_map_->emplace(i, as);
}
}
controller::~controller(){} controller::~controller(){}
void controller::reset(){ void controller::reset(){
/*controllers_.at(it.first).controllerReleasedMap.clear(); for(auto it:*axis_map_){
controllers_.at(it.first).controllerMap.clear(); if(!it.second.changed && it.second.down > 0){
for (auto axisIt:controllers_.at(it.first).axisMap){ it.second.released = true;
if(!axisIt.second.changed && axisIt.second.down > 0){ it.second.down = 0;
axisIt.second.released = true;
axisIt.second.down = 0;
}else{ }else{
if(axisIt.second.released)axisIt.second.released = false; if(it.second.released)it.second.released = false;
} }
axisIt.second.changed = false; it.second.changed = false;
}*/ }
} }
//EVENTS void controller::update(float delta){
void controller::down_event(const SDL_ControllerButtonEvent &e){//TODO for(int i=SDL_CONTROLLER_AXIS_INVALID+1; i<SDL_CONTROLLER_AXIS_MAX; ++i){
/*auto it = controllers_.find(e.which); if(axis(SDL_GameControllerAxis(i))){
if(it != controllers_.end()){ auto it = axis_map_->find(i);
controllers_.at(it->first).controllerMap.emplace(e.button, true); it->second.down = 1;
}*/ it->second.changed = true;
}
}
} }
void controller::up_event(const SDL_ControllerButtonEvent &e){//TODO bool controller::pressed_axis(SDL_GameControllerAxis axis_id){
/*auto it = controllers_.find(e.which); auto a = axis_map_->find(axis_id);
if(it != controllers_.end()){ if(a != axis_map_->end())return a->second.down == 1;
controllers_.at(it->first).controllerReleasedMap.emplace(e.button, true); return false;
}*/
} }
//RETURN bool controller::down_axis(SDL_GameControllerAxis axis_id){
SDL_GameController* controller::get(){ auto a = axis_map_->find(axis_id);
return sdl_controller_; if(a != axis_map_->end())return a->second.down;
return false;
} }
bool controller::pressed(SDL_GameControllerButton button){ bool controller::released_axis(SDL_GameControllerAxis axis_id){
/*if(nr == -1)nr = firstController(); auto a = axis_map_->find(axis_id);
if(nr == -1)return false; if(a != axis_map_->end())return a->second.released;
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; return false;
} }
bool controller::down(SDL_GameControllerButton button){ void controller::down_event(const SDL_ControllerButtonEvent &e){
/*if(nr == -1)nr = firstController(); button_map_.emplace(e.button, true);
if(nr == -1)return false; }
auto it = controllers_.find(nr);
if(controllers_.size()>0){ void controller::up_event(const SDL_ControllerButtonEvent &e){
return SDL_GameControllerGetButton(controllers_.at(it->first).sdlController, button); button_released_map_.emplace(e.button, true);
}*/ }
SDL_GameController* controller::sdl_controller(){
return sdl_controller_;
}
bool controller::pressed(SDL_GameControllerButton button_id){
auto btn = button_map_.find(button_id);
if(btn != button_map_.end())return btn->second;
return false; return false;
} }
bool controller::released(SDL_GameControllerButton button){ bool controller::down(SDL_GameControllerButton button_id){
/*if(nr == -1)nr = firstController(); return SDL_GameControllerGetButton(sdl_controller(), button_id);
if(nr == -1)return false; }
auto it = controllers_.find(nr);
if(controllers_.size()>0){ bool controller::released(SDL_GameControllerButton button_id){
auto btnIt = controllers_.at(it->first).controllerReleasedMap.find(button); auto btn = button_released_map_.find(button_id);
return btnIt->second; if(btn != button_released_map_.end())return btn->second;
}*/
return false; return false;
} }
float controller::axis(SDL_GameControllerAxis axis){ float controller::axis(SDL_GameControllerAxis axis_id){
/*if(nr == -1)nr = firstController(); float f = SDL_GameControllerGetAxis(sdl_controller(), axis_id) / axis_max_;
if(nr == -1)return 0.f; return !(f < axis_dead_zone_ && f > -axis_dead_zone_)? f: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;
} }
} }
...@@ -5,7 +5,7 @@ namespace gdw{ ...@@ -5,7 +5,7 @@ namespace gdw{
input_(input), input_(input),
engine_(engine), engine_(engine),
controllers_(){ controllers_(){
controllerDB_ = engine.asset_manager().load(engine.asset_manager().native_name(controller_database_path_)); controller_database_ = engine.asset_manager().load(engine.asset_manager().native_name(controller_database_path_));
//ccursor = getCCursorFromName(engine_.getConfig().get<std::string>("controller_cursor","RIGHT")); //ccursor = getCCursorFromName(engine_.getConfig().get<std::string>("controller_cursor","RIGHT"));
//controllerUse = engine_.getConfig().get<bool>("use_controller",false); //controllerUse = engine_.getConfig().get<bool>("use_controller",false);
...@@ -16,24 +16,8 @@ namespace gdw{ ...@@ -16,24 +16,8 @@ namespace gdw{
controller_manager::~controller_manager(){} controller_manager::~controller_manager(){}
void controller_manager::update(float delta){ void controller_manager::update(float delta){
//TODO MULTI CONTROLLER SUPPORT
for(auto it:controllers_){ for(auto it:controllers_){
/*for(int i=SDL_CONTROLLER_AXIS_INVALID+1; i<SDL_CONTROLLER_AXIS_MAX; ++i){ it.second->update(delta);
float f = SDL_GameControllerGetAxis(
controllers_.at(it.first).sdlController, SDL_GameControllerAxis(i)) / controllerAxisMax_;
if(!(f < controllerAxisDeadZone_ && f > -controllerAxisDeadZone_)){
auto axisIt = controllers_.at(it.first).axisMap.find(i);
if(axisIt == controllers_.at(it.first).axisMap.end()){
axisStruct as;
as.down = 1;
as.changed = true;
controllers_.at(it.first).axisMap.emplace(i, as);
}else{
++controllers_.at(it.first).axisMap.at(axisIt->first).down;
controllers_.at(it.first).axisMap.at(axisIt->first).changed = true;
}
}
}*/
} }
/*if(ccursor != controllerCursor::none){ /*if(ccursor != controllerCursor::none){
...@@ -55,10 +39,8 @@ namespace gdw{ ...@@ -55,10 +39,8 @@ namespace gdw{
} }
std::shared_ptr<gdw::controller> controller_manager::controller(int id){ std::shared_ptr<gdw::controller> controller_manager::controller(int id){
return nullptr; auto it = controllers_.find(id);
} if(it != controllers_.end()) return it->second;
SDL_GameController* controller_manager::get(int controller_id){
return nullptr; return nullptr;
} }
...@@ -68,32 +50,34 @@ namespace gdw{ ...@@ -68,32 +50,34 @@ namespace gdw{
} }
} }
int controller_manager::first_controller(){//TODO MULTI CONTROLLER SUPPORT int controller_manager::first_controller(){
if(controller_count() < 1)return -1; if(controller_count() < 1)return -1;
/*for (auto& it:used_controllers_){ for(auto it:controllers_){
if(it.second == true){ return it.first;
return it.first; }
}
}*/
return -1; return -1;
} }
int controller_manager::controller_count(){ int controller_manager::controller_count(){
return 0;//used_controllers_.size() //controllers_.size() return controllers_.size();
} }
void controller_manager::down_event(const SDL_ControllerButtonEvent &e){//TODO void controller_manager::down_event(const SDL_ControllerButtonEvent &e){
/*auto it = controllers_.find(e.which); if(controller_count() > 0){
if(it != controllers_.end()){ auto c = controller(e.which);
controllers_.at(it->first).controllerMap.emplace(e.button, true); if(c){
}*/ c->down_event(e);
}
}
} }
void controller_manager::up_event(const SDL_ControllerButtonEvent &e){//TODO void controller_manager::up_event(const SDL_ControllerButtonEvent &e){
/*auto it = controllers_.find(e.which); if(controller_count() > 0){
if(it != controllers_.end()){ auto c = controller(e.which);
controllers_.at(it->first).controllerReleasedMap.emplace(e.button, true); if(c){
}*/ c->up_event(e);
}
}
} }
/*controllerCursor controller_manager::getCCursorFromName(std::string name){ /*controllerCursor controller_manager::getCCursorFromName(std::string name){
...@@ -106,212 +90,150 @@ namespace gdw{ ...@@ -106,212 +90,150 @@ namespace gdw{
}*/ }*/
bool controller_manager::is_controller(int id){ bool controller_manager::is_controller(int id){
auto it = controllers_.find(id); if(controller_count() > 0){
return SDL_IsGameController(id) && it != controllers_.end(); auto it = controllers_.find(id);
return SDL_IsGameController(id) && it != controllers_.end();
}
return false;
} }
bool controller_manager::pressed_button(SDL_GameControllerButton button_id, int controller_id){ bool controller_manager::pressed(SDL_GameControllerButton button_id, int controller_id){
/*if(controller_count() > 0){ if(controller_count() > 0){
if(controller_id < 0){ if(controller_id < 0){
for(auto it:controllers_){ for(auto c:controllers_){
if(it.second->pressed(mapping_->controller_button(id))){ if(c.second->pressed(button_id))return true;
return true;
}
} }
}else{ }
auto it = controllers_.find(id); if(controller_id >= 0){
if(it != controllers_.end()){ auto c = controller(controller_id);
if(it.second->pressed(mapping_->controller_button(id))){ if(c){
return true; return c->pressed(button_id);
}
} }
} }
}*/ }
return false; return false;
} }
bool controller_manager::down_button(SDL_GameControllerButton button_id, int controller_id){ bool controller_manager::down(SDL_GameControllerButton button_id, int controller_id){
/*if(controller_count() > 0){ if(controller_count() > 0){
if(controller_id < 0){ if(controller_id < 0){
for(auto it:controllers_){ for(auto c:controllers_){
if(it.second->down(mapping_->controller_button(id))){ if(c.second->down(button_id))return true;
return true;
}
} }
}else{ }
auto it = controllers_.find(id); if(controller_id >= 0){
if(it != controllers_.end()){ auto c = controller(controller_id);
if(it.second->down(mapping_->controller_button(id))){ if(c){
return true; return c->down(button_id);
}
} }
} }
}*/ }
return false; return false;
} }
bool controller_manager::released_button(SDL_GameControllerButton button_id, int controller_id){ bool controller_manager::released(SDL_GameControllerButton button_id, int controller_id){
/*if(controller_count() > 0){ if(controller_count() > 0){
if(controller_id < 0){ if(controller_id < 0){
for(auto it:controllers_){ for(auto c:controllers_){
if(it.second->released(mapping_->controller_button(id))){ if(c.second->released(button_id))return true;
return true;
}
} }
}else{ }
auto it = controllers_.find(id); if(controller_id >= 0){
if(it != controllers_.end()){ auto c = controller(controller_id);
if(it.second->released(mapping_->controller_button(id))){ if(c){
return true; return c->released(button_id);
}
} }
} }
}*/ }
return false; return false;
} }
bool controller_manager::pressed_axis(SDL_GameControllerAxis axis_id, int controller_id){ float controller_manager::axis(SDL_GameControllerAxis axis_id, int controller_id){
if(controller_count() > 0){ if(controller_count() > 0){
/*if(controller_count() > 0){//TODO MULTI CONTROLLER SUPPORT if(controller_id < 0){
for(auto c:controllers_){
if((mappings_.at(it->first).type & input_mapping::controller_axis) != 0){ if(c.second->axis(axis_id))return c.second->axis(axis_id);
auto conIt = controllers_.find(firstController());
if(conIt != controllers_.end()){
auto axisIt = controllers_.at(conIt->first).axisMap.find(mappings_.at(it->first).caxisCode);
if(controllers_.at(conIt->first).axisMap.at(axisIt->first).down == 1){
return true;
}
}
} }
}*/ }
if(controller_id >= 0){
auto c = controller(controller_id);
if(c){
return c->axis(axis_id);
}
}
} }
return false; return 0.f;
} }
bool controller_manager::down_axis(SDL_GameControllerAxis axis_id, int controller_id){ bool controller_manager::pressed_axis(SDL_GameControllerAxis axis_id, int controller_id){
if(controller_count() > 0){ if(controller_count() > 0){
/*if((mappings_.at(it->first).type & input_mapping::controller_axis) != 0){ if(controller_id < 0){
if(!mappings_.at(it->first).negativAxis){ for(auto c:controllers_){
if(controllerAxis(mappings_.at(it->first).caxisCode) > controllerAxisDeadZone_){ if(c.second->pressed_axis(axis_id))return true;
return true; }
} }
}else{ if(controller_id >= 0){
if(controllerAxis(mappings_.at(it->first).caxisCode) < -controllerAxisDeadZone_){ auto c = controller(controller_id);
return true; if(c){
} return c->pressed_axis(axis_id);
} }
}*/ }
} }
return false; return false;
} }
bool controller_manager::released_axis(SDL_GameControllerAxis axis_id, int controller_id){ bool controller_manager::down_axis(SDL_GameControllerAxis axis_id, int controller_id){
if(controller_count() > 0){ if(controller_count() > 0){
/*if((mappings_.at(it->first).type & input_mapping::controller_axis) != 0){ if(controller_id < 0){
auto conIt = controllers_.find(firstController()); for(auto c:controllers_){
if(conIt != controllers_.end()){ if(c.second->down_axis(axis_id))return true;
auto axisIt = controllers_.at(conIt->first).axisMap.find(mappings_.at(it->first).caxisCode);
if(controllers_.at(conIt->first).axisMap.at(axisIt->first).released){
return true;
}
} }
}*/ }
} if(controller_id >= 0){
return false;