Commit 792b7d27 authored by Elias Broschin's avatar Elias Broschin
Browse files

Merge branch 'develop' into feature/gameplay/commons

parents 793db9fe 80afd2cf
......@@ -18,9 +18,13 @@ map_load_timer : 0
test_ui : false
mapGenDebug : false
player_move_left_key : A
player_move_left_key2 : Left
player_move_left_cbutton : LEFT
player_move_left_caxis : RIGHTX
player_move_right_key : D
player_move_right_key2 : Right
player_move_right_cbutton : RIGHT
player_move_right_caxis : RIGHTX
beam_normal_key : Q
beam_normal_mbutton : Left
beam_normal_caxis : LEFTTRIGGER
......
......@@ -38,7 +38,8 @@ namespace gdw{
struct inputMapingStruct {
unsigned int type = 0;
bool negativAxis = false;
SDL_Keycode keyCode = 0;
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;
......@@ -52,13 +53,14 @@ namespace gdw{
static float constexpr controllerAxisDeadZone_ = .3f;
/**inputMapingStruct.type for key*/
static unsigned int constexpr key = 0x1;
static unsigned int constexpr key1 = 0x01;
static unsigned int constexpr key2 = 0x02;
/**inputMapingStruct.type for mouse button*/
static unsigned int constexpr mbutton = 0x2;
static unsigned int constexpr mbutton = 0x04;
/**inputMapingStruct.type for controller button*/
static unsigned int constexpr cbutton = 0x4;
static unsigned int constexpr cbutton = 0x08;
/**inputMapingStruct.type for controller axis*/
static unsigned int constexpr caxis = 0x8;
static unsigned int constexpr caxis = 0x10;
public:
input(engine& engine);
~input();
......@@ -78,15 +80,15 @@ namespace gdw{
/**Returns true every frame, as long as the key is down.*/
private:
int isKeyDown(SDL_Keycode key) noexcept {return keyState_[SDL_GetScancodeFromKey(key)];};
int isKeyDown(inputMaping key) noexcept {return isKeyDown(getKeyCodeByMaping(key));};
int isKeyDown(inputMaping key) noexcept {return isKeyDown(getKeyCodeByMaping(key, 1)) || isKeyDown(getKeyCodeByMaping(key, 2));};
/**Returns true once the key is pressed*/
bool isKeyPressed(SDL_Keycode key) noexcept {return keyMap_[key]==1;};
bool isKeyPressed(inputMaping key) noexcept {return isKeyPressed(getKeyCodeByMaping(key));};
bool isKeyPressed(inputMaping key) noexcept {return isKeyPressed(getKeyCodeByMaping(key, 1)) || isKeyPressed(getKeyCodeByMaping(key, 2));};
/**Returns true once the key is released*/
bool isKeyReleased(SDL_Keycode key) noexcept {return keyReleasedMap_[key] && !keyState_[SDL_GetScancodeFromKey(key)];};
bool isKeyReleased(inputMaping key) noexcept {return isKeyReleased(getKeyCodeByMaping(key));};
bool isKeyReleased(inputMaping key) noexcept {return isKeyReleased(getKeyCodeByMaping(key, 1)) || isKeyReleased(getKeyCodeByMaping(key, 2));};
/**Returns true every frame, as long as the mouse button is down.*/
bool isMouseButtonDown(int button) noexcept {return SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(button);};
......@@ -157,7 +159,7 @@ namespace gdw{
void makeMapings();
bool isMapingOk(inputMapingStruct maping, std::string name);
SDL_Keycode getKeyCodeByMaping(inputMaping id);
SDL_Keycode getKeyCodeByMaping(inputMaping id, int nr);
int getMButtonCodeByMaping(inputMaping id);
SDL_GameControllerButton getCButtonCodeByMaping(inputMaping id);
SDL_GameControllerAxis getCAxisCodeByMaping(inputMaping id);
......
......@@ -30,20 +30,21 @@ namespace gdw{
* caxis only supports isDown
* if the axis should be true with negativ value than set 'negativAxis' to true (trigger can't become negativ)
*
* DON'T MISS TO ADD THE RIGHT TYPES [ key | mbutton | cbutton | caxis]!
* DON'T MISS TO ADD THE RIGHT TYPES [ key1 | mbutton | cbutton | caxis]!
* CAXIS DON'T TRIGGER 'PRESSED' OR 'RELEASED' IT ONLY TRIGGERS 'DOWN'!
*/
void input::makeMapings(){
inputMapingStruct state_switch_;
state_switch_.type = key + cbutton;
state_switch_.keyCode = getKeyFromName(engine_.getConfig().get<std::string>("state_switch_key","Escape"));
state_switch_.type = key1 + cbutton;
state_switch_.keyCode1 = getKeyFromName(engine_.getConfig().get<std::string>("state_switch_key","Escape"));
state_switch_.cbuttonCode = getCButtonFromName(engine_.getConfig().get<std::string>("state_switch_cbutton","START"));
isMapingOk(state_switch_, "state_switch");
mapings_.emplace(inputMaping::state_switch, state_switch_);
inputMapingStruct player_move_left_;
player_move_left_.type = key + cbutton + caxis;
player_move_left_.keyCode = getKeyFromName(engine_.getConfig().get<std::string>("player_move_left_key","A"));
player_move_left_.type = key1 + key2 + cbutton + caxis;
player_move_left_.keyCode1 = getKeyFromName(engine_.getConfig().get<std::string>("player_move_left_key","A"));
player_move_left_.keyCode2 = getKeyFromName(engine_.getConfig().get<std::string>("player_move_left_key2","A"));
player_move_left_.cbuttonCode = getCButtonFromName(engine_.getConfig().get<std::string>("player_move_left_cbutton","LEFT"));
player_move_left_.caxisCode = getCAxisFromName(engine_.getConfig().get<std::string>("player_move_left_caxis","RIGHTX"));
player_move_left_.negativAxis = true;
......@@ -51,33 +52,34 @@ namespace gdw{
mapings_.emplace(inputMaping::player_move_left, player_move_left_);
inputMapingStruct player_move_right_;
player_move_right_.type = key + cbutton + caxis;
player_move_right_.keyCode = getKeyFromName(engine_.getConfig().get<std::string>("player_move_right_key","D"));
player_move_right_.type = key1 + key2 + cbutton + caxis;
player_move_right_.keyCode1 = getKeyFromName(engine_.getConfig().get<std::string>("player_move_right_key","D"));
player_move_right_.keyCode2 = getKeyFromName(engine_.getConfig().get<std::string>("player_move_right_key2","A"));
player_move_right_.cbuttonCode = getCButtonFromName(engine_.getConfig().get<std::string>("player_move_right_cbutton","RIGHT"));
player_move_right_.caxisCode = getCAxisFromName(engine_.getConfig().get<std::string>("player_move_right_caxis","RIGHTX"));
isMapingOk(player_move_right_, "player_move_right");
mapings_.emplace(inputMaping::player_move_right, player_move_right_);
inputMapingStruct place_item_;
place_item_.type = key + mbutton + cbutton;
place_item_.keyCode = getKeyFromName(engine_.getConfig().get<std::string>("place_item_key","Space"));
place_item_.type = key1 + mbutton + cbutton;
place_item_.keyCode1 = getKeyFromName(engine_.getConfig().get<std::string>("place_item_key","Space"));
place_item_.mbuttonCode = getMButtonFromName(engine_.getConfig().get<std::string>("place_item_mbutton","Middle"));
place_item_.cbuttonCode = getCButtonFromName(engine_.getConfig().get<std::string>("place_item_cbutton","A"));
isMapingOk(place_item_, "place_item");
mapings_.emplace(inputMaping::place_item, place_item_);
inputMapingStruct beam_normal_;
beam_normal_.type = key + mbutton + caxis;
beam_normal_.type = key1 + mbutton + caxis;
beam_normal_.mbuttonCode = getMButtonFromName(engine_.getConfig().get<std::string>("beam_normal_mouse","Left"));
beam_normal_.keyCode = getKeyFromName(engine_.getConfig().get<std::string>("beam_normal_key","Q"));
beam_normal_.keyCode1 = getKeyFromName(engine_.getConfig().get<std::string>("beam_normal_key","Q"));
beam_normal_.caxisCode = getCAxisFromName(engine_.getConfig().get<std::string>("beam_normal_caxis","LEFTTRIGGER"));
isMapingOk(beam_normal_, "beam_normal");
mapings_.emplace(inputMaping::beam_normal, beam_normal_);
inputMapingStruct beam_inverse_;
beam_inverse_.type = key + mbutton + caxis;
beam_inverse_.type = key1 + mbutton + caxis;
beam_inverse_.mbuttonCode = getMButtonFromName(engine_.getConfig().get<std::string>("beam_inverse_mouse","Right"));
beam_inverse_.keyCode = getKeyFromName(engine_.getConfig().get<std::string>("beam_inverse_key","E"));
beam_inverse_.keyCode1 = getKeyFromName(engine_.getConfig().get<std::string>("beam_inverse_key","E"));
beam_inverse_.caxisCode = getCAxisFromName(engine_.getConfig().get<std::string>("beam_inverse_caxis","RIGHTTRIGGER"));
isMapingOk(beam_inverse_, "beam_inverse");
mapings_.emplace(inputMaping::beam_inverse, beam_inverse_);
......@@ -138,10 +140,7 @@ namespace gdw{
}
void input::mouseDown(const SDL_MouseButtonEvent &e){
if((mouseMap_.find(e.button)) == mouseMap_.end()){
mouseMap_.emplace(e.button, 0);
}
++mouseMap_[e.button];
mouseMap_.emplace(e.button, 1);
}
void input::mouseUp(const SDL_MouseButtonEvent &e){
......@@ -199,6 +198,7 @@ namespace gdw{
keyReleasedMap_.clear();
mouseReleasedMap_.clear();
mouseMap_.clear();
for(int i=0;i<controllers_.size();++i){
controllers_[i].controllerReleasedMap.clear();
controllers_[i].controllerMap.clear();
......@@ -218,8 +218,13 @@ namespace gdw{
}
//MAPING
int input::getKeyCodeByMaping(inputMaping id){
return (mapings_[id].type & key) !=0? mapings_[id].keyCode : 0;
int input::getKeyCodeByMaping(inputMaping id, int nr){
if(nr == 1){
return (mapings_[id].type & key1) !=0? mapings_[id].keyCode1 : 0;
}else if(nr == 2){
return (mapings_[id].type & key2) !=0? mapings_[id].keyCode2 : 0;
}
return 0;
}
int input::getMButtonCodeByMaping(inputMaping id){
......@@ -273,8 +278,13 @@ namespace gdw{
//RETURN
bool input::isPressed(inputMaping id){
if((mapings_[id].type & key) != 0){
if(isKeyPressed(mapings_[id].keyCode)){
if((mapings_[id].type & key1) != 0){
if(isKeyPressed(mapings_[id].keyCode1)){
return true;
}
}
if((mapings_[id].type & key2) != 0){
if(isKeyPressed(mapings_[id].keyCode2)){
return true;
}
}
......@@ -297,8 +307,13 @@ namespace gdw{
}
bool input::isDown(inputMaping id){
if((mapings_[id].type & key) != 0){
if(isKeyDown(mapings_[id].keyCode)){
if((mapings_[id].type & key1) != 0){
if(isKeyDown(mapings_[id].keyCode1)){
return true;
}
}
if((mapings_[id].type & key2) != 0){
if(isKeyDown(mapings_[id].keyCode2)){
return true;
}
}
......@@ -327,8 +342,13 @@ namespace gdw{
}
bool input::isReleased(inputMaping id){
if((mapings_[id].type & key) != 0){
if(isKeyReleased(mapings_[id].keyCode)){
if((mapings_[id].type & key1) != 0){
if(isKeyReleased(mapings_[id].keyCode1)){
return true;
}
}
if((mapings_[id].type & key2) != 0){
if(isKeyReleased(mapings_[id].keyCode2)){
return true;
}
}
......@@ -404,13 +424,21 @@ namespace gdw{
bool input::isMapingOk(inputMapingStruct m, std::string n){
bool ok = true;
if((m.type & key) != 0 && m.keyCode == 0){
if((m.type & key1) != 0 && m.keyCode1 == 0){
ok = false;
log(log_level::error) << "[input] mapping '" << n << "' is wrong, 'key1' is set but has default or error value" << std::endl;
}
if((m.type & key1) == 0 && m.keyCode1 != 0){
ok = false;
log(log_level::error) << "[input] mapping '" << n << "' is wrong, 'key1' is not set but has not default value" << std::endl;
}
if((m.type & key2) != 0 && m.keyCode2 == 0){
ok = false;
log(log_level::error) << "[input] mapping '" << n << "' is wrong, 'key' is set but has default or error value" << std::endl;
log(log_level::error) << "[input] mapping '" << n << "' is wrong, 'key2' is set but has default or error value" << std::endl;
}
if((m.type & key) == 0 && m.keyCode != 0){
if((m.type & key2) == 0 && m.keyCode2 != 0){
ok = false;
log(log_level::error) << "[input] mapping '" << n << "' is wrong, 'key' is not set but has not default value" << std::endl;
log(log_level::error) << "[input] mapping '" << n << "' is wrong, 'key2' is not set but has not default value" << std::endl;
}
if((m.type & mbutton) != 0 && m.mbuttonCode == -1){
ok = false;
......
Supports Markdown
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