From 829dade53bb3e5a23d60d0c2fc83aeef51debc56 Mon Sep 17 00:00:00 2001 From: Malcolm Anderson Date: Tue, 12 Nov 2024 09:43:29 -0800 Subject: [PATCH] Replace default deadzone magic number with named constant --- core/config/project_settings.cpp | 2 +- core/input/input_event.cpp | 2 +- core/input/input_map.cpp | 4 ++-- core/input/input_map.h | 4 +++- editor/project_settings_editor.cpp | 3 ++- 5 files changed, 9 insertions(+), 6 deletions(-) diff --git a/core/config/project_settings.cpp b/core/config/project_settings.cpp index 092177bc154..fa0e4037ba3 100644 --- a/core/config/project_settings.cpp +++ b/core/config/project_settings.cpp @@ -1408,7 +1408,7 @@ void ProjectSettings::_add_builtin_input_map() { } Dictionary action; - action["deadzone"] = Variant(0.2f); + action["deadzone"] = Variant(InputMap::DEFAULT_DEADZONE); action["events"] = events; String action_name = "input/" + E.key; diff --git a/core/input/input_event.cpp b/core/input/input_event.cpp index d125bad2525..4733aaf220a 100644 --- a/core/input/input_event.cpp +++ b/core/input/input_event.cpp @@ -1097,7 +1097,7 @@ JoyAxis InputEventJoypadMotion::get_axis() const { void InputEventJoypadMotion::set_axis_value(float p_value) { axis_value = p_value; - pressed = Math::abs(axis_value) >= 0.5f; + pressed = Math::abs(axis_value) >= InputMap::DEFAULT_DEADZONE; emit_changed(); } diff --git a/core/input/input_map.cpp b/core/input/input_map.cpp index 6378f185450..954df36f3e3 100644 --- a/core/input/input_map.cpp +++ b/core/input/input_map.cpp @@ -42,7 +42,7 @@ InputMap *InputMap::singleton = nullptr; void InputMap::_bind_methods() { ClassDB::bind_method(D_METHOD("has_action", "action"), &InputMap::has_action); ClassDB::bind_method(D_METHOD("get_actions"), &InputMap::_get_actions); - ClassDB::bind_method(D_METHOD("add_action", "action", "deadzone"), &InputMap::add_action, DEFVAL(0.2f)); + ClassDB::bind_method(D_METHOD("add_action", "action", "deadzone"), &InputMap::add_action, DEFVAL(DEFAULT_DEADZONE)); ClassDB::bind_method(D_METHOD("erase_action", "action"), &InputMap::erase_action); ClassDB::bind_method(D_METHOD("action_set_deadzone", "action", "deadzone"), &InputMap::action_set_deadzone); @@ -305,7 +305,7 @@ void InputMap::load_from_project_settings() { String name = pi.name.substr(pi.name.find("/") + 1, pi.name.length()); Dictionary action = GLOBAL_GET(pi.name); - float deadzone = action.has("deadzone") ? (float)action["deadzone"] : 0.2f; + float deadzone = action.has("deadzone") ? (float)action["deadzone"] : DEFAULT_DEADZONE; Array events = action["events"]; add_action(name, deadzone); diff --git a/core/input/input_map.h b/core/input/input_map.h index 45798490f7a..2b2a0253321 100644 --- a/core/input/input_map.h +++ b/core/input/input_map.h @@ -49,6 +49,8 @@ public: List> inputs; }; + static constexpr float DEFAULT_DEADZONE = 0.2f; + private: static InputMap *singleton; @@ -74,7 +76,7 @@ public: bool has_action(const StringName &p_action) const; List get_actions() const; - void add_action(const StringName &p_action, float p_deadzone = 0.2); + void add_action(const StringName &p_action, float p_deadzone = DEFAULT_DEADZONE); void erase_action(const StringName &p_action); float action_get_deadzone(const StringName &p_action); diff --git a/editor/project_settings_editor.cpp b/editor/project_settings_editor.cpp index 89c18143dc6..97f1d5d6411 100644 --- a/editor/project_settings_editor.cpp +++ b/editor/project_settings_editor.cpp @@ -31,6 +31,7 @@ #include "project_settings_editor.h" #include "core/config/project_settings.h" +#include "core/input/input_map.h" #include "editor/editor_inspector.h" #include "editor/editor_node.h" #include "editor/editor_settings.h" @@ -390,7 +391,7 @@ void ProjectSettingsEditor::_action_added(const String &p_name) { Dictionary action; action["events"] = Array(); - action["deadzone"] = 0.2f; + action["deadzone"] = InputMap::DEFAULT_DEADZONE; EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); undo_redo->create_action(TTR("Add Input Action"));