Optimise is_layout_rtl by caching its return value.

This commit is contained in:
bruvzg 2021-07-25 10:33:00 +03:00
parent cf8e9fd80c
commit 9bb03e60d9
2 changed files with 34 additions and 20 deletions

View file

@ -454,6 +454,8 @@ void Control::set_layout_direction(Control::LayoutDirection p_direction) {
ERR_FAIL_INDEX((int)p_direction, 4); ERR_FAIL_INDEX((int)p_direction, 4);
data.layout_dir = p_direction; data.layout_dir = p_direction;
data.is_rtl_dirty = true;
propagate_notification(NOTIFICATION_LAYOUT_DIRECTION_CHANGED); propagate_notification(NOTIFICATION_LAYOUT_DIRECTION_CHANGED);
} }
@ -462,29 +464,35 @@ Control::LayoutDirection Control::get_layout_direction() const {
} }
bool Control::is_layout_rtl() const { bool Control::is_layout_rtl() const {
if (data.layout_dir == LAYOUT_DIRECTION_INHERITED) { if (data.is_rtl_dirty) {
Window *parent_window = get_parent_window(); const_cast<Control *>(this)->data.is_rtl_dirty = false;
Control *parent_control = get_parent_control(); if (data.layout_dir == LAYOUT_DIRECTION_INHERITED) {
if (parent_control) { Window *parent_window = get_parent_window();
return parent_control->is_layout_rtl(); Control *parent_control = get_parent_control();
} else if (parent_window) { if (parent_control) {
return parent_window->is_layout_rtl(); const_cast<Control *>(this)->data.is_rtl = parent_control->is_layout_rtl();
} else { } else if (parent_window) {
if (GLOBAL_GET(SNAME("internationalization/rendering/force_right_to_left_layout_direction"))) { const_cast<Control *>(this)->data.is_rtl = parent_window->is_layout_rtl();
return true; } else {
if (GLOBAL_GET(SNAME("internationalization/rendering/force_right_to_left_layout_direction"))) {
const_cast<Control *>(this)->data.is_rtl = true;
} else {
String locale = TranslationServer::get_singleton()->get_tool_locale();
const_cast<Control *>(this)->data.is_rtl = TS->is_locale_right_to_left(locale);
}
} }
String locale = TranslationServer::get_singleton()->get_tool_locale(); } else if (data.layout_dir == LAYOUT_DIRECTION_LOCALE) {
return TS->is_locale_right_to_left(locale); if (GLOBAL_GET(SNAME("internationalization/rendering/force_right_to_left_layout_direction"))) {
const_cast<Control *>(this)->data.is_rtl = true;
} else {
String locale = TranslationServer::get_singleton()->get_tool_locale();
const_cast<Control *>(this)->data.is_rtl = TS->is_locale_right_to_left(locale);
}
} else {
const_cast<Control *>(this)->data.is_rtl = (data.layout_dir == LAYOUT_DIRECTION_RTL);
} }
} else if (data.layout_dir == LAYOUT_DIRECTION_LOCALE) {
if (GLOBAL_GET(SNAME("internationalization/rendering/force_right_to_left_layout_direction"))) {
return true;
}
String locale = TranslationServer::get_singleton()->get_tool_locale();
return TS->is_locale_right_to_left(locale);
} else {
return (data.layout_dir == LAYOUT_DIRECTION_RTL);
} }
return data.is_rtl;
} }
void Control::_clear_size_warning() { void Control::_clear_size_warning() {
@ -534,6 +542,7 @@ void Control::_notification(int p_notification) {
} break; } break;
case NOTIFICATION_POST_ENTER_TREE: { case NOTIFICATION_POST_ENTER_TREE: {
data.minimum_size_valid = false; data.minimum_size_valid = false;
data.is_rtl_dirty = true;
_size_changed(); _size_changed();
} break; } break;
case NOTIFICATION_EXIT_TREE: { case NOTIFICATION_EXIT_TREE: {
@ -548,6 +557,7 @@ void Control::_notification(int p_notification) {
case NOTIFICATION_ENTER_CANVAS: { case NOTIFICATION_ENTER_CANVAS: {
data.parent = Object::cast_to<Control>(get_parent()); data.parent = Object::cast_to<Control>(get_parent());
data.parent_window = Object::cast_to<Window>(get_parent()); data.parent_window = Object::cast_to<Window>(get_parent());
data.is_rtl_dirty = true;
Node *parent = this; //meh Node *parent = this; //meh
Control *parent_control = nullptr; Control *parent_control = nullptr;
@ -613,6 +623,7 @@ void Control::_notification(int p_notification) {
data.parent = nullptr; data.parent = nullptr;
data.parent_canvas_item = nullptr; data.parent_canvas_item = nullptr;
data.parent_window = nullptr; data.parent_window = nullptr;
data.is_rtl_dirty = true;
} break; } break;
case NOTIFICATION_MOVED_IN_PARENT: { case NOTIFICATION_MOVED_IN_PARENT: {
@ -672,6 +683,7 @@ void Control::_notification(int p_notification) {
} break; } break;
case NOTIFICATION_TRANSLATION_CHANGED: case NOTIFICATION_TRANSLATION_CHANGED:
case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: { case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
data.is_rtl_dirty = true;
_size_changed(); _size_changed();
} break; } break;
} }

View file

@ -179,6 +179,8 @@ private:
GrowDirection v_grow = GROW_DIRECTION_END; GrowDirection v_grow = GROW_DIRECTION_END;
LayoutDirection layout_dir = LAYOUT_DIRECTION_INHERITED; LayoutDirection layout_dir = LAYOUT_DIRECTION_INHERITED;
bool is_rtl_dirty = true;
bool is_rtl = false;
real_t rotation = 0.0; real_t rotation = 0.0;
Vector2 scale = Vector2(1, 1); Vector2 scale = Vector2(1, 1);