Merge pull request #77040 from lawnjelly/input_just_pressed
[3.x] Input - fix just pressed and released with short presses
This commit is contained in:
commit
da35288d4e
|
@ -206,9 +206,10 @@
|
||||||
<argument index="0" name="action" type="String" />
|
<argument index="0" name="action" type="String" />
|
||||||
<argument index="1" name="exact" type="bool" default="false" />
|
<argument index="1" name="exact" type="bool" default="false" />
|
||||||
<description>
|
<description>
|
||||||
Returns [code]true[/code] when the user starts pressing the action event, meaning it's [code]true[/code] only on the frame that the user pressed down the button.
|
Returns [code]true[/code] when the user has [i]started[/i] pressing the action event in the current frame or physics tick. It will only return [code]true[/code] on the frame or tick that the user pressed down the button.
|
||||||
This is useful for code that needs to run only once when an action is pressed, instead of every frame while it's pressed.
|
This is useful for code that needs to run only once when an action is pressed, instead of every frame while it's pressed.
|
||||||
If [code]exact[/code] is [code]false[/code], it ignores additional input modifiers for [InputEventKey] and [InputEventMouseButton] events, and the direction for [InputEventJoypadMotion] events.
|
If [code]exact[/code] is [code]false[/code], it ignores additional input modifiers for [InputEventKey] and [InputEventMouseButton] events, and the direction for [InputEventJoypadMotion] events.
|
||||||
|
[b]Note:[/b] Returning [code]true[/code] does not imply that the action is [i]still[/i] pressed. An action can be pressed and released again rapidly, and [code]true[/code] will still be returned so as not to miss input.
|
||||||
[b]Note:[/b] Due to keyboard ghosting, [method is_action_just_pressed] may return [code]false[/code] even if one of the action's keys is pressed. See [url=$DOCS_URL/tutorials/inputs/input_examples.html#keyboard-events]Input examples[/url] in the documentation for more information.
|
[b]Note:[/b] Due to keyboard ghosting, [method is_action_just_pressed] may return [code]false[/code] even if one of the action's keys is pressed. See [url=$DOCS_URL/tutorials/inputs/input_examples.html#keyboard-events]Input examples[/url] in the documentation for more information.
|
||||||
</description>
|
</description>
|
||||||
</method>
|
</method>
|
||||||
|
@ -217,8 +218,9 @@
|
||||||
<argument index="0" name="action" type="String" />
|
<argument index="0" name="action" type="String" />
|
||||||
<argument index="1" name="exact" type="bool" default="false" />
|
<argument index="1" name="exact" type="bool" default="false" />
|
||||||
<description>
|
<description>
|
||||||
Returns [code]true[/code] when the user stops pressing the action event, meaning it's [code]true[/code] only on the frame that the user released the button.
|
Returns [code]true[/code] when the user [i]stops[/i] pressing the action event in the current frame or physics tick. It will only return [code]true[/code] on the frame or tick that the user releases the button.
|
||||||
If [code]exact[/code] is [code]false[/code], it ignores additional input modifiers for [InputEventKey] and [InputEventMouseButton] events, and the direction for [InputEventJoypadMotion] events.
|
If [code]exact[/code] is [code]false[/code], it ignores additional input modifiers for [InputEventKey] and [InputEventMouseButton] events, and the direction for [InputEventJoypadMotion] events.
|
||||||
|
[b]Note:[/b] Returning [code]true[/code] does not imply that the action is [i]still[/i] not pressed. An action can be released and pressed again rapidly, and [code]true[/code] will still be returned so as not to miss input.
|
||||||
</description>
|
</description>
|
||||||
</method>
|
</method>
|
||||||
<method name="is_action_pressed" qualifiers="const">
|
<method name="is_action_pressed" qualifiers="const">
|
||||||
|
|
|
@ -664,6 +664,11 @@
|
||||||
Enabling this can greatly improve the responsiveness to input, specially in devices that need to run multiple physics frames per visible (idle) frame, because they can't run at the target frame rate.
|
Enabling this can greatly improve the responsiveness to input, specially in devices that need to run multiple physics frames per visible (idle) frame, because they can't run at the target frame rate.
|
||||||
[b]Note:[/b] Currently implemented only in Android.
|
[b]Note:[/b] Currently implemented only in Android.
|
||||||
</member>
|
</member>
|
||||||
|
<member name="input_devices/compatibility/legacy_just_pressed_behavior" type="bool" setter="" getter="" default="false">
|
||||||
|
If [code]true[/code], [method Input.is_action_just_pressed] and [method Input.is_action_just_released] will only return [code]true[/code] if the action is still in the respective state, i.e. an action that is pressed [i]and[/i] released on the same frame will be missed.
|
||||||
|
If [code]false[/code], no input will be lost.
|
||||||
|
[b]Note:[/b] You should in nearly all cases prefer the [code]false[/code] setting. The legacy behavior is to enable supporting old projects that rely on the old logic, without changes to script.
|
||||||
|
</member>
|
||||||
<member name="input_devices/pointing/emulate_mouse_from_touch" type="bool" setter="" getter="" default="true">
|
<member name="input_devices/pointing/emulate_mouse_from_touch" type="bool" setter="" getter="" default="true">
|
||||||
If [code]true[/code], sends mouse input events when tapping or swiping on the touchscreen.
|
If [code]true[/code], sends mouse input events when tapping or swiping on the touchscreen.
|
||||||
</member>
|
</member>
|
||||||
|
|
|
@ -32,6 +32,7 @@
|
||||||
|
|
||||||
#include "core/input_map.h"
|
#include "core/input_map.h"
|
||||||
#include "core/os/os.h"
|
#include "core/os/os.h"
|
||||||
|
#include "core/project_settings.h"
|
||||||
#include "main/default_controller_mappings.h"
|
#include "main/default_controller_mappings.h"
|
||||||
#include "scene/resources/texture.h"
|
#include "scene/resources/texture.h"
|
||||||
#include "servers/visual_server.h"
|
#include "servers/visual_server.h"
|
||||||
|
@ -120,10 +121,13 @@ bool InputDefault::is_action_just_pressed(const StringName &p_action, bool p_exa
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Backward compatibility for legacy behavior, only return true if currently pressed.
|
||||||
|
bool pressed_requirement = legacy_just_pressed_behavior ? E->get().pressed : true;
|
||||||
|
|
||||||
if (Engine::get_singleton()->is_in_physics_frame()) {
|
if (Engine::get_singleton()->is_in_physics_frame()) {
|
||||||
return E->get().pressed && E->get().physics_frame == Engine::get_singleton()->get_physics_frames();
|
return pressed_requirement && E->get().pressed_physics_frame == Engine::get_singleton()->get_physics_frames();
|
||||||
} else {
|
} else {
|
||||||
return E->get().pressed && E->get().idle_frame == Engine::get_singleton()->get_idle_frames();
|
return pressed_requirement && E->get().pressed_idle_frame == Engine::get_singleton()->get_idle_frames();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,10 +142,13 @@ bool InputDefault::is_action_just_released(const StringName &p_action, bool p_ex
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Backward compatibility for legacy behavior, only return true if currently released.
|
||||||
|
bool released_requirement = legacy_just_pressed_behavior ? !E->get().pressed : true;
|
||||||
|
|
||||||
if (Engine::get_singleton()->is_in_physics_frame()) {
|
if (Engine::get_singleton()->is_in_physics_frame()) {
|
||||||
return !E->get().pressed && E->get().physics_frame == Engine::get_singleton()->get_physics_frames();
|
return released_requirement && E->get().released_physics_frame == Engine::get_singleton()->get_physics_frames();
|
||||||
} else {
|
} else {
|
||||||
return !E->get().pressed && E->get().idle_frame == Engine::get_singleton()->get_idle_frames();
|
return released_requirement && E->get().released_idle_frame == Engine::get_singleton()->get_idle_frames();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -494,19 +501,26 @@ void InputDefault::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool
|
||||||
|
|
||||||
for (const Map<StringName, InputMap::Action>::Element *E = InputMap::get_singleton()->get_action_map().front(); E; E = E->next()) {
|
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())) {
|
if (InputMap::get_singleton()->event_is_action(p_event, E->key())) {
|
||||||
|
Action &action = action_state[E->key()];
|
||||||
|
|
||||||
// If not echo and action pressed state has changed
|
// If not echo and action pressed state has changed
|
||||||
if (!p_event->is_echo() && is_action_pressed(E->key(), false) != p_event->is_action_pressed(E->key())) {
|
if (!p_event->is_echo() && is_action_pressed(E->key(), false) != p_event->is_action_pressed(E->key())) {
|
||||||
Action action;
|
if (p_event->is_action_pressed(E->key())) {
|
||||||
action.physics_frame = Engine::get_singleton()->get_physics_frames();
|
action.pressed = true;
|
||||||
action.idle_frame = Engine::get_singleton()->get_idle_frames();
|
action.pressed_physics_frame = Engine::get_singleton()->get_physics_frames();
|
||||||
action.pressed = p_event->is_action_pressed(E->key());
|
action.pressed_idle_frame = Engine::get_singleton()->get_idle_frames();
|
||||||
|
} else {
|
||||||
|
action.pressed = false;
|
||||||
|
action.released_physics_frame = Engine::get_singleton()->get_physics_frames();
|
||||||
|
action.released_idle_frame = Engine::get_singleton()->get_idle_frames();
|
||||||
|
}
|
||||||
action.strength = 0.0f;
|
action.strength = 0.0f;
|
||||||
action.raw_strength = 0.0f;
|
action.raw_strength = 0.0f;
|
||||||
action.exact = InputMap::get_singleton()->event_is_action(p_event, E->key(), true);
|
action.exact = InputMap::get_singleton()->event_is_action(p_event, E->key(), true);
|
||||||
action_state[E->key()] = action;
|
|
||||||
}
|
}
|
||||||
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());
|
action.strength = p_event->get_action_strength(E->key());
|
||||||
|
action.raw_strength = p_event->get_action_raw_strength(E->key());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -627,29 +641,27 @@ void InputDefault::iteration(float p_step) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void InputDefault::action_press(const StringName &p_action, float p_strength) {
|
void InputDefault::action_press(const StringName &p_action, float p_strength) {
|
||||||
Action action;
|
// Create or retrieve existing action.
|
||||||
|
Action &action = action_state[p_action];
|
||||||
|
|
||||||
action.physics_frame = Engine::get_singleton()->get_physics_frames();
|
action.pressed_physics_frame = Engine::get_singleton()->get_physics_frames();
|
||||||
action.idle_frame = Engine::get_singleton()->get_idle_frames();
|
action.pressed_idle_frame = Engine::get_singleton()->get_idle_frames();
|
||||||
action.pressed = true;
|
action.pressed = true;
|
||||||
|
action.exact = true;
|
||||||
action.strength = p_strength;
|
action.strength = p_strength;
|
||||||
action.raw_strength = p_strength;
|
action.raw_strength = p_strength;
|
||||||
action.exact = true;
|
|
||||||
|
|
||||||
action_state[p_action] = action;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void InputDefault::action_release(const StringName &p_action) {
|
void InputDefault::action_release(const StringName &p_action) {
|
||||||
Action action;
|
// Create or retrieve existing action.
|
||||||
|
Action &action = action_state[p_action];
|
||||||
|
|
||||||
action.physics_frame = Engine::get_singleton()->get_physics_frames();
|
action.released_physics_frame = Engine::get_singleton()->get_physics_frames();
|
||||||
action.idle_frame = Engine::get_singleton()->get_idle_frames();
|
action.released_idle_frame = Engine::get_singleton()->get_idle_frames();
|
||||||
action.pressed = false;
|
action.pressed = false;
|
||||||
action.strength = 0.f;
|
|
||||||
action.raw_strength = 0.f;
|
|
||||||
action.exact = true;
|
action.exact = true;
|
||||||
|
action.strength = 0.0f;
|
||||||
action_state[p_action] = action;
|
action.raw_strength = 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
void InputDefault::set_emulate_touch_from_mouse(bool p_emulate) {
|
void InputDefault::set_emulate_touch_from_mouse(bool p_emulate) {
|
||||||
|
@ -821,6 +833,12 @@ InputDefault::InputDefault() {
|
||||||
|
|
||||||
fallback_mapping = -1;
|
fallback_mapping = -1;
|
||||||
|
|
||||||
|
legacy_just_pressed_behavior = GLOBAL_DEF("input_devices/compatibility/legacy_just_pressed_behavior", false);
|
||||||
|
if (Engine::get_singleton()->is_editor_hint()) {
|
||||||
|
// Always use standard behaviour in the editor.
|
||||||
|
legacy_just_pressed_behavior = false;
|
||||||
|
}
|
||||||
|
|
||||||
// Parse default mappings.
|
// Parse default mappings.
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
|
@ -50,14 +50,17 @@ class InputDefault : public Input {
|
||||||
Vector3 gyroscope;
|
Vector3 gyroscope;
|
||||||
Vector2 mouse_pos;
|
Vector2 mouse_pos;
|
||||||
MainLoop *main_loop;
|
MainLoop *main_loop;
|
||||||
|
bool legacy_just_pressed_behavior = false;
|
||||||
|
|
||||||
struct Action {
|
struct Action {
|
||||||
uint64_t physics_frame;
|
uint64_t pressed_physics_frame = UINT64_MAX;
|
||||||
uint64_t idle_frame;
|
uint64_t pressed_idle_frame = UINT64_MAX;
|
||||||
bool pressed;
|
uint64_t released_physics_frame = UINT64_MAX;
|
||||||
bool exact;
|
uint64_t released_idle_frame = UINT64_MAX;
|
||||||
float strength;
|
bool pressed = false;
|
||||||
float raw_strength;
|
bool exact = true;
|
||||||
|
float strength = 0.0f;
|
||||||
|
float raw_strength = 0.0f;
|
||||||
};
|
};
|
||||||
|
|
||||||
Map<StringName, Action> action_state;
|
Map<StringName, Action> action_state;
|
||||||
|
|
Loading…
Reference in New Issue