From 3c3fad858cba4a11046d2e35b129f589c631406a Mon Sep 17 00:00:00 2001 From: GrammAcc Date: Sat, 30 Sep 2023 18:16:39 -0500 Subject: [PATCH] AssetLib: Fix long plugin names breaking the UI The UI was extending past the screen width when loading a page diplaying a plugin with an especially long title in the asset store plugin. I implemented a new `EditorAssetLibraryItem::clamp_width` method that checks that the title text is not longer than the column width minus some padding and truncates it if it is. I also noticed that the nav buttons for paginated results were causing the UI to extend past the screen width on higher editor scales since they were hardcoded to show ten page buttons if there were enough results. I modified the pagination slightly to display a dynamic number of nav buttons based on the editor scale in order to fix this other cause of the same problem. I had to use the font of the `title`, which is a `LinkButton` in order to determine the text width, so I added a public getter `get_button_font` to the `LinkButton` class. (cherry picked from commit d63a88bef1e10138741b6666def5b90460d6ba82) --- .../plugins/asset_library_editor_plugin.cpp | 22 +++++++++++++++++-- editor/plugins/asset_library_editor_plugin.h | 4 ++++ scene/gui/link_button.cpp | 4 ++++ scene/gui/link_button.h | 2 ++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/editor/plugins/asset_library_editor_plugin.cpp b/editor/plugins/asset_library_editor_plugin.cpp index eeb0fd5f664..ab81f96da7b 100644 --- a/editor/plugins/asset_library_editor_plugin.cpp +++ b/editor/plugins/asset_library_editor_plugin.cpp @@ -67,6 +67,21 @@ void EditorAssetLibraryItem::configure(const String &p_title, int p_asset_id, co price->set_text(p_cost); } +// TODO: Refactor this method to use the TextServer. +void EditorAssetLibraryItem::clamp_width(int p_max_width) { + int text_pixel_width = title->get_button_font().ptr()->get_string_size(title->get_text()).x * EDSCALE; + + String full_text = title->get_text(); + title->set_tooltip_text(full_text); + + if (text_pixel_width > p_max_width) { + // Truncate title text to within the current column width. + int max_length = p_max_width / (text_pixel_width / full_text.length()); + String truncated_text = full_text.left(max_length - 3) + "..."; + title->set_text(truncated_text); + } +} + void EditorAssetLibraryItem::set_image(int p_type, int p_index, const Ref &p_image) { ERR_FAIL_COND(p_type != EditorAssetLibrary::IMAGE_QUEUE_ICON); ERR_FAIL_COND(p_index != 0); @@ -1020,11 +1035,11 @@ HBoxContainer *EditorAssetLibrary::_make_pages(int p_page, int p_page_count, int } //do the mario - int from = p_page - 5; + int from = p_page - (5 / EDSCALE); if (from < 0) { from = 0; } - int to = from + 10; + int to = from + (10 / EDSCALE); if (to > p_page_count) { to = p_page_count; } @@ -1291,6 +1306,7 @@ void EditorAssetLibrary::_http_request_completed(int p_status, int p_code, const EditorAssetLibraryItem *item = memnew(EditorAssetLibraryItem); asset_items->add_child(item); item->configure(r["title"], r["asset_id"], category_map[r["category_id"]], r["category_id"], r["author"], r["author_id"], r["cost"]); + item->clamp_width(asset_items_column_width); item->connect("asset_selected", callable_mp(this, &EditorAssetLibrary::_select_asset)); item->connect("author_selected", callable_mp(this, &EditorAssetLibrary::_select_author)); item->connect("category_selected", callable_mp(this, &EditorAssetLibrary::_select_category)); @@ -1425,6 +1441,8 @@ void EditorAssetLibrary::_update_asset_items_columns() { if (new_columns != asset_items->get_columns()) { asset_items->set_columns(new_columns); } + + asset_items_column_width = (get_size().x / new_columns) - (100 * EDSCALE); } void EditorAssetLibrary::disable_community_support() { diff --git a/editor/plugins/asset_library_editor_plugin.h b/editor/plugins/asset_library_editor_plugin.h index 8c74da0e2a0..2dff1869ef2 100644 --- a/editor/plugins/asset_library_editor_plugin.h +++ b/editor/plugins/asset_library_editor_plugin.h @@ -80,6 +80,8 @@ protected: public: void configure(const String &p_title, int p_asset_id, const String &p_category, int p_category_id, const String &p_author, int p_author_id, const String &p_cost); + void clamp_width(int p_max_width); + EditorAssetLibraryItem(); }; @@ -305,6 +307,8 @@ class EditorAssetLibrary : public PanelContainer { void _install_external_asset(String p_zip_path, String p_title); + int asset_items_column_width = 0; + void _update_asset_items_columns(); friend class EditorAssetLibraryItemDescription; diff --git a/scene/gui/link_button.cpp b/scene/gui/link_button.cpp index b5797919902..1389148d300 100644 --- a/scene/gui/link_button.cpp +++ b/scene/gui/link_button.cpp @@ -129,6 +129,10 @@ LinkButton::UnderlineMode LinkButton::get_underline_mode() const { return underline_mode; } +Ref LinkButton::get_button_font() const { + return theme_cache.font; +} + void LinkButton::pressed() { if (uri.is_empty()) { return; diff --git a/scene/gui/link_button.h b/scene/gui/link_button.h index e3d6ef2c5b5..c4557d0a373 100644 --- a/scene/gui/link_button.h +++ b/scene/gui/link_button.h @@ -104,6 +104,8 @@ public: void set_underline_mode(UnderlineMode p_underline_mode); UnderlineMode get_underline_mode() const; + Ref get_button_font() const; + LinkButton(const String &p_text = String()); };