1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-04 12:00:25 +00:00

Fix and rename mouse filter recursive behavior

and focus mode recursive behavior.
This commit is contained in:
kit
2025-04-09 12:40:40 -04:00
parent a8598cd8e2
commit d16e8b7ca4
10 changed files with 282 additions and 173 deletions

View File

@@ -1884,44 +1884,67 @@ Control::MouseFilter Control::get_mouse_filter() const {
return data.mouse_filter;
}
Control::MouseFilter Control::get_mouse_filter_with_recursive() const {
Control::MouseFilter Control::get_mouse_filter_with_override() const {
ERR_READ_THREAD_GUARD_V(MOUSE_FILTER_IGNORE);
if (_is_parent_mouse_disabled()) {
if (!_is_mouse_filter_enabled()) {
return MOUSE_FILTER_IGNORE;
}
return data.mouse_filter;
}
void Control::set_mouse_recursive_behavior(RecursiveBehavior p_recursive_mouse_behavior) {
void Control::set_mouse_behavior_recursive(MouseBehaviorRecursive p_mouse_behavior_recursive) {
ERR_MAIN_THREAD_GUARD;
ERR_FAIL_INDEX((int)p_recursive_mouse_behavior, 4);
if (data.mouse_recursive_behavior == p_recursive_mouse_behavior) {
ERR_FAIL_INDEX(p_mouse_behavior_recursive, 3);
if (data.mouse_behavior_recursive == p_mouse_behavior_recursive) {
return;
}
_set_mouse_recursive_behavior_ignore_cache(p_recursive_mouse_behavior);
data.mouse_behavior_recursive = p_mouse_behavior_recursive;
_update_mouse_behavior_recursive();
}
void Control::_set_mouse_recursive_behavior_ignore_cache(RecursiveBehavior p_recursive_mouse_behavior) {
data.mouse_recursive_behavior = p_recursive_mouse_behavior;
if (p_recursive_mouse_behavior == RECURSIVE_BEHAVIOR_INHERITED) {
Control *parent = get_parent_control();
if (parent) {
_propagate_mouse_behavior_recursively(parent->data.parent_mouse_recursive_behavior, false);
Control::MouseBehaviorRecursive Control::get_mouse_behavior_recursive() const {
ERR_READ_THREAD_GUARD_V(MOUSE_BEHAVIOR_INHERITED);
return data.mouse_behavior_recursive;
}
bool Control::_is_mouse_filter_enabled() const {
ERR_READ_THREAD_GUARD_V(false);
if (data.mouse_behavior_recursive == MOUSE_BEHAVIOR_INHERITED) {
if (data.parent_control) {
return data.parent_mouse_behavior_recursive_enabled;
}
return true;
}
return data.mouse_behavior_recursive == MOUSE_BEHAVIOR_ENABLED;
}
void Control::_update_mouse_behavior_recursive() {
if (data.mouse_behavior_recursive == MOUSE_BEHAVIOR_INHERITED) {
if (data.parent_control) {
_propagate_mouse_behavior_recursive_recursively(data.parent_control->_is_mouse_filter_enabled(), false);
} else {
_propagate_mouse_behavior_recursively(RECURSIVE_BEHAVIOR_ENABLED, false);
_propagate_mouse_behavior_recursive_recursively(true, false);
}
} else {
_propagate_mouse_behavior_recursively(p_recursive_mouse_behavior, false);
_propagate_mouse_behavior_recursive_recursively(data.mouse_behavior_recursive == MOUSE_BEHAVIOR_ENABLED, false);
}
if (get_viewport()) {
get_viewport()->_gui_update_mouse_over();
}
}
Control::RecursiveBehavior Control::get_mouse_recursive_behavior() const {
ERR_READ_THREAD_GUARD_V(RECURSIVE_BEHAVIOR_INHERITED);
return data.mouse_recursive_behavior;
void Control::_propagate_mouse_behavior_recursive_recursively(bool p_enabled, bool p_skip_non_inherited) {
if (p_skip_non_inherited && data.mouse_behavior_recursive != MOUSE_BEHAVIOR_INHERITED) {
return;
}
data.parent_mouse_behavior_recursive_enabled = p_enabled;
for (int i = 0; i < get_child_count(); i++) {
Control *control = Object::cast_to<Control>(get_child(i));
if (control) {
control->_propagate_mouse_behavior_recursive_recursively(p_enabled, true);
}
}
}
void Control::set_force_pass_scroll_events(bool p_force_pass_scroll_events) {
@@ -2105,40 +2128,69 @@ Control::FocusMode Control::get_focus_mode() const {
return data.focus_mode;
}
Control::FocusMode Control::get_focus_mode_with_recursive() const {
Control::FocusMode Control::get_focus_mode_with_override() const {
ERR_READ_THREAD_GUARD_V(FOCUS_NONE);
if (_is_focus_disabled_recursively()) {
if (!_is_focus_mode_enabled()) {
return FOCUS_NONE;
}
return data.focus_mode;
}
void Control::set_focus_recursive_behavior(RecursiveBehavior p_recursive_focus_behavior) {
void Control::set_focus_behavior_recursive(FocusBehaviorRecursive p_focus_behavior_recursive) {
ERR_MAIN_THREAD_GUARD;
ERR_FAIL_INDEX((int)p_recursive_focus_behavior, 4);
if (data.focus_recursive_behavior == p_recursive_focus_behavior) {
ERR_FAIL_INDEX((int)p_focus_behavior_recursive, 3);
if (data.focus_behavior_recursive == p_focus_behavior_recursive) {
return;
}
_set_focus_recursive_behavior_ignore_cache(p_recursive_focus_behavior);
data.focus_behavior_recursive = p_focus_behavior_recursive;
_update_focus_behavior_recursive();
}
void Control::_set_focus_recursive_behavior_ignore_cache(RecursiveBehavior p_recursive_focus_behavior) {
data.focus_recursive_behavior = p_recursive_focus_behavior;
if (p_recursive_focus_behavior == RECURSIVE_BEHAVIOR_INHERITED) {
Control::FocusBehaviorRecursive Control::get_focus_behavior_recursive() const {
ERR_READ_THREAD_GUARD_V(FOCUS_BEHAVIOR_INHERITED);
return data.focus_behavior_recursive;
}
bool Control::_is_focus_mode_enabled() const {
if (data.focus_behavior_recursive == FOCUS_BEHAVIOR_INHERITED) {
if (data.parent_control) {
return data.parent_focus_behavior_recursive_enabled;
}
return true;
}
return data.focus_behavior_recursive == FOCUS_BEHAVIOR_ENABLED;
}
void Control::_update_focus_behavior_recursive() {
if (data.focus_behavior_recursive == FOCUS_BEHAVIOR_INHERITED) {
Control *parent = get_parent_control();
if (parent) {
_propagate_focus_behavior_recursively(parent->data.parent_focus_recursive_behavior, false);
_propagate_focus_behavior_recursive_recursively(parent->_is_focus_mode_enabled(), false);
} else {
_propagate_focus_behavior_recursively(RECURSIVE_BEHAVIOR_ENABLED, false);
_propagate_focus_behavior_recursive_recursively(true, false);
}
} else {
_propagate_focus_behavior_recursively(p_recursive_focus_behavior, false);
_propagate_focus_behavior_recursive_recursively(data.focus_behavior_recursive == FOCUS_BEHAVIOR_ENABLED, false);
}
}
Control::RecursiveBehavior Control::get_focus_recursive_behavior() const {
ERR_READ_THREAD_GUARD_V(RECURSIVE_BEHAVIOR_INHERITED);
return data.focus_recursive_behavior;
void Control::_propagate_focus_behavior_recursive_recursively(bool p_enabled, bool p_skip_non_inherited) {
if (is_inside_tree() && (data.focus_behavior_recursive == FOCUS_BEHAVIOR_DISABLED || (data.focus_behavior_recursive == FOCUS_BEHAVIOR_INHERITED && !p_enabled)) && has_focus()) {
release_focus();
}
if (p_skip_non_inherited && data.focus_behavior_recursive != FOCUS_BEHAVIOR_INHERITED) {
return;
}
data.parent_focus_behavior_recursive_enabled = p_enabled;
for (int i = 0; i < get_child_count(); i++) {
Control *control = Object::cast_to<Control>(get_child(i));
if (control) {
control->_propagate_focus_behavior_recursive_recursively(p_enabled, true);
}
}
}
bool Control::has_focus() const {
@@ -2150,8 +2202,8 @@ void Control::grab_focus() {
ERR_MAIN_THREAD_GUARD;
ERR_FAIL_COND(!is_inside_tree());
if (data.focus_mode == FOCUS_NONE) {
WARN_PRINT("This control can't grab focus. Use set_focus_mode() to allow a control to get focus.");
if (get_focus_mode_with_override() == FOCUS_NONE) {
WARN_PRINT("This control can't grab focus. Use set_focus_mode() and set_focus_behavior_recursive() to allow a control to get focus.");
return;
}
@@ -2211,7 +2263,7 @@ Control *Control::find_next_valid_focus() const {
ERR_FAIL_NULL_V_MSG(n, nullptr, "Next focus node path is invalid: '" + data.focus_next + "'.");
Control *c = Object::cast_to<Control>(n);
ERR_FAIL_NULL_V_MSG(c, nullptr, "Next focus node is not a control: '" + n->get_name() + "'.");
if (c->is_visible_in_tree() && c->get_focus_mode_with_recursive() != FOCUS_NONE) {
if (c->is_visible_in_tree() && c->get_focus_mode_with_override() != FOCUS_NONE) {
return c;
}
}
@@ -2278,7 +2330,7 @@ Control *Control::find_next_valid_focus() const {
break;
}
if ((next_child->get_focus_mode_with_recursive() == FOCUS_ALL) || (ac_enabled && next_child->get_focus_mode_with_recursive() == FOCUS_ACCESSIBILITY)) {
if ((next_child->get_focus_mode_with_override() == FOCUS_ALL) || (ac_enabled && next_child->get_focus_mode_with_override() == FOCUS_ACCESSIBILITY)) {
return next_child;
}
@@ -2315,7 +2367,7 @@ Control *Control::find_prev_valid_focus() const {
ERR_FAIL_NULL_V_MSG(n, nullptr, "Previous focus node path is invalid: '" + data.focus_prev + "'.");
Control *c = Object::cast_to<Control>(n);
ERR_FAIL_NULL_V_MSG(c, nullptr, "Previous focus node is not a control: '" + n->get_name() + "'.");
if (c->is_visible_in_tree() && c->get_focus_mode_with_recursive() != FOCUS_NONE) {
if (c->is_visible_in_tree() && c->get_focus_mode_with_override() != FOCUS_NONE) {
return c;
}
}
@@ -2376,7 +2428,7 @@ Control *Control::find_prev_valid_focus() const {
}
}
if ((prev_child->get_focus_mode_with_recursive() == FOCUS_ALL) || (ac_enabled && prev_child->get_focus_mode_with_recursive() == FOCUS_ACCESSIBILITY)) {
if ((prev_child->get_focus_mode_with_override() == FOCUS_ALL) || (ac_enabled && prev_child->get_focus_mode_with_override() == FOCUS_ACCESSIBILITY)) {
return prev_child;
}
@@ -2435,7 +2487,7 @@ Control *Control::_get_focus_neighbor(Side p_side, int p_count) {
ERR_FAIL_NULL_V_MSG(n, nullptr, "Neighbor focus node path is invalid: '" + data.focus_neighbor[p_side] + "'.");
Control *c = Object::cast_to<Control>(n);
ERR_FAIL_NULL_V_MSG(c, nullptr, "Neighbor focus node is not a control: '" + n->get_name() + "'.");
if (c->is_visible_in_tree() && c->get_focus_mode_with_recursive() != FOCUS_NONE) {
if (c->is_visible_in_tree() && c->get_focus_mode_with_override() != FOCUS_NONE) {
return c;
}
@@ -2544,64 +2596,6 @@ Control *Control::_get_focus_neighbor(Side p_side, int p_count) {
return result;
}
bool Control::_is_focus_disabled_recursively() const {
switch (data.focus_recursive_behavior) {
case RECURSIVE_BEHAVIOR_INHERITED:
return data.parent_focus_recursive_behavior == RECURSIVE_BEHAVIOR_DISABLED;
case RECURSIVE_BEHAVIOR_DISABLED:
return true;
case RECURSIVE_BEHAVIOR_ENABLED:
return false;
}
return false;
}
void Control::_propagate_focus_behavior_recursively(RecursiveBehavior p_focus_recursive_behavior, bool p_skip_non_inherited) {
if (is_inside_tree() && (data.focus_recursive_behavior == RECURSIVE_BEHAVIOR_DISABLED || (data.focus_recursive_behavior == RECURSIVE_BEHAVIOR_INHERITED && p_focus_recursive_behavior == RECURSIVE_BEHAVIOR_DISABLED)) && has_focus()) {
release_focus();
}
if (p_skip_non_inherited && data.focus_recursive_behavior != RECURSIVE_BEHAVIOR_INHERITED) {
return;
}
data.parent_focus_recursive_behavior = p_focus_recursive_behavior;
for (int i = 0; i < get_child_count(); i++) {
Control *control = Object::cast_to<Control>(get_child(i));
if (control) {
control->_propagate_focus_behavior_recursively(p_focus_recursive_behavior, true);
}
}
}
bool Control::_is_parent_mouse_disabled() const {
switch (data.mouse_recursive_behavior) {
case RECURSIVE_BEHAVIOR_INHERITED:
return data.parent_mouse_recursive_behavior == RECURSIVE_BEHAVIOR_DISABLED;
case RECURSIVE_BEHAVIOR_DISABLED:
return true;
case RECURSIVE_BEHAVIOR_ENABLED:
return false;
}
return false;
}
void Control::_propagate_mouse_behavior_recursively(RecursiveBehavior p_mouse_recursive_behavior, bool p_skip_non_inherited) {
if (p_skip_non_inherited && data.mouse_recursive_behavior != RECURSIVE_BEHAVIOR_INHERITED) {
return;
}
data.parent_mouse_recursive_behavior = p_mouse_recursive_behavior;
for (int i = 0; i < get_child_count(); i++) {
Control *control = Object::cast_to<Control>(get_child(i));
if (control) {
control->_propagate_mouse_behavior_recursively(p_mouse_recursive_behavior, true);
}
}
}
Control *Control::find_valid_focus_neighbor(Side p_side) const {
return const_cast<Control *>(this)->_get_focus_neighbor(p_side);
}
@@ -2617,7 +2611,7 @@ void Control::_window_find_focus_neighbor(const Vector2 &p_dir, Node *p_at, cons
Container *container = Object::cast_to<Container>(p_at);
bool in_container = container ? container->is_ancestor_of(this) : false;
if (c && c != this && ((c->get_focus_mode_with_recursive() == FOCUS_ALL) || (ac_enabled && c->get_focus_mode_with_recursive() == FOCUS_ACCESSIBILITY)) && !in_container && p_clamp.intersects(c->get_global_rect())) {
if (c && c != this && ((c->get_focus_mode_with_override() == FOCUS_ALL) || (ac_enabled && c->get_focus_mode_with_override() == FOCUS_ACCESSIBILITY)) && !in_container && p_clamp.intersects(c->get_global_rect())) {
Rect2 r_c = c->get_global_rect();
r_c = r_c.intersection(p_clamp);
real_t begin_d = p_dir.dot(r_c.get_position());
@@ -3620,8 +3614,8 @@ void Control::_notification(int p_notification) {
_update_layout_mode();
_set_focus_recursive_behavior_ignore_cache(data.focus_recursive_behavior);
_set_mouse_recursive_behavior_ignore_cache(data.mouse_recursive_behavior);
_update_focus_behavior_recursive();
_update_mouse_behavior_recursive();
} break;
case NOTIFICATION_UNPARENTED: {
@@ -3842,9 +3836,9 @@ void Control::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_global_rect"), &Control::get_global_rect);
ClassDB::bind_method(D_METHOD("set_focus_mode", "mode"), &Control::set_focus_mode);
ClassDB::bind_method(D_METHOD("get_focus_mode"), &Control::get_focus_mode);
ClassDB::bind_method(D_METHOD("get_focus_mode_with_recursive"), &Control::get_focus_mode_with_recursive);
ClassDB::bind_method(D_METHOD("set_focus_recursive_behavior", "focus_recursive_behavior"), &Control::set_focus_recursive_behavior);
ClassDB::bind_method(D_METHOD("get_focus_recursive_behavior"), &Control::get_focus_recursive_behavior);
ClassDB::bind_method(D_METHOD("get_focus_mode_with_override"), &Control::get_focus_mode_with_override);
ClassDB::bind_method(D_METHOD("set_focus_behavior_recursive", "focus_behavior_recursive"), &Control::set_focus_behavior_recursive);
ClassDB::bind_method(D_METHOD("get_focus_behavior_recursive"), &Control::get_focus_behavior_recursive);
ClassDB::bind_method(D_METHOD("has_focus"), &Control::has_focus);
ClassDB::bind_method(D_METHOD("grab_focus"), &Control::grab_focus);
ClassDB::bind_method(D_METHOD("release_focus"), &Control::release_focus);
@@ -3943,10 +3937,10 @@ void Control::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_mouse_filter", "filter"), &Control::set_mouse_filter);
ClassDB::bind_method(D_METHOD("get_mouse_filter"), &Control::get_mouse_filter);
ClassDB::bind_method(D_METHOD("get_mouse_filter_with_recursive"), &Control::get_mouse_filter_with_recursive);
ClassDB::bind_method(D_METHOD("get_mouse_filter_with_override"), &Control::get_mouse_filter_with_override);
ClassDB::bind_method(D_METHOD("set_mouse_recursive_behavior", "mouse_recursive_behavior"), &Control::set_mouse_recursive_behavior);
ClassDB::bind_method(D_METHOD("get_mouse_recursive_behavior"), &Control::get_mouse_recursive_behavior);
ClassDB::bind_method(D_METHOD("set_mouse_behavior_recursive", "mouse_behavior_recursive"), &Control::set_mouse_behavior_recursive);
ClassDB::bind_method(D_METHOD("get_mouse_behavior_recursive"), &Control::get_mouse_behavior_recursive);
ClassDB::bind_method(D_METHOD("set_force_pass_scroll_events", "force_pass_scroll_events"), &Control::set_force_pass_scroll_events);
ClassDB::bind_method(D_METHOD("is_force_pass_scroll_events"), &Control::is_force_pass_scroll_events);
@@ -4043,11 +4037,11 @@ void Control::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "focus_next", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_next", "get_focus_next");
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "focus_previous", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_previous", "get_focus_previous");
ADD_PROPERTY(PropertyInfo(Variant::INT, "focus_mode", PROPERTY_HINT_ENUM, "None,Click,All,Accessibility"), "set_focus_mode", "get_focus_mode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "focus_recursive_behavior", PROPERTY_HINT_ENUM, "Inherited,Disabled,Enabled"), "set_focus_recursive_behavior", "get_focus_recursive_behavior");
ADD_PROPERTY(PropertyInfo(Variant::INT, "focus_behavior_recursive", PROPERTY_HINT_ENUM, "Inherited,Disabled,Enabled"), "set_focus_behavior_recursive", "get_focus_behavior_recursive");
ADD_GROUP("Mouse", "mouse_");
ADD_PROPERTY(PropertyInfo(Variant::INT, "mouse_filter", PROPERTY_HINT_ENUM, "Stop,Pass (Propagate Up),Ignore"), "set_mouse_filter", "get_mouse_filter");
ADD_PROPERTY(PropertyInfo(Variant::INT, "mouse_recursive_behavior", PROPERTY_HINT_ENUM, "Inherited,Disabled,Enabled"), "set_mouse_recursive_behavior", "get_mouse_recursive_behavior");
ADD_PROPERTY(PropertyInfo(Variant::INT, "mouse_behavior_recursive", PROPERTY_HINT_ENUM, "Inherited,Disabled,Enabled"), "set_mouse_behavior_recursive", "get_mouse_behavior_recursive");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "mouse_force_pass_scroll_events"), "set_force_pass_scroll_events", "is_force_pass_scroll_events");
ADD_PROPERTY(PropertyInfo(Variant::INT, "mouse_default_cursor_shape", PROPERTY_HINT_ENUM, "Arrow,I-Beam,Pointing Hand,Cross,Wait,Busy,Drag,Can Drop,Forbidden,Vertical Resize,Horizontal Resize,Secondary Diagonal Resize,Main Diagonal Resize,Move,Vertical Split,Horizontal Split,Help"), "set_default_cursor_shape", "get_default_cursor_shape");
@@ -4063,9 +4057,13 @@ void Control::_bind_methods() {
BIND_ENUM_CONSTANT(FOCUS_ALL);
BIND_ENUM_CONSTANT(FOCUS_ACCESSIBILITY);
BIND_ENUM_CONSTANT(RECURSIVE_BEHAVIOR_INHERITED);
BIND_ENUM_CONSTANT(RECURSIVE_BEHAVIOR_DISABLED);
BIND_ENUM_CONSTANT(RECURSIVE_BEHAVIOR_ENABLED);
BIND_ENUM_CONSTANT(FOCUS_BEHAVIOR_INHERITED);
BIND_ENUM_CONSTANT(FOCUS_BEHAVIOR_DISABLED);
BIND_ENUM_CONSTANT(FOCUS_BEHAVIOR_ENABLED);
BIND_ENUM_CONSTANT(MOUSE_BEHAVIOR_INHERITED);
BIND_ENUM_CONSTANT(MOUSE_BEHAVIOR_DISABLED);
BIND_ENUM_CONSTANT(MOUSE_BEHAVIOR_ENABLED);
BIND_CONSTANT(NOTIFICATION_RESIZED);
BIND_CONSTANT(NOTIFICATION_MOUSE_ENTER);