2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* tab_container.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:11:45 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2018-01-02 11:27:24 +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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "tab_container.h"
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "message_queue.h"
|
|
|
|
|
|
|
|
int TabContainer::_get_top_margin() const {
|
|
|
|
|
|
|
|
Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
|
|
|
|
Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
|
|
|
|
Ref<Font> font = get_font("font");
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
int h = MAX(tab_bg->get_minimum_size().height, tab_fg->get_minimum_size().height);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-14 18:03:38 +01:00
|
|
|
int ch = font->get_height();
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Control *c = get_child(i)->cast_to<Control>();
|
|
|
|
if (!c)
|
|
|
|
continue;
|
|
|
|
if (c->is_set_as_toplevel())
|
|
|
|
continue;
|
|
|
|
if (!c->has_meta("_tab_icon"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Ref<Texture> tex = c->get_meta("_tab_icon");
|
|
|
|
if (!tex.is_valid())
|
|
|
|
continue;
|
2017-03-19 00:36:26 +01:00
|
|
|
ch = MAX(ch, tex->get_size().height);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
h += ch;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void TabContainer::_input_event(const InputEvent &p_event) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p_event.type == InputEvent::MOUSE_BUTTON &&
|
|
|
|
p_event.mouse_button.pressed &&
|
|
|
|
p_event.mouse_button.button_index == BUTTON_LEFT) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
// clicks
|
2017-03-19 00:36:26 +01:00
|
|
|
Point2 pos(p_event.mouse_button.x, p_event.mouse_button.y);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
int top_margin = _get_top_margin();
|
2017-03-19 00:36:26 +01:00
|
|
|
if (pos.y > top_margin)
|
2014-02-10 02:10:30 +01:00
|
|
|
return; // no click (too far down)
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (pos.x < tabs_ofs_cache)
|
2014-02-10 02:10:30 +01:00
|
|
|
return; // no click (too far left)
|
|
|
|
|
|
|
|
Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
|
|
|
|
Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
|
|
|
|
Ref<Font> font = get_font("font");
|
|
|
|
Ref<Texture> incr = get_icon("increment");
|
|
|
|
Ref<Texture> decr = get_icon("decrement");
|
2015-06-14 03:12:53 +02:00
|
|
|
Ref<Texture> menu = get_icon("menu");
|
|
|
|
Ref<Texture> menu_hl = get_icon("menu_hl");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (popup && pos.x > get_size().width - menu->get_width()) {
|
2015-06-14 03:12:53 +02:00
|
|
|
|
|
|
|
emit_signal("pre_popup_pressed");
|
|
|
|
Vector2 pp_pos = get_global_pos();
|
2017-03-19 00:36:26 +01:00
|
|
|
pp_pos.x += get_size().width;
|
|
|
|
pp_pos.x -= popup->get_size().width;
|
|
|
|
pp_pos.y += menu->get_height();
|
2015-06-14 03:12:53 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
popup->set_global_pos(pp_pos);
|
2017-01-14 18:03:38 +01:00
|
|
|
popup->popup();
|
2015-06-14 03:12:53 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
pos.x -= tabs_ofs_cache;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
int idx = 0;
|
|
|
|
int found = -1;
|
|
|
|
bool rightroom = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Control *c = get_child(i)->cast_to<Control>();
|
|
|
|
if (!c)
|
|
|
|
continue;
|
|
|
|
if (c->is_set_as_toplevel())
|
|
|
|
continue;
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (idx < tab_display_ofs) {
|
2014-02-10 02:10:30 +01:00
|
|
|
idx++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (idx > last_tab_cache) {
|
|
|
|
rightroom = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
String s = c->has_meta("_tab_name") ? String(XL_MESSAGE(String(c->get_meta("_tab_name")))) : String(c->get_name());
|
|
|
|
int tab_width = font->get_string_size(s).width;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-06-14 03:12:53 +02:00
|
|
|
if (c->has_meta("_tab_icon")) {
|
|
|
|
Ref<Texture> icon = c->get_meta("_tab_icon");
|
|
|
|
if (icon.is_valid()) {
|
2017-03-19 00:36:26 +01:00
|
|
|
tab_width += icon->get_width();
|
|
|
|
if (s != "")
|
|
|
|
tab_width += get_constant("hseparation");
|
2015-06-14 03:12:53 +02:00
|
|
|
}
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (idx == current) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
tab_width += tab_fg->get_minimum_size().width;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2017-03-19 00:36:26 +01:00
|
|
|
tab_width += tab_bg->get_minimum_size().width;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pos.x < tab_width) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
found = idx;
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
pos.x -= tab_width;
|
2014-02-10 02:10:30 +01:00
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buttons_visible_cache) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p_event.mouse_button.x > get_size().width - incr->get_width()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
if (rightroom) {
|
2017-03-19 00:36:26 +01:00
|
|
|
tab_display_ofs += 1;
|
2014-02-10 02:10:30 +01:00
|
|
|
update();
|
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
} else if (p_event.mouse_button.x > get_size().width - incr->get_width() - decr->get_width()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (tab_display_ofs > 0) {
|
|
|
|
tab_display_ofs -= 1;
|
2014-02-10 02:10:30 +01:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (found != -1) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-06-14 03:12:53 +02:00
|
|
|
set_current_tab(found);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TabContainer::_notification(int p_what) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
switch (p_what) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
case NOTIFICATION_DRAW: {
|
|
|
|
|
|
|
|
RID ci = get_canvas_item();
|
|
|
|
Ref<StyleBox> panel = get_stylebox("panel");
|
|
|
|
Size2 size = get_size();
|
|
|
|
|
|
|
|
if (!tabs_visible) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
panel->draw(ci, Rect2(0, 0, size.width, size.height));
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
|
|
|
|
Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
|
|
|
|
Ref<Texture> incr = get_icon("increment");
|
|
|
|
Ref<Texture> decr = get_icon("decrement");
|
2015-06-14 03:12:53 +02:00
|
|
|
Ref<Texture> menu = get_icon("menu");
|
|
|
|
Ref<Texture> menu_hl = get_icon("menu_hl");
|
2014-02-10 02:10:30 +01:00
|
|
|
Ref<Font> font = get_font("font");
|
|
|
|
Color color_fg = get_color("font_color_fg");
|
|
|
|
Color color_bg = get_color("font_color_bg");
|
|
|
|
|
|
|
|
int side_margin = get_constant("side_margin");
|
|
|
|
int top_margin = _get_top_margin();
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Size2 top_size = Size2(size.width, top_margin);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
int w = 0;
|
|
|
|
int idx = 0;
|
2015-10-21 04:54:24 +02:00
|
|
|
Vector<int> offsets;
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector<Control *> controls;
|
|
|
|
int from = 0;
|
|
|
|
int limit = get_size().width;
|
2015-10-21 04:54:24 +02:00
|
|
|
if (popup) {
|
2017-03-19 00:36:26 +01:00
|
|
|
top_size.width -= menu->get_width();
|
|
|
|
limit -= menu->get_width();
|
2015-10-21 04:54:24 +02:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
bool notdone = false;
|
|
|
|
last_tab_cache = -1;
|
2015-10-21 04:54:24 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Control *c = get_child(i)->cast_to<Control>();
|
|
|
|
if (!c)
|
|
|
|
continue;
|
|
|
|
if (c->is_set_as_toplevel())
|
|
|
|
continue;
|
2017-03-19 00:36:26 +01:00
|
|
|
if (idx < tab_display_ofs) {
|
2015-10-21 04:54:24 +02:00
|
|
|
idx++;
|
2017-03-19 00:36:26 +01:00
|
|
|
from = idx;
|
2015-10-21 04:54:24 +02:00
|
|
|
continue;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (w >= get_size().width) {
|
|
|
|
buttons_visible_cache = true;
|
|
|
|
notdone = true;
|
2015-10-21 04:54:24 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
offsets.push_back(w);
|
|
|
|
controls.push_back(c);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
String s = c->has_meta("_tab_name") ? String(XL_MESSAGE(String(c->get_meta("_tab_name")))) : String(c->get_name());
|
|
|
|
w += font->get_string_size(s).width;
|
2014-02-10 02:10:30 +01:00
|
|
|
if (c->has_meta("_tab_icon")) {
|
|
|
|
Ref<Texture> icon = c->get_meta("_tab_icon");
|
|
|
|
if (icon.is_valid()) {
|
2017-03-19 00:36:26 +01:00
|
|
|
w += icon->get_width();
|
|
|
|
if (s != "")
|
|
|
|
w += get_constant("hseparation");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (idx == current) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
w += tab_fg->get_minimum_size().width;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2017-03-19 00:36:26 +01:00
|
|
|
w += tab_bg->get_minimum_size().width;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (idx < tab_display_ofs) {
|
2015-10-21 04:54:24 +02:00
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
last_tab_cache = idx;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-10-21 04:54:24 +02:00
|
|
|
idx++;
|
2015-06-14 03:12:53 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-10-21 04:54:24 +02:00
|
|
|
int ofs;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
switch (align) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-10-21 04:54:24 +02:00
|
|
|
case ALIGN_LEFT: ofs = side_margin; break;
|
2017-03-19 00:36:26 +01:00
|
|
|
case ALIGN_CENTER: ofs = (int(limit) - w) / 2; break;
|
2015-10-21 04:54:24 +02:00
|
|
|
case ALIGN_RIGHT: ofs = int(limit) - w - side_margin; break;
|
|
|
|
};
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
tab_display_ofs = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
tabs_ofs_cache = ofs;
|
|
|
|
idx = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < controls.size(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
idx = i + from;
|
|
|
|
if (current >= from && current < from + controls.size() - 1) {
|
2015-10-21 04:54:24 +02:00
|
|
|
//current is visible! draw it last.
|
2017-03-19 00:36:26 +01:00
|
|
|
if (i == controls.size() - 1) {
|
|
|
|
idx = current;
|
|
|
|
} else if (idx >= current) {
|
|
|
|
idx += 1;
|
2015-10-21 04:54:24 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Control *c = controls[idx - from];
|
2015-10-21 04:54:24 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
String s = c->has_meta("_tab_name") ? String(c->get_meta("_tab_name")) : String(c->get_name());
|
|
|
|
int w = font->get_string_size(s).width;
|
2014-02-10 02:10:30 +01:00
|
|
|
Ref<Texture> icon;
|
|
|
|
if (c->has_meta("_tab_icon")) {
|
|
|
|
icon = c->get_meta("_tab_icon");
|
|
|
|
if (icon.is_valid()) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
w += icon->get_width();
|
|
|
|
if (s != "")
|
|
|
|
w += get_constant("hseparation");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<StyleBox> sb;
|
|
|
|
Color col;
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (idx == current) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
sb = tab_fg;
|
|
|
|
col = color_fg;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2017-03-19 00:36:26 +01:00
|
|
|
sb = tab_bg;
|
|
|
|
col = color_bg;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
int lofs = ofs + offsets[idx - from];
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Size2i sb_ms = sb->get_minimum_size();
|
2017-03-19 00:36:26 +01:00
|
|
|
Rect2 sb_rect = Rect2(lofs, 0, w + sb_ms.width, top_margin);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
sb->draw(ci, sb_rect);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Point2i lpos = sb_rect.pos;
|
2017-03-19 00:36:26 +01:00
|
|
|
lpos.x += sb->get_margin(MARGIN_LEFT);
|
2014-02-10 02:10:30 +01:00
|
|
|
if (icon.is_valid()) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
icon->draw(ci, Point2i(lpos.x, sb->get_margin(MARGIN_TOP) + ((sb_rect.size.y - sb_ms.y) - icon->get_height()) / 2));
|
|
|
|
if (s != "")
|
|
|
|
lpos.x += icon->get_width() + get_constant("hseparation");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
font->draw(ci, Point2i(lpos.x, sb->get_margin(MARGIN_TOP) + ((sb_rect.size.y - sb_ms.y) - font->get_height()) / 2 + font->get_ascent()), s, col);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buttons_visible_cache) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
int vofs = (top_margin - incr->get_height()) / 2;
|
|
|
|
decr->draw(ci, Point2(limit, vofs), Color(1, 1, 1, tab_display_ofs == 0 ? 0.5 : 1.0));
|
|
|
|
incr->draw(ci, Point2(limit + incr->get_width(), vofs), Color(1, 1, 1, notdone ? 1.0 : 0.5));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2015-06-14 03:12:53 +02:00
|
|
|
if (popup) {
|
2017-03-19 00:36:26 +01:00
|
|
|
int from = get_size().width - menu->get_width();
|
2015-06-14 03:12:53 +02:00
|
|
|
|
|
|
|
if (mouse_x_cache > from)
|
2017-03-19 00:36:26 +01:00
|
|
|
menu_hl->draw(get_canvas_item(), Size2(from, 0));
|
2015-06-14 03:12:53 +02:00
|
|
|
else
|
2017-03-19 00:36:26 +01:00
|
|
|
menu->draw(get_canvas_item(), Size2(from, 0));
|
2015-06-14 03:12:53 +02:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
panel->draw(ci, Rect2(0, top_size.height, size.width, size.height - top_size.height));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
} break;
|
2016-05-30 05:28:29 +02:00
|
|
|
case NOTIFICATION_THEME_CHANGED: {
|
2016-06-15 19:19:34 +02:00
|
|
|
if (get_tab_count() > 0) {
|
2017-03-19 00:36:26 +01:00
|
|
|
call_deferred("set_current_tab", get_current_tab()); //wait until all changed theme
|
2016-06-15 19:19:34 +02:00
|
|
|
}
|
2016-05-30 05:28:29 +02:00
|
|
|
} break;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TabContainer::_child_renamed_callback() {
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TabContainer::add_child_notify(Node *p_child) {
|
|
|
|
|
|
|
|
Control *c = p_child->cast_to<Control>();
|
|
|
|
if (!c)
|
|
|
|
return;
|
|
|
|
if (c->is_set_as_toplevel())
|
|
|
|
return;
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
bool first = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (get_tab_count() != 1)
|
2014-02-10 02:10:30 +01:00
|
|
|
c->hide();
|
|
|
|
else {
|
|
|
|
c->show();
|
|
|
|
//call_deferred("set_current_tab",0);
|
2017-03-03 00:07:07 +01:00
|
|
|
first = true;
|
|
|
|
current = 0;
|
|
|
|
previous = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
c->set_area_as_parent_rect();
|
|
|
|
if (tabs_visible)
|
2017-03-19 00:36:26 +01:00
|
|
|
c->set_margin(MARGIN_TOP, _get_top_margin());
|
2014-02-10 02:10:30 +01:00
|
|
|
Ref<StyleBox> sb = get_stylebox("panel");
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
c->set_margin(Margin(i), c->get_margin(Margin(i)) + sb->get_margin(Margin(i)));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
update();
|
2017-03-19 00:36:26 +01:00
|
|
|
p_child->connect("renamed", this, "_child_renamed_callback");
|
|
|
|
if (first)
|
|
|
|
emit_signal("tab_changed", current);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int TabContainer::get_tab_count() const {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
int count = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Control *c = get_child(i)->cast_to<Control>();
|
|
|
|
if (!c)
|
|
|
|
continue;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TabContainer::set_current_tab(int p_current) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_INDEX(p_current, get_tab_count());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-03 00:07:07 +01:00
|
|
|
int pending_previous = current;
|
|
|
|
current = p_current;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
int idx = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Ref<StyleBox> sb = get_stylebox("panel");
|
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Control *c = get_child(i)->cast_to<Control>();
|
|
|
|
if (!c)
|
|
|
|
continue;
|
|
|
|
if (c->is_set_as_toplevel())
|
|
|
|
continue;
|
2017-03-19 00:36:26 +01:00
|
|
|
if (idx == current) {
|
2014-02-10 02:10:30 +01:00
|
|
|
c->show();
|
|
|
|
c->set_area_as_parent_rect();
|
|
|
|
if (tabs_visible)
|
2017-03-19 00:36:26 +01:00
|
|
|
c->set_margin(MARGIN_TOP, _get_top_margin());
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
c->set_margin(Margin(i), c->get_margin(Margin(i)) + sb->get_margin(Margin(i)));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
} else
|
|
|
|
c->hide();
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
_change_notify("current_tab");
|
2017-03-19 00:36:26 +01:00
|
|
|
|
2017-03-03 00:07:07 +01:00
|
|
|
if (pending_previous != current)
|
|
|
|
previous = pending_previous;
|
|
|
|
|
|
|
|
emit_signal("tab_selected", current);
|
|
|
|
emit_signal("tab_changed", current);
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
int TabContainer::get_current_tab() const {
|
|
|
|
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
2017-03-03 00:07:07 +01:00
|
|
|
int TabContainer::get_previous_tab() const {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
return previous;
|
|
|
|
}
|
2015-06-14 03:12:53 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Control *TabContainer::get_tab_control(int p_idx) const {
|
2015-06-14 03:12:53 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
int idx = 0;
|
2015-06-14 03:12:53 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
2015-06-14 03:12:53 +02:00
|
|
|
|
|
|
|
Control *c = get_child(i)->cast_to<Control>();
|
|
|
|
if (!c)
|
|
|
|
continue;
|
|
|
|
if (c->is_set_as_toplevel())
|
|
|
|
continue;
|
2017-03-19 00:36:26 +01:00
|
|
|
if (idx == p_idx) {
|
2015-06-14 03:12:53 +02:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-03-03 00:07:07 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Control *TabContainer::get_current_tab_control() const {
|
2015-06-14 03:12:53 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
int idx = 0;
|
2015-06-14 03:12:53 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
2015-06-14 03:12:53 +02:00
|
|
|
|
|
|
|
Control *c = get_child(i)->cast_to<Control>();
|
|
|
|
if (!c)
|
|
|
|
continue;
|
|
|
|
if (c->is_set_as_toplevel())
|
|
|
|
continue;
|
2017-03-19 00:36:26 +01:00
|
|
|
if (idx == current) {
|
2015-06-14 03:12:53 +02:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
void TabContainer::remove_child_notify(Node *p_child) {
|
|
|
|
|
|
|
|
int tc = get_tab_count();
|
2017-03-19 00:36:26 +01:00
|
|
|
if (current == tc - 1) {
|
2014-02-10 02:10:30 +01:00
|
|
|
current--;
|
2017-03-19 00:36:26 +01:00
|
|
|
if (current < 0)
|
|
|
|
current = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
else {
|
2017-03-19 00:36:26 +01:00
|
|
|
call_deferred("set_current_tab", current);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
p_child->disconnect("renamed", this, "_child_renamed_callback");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TabContainer::set_tab_align(TabAlign p_align) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_INDEX(p_align, 3);
|
|
|
|
align = p_align;
|
2014-02-10 02:10:30 +01:00
|
|
|
update();
|
|
|
|
|
|
|
|
_change_notify("tab_align");
|
|
|
|
}
|
2017-03-03 00:07:07 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
TabContainer::TabAlign TabContainer::get_tab_align() const {
|
|
|
|
|
|
|
|
return align;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TabContainer::set_tabs_visible(bool p_visibe) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p_visibe == tabs_visible)
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
tabs_visible = p_visibe;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Control *c = get_child(i)->cast_to<Control>();
|
|
|
|
if (!c)
|
|
|
|
continue;
|
|
|
|
if (p_visibe)
|
2017-03-19 00:36:26 +01:00
|
|
|
c->set_margin(MARGIN_TOP, _get_top_margin());
|
2014-02-10 02:10:30 +01:00
|
|
|
else
|
2017-03-19 00:36:26 +01:00
|
|
|
c->set_margin(MARGIN_TOP, 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TabContainer::are_tabs_visible() const {
|
|
|
|
|
|
|
|
return tabs_visible;
|
|
|
|
}
|
|
|
|
|
|
|
|
Control *TabContainer::_get_tab(int p_idx) const {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
int idx = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Control *c = get_child(i)->cast_to<Control>();
|
|
|
|
if (!c)
|
|
|
|
continue;
|
|
|
|
if (c->is_set_as_toplevel())
|
|
|
|
continue;
|
2017-03-19 00:36:26 +01:00
|
|
|
if (idx == p_idx)
|
2014-02-10 02:10:30 +01:00
|
|
|
return c;
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void TabContainer::set_tab_title(int p_tab, const String &p_title) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Control *child = _get_tab(p_tab);
|
|
|
|
ERR_FAIL_COND(!child);
|
2017-03-19 00:36:26 +01:00
|
|
|
child->set_meta("_tab_name", p_title);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
String TabContainer::get_tab_title(int p_tab) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Control *child = _get_tab(p_tab);
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_COND_V(!child, "");
|
2014-02-10 02:10:30 +01:00
|
|
|
if (child->has_meta("_tab_name"))
|
|
|
|
return child->get_meta("_tab_name");
|
|
|
|
else
|
|
|
|
return child->get_name();
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void TabContainer::set_tab_icon(int p_tab, const Ref<Texture> &p_icon) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Control *child = _get_tab(p_tab);
|
|
|
|
ERR_FAIL_COND(!child);
|
2017-03-19 00:36:26 +01:00
|
|
|
child->set_meta("_tab_icon", p_icon);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
Ref<Texture> TabContainer::get_tab_icon(int p_tab) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Control *child = _get_tab(p_tab);
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_COND_V(!child, Ref<Texture>());
|
2014-02-10 02:10:30 +01:00
|
|
|
if (child->has_meta("_tab_icon"))
|
|
|
|
return child->get_meta("_tab_icon");
|
|
|
|
else
|
|
|
|
return Ref<Texture>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TabContainer::get_translatable_strings(List<String> *p_strings) const {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Control *c = get_child(i)->cast_to<Control>();
|
|
|
|
if (!c)
|
|
|
|
continue;
|
|
|
|
if (c->is_set_as_toplevel())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!c->has_meta("_tab_name"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
String name = c->get_meta("_tab_name");
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (name != "")
|
2014-02-10 02:10:30 +01:00
|
|
|
p_strings->push_back(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-03 02:34:22 +01:00
|
|
|
Size2 TabContainer::get_minimum_size() const {
|
|
|
|
|
|
|
|
Size2 ms;
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
2015-01-03 02:34:22 +01:00
|
|
|
|
|
|
|
Control *c = get_child(i)->cast_to<Control>();
|
|
|
|
if (!c)
|
|
|
|
continue;
|
|
|
|
if (c->is_set_as_toplevel())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!c->is_visible())
|
|
|
|
continue;
|
|
|
|
|
2016-06-17 23:38:40 +02:00
|
|
|
Size2 cms = c->get_combined_minimum_size();
|
2017-03-19 00:36:26 +01:00
|
|
|
ms.x = MAX(ms.x, cms.x);
|
|
|
|
ms.y = MAX(ms.y, cms.y);
|
2015-01-03 02:34:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
|
|
|
|
Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
|
|
|
|
Ref<Font> font = get_font("font");
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ms.y += MAX(tab_bg->get_minimum_size().y, tab_fg->get_minimum_size().y);
|
|
|
|
ms.y += font->get_height();
|
2015-01-03 02:34:22 +01:00
|
|
|
|
2016-06-17 23:38:40 +02:00
|
|
|
Ref<StyleBox> sb = get_stylebox("panel");
|
2017-03-19 00:36:26 +01:00
|
|
|
ms += sb->get_minimum_size();
|
2016-06-17 23:38:40 +02:00
|
|
|
|
2015-01-03 02:34:22 +01:00
|
|
|
return ms;
|
|
|
|
}
|
|
|
|
|
2015-06-14 03:12:53 +02:00
|
|
|
void TabContainer::set_popup(Node *p_popup) {
|
|
|
|
ERR_FAIL_NULL(p_popup);
|
2017-03-19 00:36:26 +01:00
|
|
|
popup = p_popup->cast_to<Popup>();
|
2015-06-14 03:12:53 +02:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Popup *TabContainer::get_popup() const {
|
2015-06-14 03:12:53 +02:00
|
|
|
return popup;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void TabContainer::_bind_methods() {
|
|
|
|
|
2017-03-03 00:07:07 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("_input_event"), &TabContainer::_input_event);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_tab_count"), &TabContainer::get_tab_count);
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_current_tab", "tab_idx"), &TabContainer::set_current_tab);
|
2017-03-03 00:07:07 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("get_current_tab"), &TabContainer::get_current_tab);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_previous_tab"), &TabContainer::get_previous_tab);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_current_tab_control:Control"), &TabContainer::get_current_tab_control);
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("get_tab_control:Control", "idx"), &TabContainer::get_tab_control);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_tab_align", "align"), &TabContainer::set_tab_align);
|
2017-03-03 00:07:07 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("get_tab_align"), &TabContainer::get_tab_align);
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_tabs_visible", "visible"), &TabContainer::set_tabs_visible);
|
2017-03-03 00:07:07 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("are_tabs_visible"), &TabContainer::are_tabs_visible);
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_tab_title", "tab_idx", "title"), &TabContainer::set_tab_title);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_tab_title", "tab_idx"), &TabContainer::get_tab_title);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_tab_icon", "tab_idx", "icon:Texture"), &TabContainer::set_tab_icon);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_tab_icon:Texture", "tab_idx"), &TabContainer::get_tab_icon);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_popup", "popup:Popup"), &TabContainer::set_popup);
|
2017-03-03 00:07:07 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("get_popup:Popup"), &TabContainer::get_popup);
|
|
|
|
|
|
|
|
ObjectTypeDB::bind_method(_MD("_child_renamed_callback"), &TabContainer::_child_renamed_callback);
|
|
|
|
|
|
|
|
ADD_SIGNAL(MethodInfo("tab_changed", PropertyInfo(Variant::INT, "tab")));
|
|
|
|
ADD_SIGNAL(MethodInfo("tab_selected", PropertyInfo(Variant::INT, "tab")));
|
2015-06-14 03:12:53 +02:00
|
|
|
ADD_SIGNAL(MethodInfo("pre_popup_pressed"));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_align", PROPERTY_HINT_ENUM, "Left,Center,Right"), _SCS("set_tab_align"), _SCS("get_tab_align"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE, "-1,4096,1", PROPERTY_USAGE_EDITOR), _SCS("set_current_tab"), _SCS("get_current_tab"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "tabs_visible"), _SCS("set_tabs_visible"), _SCS("are_tabs_visible"));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TabContainer::TabContainer() {
|
|
|
|
|
2017-03-03 00:07:07 +01:00
|
|
|
tab_display_ofs = 0;
|
|
|
|
buttons_visible_cache = false;
|
|
|
|
tabs_ofs_cache = 0;
|
|
|
|
current = 0;
|
|
|
|
previous = 0;
|
|
|
|
mouse_x_cache = 0;
|
|
|
|
align = ALIGN_CENTER;
|
|
|
|
tabs_visible = true;
|
|
|
|
popup = NULL;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|