Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set vertical alignment on tabs #31909

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions scene/gui/tab_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,18 @@ void TabContainer::_notification(int p_what) {
break;
}

int area_y;
int tabs_y;
if (position == TOP) {
area_y = header_height;
tabs_y = 0;
} else {
area_y = 0;
tabs_y = size.height - header_height;
}

// Draw the tab area.
panel->draw(canvas, Rect2(0, header_height, size.width, size.height - header_height));
panel->draw(canvas, Rect2(0, area_y, size.width, size.height - header_height));

// Draw all visible tabs.
int x = 0;
Expand All @@ -298,7 +308,7 @@ void TabContainer::_notification(int p_what) {

// Draw the tab background.
int tab_width = tab_widths[i];
Rect2 tab_rect(tabs_ofs_cache + x, 0, tab_width, header_height);
Rect2 tab_rect(tabs_ofs_cache + x, tabs_y, tab_width, header_height);
tab_style->draw(canvas, tab_rect);

// Draw the tab contents.
Expand All @@ -307,7 +317,7 @@ void TabContainer::_notification(int p_what) {

int x_content = tab_rect.position.x + tab_style->get_margin(MARGIN_LEFT);
int top_margin = tab_style->get_margin(MARGIN_TOP);
int y_center = top_margin + (tab_rect.size.y - tab_style->get_minimum_size().y) / 2;
int y_center = tab_rect.position.y + top_margin + (tab_rect.size.y - tab_style->get_minimum_size().y) / 2;

// Draw the tab icon.
if (control->has_meta("_tab_icon")) {
Expand Down Expand Up @@ -697,11 +707,25 @@ void TabContainer::set_tab_align(TabAlign p_align) {
_change_notify("tab_align");
}

void TabContainer::set_tab_position(Position p_position) {

ERR_FAIL_INDEX(p_position, 2);
position = p_position;
update();

_change_notify("tab_position");
}

TabContainer::TabAlign TabContainer::get_tab_align() const {

return align;
}

TabContainer::Position TabContainer::get_tab_position() const {

return position;
}

void TabContainer::set_tabs_visible(bool p_visibe) {

if (p_visibe == tabs_visible)
Expand Down Expand Up @@ -905,6 +929,8 @@ void TabContainer::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_tab_control", "idx"), &TabContainer::get_tab_control);
ClassDB::bind_method(D_METHOD("set_tab_align", "align"), &TabContainer::set_tab_align);
ClassDB::bind_method(D_METHOD("get_tab_align"), &TabContainer::get_tab_align);
ClassDB::bind_method(D_METHOD("set_tab_position", "position"), &TabContainer::set_tab_position);
ClassDB::bind_method(D_METHOD("get_tab_position"), &TabContainer::get_tab_position);
ClassDB::bind_method(D_METHOD("set_tabs_visible", "visible"), &TabContainer::set_tabs_visible);
ClassDB::bind_method(D_METHOD("are_tabs_visible"), &TabContainer::are_tabs_visible);
ClassDB::bind_method(D_METHOD("set_tab_title", "tab_idx", "title"), &TabContainer::set_tab_title);
Expand Down Expand Up @@ -932,6 +958,7 @@ void TabContainer::_bind_methods() {
ADD_SIGNAL(MethodInfo("pre_popup_pressed"));

ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_align", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_tab_align", "get_tab_align");
ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_position", PROPERTY_HINT_ENUM, "Top,Bottom"), "set_tab_position", "get_tab_position");
ADD_PROPERTY(PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE, "-1,4096,1", PROPERTY_USAGE_EDITOR), "set_current_tab", "get_current_tab");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "tabs_visible"), "set_tabs_visible", "are_tabs_visible");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_to_rearrange_enabled"), "set_drag_to_rearrange_enabled", "get_drag_to_rearrange_enabled");
Expand All @@ -952,6 +979,7 @@ TabContainer::TabContainer() {
previous = 0;
mouse_x_cache = 0;
align = ALIGN_CENTER;
position = TOP;
tabs_visible = true;
popup = NULL;
drag_to_rearrange_enabled = false;
Expand Down
10 changes: 10 additions & 0 deletions scene/gui/tab_container.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class TabContainer : public Container {
ALIGN_RIGHT
};

enum Position {
TOP,
BOTTOM
};

private:
int mouse_x_cache;
int first_tab_cache;
Expand All @@ -55,6 +60,7 @@ class TabContainer : public Container {
bool tabs_visible;
bool buttons_visible_cache;
TabAlign align;
Position position;
Control *_get_tab(int p_idx) const;
int _get_top_margin() const;
Popup *popup;
Expand Down Expand Up @@ -85,6 +91,9 @@ class TabContainer : public Container {
void set_tab_align(TabAlign p_align);
TabAlign get_tab_align() const;

void set_tab_position(Position p_position);
Position get_tab_position() const;

void set_tabs_visible(bool p_visibe);
bool are_tabs_visible() const;

Expand Down Expand Up @@ -126,5 +135,6 @@ class TabContainer : public Container {
};

VARIANT_ENUM_CAST(TabContainer::TabAlign);
VARIANT_ENUM_CAST(TabContainer::Position);

#endif // TAB_CONTAINER_H
43 changes: 40 additions & 3 deletions scene/gui/tabs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ void Tabs::_notification(int p_what) {

int h = get_size().height;
int w = 0;
int y = 0;
int font_y = 0;
int icon_y = 0;
int mw = 0;

for (int i = 0; i < tabs.size(); i++) {
Expand All @@ -270,6 +273,10 @@ void Tabs::_notification(int p_what) {
w = 0;
}

if (position == BOTTOM) {
y = get_size().height - (font->get_height() + font->get_ascent());
}

Ref<Texture> incr = get_icon("increment");
Ref<Texture> decr = get_icon("decrement");
Ref<Texture> incr_hl = get_icon("increment_highlight");
Expand Down Expand Up @@ -310,7 +317,7 @@ void Tabs::_notification(int p_what) {
max_drawn_tab = i;
}

Rect2 sb_rect = Rect2(w, 0, tabs[i].size_cache, h);
Rect2 sb_rect = Rect2(w, y, tabs[i].size_cache, font->get_height() + font->get_ascent());
sb->draw(ci, sb_rect);

w += sb->get_margin(MARGIN_LEFT);
Expand All @@ -319,12 +326,24 @@ void Tabs::_notification(int p_what) {
Ref<Texture> icon = tabs[i].icon;
if (icon.is_valid()) {

icon->draw(ci, Point2i(w, sb->get_margin(MARGIN_TOP) + ((sb_rect.size.y - sb_ms.y) - icon->get_height()) / 2));
if (position == BOTTOM) {
icon_y = get_size().height - icon->get_height() / 2;
} else {
icon_y = sb->get_margin(MARGIN_TOP) + ((sb_rect.size.y - sb_ms.y) - icon->get_height()) / 2;
}

icon->draw(ci, Point2i(w, icon_y));
if (tabs[i].text != "")
w += icon->get_width() + get_constant("hseparation");
}

font->draw(ci, Point2i(w, sb->get_margin(MARGIN_TOP) + ((sb_rect.size.y - sb_ms.y) - font->get_height()) / 2 + font->get_ascent()), tabs[i].text, col, tabs[i].size_text);
if (position == BOTTOM) {
font_y = get_size().height - (font->get_height() / 2);
} else {
font_y = sb->get_margin(MARGIN_TOP) + ((sb_rect.size.y - sb_ms.y) - font->get_height()) / 2 + font->get_ascent();
}

font->draw(ci, Point2i(w, font_y), tabs[i].text, col, tabs[i].size_text);

w += tabs[i].size_text;

Expand Down Expand Up @@ -766,11 +785,25 @@ void Tabs::set_tab_align(TabAlign p_align) {
update();
}

void Tabs::set_tab_position(Position p_position) {

ERR_FAIL_INDEX(p_position, 2);
position = p_position;
update();

_change_notify("tab_position");
}

Tabs::TabAlign Tabs::get_tab_align() const {

return tab_align;
}

Tabs::Position Tabs::get_tab_position() const {

return position;
}

void Tabs::move_tab(int from, int to) {

if (from == to)
Expand Down Expand Up @@ -961,6 +994,8 @@ void Tabs::_bind_methods() {
ClassDB::bind_method(D_METHOD("add_tab", "title", "icon"), &Tabs::add_tab, DEFVAL(""), DEFVAL(Ref<Texture>()));
ClassDB::bind_method(D_METHOD("set_tab_align", "align"), &Tabs::set_tab_align);
ClassDB::bind_method(D_METHOD("get_tab_align"), &Tabs::get_tab_align);
ClassDB::bind_method(D_METHOD("set_tab_position", "position"), &Tabs::set_tab_position);
ClassDB::bind_method(D_METHOD("get_tab_position"), &Tabs::get_tab_position);
ClassDB::bind_method(D_METHOD("get_tab_offset"), &Tabs::get_tab_offset);
ClassDB::bind_method(D_METHOD("get_offset_buttons_visible"), &Tabs::get_offset_buttons_visible);
ClassDB::bind_method(D_METHOD("ensure_tab_visible", "idx"), &Tabs::ensure_tab_visible);
Expand All @@ -987,6 +1022,7 @@ void Tabs::_bind_methods() {

ADD_PROPERTY(PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE, "-1,4096,1", PROPERTY_USAGE_EDITOR), "set_current_tab", "get_current_tab");
ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_align", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_tab_align", "get_tab_align");
ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_position", PROPERTY_HINT_ENUM, "Top,Bottom"), "set_tab_position", "get_tab_position");
ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_close_display_policy", PROPERTY_HINT_ENUM, "Show Never,Show Active Only,Show Always"), "set_tab_close_display_policy", "get_tab_close_display_policy");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scrolling_enabled"), "set_scrolling_enabled", "get_scrolling_enabled");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_to_rearrange_enabled"), "set_drag_to_rearrange_enabled", "get_drag_to_rearrange_enabled");
Expand All @@ -1006,6 +1042,7 @@ Tabs::Tabs() {

current = 0;
tab_align = ALIGN_CENTER;
position = TOP;
rb_hover = -1;
rb_pressing = false;
highlight_arrow = -1;
Expand Down
10 changes: 10 additions & 0 deletions scene/gui/tabs.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ class Tabs : public Control {
ALIGN_MAX
};

enum Position {
TOP,
BOTTOM
};

enum CloseButtonDisplayPolicy {

CLOSE_BUTTON_SHOW_NEVER,
Expand Down Expand Up @@ -80,6 +85,7 @@ class Tabs : public Control {
int current;
int _get_top_margin() const;
TabAlign tab_align;
Position position;
int rb_hover;
bool rb_pressing;

Expand Down Expand Up @@ -129,6 +135,9 @@ class Tabs : public Control {
void set_tab_align(TabAlign p_align);
TabAlign get_tab_align() const;

void set_tab_position(Position p_position);
Position get_tab_position() const;

void move_tab(int from, int to);

void set_tab_close_display_policy(CloseButtonDisplayPolicy p_policy);
Expand Down Expand Up @@ -167,6 +176,7 @@ class Tabs : public Control {
};

VARIANT_ENUM_CAST(Tabs::TabAlign);
VARIANT_ENUM_CAST(Tabs::Position);
VARIANT_ENUM_CAST(Tabs::CloseButtonDisplayPolicy);

#endif // TABS_H