From 38579a1e847132428019dbc73d88c91eb4f08586 Mon Sep 17 00:00:00 2001 From: Haoyu Qiu Date: Fri, 27 Sep 2024 15:51:54 +0800 Subject: [PATCH] Unify editor version buttons --- editor/editor_about.cpp | 30 +--------- editor/editor_about.h | 5 -- editor/gui/editor_bottom_panel.cpp | 29 +--------- editor/gui/editor_bottom_panel.h | 3 - editor/gui/editor_version_button.cpp | 85 ++++++++++++++++++++++++++++ editor/gui/editor_version_button.h | 61 ++++++++++++++++++++ editor/project_manager.cpp | 24 +------- editor/project_manager.h | 7 --- 8 files changed, 152 insertions(+), 92 deletions(-) create mode 100644 editor/gui/editor_version_button.cpp create mode 100644 editor/gui/editor_version_button.h diff --git a/editor/editor_about.cpp b/editor/editor_about.cpp index dc943fc783c..34f432aa7e7 100644 --- a/editor/editor_about.cpp +++ b/editor/editor_about.cpp @@ -33,16 +33,12 @@ #include "core/authors.gen.h" #include "core/donors.gen.h" #include "core/license.gen.h" -#include "core/os/time.h" -#include "core/version.h" #include "editor/editor_string_names.h" +#include "editor/gui/editor_version_button.h" #include "editor/themes/editor_scale.h" #include "scene/gui/item_list.h" #include "scene/resources/style_box.h" -// The metadata key used to store and retrieve the version text to copy to the clipboard. -const String EditorAbout::META_TEXT_TO_COPY = "text_to_copy"; - void EditorAbout::_notification(int p_what) { switch (p_what) { case NOTIFICATION_THEME_CHANGED: { @@ -81,10 +77,6 @@ void EditorAbout::_license_tree_selected() { _tpl_text->set_text(selected->get_metadata(0)); } -void EditorAbout::_version_button_pressed() { - DisplayServer::get_singleton()->clipboard_set(version_btn->get_meta(META_TEXT_TO_COPY)); -} - void EditorAbout::_item_with_website_selected(int p_id, ItemList *p_il) { const String website = p_il->get_item_metadata(p_id); if (!website.is_empty()) { @@ -198,25 +190,7 @@ EditorAbout::EditorAbout() { Control *v_spacer = memnew(Control); version_info_vbc->add_child(v_spacer); - version_btn = memnew(LinkButton); - String hash = String(VERSION_HASH); - if (hash.length() != 0) { - hash = " " + vformat("[%s]", hash.left(9)); - } - version_btn->set_text(VERSION_FULL_NAME + hash); - // Set the text to copy in metadata as it slightly differs from the button's text. - version_btn->set_meta(META_TEXT_TO_COPY, "v" VERSION_FULL_BUILD + hash); - version_btn->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER); - String build_date; - if (VERSION_TIMESTAMP > 0) { - build_date = Time::get_singleton()->get_datetime_string_from_unix_time(VERSION_TIMESTAMP, true) + " UTC"; - } else { - build_date = TTR("(unknown)"); - } - version_btn->set_tooltip_text(vformat(TTR("Git commit date: %s\nClick to copy the version number."), build_date)); - - version_btn->connect(SceneStringName(pressed), callable_mp(this, &EditorAbout::_version_button_pressed)); - version_info_vbc->add_child(version_btn); + version_info_vbc->add_child(memnew(EditorVersionButton(EditorVersionButton::FORMAT_WITH_NAME_AND_BUILD))); Label *about_text = memnew(Label); about_text->set_v_size_flags(Control::SIZE_SHRINK_CENTER); diff --git a/editor/editor_about.h b/editor/editor_about.h index fc3d6cedce5..6f33d502d7e 100644 --- a/editor/editor_about.h +++ b/editor/editor_about.h @@ -33,7 +33,6 @@ #include "scene/gui/dialogs.h" #include "scene/gui/item_list.h" -#include "scene/gui/link_button.h" #include "scene/gui/rich_text_label.h" #include "scene/gui/scroll_container.h" #include "scene/gui/separator.h" @@ -49,16 +48,12 @@ class EditorAbout : public AcceptDialog { GDCLASS(EditorAbout, AcceptDialog); - static const String META_TEXT_TO_COPY; - private: void _license_tree_selected(); - void _version_button_pressed(); void _item_with_website_selected(int p_id, ItemList *p_il); void _item_list_resized(ItemList *p_il); ScrollContainer *_populate_list(const String &p_name, const List &p_sections, const char *const *const p_src[], int p_single_column_flags = 0, bool p_allow_website = false); - LinkButton *version_btn = nullptr; Tree *_tpl_tree = nullptr; RichTextLabel *license_text_label = nullptr; RichTextLabel *_tpl_text = nullptr; diff --git a/editor/gui/editor_bottom_panel.cpp b/editor/gui/editor_bottom_panel.cpp index 4b2fd9cb2f1..f6ba74fe955 100644 --- a/editor/gui/editor_bottom_panel.cpp +++ b/editor/gui/editor_bottom_panel.cpp @@ -30,8 +30,6 @@ #include "editor_bottom_panel.h" -#include "core/os/time.h" -#include "core/version.h" #include "editor/debugger/editor_debugger_node.h" #include "editor/editor_about.h" #include "editor/editor_command_palette.h" @@ -39,13 +37,10 @@ #include "editor/editor_string_names.h" #include "editor/engine_update_label.h" #include "editor/gui/editor_toaster.h" +#include "editor/gui/editor_version_button.h" #include "editor/themes/editor_scale.h" #include "scene/gui/box_container.h" #include "scene/gui/button.h" -#include "scene/gui/link_button.h" - -// The metadata key used to store and retrieve the version text to copy to the clipboard. -static const String META_TEXT_TO_COPY = "text_to_copy"; void EditorBottomPanel::_notification(int p_what) { switch (p_what) { @@ -110,10 +105,6 @@ void EditorBottomPanel::_expand_button_toggled(bool p_pressed) { EditorNode::get_top_split()->set_visible(!p_pressed); } -void EditorBottomPanel::_version_button_pressed() { - DisplayServer::get_singleton()->clipboard_set(version_btn->get_meta(META_TEXT_TO_COPY)); -} - bool EditorBottomPanel::_button_drag_hover(const Vector2 &, const Variant &, Button *p_button, Control *p_control) { if (!p_button->is_pressed()) { _switch_by_control(true, p_control); @@ -262,25 +253,9 @@ EditorBottomPanel::EditorBottomPanel() { editor_toaster = memnew(EditorToaster); bottom_hbox->add_child(editor_toaster); - version_btn = memnew(LinkButton); - version_btn->set_text(VERSION_FULL_CONFIG); - String hash = String(VERSION_HASH); - if (hash.length() != 0) { - hash = " " + vformat("[%s]", hash.left(9)); - } - // Set the text to copy in metadata as it slightly differs from the button's text. - version_btn->set_meta(META_TEXT_TO_COPY, "v" VERSION_FULL_BUILD + hash); + EditorVersionButton *version_btn = memnew(EditorVersionButton(EditorVersionButton::FORMAT_BASIC)); // Fade out the version label to be less prominent, but still readable. version_btn->set_self_modulate(Color(1, 1, 1, 0.65)); - version_btn->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER); - String build_date; - if (VERSION_TIMESTAMP > 0) { - build_date = Time::get_singleton()->get_datetime_string_from_unix_time(VERSION_TIMESTAMP, true) + " UTC"; - } else { - build_date = TTR("(unknown)"); - } - version_btn->set_tooltip_text(vformat(TTR("Git commit date: %s\nClick to copy the version information."), build_date)); - version_btn->connect(SceneStringName(pressed), callable_mp(this, &EditorBottomPanel::_version_button_pressed)); version_btn->set_v_size_flags(Control::SIZE_SHRINK_CENTER); bottom_hbox->add_child(version_btn); diff --git a/editor/gui/editor_bottom_panel.h b/editor/gui/editor_bottom_panel.h index 95c767dae5d..3d44b3750af 100644 --- a/editor/gui/editor_bottom_panel.h +++ b/editor/gui/editor_bottom_panel.h @@ -37,7 +37,6 @@ class Button; class ConfigFile; class EditorToaster; class HBoxContainer; -class LinkButton; class VBoxContainer; class EditorBottomPanel : public PanelContainer { @@ -55,14 +54,12 @@ class EditorBottomPanel : public PanelContainer { HBoxContainer *bottom_hbox = nullptr; HBoxContainer *button_hbox = nullptr; EditorToaster *editor_toaster = nullptr; - LinkButton *version_btn = nullptr; Button *expand_button = nullptr; Control *last_opened_control = nullptr; void _switch_by_control(bool p_visible, Control *p_control); void _switch_to_item(bool p_visible, int p_idx); void _expand_button_toggled(bool p_pressed); - void _version_button_pressed(); bool _button_drag_hover(const Vector2 &, const Variant &, Button *p_button, Control *p_control); diff --git a/editor/gui/editor_version_button.cpp b/editor/gui/editor_version_button.cpp new file mode 100644 index 00000000000..635d66f42a9 --- /dev/null +++ b/editor/gui/editor_version_button.cpp @@ -0,0 +1,85 @@ +/**************************************************************************/ +/* editor_version_button.cpp */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* 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 "editor_version_button.h" + +#include "core/os/time.h" +#include "core/version.h" + +String _get_version_string(EditorVersionButton::VersionFormat p_format) { + String main; + switch (p_format) { + case EditorVersionButton::FORMAT_BASIC: { + return VERSION_FULL_CONFIG; + } break; + case EditorVersionButton::FORMAT_WITH_BUILD: { + main = "v" VERSION_FULL_BUILD; + } break; + case EditorVersionButton::FORMAT_WITH_NAME_AND_BUILD: { + main = VERSION_FULL_NAME; + } break; + default: { + ERR_FAIL_V_MSG(VERSION_FULL_NAME, "Unexpected format: " + itos(p_format)); + } break; + } + + String hash = VERSION_HASH; + if (!hash.is_empty()) { + hash = vformat(" [%s]", hash.left(9)); + } + return main + hash; +} + +void EditorVersionButton::_notification(int p_what) { + switch (p_what) { + case NOTIFICATION_POSTINITIALIZE: { + // This can't be done in the constructor because theme cache is not ready yet. + set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); + set_text(_get_version_string(format)); + } break; + } +} + +void EditorVersionButton::pressed() { + DisplayServer::get_singleton()->clipboard_set(_get_version_string(FORMAT_WITH_BUILD)); +} + +EditorVersionButton::EditorVersionButton(VersionFormat p_format) { + format = p_format; + set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER); + + String build_date; + if (VERSION_TIMESTAMP > 0) { + build_date = Time::get_singleton()->get_datetime_string_from_unix_time(VERSION_TIMESTAMP, true) + " UTC"; + } else { + build_date = TTR("(unknown)"); + } + set_tooltip_text(vformat(TTR("Git commit date: %s\nClick to copy the version information."), build_date)); +} diff --git a/editor/gui/editor_version_button.h b/editor/gui/editor_version_button.h new file mode 100644 index 00000000000..591c3d483ee --- /dev/null +++ b/editor/gui/editor_version_button.h @@ -0,0 +1,61 @@ +/**************************************************************************/ +/* editor_version_button.h */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* 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. */ +/**************************************************************************/ + +#ifndef EDITOR_VERSION_BUTTON_H +#define EDITOR_VERSION_BUTTON_H + +#include "scene/gui/link_button.h" + +class EditorVersionButton : public LinkButton { + GDCLASS(EditorVersionButton, LinkButton); + +public: + enum VersionFormat { + // 4.3.2.stable + FORMAT_BASIC, + // v4.3.2.stable.mono [HASH] + FORMAT_WITH_BUILD, + // Godot Engine v4.3.2.stable.mono.official [HASH] + FORMAT_WITH_NAME_AND_BUILD, + }; + +private: + VersionFormat format = FORMAT_WITH_NAME_AND_BUILD; + +protected: + void _notification(int p_what); + + virtual void pressed() override; + +public: + EditorVersionButton(VersionFormat p_format); +}; + +#endif // EDITOR_VERSION_BUTTON_H diff --git a/editor/project_manager.cpp b/editor/project_manager.cpp index 8411c0edea1..30878a24880 100644 --- a/editor/project_manager.cpp +++ b/editor/project_manager.cpp @@ -38,7 +38,6 @@ #include "core/io/stream_peer_tls.h" #include "core/os/keyboard.h" #include "core/os/os.h" -#include "core/os/time.h" #include "core/version.h" #include "editor/editor_about.h" #include "editor/editor_settings.h" @@ -46,6 +45,7 @@ #include "editor/engine_update_label.h" #include "editor/gui/editor_file_dialog.h" #include "editor/gui/editor_title_bar.h" +#include "editor/gui/editor_version_button.h" #include "editor/plugins/asset_library_editor_plugin.h" #include "editor/project_manager/project_dialog.h" #include "editor/project_manager/project_list.h" @@ -398,12 +398,6 @@ void ProjectManager::_restart_confirmed() { get_tree()->quit(); } -// Footer. - -void ProjectManager::_version_button_pressed() { - DisplayServer::get_singleton()->clipboard_set(version_btn->get_text()); -} - // Project list. void ProjectManager::_update_list_placeholder() { @@ -1459,23 +1453,9 @@ ProjectManager::ProjectManager() { update_label->connect("offline_clicked", callable_mp(this, &ProjectManager::_show_quick_settings)); #endif - version_btn = memnew(LinkButton); - String hash = String(VERSION_HASH); - if (hash.length() != 0) { - hash = " " + vformat("[%s]", hash.left(9)); - } - version_btn->set_text("v" VERSION_FULL_BUILD + hash); + EditorVersionButton *version_btn = memnew(EditorVersionButton(EditorVersionButton::FORMAT_WITH_BUILD)); // Fade the version label to be less prominent, but still readable. version_btn->set_self_modulate(Color(1, 1, 1, 0.6)); - version_btn->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER); - String build_date; - if (VERSION_TIMESTAMP > 0) { - build_date = Time::get_singleton()->get_datetime_string_from_unix_time(VERSION_TIMESTAMP, true) + " UTC"; - } else { - build_date = TTR("(unknown)"); - } - version_btn->set_tooltip_text(vformat(TTR("Git commit date: %s\nClick to copy the version information."), build_date)); - version_btn->connect(SceneStringName(pressed), callable_mp(this, &ProjectManager::_version_button_pressed)); footer_bar->add_child(version_btn); } diff --git a/editor/project_manager.h b/editor/project_manager.h index aad51d0e985..07da0059c01 100644 --- a/editor/project_manager.h +++ b/editor/project_manager.h @@ -41,7 +41,6 @@ class EditorFileDialog; class EditorTitleBar; class HFlowContainer; class LineEdit; -class LinkButton; class MarginContainer; class OptionButton; class PanelContainer; @@ -124,12 +123,6 @@ class ProjectManager : public Control { void _show_quick_settings(); void _restart_confirmed(); - // Footer. - - LinkButton *version_btn = nullptr; - - void _version_button_pressed(); - // Project list. VBoxContainer *empty_list_placeholder = nullptr;