Skip to content

Commit

Permalink
Add Copy Script UID option to Script Editor
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Drozd <[email protected]>
  • Loading branch information
KoBeWi and brno32 committed Jan 11, 2025
1 parent abf8e1e commit 5c35383
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
32 changes: 25 additions & 7 deletions editor/plugins/script_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,15 @@ void ScriptEditor::_copy_script_path() {
}
}

void ScriptEditor::_copy_script_uid() {
ScriptEditorBase *se = _get_current_editor();
if (se) {
Ref<Resource> scr = se->get_edited_resource();
ResourceUID::ID uid = ResourceLoader::get_resource_uid(scr->get_path());
DisplayServer::get_singleton()->clipboard_set(ResourceUID::get_singleton()->id_to_text(uid));
}
}

void ScriptEditor::_close_other_tabs() {
int current_idx = tab_container->get_current_tab();
for (int i = tab_container->get_tab_count() - 1; i >= 0; i--) {
Expand Down Expand Up @@ -1561,6 +1570,9 @@ void ScriptEditor::_menu_option(int p_option) {
case FILE_COPY_PATH: {
_copy_script_path();
} break;
case FILE_COPY_UID: {
_copy_script_uid();
} break;
case SHOW_IN_FILE_SYSTEM: {
const Ref<Resource> scr = current->get_edited_resource();
String path = scr->get_path();
Expand Down Expand Up @@ -1706,17 +1718,19 @@ bool ScriptEditor::_has_script_tab() const {

void ScriptEditor::_prepare_file_menu() {
PopupMenu *menu = file_menu->get_popup();
const bool current_is_doc = _get_current_editor() == nullptr;
ScriptEditorBase *editor = _get_current_editor();
const Ref<Resource> res = editor ? editor->get_edited_resource() : Ref<Resource>();

menu->set_item_disabled(menu->get_item_index(FILE_REOPEN_CLOSED), previous_scripts.is_empty());

menu->set_item_disabled(menu->get_item_index(FILE_SAVE), current_is_doc);
menu->set_item_disabled(menu->get_item_index(FILE_SAVE_AS), current_is_doc);
menu->set_item_disabled(menu->get_item_index(FILE_SAVE), res.is_null());
menu->set_item_disabled(menu->get_item_index(FILE_SAVE_AS), res.is_null());
menu->set_item_disabled(menu->get_item_index(FILE_SAVE_ALL), !_has_script_tab());

menu->set_item_disabled(menu->get_item_index(FILE_TOOL_RELOAD_SOFT), current_is_doc);
menu->set_item_disabled(menu->get_item_index(FILE_COPY_PATH), current_is_doc);
menu->set_item_disabled(menu->get_item_index(SHOW_IN_FILE_SYSTEM), current_is_doc);
menu->set_item_disabled(menu->get_item_index(FILE_TOOL_RELOAD_SOFT), res.is_null());
menu->set_item_disabled(menu->get_item_index(FILE_COPY_PATH), res.is_null() || res->get_path().is_empty());
menu->set_item_disabled(menu->get_item_index(FILE_COPY_UID), res.is_null() || ResourceLoader::get_resource_uid(res->get_path()) == ResourceUID::INVALID_ID);
menu->set_item_disabled(menu->get_item_index(SHOW_IN_FILE_SYSTEM), res.is_null());

menu->set_item_disabled(menu->get_item_index(WINDOW_PREV), history_pos <= 0);
menu->set_item_disabled(menu->get_item_index(WINDOW_NEXT), history_pos >= history.size() - 1);
Expand All @@ -1726,7 +1740,7 @@ void ScriptEditor::_prepare_file_menu() {
menu->set_item_disabled(menu->get_item_index(CLOSE_OTHER_TABS), tab_container->get_tab_count() <= 1);
menu->set_item_disabled(menu->get_item_index(CLOSE_DOCS), !_has_docs_tab());

menu->set_item_disabled(menu->get_item_index(FILE_RUN), current_is_doc);
menu->set_item_disabled(menu->get_item_index(FILE_RUN), res.is_null());
}

void ScriptEditor::_file_menu_closed() {
Expand Down Expand Up @@ -3404,6 +3418,9 @@ void ScriptEditor::_make_script_list_context_menu() {
context_menu->add_separator();
}
context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/copy_path"), FILE_COPY_PATH);
context_menu->set_item_disabled(-1, se->get_edited_resource()->get_path().is_empty());
context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/copy_uid"), FILE_COPY_UID);
context_menu->set_item_disabled(-1, ResourceLoader::get_resource_uid(se->get_edited_resource()->get_path()) == ResourceUID::INVALID_ID);
context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/show_in_file_system"), SHOW_IN_FILE_SYSTEM);
context_menu->add_separator();
}
Expand Down Expand Up @@ -4248,6 +4265,7 @@ ScriptEditor::ScriptEditor(WindowWrapper *p_wrapper) {
file_menu->get_popup()->add_separator();
file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/reload_script_soft", TTRC("Soft Reload Tool Script"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT | Key::R), FILE_TOOL_RELOAD_SOFT);
file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/copy_path", TTRC("Copy Script Path")), FILE_COPY_PATH);
file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/copy_uid", TTRC("Copy Script UID")), FILE_COPY_UID);
file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/show_in_file_system", TTRC("Show in FileSystem")), SHOW_IN_FILE_SYSTEM);
file_menu->get_popup()->add_separator();

Expand Down
2 changes: 2 additions & 0 deletions editor/plugins/script_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ class ScriptEditor : public PanelContainer {
TOGGLE_SCRIPTS_PANEL,
SHOW_IN_FILE_SYSTEM,
FILE_COPY_PATH,
FILE_COPY_UID,
FILE_TOOL_RELOAD_SOFT,
SEARCH_IN_FILES,
REPLACE_IN_FILES,
Expand Down Expand Up @@ -399,6 +400,7 @@ class ScriptEditor : public PanelContainer {
void _queue_close_tabs();

void _copy_script_path();
void _copy_script_uid();

void _ask_close_current_unsaved_tab(ScriptEditorBase *current);

Expand Down

0 comments on commit 5c35383

Please sign in to comment.