فهرست منبع

Merge pull request #9196 from volzhs/scene-tab

Enhance scene tabs
Rémi Verschelde 8 سال پیش
والد
کامیت
3e7bbf2ca3
7فایلهای تغییر یافته به همراه216 افزوده شده و 43 حذف شده
  1. 6 1
      editor/editor_data.cpp
  2. 68 1
      editor/editor_node.cpp
  3. 6 0
      editor/editor_node.h
  4. 6 0
      editor/editor_settings.cpp
  5. 2 0
      editor/editor_themes.cpp
  6. 122 40
      scene/gui/tabs.cpp
  7. 6 1
      scene/gui/tabs.h

+ 6 - 1
editor/editor_data.cpp

@@ -675,7 +675,12 @@ String EditorData::get_scene_title(int p_idx) const {
 		return "[empty]";
 	if (edited_scene[p_idx].root->get_filename() == "")
 		return "[unsaved]";
-	return edited_scene[p_idx].root->get_filename().get_file();
+	bool show_ext = EDITOR_DEF("interface/scene_tabs/show_extension", false);
+	String name = edited_scene[p_idx].root->get_filename().get_file();
+	if (!show_ext) {
+		name = name.get_basename();
+	}
+	return name;
 }
 
 String EditorData::get_scene_path(int p_idx) const {

+ 68 - 1
editor/editor_node.cpp

@@ -342,6 +342,12 @@ void EditorNode::_notification(int p_what) {
 		play_button_panel->add_style_override("panel", gui_base->get_stylebox("PlayButtonPanel", "EditorStyles"));
 		scene_root_parent->add_style_override("panel", gui_base->get_stylebox("Content", "EditorStyles"));
 		bottom_panel->add_style_override("panel", gui_base->get_stylebox("Content", "EditorStyles"));
+		if (bool(EDITOR_DEF("interface/scene_tabs/resize_if_many_tabs", true))) {
+			scene_tabs->set_min_width(int(EDITOR_DEF("interface/scene_tabs/minimum_width", 50)) * EDSCALE);
+		} else {
+			scene_tabs->set_min_width(0);
+		}
+		_update_scene_tabs();
 	}
 }
 
@@ -4269,7 +4275,47 @@ void EditorNode::_scene_tab_closed(int p_tab) {
 	}
 }
 
+void EditorNode::_scene_tab_hover(int p_tab) {
+	if (bool(EDITOR_DEF("interface/scene_tabs/show_thumbnail_on_hover", true)) == false) {
+		return;
+	}
+	int current_tab = scene_tabs->get_current_tab();
+
+	if (p_tab == current_tab || p_tab < 0) {
+		tab_preview_panel->hide();
+	} else {
+		String path = editor_data.get_scene_path(p_tab);
+		EditorResourcePreview::get_singleton()->queue_resource_preview(path, this, "_thumbnail_done", p_tab);
+	}
+}
+
+void EditorNode::_scene_tab_exit() {
+	tab_preview_panel->hide();
+}
+
+void EditorNode::_scene_tab_input(const Ref<InputEvent> &p_input) {
+	Ref<InputEventMouseButton> mb = p_input;
+
+	if (mb.is_valid()) {
+		if (mb->get_button_index() == BUTTON_MIDDLE && mb->is_pressed() && scene_tabs->get_hovered_tab() >= 0) {
+			_scene_tab_closed(scene_tabs->get_hovered_tab());
+		}
+	}
+}
+
+void EditorNode::_thumbnail_done(const String &p_path, const Ref<Texture> &p_preview, const Variant &p_udata) {
+	int p_tab = p_udata.operator signed int();
+	if (p_preview.is_valid()) {
+		Rect2 rect = scene_tabs->get_tab_rect(p_tab);
+		rect.position += scene_tabs->get_global_position();
+		tab_preview->set_texture(p_preview);
+		tab_preview_panel->set_position(rect.position + Vector2(0, rect.size.height));
+		tab_preview_panel->show();
+	}
+}
+
 void EditorNode::_scene_tab_changed(int p_tab) {
+	tab_preview_panel->hide();
 
 	//print_line("set current 1 ");
 	bool unsaved = (saved_version != editor_data.get_undo_redo().get_version());
@@ -4760,7 +4806,6 @@ void EditorNode::_dim_timeout() {
 }
 
 void EditorNode::_check_gui_base_size() {
-	print_line(itos(int(gui_base->get_size().width)));
 	if (gui_base->get_size().width > 1200 * EDSCALE) {
 		for (int i = 0; i < singleton->main_editor_button_vb->get_child_count(); i++) {
 			ToolButton *btn = singleton->main_editor_button_vb->get_child(i)->cast_to<ToolButton>();
@@ -4837,6 +4882,10 @@ void EditorNode::_bind_methods() {
 	ClassDB::bind_method("set_current_version", &EditorNode::set_current_version);
 	ClassDB::bind_method("_scene_tab_changed", &EditorNode::_scene_tab_changed);
 	ClassDB::bind_method("_scene_tab_closed", &EditorNode::_scene_tab_closed);
+	ClassDB::bind_method("_scene_tab_hover", &EditorNode::_scene_tab_hover);
+	ClassDB::bind_method("_scene_tab_exit", &EditorNode::_scene_tab_exit);
+	ClassDB::bind_method("_scene_tab_input", &EditorNode::_scene_tab_input);
+	ClassDB::bind_method("_thumbnail_done", &EditorNode::_thumbnail_done);
 	ClassDB::bind_method("_scene_tab_script_edited", &EditorNode::_scene_tab_script_edited);
 	ClassDB::bind_method("_set_main_scene_state", &EditorNode::_set_main_scene_state);
 	ClassDB::bind_method("_update_scene_tabs", &EditorNode::_update_scene_tabs);
@@ -5181,13 +5230,31 @@ EditorNode::EditorNode() {
 	main_editor_tabs->connect("tab_changed",this,"_editor_select");
 	main_editor_tabs->set_tab_close_display_policy(Tabs::SHOW_NEVER);
 */
+	tab_preview_panel = memnew(Panel);
+	tab_preview_panel->set_size(Size2(100, 100) * EDSCALE);
+	tab_preview_panel->hide();
+	tab_preview_panel->set_self_modulate(Color(1, 1, 1, 0.7));
+	gui_base->add_child(tab_preview_panel);
+
+	tab_preview = memnew(TextureRect);
+	tab_preview->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
+	tab_preview->set_size(Size2(96, 96) * EDSCALE);
+	tab_preview->set_position(Point2(2, 2) * EDSCALE);
+	tab_preview_panel->add_child(tab_preview);
+
 	scene_tabs = memnew(Tabs);
+	scene_tabs->add_style_override("tab_fg", gui_base->get_stylebox("SceneTabFG", "EditorStyles"));
+	scene_tabs->add_style_override("tab_bg", gui_base->get_stylebox("SceneTabBG", "EditorStyles"));
 	scene_tabs->add_tab("unsaved");
 	scene_tabs->set_tab_align(Tabs::ALIGN_LEFT);
 	scene_tabs->set_tab_close_display_policy((bool(EDITOR_DEF("interface/always_show_close_button_in_scene_tabs", false)) ? Tabs::CLOSE_BUTTON_SHOW_ALWAYS : Tabs::CLOSE_BUTTON_SHOW_ACTIVE_ONLY));
+	scene_tabs->set_min_width(int(EDITOR_DEF("interface/scene_tabs/minimum_width", 50)) * EDSCALE);
 	scene_tabs->connect("tab_changed", this, "_scene_tab_changed");
 	scene_tabs->connect("right_button_pressed", this, "_scene_tab_script_edited");
 	scene_tabs->connect("tab_close", this, "_scene_tab_closed");
+	scene_tabs->connect("tab_hover", this, "_scene_tab_hover");
+	scene_tabs->connect("mouse_exited", this, "_scene_tab_exit");
+	scene_tabs->connect("gui_input", this, "_scene_tab_input");
 
 	HBoxContainer *tabbar_container = memnew(HBoxContainer);
 	scene_tabs->set_h_size_flags(Control::SIZE_EXPAND_FILL);

+ 6 - 0
editor/editor_node.h

@@ -233,6 +233,8 @@ private:
 	//main tabs
 
 	Tabs *scene_tabs;
+	Panel *tab_preview_panel;
+	TextureRect *tab_preview;
 	int tab_closing;
 
 	bool exiting;
@@ -556,6 +558,10 @@ private:
 	void _dock_popup_exit();
 	void _scene_tab_changed(int p_tab);
 	void _scene_tab_closed(int p_tab);
+	void _scene_tab_hover(int p_tab);
+	void _scene_tab_exit();
+	void _scene_tab_input(const Ref<InputEvent> &p_input);
+	void _thumbnail_done(const String &p_path, const Ref<Texture> &p_preview, const Variant &p_udata);
 	void _scene_tab_script_edited(int p_tab);
 
 	Dictionary _get_main_scene_state();

+ 6 - 0
editor/editor_settings.cpp

@@ -528,6 +528,12 @@ void EditorSettings::_load_defaults(Ref<ConfigFile> p_extra_config) {
 	set("interface/theme/custom_theme", "");
 	hints["interface/theme/custom_theme"] = PropertyInfo(Variant::STRING, "interface/theme/custom_theme", PROPERTY_HINT_GLOBAL_FILE, "*.res,*.tres,*.theme", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED);
 
+	set("interface/scene_tabs/show_extension", false);
+	set("interface/scene_tabs/show_thumbnail_on_hover", true);
+	set("interface/scene_tabs/resize_if_many_tabs", true);
+	set("interface/scene_tabs/minimum_width", 50);
+	hints["interface/scene_tabs/minimum_width"] = PropertyInfo(Variant::INT, "interface/scene_tabs/minimum_width", PROPERTY_HINT_RANGE, "50,500,1", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED);
+
 	set("filesystem/directories/autoscan_project_path", "");
 	hints["filesystem/directories/autoscan_project_path"] = PropertyInfo(Variant::STRING, "filesystem/directories/autoscan_project_path", PROPERTY_HINT_GLOBAL_DIR);
 	set("filesystem/directories/default_project_path", "");

+ 2 - 0
editor/editor_themes.cpp

@@ -307,6 +307,8 @@ Ref<Theme> create_editor_theme() {
 	theme->set_color("font_color_bg", "TabContainer", light_color_2);
 	theme->set_icon("menu", "TabContainer", theme->get_icon("TabMenu", "EditorIcons"));
 	theme->set_icon("menu_hl", "TabContainer", theme->get_icon("TabMenu", "EditorIcons"));
+	theme->set_stylebox("SceneTabFG", "EditorStyles", make_flat_stylebox(base_color, 10, 5, 10, 5));
+	theme->set_stylebox("SceneTabBG", "EditorStyles", make_empty_stylebox(6, 5, 6, 5));
 
 	// Debugger
 	Ref<StyleBoxFlat> style_panel_debugger = make_flat_stylebox(dark_color_2, 0, 4, 0, 0);

+ 122 - 40
scene/gui/tabs.cpp

@@ -103,13 +103,17 @@ void Tabs::_gui_input(const Ref<InputEvent> &p_event) {
 		}
 
 		// test hovering to display right or close button
+		int hover_now = -1;
 		int hover_buttons = -1;
-		hover = -1;
 		for (int i = 0; i < tabs.size(); i++) {
 
 			if (i < offset)
 				continue;
 
+			Rect2 rect = get_tab_rect(i);
+			if (rect.has_point(pos)) {
+				hover_now = i;
+			}
 			if (tabs[i].rb_rect.has_point(pos)) {
 				rb_hover = i;
 				cb_hover = -1;
@@ -122,6 +126,10 @@ void Tabs::_gui_input(const Ref<InputEvent> &p_event) {
 				break;
 			}
 		}
+		if (hover != hover_now) {
+			hover = hover_now;
+			emit_signal("tab_hover", hover);
+		}
 
 		if (hover_buttons == -1) { // no hover
 			rb_hover = hover_buttons;
@@ -234,11 +242,13 @@ void Tabs::_notification(int p_what) {
 			update();
 		} break;
 		case NOTIFICATION_RESIZED: {
-
+			_update_cache();
 			_ensure_no_over_offset();
+			ensure_tab_visible(current);
+
 		} break;
 		case NOTIFICATION_DRAW: {
-
+			_update_cache();
 			RID ci = get_canvas_item();
 
 			Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
@@ -286,18 +296,7 @@ void Tabs::_notification(int p_what) {
 
 				tabs[i].ofs_cache = w;
 
-				int lsize = get_tab_width(i);
-
-				String text = tabs[i].text;
-				int slen = font->get_string_size(text).width;
-
-				if (w + lsize > limit) {
-					max_drawn_tab = i - 1;
-					missing_right = true;
-					break;
-				} else {
-					max_drawn_tab = i;
-				}
+				int lsize = tabs[i].size_cache;
 
 				Ref<StyleBox> sb;
 				Color col;
@@ -313,7 +312,15 @@ void Tabs::_notification(int p_what) {
 					col = color_bg;
 				}
 
-				Rect2 sb_rect = Rect2(w, 0, lsize, h);
+				if (w + lsize > limit) {
+					max_drawn_tab = i - 1;
+					missing_right = true;
+					break;
+				} else {
+					max_drawn_tab = i;
+				}
+
+				Rect2 sb_rect = Rect2(w, 0, tabs[i].size_cache, h);
 				sb->draw(ci, sb_rect);
 
 				w += sb->get_margin(MARGIN_LEFT);
@@ -323,13 +330,13 @@ void Tabs::_notification(int p_what) {
 				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 (text != "")
+					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()), text, col);
+				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);
 
-				w += slen;
+				w += tabs[i].size_text;
 
 				if (tabs[i].right_button.is_valid()) {
 
@@ -380,8 +387,6 @@ void Tabs::_notification(int p_what) {
 				}
 
 				w += sb->get_margin(MARGIN_RIGHT);
-
-				tabs[i].size_cache = w - tabs[i].ofs_cache;
 			}
 
 			if (offset > 0 || missing_right) {
@@ -419,6 +424,7 @@ void Tabs::set_current_tab(int p_current) {
 	current = p_current;
 
 	_change_notify("current_tab");
+	_update_cache();
 	update();
 }
 
@@ -427,6 +433,10 @@ int Tabs::get_current_tab() const {
 	return current;
 }
 
+int Tabs::get_hovered_tab() const {
+	return hover;
+}
+
 void Tabs::set_tab_title(int p_tab, const String &p_title) {
 
 	ERR_FAIL_INDEX(p_tab, tabs.size());
@@ -480,15 +490,81 @@ Ref<Texture> Tabs::get_tab_right_button(int p_tab) const {
 	return tabs[p_tab].right_button;
 }
 
+void Tabs::_update_cache() {
+	Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled");
+	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");
+	int limit = get_size().width - incr->get_width() - decr->get_width();
+
+	int w = 0;
+	int mw = 0;
+	int size_fixed = 0;
+	int count_resize = 0;
+	for (int i = 0; i < tabs.size(); i++) {
+		tabs[i].ofs_cache = mw;
+		tabs[i].size_cache = get_tab_width(i);
+		tabs[i].size_text = font->get_string_size(tabs[i].text).width;
+		mw += tabs[i].size_cache;
+		if (tabs[i].size_cache <= min_width || i == current) {
+			size_fixed += tabs[i].size_cache;
+		} else {
+			count_resize++;
+		}
+	}
+	int m_width = min_width;
+	if (count_resize > 0) {
+		m_width = MAX((limit - size_fixed) / count_resize, min_width);
+	}
+	for (int i = 0; i < tabs.size(); i++) {
+		if (i < offset)
+			continue;
+		Ref<StyleBox> sb;
+		if (tabs[i].disabled) {
+			sb = tab_disabled;
+		} else if (i == current) {
+			sb = tab_fg;
+		} else {
+			sb = tab_bg;
+		}
+		int lsize = tabs[i].size_cache;
+		int slen = tabs[i].size_text;
+		if (min_width > 0 && mw > limit && i != current) {
+			if (lsize > m_width) {
+				slen = m_width - (sb->get_margin(MARGIN_LEFT) + sb->get_margin(MARGIN_RIGHT));
+				if (tabs[i].icon.is_valid()) {
+					slen -= tabs[i].icon->get_width();
+					slen -= get_constant("hseparation");
+				}
+				if (cb_displaypolicy == CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy == CLOSE_BUTTON_SHOW_ACTIVE_ONLY && i == current)) {
+					Ref<Texture> cb = get_icon("close");
+					slen -= cb->get_width();
+					slen -= get_constant("hseparation");
+				}
+				slen = MAX(slen, 1);
+				lsize = m_width;
+			}
+		}
+		tabs[i].ofs_cache = w;
+		tabs[i].size_cache = lsize;
+		tabs[i].size_text = slen;
+		w += lsize;
+	}
+}
+
 void Tabs::add_tab(const String &p_str, const Ref<Texture> &p_icon) {
 
 	Tab t;
 	t.text = p_str;
 	t.icon = p_icon;
 	t.disabled = false;
+	t.ofs_cache = 0;
+	t.size_cache = 0;
 
 	tabs.push_back(t);
-
+	_update_cache();
 	update();
 	minimum_size_changed();
 }
@@ -505,6 +581,7 @@ void Tabs::remove_tab(int p_idx) {
 	tabs.remove(p_idx);
 	if (current >= p_idx)
 		current--;
+	_update_cache();
 	update();
 	minimum_size_changed();
 
@@ -587,7 +664,7 @@ void Tabs::_ensure_no_over_offset() {
 			if (i < offset - 1)
 				continue;
 
-			total_w += get_tab_width(i);
+			total_w += tabs[i].size_cache;
 		}
 
 		if (total_w < limit) {
@@ -604,37 +681,35 @@ void Tabs::ensure_tab_visible(int p_idx) {
 	if (!is_inside_tree())
 		return;
 
+	if (tabs.size() == 0) return;
 	ERR_FAIL_INDEX(p_idx, tabs.size());
 
-	_ensure_no_over_offset();
-
-	if (p_idx <= offset) {
+	if (p_idx == offset) {
+		return;
+	}
+	if (p_idx < offset) {
 		offset = p_idx;
 		update();
 		return;
 	}
 
+	int prev_offset = offset;
 	Ref<Texture> incr = get_icon("increment");
 	Ref<Texture> decr = get_icon("decrement");
 	int limit = get_size().width - incr->get_width() - decr->get_width();
-
-	int x = 0;
-	for (int i = 0; i < tabs.size(); i++) {
-
-		if (i < offset)
-			continue;
-
-		int sz = get_tab_width(i);
-		tabs[i].x_cache = x;
-		tabs[i].x_size_cache = sz;
-		x += sz;
+	for (int i = offset; i <= p_idx; i++) {
+		if (tabs[i].ofs_cache + tabs[i].size_cache > limit) {
+			offset++;
+		}
 	}
 
-	while (offset < tabs.size() && ((tabs[p_idx].x_cache + tabs[p_idx].x_size_cache) - tabs[offset].x_cache) > limit) {
-		offset++;
+	if (prev_offset != offset) {
+		update();
 	}
+}
 
-	update();
+Rect2 Tabs::get_tab_rect(int p_tab) {
+	return Rect2(tabs[p_tab].ofs_cache, 0, tabs[p_tab].size_cache, get_size().height);
 }
 
 void Tabs::set_tab_close_display_policy(CloseButtonDisplayPolicy p_policy) {
@@ -642,6 +717,10 @@ void Tabs::set_tab_close_display_policy(CloseButtonDisplayPolicy p_policy) {
 	update();
 }
 
+void Tabs::set_min_width(int p_width) {
+	min_width = p_width;
+}
+
 void Tabs::_bind_methods() {
 
 	ClassDB::bind_method(D_METHOD("_gui_input"), &Tabs::_gui_input);
@@ -663,6 +742,7 @@ void Tabs::_bind_methods() {
 	ADD_SIGNAL(MethodInfo("tab_changed", PropertyInfo(Variant::INT, "tab")));
 	ADD_SIGNAL(MethodInfo("right_button_pressed", PropertyInfo(Variant::INT, "tab")));
 	ADD_SIGNAL(MethodInfo("tab_close", PropertyInfo(Variant::INT, "tab")));
+	ADD_SIGNAL(MethodInfo("tab_hover", PropertyInfo(Variant::INT, "tab")));
 
 	ADD_PROPERTY(PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE, "-1,4096,1", PROPERTY_USAGE_EDITOR), "set_current_tab", "get_current_tab");
 
@@ -688,4 +768,6 @@ Tabs::Tabs() {
 	cb_displaypolicy = CLOSE_BUTTON_SHOW_NEVER;
 	offset = 0;
 	max_drawn_tab = 0;
+
+	min_width = 0;
 }

+ 6 - 1
scene/gui/tabs.h

@@ -59,6 +59,7 @@ private:
 		int ofs_cache;
 		bool disabled;
 		int size_cache;
+		int size_text;
 		int x_cache;
 		int x_size_cache;
 
@@ -74,7 +75,6 @@ private:
 	bool missing_right;
 	Vector<Tab> tabs;
 	int current;
-	Control *_get_tab(int idx) const;
 	int _get_top_margin() const;
 	TabAlign tab_align;
 	int rb_hover;
@@ -85,9 +85,11 @@ private:
 	CloseButtonDisplayPolicy cb_displaypolicy;
 
 	int hover; // hovered tab
+	int min_width;
 
 	int get_tab_width(int p_idx) const;
 	void _ensure_no_over_offset();
+	void _update_cache();
 
 protected:
 	void _gui_input(const Ref<InputEvent> &p_event);
@@ -117,13 +119,16 @@ public:
 	int get_tab_count() const;
 	void set_current_tab(int p_current);
 	int get_current_tab() const;
+	int get_hovered_tab() const;
 
 	void remove_tab(int p_idx);
 
 	void clear_tabs();
 
 	void ensure_tab_visible(int p_idx);
+	void set_min_width(int p_width);
 
+	Rect2 get_tab_rect(int p_tab);
 	Size2 get_minimum_size() const;
 
 	Tabs();