Commit 12b23839 authored by Benjamin 'Albsi' Albsmeier's avatar Benjamin 'Albsi' Albsmeier
Browse files

a bit near to controller support and +1 class

parent 8a503685
......@@ -26,6 +26,7 @@ namespace gdw{
public:
controller(input *input);
~controller();
void reset();
SDL_GameController* get();
......@@ -36,19 +37,7 @@ namespace gdw{
void up_event(const SDL_ControllerButtonEvent &e);
void down_event(const SDL_ControllerButtonEvent &e);
void added(const SDL_ControllerDeviceEvent &e);
void removed(const SDL_ControllerDeviceEvent &e);
//FOLLOWED METHODES ARE UNUSED
void remapped(const SDL_ControllerDeviceEvent &e) noexcept {log << "input: controllerRemapped was triggerd but is not handelt" << std::endl;};
void axis_event(const SDL_ControllerAxisEvent &e) noexcept {/*handelt on another way, see: float controllerAxis(SDL_GameControllerAxis axis, int nr)*/};
void joy_added(const SDL_JoyDeviceEvent &e) noexcept {/*controllerAdded*/};
void joy_removed(const SDL_JoyDeviceEvent &e) noexcept {/*controllerRemoved*/};
void joy_hat(const SDL_JoyHatEvent &e) noexcept {/*controllerDown / controllerUp*/};
void joy_down(const SDL_JoyButtonEvent &e) noexcept {/*controllerDown*/};
void joy_up(const SDL_JoyButtonEvent &e) noexcept {/*controllerUp*/};
void joy_ball(const SDL_JoyBallEvent &e) noexcept {log << "input: joyBall was triggerd but is not handelt" << std::endl;};
void joy_axis(const SDL_JoyAxisEvent &e) noexcept {/*controllerAxis*/};
};
}
......
#ifndef __CONTROLLER_MANAGER_HPP__
#define __CONTROLLER_MANAGER_HPP__
#include <unordered_map>
#include <SDL.h>
#include <input/input.hpp>
#include <input/controller.hpp>
namespace gdw{
/*enum class controllerCursor {
none,
left,
right
};*/
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_;
//std::unordered_map<int, bool> used_controllers_;
//int controllerNr = -1;
//bool controllerUse = false;
//controllerCursor ccursor = controllerCursor::none;
//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);
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();
std::shared_ptr<gdw::controller> controller(int id);
SDL_GameController* get(int controller_id);
void up_event(const SDL_ControllerButtonEvent &e);
void down_event(const SDL_ControllerButtonEvent &e);
void added(const SDL_ControllerDeviceEvent &e);
void removed(const SDL_ControllerDeviceEvent &e);
//FOLLOWED METHODES ARE UNUSED
void axis_event(const SDL_ControllerAxisEvent &e) noexcept {/*handelt on another way, see: float controllerAxis(SDL_GameControllerAxis axis, int nr)*/};
void remapped(const SDL_ControllerDeviceEvent &e) noexcept {log << "input: controllerRemapped was triggerd but is not handelt" << std::endl;};
void joy_added(const SDL_JoyDeviceEvent &e) noexcept {/*controllerAdded*/};
void joy_removed(const SDL_JoyDeviceEvent &e) noexcept {/*controllerRemoved*/};
void joy_hat(const SDL_JoyHatEvent &e) noexcept {/*controllerDown / controllerUp*/};
void joy_down(const SDL_JoyButtonEvent &e) noexcept {/*controllerDown*/};
void joy_up(const SDL_JoyButtonEvent &e) noexcept {/*controllerUp*/};
void joy_ball(const SDL_JoyBallEvent &e) noexcept {log << "input: joyBall was triggerd but is not handelt" << std::endl;};
void joy_axis(const SDL_JoyAxisEvent &e) noexcept {/*controllerAxis*/};
};
}
#endif
......@@ -16,13 +16,8 @@
#include <util/logger.hpp>
namespace gdw{
/*enum class controllerCursor {
none,
left,
right
};*/
class engine;
class controller_manager;
class mouse;
class keyboard;
class controller;
......@@ -30,32 +25,17 @@ namespace gdw{
class input_events;
class input{
private:
std::string controller_database_path_ = "input/gamecontrollerdb.txt";
std::shared_ptr<gdw::keyboard> keyboard_;
std::shared_ptr<gdw::mouse> mouse_;
std::shared_ptr<input_mapping> mapping_;
std::shared_ptr<input_events> events_;
//std::shared_ptr<gdw::controller> controller_;
std::shared_ptr<gdw::input_mapping> mapping_;
std::shared_ptr<gdw::input_events> events_;
std::shared_ptr<gdw::controller_manager> controller_manager_;
engine &engine_;
glm::vec2 pos_;
int windowWidth_ = 800;
int windowHeight_ = 600;
//std::unordered_map<int, bool> used_controllers_;
//int controllerNr = -1;
//std::shared_ptr<const asset> controllerDB_;
//std::unordered_map<int, controller> controllers_;
//bool controllerUse = false;
//controllerCursor ccursor = controllerCursor::none;
//controllerCursor getCCursorFromName(std::string name);
int first_controller();
bool isController(int id);
public:
input(engine& engine);
~input();
......@@ -128,13 +108,12 @@ namespace gdw{
/**y motion of the mouse wheel*/
int mouseWheelY();
int controller_count();
std::shared_ptr<gdw::keyboard> keyboard() noexcept {return keyboard_;};
std::shared_ptr<gdw::controller> controller(int id = -1);
std::shared_ptr<gdw::controller_manager> controller_manager() noexcept {return controller_manager_;};
std::shared_ptr<gdw::mouse> mouse() noexcept {return mouse_;};
std::shared_ptr<input_events> events() noexcept {return events_;};
std::shared_ptr<input_mapping> mapping() noexcept {return mapping_;};
std::shared_ptr<gdw::input_events> events() noexcept {return events_;};
std::shared_ptr<gdw::input_mapping> mapping() noexcept {return mapping_;};
};
}
......
......@@ -6,6 +6,7 @@
#include <input/keyboard.hpp>
#include <input/mouse.hpp>
#include <input/controller.hpp>
#include <input/controller_manager.hpp>
namespace gdw{
class input;
......
......@@ -5,6 +5,20 @@ namespace gdw{
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;
}else{
if(axisIt.second.released)axisIt.second.released = false;
}
axisIt.second.changed = false;
}*/
}
//EVENTS
void controller::down_event(const SDL_ControllerButtonEvent &e){//TODO
/*auto it = controllers_.find(e.which);
......@@ -20,38 +34,6 @@ namespace gdw{
}*/
}
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_;
......
#include <input/controller_manager.hpp>
namespace gdw{
controller_manager::controller_manager(input *input, engine& engine):
input_(input),
engine_(engine),
controllers_(){
controllerDB_ = 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);
//SDL_SetCursor(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_CROSSHAIR));
//if(controllerUse)SDL_ShowCursor(SDL_FALSE);
}
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;
}
}
}*/
}
/*if(ccursor != controllerCursor::none){
if(controllerUse){
float speed = controllerSpeed_ * delta;
if(ccursor == controllerCursor::left){
pos_.x += controllerAxis(SDL_CONTROLLER_AXIS_LEFTX) * speed;
pos_.y += controllerAxis(SDL_CONTROLLER_AXIS_LEFTY) * speed;;
}else if(ccursor == controllerCursor::right){
pos_.x += controllerAxis(SDL_CONTROLLER_AXIS_RIGHTX) * speed;;
pos_.y += controllerAxis(SDL_CONTROLLER_AXIS_RIGHTY) * speed;;
}
}
pos_.x = pos_.x <= windowWidth_ ? pos_.x : windowWidth_;
pos_.x = pos_.x >= 0.f ? pos_.x : 0.f;
pos_.y = pos_.y <= windowHeight_ ? pos_.y : windowHeight_;
pos_.y = pos_.y >= 0.f ? pos_.y : 0.f;
}*/
}
std::shared_ptr<gdw::controller> controller_manager::controller(int id){
return nullptr;
}
SDL_GameController* controller_manager::get(int controller_id){
return nullptr;
}
void controller_manager::reset(){
for(auto it:controllers_){
it.second->reset();
}
}
int controller_manager::first_controller(){//TODO MULTI CONTROLLER SUPPORT
if(controller_count() < 1)return -1;
/*for (auto& it:used_controllers_){
if(it.second == true){
return it.first;
}
}*/
return -1;
}
int controller_manager::controller_count(){
return 0;//used_controllers_.size() //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::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);
}*/
}
/*controllerCursor controller_manager::getCCursorFromName(std::string name){
if(name == "LEFT" || name == "left" || name == "Left"){
return controllerCursor::left;
}else if(name == "RIGHT" || name == "right" || name == "Right"){
return controllerCursor::right;
}
return controllerCursor::none;
}*/
bool controller_manager::is_controller(int id){
auto it = controllers_.find(id);
return SDL_IsGameController(id) && it != controllers_.end();
}
bool controller_manager::pressed_button(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;
}
}
}else{
auto it = controllers_.find(id);
if(it != controllers_.end()){
if(it.second->pressed(mapping_->controller_button(id))){
return true;
}
}
}
}*/
return false;
}
bool controller_manager::down_button(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;
}
}
}else{
auto it = controllers_.find(id);
if(it != controllers_.end()){
if(it.second->down(mapping_->controller_button(id))){
return true;
}
}
}
}*/
return false;
}
bool controller_manager::released_button(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;
}
}
}else{
auto it = controllers_.find(id);
if(it != controllers_.end()){
if(it.second->released(mapping_->controller_button(id))){
return true;
}
}
}
}*/
return false;
}
bool controller_manager::pressed_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;
}
}
}
}*/
}
return false;
}
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){
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;
}
}
}*/
}
return false;
}
bool controller_manager::released_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;
}
}
}*/
}
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;
}
}
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;
}
/*
//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);
}
}
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);
}
}
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);
}
}
*/
}
......@@ -10,25 +10,17 @@
#include <input/input_mapping.hpp>
#include <input/input_util.hpp>
#include <input/input_events.hpp>
#include <input/controller_manager.hpp>
namespace gdw{
input::input(engine& engine):engine_(engine),
pos_(0,0)/*,
controllers_()*/{
pos_(0,0){
keyboard_ = std::make_shared<gdw::keyboard>(this);
mouse_ = std::make_shared<gdw::mouse>(this);
events_ = std::make_shared<input_events>(this);
mapping_ = std::make_shared<input_mapping>(this, engine_);
//controller_ = std::make_shared<gdw::controller>(this);
//controllerDB_ = engine.asset_manager().load(engine.asset_manager().native_name(controllerDBPath_));
//ccursor = getCCursorFromName(engine_.getConfig().get<std::string>("controller_cursor","RIGHT"));
//controllerUse = engine_.getConfig().get<bool>("use_controller",false);
//SDL_SetCursor(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_CROSSHAIR));
//if(controllerUse)SDL_ShowCursor(SDL_FALSE);
events_ = std::make_shared<gdw::input_events>(this);
mapping_ = std::make_shared<gdw::input_mapping>(this, engine_);
controller_manager_ = std::make_shared<gdw::controller_manager>(this, engine_);
windowWidth_ = engine_.getConfig().get<int>("width",800);
windowHeight_ = engine_.getConfig().get<int>("height",600);
......@@ -43,90 +35,13 @@ namespace gdw{
void input::update(float delta){
pos_.x = mouse_->position_x();
pos_.y = mouse_->position_y();
//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;
}
}
}
}
if(ccursor != controllerCursor::none){
if(controllerUse){
float speed = controllerSpeed_ * delta;
if(ccursor == controllerCursor::left){
pos_.x += controllerAxis(SDL_CONTROLLER_AXIS_LEFTX) * speed;
pos_.y += controllerAxis(SDL_CONTROLLER_AXIS_LEFTY) * speed;;
}else if(ccursor == controllerCursor::right){
pos_.x += controllerAxis(SDL_CONTROLLER_AXIS_RIGHTX) * speed;;
pos_.y += controllerAxis(SDL_CONTROLLER_AXIS_RIGHTY) * speed;;
}
}
pos_.x = pos_.x <= windowWidth_ ? pos_.x : windowWidth_;
pos_.x = pos_.x >= 0.f ? pos_.x : 0.f;
pos_.y = pos_.y <= windowHeight_ ? pos_.y : windowHeight_;
pos_.y = pos_.y >= 0.f ? pos_.y : 0.f;
}*/
controller_manager_->update(delta);