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