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
|
|
|
/*************************************************************************/
|
2018-01-01 14:40:08 +01:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 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: {
|
|
|
|
|
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
|
|
|
Set<int> col_expanded;
|
|
|
|
Set<int> row_expanded;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int hsep = get_constant("hseparation");
|
|
|
|
int vsep = get_constant("vseparation");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int idx = 0;
|
|
|
|
int max_row = 0;
|
|
|
|
int max_col = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Size2 size = get_size();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-08-24 22:58:51 +02:00
|
|
|
Control *c = Object::cast_to<Control>(get_child(i));
|
2017-01-13 14:45:50 +01:00
|
|
|
if (!c || !c->is_visible_in_tree())
|
2014-02-10 02:10:30 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
int row = idx / columns;
|
|
|
|
int col = idx % columns;
|
|
|
|
|
|
|
|
Size2i ms = c->get_combined_minimum_size();
|
|
|
|
if (col_minw.has(col))
|
2017-03-05 16:44:50 +01:00
|
|
|
col_minw[col] = MAX(col_minw[col], ms.width);
|
2014-02-10 02:10:30 +01:00
|
|
|
else
|
2017-03-05 16:44:50 +01:00
|
|
|
col_minw[col] = ms.width;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (row_minh.has(row))
|
2017-03-05 16:44:50 +01:00
|
|
|
row_minh[row] = MAX(row_minh[row], ms.height);
|
2014-02-10 02:10:30 +01:00
|
|
|
else
|
2017-03-05 16:44:50 +01:00
|
|
|
row_minh[row] = ms.height;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-14 12:26:56 +01:00
|
|
|
//print_line("store row "+itos(row)+" mw "+itos(ms.height));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (c->get_h_size_flags() & SIZE_EXPAND)
|
2014-02-10 02:10:30 +01:00
|
|
|
col_expanded.insert(col);
|
2017-03-05 16:44:50 +01:00
|
|
|
if (c->get_v_size_flags() & SIZE_EXPAND)
|
2014-02-10 02:10:30 +01:00
|
|
|
row_expanded.insert(row);
|
|
|
|
|
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
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
Size2 ms;
|
2017-03-05 16:44:50 +01:00
|
|
|
int expand_rows = 0;
|
|
|
|
int expand_cols = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
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
|
|
|
if (col_expanded.has(E->key()))
|
|
|
|
expand_cols++;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
if (row_expanded.has(E->key()))
|
|
|
|
expand_rows++;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int row_expand = expand_rows ? (size.y - ms.y) / expand_rows : 0;
|
|
|
|
int col_expand = expand_cols ? (size.x - ms.x) / expand_cols : 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int col_ofs = 0;
|
|
|
|
int row_ofs = 0;
|
|
|
|
idx = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-08-24 22:58:51 +02:00
|
|
|
Control *c = Object::cast_to<Control>(get_child(i));
|
2017-01-13 14:45:50 +01:00
|
|
|
if (!c || !c->is_visible_in_tree())
|
2014-02-10 02:10:30 +01:00
|
|
|
continue;
|
|
|
|
int row = idx / columns;
|
|
|
|
int col = idx % columns;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (col == 0) {
|
|
|
|
col_ofs = 0;
|
|
|
|
if (row > 0 && row_minh.has(row - 1))
|
|
|
|
row_ofs += row_minh[row - 1] + vsep + (row_expanded.has(row - 1) ? row_expand : 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2015-04-29 04:10:06 +02:00
|
|
|
Size2 s;
|
|
|
|
if (col_minw.has(col))
|
2017-03-05 16:44:50 +01:00
|
|
|
s.width = col_minw[col];
|
2015-04-29 04:10:06 +02:00
|
|
|
if (row_minh.has(row))
|
2017-03-05 16:44:50 +01:00
|
|
|
s.height = row_minh[row];
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-04-29 04:10:06 +02:00
|
|
|
if (row_expanded.has(row))
|
2017-03-05 16:44:50 +01:00
|
|
|
s.height += row_expand;
|
2015-04-29 04:10:06 +02:00
|
|
|
if (col_expanded.has(col))
|
2017-03-05 16:44:50 +01:00
|
|
|
s.width += col_expand;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Point2 p(col_ofs, row_ofs);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-14 12:26:56 +01:00
|
|
|
//print_line("col: "+itos(col)+" row: "+itos(row)+" col_ofs: "+itos(col_ofs)+" row_ofs: "+itos(row_ofs));
|
2017-03-05 16:44:50 +01:00
|
|
|
fit_child_in_rect(c, Rect2(p, s));
|
2015-04-29 04:10:06 +02:00
|
|
|
//print_line("col: "+itos(col)+" row: "+itos(row)+" rect: "+Rect2(p,s));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (col_minw.has(col)) {
|
2017-03-05 16:44:50 +01:00
|
|
|
col_ofs += col_minw[col] + hsep + (col_expanded.has(col) ? col_expand : 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01: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 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
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int hsep = get_constant("hseparation");
|
|
|
|
int vsep = get_constant("vseparation");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int idx = 0;
|
|
|
|
int max_row = 0;
|
|
|
|
int max_col = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-08-24 22:58:51 +02:00
|
|
|
Control *c = Object::cast_to<Control>(get_child(i));
|
2017-01-13 14:45:50 +01:00
|
|
|
if (!c || !c->is_visible_in_tree())
|
2014-02-10 02:10:30 +01:00
|
|
|
continue;
|
|
|
|
int row = idx / columns;
|
|
|
|
int col = idx % columns;
|
|
|
|
Size2i ms = c->get_combined_minimum_size();
|
|
|
|
if (col_minw.has(col))
|
2017-03-05 16:44:50 +01:00
|
|
|
col_minw[col] = MAX(col_minw[col], ms.width);
|
2014-02-10 02:10:30 +01:00
|
|
|
else
|
2017-03-05 16:44:50 +01:00
|
|
|
col_minw[col] = ms.width;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (row_minh.has(row))
|
2017-03-05 16:44:50 +01:00
|
|
|
row_minh[row] = MAX(row_minh[row], ms.height);
|
2014-02-10 02:10:30 +01:00
|
|
|
else
|
2017-03-05 16:44:50 +01:00
|
|
|
row_minh[row] = ms.height;
|
|
|
|
max_col = MAX(col, max_col);
|
|
|
|
max_row = MAX(row, max_row);
|
2014-02-10 02:10:30 +01:00
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
GridContainer::GridContainer() {
|
|
|
|
|
2017-01-08 23:54:19 +01:00
|
|
|
set_mouse_filter(MOUSE_FILTER_PASS);
|
2017-03-05 16:44:50 +01:00
|
|
|
columns = 1;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|