summaryrefslogtreecommitdiff
path: root/scene/gui/grid_container.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'scene/gui/grid_container.cpp')
-rw-r--r--scene/gui/grid_container.cpp93
1 files changed, 53 insertions, 40 deletions
diff --git a/scene/gui/grid_container.cpp b/scene/gui/grid_container.cpp
index 16f6fd0111..1107e3a4af 100644
--- a/scene/gui/grid_container.cpp
+++ b/scene/gui/grid_container.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
@@ -31,18 +31,15 @@
#include "grid_container.h"
void GridContainer::_notification(int p_what) {
-
switch (p_what) {
-
case NOTIFICATION_SORT_CHILDREN: {
-
- Map<int, int> col_minw; // Max of min_width of all controls in each col (indexed by col).
+ Map<int, int> col_minw; // Max of min_width of all controls in each col (indexed by col).
Map<int, int> row_minh; // Max of min_height of all controls in each row (indexed by row).
Set<int> col_expanded; // Columns which have the SIZE_EXPAND flag set.
Set<int> row_expanded; // Rows which have the SIZE_EXPAND flag set.
- int hsep = get_theme_constant("hseparation");
- int vsep = get_theme_constant("vseparation");
+ int hsep = get_theme_constant(SNAME("hseparation"));
+ int vsep = get_theme_constant(SNAME("vseparation"));
int max_col = MIN(get_child_count(), columns);
int max_row = ceil((float)get_child_count() / (float)columns);
@@ -50,22 +47,25 @@ void GridContainer::_notification(int p_what) {
int valid_controls_index = 0;
for (int i = 0; i < get_child_count(); i++) {
Control *c = Object::cast_to<Control>(get_child(i));
- if (!c || !c->is_visible_in_tree())
+ if (!c || !c->is_visible_in_tree()) {
continue;
+ }
int row = valid_controls_index / columns;
int col = valid_controls_index % columns;
valid_controls_index++;
Size2i ms = c->get_combined_minimum_size();
- if (col_minw.has(col))
+ if (col_minw.has(col)) {
col_minw[col] = MAX(col_minw[col], ms.width);
- else
+ } else {
col_minw[col] = ms.width;
- if (row_minh.has(row))
+ }
+ if (row_minh.has(row)) {
row_minh[row] = MAX(row_minh[row], ms.height);
- else
+ } else {
row_minh[row] = ms.height;
+ }
if (c->get_h_size_flags() & SIZE_EXPAND) {
col_expanded.insert(col);
@@ -83,13 +83,15 @@ void GridContainer::_notification(int p_what) {
// Evaluate the remaining space for expanded columns/rows.
Size2 remaining_space = get_size();
for (Map<int, int>::Element *E = col_minw.front(); E; E = E->next()) {
- if (!col_expanded.has(E->key()))
+ if (!col_expanded.has(E->key())) {
remaining_space.width -= E->get();
+ }
}
for (Map<int, int>::Element *E = row_minh.front(); E; E = E->next()) {
- if (!row_expanded.has(E->key()))
+ if (!row_expanded.has(E->key())) {
remaining_space.height -= E->get();
+ }
}
remaining_space.height -= vsep * MAX(max_row - 1, 0);
remaining_space.width -= hsep * MAX(max_col - 1, 0);
@@ -139,6 +141,7 @@ void GridContainer::_notification(int p_what) {
// Finally, fit the nodes.
int col_expand = col_expanded.size() > 0 ? remaining_space.width / col_expanded.size() : 0;
int row_expand = row_expanded.size() > 0 ? remaining_space.height / row_expanded.size() : 0;
+ bool rtl = is_layout_rtl();
int col_ofs = 0;
int row_ofs = 0;
@@ -146,36 +149,49 @@ void GridContainer::_notification(int p_what) {
valid_controls_index = 0;
for (int i = 0; i < get_child_count(); i++) {
Control *c = Object::cast_to<Control>(get_child(i));
- if (!c || !c->is_visible_in_tree())
+ if (!c || !c->is_visible_in_tree()) {
continue;
+ }
int row = valid_controls_index / columns;
int col = valid_controls_index % columns;
valid_controls_index++;
if (col == 0) {
- col_ofs = 0;
- if (row > 0)
+ if (rtl) {
+ col_ofs = get_size().width;
+ } else {
+ col_ofs = 0;
+ }
+ if (row > 0) {
row_ofs += (row_expanded.has(row - 1) ? row_expand : row_minh[row - 1]) + vsep;
+ }
}
- Point2 p(col_ofs, row_ofs);
- Size2 s(col_expanded.has(col) ? col_expand : col_minw[col], row_expanded.has(row) ? row_expand : row_minh[row]);
-
- fit_child_in_rect(c, Rect2(p, s));
-
- col_ofs += s.width + hsep;
+ if (rtl) {
+ Size2 s(col_expanded.has(col) ? col_expand : col_minw[col], row_expanded.has(row) ? row_expand : row_minh[row]);
+ Point2 p(col_ofs - s.width, row_ofs);
+ fit_child_in_rect(c, Rect2(p, s));
+ col_ofs -= s.width + hsep;
+ } else {
+ Point2 p(col_ofs, row_ofs);
+ Size2 s(col_expanded.has(col) ? col_expand : col_minw[col], row_expanded.has(row) ? row_expand : row_minh[row]);
+ fit_child_in_rect(c, Rect2(p, s));
+ col_ofs += s.width + hsep;
+ }
}
} break;
case NOTIFICATION_THEME_CHANGED: {
-
minimum_size_changed();
} break;
+ case NOTIFICATION_TRANSLATION_CHANGED:
+ case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
+ queue_sort();
+ } break;
}
}
void GridContainer::set_columns(int p_columns) {
-
ERR_FAIL_COND(p_columns < 1);
columns = p_columns;
queue_sort();
@@ -183,12 +199,10 @@ void GridContainer::set_columns(int p_columns) {
}
int GridContainer::get_columns() const {
-
return columns;
}
void GridContainer::_bind_methods() {
-
ClassDB::bind_method(D_METHOD("set_columns", "columns"), &GridContainer::set_columns);
ClassDB::bind_method(D_METHOD("get_columns"), &GridContainer::get_columns);
@@ -196,36 +210,37 @@ void GridContainer::_bind_methods() {
}
Size2 GridContainer::get_minimum_size() const {
-
Map<int, int> col_minw;
Map<int, int> row_minh;
- int hsep = get_theme_constant("hseparation");
- int vsep = get_theme_constant("vseparation");
+ int hsep = get_theme_constant(SNAME("hseparation"));
+ int vsep = get_theme_constant(SNAME("vseparation"));
int max_row = 0;
int max_col = 0;
int valid_controls_index = 0;
for (int i = 0; i < get_child_count(); i++) {
-
Control *c = Object::cast_to<Control>(get_child(i));
- if (!c || !c->is_visible())
+ if (!c || !c->is_visible()) {
continue;
+ }
int row = valid_controls_index / columns;
int col = valid_controls_index % columns;
valid_controls_index++;
Size2i ms = c->get_combined_minimum_size();
- if (col_minw.has(col))
+ if (col_minw.has(col)) {
col_minw[col] = MAX(col_minw[col], ms.width);
- else
+ } else {
col_minw[col] = ms.width;
+ }
- if (row_minh.has(row))
+ if (row_minh.has(row)) {
row_minh[row] = MAX(row_minh[row], ms.height);
- else
+ } else {
row_minh[row] = ms.height;
+ }
max_col = MAX(col, max_col);
max_row = MAX(row, max_row);
}
@@ -246,6 +261,4 @@ Size2 GridContainer::get_minimum_size() const {
return ms;
}
-GridContainer::GridContainer() {
- columns = 1;
-}
+GridContainer::GridContainer() {}