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{
class engine;
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;
/**inputMappingStruct.type for key*/
static unsigned int constexpr key1 = 0x01;
static unsigned int constexpr key2 = 0x02;
/**inputMappingStruct.type for mouse button*/
static unsigned int constexpr mbutton = 0x04;
/**inputMappingStruct.type for controller button*/
static unsigned int constexpr cbutton = 0x08;
/**inputMappingStruct.type for controller axis*/
static unsigned int constexpr caxis = 0x10;
public:
input(engine& engine);
~input();
/**updates the keys and computes the SDL_Events
* should called for every event*/
/**needs to be called in the event loop*/
void update(const SDL_Event &event);
/**resets some data
* should called once per game loop, at the end*/
/**needs to be called after the the event loop but in the game loop*/
void calcPosition(float delta);
/**needs to be called at the end of every game loop*/
void reset();
/***/
bool addMapping(std::string name);
void calcPosition(float delta);
/**Returns true every frame, as long as it is down - NOT FOR MULTI CONTROLLER*/
bool isPressed(std::string id);
......@@ -90,35 +86,11 @@ namespace gdw{
/**Returns true once it is released - NOT FOR MULTI CONTROLLER*/
bool isReleased(std::string id);
/**Returns true every frame, as long as the key is down.*/
private:
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);
/**returns x position of the mouse cursor or of the controller cross-hair*/
int positionX()noexcept{return pos_.x;};
public:
/**Returns a glm::vec2 with the x and y motion of the mouse wheel
* y is the motion a nomal mouse wheel can perform*/
glm::vec2 mouseWheel()noexcept{return mousePos_;};
/**returns y position of the mouse cursor or of the controller cross-hair*/
int positionY()noexcept{return pos_.y;};
/**x motion of the mouse wheel*/
int mouseWheelX()noexcept{return mouseWheel_.x;};
......@@ -126,66 +98,37 @@ namespace gdw{
/**y motion of the mouse wheel*/
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.*/
private:
bool isControllerButtonDown(SDL_GameControllerButton button, int nr = 0);
public:
bool isControllerButtonDown(std::string button, int nr = 0) noexcept {return isControllerButtonDown(getCButtonCodeByMapping(button), nr);};
/**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);};
/**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);};
/**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);};
/**Returns number of aktiv controllers*/
/**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::unordered_map<int, controller> controllers_;
std::map<std::string, gdw::inputMappingStruct> mappings_;
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, 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_;
......@@ -193,19 +136,42 @@ namespace gdw{
int windowWidth_ = 800;
int windowHeight_ = 600;
bool controllerUse = false;
controllerCursor getCCursorFromName(std::string name);
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_;
std::unordered_map<int, int> keyMap_;
std::unordered_map<int, int> keyReleasedMap_;
//
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();
controllerCursor getCCursorFromName(std::string name);
//EVENT HANDLING
//KEYBOARD
void keyDown(const SDL_KeyboardEvent &e);
void keyUp(const SDL_KeyboardEvent &e);
......@@ -219,46 +185,28 @@ namespace gdw{
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)*/};
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*/};
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;};
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*/};
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;};
void drop(const SDL_DropEvent &e) noexcept {log << "input: drop was triggerd but is not handelt" << std::endl;};
};
}
......
......@@ -57,14 +57,13 @@ namespace gdw {
void engine::update(float delta_time) {
physics_system_->update(1.f/60.f); //Update fix 60 Hz
input_->calcPosition(delta_time);
game_state_machine_->update(delta_time);
graphics_system_->begin();
rendering_system_->update(delta_time);
ui_system_->render(delta_time);
graphics_system_->end(delta_time);
}
void engine::run() {
......
......@@ -49,8 +49,8 @@ namespace gdw {
auto& input = engine_.input();
auto& level_manager = engine_.game_play_system().level_manager();
float x = (input.mousePos().x/engine_.graphics_system().width()) * 2.0f -1;
float y = (input.mousePos().y/engine_.graphics_system().height()) * 2.0f -1;
float x = (float(input.positionX())/engine_.graphics_system().width()) * 2.0f -1;
float y = (float(input.positionY())/engine_.graphics_system().height()) * 2.0f -1;
auto cam = level_manager.camera()->component<camera_component>();
auto camera = level_manager.camera();
......@@ -87,13 +87,14 @@ namespace gdw {
/*if (input.isPressed("test")){
log << "input test pressed" << std::endl;
}
}*/
if (input.isDown("test")){
log << "input test down" << std::endl;
}
/*if (input.isDown("test")){
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;
}*/
......
......@@ -6,8 +6,13 @@
#include <util/config.hpp>
namespace gdw{
input::input(engine& engine):engine_(engine),mousePos_(0,0),mouseWheel_(0,0),
controllers_(),mappings_(){
input::input(engine& engine):engine_(engine),
mousePos_(0,0),
oldMousePos_(0,0),
pos_(0,0),
mouseWheel_(0,0),
controllers_(),
mappings_(){
keyState_ = SDL_GetKeyboardState(NULL);
controllerDB_ = engine.asset_manager().load(engine.asset_manager().native_name(controllerDBPath_));
......@@ -164,9 +169,9 @@ namespace gdw{
}
}
pos_.x = mousePos_.x;
pos_.y = mousePos_.y;
if(ccursor != controllerCursor::none){
pos_.x += mousePos_.x - oldMousePos_.x;
pos_.y += mousePos_.y - oldMousePos_.y;
if(controllerUse){
float speed = controllerSpeed_ * delta;
if(ccursor == controllerCursor::left){
......
......@@ -56,8 +56,8 @@ namespace gdw {
//engine_.graphics_system().height()
//glClearStencil( 0 );
//nvgluBindFramebuffer( NVGFrameBuffer_ );
float mousex = engine_.input().mousePosX();
float mousey = engine_.input().mousePosY();
float mousex = engine_.input().positionX();
float mousey = engine_.input().positionY();
for(auto& widget : child_widgets_) {
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