Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a signal for detecting action toggle states (pressed/released) in Input #43646

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions core/input/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,19 @@ Input::MouseMode Input::get_mouse_mode() const {
return get_mouse_mode_func();
}

void Input::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_POSTINITIALIZE: {
for (const Map<StringName, InputMap::Action>::Element *E = InputMap::get_singleton()->get_action_map().front(); E; E = E->next()) {
MethodInfo signal_pressed(Variant::NIL, vformat("%s_pressed", E->key()));
add_user_signal(signal_pressed);
MethodInfo signal_released(Variant::NIL, vformat("%s_released", E->key()));
add_user_signal(signal_released);
}
} break;
}
}

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);
Expand Down Expand Up @@ -209,6 +222,7 @@ void Input::_bind_methods() {
BIND_ENUM_CONSTANT(CURSOR_HSPLIT);
BIND_ENUM_CONSTANT(CURSOR_HELP);

ADD_SIGNAL(MethodInfo("action_toggled", PropertyInfo(Variant::STRING, "action"), PropertyInfo(Variant::BOOL, "pressed")));
ADD_SIGNAL(MethodInfo("joy_connection_changed", PropertyInfo(Variant::INT, "device"), PropertyInfo(Variant::BOOL, "connected")));
}

Expand Down Expand Up @@ -651,6 +665,14 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em
action.strength = 0.0f;
action.raw_strength = 0.0f;
action_state[E->key()] = action;
// For any action.
emit_signal("action_toggled", E->key(), action.pressed);
// For specific actions defined in `InputMap`.
if (action.pressed) {
emit_signal(vformat("%s_pressed", E->key()));
} else {
emit_signal(vformat("%s_released", E->key()));
}
}
action_state[E->key()].strength = p_event->get_action_strength(E->key());
action_state[E->key()].raw_strength = p_event->get_action_raw_strength(E->key());
Expand Down
1 change: 1 addition & 0 deletions core/input/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ class Input : public Object {

Map<int, VibrationInfo> joy_vibration;

void _notification(int p_what);
static void _bind_methods();

public:
Expand Down