2023-01-05 12:25:55 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* grid_container.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "grid_container.h"
|
2022-08-31 12:02:40 +00:00
|
|
|
|
2023-09-08 19:00:10 +00:00
|
|
|
#include "core/templates/rb_set.h"
|
|
|
|
#include "scene/theme/theme_db.h"
|
2022-08-31 12:02:40 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void GridContainer::_notification(int p_what) {
|
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_SORT_CHILDREN: {
|
2022-05-19 15:00:06 +00: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 13:04:37 +00: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 01:10:30 +00:00
|
|
|
|
2019-09-08 05:20:34 +00:00
|
|
|
// Compute the per-column/per-row data.
|
|
|
|
int valid_controls_index = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
2017-08-24 20:58:51 +00:00
|
|
|
Control *c = Object::cast_to<Control>(get_child(i));
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!c || !c->is_visible_in_tree()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2022-04-13 21:09:51 +00:00
|
|
|
if (c->is_set_as_top_level()) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-04-23 18:00:06 +00:00
|
|
|
int row = valid_controls_index / columns;
|
|
|
|
int col = valid_controls_index % columns;
|
|
|
|
valid_controls_index++;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Size2i ms = c->get_combined_minimum_size();
|
2020-05-14 14:41:43 +00:00
|
|
|
if (col_minw.has(col)) {
|
2014-02-10 01:10:30 +00:00
|
|
|
col_minw[col] = MAX(col_minw[col], ms.width);
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2014-02-10 01:10:30 +00:00
|
|
|
col_minw[col] = ms.width;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (row_minh.has(row)) {
|
2014-02-10 01:10:30 +00:00
|
|
|
row_minh[row] = MAX(row_minh[row], ms.height);
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2014-02-10 01:10:30 +00:00
|
|
|
row_minh[row] = ms.height;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2023-01-07 19:37:21 +00:00
|
|
|
if (c->get_h_size_flags().has_flag(SIZE_EXPAND)) {
|
2014-02-10 01:10:30 +00:00
|
|
|
col_expanded.insert(col);
|
2018-01-18 23:56:30 +00:00
|
|
|
}
|
2023-01-07 19:37:21 +00:00
|
|
|
if (c->get_v_size_flags().has_flag(SIZE_EXPAND)) {
|
2014-02-10 01:10:30 +00:00
|
|
|
row_expanded.insert(row);
|
2018-01-18 23:56:30 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2022-07-08 11:07:15 +00:00
|
|
|
int max_col = MIN(valid_controls_index, columns);
|
|
|
|
int max_row = ceil((float)valid_controls_index / (float)columns);
|
|
|
|
|
2019-09-08 05:20:34 +00: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-18 23:56:30 +00:00
|
|
|
Size2 remaining_space = get_size();
|
2021-08-09 20:13:42 +00:00
|
|
|
for (const KeyValue<int, int> &E : col_minw) {
|
|
|
|
if (!col_expanded.has(E.key)) {
|
|
|
|
remaining_space.width -= E.value;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2021-08-09 20:13:42 +00:00
|
|
|
for (const KeyValue<int, int> &E : row_minh) {
|
|
|
|
if (!row_expanded.has(E.key)) {
|
|
|
|
remaining_space.height -= E.value;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-01-18 23:56:30 +00:00
|
|
|
}
|
2022-08-31 12:02:40 +00: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-18 23:56:30 +00:00
|
|
|
|
|
|
|
bool can_fit = false;
|
2018-03-10 13:01:20 +00:00
|
|
|
while (!can_fit && col_expanded.size() > 0) {
|
2019-09-08 05:20:34 +00:00
|
|
|
// Check if all minwidth constraints are OK if we use the remaining space.
|
2018-01-18 23:56:30 +00:00
|
|
|
can_fit = true;
|
2018-03-10 13:01:20 +00:00
|
|
|
int max_index = col_expanded.front()->get();
|
2022-05-18 23:43:40 +00:00
|
|
|
for (const int &E : col_expanded) {
|
|
|
|
if (col_minw[E] > col_minw[max_index]) {
|
|
|
|
max_index = E;
|
2018-01-18 23:56:30 +00:00
|
|
|
}
|
2022-05-18 23:43:40 +00:00
|
|
|
if (can_fit && (remaining_space.width / col_expanded.size()) < col_minw[E]) {
|
2018-01-18 23:56:30 +00:00
|
|
|
can_fit = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 05:20:34 +00:00
|
|
|
// If not, the column with maximum minwidth is not expanded.
|
2018-01-18 23:56:30 +00:00
|
|
|
if (!can_fit) {
|
|
|
|
col_expanded.erase(max_index);
|
|
|
|
remaining_space.width -= col_minw[max_index];
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2018-01-18 23:56:30 +00:00
|
|
|
can_fit = false;
|
2018-03-10 13:01:20 +00:00
|
|
|
while (!can_fit && row_expanded.size() > 0) {
|
2019-09-08 05:20:34 +00:00
|
|
|
// Check if all minheight constraints are OK if we use the remaining space.
|
2018-01-18 23:56:30 +00:00
|
|
|
can_fit = true;
|
2018-03-10 13:01:20 +00:00
|
|
|
int max_index = row_expanded.front()->get();
|
2022-05-18 23:43:40 +00:00
|
|
|
for (const int &E : row_expanded) {
|
|
|
|
if (row_minh[E] > row_minh[max_index]) {
|
|
|
|
max_index = E;
|
2018-01-18 23:56:30 +00:00
|
|
|
}
|
2022-05-18 23:43:40 +00:00
|
|
|
if (can_fit && (remaining_space.height / row_expanded.size()) < row_minh[E]) {
|
2018-01-18 23:56:30 +00:00
|
|
|
can_fit = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 05:20:34 +00:00
|
|
|
// If not, the row with maximum minheight is not expanded.
|
2018-01-18 23:56:30 +00:00
|
|
|
if (!can_fit) {
|
|
|
|
row_expanded.erase(max_index);
|
|
|
|
remaining_space.height -= row_minh[max_index];
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-09-08 05:20:34 +00:00
|
|
|
// Finally, fit the nodes.
|
2022-04-01 16:46:52 +00: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 11:22:16 +00:00
|
|
|
bool rtl = is_layout_rtl();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
int col_ofs = 0;
|
|
|
|
int row_ofs = 0;
|
|
|
|
|
2022-04-01 16:46:52 +00: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 18:00:06 +00:00
|
|
|
valid_controls_index = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
2017-08-24 20:58:51 +00:00
|
|
|
Control *c = Object::cast_to<Control>(get_child(i));
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!c || !c->is_visible_in_tree()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-04-23 18:00:06 +00:00
|
|
|
int row = valid_controls_index / columns;
|
|
|
|
int col = valid_controls_index % columns;
|
|
|
|
valid_controls_index++;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (col == 0) {
|
2020-09-03 11:22:16 +00:00
|
|
|
if (rtl) {
|
|
|
|
col_ofs = get_size().width;
|
|
|
|
} else {
|
|
|
|
col_ofs = 0;
|
|
|
|
}
|
2020-05-14 14:41:43 +00:00
|
|
|
if (row > 0) {
|
2022-08-31 12:02:40 +00:00
|
|
|
row_ofs += (row_expanded.has(row - 1) ? row_expand : row_minh[row - 1]) + theme_cache.v_separation;
|
2022-04-01 16:46:52 +00: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 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 16:46:52 +00: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 11:22:16 +00:00
|
|
|
if (rtl) {
|
|
|
|
Point2 p(col_ofs - s.width, row_ofs);
|
|
|
|
fit_child_in_rect(c, Rect2(p, s));
|
2022-08-31 12:02:40 +00:00
|
|
|
col_ofs -= s.width + theme_cache.h_separation;
|
2020-09-03 11:22:16 +00:00
|
|
|
} else {
|
|
|
|
Point2 p(col_ofs, row_ofs);
|
|
|
|
fit_child_in_rect(c, Rect2(p, s));
|
2022-08-31 12:02:40 +00:00
|
|
|
col_ofs += s.width + theme_cache.h_separation;
|
2020-09-03 11:22:16 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
} break;
|
2022-02-15 17:06:48 +00:00
|
|
|
|
2019-01-24 21:31:33 +00:00
|
|
|
case NOTIFICATION_THEME_CHANGED: {
|
2021-12-06 13:02:34 +00:00
|
|
|
update_minimum_size();
|
2019-01-24 21:31:33 +00:00
|
|
|
} break;
|
2022-02-15 17:06:48 +00:00
|
|
|
|
2020-09-03 11:22:16 +00:00
|
|
|
case NOTIFICATION_TRANSLATION_CHANGED:
|
|
|
|
case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
|
|
|
|
queue_sort();
|
|
|
|
} break;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GridContainer::set_columns(int p_columns) {
|
2016-06-30 21:54:54 +00:00
|
|
|
ERR_FAIL_COND(p_columns < 1);
|
2022-03-16 07:50:48 +00:00
|
|
|
|
|
|
|
if (columns == p_columns) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
columns = p_columns;
|
|
|
|
queue_sort();
|
2021-12-06 13:02:34 +00:00
|
|
|
update_minimum_size();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int GridContainer::get_columns() const {
|
|
|
|
return columns;
|
|
|
|
}
|
|
|
|
|
2023-09-12 13:01:42 +00:00
|
|
|
int GridContainer::get_h_separation() const {
|
|
|
|
return theme_cache.h_separation;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void GridContainer::_bind_methods() {
|
2017-02-13 11:47:24 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_columns", "columns"), &GridContainer::set_columns);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_columns"), &GridContainer::get_columns);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-02-12 00:11:37 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "columns", PROPERTY_HINT_RANGE, "1,1024,1"), "set_columns", "get_columns");
|
2023-09-08 19:00:10 +00:00
|
|
|
|
|
|
|
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, GridContainer, h_separation);
|
|
|
|
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, GridContainer, v_separation);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Size2 GridContainer::get_minimum_size() const {
|
2022-05-19 15:00:06 +00:00
|
|
|
RBMap<int, int> col_minw;
|
|
|
|
RBMap<int, int> row_minh;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
int max_row = 0;
|
|
|
|
int max_col = 0;
|
|
|
|
|
2018-04-23 18:00:06 +00:00
|
|
|
int valid_controls_index = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
2017-08-24 20:58:51 +00:00
|
|
|
Control *c = Object::cast_to<Control>(get_child(i));
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!c || !c->is_visible()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-04-23 18:00:06 +00:00
|
|
|
int row = valid_controls_index / columns;
|
|
|
|
int col = valid_controls_index % columns;
|
|
|
|
valid_controls_index++;
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
Size2i ms = c->get_combined_minimum_size();
|
2020-05-14 14:41:43 +00:00
|
|
|
if (col_minw.has(col)) {
|
2014-02-10 01:10:30 +00:00
|
|
|
col_minw[col] = MAX(col_minw[col], ms.width);
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2014-02-10 01:10:30 +00:00
|
|
|
col_minw[col] = ms.width;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (row_minh.has(row)) {
|
2014-02-10 01:10:30 +00:00
|
|
|
row_minh[row] = MAX(row_minh[row], ms.height);
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2014-02-10 01:10:30 +00:00
|
|
|
row_minh[row] = ms.height;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
max_col = MAX(col, max_col);
|
|
|
|
max_row = MAX(row, max_row);
|
|
|
|
}
|
|
|
|
|
|
|
|
Size2 ms;
|
|
|
|
|
2021-08-09 20:13:42 +00:00
|
|
|
for (const KeyValue<int, int> &E : col_minw) {
|
|
|
|
ms.width += E.value;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2021-08-09 20:13:42 +00:00
|
|
|
for (const KeyValue<int, int> &E : row_minh) {
|
|
|
|
ms.height += E.value;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 12:02:40 +00:00
|
|
|
ms.height += theme_cache.v_separation * max_row;
|
|
|
|
ms.width += theme_cache.h_separation * max_col;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return ms;
|
|
|
|
}
|
|
|
|
|
2021-02-09 17:24:36 +00:00
|
|
|
GridContainer::GridContainer() {}
|