Skip to content

Commit 0129ffd

Browse files
committed
Fix author names not showing up in the AssetLib
1 parent 07f4c06 commit 0129ffd

File tree

4 files changed

+72
-3
lines changed

4 files changed

+72
-3
lines changed

editor/asset_library/asset_library_editor_plugin.cpp

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ void EditorAssetLibraryItem::configure(const String &p_title, int p_asset_id, co
6565
author->set_text(p_author);
6666
author_id = p_author_id;
6767
price->set_text(p_cost);
68+
69+
_calculate_misc_links_size();
6870
}
6971

7072
void EditorAssetLibraryItem::set_image(int p_type, int p_index, const Ref<Texture2D> &p_image) {
@@ -87,10 +89,53 @@ void EditorAssetLibraryItem::_notification(int p_what) {
8789
author->add_theme_color_override("font_pressed_color", Color(0.5, 0.5, 0.5));
8890
author->add_theme_color_override("font_hover_color", Color(0.5, 0.5, 0.5));
8991
}
92+
93+
calculate_misc_links_ratio();
94+
} break;
95+
96+
case NOTIFICATION_THEME_CHANGED:
97+
case NOTIFICATION_TRANSLATION_CHANGED: {
98+
_calculate_misc_links_size();
99+
calculate_misc_links_ratio();
90100
} break;
91101
}
92102
}
93103

104+
void EditorAssetLibraryItem::_calculate_misc_links_size() {
105+
Ref<TextLine> text_buf;
106+
text_buf.instantiate();
107+
text_buf->add_string(author->get_text(), author->get_button_font(), author->get_button_font_size());
108+
author_width = text_buf->get_line_width();
109+
110+
text_buf->clear();
111+
const Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
112+
const int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
113+
text_buf->add_string(price->get_text(), font, font_size);
114+
price_width = text_buf->get_line_width();
115+
}
116+
117+
void EditorAssetLibraryItem::calculate_misc_links_ratio() {
118+
const int separators_width = 15 * EDSCALE;
119+
const float total_width = author_price_hbox->get_size().width - (separator->get_size().width + separators_width);
120+
if (total_width <= 0) {
121+
return;
122+
}
123+
124+
float ratio_left = 1;
125+
// Make the ratios a fraction bigger, to avoid unnecessary trimming.
126+
const float extra_ratio = 4.0 / total_width;
127+
128+
const float author_ratio = MIN(1, author_width / total_width);
129+
author->set_stretch_ratio(author_ratio + extra_ratio);
130+
ratio_left -= author_ratio;
131+
132+
const float price_ratio = MIN(1, price_width / total_width);
133+
price->set_stretch_ratio(price_ratio + extra_ratio);
134+
ratio_left -= price_ratio;
135+
136+
spacer->set_stretch_ratio(ratio_left);
137+
}
138+
94139
void EditorAssetLibraryItem::_asset_clicked() {
95140
emit_signal(SNAME("asset_selected"), asset_id);
96141
}
@@ -144,17 +189,19 @@ EditorAssetLibraryItem::EditorAssetLibraryItem(bool p_clickable) {
144189
category->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
145190
vb->add_child(category);
146191

147-
HBoxContainer *author_price_hbox = memnew(HBoxContainer);
192+
author_price_hbox = memnew(HBoxContainer);
148193
author_price_hbox->add_theme_constant_override("separation", 5 * EDSCALE);
149194
vb->add_child(author_price_hbox);
150195

151196
author = memnew(LinkButton);
152197
author->set_tooltip_text(TTRC("Author"));
153198
author->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
154199
author->set_accessibility_name(TTRC("Author"));
200+
author->set_h_size_flags(Control::SIZE_EXPAND_FILL);
155201
author_price_hbox->add_child(author);
156202

157-
author_price_hbox->add_child(memnew(HSeparator));
203+
separator = memnew(HSeparator);
204+
author_price_hbox->add_child(separator);
158205

159206
if (p_clickable) {
160207
author->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
@@ -178,11 +225,16 @@ EditorAssetLibraryItem::EditorAssetLibraryItem(bool p_clickable) {
178225
price->set_focus_mode(FOCUS_ACCESSIBILITY);
179226
price->add_theme_style_override(CoreStringName(normal), label_margin);
180227
price->set_tooltip_text(TTRC("License"));
228+
price->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
181229
price->set_accessibility_name(TTRC("License"));
230+
price->set_h_size_flags(Control::SIZE_EXPAND_FILL);
182231
price->set_mouse_filter(MOUSE_FILTER_PASS);
183-
184232
author_price_hbox->add_child(price);
185233

234+
spacer = memnew(Control);
235+
spacer->set_h_size_flags(Control::SIZE_EXPAND_FILL);
236+
author_price_hbox->add_child(spacer);
237+
186238
set_custom_minimum_size(Size2(250, 80) * EDSCALE);
187239
set_h_size_flags(Control::SIZE_EXPAND_FILL);
188240
}
@@ -1386,6 +1438,7 @@ void EditorAssetLibrary::_http_request_completed(int p_status, int p_code, const
13861438

13871439
EditorAssetLibraryItem *item = memnew(EditorAssetLibraryItem(true));
13881440
asset_items->add_child(item);
1441+
asset_items->connect(SceneStringName(sort_children), callable_mp(item, &EditorAssetLibraryItem::calculate_misc_links_ratio));
13891442
item->configure(r["title"], r["asset_id"], category_map[r["category_id"]], r["category_id"], r["author"], r["author_id"], r["cost"]);
13901443
item->connect("asset_selected", callable_mp(this, &EditorAssetLibrary::_select_asset));
13911444
item->connect("author_selected", callable_mp(this, &EditorAssetLibrary::_select_author));

editor/asset_library/asset_library_editor_plugin.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "scene/main/http_request.h"
4848

4949
class EditorFileDialog;
50+
class HSeparator;
5051
class MenuButton;
5152

5253
class EditorAssetLibraryItem : public PanelContainer {
@@ -57,16 +58,24 @@ class EditorAssetLibraryItem : public PanelContainer {
5758
LinkButton *category = nullptr;
5859
LinkButton *author = nullptr;
5960
Label *price = nullptr;
61+
HSeparator *separator = nullptr;
62+
Control *spacer = nullptr;
63+
HBoxContainer *author_price_hbox = nullptr;
6064

6165
String title_text;
6266
int asset_id = 0;
6367
int category_id = 0;
6468
int author_id = 0;
6569

70+
int author_width = 0;
71+
int price_width = 0;
72+
6673
void _asset_clicked();
6774
void _category_clicked();
6875
void _author_clicked();
6976

77+
void _calculate_misc_links_size();
78+
7079
void set_image(int p_type, int p_index, const Ref<Texture2D> &p_image);
7180

7281
protected:
@@ -76,6 +85,8 @@ class EditorAssetLibraryItem : public PanelContainer {
7685
public:
7786
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);
7887

88+
void calculate_misc_links_ratio();
89+
7990
EditorAssetLibraryItem(bool p_clickable = false);
8091
};
8192

scene/gui/link_button.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ Ref<Font> LinkButton::get_button_font() const {
154154
return theme_cache.font;
155155
}
156156

157+
int LinkButton::get_button_font_size() const {
158+
return theme_cache.font_size;
159+
}
160+
157161
void LinkButton::pressed() {
158162
if (uri.is_empty()) {
159163
return;

scene/gui/link_button.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ class LinkButton : public BaseButton {
108108
UnderlineMode get_underline_mode() const;
109109

110110
Ref<Font> get_button_font() const;
111+
int get_button_font_size() const;
111112

112113
LinkButton(const String &p_text = String());
113114
};

0 commit comments

Comments
 (0)