Commit 8a503685 authored by Benjamin 'Albsi' Albsmeier's avatar Benjamin 'Albsi' Albsmeier
Browse files

splited input in 7 files - mapping, mouse and keyboard works - controller not

parent 6d5a4bb1
#ifndef __CONTROLLER_HPP__
#define __CONTROLLER_HPP__
#include <unordered_map>
#include <SDL.h>
#include <input/input.hpp>
namespace gdw{
struct axis_struct{
bool changed = false;
int down = 0;
bool released = false;
};
class controller{
private:
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_;
public:
controller(input *input);
~controller();
SDL_GameController* get();
bool down(SDL_GameControllerButton button);
bool pressed(SDL_GameControllerButton button);
bool released(SDL_GameControllerButton button);
float axis(SDL_GameControllerAxis axis);
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*/};
};
}
#endif
......@@ -3,8 +3,10 @@
// input/input.hpp for GDW SS15
// mouse, keyboard input
//****************************************
#ifndef __INPUT_HPP__
#define __INPUT_HPP__
#include <memory>
#include <map>
#include <unordered_map>
#include <vector>
#include <SDL.h>
......@@ -13,63 +15,56 @@
#include <core/engine.hpp>
#include <util/logger.hpp>
#ifndef __INPUT_HPP__
#define __INPUT_HPP__
namespace gdw{
struct axisStruct{
bool changed = false;
int down = 0;
bool released = false;
};
struct controller {
SDL_GameController *sdlController;
int controllerNr = -1;
std::unordered_map<int, bool> controllerMap;
std::unordered_map<int, bool> controllerReleasedMap;
std::unordered_map<int, axisStruct> axisMap;
bool active = false;
};
struct inputMappingStruct {
unsigned int type = 0;
bool negativAxis = false;
SDL_Keycode keyCode1 = 0;
SDL_Keycode keyCode2 = 0;
int mbuttonCode = -1;
SDL_GameControllerButton cbuttonCode = SDL_CONTROLLER_BUTTON_INVALID;
SDL_GameControllerAxis caxisCode = SDL_CONTROLLER_AXIS_INVALID;
};
enum class controllerCursor {
/*enum class controllerCursor {
none,
left,
right
};
};*/
class engine;
class mouse;
class keyboard;
class controller;
class input_mapping;
class input_events;
class input{
private:
//CONSTS
std::string controllerDBPath_ = "input/gamecontrollerdb.txt";
static float constexpr controllerAxisMax_ = 32767.f;
static float constexpr controllerAxisDeadZone_ = .3f;
static float constexpr controllerSpeed_ = 800.f;
static unsigned int constexpr key1 = 0x01;
static unsigned int constexpr key2 = 0x02;
static unsigned int constexpr mbutton = 0x04;
static unsigned int constexpr cbutton = 0x08;
static unsigned int constexpr caxis = 0x10;
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_;
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();
/**needs to be called in the event loop*/
void update(const SDL_Event &event);
void update_events(const SDL_Event &event);
/**needs to be called after the the event loop but in the game loop*/
void calcPosition(float delta);
void update(float delta);
/**needs to be called at the end of every game loop*/
void reset();
......@@ -101,149 +96,45 @@ namespace gdw{
* controller axis: <name>_controller_axis : <axis> + '_positiv' or '_negativ' (ignored if trigger)
* example: test_controller_axis : leftx_positiv
*/
bool addMapping(std::string name);
void addMapping(std::string name);
/**prints the mapping*/
void printMapping();
/**Returns true every frame, as long as it is down - NOT FOR MULTI CONTROLLER*/
bool isPressed(std::string id);
/**Returns true every frame, as long as it is down
* controller_id only for multi controller
*/
bool isPressed(std::string mapping, int controller_id = -1);
/**Returns true once it is pressed - NOT FOR MULTI CONTROLLER*/
bool isDown(std::string id);
/**Returns true once it is pressed
* controller_id only for multi controller
*/
bool isDown(std::string mapping, int controller_id = -1);
/**Returns true once it is released - NOT FOR MULTI CONTROLLER*/
bool isReleased(std::string id);
/**Returns true once it is released
* controller_id only for multi controller
*/
bool isReleased(std::string mapping, int controller_id = -1);
/**returns x position of the mouse cursor or of the controller cross-hair*/
int positionX()noexcept{return pos_.x;};
int positionX();
/**returns y position of the mouse cursor or of the controller cross-hair*/
int positionY()noexcept{return pos_.y;};
int positionY();
/**x motion of the mouse wheel*/
int mouseWheelX()noexcept{return mouseWheel_.x;};
int mouseWheelX();
/**y motion of the mouse wheel*/
int mouseWheelY()noexcept{return mouseWheel_.y;};
/**Returns true every frame, as long as the controller button is down.*/
bool isControllerButtonDown(std::string button, int nr = 0) noexcept {return isControllerButtonDown(getCButtonCodeByMapping(button), nr);};
/**Returns true once the controller button is pressed*/
bool isControllerButtonPressed(std::string button, int nr = 0) noexcept {return isControllerButtonPressed(getCButtonCodeByMapping(button), nr);};
int mouseWheelY();
/**Returns true once the controller button is released*/
bool isControllerButtonReleased(std::string button, int nr = 0) noexcept {return isControllerButtonReleased(getCButtonCodeByMapping(button), nr);};
int controller_count();
/**Returns the value of that axis as a float between -1 and 1*/
float controllerAxis(std::string axis, int nr = 0) noexcept {return controllerAxis(getCAxisCodeByMapping(axis), nr);};
/**Returns number of controllers*/
int controllerCount() noexcept {return controllers_.size();};
/**Returns true if a controller exists with that id*/
bool isController(int id);
private:
std::shared_ptr<const asset> controllerDB_;
std::map<std::string, gdw::inputMappingStruct> mappings_;
std::unordered_map<int, controller> controllers_;
std::unordered_map<int, bool> usedControllers_;
std::unordered_map<int, int> mousePressedMap_;
std::unordered_map<int, int> mouseReleasedMap_;
std::unordered_map<int, int> keyMap_;
std::unordered_map<int, int> keyReleasedMap_;
controller controller_;
engine &engine_;
const Uint8 *keyState_;
glm::vec2 mousePos_;
glm::vec2 oldMousePos_;
glm::vec2 mouseWheel_;
glm::vec2 pos_;
int windowWidth_ = 800;
int windowHeight_ = 600;
bool controllerUse = false;
controllerCursor ccursor = controllerCursor::none;
//OLD RETURN STUFF //TODO make nicer
int isKeyDown(SDL_Keycode key);
int isKeyDown(std::string key);
bool isKeyPressed(SDL_Keycode key);
bool isKeyPressed(std::string key);
bool isKeyReleased(SDL_Keycode key);
bool isKeyReleased(std::string key);
bool isMouseButtonDown(int button);
bool isMouseButtonDown(std::string button);
bool isMouseButtonPressed(int button);
bool isMouseButtonPressed(std::string button);
bool isMouseButtonReleased(int button);
bool isMouseButtonReleased(std::string button);
bool isControllerButtonDown(SDL_GameControllerButton button, int nr = 0);
bool isControllerButtonPressed(SDL_GameControllerButton button, int nr = 0);
bool isControllerButtonReleased(SDL_GameControllerButton button, int nr = 0);
float controllerAxis(SDL_GameControllerAxis axis, int nr = 0);
std::string makeStringLowerCase(std::string str);
std::string makeStringUpperCase(std::string inString);
//
std::string getMouseButtonName(int button);
std::string getKeyName(SDL_Keycode key);
std::string getControllerButtonName(SDL_GameControllerButton button);
std::string getControllerAxisName(SDL_GameControllerAxis axis);
SDL_Keycode getKeyCodeByMapping(std::string id, int nr);
int getMButtonCodeByMapping(std::string id);
SDL_GameControllerButton getCButtonCodeByMapping(std::string id);
SDL_GameControllerAxis getCAxisCodeByMapping(std::string id);
int getMButtonFromName(std::string name);
SDL_Keycode getKeyFromName(std::string name);
SDL_GameControllerButton getCButtonFromName(std::string name);
SDL_GameControllerAxis getCAxisFromName(std::string name);
bool isNegativAxis(std::string name);
int firstController();
controllerCursor getCCursorFromName(std::string name);
//EVENT HANDLING
//KEYBOARD
void keyDown(const SDL_KeyboardEvent &e);
void keyUp(const SDL_KeyboardEvent &e);
//MOUSE
void mouseDown(const SDL_MouseButtonEvent &e);
void mouseUp(const SDL_MouseButtonEvent &e);
void mouseMotion(const SDL_MouseMotionEvent &e);
void mouseWheel(const SDL_MouseWheelEvent &e);
//CONTROLLER
void controllerUp(const SDL_ControllerButtonEvent &e);
void controllerDown(const SDL_ControllerButtonEvent &e);
void controllerAdded(const SDL_ControllerDeviceEvent &e);
void controllerRemoved(const SDL_ControllerDeviceEvent &e);
void controllerRemapped(const SDL_ControllerDeviceEvent &e) noexcept {log << "input: controllerRemapped was triggerd but is not handelt" << std::endl;};
void controllerAxis(const SDL_ControllerAxisEvent &e) noexcept {/*handelt on another way, see: float controllerAxis(SDL_GameControllerAxis axis, int nr)*/};
//TEXTINPUT
void textEdit(const SDL_TextEditingEvent &e) noexcept {log << "input: textInput was triggerd but is not handelt" << std::endl;};
void textInput(const SDL_TextInputEvent &e) noexcept {/*spams the log on every key input - not handelt*/};
//TOUCH
void touchMotion(const SDL_TouchFingerEvent &e) noexcept {log << "input: touchMotion was triggerd but is not handelt" << std::endl;};
void touchDown(const SDL_TouchFingerEvent &e) noexcept {log << "input: touchDown was triggerd but is not handelt" << std::endl;};
void touchUp(const SDL_TouchFingerEvent &e) noexcept {log << "input: touchUp was triggerd but is not handelt" << std::endl;};
void touchGesture(const SDL_MultiGestureEvent &e) noexcept {log << "input: touchGesture was triggerd but is not handelt" << std::endl;};
void touchComplexGesture(const SDL_DollarGestureEvent &e) noexcept {log << "input: touchComplexGesture was triggerd but is not handelt" << std::endl;};
void touchGestureRecord(const SDL_DollarGestureEvent &e) noexcept {log << "input: touchGestureRecord was triggerd but is not handelt" << std::endl;};
//JOYSTICK
void joyAdded(const SDL_JoyDeviceEvent &e) noexcept {/*controllerAdded*/};
void joyRemoved(const SDL_JoyDeviceEvent &e) noexcept {/*controllerRemoved*/};
void joyHat(const SDL_JoyHatEvent &e) noexcept {/*controllerDown / controllerUp*/};
void joyDown(const SDL_JoyButtonEvent &e) noexcept {/*controllerDown*/};
void joyUp(const SDL_JoyButtonEvent &e) noexcept {/*controllerUp*/};
void joyBall(const SDL_JoyBallEvent &e) noexcept {log << "input: joyBall was triggerd but is not handelt" << std::endl;};
void joyAxis(const SDL_JoyAxisEvent &e) noexcept {/*controllerAxis*/};
//DROP
void drop(const SDL_DropEvent &e) noexcept {log << "input: drop was triggerd but is not handelt" << std::endl;};
std::shared_ptr<gdw::keyboard> keyboard() noexcept {return keyboard_;};
std::shared_ptr<gdw::controller> controller(int id = -1);
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_;};
};
}
......
#ifndef __INPUT_EVENTS_HPP__
#define __INPUT_EVENTS_HPP__
#include <SDL.h>
#include <input/input.hpp>
#include <input/keyboard.hpp>
#include <input/mouse.hpp>
#include <input/controller.hpp>
namespace gdw{
class input;
class input_events{
private:
input *input_;
public:
input_events(input *input);
~input_events();
void update(const SDL_Event &e);
//FOLLOED METHODES ARE UNUSED
//TEXTINPUT
void text_edit(const SDL_TextEditingEvent &e) noexcept {log << "input: textInput was triggerd but is not handelt" << std::endl;};
void text_input(const SDL_TextInputEvent &e) noexcept {/*spams the log on every key input - not handelt*/};
//TOUCH
void touch_motion(const SDL_TouchFingerEvent &e) noexcept {log << "input: touchMotion was triggerd but is not handelt" << std::endl;};
void touch_down(const SDL_TouchFingerEvent &e) noexcept {log << "input: touchDown was triggerd but is not handelt" << std::endl;};
void touch_up(const SDL_TouchFingerEvent &e) noexcept {log << "input: touchUp was triggerd but is not handelt" << std::endl;};
void touch_gesture(const SDL_MultiGestureEvent &e) noexcept {log << "input: touchGesture was triggerd but is not handelt" << std::endl;};
void touch_complex_gesture(const SDL_DollarGestureEvent &e) noexcept {log << "input: touchComplexGesture was triggerd but is not handelt" << std::endl;};
void touch_gesture_record(const SDL_DollarGestureEvent &e) noexcept {log << "input: touchGestureRecord was triggerd but is not handelt" << std::endl;};
//DROP
void drop(const SDL_DropEvent &e) noexcept {log << "input: drop was triggerd but is not handelt" << std::endl;};
};
}
#endif
#ifndef __INPUT_MAPPING_HPP__
#define __INPUT_MAPPING_HPP__
#include <map>
#include <SDL.h>
#include <input/input.hpp>
#include <input/keyboard.hpp>
#include <input/mouse.hpp>
#include <input/controller.hpp>
namespace gdw{
struct mapping{
unsigned int type = 0;
SDL_Keycode key_1 = SDLK_UNKNOWN;
SDL_Keycode key_2 = SDLK_UNKNOWN;
int mouse_button = -1;
SDL_GameControllerButton controller_button = SDL_CONTROLLER_BUTTON_INVALID;
SDL_GameControllerAxis controller_axis = SDL_CONTROLLER_AXIS_INVALID;
bool negativ = false;
};
class engine;
class input_mapping{
private:
engine &engine_;
input *input_;
static unsigned int constexpr key_1_ = 0x01;
static unsigned int constexpr key_2_ = 0x02;
static unsigned int constexpr mouse_button_ = 0x04;
static unsigned int constexpr controller_button_ = 0x08;
static unsigned int constexpr controller_axis_ = 0x10;
std::map<std::string, gdw::mapping> mappings_;
public:
input_mapping(input *input, engine& engine);
~input_mapping();
SDL_Keycode key_1(std::string id);
SDL_Keycode key_2(std::string id);
int mouse_button(std::string id);
SDL_GameControllerButton controller_button(std::string id);
SDL_GameControllerAxis controller_axis(std::string id);
bool add(std::string name);
void print();
bool is_mapping(std::string id);
};
}
#endif
#ifndef __INPUT_UTIL_HPP__
#define __INPUT_UTIL_HPP__
#include <input/input.hpp>
#include <SDL.h>
namespace gdw{
//class input;
class input_util{
//friend class input;
private:
input *input_;
public:
input_util(input *input);
~input_util();
static std::string to_lower_case(std::string inString);
static std::string to_upper_case(std::string inString);
static std::string get_key_name(SDL_Keycode key);
static SDL_Keycode get_key_from_name(std::string name);
static std::string get_mouse_button_name(int button);
static int get_mouse_button_from_name(std::string name);
static std::string get_controller_button_name(SDL_GameControllerButton button);
static SDL_GameControllerButton get_controller_button_from_name(std::string name);
static std::string get_controller_axis_name(SDL_GameControllerAxis axis);
static SDL_GameControllerAxis get_controller_axis_from_name(std::string name);
static bool controller_negativ_axis(std::string name);
};
}
#endif
#ifndef __KEYBOARD_HPP__
#define __KEYBOARD_HPP__
#include <unordered_map>
#include <SDL.h>
#include <input/input.hpp>
namespace gdw{
class keyboard{
private:
input *input_;
std::unordered_map<int, int> key_map_;
std::unordered_map<int, bool> released_map_;
const Uint8 *key_state_;
public:
keyboard(input *input);
~keyboard();
void reset();
int down(SDL_Keycode key);
bool pressed(SDL_Keycode key);
bool released(SDL_Keycode key);
void down_event(const SDL_KeyboardEvent &e);
void up_event(const SDL_KeyboardEvent &e);
};
}
#endif
#ifndef __MOUSE_HPP__
#define __MOUSE_HPP__
#include <unordered_map>
#include <SDL.h>
#include <glm/glm.hpp>
#include <input/input.hpp>
#include <input/input_util.hpp>
namespace gdw{
class mouse{
private:
input *input_;
std::unordered_map<int, int> pressed_map_;
std::unordered_map<int, int> released_map_;
glm::vec2 pos_;
glm::vec2 last_pos_;
glm::vec2 wheel_;
public:
mouse(input *input);
~mouse();
void reset();
bool down(int button);
bool pressed(int button);
bool released(int button);
int wheel_x();
int wheel_y();
int position_x();
int position_y();
int last_position_x();
int last_position_y();
void down_event(const SDL_MouseButtonEvent &e);
void up_event(const SDL_MouseButtonEvent &e);
void motion(const SDL_MouseMotionEvent &e);
void wheel(const SDL_MouseWheelEvent &e);
};
}
#endif
......@@ -57,7 +57,7 @@ namespace gdw {
void engine::update(float delta_time) {
physics_system_->update(1.f/60.f); //Update fix 60 Hz
input_->calcPosition(delta_time);
input_->update(delta_time);
game_state_machine_->update(delta_time);
graphics_system_->begin();
......@@ -77,7 +77,7 @@ namespace gdw {
if (event.type == SDL_QUIT) {
quit_ = true;
}
input_->update(event);
input_->update_events(event);
}
last_time = current_time;
current_time = static_cast<float>(SDL_GetTicks()) / 1000.f;
......
#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);