diff --git a/core/debugger/remote_debugger.cpp b/core/debugger/remote_debugger.cpp index 1cf18239abd..b9db8ab1b02 100644 --- a/core/debugger/remote_debugger.cpp +++ b/core/debugger/remote_debugger.cpp @@ -33,7 +33,7 @@ #include "core/debugger/debugger_marshalls.h" #include "core/debugger/engine_debugger.h" #include "core/debugger/script_debugger.h" -#include "core/input/input.h" +#include "core/input/input_filter.h" #include "core/os/os.h" #include "core/project_settings.h" #include "core/script_language.h" @@ -659,9 +659,9 @@ void RemoteDebugger::debug(bool p_can_continue, bool p_is_error_breakpoint) { servers_profiler->skip_profile_frame = true; // Avoid frame time spike in debug. - Input::MouseMode mouse_mode = Input::get_singleton()->get_mouse_mode(); - if (mouse_mode != Input::MOUSE_MODE_VISIBLE) - Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE); + InputFilter::MouseMode mouse_mode = InputFilter::get_singleton()->get_mouse_mode(); + if (mouse_mode != InputFilter::MOUSE_MODE_VISIBLE) + InputFilter::get_singleton()->set_mouse_mode(InputFilter::MOUSE_MODE_VISIBLE); uint64_t loop_begin_usec = 0; uint64_t loop_time_sec = 0; @@ -779,8 +779,8 @@ void RemoteDebugger::debug(bool p_can_continue, bool p_is_error_breakpoint) { send_message("debug_exit", Array()); - if (mouse_mode != Input::MOUSE_MODE_VISIBLE) - Input::get_singleton()->set_mouse_mode(mouse_mode); + if (mouse_mode != InputFilter::MOUSE_MODE_VISIBLE) + InputFilter::get_singleton()->set_mouse_mode(mouse_mode); } void RemoteDebugger::poll_events(bool p_is_idle) { diff --git a/core/input/input.cpp b/core/input/input_filter.cpp similarity index 79% rename from core/input/input.cpp rename to core/input/input_filter.cpp index d40feffe972..088f7effd6e 100644 --- a/core/input/input.cpp +++ b/core/input/input_filter.cpp @@ -1,5 +1,5 @@ /*************************************************************************/ -/* input.cpp */ +/* input_filter.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ @@ -28,7 +28,7 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#include "input.h" +#include "input_filter.h" #include "core/input/default_controller_mappings.h" #include "core/input/input_map.h" @@ -39,72 +39,71 @@ #include "editor/editor_settings.h" #endif -Input *Input::singleton = NULL; +InputFilter *InputFilter::singleton = NULL; -void (*Input::set_mouse_mode_func)(Input::MouseMode) = nullptr; -Input::MouseMode (*Input::get_mouse_mode_func)() = nullptr; -void (*Input::warp_mouse_func)(const Vector2 &p_to_pos) = nullptr; -Input::CursorShape (*Input::get_current_cursor_shape_func)() = nullptr; -void (*Input::set_custom_mouse_cursor_func)(const RES &, Input::CursorShape, const Vector2 &) = nullptr; +void (*InputFilter::set_mouse_mode_func)(InputFilter::MouseMode) = nullptr; +InputFilter::MouseMode (*InputFilter::get_mouse_mode_func)() = nullptr; +void (*InputFilter::warp_mouse_func)(const Vector2 &p_to_pos) = nullptr; +InputFilter::CursorShape (*InputFilter::get_current_cursor_shape_func)() = nullptr; +void (*InputFilter::set_custom_mouse_cursor_func)(const RES &, InputFilter::CursorShape, const Vector2 &) = nullptr; -Input *Input::get_singleton() { +InputFilter *InputFilter::get_singleton() { return singleton; } -void Input::set_mouse_mode(MouseMode p_mode) { +void InputFilter::set_mouse_mode(MouseMode p_mode) { ERR_FAIL_INDEX((int)p_mode, 4); set_mouse_mode_func(p_mode); } -Input::MouseMode Input::get_mouse_mode() const { +InputFilter::MouseMode InputFilter::get_mouse_mode() const { return get_mouse_mode_func(); } -void Input::_bind_methods() { +void InputFilter::_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); - ClassDB::bind_method(D_METHOD("is_action_pressed", "action"), &Input::is_action_pressed); - ClassDB::bind_method(D_METHOD("is_action_just_pressed", "action"), &Input::is_action_just_pressed); - ClassDB::bind_method(D_METHOD("is_action_just_released", "action"), &Input::is_action_just_released); - ClassDB::bind_method(D_METHOD("get_action_strength", "action"), &Input::get_action_strength); - ClassDB::bind_method(D_METHOD("add_joy_mapping", "mapping", "update_existing"), &Input::add_joy_mapping, DEFVAL(false)); - ClassDB::bind_method(D_METHOD("remove_joy_mapping", "guid"), &Input::remove_joy_mapping); - ClassDB::bind_method(D_METHOD("joy_connection_changed", "device", "connected", "name", "guid"), &Input::joy_connection_changed); - ClassDB::bind_method(D_METHOD("is_joy_known", "device"), &Input::is_joy_known); - ClassDB::bind_method(D_METHOD("get_joy_axis", "device", "axis"), &Input::get_joy_axis); - ClassDB::bind_method(D_METHOD("get_joy_name", "device"), &Input::get_joy_name); - ClassDB::bind_method(D_METHOD("get_joy_guid", "device"), &Input::get_joy_guid); - ClassDB::bind_method(D_METHOD("get_connected_joypads"), &Input::get_connected_joypads); - ClassDB::bind_method(D_METHOD("get_joy_vibration_strength", "device"), &Input::get_joy_vibration_strength); - ClassDB::bind_method(D_METHOD("get_joy_vibration_duration", "device"), &Input::get_joy_vibration_duration); - ClassDB::bind_method(D_METHOD("get_joy_button_string", "button_index"), &Input::get_joy_button_string); - ClassDB::bind_method(D_METHOD("get_joy_button_index_from_string", "button"), &Input::get_joy_button_index_from_string); - ClassDB::bind_method(D_METHOD("get_joy_axis_string", "axis_index"), &Input::get_joy_axis_string); - ClassDB::bind_method(D_METHOD("get_joy_axis_index_from_string", "axis"), &Input::get_joy_axis_index_from_string); - ClassDB::bind_method(D_METHOD("start_joy_vibration", "device", "weak_magnitude", "strong_magnitude", "duration"), &Input::start_joy_vibration, DEFVAL(0)); - ClassDB::bind_method(D_METHOD("stop_joy_vibration", "device"), &Input::stop_joy_vibration); - ClassDB::bind_method(D_METHOD("vibrate_handheld", "duration_ms"), &Input::vibrate_handheld, DEFVAL(500)); - ClassDB::bind_method(D_METHOD("get_gravity"), &Input::get_gravity); - ClassDB::bind_method(D_METHOD("get_accelerometer"), &Input::get_accelerometer); - ClassDB::bind_method(D_METHOD("get_magnetometer"), &Input::get_magnetometer); - ClassDB::bind_method(D_METHOD("get_gyroscope"), &Input::get_gyroscope); - //ClassDB::bind_method(D_METHOD("get_mouse_position"),&Input::get_mouse_position); - this is not the function you want - ClassDB::bind_method(D_METHOD("get_last_mouse_speed"), &Input::get_last_mouse_speed); - ClassDB::bind_method(D_METHOD("get_mouse_button_mask"), &Input::get_mouse_button_mask); - ClassDB::bind_method(D_METHOD("set_mouse_mode", "mode"), &Input::set_mouse_mode); - ClassDB::bind_method(D_METHOD("get_mouse_mode"), &Input::get_mouse_mode); - ClassDB::bind_method(D_METHOD("warp_mouse_position", "to"), &Input::warp_mouse_position); - ClassDB::bind_method(D_METHOD("action_press", "action", "strength"), &Input::action_press, DEFVAL(1.f)); - ClassDB::bind_method(D_METHOD("action_release", "action"), &Input::action_release); - ClassDB::bind_method(D_METHOD("set_default_cursor_shape", "shape"), &Input::set_default_cursor_shape, DEFVAL(CURSOR_ARROW)); - ClassDB::bind_method(D_METHOD("get_current_cursor_shape"), &Input::get_current_cursor_shape); - ClassDB::bind_method(D_METHOD("set_custom_mouse_cursor", "image", "shape", "hotspot"), &Input::set_custom_mouse_cursor, DEFVAL(CURSOR_ARROW), DEFVAL(Vector2())); - ClassDB::bind_method(D_METHOD("parse_input_event", "event"), &Input::parse_input_event); - ClassDB::bind_method(D_METHOD("set_use_accumulated_input", "enable"), &Input::set_use_accumulated_input); + ClassDB::bind_method(D_METHOD("is_key_pressed", "keycode"), &InputFilter::is_key_pressed); + ClassDB::bind_method(D_METHOD("is_mouse_button_pressed", "button"), &InputFilter::is_mouse_button_pressed); + ClassDB::bind_method(D_METHOD("is_joy_button_pressed", "device", "button"), &InputFilter::is_joy_button_pressed); + ClassDB::bind_method(D_METHOD("is_action_pressed", "action"), &InputFilter::is_action_pressed); + ClassDB::bind_method(D_METHOD("is_action_just_pressed", "action"), &InputFilter::is_action_just_pressed); + ClassDB::bind_method(D_METHOD("is_action_just_released", "action"), &InputFilter::is_action_just_released); + ClassDB::bind_method(D_METHOD("get_action_strength", "action"), &InputFilter::get_action_strength); + ClassDB::bind_method(D_METHOD("add_joy_mapping", "mapping", "update_existing"), &InputFilter::add_joy_mapping, DEFVAL(false)); + ClassDB::bind_method(D_METHOD("remove_joy_mapping", "guid"), &InputFilter::remove_joy_mapping); + ClassDB::bind_method(D_METHOD("joy_connection_changed", "device", "connected", "name", "guid"), &InputFilter::joy_connection_changed); + ClassDB::bind_method(D_METHOD("is_joy_known", "device"), &InputFilter::is_joy_known); + ClassDB::bind_method(D_METHOD("get_joy_axis", "device", "axis"), &InputFilter::get_joy_axis); + ClassDB::bind_method(D_METHOD("get_joy_name", "device"), &InputFilter::get_joy_name); + ClassDB::bind_method(D_METHOD("get_joy_guid", "device"), &InputFilter::get_joy_guid); + ClassDB::bind_method(D_METHOD("get_connected_joypads"), &InputFilter::get_connected_joypads); + ClassDB::bind_method(D_METHOD("get_joy_vibration_strength", "device"), &InputFilter::get_joy_vibration_strength); + ClassDB::bind_method(D_METHOD("get_joy_vibration_duration", "device"), &InputFilter::get_joy_vibration_duration); + ClassDB::bind_method(D_METHOD("get_joy_button_string", "button_index"), &InputFilter::get_joy_button_string); + ClassDB::bind_method(D_METHOD("get_joy_button_index_from_string", "button"), &InputFilter::get_joy_button_index_from_string); + ClassDB::bind_method(D_METHOD("get_joy_axis_string", "axis_index"), &InputFilter::get_joy_axis_string); + ClassDB::bind_method(D_METHOD("get_joy_axis_index_from_string", "axis"), &InputFilter::get_joy_axis_index_from_string); + ClassDB::bind_method(D_METHOD("start_joy_vibration", "device", "weak_magnitude", "strong_magnitude", "duration"), &InputFilter::start_joy_vibration, DEFVAL(0)); + ClassDB::bind_method(D_METHOD("stop_joy_vibration", "device"), &InputFilter::stop_joy_vibration); + ClassDB::bind_method(D_METHOD("vibrate_handheld", "duration_ms"), &InputFilter::vibrate_handheld, DEFVAL(500)); + ClassDB::bind_method(D_METHOD("get_gravity"), &InputFilter::get_gravity); + ClassDB::bind_method(D_METHOD("get_accelerometer"), &InputFilter::get_accelerometer); + ClassDB::bind_method(D_METHOD("get_magnetometer"), &InputFilter::get_magnetometer); + ClassDB::bind_method(D_METHOD("get_gyroscope"), &InputFilter::get_gyroscope); + ClassDB::bind_method(D_METHOD("get_last_mouse_speed"), &InputFilter::get_last_mouse_speed); + ClassDB::bind_method(D_METHOD("get_mouse_button_mask"), &InputFilter::get_mouse_button_mask); + ClassDB::bind_method(D_METHOD("set_mouse_mode", "mode"), &InputFilter::set_mouse_mode); + ClassDB::bind_method(D_METHOD("get_mouse_mode"), &InputFilter::get_mouse_mode); + ClassDB::bind_method(D_METHOD("warp_mouse_position", "to"), &InputFilter::warp_mouse_position); + ClassDB::bind_method(D_METHOD("action_press", "action", "strength"), &InputFilter::action_press, DEFVAL(1.f)); + ClassDB::bind_method(D_METHOD("action_release", "action"), &InputFilter::action_release); + ClassDB::bind_method(D_METHOD("set_default_cursor_shape", "shape"), &InputFilter::set_default_cursor_shape, DEFVAL(CURSOR_ARROW)); + ClassDB::bind_method(D_METHOD("get_current_cursor_shape"), &InputFilter::get_current_cursor_shape); + ClassDB::bind_method(D_METHOD("set_custom_mouse_cursor", "image", "shape", "hotspot"), &InputFilter::set_custom_mouse_cursor, DEFVAL(CURSOR_ARROW), DEFVAL(Vector2())); + ClassDB::bind_method(D_METHOD("parse_input_event", "event"), &InputFilter::parse_input_event); + ClassDB::bind_method(D_METHOD("set_use_accumulated_input", "enable"), &InputFilter::set_use_accumulated_input); BIND_ENUM_CONSTANT(MOUSE_MODE_VISIBLE); BIND_ENUM_CONSTANT(MOUSE_MODE_HIDDEN); @@ -132,7 +131,7 @@ void Input::_bind_methods() { ADD_SIGNAL(MethodInfo("joy_connection_changed", PropertyInfo(Variant::INT, "device"), PropertyInfo(Variant::BOOL, "connected"))); } -void Input::get_argument_options(const StringName &p_function, int p_idx, List *r_options) const { +void InputFilter::get_argument_options(const StringName &p_function, int p_idx, List *r_options) const { #ifdef TOOLS_ENABLED const String quote_style = EDITOR_DEF("text_editor/completion/use_single_quotes", 0) ? "'" : "\""; @@ -156,7 +155,7 @@ void Input::get_argument_options(const StringName &p_function, int p_idx, Listget_ticks_usec(); uint32_t tdiff = tick - last_tick; @@ -180,26 +179,26 @@ void Input::SpeedTrack::update(const Vector2 &p_delta_p) { } } -void Input::SpeedTrack::reset() { +void InputFilter::SpeedTrack::reset() { last_tick = OS::get_singleton()->get_ticks_usec(); speed = Vector2(); accum_t = 0; } -Input::SpeedTrack::SpeedTrack() { +InputFilter::SpeedTrack::SpeedTrack() { min_ref_frame = 0.1; max_ref_frame = 0.3; reset(); } -bool Input::is_key_pressed(int p_keycode) const { +bool InputFilter::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 { +bool InputFilter::is_mouse_button_pressed(int p_button) const { _THREAD_SAFE_METHOD_ return (mouse_button_mask & (1 << (p_button - 1))) != 0; @@ -210,18 +209,18 @@ 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 { +bool InputFilter::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 { +bool InputFilter::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 { +bool InputFilter::is_action_just_pressed(const StringName &p_action) const { const Map::Element *E = action_state.find(p_action); if (!E) @@ -234,7 +233,7 @@ bool Input::is_action_just_pressed(const StringName &p_action) const { } } -bool Input::is_action_just_released(const StringName &p_action) const { +bool InputFilter::is_action_just_released(const StringName &p_action) const { const Map::Element *E = action_state.find(p_action); if (!E) @@ -247,7 +246,7 @@ bool Input::is_action_just_released(const StringName &p_action) const { } } -float Input::get_action_strength(const StringName &p_action) const { +float InputFilter::get_action_strength(const StringName &p_action) const { const Map::Element *E = action_state.find(p_action); if (!E) return 0.0f; @@ -255,7 +254,7 @@ float Input::get_action_strength(const StringName &p_action) const { return E->get().strength; } -float Input::get_joy_axis(int p_device, int p_axis) const { +float InputFilter::get_joy_axis(int p_device, int p_axis) const { _THREAD_SAFE_METHOD_ int c = _combine_device(p_axis, p_device); @@ -266,13 +265,13 @@ float Input::get_joy_axis(int p_device, int p_axis) const { } } -String Input::get_joy_name(int p_idx) { +String InputFilter::get_joy_name(int p_idx) { _THREAD_SAFE_METHOD_ return joy_names[p_idx].name; }; -Vector2 Input::get_joy_vibration_strength(int p_device) { +Vector2 InputFilter::get_joy_vibration_strength(int p_device) { if (joy_vibration.has(p_device)) { return Vector2(joy_vibration[p_device].weak_magnitude, joy_vibration[p_device].strong_magnitude); } else { @@ -280,7 +279,7 @@ Vector2 Input::get_joy_vibration_strength(int p_device) { } } -uint64_t Input::get_joy_vibration_timestamp(int p_device) { +uint64_t InputFilter::get_joy_vibration_timestamp(int p_device) { if (joy_vibration.has(p_device)) { return joy_vibration[p_device].timestamp; } else { @@ -288,7 +287,7 @@ uint64_t Input::get_joy_vibration_timestamp(int p_device) { } } -float Input::get_joy_vibration_duration(int p_device) { +float InputFilter::get_joy_vibration_duration(int p_device) { if (joy_vibration.has(p_device)) { return joy_vibration[p_device].duration; } else { @@ -308,7 +307,7 @@ static String _hex_str(uint8_t p_byte) { return ret; }; -void Input::joy_connection_changed(int p_idx, bool p_connected, String p_name, String p_guid) { +void InputFilter::joy_connection_changed(int p_idx, bool p_connected, String p_name, String p_guid) { _THREAD_SAFE_METHOD_ Joypad js; @@ -350,47 +349,36 @@ void Input::joy_connection_changed(int p_idx, bool p_connected, String p_name, S emit_signal("joy_connection_changed", p_idx, p_connected); }; -Vector3 Input::get_gravity() const { +Vector3 InputFilter::get_gravity() const { _THREAD_SAFE_METHOD_ return gravity; } -Vector3 Input::get_accelerometer() const { +Vector3 InputFilter::get_accelerometer() const { _THREAD_SAFE_METHOD_ return accelerometer; } -Vector3 Input::get_magnetometer() const { +Vector3 InputFilter::get_magnetometer() const { _THREAD_SAFE_METHOD_ return magnetometer; } -Vector3 Input::get_gyroscope() const { +Vector3 InputFilter::get_gyroscope() const { _THREAD_SAFE_METHOD_ return gyroscope; } -void Input::parse_drop_files(const Vector &p_files) { - if (main_loop) { - main_loop->drop_files(p_files); - } -} -void Input::parse_notification(int p_notification) { - if (main_loop) { - main_loop->notification(p_notification); - } -} - -void Input::parse_input_event(const Ref &p_event) { +void InputFilter::parse_input_event(const Ref &p_event) { _parse_input_event_impl(p_event, false); } -void Input::_parse_input_event_impl(const Ref &p_event, bool p_is_emulated) { +void InputFilter::_parse_input_event_impl(const Ref &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 @@ -424,12 +412,12 @@ void Input::_parse_input_event_impl(const Ref &p_event, bool p_is_em set_mouse_position(pos); } - if (main_loop && emulate_touch_from_mouse && !p_is_emulated && mb->get_button_index() == 1) { + if (event_dispatch_function && emulate_touch_from_mouse && !p_is_emulated && mb->get_button_index() == 1) { Ref touch_event; touch_event.instance(); touch_event->set_pressed(mb->is_pressed()); touch_event->set_position(mb->get_position()); - main_loop->input_event(touch_event); + event_dispatch_function(touch_event); } } @@ -442,7 +430,7 @@ void Input::_parse_input_event_impl(const Ref &p_event, bool p_is_em set_mouse_position(pos); } - if (main_loop && emulate_touch_from_mouse && !p_is_emulated && mm->get_button_mask() & 1) { + if (event_dispatch_function && emulate_touch_from_mouse && !p_is_emulated && mm->get_button_mask() & 1) { Ref drag_event; drag_event.instance(); @@ -450,7 +438,7 @@ void Input::_parse_input_event_impl(const Ref &p_event, bool p_is_em drag_event->set_relative(mm->get_relative()); drag_event->set_speed(mm->get_speed()); - main_loop->input_event(drag_event); + event_dispatch_function(drag_event); } } @@ -548,8 +536,8 @@ void Input::_parse_input_event_impl(const Ref &p_event, bool p_is_em if (ge.is_valid()) { - if (main_loop) { - main_loop->input_event(ge); + if (event_dispatch_function) { + event_dispatch_function(ge); } } @@ -569,18 +557,18 @@ void Input::_parse_input_event_impl(const Ref &p_event, bool p_is_em } } - if (main_loop) - main_loop->input_event(p_event); + if (event_dispatch_function) + event_dispatch_function(p_event); } -void Input::set_joy_axis(int p_device, int p_axis, float p_value) { +void InputFilter::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; } -void Input::start_joy_vibration(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration) { +void InputFilter::start_joy_vibration(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration) { _THREAD_SAFE_METHOD_ if (p_weak_magnitude < 0.f || p_weak_magnitude > 1.f || p_strong_magnitude < 0.f || p_strong_magnitude > 1.f) { return; @@ -593,7 +581,7 @@ void Input::start_joy_vibration(int p_device, float p_weak_magnitude, float p_st joy_vibration[p_device] = vibration; } -void Input::stop_joy_vibration(int p_device) { +void InputFilter::stop_joy_vibration(int p_device) { _THREAD_SAFE_METHOD_ VibrationInfo vibration; vibration.weak_magnitude = 0; @@ -603,66 +591,63 @@ void Input::stop_joy_vibration(int p_device) { joy_vibration[p_device] = vibration; } -void Input::vibrate_handheld(int p_duration_ms) { +void InputFilter::vibrate_handheld(int p_duration_ms) { OS::get_singleton()->vibrate_handheld(p_duration_ms); } -void Input::set_gravity(const Vector3 &p_gravity) { +void InputFilter::set_gravity(const Vector3 &p_gravity) { _THREAD_SAFE_METHOD_ gravity = p_gravity; } -void Input::set_accelerometer(const Vector3 &p_accel) { +void InputFilter::set_accelerometer(const Vector3 &p_accel) { _THREAD_SAFE_METHOD_ accelerometer = p_accel; } -void Input::set_magnetometer(const Vector3 &p_magnetometer) { +void InputFilter::set_magnetometer(const Vector3 &p_magnetometer) { _THREAD_SAFE_METHOD_ magnetometer = p_magnetometer; } -void Input::set_gyroscope(const Vector3 &p_gyroscope) { +void InputFilter::set_gyroscope(const Vector3 &p_gyroscope) { _THREAD_SAFE_METHOD_ gyroscope = p_gyroscope; } -void Input::set_main_loop(MainLoop *p_main_loop) { - main_loop = p_main_loop; -} - -void Input::set_mouse_position(const Point2 &p_posf) { +void InputFilter::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 { +Point2 InputFilter::get_mouse_position() const { return mouse_pos; } -Point2 Input::get_last_mouse_speed() const { +Point2 InputFilter::get_last_mouse_speed() const { return mouse_speed_track.speed; } -int Input::get_mouse_button_mask() const { +int InputFilter::get_mouse_button_mask() const { return mouse_button_mask; // do not trust OS implementation, should remove it - OS::get_singleton()->get_mouse_button_state(); } -void Input::warp_mouse_position(const Vector2 &p_to) { +void InputFilter::warp_mouse_position(const Vector2 &p_to) { warp_mouse_func(p_to); } -Point2i Input::warp_mouse_motion(const Ref &p_motion, const Rect2 &p_rect) { + +Point2i InputFilter::warp_mouse_motion(const Ref &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() @@ -688,10 +673,10 @@ Point2i Input::warp_mouse_motion(const Ref &p_motion, con return rel_warped; } -void Input::iteration(float p_step) { +void InputFilter::iteration(float p_step) { } -void Input::action_press(const StringName &p_action, float p_strength) { +void InputFilter::action_press(const StringName &p_action, float p_strength) { Action action; @@ -703,7 +688,7 @@ void Input::action_press(const StringName &p_action, float p_strength) { action_state[p_action] = action; } -void Input::action_release(const StringName &p_action) { +void InputFilter::action_release(const StringName &p_action) { Action action; @@ -715,19 +700,19 @@ void Input::action_release(const StringName &p_action) { action_state[p_action] = action; } -void Input::set_emulate_touch_from_mouse(bool p_emulate) { +void InputFilter::set_emulate_touch_from_mouse(bool p_emulate) { emulate_touch_from_mouse = p_emulate; } -bool Input::is_emulating_touch_from_mouse() const { +bool InputFilter::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() { +void InputFilter::ensure_touch_mouse_raised() { if (mouse_from_touch_index != -1) { mouse_from_touch_index = -1; @@ -746,22 +731,22 @@ void Input::ensure_touch_mouse_raised() { } } -void Input::set_emulate_mouse_from_touch(bool p_emulate) { +void InputFilter::set_emulate_mouse_from_touch(bool p_emulate) { emulate_mouse_from_touch = p_emulate; } -bool Input::is_emulating_mouse_from_touch() const { +bool InputFilter::is_emulating_mouse_from_touch() const { return emulate_mouse_from_touch; } -Input::CursorShape Input::get_default_cursor_shape() const { +InputFilter::CursorShape InputFilter::get_default_cursor_shape() const { return default_shape; } -void Input::set_default_cursor_shape(CursorShape p_shape) { +void InputFilter::set_default_cursor_shape(CursorShape p_shape) { if (default_shape == p_shape) return; @@ -776,19 +761,19 @@ void Input::set_default_cursor_shape(CursorShape p_shape) { parse_input_event(mm); } -Input::CursorShape Input::get_current_cursor_shape() const { +InputFilter::CursorShape InputFilter::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) { +void InputFilter::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) { if (Engine::get_singleton()->is_editor_hint()) return; set_custom_mouse_cursor_func(p_cursor, p_shape, p_hotspot); } -void Input::accumulate_input_event(const Ref &p_event) { +void InputFilter::accumulate_input_event(const Ref &p_event) { ERR_FAIL_COND(p_event.is_null()); if (!use_accumulated_input) { @@ -801,7 +786,7 @@ void Input::accumulate_input_event(const Ref &p_event) { accumulated_events.push_back(p_event); } -void Input::flush_accumulated_events() { +void InputFilter::flush_accumulated_events() { while (accumulated_events.front()) { parse_input_event(accumulated_events.front()->get()); @@ -809,12 +794,12 @@ void Input::flush_accumulated_events() { } } -void Input::set_use_accumulated_input(bool p_enable) { +void InputFilter::set_use_accumulated_input(bool p_enable) { use_accumulated_input = p_enable; } -void Input::release_pressed_events() { +void InputFilter::release_pressed_events() { flush_accumulated_events(); // this is needed to release actions strengths @@ -822,13 +807,17 @@ void Input::release_pressed_events() { joy_buttons_pressed.clear(); _joy_axis.clear(); - for (Map::Element *E = action_state.front(); E; E = E->next()) { + for (Map::Element *E = action_state.front(); E; E = E->next()) { if (E->get().pressed) action_release(E->key()); } } -void Input::joy_button(int p_device, int p_button, bool p_pressed) { +void InputFilter::set_event_dispatch_function(EventDispatchFunc p_function) { + event_dispatch_function = p_function; +} + +void InputFilter::joy_button(int p_device, int p_button, bool p_pressed) { _THREAD_SAFE_METHOD_; Joypad &joy = joy_names[p_device]; @@ -867,7 +856,7 @@ void Input::joy_button(int p_device, int p_button, bool p_pressed) { // no event? } -void Input::joy_axis(int p_device, int p_axis, const JoyAxis &p_value) { +void InputFilter::joy_axis(int p_device, int p_axis, const JoyAxis &p_value) { _THREAD_SAFE_METHOD_; @@ -982,7 +971,7 @@ void Input::joy_axis(int p_device, int p_axis, const JoyAxis &p_value) { //printf("invalid mapping\n"); } -void Input::joy_hat(int p_device, int p_val) { +void InputFilter::joy_hat(int p_device, int p_val) { _THREAD_SAFE_METHOD_; const Joypad &joy = joy_names[p_device]; @@ -1014,7 +1003,7 @@ void Input::joy_hat(int p_device, int p_val) { joy_names[p_device].hat_current = p_val; } -void Input::_button_event(int p_device, int p_index, bool p_pressed) { +void InputFilter::_button_event(int p_device, int p_index, bool p_pressed) { Ref ievent; ievent.instance(); @@ -1025,7 +1014,7 @@ void Input::_button_event(int p_device, int p_index, bool p_pressed) { parse_input_event(ievent); } -void Input::_axis_event(int p_device, int p_axis, float p_value) { +void InputFilter::_axis_event(int p_device, int p_axis, float p_value) { Ref ievent; ievent.instance(); @@ -1036,7 +1025,7 @@ void Input::_axis_event(int p_device, int p_axis, float p_value) { parse_input_event(ievent); }; -Input::JoyEvent Input::_find_to_event(String p_to) { +InputFilter::JoyEvent InputFilter::_find_to_event(String p_to) { // string names of the SDL buttons in the same order as input_event.h godot buttons static const char *buttons[] = { "a", "b", "x", "y", "leftshoulder", "rightshoulder", "lefttrigger", "righttrigger", "leftstick", "rightstick", "back", "start", "dpup", "dpdown", "dpleft", "dpright", "guide", NULL }; @@ -1074,7 +1063,7 @@ Input::JoyEvent Input::_find_to_event(String p_to) { return ret; }; -void Input::parse_mapping(String p_mapping) { +void InputFilter::parse_mapping(String p_mapping) { _THREAD_SAFE_METHOD_; JoyDeviceMapping mapping; @@ -1139,7 +1128,7 @@ void Input::parse_mapping(String p_mapping) { //printf("added mapping with uuid %ls\n", mapping.uid.c_str()); }; -void Input::add_joy_mapping(String p_mapping, bool p_update_existing) { +void InputFilter::add_joy_mapping(String p_mapping, bool p_update_existing) { parse_mapping(p_mapping); if (p_update_existing) { Vector entry = p_mapping.split(","); @@ -1152,7 +1141,7 @@ void Input::add_joy_mapping(String p_mapping, bool p_update_existing) { } } -void Input::remove_joy_mapping(String p_guid) { +void InputFilter::remove_joy_mapping(String p_guid) { for (int i = map_db.size() - 1; i >= 0; i--) { if (p_guid == map_db[i].uid) { map_db.remove(i); @@ -1165,7 +1154,7 @@ void Input::remove_joy_mapping(String p_guid) { } } -void Input::set_fallback_mapping(String p_guid) { +void InputFilter::set_fallback_mapping(String p_guid) { for (int i = 0; i < map_db.size(); i++) { if (map_db[i].uid == p_guid) { @@ -1176,17 +1165,17 @@ void Input::set_fallback_mapping(String p_guid) { } //platforms that use the remapping system can override and call to these ones -bool Input::is_joy_known(int p_device) { +bool InputFilter::is_joy_known(int p_device) { int mapping = joy_names[p_device].mapping; return mapping != -1 ? (mapping != fallback_mapping) : false; } -String Input::get_joy_guid(int p_device) const { +String InputFilter::get_joy_guid(int p_device) const { ERR_FAIL_COND_V(!joy_names.has(p_device), ""); return joy_names[p_device].uid; } -Array Input::get_connected_joypads() { +Array InputFilter::get_connected_joypads() { Array ret; Map::Element *elem = joy_names.front(); while (elem) { @@ -1230,12 +1219,12 @@ static const char *_axes[JOY_AXIS_MAX] = { "" }; -String Input::get_joy_button_string(int p_button) { +String InputFilter::get_joy_button_string(int p_button) { ERR_FAIL_INDEX_V(p_button, JOY_BUTTON_MAX, ""); return _buttons[p_button]; } -int Input::get_joy_button_index_from_string(String p_button) { +int InputFilter::get_joy_button_index_from_string(String p_button) { for (int i = 0; i < JOY_BUTTON_MAX; i++) { if (p_button == _buttons[i]) { return i; @@ -1244,7 +1233,7 @@ int Input::get_joy_button_index_from_string(String p_button) { ERR_FAIL_V(-1); } -int Input::get_unused_joy_id() { +int InputFilter::get_unused_joy_id() { for (int i = 0; i < JOYPADS_MAX; i++) { if (!joy_names.has(i) || !joy_names[i].connected) { return i; @@ -1253,12 +1242,12 @@ int Input::get_unused_joy_id() { return -1; } -String Input::get_joy_axis_string(int p_axis) { +String InputFilter::get_joy_axis_string(int p_axis) { ERR_FAIL_INDEX_V(p_axis, JOY_AXIS_MAX, ""); return _axes[p_axis]; } -int Input::get_joy_axis_index_from_string(String p_axis) { +int InputFilter::get_joy_axis_index_from_string(String p_axis) { for (int i = 0; i < JOY_AXIS_MAX; i++) { if (p_axis == _axes[i]) { return i; @@ -1267,7 +1256,7 @@ int Input::get_joy_axis_index_from_string(String p_axis) { ERR_FAIL_V(-1); } -Input::Input() { +InputFilter::InputFilter() { singleton = this; use_accumulated_input = true; @@ -1276,7 +1265,7 @@ Input::Input() { emulate_touch_from_mouse = false; emulate_mouse_from_touch = false; mouse_from_touch_index = -1; - main_loop = NULL; + event_dispatch_function = nullptr; default_shape = CURSOR_ARROW; hat_map_default[HAT_UP].type = TYPE_BUTTON; diff --git a/core/input/input.h b/core/input/input_filter.h similarity index 93% rename from core/input/input.h rename to core/input/input_filter.h index 6299255a921..908a0052283 100644 --- a/core/input/input.h +++ b/core/input/input_filter.h @@ -1,5 +1,5 @@ /*************************************************************************/ -/* input.h */ +/* input_filter.h */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ @@ -31,16 +31,16 @@ #ifndef INPUT_H #define INPUT_H +#include "core/input/input_event.h" #include "core/object.h" -#include "core/os/main_loop.h" #include "core/os/thread_safe.h" -class Input : public Object { +class InputFilter : public Object { - GDCLASS(Input, Object); + GDCLASS(InputFilter, Object); _THREAD_SAFE_CLASS_ - static Input *singleton; + static InputFilter *singleton; public: enum MouseMode { @@ -97,6 +97,8 @@ public: float value; }; + typedef void (*EventDispatchFunc)(const Ref &p_event); + private: int mouse_button_mask; @@ -110,7 +112,6 @@ private: Vector3 gyroscope; Vector2 mouse_pos; int64_t mouse_window; - MainLoop *main_loop; struct Action { uint64_t physics_frame; @@ -228,6 +229,8 @@ private: static CursorShape (*get_current_cursor_shape_func)(); static void (*set_custom_mouse_cursor_func)(const RES &, CursorShape, const Vector2 &); + EventDispatchFunc event_dispatch_function; + protected: static void _bind_methods(); @@ -236,7 +239,7 @@ public: MouseMode get_mouse_mode() const; void get_argument_options(const StringName &p_function, int p_idx, List *r_options) const; - static Input *get_singleton(); + static InputFilter *get_singleton(); bool is_key_pressed(int p_keycode) const; bool is_mouse_button_pressed(int p_button) const; @@ -267,8 +270,6 @@ public: void warp_mouse_position(const Vector2 &p_to); Point2i warp_mouse_motion(const Ref &p_motion, const Rect2 &p_rect); - void parse_drop_files(const Vector &p_files); - void parse_notification(int p_notification); void parse_input_event(const Ref &p_event); void set_gravity(const Vector3 &p_gravity); @@ -281,7 +282,6 @@ public: void stop_joy_vibration(int p_device); void vibrate_handheld(int p_duration_ms = 500); - void set_main_loop(MainLoop *p_main_loop); void set_mouse_position(const Point2 &p_posf); void action_press(const StringName &p_action, float p_strength = 1.f); @@ -299,7 +299,7 @@ public: CursorShape get_default_cursor_shape() const; void set_default_cursor_shape(CursorShape p_shape); CursorShape get_current_cursor_shape() const; - void set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape = Input::CURSOR_ARROW, const Vector2 &p_hotspot = Vector2()); + void set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape = InputFilter::CURSOR_ARROW, const Vector2 &p_hotspot = Vector2()); void parse_mapping(String p_mapping); void joy_button(int p_device, int p_button, bool p_pressed); @@ -326,10 +326,12 @@ public: void release_pressed_events(); - Input(); + void set_event_dispatch_function(EventDispatchFunc p_function); + + InputFilter(); }; -VARIANT_ENUM_CAST(Input::MouseMode); -VARIANT_ENUM_CAST(Input::CursorShape); +VARIANT_ENUM_CAST(InputFilter::MouseMode); +VARIANT_ENUM_CAST(InputFilter::CursorShape); #endif // INPUT_H diff --git a/core/os/main_loop.cpp b/core/os/main_loop.cpp index ab43ce4af7f..4e0743809b4 100644 --- a/core/os/main_loop.cpp +++ b/core/os/main_loop.cpp @@ -34,30 +34,18 @@ void MainLoop::_bind_methods() { - ClassDB::bind_method(D_METHOD("input_event", "event"), &MainLoop::input_event); - ClassDB::bind_method(D_METHOD("input_text", "text"), &MainLoop::input_text); ClassDB::bind_method(D_METHOD("init"), &MainLoop::init); ClassDB::bind_method(D_METHOD("iteration", "delta"), &MainLoop::iteration); ClassDB::bind_method(D_METHOD("idle", "delta"), &MainLoop::idle); ClassDB::bind_method(D_METHOD("finish"), &MainLoop::finish); - BIND_VMETHOD(MethodInfo("_input_event", PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent"))); - BIND_VMETHOD(MethodInfo("_input_text", PropertyInfo(Variant::STRING, "text"))); BIND_VMETHOD(MethodInfo("_initialize")); BIND_VMETHOD(MethodInfo(Variant::BOOL, "_iteration", PropertyInfo(Variant::FLOAT, "delta"))); BIND_VMETHOD(MethodInfo(Variant::BOOL, "_idle", PropertyInfo(Variant::FLOAT, "delta"))); - BIND_VMETHOD(MethodInfo("_drop_files", PropertyInfo(Variant::PACKED_STRING_ARRAY, "files"), PropertyInfo(Variant::INT, "from_screen"))); BIND_VMETHOD(MethodInfo("_finalize")); BIND_VMETHOD(MethodInfo("_global_menu_action", PropertyInfo(Variant::NIL, "id"), PropertyInfo(Variant::NIL, "meta"))); - BIND_CONSTANT(NOTIFICATION_WM_MOUSE_ENTER); - BIND_CONSTANT(NOTIFICATION_WM_MOUSE_EXIT); - BIND_CONSTANT(NOTIFICATION_WM_FOCUS_IN); - BIND_CONSTANT(NOTIFICATION_WM_FOCUS_OUT); - BIND_CONSTANT(NOTIFICATION_WM_QUIT_REQUEST); - BIND_CONSTANT(NOTIFICATION_WM_GO_BACK_REQUEST); - BIND_CONSTANT(NOTIFICATION_WM_UNFOCUS_REQUEST); BIND_CONSTANT(NOTIFICATION_OS_MEMORY_WARNING); BIND_CONSTANT(NOTIFICATION_TRANSLATION_CHANGED); BIND_CONSTANT(NOTIFICATION_WM_ABOUT); @@ -80,18 +68,6 @@ MainLoop::MainLoop() { MainLoop::~MainLoop() { } -void MainLoop::input_text(const String &p_text) { - - if (get_script_instance()) - get_script_instance()->call("_input_text", p_text); -} - -void MainLoop::input_event(const Ref &p_event) { - - if (get_script_instance()) - get_script_instance()->call("_input_event", p_event); -} - void MainLoop::init() { if (init_script.is_valid()) @@ -115,12 +91,6 @@ bool MainLoop::idle(float p_time) { return false; } -void MainLoop::drop_files(const Vector &p_files, int p_from_screen) { - - if (get_script_instance()) - get_script_instance()->call("_drop_files", p_files, p_from_screen); -} - void MainLoop::global_menu_action(const Variant &p_id, const Variant &p_meta) { if (get_script_instance()) diff --git a/core/os/main_loop.h b/core/os/main_loop.h index 2b7ec8fc5d7..51cdd3e8270 100644 --- a/core/os/main_loop.h +++ b/core/os/main_loop.h @@ -48,13 +48,6 @@ protected: public: enum { //make sure these are replicated in Node - NOTIFICATION_WM_MOUSE_ENTER = 1002, - NOTIFICATION_WM_MOUSE_EXIT = 1003, - NOTIFICATION_WM_FOCUS_IN = 1004, - NOTIFICATION_WM_FOCUS_OUT = 1005, - NOTIFICATION_WM_QUIT_REQUEST = 1006, - NOTIFICATION_WM_GO_BACK_REQUEST = 1007, - NOTIFICATION_WM_UNFOCUS_REQUEST = 1008, NOTIFICATION_OS_MEMORY_WARNING = 1009, NOTIFICATION_TRANSLATION_CHANGED = 1010, NOTIFICATION_WM_ABOUT = 1011, @@ -64,15 +57,11 @@ public: NOTIFICATION_APP_PAUSED = 1015, }; - virtual void input_event(const Ref &p_event); - virtual void input_text(const String &p_text); - virtual void init(); virtual bool iteration(float p_time); virtual bool idle(float p_time); virtual void finish(); - virtual void drop_files(const Vector &p_files, int p_from_screen = 0); virtual void global_menu_action(const Variant &p_id, const Variant &p_meta); void set_init_script(const Ref