Commit fe300c6c authored by Benjamin 'Albsi' Albsmeier's avatar Benjamin 'Albsi' Albsmeier
Browse files

more small changes in input

parent d0f69a0d
......@@ -9,13 +9,18 @@
#include <unordered_map>
#include <vector>
#include <SDL.h>
#include <glm/glm.hpp>
#include <asset/asset.hpp>
#include <core/engine.hpp>
#include <util/logger.hpp>
namespace gdw{
struct pos{
float x = 0.0f;
float y = 0.0f;
};
class engine;
class input_util;
class controller_manager;
class mouse;
class keyboard;
......@@ -24,6 +29,7 @@ namespace gdw{
class input_events;
class input{
private:
std::shared_ptr<gdw::input_util> util_;
std::shared_ptr<gdw::keyboard> keyboard_;
std::shared_ptr<gdw::mouse> mouse_;
std::shared_ptr<gdw::input_mapping> mapping_;
......@@ -31,19 +37,12 @@ namespace gdw{
std::shared_ptr<gdw::controller_manager> controller_manager_;
engine &engine_;
glm::vec2 pos_;
glm::vec2 last_pos_;
glm::vec2 relative_pos_;
pos pos_;
pos last_pos_;
pos relative_pos_;
int window_width_ = 800;
int window_height_ = 600;
std::shared_ptr<gdw::keyboard> keyboard() noexcept {return keyboard_;};
std::shared_ptr<gdw::controller> controller(int id = -1);
std::shared_ptr<gdw::controller_manager> controller_manager() noexcept {return controller_manager_;};
std::shared_ptr<gdw::mouse> mouse() noexcept {return mouse_;};
std::shared_ptr<gdw::input_events> events() noexcept {return events_;};
std::shared_ptr<gdw::input_mapping> mapping() noexcept {return mapping_;};
public:
input(engine& engine);
~input();
......@@ -86,7 +85,7 @@ namespace gdw{
*/
void add_mapping(std::string name);
/**prints the mapping*/
/**prints the mappings*/
void print_mapping();
/**Returns true every frame, as long as it is down
......
......@@ -26,7 +26,7 @@ namespace gdw{
class engine;
class input_mapping{
private:
engine &engine_;
std::shared_ptr<gdw::input_util> util_;
input *input_;
static unsigned int constexpr key_1_ = 0x01;
static unsigned int constexpr key_2_ = 0x02;
......@@ -35,7 +35,7 @@ namespace gdw{
static unsigned int constexpr controller_axis_ = 0x10;
std::unordered_map<std::string, gdw::mapping> mappings_;
public:
input_mapping(input *input, engine& engine);
input_mapping(input *input, std::shared_ptr<gdw::input_util> util);
~input_mapping();
SDL_Keycode key_1(std::string id);
......
......@@ -5,32 +5,36 @@
#ifndef __INPUT_UTIL_HPP__
#define __INPUT_UTIL_HPP__
#include <input/input.hpp>
#include <SDL.h>
#include <input/input.hpp>
namespace gdw{
//class input;
class engine;
class input_util{
//friend class input;
private:
engine &engine_;
input *input_;
public:
input_util(input *input);
input_util(input *input, engine& engine);
~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);
std::string config_string(std::string name, std::string default_value = "none");
int config_int(std::string name, int default_value = 0);
static std::string to_lower_case(std::string in_string);
static std::string to_upper_case(std::string in_string);
static std::string key_name(SDL_Keycode key);
static SDL_Keycode 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 mouse_button_name(int button);
static int 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 controller_button_name(SDL_GameControllerButton button);
static SDL_GameControllerButton 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 std::string controller_axis_name(SDL_GameControllerAxis axis);
static SDL_GameControllerAxis controller_axis_from_name(std::string name);
static bool controller_negativ_axis(std::string name);
};
}
......
......@@ -7,7 +7,6 @@
#include <unordered_map>
#include <SDL.h>
#include <glm/glm.hpp>
#include <input/input.hpp>
#include <input/input_util.hpp>
......@@ -19,9 +18,9 @@ namespace gdw{
std::unordered_map<int, int> pressed_map_;
std::unordered_map<int, int> released_map_;
glm::vec2 pos_;
glm::vec2 last_pos_;
glm::vec2 wheel_;
pos pos_;
pos last_pos_;
pos wheel_;
public:
mouse(input *input);
~mouse();
......
......@@ -87,14 +87,16 @@ namespace gdw {
input.print_mapping();
}
/*if (input.down("test")){
log << "pos: x:" << input.position_x() << " y:" << input.position_y() << std::endl;
if (input.down("test")){
//log << "pos: x:" << input.position_x() << " y:" << input.position_y() << std::endl;
//log << "input test down" << std::endl;
}*/
//log << "m x:" << input.position_x() << " y:" << input.position_y() << std::endl
// << "r x:" << input.relative_position_x() << " y:" << input.relative_position_y() << std::endl;
}
/*if (input.released("test")){
log << "input test released" << std::endl;
}*/
if (input.released("test")){
//log << "input test released" << std::endl;
}
if (input.down("move_left") && player->position().x > -maximum_movement_distance) {
auto orientation = player->rotation()*glm::vec3(0,0,1);
......
......@@ -14,18 +14,19 @@
namespace gdw{
input::input(engine& engine):engine_(engine),
pos_(0,0),
last_pos_(0,0),
relative_pos_(0,0){
pos_(),
last_pos_(),
relative_pos_(){
mapping_ = std::make_shared<gdw::input_mapping>(this, engine_);
util_ = std::make_shared<gdw::input_util>(this, engine_);
mapping_ = std::make_shared<gdw::input_mapping>(this, util_);
keyboard_ = std::make_shared<gdw::keyboard>(this);
mouse_ = std::make_shared<gdw::mouse>(this);
controller_manager_ = std::make_shared<gdw::controller_manager>(this, engine_);
events_ = std::make_shared<gdw::input_events>(this, keyboard_, mouse_, controller_manager_);//must be last
window_width_ = engine_.getConfig().get<int>("width",800);
window_height_ = engine_.getConfig().get<int>("height",600);
window_width_ = util_->config_int("width",800);
window_height_ = util_->config_int("height",600);
}
input::~input(){}
......@@ -37,8 +38,6 @@ namespace gdw{
void input::update(float delta){
pos_.x = mouse_->position_x();
pos_.y = mouse_->position_y();
last_pos_.x = mouse_->last_position_x();
last_pos_.y = mouse_->last_position_y();
relative_pos_.x = last_pos_.x - pos_.x;
relative_pos_.y = last_pos_.y - pos_.y;
controller_manager_->update(delta);
......@@ -48,6 +47,8 @@ namespace gdw{
mouse_->reset();//do not do mouse_.reset(); it will SegFault >> shared_ptr.reset() !
keyboard_->reset();
controller_manager_->reset();
last_pos_.x = mouse_->last_position_x();
last_pos_.y = mouse_->last_position_y();
}
//MAPPING
......@@ -144,7 +145,7 @@ namespace gdw{
return mouse_->wheel_y();
}
std::shared_ptr<gdw::controller> input::controller(int id){
/*std::shared_ptr<gdw::controller> input::controller(int id){
return controller_manager_->controller(id);
}
}*/
}
......@@ -5,52 +5,51 @@
#include <input/input_mapping.hpp>
namespace gdw{
input_mapping::input_mapping(input *input, engine& engine):input_(input), engine_(engine),mappings_(){}
input_mapping::input_mapping(input *input, std::shared_ptr<gdw::input_util> util):input_(input), util_(util),mappings_(){}
input_mapping::~input_mapping(){}
//ADD
bool input_mapping::add(std::string name){
bool ok = false;
mapping ims;
std::string s = engine_.getConfig().get<std::string>(name + "_key1","none");
std::string s = util_->config_string(name + "_key1");
if(s != "none"){
ims.key_1 = input_util::get_key_from_name(s);
ims.key_1 = util_->key_from_name(s);
if(ims.key_1 != SDLK_UNKNOWN){
ims.type += key_1_;
ok = true;
}
}
s = engine_.getConfig().get<std::string>(name + "_key2","none");
s = util_->config_string(name + "_key2");
if(s != "none"){
ims.key_2 = input_util::get_key_from_name(s);
ims.key_2 = util_->key_from_name(s);
if(ims.key_2 != SDLK_UNKNOWN){
ims.type += key_2_;
ok = true;
}
}
s = engine_.getConfig().get<std::string>(name + "_mouse_button","none");
s = util_->config_string(name + "_mouse_button");
if(s != "none"){
ims.mouse_button = input_util::get_mouse_button_from_name(s);
ims.mouse_button = util_->mouse_button_from_name(s);
if(ims.mouse_button != -1){
ims.type += mouse_button_;
ok = true;
}
}
s = engine_.getConfig().get<std::string>(name + "_controller_button","none");
s = util_->config_string(name + "_controller_button");
if(s != "none"){
ims.controller_button = input_util::get_controller_button_from_name(s);
ims.controller_button = util_->controller_button_from_name(s);
if(ims.controller_button != SDL_CONTROLLER_BUTTON_INVALID){
ims.type += controller_button_;
ok = true;
}
}
s = engine_.getConfig().get<std::string>(name + "_controller_axis","none");
s = util_->config_string(name + "_controller_axis");
if(s != "none"){
ims.controller_axis = input_util::get_controller_axis_from_name(s);
ims.controller_axis = util_->controller_axis_from_name(s);
if(ims.controller_axis != SDL_CONTROLLER_AXIS_INVALID){
ims.type += controller_axis_;
ims.negativ = input_util::controller_negativ_axis(s);
ims.negativ = util_->controller_negativ_axis(s);
if(ims.controller_axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT || ims.controller_axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT){
ims.negativ = false;
}
......@@ -61,7 +60,6 @@ namespace gdw{
return ok;
}
//RETURN
SDL_Keycode input_mapping::key_1(std::string id){
auto it = mappings_.find(id);
if(it == mappings_.end())return SDLK_UNKNOWN;
......@@ -96,16 +94,15 @@ namespace gdw{
return mappings_.find(id) != mappings_.end();
}
//PRINT
void input_mapping::print(){
log << "input: print mappings:" << std::endl;
for(auto it:mappings_){
log << "input: " << it.first << std::endl;
if(it.second.key_1 != 0)log << "input: key1: " << input_util::get_key_name(it.second.key_1) << std::endl;
if(it.second.key_2 != 0)log << "input: key2: " << input_util::get_key_name(it.second.key_2) << std::endl;
if(it.second.mouse_button != -1)log << "input: mouse button: " << input_util::get_mouse_button_name(it.second.mouse_button) << std::endl;
if(it.second.controller_button != -1)log << "input: controller button: " << input_util::get_controller_button_name(it.second.controller_button) << std::endl;
if(it.second.controller_axis != -1)log << "input: controller axis: " << input_util::get_controller_axis_name(it.second.controller_axis) << std::endl;
if(it.second.key_1 != 0)log << "input: key1: " << util_->key_name(it.second.key_1) << std::endl;
if(it.second.key_2 != 0)log << "input: key2: " << util_->key_name(it.second.key_2) << std::endl;
if(it.second.mouse_button != -1)log << "input: mouse button: " << util_->mouse_button_name(it.second.mouse_button) << std::endl;
if(it.second.controller_button != -1)log << "input: controller button: " << util_->controller_button_name(it.second.controller_button) << std::endl;
if(it.second.controller_axis != -1)log << "input: controller axis: " << util_->controller_axis_name(it.second.controller_axis) << std::endl;
if(it.second.negativ)log << "input: negativ axis" << std::endl;
}
}
......
......@@ -3,47 +3,56 @@
// input/input.cpp for GDW SS15
//****************************************
#include <input/input_util.hpp>
#include <core/engine.hpp>
namespace gdw{
input_util::input_util(input *input):input_(input){}
input_util::input_util(input *input, engine& engine):input_(input), engine_(engine){}
input_util::~input_util(){}
std::string input_util::to_lower_case(std::string inString){
std::string outString;
for(auto s:inString){
std::string input_util::config_string(std::string name, std::string default_value){
return engine_.getConfig().get<std::string>(name, default_value);
}
int input_util::config_int(std::string name, int default_value){
return engine_.getConfig().get<int>(name, default_value);
}
std::string input_util::to_lower_case(std::string in_string){
std::string out_string;
for(auto s:in_string){
if(s >= 'A' && s <= 'Z'){
outString += s - 'A' + 'a';
out_string += s - 'A' + 'a';
}else{
outString += s;
out_string += s;
}
}
return outString;
return out_string;
}
std::string input_util::to_upper_case(std::string inString){
std::string outString;
for(auto s:inString){
std::string input_util::to_upper_case(std::string in_string){
std::string out_string;
for(auto s:in_string){
if(s >= 'a' && s <= 'z'){
outString += s - 'a' + 'A';
out_string += s - 'a' + 'A';
}else{
outString += s;
out_string += s;
}
}
return outString;
return out_string;
}
//NAME <> SDL_KEYCODE
std::string input_util::get_key_name(SDL_Keycode key){
std::string input_util::key_name(SDL_Keycode key){
return SDL_GetKeyName(key);
}
SDL_Keycode input_util::get_key_from_name(std::string name){
SDL_Keycode input_util::key_from_name(std::string name){
return SDL_GetKeyFromName(name.c_str());
}
//NAME <> SDL_MOUSE_BUTTON
int input_util::get_mouse_button_from_name(std::string name){
int input_util::mouse_button_from_name(std::string name){
name = to_lower_case(name);
if(name == "left"){
return SDL_BUTTON_LEFT;
......@@ -60,7 +69,7 @@ namespace gdw{
}
}
std::string input_util::get_mouse_button_name(int button){
std::string input_util::mouse_button_name(int button){
switch(button){
case SDL_BUTTON_LEFT: return "left";
case SDL_BUTTON_RIGHT: return "right";
......@@ -72,19 +81,19 @@ namespace gdw{
}
//NAME <> SDL_CONTROLLER_BUTTON/AXIS
std::string input_util::get_controller_button_name(SDL_GameControllerButton button){
std::string input_util::controller_button_name(SDL_GameControllerButton button){
return SDL_GameControllerGetStringForButton(button);
}
std::string input_util::get_controller_axis_name(SDL_GameControllerAxis axis){
std::string input_util::controller_axis_name(SDL_GameControllerAxis axis){
return SDL_GameControllerGetStringForAxis(axis);
}
SDL_GameControllerButton input_util::get_controller_button_from_name(std::string name){
SDL_GameControllerButton input_util::controller_button_from_name(std::string name){
return SDL_GameControllerGetButtonFromString(name.c_str());
}
SDL_GameControllerAxis input_util::get_controller_axis_from_name(std::string name){
SDL_GameControllerAxis input_util::controller_axis_from_name(std::string name){
size_t sep = name.find("_");
if(sep != std::string::npos){
std::string first = name.substr(0, sep);
......
......@@ -6,9 +6,9 @@
namespace gdw{
mouse::mouse(input *input): input_(input),
last_pos_(0,0),
pos_(0,0),
wheel_(0,0),
last_pos_(),
pos_(),
wheel_(),
pressed_map_(),
released_map_(){}
......
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