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() {
|
||||
const auto& [x, y] = platform::get_cursor_position();
|
||||
auto& [oldX, oldY] = _last_cursor_position;
|
||||
|
||||
const auto [width, height] = platform::get_window_size(::engine->get_main_window());
|
||||
const auto& [x, y] = platform::get_cursor_position();
|
||||
auto& [oldX, oldY] = _last_cursor_position;
|
||||
|
||||
float xDelta = (x - oldX) / (float)width;
|
||||
float yDelta = (y - oldY) / (float)height;
|
||||
const auto [width, height] =
|
||||
platform::get_window_size(::engine->get_main_window());
|
||||
|
||||
if(is_in_range(x, width / 2, 3) && is_in_range(y, height / 2, 3)) {
|
||||
xDelta = 0.0f;
|
||||
yDelta = 0.0f;
|
||||
float xDelta = (x - oldX) / (float)width;
|
||||
float yDelta = (y - oldY) / (float)height;
|
||||
|
||||
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) {
|
||||
binding.value = 0.0f;
|
||||
for (auto& binding : _input_bindings) {
|
||||
binding.value = 0.0f;
|
||||
|
||||
for (auto& [key, value] : binding.buttons) {
|
||||
for (auto& [key, value] : binding.buttons) {
|
||||
bool is_pressed = false;
|
||||
switch(key) {
|
||||
case InputButton::MouseLeft:
|
||||
is_pressed = platform::get_mouse_button_down(0);
|
||||
break;
|
||||
case InputButton::MouseRight:
|
||||
is_pressed = platform::get_mouse_button_down(1);
|
||||
break;
|
||||
default:
|
||||
is_pressed = platform::get_key_down(key);
|
||||
break;
|
||||
switch (key) {
|
||||
case InputButton::MouseLeft:
|
||||
is_pressed = platform::get_mouse_button_down(0);
|
||||
break;
|
||||
case InputButton::MouseRight:
|
||||
is_pressed = platform::get_mouse_button_down(1);
|
||||
break;
|
||||
default:
|
||||
is_pressed = platform::get_key_down(key);
|
||||
break;
|
||||
}
|
||||
|
||||
if (is_pressed) {
|
||||
binding.value = value;
|
||||
if (is_pressed) {
|
||||
binding.value = value;
|
||||
|
||||
if (binding.last_button == key) {
|
||||
binding.repeat = true;
|
||||
}
|
||||
else {
|
||||
binding.repeat = false;
|
||||
}
|
||||
if (binding.last_button == key) {
|
||||
binding.repeat = true;
|
||||
} else {
|
||||
binding.repeat = false;
|
||||
}
|
||||
|
||||
binding.last_button = key;
|
||||
}
|
||||
}
|
||||
binding.last_button = key;
|
||||
}
|
||||
}
|
||||
|
||||
if (binding.value == 0.0f) {
|
||||
binding.last_button = InputButton::Invalid;
|
||||
binding.repeat = false;
|
||||
}
|
||||
if (binding.value == 0.0f) {
|
||||
binding.last_button = InputButton::Invalid;
|
||||
binding.repeat = false;
|
||||
}
|
||||
|
||||
for(auto& axis : binding.axises) {
|
||||
for (auto& axis : binding.axises) {
|
||||
auto [lx, ly] = platform::get_left_stick_position();
|
||||
auto [rx, ry] = platform::get_right_stick_position();
|
||||
auto [sx, sy] = platform::get_wheel_delta();
|
||||
|
@ -99,56 +99,57 @@ void input_system::update() {
|
|||
break;
|
||||
}
|
||||
|
||||
if(nextValue != 0.0f)
|
||||
if (nextValue != 0.0f)
|
||||
binding.value = nextValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void input_system::add_binding(const std::string& name) {
|
||||
input_binding data;
|
||||
data.name = name;
|
||||
input_binding data;
|
||||
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) {
|
||||
for (auto& binding : _input_bindings) {
|
||||
if (binding.name == name)
|
||||
binding.buttons[key] = value;
|
||||
}
|
||||
void input_system::add_binding_button(const std::string& name, InputButton key,
|
||||
float value) {
|
||||
for (auto& binding : _input_bindings) {
|
||||
if (binding.name == name)
|
||||
binding.buttons[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
void input_system::add_binding_axis(const std::string& name, axis axis) {
|
||||
for (auto& binding : _input_bindings) {
|
||||
if (binding.name == name) {
|
||||
for (auto& binding : _input_bindings) {
|
||||
if (binding.name == name) {
|
||||
binding.axises.push_back(axis);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float input_system::get_value(const std::string& name) {
|
||||
for (auto& binding : _input_bindings) {
|
||||
if (binding.name == name) {
|
||||
return binding.value;
|
||||
}
|
||||
}
|
||||
for (auto& binding : _input_bindings) {
|
||||
if (binding.name == name) {
|
||||
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));
|
||||
}
|
||||
|
||||
bool input_system::is_repeating(const std::string& name) {
|
||||
for (auto& binding : _input_bindings) {
|
||||
if (binding.name == name) {
|
||||
return binding.repeat;
|
||||
}
|
||||
}
|
||||
for (auto& binding : _input_bindings) {
|
||||
if (binding.name == name) {
|
||||
return binding.repeat;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<prism::input_binding> input_system::get_bindings() const {
|
||||
|
@ -165,15 +166,13 @@ void input_system::end_text_input() {
|
|||
platform::end_text_input();
|
||||
}
|
||||
|
||||
bool input_system::is_text_input() const {
|
||||
return _in_text_input;
|
||||
}
|
||||
bool input_system::is_text_input() const { return _in_text_input; }
|
||||
|
||||
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;
|
||||
|
||||
if(platform::get_keycode(InputButton::Enter) == keycode)
|
||||
if (platform::get_keycode(InputButton::Enter) == keycode)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
|
|
Reference in a new issue