Commit 5e996808 authored by Benjamin 'Albsi' Albsmeier's avatar Benjamin 'Albsi' Albsmeier
Browse files

its done - input may does what it should

parent 0ca2d0ac
......@@ -20,11 +20,11 @@ mapGenDebug : false
player_move_left_key : A
player_move_left_key2 : Left
player_move_left_cbutton : LEFT
player_move_left_caxis : RIGHTX
player_move_left_caxis : LEFTX
player_move_right_key : D
player_move_right_key2 : Right
player_move_right_cbutton : RIGHT
player_move_right_caxis : RIGHTX
player_move_right_caxis : LEFTX
beam_normal_key : Q
beam_normal_mbutton : Left
beam_normal_caxis : LEFTTRIGGER
......@@ -36,3 +36,4 @@ place_item_mbutton : Middle
place_item_cbutton : A
state_switch_key : Escape
state_switch_cbutton : START
controller_cursor : RIGHT
......@@ -52,12 +52,19 @@ namespace gdw{
SDL_GameControllerAxis caxisCode = SDL_CONTROLLER_AXIS_INVALID;
};
enum class controllerCursor {
none,
left,
right
};
class engine;
class input{
private:
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;
......@@ -118,7 +125,7 @@ namespace gdw{
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_;};
glm::vec2 mouseWheel()noexcept{return mouseWheel_;};
/**x motion of the mouse wheel*/
int mouseWheelX()noexcept{return mouseWheel_.x;};
......@@ -127,13 +134,13 @@ namespace gdw{
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_;};
glm::vec2 mousePos()noexcept{/*return mousePos_;*/return pos_;};
/**returns x position of the mouse cursor*/
int mousePosX()noexcept{return mousePos_.x;};
int mousePosX()noexcept{/*return mousePos_.x;*/return pos_.x;};
/**returns y position of the mouse cursor*/
int mousePosY()noexcept{return mousePos_.y;};
int mousePosY()noexcept{/*return mousePos_.y;*/return pos_.y;};
/**Returns true every frame, as long as the controller button is down.*/
private:
......@@ -189,7 +196,15 @@ namespace gdw{
engine &engine_;
glm::vec2 mousePos_;
glm::vec2 oldMousePos_;
glm::vec2 mouseWheel_;
glm::vec2 pos_;
int windowWidth_ = 800;
int windowHeight_ = 600;
controllerCursor getCCursorFromName(std::string name);
controllerCursor ccursor = controllerCursor::none;
const Uint8 *keyState_;
......
......@@ -112,7 +112,7 @@ namespace gdw {
auto &input = engine_.input();
auto &level_manager = engine_.game_play_system().level_manager();
log << "mouse: x:" << input.mousePosX() << " y:" << input.mousePosY() << std::endl;
//log << "mouse: x:" << input.mousePosX() << " y:" << input.mousePosY() << std::endl;
//MOUSEWHEEL
if(input.mouseWheelY() < 0.f) {
......
......@@ -6,12 +6,17 @@
#include <util/config.hpp>
namespace gdw{
input::input(engine& engine):engine_(engine),mousePos_(0,0),mouseWheel_(0,0),controllers_(),mappings_(),usedControllers_(){
input::input(engine& engine):engine_(engine),pos_(0,0),mousePos_(0,0),oldMousePos_(0,0),mouseWheel_(0,0),controllers_(),mappings_(),usedControllers_(){
keyState_ = SDL_GetKeyboardState(NULL);
controllerDB_ = engine.asset_manager().load(engine.asset_manager().native_name(controllerDBPath_));
makeMappings();
SDL_SetCursor(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_CROSSHAIR));
//SDL_SetCursor(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_CROSSHAIR));
SDL_ShowCursor(SDL_FALSE);
ccursor = getCCursorFromName(engine_.getConfig().get<std::string>("controller_cursor","RIGHT"));
windowWidth_ = engine_.getConfig().get<int>("width",800);
windowHeight_ = engine_.getConfig().get<int>("height",600);
}
/** if you add here, add also in enum inputMapping in input.hpp
......@@ -46,9 +51,9 @@ namespace gdw{
inputMappingStruct player_move_left_;
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_.keyCode2 = getKeyFromName(engine_.getConfig().get<std::string>("player_move_left_key2","Left"));
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_.caxisCode = getCAxisFromName(engine_.getConfig().get<std::string>("player_move_left_caxis","LEFTX"));
player_move_left_.negativAxis = true;
isMappingOk(player_move_left_, "player_move_left");
mappings_.emplace(inputMapping::player_move_left, player_move_left_);
......@@ -56,9 +61,9 @@ namespace gdw{
inputMappingStruct player_move_right_;
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_.keyCode2 = getKeyFromName(engine_.getConfig().get<std::string>("player_move_right_key2","Right"));
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"));
player_move_right_.caxisCode = getCAxisFromName(engine_.getConfig().get<std::string>("player_move_right_caxis","LEFTX"));
isMappingOk(player_move_right_, "player_move_right");
mappings_.emplace(inputMapping::player_move_right, player_move_right_);
......@@ -211,8 +216,22 @@ namespace gdw{
}
}
}
if(ccursor != controllerCursor::none){
pos_.x += mousePos_.x - oldMousePos_.x;
pos_.y += mousePos_.y - oldMousePos_.y;
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;
}
}
int input::firstController(){
......@@ -258,6 +277,8 @@ namespace gdw{
++keyIt_->second;
}
}
oldMousePos_.x = mousePos_.x;
oldMousePos_.y = mousePos_.y;
}
//MAPPING
......@@ -319,6 +340,15 @@ namespace gdw{
return SDL_GameControllerGetAxisFromString(name.c_str());
}
controllerCursor input::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;
}
//RETURN
bool input::isPressed(inputMapping id){
if((mappings_[id].type & key1) != 0){
......
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