1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-07 12:30:27 +00:00

Editor: Inspector and Signal docks improvements

This commit is contained in:
Danil Alexeev
2023-08-31 22:20:39 +03:00
parent 549fcce5f8
commit c33ca26e17
6 changed files with 271 additions and 145 deletions

View File

@@ -811,26 +811,30 @@ ConnectDialog::~ConnectDialog() {
// Originally copied and adapted from EditorProperty, try to keep style in sync.
Control *ConnectionsDockTree::make_custom_tooltip(const String &p_text) const {
// `p_text` is expected to be something like this:
// - `class|Control||Control brief description.`;
// - `signal|gui_input|(event: InputEvent)|gui_input description.`;
// - `../../.. :: _on_gui_input()`.
// Note that the description can be empty or contain `|`.
PackedStringArray slices = p_text.split("|", true, 3);
if (slices.size() < 4) {
return nullptr; // Use default tooltip instead.
}
String item_type = (slices[0] == "class") ? TTR("Class:") : TTR("Signal:");
String item_name = slices[1].strip_edges();
String item_params = slices[2].strip_edges();
String item_descr = slices[3].strip_edges();
String text = item_type + " [u][b]" + item_name + "[/b][/u]" + item_params + "\n";
if (item_descr.is_empty()) {
text += "[i]" + TTR("No description.") + "[/i]";
} else {
text += item_descr;
}
EditorHelpBit *help_bit = memnew(EditorHelpBit);
help_bit->get_rich_text()->set_custom_minimum_size(Size2(360 * EDSCALE, 1));
// p_text is expected to be something like this:
// "gui_input::(event: InputEvent)::<Signal description>"
// with the latter being possibly empty.
PackedStringArray slices = p_text.split("::", false);
if (slices.size() < 2) {
// Shouldn't happen here, but just in case pass the text along.
help_bit->set_text(p_text);
return help_bit;
}
String text = TTR("Signal:") + " [u][b]" + slices[0] + "[/b][/u]";
text += slices[1].strip_edges() + "\n";
if (slices.size() > 2) {
text += slices[2].strip_edges();
} else {
text += "[i]" + TTR("No description.") + "[/i]";
}
help_bit->set_text(text);
return help_bit;
@@ -960,8 +964,7 @@ void ConnectionsDock::_disconnect(const ConnectDialog::ConnectionData &p_cd) {
*/
void ConnectionsDock::_disconnect_all() {
TreeItem *item = tree->get_selected();
if (!_is_item_signal(*item)) {
if (!item || _get_item_type(*item) != TREE_ITEM_TYPE_SIGNAL) {
return;
}
@@ -990,38 +993,44 @@ void ConnectionsDock::_disconnect_all() {
void ConnectionsDock::_tree_item_selected() {
TreeItem *item = tree->get_selected();
if (!item) { // Unlikely. Disable button just in case.
connect_button->set_text(TTR("Connect..."));
connect_button->set_icon(get_theme_icon(SNAME("Instance"), SNAME("EditorIcons")));
connect_button->set_disabled(true);
} else if (_is_item_signal(*item)) {
if (item && _get_item_type(*item) == TREE_ITEM_TYPE_SIGNAL) {
connect_button->set_text(TTR("Connect..."));
connect_button->set_icon(get_theme_icon(SNAME("Instance"), SNAME("EditorIcons")));
connect_button->set_disabled(false);
} else {
} else if (item && _get_item_type(*item) == TREE_ITEM_TYPE_CONNECTION) {
connect_button->set_text(TTR("Disconnect"));
connect_button->set_icon(get_theme_icon(SNAME("Unlinked"), SNAME("EditorIcons")));
connect_button->set_disabled(false);
} else {
connect_button->set_text(TTR("Connect..."));
connect_button->set_icon(get_theme_icon(SNAME("Instance"), SNAME("EditorIcons")));
connect_button->set_disabled(true);
}
}
void ConnectionsDock::_tree_item_activated() { // "Activation" on double-click.
TreeItem *item = tree->get_selected();
if (!item) {
return;
}
if (_is_item_signal(*item)) {
if (_get_item_type(*item) == TREE_ITEM_TYPE_SIGNAL) {
_open_connection_dialog(*item);
} else {
_go_to_script(*item);
} else if (_get_item_type(*item) == TREE_ITEM_TYPE_CONNECTION) {
_go_to_method(*item);
}
}
bool ConnectionsDock::_is_item_signal(TreeItem &p_item) {
return (p_item.get_parent() == tree->get_root() || p_item.get_parent()->get_parent() == tree->get_root());
ConnectionsDock::TreeItemType ConnectionsDock::_get_item_type(const TreeItem &p_item) const {
if (&p_item == tree->get_root()) {
return TREE_ITEM_TYPE_ROOT;
} else if (p_item.get_parent() == tree->get_root()) {
return TREE_ITEM_TYPE_CLASS;
} else if (p_item.get_parent()->get_parent() == tree->get_root()) {
return TREE_ITEM_TYPE_SIGNAL;
} else {
return TREE_ITEM_TYPE_CONNECTION;
}
}
bool ConnectionsDock::_is_connection_inherited(Connection &p_connection) {
@@ -1077,8 +1086,8 @@ void ConnectionsDock::_open_edit_connection_dialog(TreeItem &p_item) {
/*
* Open slot method location in script editor.
*/
void ConnectionsDock::_go_to_script(TreeItem &p_item) {
if (_is_item_signal(p_item)) {
void ConnectionsDock::_go_to_method(TreeItem &p_item) {
if (_get_item_type(p_item) != TREE_ITEM_TYPE_CONNECTION) {
return;
}
@@ -1101,27 +1110,39 @@ void ConnectionsDock::_go_to_script(TreeItem &p_item) {
}
}
void ConnectionsDock::_handle_class_menu_option(int p_option) {
switch (p_option) {
case CLASS_MENU_OPEN_DOCS:
ScriptEditor::get_singleton()->goto_help("class:" + class_menu_doc_class_name);
EditorNode::get_singleton()->set_visible_editor(EditorNode::EDITOR_SCRIPT);
break;
}
}
void ConnectionsDock::_class_menu_about_to_popup() {
class_menu->set_item_disabled(class_menu->get_item_index(CLASS_MENU_OPEN_DOCS), class_menu_doc_class_name.is_empty());
}
void ConnectionsDock::_handle_signal_menu_option(int p_option) {
TreeItem *item = tree->get_selected();
if (!item) {
if (!item || _get_item_type(*item) != TREE_ITEM_TYPE_SIGNAL) {
return;
}
Dictionary meta = item->get_metadata(0);
switch (p_option) {
case CONNECT: {
case SIGNAL_MENU_CONNECT: {
_open_connection_dialog(*item);
} break;
case DISCONNECT_ALL: {
case SIGNAL_MENU_DISCONNECT_ALL: {
disconnect_all_dialog->set_text(vformat(TTR("Are you sure you want to remove all connections from the \"%s\" signal?"), meta["name"]));
disconnect_all_dialog->popup_centered();
} break;
case COPY_NAME: {
case SIGNAL_MENU_COPY_NAME: {
DisplayServer::get_singleton()->clipboard_set(meta["name"]);
} break;
case OPEN_DOCUMENTATION: {
case SIGNAL_MENU_OPEN_DOCS: {
ScriptEditor::get_singleton()->goto_help("class_signal:" + String(meta["class"]) + ":" + String(meta["name"]));
EditorNode::get_singleton()->set_visible_editor(EditorNode::EDITOR_SCRIPT);
} break;
@@ -1130,8 +1151,7 @@ void ConnectionsDock::_handle_signal_menu_option(int p_option) {
void ConnectionsDock::_signal_menu_about_to_popup() {
TreeItem *item = tree->get_selected();
if (!item) {
if (!item || _get_item_type(*item) != TREE_ITEM_TYPE_SIGNAL) {
return;
}
@@ -1144,25 +1164,24 @@ void ConnectionsDock::_signal_menu_about_to_popup() {
}
}
signal_menu->set_item_disabled(signal_menu->get_item_index(DISCONNECT_ALL), disable_disconnect_all);
signal_menu->set_item_disabled(signal_menu->get_item_index(OPEN_DOCUMENTATION), String(meta["class"]).is_empty());
signal_menu->set_item_disabled(signal_menu->get_item_index(SIGNAL_MENU_DISCONNECT_ALL), disable_disconnect_all);
signal_menu->set_item_disabled(signal_menu->get_item_index(SIGNAL_MENU_OPEN_DOCS), String(meta["class"]).is_empty());
}
void ConnectionsDock::_handle_slot_menu_option(int p_option) {
TreeItem *item = tree->get_selected();
if (!item) {
if (!item || _get_item_type(*item) != TREE_ITEM_TYPE_CONNECTION) {
return;
}
switch (p_option) {
case EDIT: {
case SLOT_MENU_EDIT: {
_open_edit_connection_dialog(*item);
} break;
case GO_TO_SCRIPT: {
_go_to_script(*item);
case SLOT_MENU_GO_TO_METHOD: {
_go_to_method(*item);
} break;
case DISCONNECT: {
case SLOT_MENU_DISCONNECT: {
Connection connection = item->get_metadata(0);
_disconnect(connection);
update_tree();
@@ -1171,33 +1190,50 @@ void ConnectionsDock::_handle_slot_menu_option(int p_option) {
}
void ConnectionsDock::_slot_menu_about_to_popup() {
bool connection_is_inherited = tree->get_selected()->has_meta("_inherited_connection");
slot_menu->set_item_disabled(slot_menu->get_item_index(EDIT), connection_is_inherited);
slot_menu->set_item_disabled(slot_menu->get_item_index(DISCONNECT), connection_is_inherited);
}
void ConnectionsDock::_rmb_pressed(Vector2 p_position, MouseButton p_button) {
if (p_button != MouseButton::RIGHT) {
TreeItem *item = tree->get_selected();
if (!item || _get_item_type(*item) != TREE_ITEM_TYPE_CONNECTION) {
return;
}
TreeItem *item = tree->get_selected();
bool connection_is_inherited = item->has_meta("_inherited_connection");
slot_menu->set_item_disabled(slot_menu->get_item_index(SLOT_MENU_EDIT), connection_is_inherited);
slot_menu->set_item_disabled(slot_menu->get_item_index(SLOT_MENU_DISCONNECT), connection_is_inherited);
}
void ConnectionsDock::_rmb_pressed(const Ref<InputEvent> &p_event) {
const Ref<InputEventMouseButton> &mb_event = p_event;
if (mb_event.is_null() || !mb_event->is_pressed() || mb_event->get_button_index() != MouseButton::RIGHT) {
return;
}
TreeItem *item = tree->get_item_at_position(mb_event->get_position());
if (!item) {
return;
}
Vector2 screen_position = tree->get_screen_position() + p_position;
Vector2 screen_position = tree->get_screen_position() + mb_event->get_position();
if (_is_item_signal(*item)) {
signal_menu->set_position(screen_position);
signal_menu->reset_size();
signal_menu->popup();
} else {
slot_menu->set_position(screen_position);
slot_menu->reset_size();
slot_menu->popup();
switch (_get_item_type(*item)) {
case TREE_ITEM_TYPE_ROOT:
break;
case TREE_ITEM_TYPE_CLASS:
class_menu_doc_class_name = item->get_metadata(0);
class_menu->set_position(screen_position);
class_menu->reset_size();
class_menu->popup();
accept_event(); // Don't collapse item.
break;
case TREE_ITEM_TYPE_SIGNAL:
signal_menu->set_position(screen_position);
signal_menu->reset_size();
signal_menu->popup();
break;
case TREE_ITEM_TYPE_CONNECTION:
slot_menu->set_position(screen_position);
slot_menu->reset_size();
slot_menu->popup();
break;
}
}
@@ -1212,9 +1248,9 @@ void ConnectionsDock::_connect_pressed() {
return;
}
if (_is_item_signal(*item)) {
if (_get_item_type(*item) == TREE_ITEM_TYPE_SIGNAL) {
_open_connection_dialog(*item);
} else {
} else if (_get_item_type(*item) == TREE_ITEM_TYPE_CONNECTION) {
Connection connection = item->get_metadata(0);
_disconnect(connection);
update_tree();
@@ -1227,14 +1263,16 @@ void ConnectionsDock::_notification(int p_what) {
case NOTIFICATION_THEME_CHANGED: {
search_box->set_right_icon(get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
signal_menu->set_item_icon(signal_menu->get_item_index(CONNECT), get_theme_icon(SNAME("Instance"), SNAME("EditorIcons")));
signal_menu->set_item_icon(signal_menu->get_item_index(DISCONNECT_ALL), get_theme_icon(SNAME("Unlinked"), SNAME("EditorIcons")));
signal_menu->set_item_icon(signal_menu->get_item_index(COPY_NAME), get_theme_icon(SNAME("ActionCopy"), SNAME("EditorIcons")));
signal_menu->set_item_icon(signal_menu->get_item_index(OPEN_DOCUMENTATION), get_theme_icon(SNAME("Help"), SNAME("EditorIcons")));
class_menu->set_item_icon(class_menu->get_item_index(CLASS_MENU_OPEN_DOCS), get_theme_icon(SNAME("Help"), SNAME("EditorIcons")));
slot_menu->set_item_icon(slot_menu->get_item_index(EDIT), get_theme_icon(SNAME("Edit"), SNAME("EditorIcons")));
slot_menu->set_item_icon(slot_menu->get_item_index(GO_TO_SCRIPT), get_theme_icon(SNAME("ArrowRight"), SNAME("EditorIcons")));
slot_menu->set_item_icon(slot_menu->get_item_index(DISCONNECT), get_theme_icon(SNAME("Unlinked"), SNAME("EditorIcons")));
signal_menu->set_item_icon(signal_menu->get_item_index(SIGNAL_MENU_CONNECT), get_theme_icon(SNAME("Instance"), SNAME("EditorIcons")));
signal_menu->set_item_icon(signal_menu->get_item_index(SIGNAL_MENU_DISCONNECT_ALL), get_theme_icon(SNAME("Unlinked"), SNAME("EditorIcons")));
signal_menu->set_item_icon(signal_menu->get_item_index(SIGNAL_MENU_COPY_NAME), get_theme_icon(SNAME("ActionCopy"), SNAME("EditorIcons")));
signal_menu->set_item_icon(signal_menu->get_item_index(SIGNAL_MENU_OPEN_DOCS), get_theme_icon(SNAME("Help"), SNAME("EditorIcons")));
slot_menu->set_item_icon(slot_menu->get_item_index(SLOT_MENU_EDIT), get_theme_icon(SNAME("Edit"), SNAME("EditorIcons")));
slot_menu->set_item_icon(slot_menu->get_item_index(SLOT_MENU_GO_TO_METHOD), get_theme_icon(SNAME("ArrowRight"), SNAME("EditorIcons")));
slot_menu->set_item_icon(slot_menu->get_item_index(SLOT_MENU_DISCONNECT), get_theme_icon(SNAME("Unlinked"), SNAME("EditorIcons")));
} break;
case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
@@ -1272,6 +1310,7 @@ void ConnectionsDock::update_tree() {
while (native_base != StringName()) {
String class_name;
String doc_class_name;
String class_brief;
Ref<Texture2D> class_icon;
List<MethodInfo> class_signals;
@@ -1293,6 +1332,7 @@ void ConnectionsDock::update_tree() {
}
HashMap<String, DocData::ClassDoc>::ConstIterator F = doc_data->class_list.find(doc_class_name);
if (F) {
class_brief = F->value.brief_description;
for (int i = 0; i < F->value.signals.size(); i++) {
descr_cache[doc_class_name][F->value.signals[i].name] = F->value.signals[i].description;
}
@@ -1329,16 +1369,17 @@ void ConnectionsDock::update_tree() {
class_name = native_base;
doc_class_name = class_name;
// For a native class, the cache is filled once.
if (!descr_cache.has(doc_class_name)) {
HashMap<String, DocData::ClassDoc>::ConstIterator F = doc_data->class_list.find(doc_class_name);
if (F) {
HashMap<String, DocData::ClassDoc>::ConstIterator F = doc_data->class_list.find(doc_class_name);
if (F) {
class_brief = DTR(F->value.brief_description);
// For a native class, the cache is filled once.
if (!descr_cache.has(doc_class_name)) {
for (int i = 0; i < F->value.signals.size(); i++) {
descr_cache[doc_class_name][F->value.signals[i].name] = DTR(F->value.signals[i].description);
}
} else {
doc_class_name = String();
}
} else {
doc_class_name = String();
}
if (has_theme_icon(native_base, SNAME("EditorIcons"))) {
@@ -1362,14 +1403,17 @@ void ConnectionsDock::update_tree() {
section_item = tree->create_item(root);
section_item->set_text(0, class_name);
// `|` separators used in `make_custom_tooltip()` for formatting.
section_item->set_tooltip_text(0, "class|" + class_name + "||" + class_brief);
section_item->set_icon(0, class_icon);
section_item->set_selectable(0, false);
section_item->set_editable(0, false);
section_item->set_custom_bg_color(0, get_theme_color(SNAME("prop_subsection"), SNAME("Editor")));
section_item->set_metadata(0, doc_class_name);
}
for (MethodInfo &mi : class_signals) {
const StringName signal_name = mi.name;
const StringName &signal_name = mi.name;
if (!search_box->get_text().is_subsequence_ofn(signal_name)) {
continue;
}
@@ -1404,8 +1448,8 @@ void ConnectionsDock::update_tree() {
}
}
// "::" separators used in make_custom_tooltip for formatting.
signal_item->set_tooltip_text(0, String(signal_name) + "::" + signame.trim_prefix(mi.name) + "::" + descr);
// `|` separators used in `make_custom_tooltip()` for formatting.
signal_item->set_tooltip_text(0, "signal|" + String(signal_name) + "|" + signame.trim_prefix(mi.name) + "|" + descr);
}
// List existing connections.
@@ -1500,28 +1544,34 @@ ConnectionsDock::ConnectionsDock() {
disconnect_all_dialog->connect("confirmed", callable_mp(this, &ConnectionsDock::_disconnect_all));
disconnect_all_dialog->set_text(TTR("Are you sure you want to remove all connections from this signal?"));
class_menu = memnew(PopupMenu);
class_menu->connect("id_pressed", callable_mp(this, &ConnectionsDock::_handle_class_menu_option));
class_menu->connect("about_to_popup", callable_mp(this, &ConnectionsDock::_class_menu_about_to_popup));
class_menu->add_item(TTR("Open Documentation"), CLASS_MENU_OPEN_DOCS);
add_child(class_menu);
signal_menu = memnew(PopupMenu);
add_child(signal_menu);
signal_menu->connect("id_pressed", callable_mp(this, &ConnectionsDock::_handle_signal_menu_option));
signal_menu->connect("about_to_popup", callable_mp(this, &ConnectionsDock::_signal_menu_about_to_popup));
signal_menu->add_item(TTR("Connect..."), CONNECT);
signal_menu->add_item(TTR("Disconnect All"), DISCONNECT_ALL);
signal_menu->add_item(TTR("Copy Name"), COPY_NAME);
signal_menu->add_item(TTR("Connect..."), SIGNAL_MENU_CONNECT);
signal_menu->add_item(TTR("Disconnect All"), SIGNAL_MENU_DISCONNECT_ALL);
signal_menu->add_item(TTR("Copy Name"), SIGNAL_MENU_COPY_NAME);
signal_menu->add_separator();
signal_menu->add_item(TTR("Open Documentation"), OPEN_DOCUMENTATION);
signal_menu->add_item(TTR("Open Documentation"), SIGNAL_MENU_OPEN_DOCS);
add_child(signal_menu);
slot_menu = memnew(PopupMenu);
add_child(slot_menu);
slot_menu->connect("id_pressed", callable_mp(this, &ConnectionsDock::_handle_slot_menu_option));
slot_menu->connect("about_to_popup", callable_mp(this, &ConnectionsDock::_slot_menu_about_to_popup));
slot_menu->add_item(TTR("Edit..."), EDIT);
slot_menu->add_item(TTR("Go to Method"), GO_TO_SCRIPT);
slot_menu->add_item(TTR("Disconnect"), DISCONNECT);
slot_menu->add_item(TTR("Edit..."), SLOT_MENU_EDIT);
slot_menu->add_item(TTR("Go to Method"), SLOT_MENU_GO_TO_METHOD);
slot_menu->add_item(TTR("Disconnect"), SLOT_MENU_DISCONNECT);
add_child(slot_menu);
connect_dialog->connect("connected", callable_mp(this, &ConnectionsDock::_make_or_edit_connection));
tree->connect("item_selected", callable_mp(this, &ConnectionsDock::_tree_item_selected));
tree->connect("item_activated", callable_mp(this, &ConnectionsDock::_tree_item_activated));
tree->connect("item_mouse_selected", callable_mp(this, &ConnectionsDock::_rmb_pressed));
tree->connect("gui_input", callable_mp(this, &ConnectionsDock::_rmb_pressed));
add_theme_constant_override("separation", 3 * EDSCALE);
}