2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* grid_container.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2022-01-03 21:27:34 +01:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "grid_container.h"
|
2022-05-19 17:00:06 +02:00
|
|
|
#include "core/templates/rb_set.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-08-31 14:02:40 +02:00
|
|
|
void GridContainer::_update_theme_item_cache() {
|
|
|
|
Container::_update_theme_item_cache();
|
|
|
|
|
|
|
|
theme_cache.h_separation = get_theme_constant(SNAME("h_separation"));
|
|
|
|
theme_cache.v_separation = get_theme_constant(SNAME("v_separation"));
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void GridContainer::_notification(int p_what) {
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (p_what) {
|
2014-02-10 02:10:30 +01:00
|
|
|
case NOTIFICATION_SORT_CHILDREN: {
|
2022-05-19 17:00:06 +02:00
|
|
|
RBMap<int, int> col_minw; // Max of min_width of all controls in each col (indexed by col).
|
|
|
|
RBMap<int, int> row_minh; // Max of min_height of all controls in each row (indexed by row).
|
2022-05-13 15:04:37 +02:00
|
|
|
RBSet<int> col_expanded; // Columns which have the SIZE_EXPAND flag set.
|
|
|
|
RBSet<int> row_expanded; // Rows which have the SIZE_EXPAND flag set.
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-09-08 07:20:34 +02:00
|
|
|
// Compute the per-column/per-row data.
|
|
|
|
int valid_controls_index = 0;
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
2017-08-24 22:58:51 +02:00
|
|
|
Control *c = Object::cast_to<Control>(get_child(i));
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!c || !c->is_visible_in_tree()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
continue;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2022-04-13 23:09:51 +02:00
|
|
|
if (c->is_set_as_top_level()) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-04-23 20:00:06 +02:00
|
|
|
int row = valid_controls_index / columns;
|
|
|
|
int col = valid_controls_index % columns;
|
|
|
|
valid_controls_index++;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Size2i ms = c->get_combined_minimum_size();
|
2020-05-14 16:41:43 +02:00
|
|
|
if (col_minw.has(col)) {
|
2017-03-05 16:44:50 +01:00
|
|
|
col_minw[col] = MAX(col_minw[col], ms.width);
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
col_minw[col] = ms.width;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (row_minh.has(row)) {
|
2017-03-05 16:44:50 +01:00
|
|
|
row_minh[row] = MAX(row_minh[row], ms.height);
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
row_minh[row] = ms.height;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-01-19 00:56:30 +01:00
|
|
|
if (c->get_h_size_flags() & SIZE_EXPAND) {
|
2014-02-10 02:10:30 +01:00
|
|
|
col_expanded.insert(col);
|
2018-01-19 00:56:30 +01:00
|
|
|
}
|
|
|
|
if (c->get_v_size_flags() & SIZE_EXPAND) {
|
2014-02-10 02:10:30 +01:00
|
|
|
row_expanded.insert(row);
|
2018-01-19 00:56:30 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2022-07-08 13:07:15 +02:00
|
|
|
int max_col = MIN(valid_controls_index, columns);
|
|
|
|
int max_row = ceil((float)valid_controls_index / (float)columns);
|
|
|
|
|
2019-09-08 07:20:34 +02:00
|
|
|
// Consider all empty columns expanded.
|
|
|
|
for (int i = valid_controls_index; i < columns; i++) {
|
|
|
|
col_expanded.insert(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Evaluate the remaining space for expanded columns/rows.
|
2018-01-19 00:56:30 +01:00
|
|
|
Size2 remaining_space = get_size();
|
2021-08-09 22:13:42 +02:00
|
|
|
for (const KeyValue<int, int> &E : col_minw) {
|
|
|
|
if (!col_expanded.has(E.key)) {
|
|
|
|
remaining_space.width -= E.value;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2021-08-09 22:13:42 +02:00
|
|
|
for (const KeyValue<int, int> &E : row_minh) {
|
|
|
|
if (!row_expanded.has(E.key)) {
|
|
|
|
remaining_space.height -= E.value;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-01-19 00:56:30 +01:00
|
|
|
}
|
2022-08-31 14:02:40 +02:00
|
|
|
remaining_space.height -= theme_cache.v_separation * MAX(max_row - 1, 0);
|
|
|
|
remaining_space.width -= theme_cache.h_separation * MAX(max_col - 1, 0);
|
2018-01-19 00:56:30 +01:00
|
|
|
|
|
|
|
bool can_fit = false;
|
2018-03-10 14:01:20 +01:00
|
|
|
while (!can_fit && col_expanded.size() > 0) {
|
2019-09-08 07:20:34 +02:00
|
|
|
// Check if all minwidth constraints are OK if we use the remaining space.
|
2018-01-19 00:56:30 +01:00
|
|
|
can_fit = true;
|
2018-03-10 14:01:20 +01:00
|
|
|
int max_index = col_expanded.front()->get();
|
2022-05-19 01:43:40 +02:00
|
|
|
for (const int &E : col_expanded) {
|
|
|
|
if (col_minw[E] > col_minw[max_index]) {
|
|
|
|
max_index = E;
|
2018-01-19 00:56:30 +01:00
|
|
|
}
|
2022-05-19 01:43:40 +02:00
|
|
|
if (can_fit && (remaining_space.width / col_expanded.size()) < col_minw[E]) {
|
2018-01-19 00:56:30 +01:00
|
|
|
can_fit = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 07:20:34 +02:00
|
|
|
// If not, the column with maximum minwidth is not expanded.
|
2018-01-19 00:56:30 +01:00
|
|
|
if (!can_fit) {
|
|
|
|
col_expanded.erase(max_index);
|
|
|
|
remaining_space.width -= col_minw[max_index];
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2018-01-19 00:56:30 +01:00
|
|
|
can_fit = false;
|
2018-03-10 14:01:20 +01:00
|
|
|
while (!can_fit && row_expanded.size() > 0) {
|
2019-09-08 07:20:34 +02:00
|
|
|
// Check if all minheight constraints are OK if we use the remaining space.
|
2018-01-19 00:56:30 +01:00
|
|
|
can_fit = true;
|
2018-03-10 14:01:20 +01:00
|
|
|
int max_index = row_expanded.front()->get();
|
2022-05-19 01:43:40 +02:00
|
|
|
for (const int &E : row_expanded) {
|
|
|
|
if (row_minh[E] > row_minh[max_index]) {
|
|
|
|
max_index = E;
|
2018-01-19 00:56:30 +01:00
|
|
|
}
|
2022-05-19 01:43:40 +02:00
|
|
|
if (can_fit && (remaining_space.height / row_expanded.size()) < row_minh[E]) {
|
2018-01-19 00:56:30 +01:00
|
|
|
can_fit = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 07:20:34 +02:00
|
|
|
// If not, the row with maximum minheight is not expanded.
|
2018-01-19 00:56:30 +01:00
|
|
|
if (!can_fit) {
|
|
|
|
row_expanded.erase(max_index);
|
|
|
|
remaining_space.height -= row_minh[max_index];
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-09-08 07:20:34 +02:00
|
|
|
// Finally, fit the nodes.
|
2022-04-01 18:46:52 +02:00
|
|
|
int col_remaining_pixel = 0;
|
|
|
|
int col_expand = 0;
|
|
|
|
if (col_expanded.size() > 0) {
|
|
|
|
col_expand = remaining_space.width / col_expanded.size();
|
|
|
|
col_remaining_pixel = remaining_space.width - col_expanded.size() * col_expand;
|
|
|
|
}
|
|
|
|
|
|
|
|
int row_remaining_pixel = 0;
|
|
|
|
int row_expand = 0;
|
|
|
|
if (row_expanded.size() > 0) {
|
|
|
|
row_expand = remaining_space.height / row_expanded.size();
|
|
|
|
row_remaining_pixel = remaining_space.height - row_expanded.size() * row_expand;
|
|
|
|
}
|
|
|
|
|
2020-09-03 13:22:16 +02:00
|
|
|
bool rtl = is_layout_rtl();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int col_ofs = 0;
|
|
|
|
int row_ofs = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-04-01 18:46:52 +02:00
|
|
|
// Calculate the index of rows and columns that receive the remaining pixel.
|
|
|
|
int col_remaining_pixel_index = 0;
|
|
|
|
for (int i = 0; i < max_col; i++) {
|
|
|
|
if (col_remaining_pixel == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (col_expanded.has(i)) {
|
|
|
|
col_remaining_pixel_index = i + 1;
|
|
|
|
col_remaining_pixel--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int row_remaining_pixel_index = 0;
|
|
|
|
for (int i = 0; i < max_row; i++) {
|
|
|
|
if (row_remaining_pixel == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (row_expanded.has(i)) {
|
|
|
|
row_remaining_pixel_index = i + 1;
|
|
|
|
row_remaining_pixel--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-23 20:00:06 +02:00
|
|
|
valid_controls_index = 0;
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
2017-08-24 22:58:51 +02:00
|
|
|
Control *c = Object::cast_to<Control>(get_child(i));
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!c || !c->is_visible_in_tree()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
continue;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-04-23 20:00:06 +02:00
|
|
|
int row = valid_controls_index / columns;
|
|
|
|
int col = valid_controls_index % columns;
|
|
|
|
valid_controls_index++;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (col == 0) {
|
2020-09-03 13:22:16 +02:00
|
|
|
if (rtl) {
|
|
|
|
col_ofs = get_size().width;
|
|
|
|
} else {
|
|
|
|
col_ofs = 0;
|
|
|
|
}
|
2020-05-14 16:41:43 +02:00
|
|
|
if (row > 0) {
|
2022-08-31 14:02:40 +02:00
|
|
|
row_ofs += (row_expanded.has(row - 1) ? row_expand : row_minh[row - 1]) + theme_cache.v_separation;
|
2022-04-01 18:46:52 +02:00
|
|
|
|
|
|
|
if (row_expanded.has(row - 1) && row - 1 < row_remaining_pixel_index) {
|
|
|
|
// Apply the remaining pixel of the previous row.
|
|
|
|
row_ofs++;
|
|
|
|
}
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2022-04-01 18:46:52 +02:00
|
|
|
Size2 s(col_expanded.has(col) ? col_expand : col_minw[col], row_expanded.has(row) ? row_expand : row_minh[row]);
|
|
|
|
|
|
|
|
// Add the remaining pixel to the expanding columns and rows, starting from left and top.
|
|
|
|
if (col_expanded.has(col) && col < col_remaining_pixel_index) {
|
|
|
|
s.x++;
|
|
|
|
}
|
|
|
|
if (row_expanded.has(row) && row < row_remaining_pixel_index) {
|
|
|
|
s.y++;
|
|
|
|
}
|
|
|
|
|
2020-09-03 13:22:16 +02:00
|
|
|
if (rtl) {
|
|
|
|
Point2 p(col_ofs - s.width, row_ofs);
|
|
|
|
fit_child_in_rect(c, Rect2(p, s));
|
2022-08-31 14:02:40 +02:00
|
|
|
col_ofs -= s.width + theme_cache.h_separation;
|
2020-09-03 13:22:16 +02:00
|
|
|
} else {
|
|
|
|
Point2 p(col_ofs, row_ofs);
|
|
|
|
fit_child_in_rect(c, Rect2(p, s));
|
2022-08-31 14:02:40 +02:00
|
|
|
col_ofs += s.width + theme_cache.h_separation;
|
2020-09-03 13:22:16 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
} break;
|
2022-02-15 18:06:48 +01:00
|
|
|
|
2019-01-24 22:31:33 +01:00
|
|
|
case NOTIFICATION_THEME_CHANGED: {
|
2021-12-06 14:02:34 +01:00
|
|
|
update_minimum_size();
|
2019-01-24 22:31:33 +01:00
|
|
|
} break;
|
2022-02-15 18:06:48 +01:00
|
|
|
|
2020-09-03 13:22:16 +02:00
|
|
|
case NOTIFICATION_TRANSLATION_CHANGED:
|
|
|
|
case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
|
|
|
|
queue_sort();
|
|
|
|
} break;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GridContainer::set_columns(int p_columns) {
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND(p_columns < 1);
|
2022-03-16 08:50:48 +01:00
|
|
|
|
|
|
|
if (columns == p_columns) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
columns = p_columns;
|
2014-02-10 02:10:30 +01:00
|
|
|
queue_sort();
|
2021-12-06 14:02:34 +01:00
|
|
|
update_minimum_size();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int GridContainer::get_columns() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return columns;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
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);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "columns", PROPERTY_HINT_RANGE, "1,1024,1"), "set_columns", "get_columns");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Size2 GridContainer::get_minimum_size() const {
|
2022-05-19 17:00:06 +02:00
|
|
|
RBMap<int, int> col_minw;
|
|
|
|
RBMap<int, int> row_minh;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int max_row = 0;
|
|
|
|
int max_col = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-04-23 20:00:06 +02:00
|
|
|
int valid_controls_index = 0;
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
2017-08-24 22:58:51 +02:00
|
|
|
Control *c = Object::cast_to<Control>(get_child(i));
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!c || !c->is_visible()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
continue;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-04-23 20:00:06 +02:00
|
|
|
int row = valid_controls_index / columns;
|
|
|
|
int col = valid_controls_index % columns;
|
|
|
|
valid_controls_index++;
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
Size2i ms = c->get_combined_minimum_size();
|
2020-05-14 16:41:43 +02:00
|
|
|
if (col_minw.has(col)) {
|
2017-03-05 16:44:50 +01:00
|
|
|
col_minw[col] = MAX(col_minw[col], ms.width);
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
col_minw[col] = ms.width;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (row_minh.has(row)) {
|
2017-03-05 16:44:50 +01:00
|
|
|
row_minh[row] = MAX(row_minh[row], ms.height);
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
row_minh[row] = ms.height;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
max_col = MAX(col, max_col);
|
|
|
|
max_row = MAX(row, max_row);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Size2 ms;
|
|
|
|
|
2021-08-09 22:13:42 +02:00
|
|
|
for (const KeyValue<int, int> &E : col_minw) {
|
|
|
|
ms.width += E.value;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2021-08-09 22:13:42 +02:00
|
|
|
for (const KeyValue<int, int> &E : row_minh) {
|
|
|
|
ms.height += E.value;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2022-08-31 14:02:40 +02:00
|
|
|
ms.height += theme_cache.v_separation * max_row;
|
|
|
|
ms.width += theme_cache.h_separation * max_col;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return ms;
|
|
|
|
}
|
|
|
|
|
2021-02-09 18:24:36 +01:00
|
|
|
GridContainer::GridContainer() {}
|