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
|
|
|
/*************************************************************************/
|
2021-01-01 20:13:46 +01:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 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"
|
|
|
|
|
|
|
|
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: {
|
2021-06-07 10:17:32 +02:00
|
|
|
Map<int, int> col_minw; // Max of min_width of all controls in each col (indexed by col).
|
2019-09-08 07:20:34 +02:00
|
|
|
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.
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-07-17 23:22:52 +02:00
|
|
|
int hsep = get_theme_constant(SNAME("hseparation"));
|
|
|
|
int vsep = get_theme_constant(SNAME("vseparation"));
|
2018-01-19 00:56:30 +01:00
|
|
|
int max_col = MIN(get_child_count(), columns);
|
2019-09-28 07:57:11 +02:00
|
|
|
int max_row = ceil((float)get_child_count() / (float)columns);
|
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
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
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();
|
2017-03-05 16:44:50 +01:00
|
|
|
for (Map<int, int>::Element *E = col_minw.front(); E; E = E->next()) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!col_expanded.has(E->key())) {
|
2018-01-19 00:56:30 +01:00
|
|
|
remaining_space.width -= E->get();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (Map<int, int>::Element *E = row_minh.front(); E; E = E->next()) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!row_expanded.has(E->key())) {
|
2018-01-19 00:56:30 +01:00
|
|
|
remaining_space.height -= E->get();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-01-19 00:56:30 +01:00
|
|
|
}
|
2018-03-18 22:33:17 +01:00
|
|
|
remaining_space.height -= vsep * MAX(max_row - 1, 0);
|
|
|
|
remaining_space.width -= hsep * 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();
|
2018-01-19 00:56:30 +01:00
|
|
|
for (Set<int>::Element *E = col_expanded.front(); E; E = E->next()) {
|
|
|
|
if (col_minw[E->get()] > col_minw[max_index]) {
|
2018-03-02 20:32:22 +01:00
|
|
|
max_index = E->get();
|
2018-01-19 00:56:30 +01:00
|
|
|
}
|
|
|
|
if (can_fit && (remaining_space.width / col_expanded.size()) < col_minw[E->get()]) {
|
|
|
|
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();
|
2018-01-19 00:56:30 +01:00
|
|
|
for (Set<int>::Element *E = row_expanded.front(); E; E = E->next()) {
|
|
|
|
if (row_minh[E->get()] > row_minh[max_index]) {
|
2018-03-02 20:32:22 +01:00
|
|
|
max_index = E->get();
|
2018-01-19 00:56:30 +01:00
|
|
|
}
|
|
|
|
if (can_fit && (remaining_space.height / row_expanded.size()) < row_minh[E->get()]) {
|
|
|
|
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.
|
2018-03-07 17:31:30 +01:00
|
|
|
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;
|
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
|
|
|
|
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) {
|
2019-09-08 07:20:34 +02:00
|
|
|
row_ofs += (row_expanded.has(row - 1) ? row_expand : row_minh[row - 1]) + vsep;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-09-03 13:22:16 +02:00
|
|
|
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;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
2019-01-24 22:31:33 +01:00
|
|
|
case NOTIFICATION_THEME_CHANGED: {
|
|
|
|
minimum_size_changed();
|
|
|
|
} break;
|
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);
|
|
|
|
columns = p_columns;
|
2014-02-10 02:10:30 +01:00
|
|
|
queue_sort();
|
2016-06-17 21:40:40 +02:00
|
|
|
minimum_size_changed();
|
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 {
|
2017-03-05 16:44:50 +01:00
|
|
|
Map<int, int> col_minw;
|
|
|
|
Map<int, int> row_minh;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-07-17 23:22:52 +02:00
|
|
|
int hsep = get_theme_constant(SNAME("hseparation"));
|
|
|
|
int vsep = get_theme_constant(SNAME("vseparation"));
|
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;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (Map<int, int>::Element *E = col_minw.front(); E; E = E->next()) {
|
|
|
|
ms.width += E->get();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (Map<int, int>::Element *E = row_minh.front(); E; E = E->next()) {
|
|
|
|
ms.height += E->get();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ms.height += vsep * max_row;
|
|
|
|
ms.width += hsep * max_col;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return ms;
|
|
|
|
}
|
|
|
|
|
2021-02-09 18:24:36 +01:00
|
|
|
GridContainer::GridContainer() {}
|