diff options
Diffstat (limited to 'core/input/input.cpp')
-rw-r--r-- | core/input/input.cpp | 142 |
1 files changed, 43 insertions, 99 deletions
diff --git a/core/input/input.cpp b/core/input/input.cpp index 38a71994d8..dc1c207524 100644 --- a/core/input/input.cpp +++ b/core/input/input.cpp @@ -129,7 +129,6 @@ Input::CursorShape (*Input::get_current_cursor_shape_func)() = nullptr; void (*Input::set_custom_mouse_cursor_func)(const RES &, Input::CursorShape, const Vector2 &) = nullptr; Input *Input::get_singleton() { - return singleton; } @@ -139,12 +138,10 @@ void Input::set_mouse_mode(MouseMode p_mode) { } Input::MouseMode Input::get_mouse_mode() const { - return get_mouse_mode_func(); } void Input::_bind_methods() { - ClassDB::bind_method(D_METHOD("is_key_pressed", "keycode"), &Input::is_key_pressed); ClassDB::bind_method(D_METHOD("is_mouse_button_pressed", "button"), &Input::is_mouse_button_pressed); ClassDB::bind_method(D_METHOD("is_joy_button_pressed", "device", "button"), &Input::is_joy_button_pressed); @@ -219,15 +216,15 @@ void Input::get_argument_options(const StringName &p_function, int p_idx, List<S String pf = p_function; if (p_idx == 0 && (pf == "is_action_pressed" || pf == "action_press" || pf == "action_release" || pf == "is_action_just_pressed" || pf == "is_action_just_released" || pf == "get_action_strength")) { - List<PropertyInfo> pinfo; ProjectSettings::get_singleton()->get_property_list(&pinfo); for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) { const PropertyInfo &pi = E->get(); - if (!pi.name.begins_with("input/")) + if (!pi.name.begins_with("input/")) { continue; + } String name = pi.name.substr(pi.name.find("/") + 1, pi.name.length()); r_options->push_back(quote_style + name + quote_style); @@ -237,7 +234,6 @@ void Input::get_argument_options(const StringName &p_function, int p_idx, List<S } void Input::SpeedTrack::update(const Vector2 &p_delta_p) { - uint64_t tick = OS::get_singleton()->get_ticks_usec(); uint32_t tdiff = tick - last_tick; float delta_t = tdiff / 1000000.0; @@ -246,11 +242,11 @@ void Input::SpeedTrack::update(const Vector2 &p_delta_p) { accum += p_delta_p; accum_t += delta_t; - if (accum_t > max_ref_frame * 10) + if (accum_t > max_ref_frame * 10) { accum_t = max_ref_frame * 10; + } while (accum_t >= min_ref_frame) { - float slice_t = min_ref_frame / accum_t; Vector2 slice = accum * slice_t; accum = accum - slice; @@ -267,45 +263,39 @@ void Input::SpeedTrack::reset() { } Input::SpeedTrack::SpeedTrack() { - min_ref_frame = 0.1; max_ref_frame = 0.3; reset(); } bool Input::is_key_pressed(int p_keycode) const { - _THREAD_SAFE_METHOD_ return keys_pressed.has(p_keycode); } bool Input::is_mouse_button_pressed(int p_button) const { - _THREAD_SAFE_METHOD_ return (mouse_button_mask & (1 << (p_button - 1))) != 0; } static int _combine_device(int p_value, int p_device) { - return p_value | (p_device << 20); } bool Input::is_joy_button_pressed(int p_device, int p_button) const { - _THREAD_SAFE_METHOD_ return joy_buttons_pressed.has(_combine_device(p_button, p_device)); } bool Input::is_action_pressed(const StringName &p_action) const { - return action_state.has(p_action) && action_state[p_action].pressed; } bool Input::is_action_just_pressed(const StringName &p_action) const { - const Map<StringName, Action>::Element *E = action_state.find(p_action); - if (!E) + if (!E) { return false; + } if (Engine::get_singleton()->is_in_physics_frame()) { return E->get().pressed && E->get().physics_frame == Engine::get_singleton()->get_physics_frames(); @@ -315,10 +305,10 @@ bool Input::is_action_just_pressed(const StringName &p_action) const { } bool Input::is_action_just_released(const StringName &p_action) const { - const Map<StringName, Action>::Element *E = action_state.find(p_action); - if (!E) + if (!E) { return false; + } if (Engine::get_singleton()->is_in_physics_frame()) { return !E->get().pressed && E->get().physics_frame == Engine::get_singleton()->get_physics_frames(); @@ -329,14 +319,14 @@ bool Input::is_action_just_released(const StringName &p_action) const { float Input::get_action_strength(const StringName &p_action) const { const Map<StringName, Action>::Element *E = action_state.find(p_action); - if (!E) + if (!E) { return 0.0f; + } return E->get().strength; } float Input::get_joy_axis(int p_device, int p_axis) const { - _THREAD_SAFE_METHOD_ int c = _combine_device(p_axis, p_device); if (_joy_axis.has(c)) { @@ -347,7 +337,6 @@ float Input::get_joy_axis(int p_device, int p_axis) const { } String Input::get_joy_name(int p_idx) { - _THREAD_SAFE_METHOD_ return joy_names[p_idx].name; }; @@ -377,7 +366,6 @@ float Input::get_joy_vibration_duration(int p_device) { } static String _hex_str(uint8_t p_byte) { - static const char *dict = "0123456789abcdef"; char ret[3]; ret[2] = 0; @@ -389,14 +377,12 @@ static String _hex_str(uint8_t p_byte) { }; void Input::joy_connection_changed(int p_idx, bool p_connected, String p_name, String p_guid) { - _THREAD_SAFE_METHOD_ Joypad js; js.name = p_connected ? p_name : ""; js.uid = p_connected ? p_guid : ""; if (p_connected) { - String uidname = p_guid; if (p_guid == "") { int uidlen = MIN(p_name.length(), 16); @@ -430,36 +416,30 @@ void Input::joy_connection_changed(int p_idx, bool p_connected, String p_name, S }; Vector3 Input::get_gravity() const { - _THREAD_SAFE_METHOD_ return gravity; } Vector3 Input::get_accelerometer() const { - _THREAD_SAFE_METHOD_ return accelerometer; } Vector3 Input::get_magnetometer() const { - _THREAD_SAFE_METHOD_ return magnetometer; } Vector3 Input::get_gyroscope() const { - _THREAD_SAFE_METHOD_ return gyroscope; } void Input::parse_input_event(const Ref<InputEvent> &p_event) { - _parse_input_event_impl(p_event, false); } void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_emulated) { - // Notes on mouse-touch emulation: // - Emulated mouse events are parsed, that is, re-routed to this method, so they make the same effects // as true mouse events. The only difference is the situation is flagged as emulated so they are not @@ -471,16 +451,16 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em Ref<InputEventKey> k = p_event; if (k.is_valid() && !k->is_echo() && k->get_keycode() != 0) { - if (k->is_pressed()) + if (k->is_pressed()) { keys_pressed.insert(k->get_keycode()); - else + } else { keys_pressed.erase(k->get_keycode()); + } } Ref<InputEventMouseButton> mb = p_event; if (mb.is_valid()) { - if (mb->is_pressed()) { mouse_button_mask |= (1 << (mb->get_button_index() - 1)); } else { @@ -504,7 +484,6 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em Ref<InputEventMouseMotion> mm = p_event; if (mm.is_valid()) { - Point2 pos = mm->get_global_position(); if (mouse_pos != pos) { set_mouse_position(pos); @@ -525,7 +504,6 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em Ref<InputEventScreenTouch> st = p_event; if (st.is_valid()) { - if (st->is_pressed()) { SpeedTrack &track = touch_speed_track[st->get_index()]; track.reset(); @@ -536,7 +514,6 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em } if (emulate_mouse_from_touch) { - bool translate = false; if (st->is_pressed()) { if (mouse_from_touch_index == -1) { @@ -573,13 +550,11 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em Ref<InputEventScreenDrag> sd = p_event; if (sd.is_valid()) { - SpeedTrack &track = touch_speed_track[sd->get_index()]; track.update(sd->get_relative()); sd->set_speed(track.speed); if (emulate_mouse_from_touch && sd->get_index() == mouse_from_touch_index) { - Ref<InputEventMouseMotion> motion_event; motion_event.instance(); @@ -597,13 +572,13 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em Ref<InputEventJoypadButton> jb = p_event; if (jb.is_valid()) { - int c = _combine_device(jb->get_button_index(), jb->get_device()); - if (jb->is_pressed()) + if (jb->is_pressed()) { joy_buttons_pressed.insert(c); - else + } else { joy_buttons_pressed.erase(c); + } } Ref<InputEventJoypadMotion> jm = p_event; @@ -615,7 +590,6 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em Ref<InputEventGesture> ge = p_event; if (ge.is_valid()) { - if (event_dispatch_function) { event_dispatch_function(ge); } @@ -623,7 +597,6 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em for (const Map<StringName, InputMap::Action>::Element *E = InputMap::get_singleton()->get_action_map().front(); E; E = E->next()) { if (InputMap::get_singleton()->event_is_action(p_event, E->key())) { - // Save the action's state if (!p_event->is_echo() && is_action_pressed(E->key()) != p_event->is_action_pressed(E->key())) { Action action; @@ -637,12 +610,12 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em } } - if (event_dispatch_function) + if (event_dispatch_function) { event_dispatch_function(p_event); + } } void Input::set_joy_axis(int p_device, int p_axis, float p_value) { - _THREAD_SAFE_METHOD_ int c = _combine_device(p_axis, p_device); _joy_axis[c] = p_value; @@ -676,50 +649,43 @@ void Input::vibrate_handheld(int p_duration_ms) { } void Input::set_gravity(const Vector3 &p_gravity) { - _THREAD_SAFE_METHOD_ gravity = p_gravity; } void Input::set_accelerometer(const Vector3 &p_accel) { - _THREAD_SAFE_METHOD_ accelerometer = p_accel; } void Input::set_magnetometer(const Vector3 &p_magnetometer) { - _THREAD_SAFE_METHOD_ magnetometer = p_magnetometer; } void Input::set_gyroscope(const Vector3 &p_gyroscope) { - _THREAD_SAFE_METHOD_ gyroscope = p_gyroscope; } void Input::set_mouse_position(const Point2 &p_posf) { - mouse_speed_track.update(p_posf - mouse_pos); mouse_pos = p_posf; } Point2 Input::get_mouse_position() const { - return mouse_pos; } -Point2 Input::get_last_mouse_speed() const { +Point2 Input::get_last_mouse_speed() const { return mouse_speed_track.speed; } int Input::get_mouse_button_mask() const { - return mouse_button_mask; // do not trust OS implementation, should remove it - OS::get_singleton()->get_mouse_button_state(); } @@ -728,7 +694,6 @@ void Input::warp_mouse_position(const Vector2 &p_to) { } Point2i Input::warp_mouse_motion(const Ref<InputEventMouseMotion> &p_motion, const Rect2 &p_rect) { - // The relative distance reported for the next event after a warp is in the boundaries of the // size of the rect on that axis, but it may be greater, in which case there's not problem as fmod() // will warp it, but if the pointer has moved in the opposite direction between the pointer relocation @@ -757,7 +722,6 @@ void Input::iteration(float p_step) { } void Input::action_press(const StringName &p_action, float p_strength) { - Action action; action.physics_frame = Engine::get_singleton()->get_physics_frames(); @@ -769,7 +733,6 @@ void Input::action_press(const StringName &p_action, float p_strength) { } void Input::action_release(const StringName &p_action) { - Action action; action.physics_frame = Engine::get_singleton()->get_physics_frames(); @@ -781,19 +744,16 @@ void Input::action_release(const StringName &p_action) { } void Input::set_emulate_touch_from_mouse(bool p_emulate) { - emulate_touch_from_mouse = p_emulate; } bool Input::is_emulating_touch_from_mouse() const { - return emulate_touch_from_mouse; } // Calling this whenever the game window is focused helps unstucking the "touch mouse" // if the OS or its abstraction class hasn't properly reported that touch pointers raised void Input::ensure_touch_mouse_raised() { - if (mouse_from_touch_index != -1) { mouse_from_touch_index = -1; @@ -812,24 +772,21 @@ void Input::ensure_touch_mouse_raised() { } void Input::set_emulate_mouse_from_touch(bool p_emulate) { - emulate_mouse_from_touch = p_emulate; } bool Input::is_emulating_mouse_from_touch() const { - return emulate_mouse_from_touch; } Input::CursorShape Input::get_default_cursor_shape() const { - return default_shape; } void Input::set_default_cursor_shape(CursorShape p_shape) { - - if (default_shape == p_shape) + if (default_shape == p_shape) { return; + } default_shape = p_shape; // The default shape is set in Viewport::_gui_input_event. To instantly @@ -842,13 +799,13 @@ void Input::set_default_cursor_shape(CursorShape p_shape) { } Input::CursorShape Input::get_current_cursor_shape() const { - return get_current_cursor_shape_func(); } void Input::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) { - if (Engine::get_singleton()->is_editor_hint()) + if (Engine::get_singleton()->is_editor_hint()) { return; + } set_custom_mouse_cursor_func(p_cursor, p_shape, p_hotspot); } @@ -866,8 +823,8 @@ void Input::accumulate_input_event(const Ref<InputEvent> &p_event) { accumulated_events.push_back(p_event); } -void Input::flush_accumulated_events() { +void Input::flush_accumulated_events() { while (accumulated_events.front()) { parse_input_event(accumulated_events.front()->get()); accumulated_events.pop_front(); @@ -875,12 +832,10 @@ void Input::flush_accumulated_events() { } void Input::set_use_accumulated_input(bool p_enable) { - use_accumulated_input = p_enable; } void Input::release_pressed_events() { - flush_accumulated_events(); // this is needed to release actions strengths keys_pressed.clear(); @@ -888,8 +843,9 @@ void Input::release_pressed_events() { _joy_axis.clear(); for (Map<StringName, Input::Action>::Element *E = action_state.front(); E; E = E->next()) { - if (E->get().pressed) + if (E->get().pressed) { action_release(E->key()); + } } } @@ -898,7 +854,6 @@ void Input::set_event_dispatch_function(EventDispatchFunc p_function) { } void Input::joy_button(int p_device, int p_button, bool p_pressed) { - _THREAD_SAFE_METHOD_; Joypad &joy = joy_names[p_device]; //printf("got button %i, mapping is %i\n", p_button, joy.mapping); @@ -925,7 +880,6 @@ void Input::joy_button(int p_device, int p_button, bool p_pressed) { } void Input::joy_axis(int p_device, int p_axis, const JoyAxis &p_value) { - _THREAD_SAFE_METHOD_; ERR_FAIL_INDEX(p_axis, JOY_AXIS_MAX); @@ -937,13 +891,10 @@ void Input::joy_axis(int p_device, int p_axis, const JoyAxis &p_value) { } if (p_value.value > joy.last_axis[p_axis]) { - if (p_value.value < joy.last_axis[p_axis] + joy.filter) { - return; } } else if (p_value.value > joy.last_axis[p_axis] - joy.filter) { - return; } @@ -972,7 +923,6 @@ void Input::joy_axis(int p_device, int p_axis, const JoyAxis &p_value) { JoyEvent map = _get_mapped_axis_event(map_db[joy.mapping], p_axis, p_value); if (map.type == TYPE_BUTTON) { - if (map.index == JOY_BUTTON_DPAD_UP || map.index == JOY_BUTTON_DPAD_DOWN) { bool pressed = p_value.value != 0.0f; int button = p_value.value < 0 ? JOY_BUTTON_DPAD_UP : JOY_BUTTON_DPAD_DOWN; @@ -1023,7 +973,6 @@ void Input::joy_axis(int p_device, int p_axis, const JoyAxis &p_value) { } if (map.type == TYPE_AXIS) { - _axis_event(p_device, map.index, map.value); return; } @@ -1031,7 +980,6 @@ void Input::joy_axis(int p_device, int p_axis, const JoyAxis &p_value) { } void Input::joy_hat(int p_device, int p_val) { - _THREAD_SAFE_METHOD_; const Joypad &joy = joy_names[p_device]; @@ -1077,7 +1025,6 @@ void Input::joy_hat(int p_device, int p_val) { } void Input::_button_event(int p_device, int p_index, bool p_pressed) { - Ref<InputEventJoypadButton> ievent; ievent.instance(); ievent->set_device(p_device); @@ -1088,7 +1035,6 @@ void Input::_button_event(int p_device, int p_index, bool p_pressed) { } void Input::_axis_event(int p_device, int p_axis, float p_value) { - Ref<InputEventJoypadMotion> ievent; ievent.instance(); ievent->set_device(p_device); @@ -1099,7 +1045,6 @@ void Input::_axis_event(int p_device, int p_axis, float p_value) { }; Input::JoyEvent Input::_get_mapped_button_event(const JoyDeviceMapping &mapping, int p_button) { - JoyEvent event; event.type = TYPE_MAX; @@ -1123,7 +1068,6 @@ Input::JoyEvent Input::_get_mapped_button_event(const JoyDeviceMapping &mapping, } Input::JoyEvent Input::_get_mapped_axis_event(const JoyDeviceMapping &mapping, int p_axis, const JoyAxis &p_value) { - JoyEvent event; event.type = TYPE_MAX; @@ -1131,8 +1075,9 @@ Input::JoyEvent Input::_get_mapped_axis_event(const JoyDeviceMapping &mapping, i const JoyBinding binding = mapping.bindings[i]; if (binding.inputType == TYPE_AXIS && binding.input.axis.axis == p_axis) { float value = p_value.value; - if (binding.input.axis.invert) + if (binding.input.axis.invert) { value = -value; + } if (binding.input.axis.range == FULL_AXIS || (binding.input.axis.range == POSITIVE_HALF_AXIS && value > 0) || (binding.input.axis.range == NEGATIVE_HALF_AXIS && value < 0)) { @@ -1180,11 +1125,9 @@ Input::JoyEvent Input::_get_mapped_axis_event(const JoyDeviceMapping &mapping, i } void Input::_get_mapped_hat_events(const JoyDeviceMapping &mapping, int p_hat, JoyEvent r_events[]) { - for (int i = 0; i < mapping.bindings.size(); i++) { const JoyBinding binding = mapping.bindings[i]; if (binding.inputType == TYPE_HAT && binding.input.hat.hat == p_hat) { - int index; switch (binding.input.hat.hat_mask) { case HAT_MASK_UP: @@ -1220,25 +1163,24 @@ void Input::_get_mapped_hat_events(const JoyDeviceMapping &mapping, int p_hat, J } JoyButtonList Input::_get_output_button(String output) { - for (int i = 0; _joy_buttons[i]; i++) { - if (output == _joy_buttons[i]) + if (output == _joy_buttons[i]) { return JoyButtonList(i); + } } return JoyButtonList::JOY_INVALID_BUTTON; } JoyAxisList Input::_get_output_axis(String output) { - for (int i = 0; _joy_axes[i]; i++) { - if (output == _joy_axes[i]) + if (output == _joy_axes[i]) { return JoyAxisList(i); + } } return JoyAxisList::JOY_INVALID_AXIS; } void Input::parse_mapping(String p_mapping) { - _THREAD_SAFE_METHOD_; JoyDeviceMapping mapping; @@ -1255,26 +1197,28 @@ void Input::parse_mapping(String p_mapping) { int idx = 1; while (++idx < entry.size()) { - - if (entry[idx] == "") + if (entry[idx] == "") { continue; + } String output = entry[idx].get_slice(":", 0).replace(" ", ""); String input = entry[idx].get_slice(":", 1).replace(" ", ""); ERR_CONTINUE_MSG(output.length() < 1 || input.length() < 2, String(entry[idx] + "\nInvalid device mapping entry: " + entry[idx])); - if (output == "platform") + if (output == "platform") { continue; + } JoyAxisRange output_range = FULL_AXIS; if (output[0] == '+' || output[0] == '-') { ERR_CONTINUE_MSG(output.length() < 2, String(entry[idx] + "\nInvalid output: " + entry[idx])); output = output.right(1); - if (output[0] == '+') + if (output[0] == '+') { output_range = POSITIVE_HALF_AXIS; - else if (output[0] == '-') + } else if (output[0] == '-') { output_range = NEGATIVE_HALF_AXIS; + } } JoyAxisRange input_range = FULL_AXIS; @@ -1286,8 +1230,9 @@ void Input::parse_mapping(String p_mapping) { input = input.right(1); } bool invert_axis = false; - if (input[input.length() - 1] == '~') + if (input[input.length() - 1] == '~') { invert_axis = true; + } JoyButtonList output_button = _get_output_button(output); JoyAxisList output_axis = _get_output_axis(output); @@ -1361,7 +1306,6 @@ void Input::remove_joy_mapping(String p_guid) { } void Input::set_fallback_mapping(String p_guid) { - for (int i = 0; i < map_db.size(); i++) { if (map_db[i].uid == p_guid) { fallback_mapping = i; @@ -1431,7 +1375,6 @@ int Input::get_joy_axis_index_from_string(String p_axis) { } Input::Input() { - singleton = this; // Parse default mappings. @@ -1447,8 +1390,9 @@ Input::Input() { if (env_mapping != "") { Vector<String> entries = env_mapping.split("\n"); for (int i = 0; i < entries.size(); i++) { - if (entries[i] == "") + if (entries[i] == "") { continue; + } parse_mapping(entries[i]); } } |