Reformat input.cpp
This commit is contained in:
parent
d76f9b12a5
commit
d167471369
1 changed files with 70 additions and 71 deletions
|
@ -14,58 +14,58 @@ bool is_in_range(int value, int cond, int range) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void input_system::update() {
|
void input_system::update() {
|
||||||
const auto& [x, y] = platform::get_cursor_position();
|
const auto& [x, y] = platform::get_cursor_position();
|
||||||
auto& [oldX, oldY] = _last_cursor_position;
|
auto& [oldX, oldY] = _last_cursor_position;
|
||||||
|
|
||||||
const auto [width, height] = platform::get_window_size(::engine->get_main_window());
|
|
||||||
|
|
||||||
float xDelta = (x - oldX) / (float)width;
|
const auto [width, height] =
|
||||||
float yDelta = (y - oldY) / (float)height;
|
platform::get_window_size(::engine->get_main_window());
|
||||||
|
|
||||||
if(is_in_range(x, width / 2, 3) && is_in_range(y, height / 2, 3)) {
|
float xDelta = (x - oldX) / (float)width;
|
||||||
xDelta = 0.0f;
|
float yDelta = (y - oldY) / (float)height;
|
||||||
yDelta = 0.0f;
|
|
||||||
|
if (is_in_range(x, width / 2, 3) && is_in_range(y, height / 2, 3)) {
|
||||||
|
xDelta = 0.0f;
|
||||||
|
yDelta = 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
_last_cursor_position = { x, y };
|
_last_cursor_position = {x, y};
|
||||||
|
|
||||||
for (auto& binding : _input_bindings) {
|
for (auto& binding : _input_bindings) {
|
||||||
binding.value = 0.0f;
|
binding.value = 0.0f;
|
||||||
|
|
||||||
for (auto& [key, value] : binding.buttons) {
|
for (auto& [key, value] : binding.buttons) {
|
||||||
bool is_pressed = false;
|
bool is_pressed = false;
|
||||||
switch(key) {
|
switch (key) {
|
||||||
case InputButton::MouseLeft:
|
case InputButton::MouseLeft:
|
||||||
is_pressed = platform::get_mouse_button_down(0);
|
is_pressed = platform::get_mouse_button_down(0);
|
||||||
break;
|
break;
|
||||||
case InputButton::MouseRight:
|
case InputButton::MouseRight:
|
||||||
is_pressed = platform::get_mouse_button_down(1);
|
is_pressed = platform::get_mouse_button_down(1);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
is_pressed = platform::get_key_down(key);
|
is_pressed = platform::get_key_down(key);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_pressed) {
|
if (is_pressed) {
|
||||||
binding.value = value;
|
binding.value = value;
|
||||||
|
|
||||||
if (binding.last_button == key) {
|
if (binding.last_button == key) {
|
||||||
binding.repeat = true;
|
binding.repeat = true;
|
||||||
}
|
} else {
|
||||||
else {
|
binding.repeat = false;
|
||||||
binding.repeat = false;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
binding.last_button = key;
|
binding.last_button = key;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (binding.value == 0.0f) {
|
if (binding.value == 0.0f) {
|
||||||
binding.last_button = InputButton::Invalid;
|
binding.last_button = InputButton::Invalid;
|
||||||
binding.repeat = false;
|
binding.repeat = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(auto& axis : binding.axises) {
|
for (auto& axis : binding.axises) {
|
||||||
auto [lx, ly] = platform::get_left_stick_position();
|
auto [lx, ly] = platform::get_left_stick_position();
|
||||||
auto [rx, ry] = platform::get_right_stick_position();
|
auto [rx, ry] = platform::get_right_stick_position();
|
||||||
auto [sx, sy] = platform::get_wheel_delta();
|
auto [sx, sy] = platform::get_wheel_delta();
|
||||||
|
@ -99,56 +99,57 @@ void input_system::update() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(nextValue != 0.0f)
|
if (nextValue != 0.0f)
|
||||||
binding.value = nextValue;
|
binding.value = nextValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void input_system::add_binding(const std::string& name) {
|
void input_system::add_binding(const std::string& name) {
|
||||||
input_binding data;
|
input_binding data;
|
||||||
data.name = name;
|
data.name = name;
|
||||||
|
|
||||||
_input_bindings.push_back(data);
|
_input_bindings.push_back(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
void input_system::add_binding_button(const std::string& name, InputButton key, float value) {
|
void input_system::add_binding_button(const std::string& name, InputButton key,
|
||||||
for (auto& binding : _input_bindings) {
|
float value) {
|
||||||
if (binding.name == name)
|
for (auto& binding : _input_bindings) {
|
||||||
binding.buttons[key] = value;
|
if (binding.name == name)
|
||||||
}
|
binding.buttons[key] = value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void input_system::add_binding_axis(const std::string& name, axis axis) {
|
void input_system::add_binding_axis(const std::string& name, axis axis) {
|
||||||
for (auto& binding : _input_bindings) {
|
for (auto& binding : _input_bindings) {
|
||||||
if (binding.name == name) {
|
if (binding.name == name) {
|
||||||
binding.axises.push_back(axis);
|
binding.axises.push_back(axis);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
float input_system::get_value(const std::string& name) {
|
float input_system::get_value(const std::string& name) {
|
||||||
for (auto& binding : _input_bindings) {
|
for (auto& binding : _input_bindings) {
|
||||||
if (binding.name == name) {
|
if (binding.name == name) {
|
||||||
return binding.value;
|
return binding.value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0.0f;
|
return 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool input_system::is_pressed(const std::string &name, bool repeating) {
|
bool input_system::is_pressed(const std::string& name, bool repeating) {
|
||||||
return get_value(name) == 1.0 && (repeating || !is_repeating(name));
|
return get_value(name) == 1.0 && (repeating || !is_repeating(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool input_system::is_repeating(const std::string& name) {
|
bool input_system::is_repeating(const std::string& name) {
|
||||||
for (auto& binding : _input_bindings) {
|
for (auto& binding : _input_bindings) {
|
||||||
if (binding.name == name) {
|
if (binding.name == name) {
|
||||||
return binding.repeat;
|
return binding.repeat;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<prism::input_binding> input_system::get_bindings() const {
|
std::vector<prism::input_binding> input_system::get_bindings() const {
|
||||||
|
@ -165,15 +166,13 @@ void input_system::end_text_input() {
|
||||||
platform::end_text_input();
|
platform::end_text_input();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool input_system::is_text_input() const {
|
bool input_system::is_text_input() const { return _in_text_input; }
|
||||||
return _in_text_input;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool input_system::get_allowable_text_button(unsigned int keycode) const {
|
bool input_system::get_allowable_text_button(unsigned int keycode) const {
|
||||||
if(platform::get_keycode(InputButton::Backspace) == keycode)
|
if (platform::get_keycode(InputButton::Backspace) == keycode)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
if(platform::get_keycode(InputButton::Enter) == keycode)
|
if (platform::get_keycode(InputButton::Enter) == keycode)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|
Reference in a new issue