Added a new CBoxContainer type which acts as both a VBoxContainer and HBoxContainer and allows changing between them using a property.

This commit is contained in:
Relintai 2023-10-20 13:44:43 +02:00
parent 4cc096a002
commit e6ee1f0c0f
5 changed files with 249 additions and 90 deletions

View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="CBoxContainer" inherits="BoxContainer" version="4.2">
<brief_description>
Horizontal or vertical box container.
</brief_description>
<description>
Horizontal or vertical box container depending on it's [member mode] setting. See [BoxContainer].
</description>
<tutorials>
<link title="GUI containers">$DOCS_URL/tutorials/ui/gui_containers.md</link>
</tutorials>
<methods>
</methods>
<members>
<member name="mode" type="int" setter="set_mode" getter="get_mode" enum="CBoxContainer.ContainerMode" default="0">
Sets whether this [CBoxContainer] will act as a [HBoxContainer] or a [VBoxContainer].
</member>
</members>
<constants>
<constant name="CONTAINER_MODE_HORIZONTAL" value="0" enum="ContainerMode">
This [CBoxContainer] will act as a [HBoxContainer].
</constant>
<constant name="CONTAINER_MODE_VERTICAL" value="1" enum="ContainerMode">
This [CBoxContainer] will act as a [VBoxContainer].
</constant>
</constants>
<theme_items>
<theme_item name="separation" data_type="constant" type="int" default="4">
The horizontal or vertical space between the [CBoxContainer]'s elements.
</theme_item>
</theme_items>
</class>

View File

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
height="16"
viewBox="0 0 16 16"
width="16"
version="1.1"
id="svg1"
sodipodi:docname="icon_c_box_container.svg"
inkscape:version="1.3 (0e150ed6c4, 2023-07-21)"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg">
<defs
id="defs1" />
<sodipodi:namedview
id="namedview1"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:showpageshadow="2"
inkscape:pageopacity="0.0"
inkscape:pagecheckerboard="0"
inkscape:deskcolor="#d1d1d1"
inkscape:zoom="51.1875"
inkscape:cx="7.990232"
inkscape:cy="8"
inkscape:window-width="1920"
inkscape:window-height="1023"
inkscape:window-x="3840"
inkscape:window-y="0"
inkscape:window-maximized="1"
inkscape:current-layer="svg1" />
<path
id="path1"
d="M 3 1 C 1.8954011 1 1 1.8954311 1 3 L 1 13 C 1 14.104599 1.8954311 15 3 15 L 7.9003906 15 L 7.9003906 13 L 7 13 L 7 3 L 7.9003906 3 L 7.9003906 1 L 3 1 z M 3 3 L 5 3 L 5 13 L 3 13 L 3 3 z "
style="fill:#a5efac;fill-opacity:1" />
<path
id="path1-5"
d="M 8.2636719 1 L 8.2636719 3 L 13 3 L 13 5 L 8.2636719 5 L 8.2636719 7 L 13 7 L 13 9 L 8.2636719 9 L 8.2636719 11 L 13 11 L 13 13 L 8.2636719 13 L 8.2636719 15 L 13 15 C 14.104599 15 15 14.104599 15 13 L 15 3 C 15 1.8954011 14.104569 1 13 1 L 8.2636719 1 z "
style="fill:#a5efac;fill-opacity:1" />
</svg>

After

Width:  |  Height:  |  Size: 1.6 KiB

View File

@ -32,6 +32,116 @@
#include "label.h"
#include "margin_container.h"
Size2 BoxContainer::get_minimum_size() const {
/* Calculate MINIMUM SIZE */
Size2i minimum;
int sep = get_theme_constant("separation"); //,vertical?"VBoxContainer":"HBoxContainer");
bool first = true;
for (int i = 0; i < get_child_count(); i++) {
Control *c = Object::cast_to<Control>(get_child(i));
if (!c) {
continue;
}
if (c->is_set_as_toplevel()) {
continue;
}
if (!c->is_visible()) {
continue;
}
Size2i size = c->get_combined_minimum_size();
if (vertical) { /* VERTICAL */
if (size.width > minimum.width) {
minimum.width = size.width;
}
minimum.height += size.height + (first ? 0 : sep);
} else { /* HORIZONTAL */
if (size.height > minimum.height) {
minimum.height = size.height;
}
minimum.width += size.width + (first ? 0 : sep);
}
first = false;
}
return minimum;
}
void BoxContainer::set_alignment(AlignMode p_align) {
align = p_align;
_resort();
}
BoxContainer::AlignMode BoxContainer::get_alignment() const {
return align;
}
void BoxContainer::add_spacer(bool p_begin) {
Control *c = memnew(Control);
c->set_mouse_filter(MOUSE_FILTER_PASS); //allow spacer to pass mouse events
c->set_meta("is_box_container_spacer", true);
if (vertical) {
c->set_v_size_flags(SIZE_EXPAND_FILL);
} else {
c->set_h_size_flags(SIZE_EXPAND_FILL);
}
add_child(c);
if (p_begin) {
move_child(c, 0);
}
}
BoxContainer::BoxContainer(bool p_vertical) {
vertical = p_vertical;
align = ALIGN_BEGIN;
//set_ignore_mouse(true);
set_mouse_filter(MOUSE_FILTER_PASS);
}
bool BoxContainer::_get_vertical() const {
return vertical;
}
void BoxContainer::_set_vertical(bool p_vertical) {
if (vertical == p_vertical) {
return;
}
vertical = p_vertical;
int cc = get_child_count();
for (int i = 0; i < cc; ++i) {
Control *c = Object::cast_to<Control>(get_child(i));
if (c) {
if (c->has_meta("is_box_container_spacer")) {
if (vertical) {
c->set_v_size_flags(SIZE_EXPAND_FILL);
c->set_h_size_flags(SIZE_FILL);
} else {
c->set_v_size_flags(SIZE_FILL);
c->set_h_size_flags(SIZE_EXPAND_FILL);
}
}
}
}
_resort();
}
void BoxContainer::_resort() {
/** First pass, determine minimum size AND amount of stretchable elements */
@ -205,52 +315,6 @@ void BoxContainer::_resort() {
}
}
Size2 BoxContainer::get_minimum_size() const {
/* Calculate MINIMUM SIZE */
Size2i minimum;
int sep = get_theme_constant("separation"); //,vertical?"VBoxContainer":"HBoxContainer");
bool first = true;
for (int i = 0; i < get_child_count(); i++) {
Control *c = Object::cast_to<Control>(get_child(i));
if (!c) {
continue;
}
if (c->is_set_as_toplevel()) {
continue;
}
if (!c->is_visible()) {
continue;
}
Size2i size = c->get_combined_minimum_size();
if (vertical) { /* VERTICAL */
if (size.width > minimum.width) {
minimum.width = size.width;
}
minimum.height += size.height + (first ? 0 : sep);
} else { /* HORIZONTAL */
if (size.height > minimum.height) {
minimum.height = size.height;
}
minimum.width += size.width + (first ? 0 : sep);
}
first = false;
}
return minimum;
}
void BoxContainer::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_SORT_CHILDREN: {
@ -262,38 +326,6 @@ void BoxContainer::_notification(int p_what) {
}
}
void BoxContainer::set_alignment(AlignMode p_align) {
align = p_align;
_resort();
}
BoxContainer::AlignMode BoxContainer::get_alignment() const {
return align;
}
void BoxContainer::add_spacer(bool p_begin) {
Control *c = memnew(Control);
c->set_mouse_filter(MOUSE_FILTER_PASS); //allow spacer to pass mouse events
if (vertical) {
c->set_v_size_flags(SIZE_EXPAND_FILL);
} else {
c->set_h_size_flags(SIZE_EXPAND_FILL);
}
add_child(c);
if (p_begin) {
move_child(c, 0);
}
}
BoxContainer::BoxContainer(bool p_vertical) {
vertical = p_vertical;
align = ALIGN_BEGIN;
//set_ignore_mouse(true);
set_mouse_filter(MOUSE_FILTER_PASS);
}
void BoxContainer::_bind_methods() {
ClassDB::bind_method(D_METHOD("add_spacer", "begin"), &BoxContainer::add_spacer);
ClassDB::bind_method(D_METHOD("get_alignment"), &BoxContainer::get_alignment);
@ -320,3 +352,31 @@ MarginContainer *VBoxContainer::add_margin_child(const String &p_label, Control
return mc;
}
CBoxContainer::ContainerMode CBoxContainer::get_mode() const {
return _mode;
}
void CBoxContainer::set_mode(const ContainerMode p_mode) {
if (p_mode == _mode) {
return;
}
_mode = p_mode;
_set_vertical(_mode == CONTAINER_MODE_VERTICAL);
}
CBoxContainer::CBoxContainer() {
_mode = CONTAINER_MODE_HORIZONTAL;
}
CBoxContainer::~CBoxContainer() {
}
void CBoxContainer::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_mode"), &CBoxContainer::get_mode);
ClassDB::bind_method(D_METHOD("set_mode", "mode"), &CBoxContainer::set_mode);
ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Horizontal,Vertical"), "set_mode", "get_mode");
BIND_ENUM_CONSTANT(CONTAINER_MODE_HORIZONTAL);
BIND_ENUM_CONSTANT(CONTAINER_MODE_VERTICAL);
}

View File

@ -48,18 +48,6 @@ public:
ALIGN_END
};
private:
bool vertical;
AlignMode align;
void _resort();
protected:
void _notification(int p_what);
static void _bind_methods();
public:
void add_spacer(bool p_begin = false);
void set_alignment(AlignMode p_align);
@ -68,6 +56,20 @@ public:
virtual Size2 get_minimum_size() const;
BoxContainer(bool p_vertical = false);
protected:
bool _get_vertical() const;
void _set_vertical(bool p_vertical);
void _resort();
void _notification(int p_what);
static void _bind_methods();
private:
bool vertical;
AlignMode align;
};
class HBoxContainer : public BoxContainer {
@ -89,6 +91,28 @@ public:
BoxContainer(true) {}
};
class CBoxContainer : public BoxContainer {
GDCLASS(CBoxContainer, BoxContainer);
public:
enum ContainerMode {
CONTAINER_MODE_HORIZONTAL = 0,
CONTAINER_MODE_VERTICAL,
};
ContainerMode get_mode() const;
void set_mode(const ContainerMode p_mode);
CBoxContainer();
~CBoxContainer();
protected:
static void _bind_methods();
ContainerMode _mode;
};
VARIANT_ENUM_CAST(BoxContainer::AlignMode);
VARIANT_ENUM_CAST(CBoxContainer::ContainerMode);
#endif // BOX_CONTAINER_H

View File

@ -341,6 +341,7 @@ void register_scene_types() {
ClassDB::register_virtual_class<BoxContainer>();
ClassDB::register_class<HBoxContainer>();
ClassDB::register_class<VBoxContainer>();
ClassDB::register_class<CBoxContainer>();
ClassDB::register_class<GridContainer>();
ClassDB::register_class<CenterContainer>();
ClassDB::register_class<ScrollContainer>();