Added a new CSplitContainer type which acts as both a VSplitContainer and HSplitContainer and allows changing between them using a property.

This commit is contained in:
Relintai 2023-10-20 15:06:23 +02:00
parent e6ee1f0c0f
commit 2e7709185a
7 changed files with 194 additions and 4 deletions

View File

@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="CSplitContainer" inherits="SplitContainer" version="4.2">
<brief_description>
Horizontal or vertical split container.
</brief_description>
<description>
Horizontal or vertical split container depending on it's [member mode] setting. See [SplitContainer].
</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="CSplitContainer.ContainerMode" default="0">
Sets whether this [CSplitContainer] will act as a [HSplitContainer] or a [VSplitContainer].
</member>
</members>
<constants>
<constant name="CONTAINER_MODE_HORIZONTAL" value="0" enum="ContainerMode">
This [CSplitContainer] will act as a [HSplitContainer].
</constant>
<constant name="CONTAINER_MODE_VERTICAL" value="1" enum="ContainerMode">
This [CSplitContainer] will act as a [VSplitContainer].
</constant>
</constants>
<theme_items>
<theme_item name="autohide" data_type="constant" type="int" default="1">
Boolean value. If 1 ([code]true[/code]), the grabber will hide automatically when it isn't under the cursor. If 0 ([code]false[/code]), it's always visible.
</theme_item>
<theme_item name="separation" data_type="constant" type="int" default="12">
The horizontal or vertical space between the [CSplitContainer]'s elements.
</theme_item>
<theme_item name="hgrabber" data_type="icon" type="Texture">
The icon used for the horizontal grabber drawn in the middle area.
</theme_item>
<theme_item name="vgrabber" data_type="icon" type="Texture">
The icon used for the vertical grabber drawn in the middle area.
</theme_item>
<theme_item name="bg" data_type="style" type="StyleBox">
The background drawn under this control.
</theme_item>
</theme_items>
</class>

View File

@ -1063,17 +1063,22 @@ Ref<Theme> create_editor_theme(const Ref<Theme> p_theme) {
// H/VSplitContainer
theme->set_stylebox("bg", "VSplitContainer", make_stylebox(theme->get_icon("GuiVsplitBg", "EditorIcons"), 1, 1, 1, 1));
theme->set_stylebox("bg", "HSplitContainer", make_stylebox(theme->get_icon("GuiHsplitBg", "EditorIcons"), 1, 1, 1, 1));
theme->set_stylebox("bg", "CSplitContainer", make_stylebox(theme->get_icon("GuiHsplitBg", "EditorIcons"), 1, 1, 1, 1));
theme->set_icon("grabber", "VSplitContainer", theme->get_icon("GuiVsplitter", "EditorIcons"));
theme->set_icon("grabber", "HSplitContainer", theme->get_icon("GuiHsplitter", "EditorIcons"));
theme->set_icon("vgrabber", "CSplitContainer", theme->get_icon("GuiVsplitter", "EditorIcons"));
theme->set_icon("hgrabber", "CSplitContainer", theme->get_icon("GuiHsplitter", "EditorIcons"));
theme->set_constant("separation", "HSplitContainer", default_margin_size * 2 * EDSCALE);
theme->set_constant("separation", "VSplitContainer", default_margin_size * 2 * EDSCALE);
theme->set_constant("separation", "CSplitContainer", default_margin_size * 2 * EDSCALE);
// Containers
theme->set_constant("separation", "BoxContainer", default_margin_size * EDSCALE);
theme->set_constant("separation", "HBoxContainer", default_margin_size * EDSCALE);
theme->set_constant("separation", "VBoxContainer", default_margin_size * EDSCALE);
theme->set_constant("separation", "CBoxContainer", default_margin_size * EDSCALE);
theme->set_constant("margin_left", "MarginContainer", 0);
theme->set_constant("margin_top", "MarginContainer", 0);
theme->set_constant("margin_right", "MarginContainer", 0);

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_split_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.8007812 15 L 7.8007812 1 L 3 1 z M 3 3 L 7 3 L 7 6 L 5 8 L 7 10 L 7 13 L 3 13 L 3 3 z "
style="fill:#a5efac;fill-opacity:1" />
<path
id="path1-5"
d="M 8.1679688 1 L 8.1679688 3 L 13 3 L 13 7 L 10 7 L 8.1679688 5.1679688 L 8.1679688 10.832031 L 10 9 L 13 9 L 13 13 L 8.1679688 13 L 8.1679688 15 L 13 15 C 14.104599 15 15 14.104569 15 13 L 15 3 C 15 1.8954011 14.104569 1 13 1 L 8.1679688 1 z "
style="fill:#a5efac;fill-opacity:1" />
</svg>

After

Width:  |  Height:  |  Size: 1.6 KiB

View File

@ -55,6 +55,25 @@ Control *SplitContainer::_getch(int p_idx) const {
return nullptr;
}
bool SplitContainer::_get_vertical() const {
return vertical;
}
void SplitContainer::_set_vertical(bool p_vertical) {
if (vertical == p_vertical) {
return;
}
vertical = p_vertical;
should_clamp_split_offset = true;
queue_sort();
minimum_size_changed();
}
String SplitContainer::get_grabber_theme_constant_name() const {
return "grabber";
}
void SplitContainer::_resort() {
int axis = vertical ? 1 : 0;
@ -76,7 +95,7 @@ void SplitContainer::_resort() {
bool second_expanded = (vertical ? second->get_v_size_flags() : second->get_h_size_flags()) & SIZE_EXPAND;
// Determine the separation between items
Ref<Texture> g = get_theme_icon("grabber");
Ref<Texture> g = get_theme_icon(get_grabber_theme_constant_name());
int sep = get_theme_constant("separation");
sep = (dragger_visibility != DRAGGER_HIDDEN_COLLAPSED) ? MAX(sep, vertical ? g->get_height() : g->get_width()) : 0;
@ -124,7 +143,7 @@ Size2 SplitContainer::get_minimum_size() const {
/* Calculate MINIMUM SIZE */
Size2i minimum;
Ref<Texture> g = get_theme_icon("grabber");
Ref<Texture> g = get_theme_icon(get_grabber_theme_constant_name());
int sep = get_theme_constant("separation");
sep = (dragger_visibility != DRAGGER_HIDDEN_COLLAPSED) ? MAX(sep, vertical ? g->get_height() : g->get_width()) : 0;
@ -180,7 +199,7 @@ void SplitContainer::_notification(int p_what) {
}
int sep = dragger_visibility != DRAGGER_HIDDEN_COLLAPSED ? get_theme_constant("separation") : 0;
Ref<Texture> tex = get_theme_icon("grabber");
Ref<Texture> tex = get_theme_icon(get_grabber_theme_constant_name());
Size2 size = get_size();
if (vertical) {
@ -353,3 +372,39 @@ SplitContainer::SplitContainer(bool p_vertical) {
collapsed = false;
dragger_visibility = DRAGGER_VISIBLE;
}
CSplitContainer::ContainerMode CSplitContainer::get_mode() const {
return _mode;
}
void CSplitContainer::set_mode(const ContainerMode p_mode) {
if (p_mode == _mode) {
return;
}
_mode = p_mode;
_set_vertical(_mode == CONTAINER_MODE_VERTICAL);
}
CSplitContainer::CSplitContainer() {
_mode = CONTAINER_MODE_HORIZONTAL;
}
CSplitContainer::~CSplitContainer() {
}
String CSplitContainer::get_grabber_theme_constant_name() const {
if (_get_vertical()) {
return "vgrabber";
} else {
return "hgrabber";
}
}
void CSplitContainer::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_mode"), &CSplitContainer::get_mode);
ClassDB::bind_method(D_METHOD("set_mode", "mode"), &CSplitContainer::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

@ -56,9 +56,14 @@ private:
Control *_getch(int p_idx) const;
protected:
bool _get_vertical() const;
void _set_vertical(bool p_vertical);
virtual String get_grabber_theme_constant_name() const;
void _resort();
protected:
void _gui_input(const Ref<InputEvent> &p_event);
void _notification(int p_what);
static void _bind_methods();
@ -83,6 +88,8 @@ public:
VARIANT_ENUM_CAST(SplitContainer::DraggerVisibility);
// ==== HSplitContainer ====
class HSplitContainer : public SplitContainer {
GDCLASS(HSplitContainer, SplitContainer);
@ -91,6 +98,8 @@ public:
SplitContainer(false) {}
};
// ==== VSplitContainer ====
class VSplitContainer : public SplitContainer {
GDCLASS(VSplitContainer, SplitContainer);
@ -99,4 +108,31 @@ public:
SplitContainer(true) {}
};
// ==== CSplitContainer ====
class CSplitContainer : public SplitContainer {
GDCLASS(CSplitContainer, SplitContainer);
public:
enum ContainerMode {
CONTAINER_MODE_HORIZONTAL = 0,
CONTAINER_MODE_VERTICAL,
};
ContainerMode get_mode() const;
void set_mode(const ContainerMode p_mode);
CSplitContainer();
~CSplitContainer();
protected:
virtual String get_grabber_theme_constant_name() const;
static void _bind_methods();
ContainerMode _mode;
};
VARIANT_ENUM_CAST(CSplitContainer::ContainerMode);
#endif // SPLIT_CONTAINER_H

View File

@ -387,6 +387,7 @@ void register_scene_types() {
ClassDB::register_virtual_class<SplitContainer>();
ClassDB::register_class<HSplitContainer>();
ClassDB::register_class<VSplitContainer>();
ClassDB::register_class<CSplitContainer>();
ClassDB::register_class<GraphNode>();
ClassDB::register_class<GraphEdit>();

View File

@ -880,12 +880,17 @@ void fill_default_theme(Ref<Theme> &theme, const Ref<Font> &default_font, const
theme->set_stylebox("bg", "VSplitContainer", make_stylebox(vsplit_bg_png, 1, 1, 1, 1));
theme->set_stylebox("bg", "HSplitContainer", make_stylebox(hsplit_bg_png, 1, 1, 1, 1));
theme->set_stylebox("bg", "CSplitContainer", make_stylebox(hsplit_bg_png, 1, 1, 1, 1));
theme->set_icon("grabber", "VSplitContainer", make_icon(vsplitter_png));
theme->set_icon("grabber", "HSplitContainer", make_icon(hsplitter_png));
theme->set_icon("vgrabber", "CSplitContainer", make_icon(vsplitter_png));
theme->set_icon("hgrabber", "CSplitContainer", make_icon(hsplitter_png));
theme->set_constant("separation", "HBoxContainer", 4 * scale);
theme->set_constant("separation", "VBoxContainer", 4 * scale);
theme->set_constant("separation", "CBoxContainer", 4 * scale);
theme->set_constant("margin_left", "MarginContainer", 0 * scale);
theme->set_constant("margin_top", "MarginContainer", 0 * scale);
theme->set_constant("margin_right", "MarginContainer", 0 * scale);
@ -894,8 +899,10 @@ void fill_default_theme(Ref<Theme> &theme, const Ref<Font> &default_font, const
theme->set_constant("vseparation", "GridContainer", 4 * scale);
theme->set_constant("separation", "HSplitContainer", 12 * scale);
theme->set_constant("separation", "VSplitContainer", 12 * scale);
theme->set_constant("separation", "CSplitContainer", 12 * scale);
theme->set_constant("autohide", "HSplitContainer", 1);
theme->set_constant("autohide", "VSplitContainer", 1);
theme->set_constant("autohide", "CSplitContainer", 1);
theme->set_constant("hseparation", "HFlowContainer", 4 * scale);
theme->set_constant("vseparation", "HFlowContainer", 4 * scale);
theme->set_constant("hseparation", "VFlowContainer", 4 * scale);