Commit 185dbdfd authored by Benjamin 'Albsi' Albsmeier's avatar Benjamin 'Albsi' Albsmeier
Browse files

a bit cleanup in input.hpp

parent 6ff9f1ef
...@@ -51,36 +51,32 @@ namespace gdw{ ...@@ -51,36 +51,32 @@ namespace gdw{
class engine; class engine;
class input{ class input{
private: private:
//CONSTS
std::string controllerDBPath_ = "input/gamecontrollerdb.txt"; std::string controllerDBPath_ = "input/gamecontrollerdb.txt";
static float constexpr controllerAxisMax_ = 32767.f; static float constexpr controllerAxisMax_ = 32767.f;
static float constexpr controllerAxisDeadZone_ = .3f; static float constexpr controllerAxisDeadZone_ = .3f;
static float constexpr controllerSpeed_ = 800.f; static float constexpr controllerSpeed_ = 800.f;
/**inputMappingStruct.type for key*/
static unsigned int constexpr key1 = 0x01; static unsigned int constexpr key1 = 0x01;
static unsigned int constexpr key2 = 0x02; static unsigned int constexpr key2 = 0x02;
/**inputMappingStruct.type for mouse button*/
static unsigned int constexpr mbutton = 0x04; static unsigned int constexpr mbutton = 0x04;
/**inputMappingStruct.type for controller button*/
static unsigned int constexpr cbutton = 0x08; static unsigned int constexpr cbutton = 0x08;
/**inputMappingStruct.type for controller axis*/
static unsigned int constexpr caxis = 0x10; static unsigned int constexpr caxis = 0x10;
public: public:
input(engine& engine); input(engine& engine);
~input(); ~input();
/**updates the keys and computes the SDL_Events /**needs to be called in the event loop*/
* should called for every event*/
void update(const SDL_Event &event); void update(const SDL_Event &event);
/**resets some data /**needs to be called after the the event loop but in the game loop*/
* should called once per game loop, at the end*/ void calcPosition(float delta);
/**needs to be called at the end of every game loop*/
void reset(); void reset();
/***/
bool addMapping(std::string name); bool addMapping(std::string name);
void calcPosition(float delta);
/**Returns true every frame, as long as it is down - NOT FOR MULTI CONTROLLER*/ /**Returns true every frame, as long as it is down - NOT FOR MULTI CONTROLLER*/
bool isPressed(std::string id); bool isPressed(std::string id);
...@@ -90,35 +86,11 @@ namespace gdw{ ...@@ -90,35 +86,11 @@ namespace gdw{
/**Returns true once it is released - NOT FOR MULTI CONTROLLER*/ /**Returns true once it is released - NOT FOR MULTI CONTROLLER*/
bool isReleased(std::string id); bool isReleased(std::string id);
/**Returns true every frame, as long as the key is down.*/ /**returns x position of the mouse cursor or of the controller cross-hair*/
private: int positionX()noexcept{return pos_.x;};
int isKeyDown(SDL_Keycode key);
int isKeyDown(std::string key);
/**Returns true once the key is pressed*/
bool isKeyPressed(SDL_Keycode key);
bool isKeyPressed(std::string key);
/**Returns true once the key is released*/
bool isKeyReleased(SDL_Keycode key);
bool isKeyReleased(std::string key);
/**Returns true every frame, as long as the mouse button is down.*/
bool isMouseButtonDown(int button);
bool isMouseButtonDown(std::string button);
/**Returns true once the mouse button is pressed*/
bool isMouseButtonPressed(int button);
bool isMouseButtonPressed(std::string button);
/**Returns true once the mouse button is released*/
bool isMouseButtonReleased(int button);
bool isMouseButtonReleased(std::string button);
public: /**returns y position of the mouse cursor or of the controller cross-hair*/
/**Returns a glm::vec2 with the x and y motion of the mouse wheel int positionY()noexcept{return pos_.y;};
* y is the motion a nomal mouse wheel can perform*/
glm::vec2 mouseWheel()noexcept{return mousePos_;};
/**x motion of the mouse wheel*/ /**x motion of the mouse wheel*/
int mouseWheelX()noexcept{return mouseWheel_.x;}; int mouseWheelX()noexcept{return mouseWheel_.x;};
...@@ -126,66 +98,37 @@ namespace gdw{ ...@@ -126,66 +98,37 @@ namespace gdw{
/**y motion of the mouse wheel*/ /**y motion of the mouse wheel*/
int mouseWheelY()noexcept{return mouseWheel_.y;}; int mouseWheelY()noexcept{return mouseWheel_.y;};
/**returns a glm::vec2 with the x and y position of the mouse cursor*/
glm::vec2 mousePos()noexcept{return mousePos_;};
/**returns x position of the mouse cursor*/
int mousePosX()noexcept{return mousePos_.x;};
/**returns y position of the mouse cursor*/
int mousePosY()noexcept{return mousePos_.y;};
/**Returns true every frame, as long as the controller button is down.*/ /**Returns true every frame, as long as the controller button is down.*/
private:
bool isControllerButtonDown(SDL_GameControllerButton button, int nr = 0);
public:
bool isControllerButtonDown(std::string button, int nr = 0) noexcept {return isControllerButtonDown(getCButtonCodeByMapping(button), nr);}; bool isControllerButtonDown(std::string button, int nr = 0) noexcept {return isControllerButtonDown(getCButtonCodeByMapping(button), nr);};
/**Returns true once the controller button is pressed*/ /**Returns true once the controller button is pressed*/
private:
bool isControllerButtonPressed(SDL_GameControllerButton button, int nr = 0);
public:
bool isControllerButtonPressed(std::string button, int nr = 0) noexcept {return isControllerButtonPressed(getCButtonCodeByMapping(button), nr);}; bool isControllerButtonPressed(std::string button, int nr = 0) noexcept {return isControllerButtonPressed(getCButtonCodeByMapping(button), nr);};
/**Returns true once the controller button is released*/ /**Returns true once the controller button is released*/
private:
bool isControllerButtonReleased(SDL_GameControllerButton button, int nr = 0);
public:
bool isControllerButtonReleased(std::string button, int nr = 0) noexcept {return isControllerButtonReleased(getCButtonCodeByMapping(button), nr);}; bool isControllerButtonReleased(std::string button, int nr = 0) noexcept {return isControllerButtonReleased(getCButtonCodeByMapping(button), nr);};
/**Returns the value of that axis as a float between -1 and 1*/ /**Returns the value of that axis as a float between -1 and 1*/
private:
float controllerAxis(SDL_GameControllerAxis axis, int nr = 0);
public:
float controllerAxis(std::string axis, int nr = 0) noexcept {return controllerAxis(getCAxisCodeByMapping(axis), nr);}; float controllerAxis(std::string axis, int nr = 0) noexcept {return controllerAxis(getCAxisCodeByMapping(axis), nr);};
/**Returns number of aktiv controllers*/ /**Returns number of controllers*/
int controllerCount() noexcept {return controllers_.size();}; int controllerCount() noexcept {return controllers_.size();};
/**Returns true if a controller exists with that id*/ /**Returns true if a controller exists with that id*/
bool isController(int id); bool isController(int id);
private: private:
std::shared_ptr<const asset> controllerDB_; std::shared_ptr<const asset> controllerDB_;
std::unordered_map<int, controller> controllers_;
std::map<std::string, gdw::inputMappingStruct> mappings_; std::map<std::string, gdw::inputMappingStruct> mappings_;
std::unordered_map<int, controller> controllers_;
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);
int firstController();
std::unordered_map<int, bool> usedControllers_; 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_; controller controller_;
engine &engine_; engine &engine_;
const Uint8 *keyState_;
glm::vec2 mousePos_; glm::vec2 mousePos_;
glm::vec2 oldMousePos_; glm::vec2 oldMousePos_;
glm::vec2 mouseWheel_; glm::vec2 mouseWheel_;
...@@ -193,19 +136,42 @@ namespace gdw{ ...@@ -193,19 +136,42 @@ namespace gdw{
int windowWidth_ = 800; int windowWidth_ = 800;
int windowHeight_ = 600; int windowHeight_ = 600;
bool controllerUse = false; bool controllerUse = false;
controllerCursor getCCursorFromName(std::string name);
controllerCursor ccursor = controllerCursor::none; controllerCursor ccursor = controllerCursor::none;
const Uint8 *keyState_; //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::unordered_map<int, int> mousePressedMap_; //
std::unordered_map<int, int> mouseReleasedMap_; SDL_Keycode getKeyCodeByMapping(std::string id, int nr);
std::unordered_map<int, int> keyMap_; int getMButtonCodeByMapping(std::string id);
std::unordered_map<int, int> keyReleasedMap_; 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);
int firstController();
controllerCursor getCCursorFromName(std::string name);
//EVENT HANDLING
//KEYBOARD //KEYBOARD
void keyDown(const SDL_KeyboardEvent &e); void keyDown(const SDL_KeyboardEvent &e);
void keyUp(const SDL_KeyboardEvent &e); void keyUp(const SDL_KeyboardEvent &e);
...@@ -219,46 +185,28 @@ namespace gdw{ ...@@ -219,46 +185,28 @@ namespace gdw{
void controllerDown(const SDL_ControllerButtonEvent &e); void controllerDown(const SDL_ControllerButtonEvent &e);
void controllerAdded(const SDL_ControllerDeviceEvent &e); void controllerAdded(const SDL_ControllerDeviceEvent &e);
void controllerRemoved(const SDL_ControllerDeviceEvent &e); void controllerRemoved(const SDL_ControllerDeviceEvent &e);
void controllerRemapped(const SDL_ControllerDeviceEvent &e) void controllerRemapped(const SDL_ControllerDeviceEvent &e) noexcept {log << "input: controllerRemapped was triggerd but is not handelt" << std::endl;};
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)*/};
void controllerAxis(const SDL_ControllerAxisEvent &e)
noexcept {/*handelt on another way, see: float controllerAxis(SDL_GameControllerAxis axis, int nr)*/};
//TEXTINPUT //TEXTINPUT
void textEdit(const SDL_TextEditingEvent &e) void textEdit(const SDL_TextEditingEvent &e) noexcept {log << "input: textInput was triggerd but is not handelt" << std::endl;};
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*/};
void textInput(const SDL_TextInputEvent &e)
noexcept {/*spams the log on every key input - not handelt*/};
//TOUCH //TOUCH
void touchMotion(const SDL_TouchFingerEvent &e) void touchMotion(const SDL_TouchFingerEvent &e) noexcept {log << "input: touchMotion was triggerd but is not handelt" << std::endl;};
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 touchDown(const SDL_TouchFingerEvent &e) void touchUp(const SDL_TouchFingerEvent &e) noexcept {log << "input: touchUp was triggerd but is not handelt" << std::endl;};
noexcept {log << "input: touchDown 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 touchUp(const SDL_TouchFingerEvent &e) void touchComplexGesture(const SDL_DollarGestureEvent &e) noexcept {log << "input: touchComplexGesture was triggerd but is not handelt" << std::endl;};
noexcept {log << "input: touchUp 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;};
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 //JOYSTICK
void joyAdded(const SDL_JoyDeviceEvent &e) void joyAdded(const SDL_JoyDeviceEvent &e) noexcept {/*controllerAdded*/};
noexcept {/*controllerAdded*/}; void joyRemoved(const SDL_JoyDeviceEvent &e) noexcept {/*controllerRemoved*/};
void joyRemoved(const SDL_JoyDeviceEvent &e) void joyHat(const SDL_JoyHatEvent &e) noexcept {/*controllerDown / controllerUp*/};
noexcept {/*controllerRemoved*/}; void joyDown(const SDL_JoyButtonEvent &e) noexcept {/*controllerDown*/};
void joyHat(const SDL_JoyHatEvent &e) void joyUp(const SDL_JoyButtonEvent &e) noexcept {/*controllerUp*/};
noexcept {/*controllerDown / controllerUp*/}; void joyBall(const SDL_JoyBallEvent &e) noexcept {log << "input: joyBall was triggerd but is not handelt" << std::endl;};
void joyDown(const SDL_JoyButtonEvent &e) void joyAxis(const SDL_JoyAxisEvent &e) noexcept {/*controllerAxis*/};
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 //DROP
void drop(const SDL_DropEvent &e) void drop(const SDL_DropEvent &e) noexcept {log << "input: drop was triggerd but is not handelt" << std::endl;};
noexcept {log << "input: drop was triggerd but is not handelt" << std::endl;};
}; };
} }
......
...@@ -57,14 +57,13 @@ namespace gdw { ...@@ -57,14 +57,13 @@ namespace gdw {
void engine::update(float delta_time) { void engine::update(float delta_time) {
physics_system_->update(1.f/60.f); //Update fix 60 Hz physics_system_->update(1.f/60.f); //Update fix 60 Hz
input_->calcPosition(delta_time);
game_state_machine_->update(delta_time); game_state_machine_->update(delta_time);
graphics_system_->begin(); graphics_system_->begin();
rendering_system_->update(delta_time); rendering_system_->update(delta_time);
ui_system_->render(delta_time); ui_system_->render(delta_time);
graphics_system_->end(delta_time); graphics_system_->end(delta_time);
} }
void engine::run() { void engine::run() {
......
...@@ -49,8 +49,8 @@ namespace gdw { ...@@ -49,8 +49,8 @@ namespace gdw {
auto& input = engine_.input(); auto& input = engine_.input();
auto& level_manager = engine_.game_play_system().level_manager(); auto& level_manager = engine_.game_play_system().level_manager();
float x = (input.mousePos().x/engine_.graphics_system().width()) * 2.0f -1; float x = (float(input.positionX())/engine_.graphics_system().width()) * 2.0f -1;
float y = (input.mousePos().y/engine_.graphics_system().height()) * 2.0f -1; float y = (float(input.positionY())/engine_.graphics_system().height()) * 2.0f -1;
auto cam = level_manager.camera()->component<camera_component>(); auto cam = level_manager.camera()->component<camera_component>();
auto camera = level_manager.camera(); auto camera = level_manager.camera();
...@@ -87,13 +87,14 @@ namespace gdw { ...@@ -87,13 +87,14 @@ namespace gdw {
/*if (input.isPressed("test")){ /*if (input.isPressed("test")){
log << "input test pressed" << std::endl; log << "input test pressed" << std::endl;
} }*/
if (input.isDown("test")){ /*if (input.isDown("test")){
log << "input test down" << std::endl; log << "pos: x:" << input.positionX() << " y:" << input.positionY() << std::endl;
} //log << "input test down" << std::endl;
}*/
if (input.isReleased("test")){ /*if (input.isReleased("test")){
log << "input test released" << std::endl; log << "input test released" << std::endl;
}*/ }*/
......
...@@ -6,8 +6,13 @@ ...@@ -6,8 +6,13 @@
#include <util/config.hpp> #include <util/config.hpp>
namespace gdw{ namespace gdw{
input::input(engine& engine):engine_(engine),mousePos_(0,0),mouseWheel_(0,0), input::input(engine& engine):engine_(engine),
controllers_(),mappings_(){ mousePos_(0,0),
oldMousePos_(0,0),
pos_(0,0),
mouseWheel_(0,0),
controllers_(),
mappings_(){
keyState_ = SDL_GetKeyboardState(NULL); keyState_ = SDL_GetKeyboardState(NULL);
controllerDB_ = engine.asset_manager().load(engine.asset_manager().native_name(controllerDBPath_)); controllerDB_ = engine.asset_manager().load(engine.asset_manager().native_name(controllerDBPath_));
...@@ -164,9 +169,9 @@ namespace gdw{ ...@@ -164,9 +169,9 @@ namespace gdw{
} }
} }
pos_.x = mousePos_.x;
pos_.y = mousePos_.y;
if(ccursor != controllerCursor::none){ if(ccursor != controllerCursor::none){
pos_.x += mousePos_.x - oldMousePos_.x;
pos_.y += mousePos_.y - oldMousePos_.y;
if(controllerUse){ if(controllerUse){
float speed = controllerSpeed_ * delta; float speed = controllerSpeed_ * delta;
if(ccursor == controllerCursor::left){ if(ccursor == controllerCursor::left){
......
...@@ -56,8 +56,8 @@ namespace gdw { ...@@ -56,8 +56,8 @@ namespace gdw {
//engine_.graphics_system().height() //engine_.graphics_system().height()
//glClearStencil( 0 ); //glClearStencil( 0 );
//nvgluBindFramebuffer( NVGFrameBuffer_ ); //nvgluBindFramebuffer( NVGFrameBuffer_ );
float mousex = engine_.input().mousePosX(); float mousex = engine_.input().positionX();
float mousey = engine_.input().mousePosY(); float mousey = engine_.input().positionY();
for(auto& widget : child_widgets_) { for(auto& widget : child_widgets_) {
handleInput(mousex,mousey,widget.get()); handleInput(mousex,mousey,widget.get());
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment