Skip to content

Commit

Permalink
Support extension icons in ScriptCreateDialog
Browse files Browse the repository at this point in the history
Attempt to grab the icon for a script type from an extension first, before falling back to the theme.

This adds a method to option_button to allow the ScriptCreateDialog to set the max size of an item icon, so that it can set the size from the Editor Theme when reading the icon from extension.
  • Loading branch information
fuzzybinary committed Nov 7, 2024
1 parent 2e7fc81 commit 4f8db25
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
15 changes: 14 additions & 1 deletion editor/script_create_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,22 @@ void ScriptCreateDialog::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE:
case NOTIFICATION_THEME_CHANGED: {
const int icon_size = get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));

EditorData &ed = EditorNode::get_editor_data();

for (int i = 0; i < ScriptServer::get_language_count(); i++) {
Ref<Texture2D> language_icon = get_editor_theme_icon(ScriptServer::get_language(i)->get_type());
// Check if the extension has an icon first
String script_type = ScriptServer::get_language(i)->get_type();
Ref<Texture2D> language_icon = ed.extension_class_get_icon(script_type);
if (language_icon.is_valid()) {
language_menu->set_item_icon(i, language_icon);
language_menu->set_item_icon_max_width(i, icon_size);
} else {
language_icon = get_editor_theme_icon(script_type);
if (language_icon.is_valid()) {
language_menu->set_item_icon(i, language_icon);
}
}
}

Expand Down Expand Up @@ -881,6 +893,7 @@ ScriptCreateDialog::ScriptCreateDialog() {

language_menu = memnew(OptionButton);
language_menu->set_custom_minimum_size(Size2(350, 0) * EDSCALE);
language_menu->set_expand_icon(true);
language_menu->set_h_size_flags(Control::SIZE_EXPAND_FILL);
gc->add_child(memnew(Label(TTR("Language:"))));
gc->add_child(language_menu);
Expand Down
7 changes: 7 additions & 0 deletions scene/gui/option_button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@ void OptionButton::set_item_icon(int p_idx, const Ref<Texture2D> &p_icon) {
_queue_update_size_cache();
}

void OptionButton::set_item_icon_max_width(int p_idx, int p_width) {
popup->set_item_icon_max_width(p_idx, p_width);

_queue_update_size_cache();
}

void OptionButton::set_item_id(int p_idx, int p_id) {
popup->set_item_id(p_idx, p_id);
}
Expand Down Expand Up @@ -550,6 +556,7 @@ void OptionButton::_bind_methods() {
ClassDB::bind_method(D_METHOD("add_icon_item", "texture", "label", "id"), &OptionButton::add_icon_item, DEFVAL(-1));
ClassDB::bind_method(D_METHOD("set_item_text", "idx", "text"), &OptionButton::set_item_text);
ClassDB::bind_method(D_METHOD("set_item_icon", "idx", "texture"), &OptionButton::set_item_icon);
ClassDB::bind_method(D_METHOD("set_item_icon_max_width", "idx", "texture"), &OptionButton::set_item_icon_max_width);
ClassDB::bind_method(D_METHOD("set_item_disabled", "idx", "disabled"), &OptionButton::set_item_disabled);
ClassDB::bind_method(D_METHOD("set_item_id", "idx", "id"), &OptionButton::set_item_id);
ClassDB::bind_method(D_METHOD("set_item_metadata", "idx", "metadata"), &OptionButton::set_item_metadata);
Expand Down
1 change: 1 addition & 0 deletions scene/gui/option_button.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class OptionButton : public Button {

void set_item_text(int p_idx, const String &p_text);
void set_item_icon(int p_idx, const Ref<Texture2D> &p_icon);
void set_item_icon_max_width(int p_idx, int p_width);
void set_item_id(int p_idx, int p_id);
void set_item_metadata(int p_idx, const Variant &p_metadata);
void set_item_disabled(int p_idx, bool p_disabled);
Expand Down

0 comments on commit 4f8db25

Please sign in to comment.