Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support extension icons in ScriptCreateDialog #98914

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions doc/classes/OptionButton.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@
Returns the icon of the item at index [param idx].
</description>
</method>
<method name="get_item_icon_max_width" qualifiers="const">
<return type="int" />
<param index="0" name="idx" type="int" />
<description>
Returns the maximum allowed width of the icon for the item at the given [param idx].
</description>
</method>
<method name="get_item_id" qualifiers="const">
<return type="int" />
<param index="0" name="idx" type="int" />
Expand Down Expand Up @@ -170,6 +177,14 @@
Sets the icon of the item at index [param idx].
</description>
</method>
<method name="set_item_icon_max_width">
<return type="void" />
<param index="0" name="idx" type="int" />
<param index="1" name="width" type="int" />
<description>
Sets the maximum allowed width of the icon for the item at the given [param idx]. The height is adjusted according to the icon's ratio.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be nice to link to [method PopupMenu.set_item_icon_max_width] here to have cross coverage.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 You're probably right, but if that's the case we should probably add it to all the get_item_* methods yeah? I feel like it would be weird if this was the only one that cross linked...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that's true. Let's consider it outside the scope of this PR. We could make a follow-up issue about this, though i guess it's not all that important.

</description>
</method>
<method name="set_item_id">
<return type="void" />
<param index="0" name="idx" type="int" />
Expand Down
19 changes: 18 additions & 1 deletion editor/script_create_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "editor/themes/editor_scale.h"
#include "scene/gui/grid_container.h"
#include "scene/gui/line_edit.h"
#include "scene/theme/theme_db.h"

static String _get_parent_class_of_script(const String &p_path) {
if (!ResourceLoader::exists(p_path, "Script")) {
Expand Down Expand Up @@ -128,8 +129,23 @@ void ScriptCreateDialog::_notification(int p_what) {
} break;

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 = get_editor_theme_icon(script_type);
if (!language_icon.is_valid() || language_icon == ThemeDB::get_singleton()->get_fallback_icon()) {
// The theme doesn't have an icon for this language, ask the extensions
Ref<Texture2D> extension_language_icon = ed.extension_class_get_icon(script_type);
if (extension_language_icon.is_valid()) {
language_menu->set_item_icon_max_width(i, icon_size);
language_icon = extension_language_icon;
}
}

if (language_icon.is_valid()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may not be easily doable with how the themes are set up, but don't you think a theme's script language icon should trump the extension's icon, if set explicitly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's a good point.

Debugging through the code though, I'd have to check the return of get_editor_theme_icon to see if it's the same icon as default fallback. That's probably fine? I'll see if I can get it to work.

language_menu->set_item_icon(i, language_icon);
}
Expand Down Expand Up @@ -850,6 +866,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
12 changes: 12 additions & 0 deletions scene/gui/option_button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,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 All @@ -254,6 +260,10 @@ Ref<Texture2D> OptionButton::get_item_icon(int p_idx) const {
return popup->get_item_icon(p_idx);
}

int OptionButton::get_item_icon_max_width(int p_idx) const {
return popup->get_item_icon_max_width(p_idx);
}

int OptionButton::get_item_id(int p_idx) const {
if (p_idx == NONE_SELECTED) {
return NONE_SELECTED;
Expand Down Expand Up @@ -511,12 +521,14 @@ 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", "width"), &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);
ClassDB::bind_method(D_METHOD("set_item_tooltip", "idx", "tooltip"), &OptionButton::set_item_tooltip);
ClassDB::bind_method(D_METHOD("get_item_text", "idx"), &OptionButton::get_item_text);
ClassDB::bind_method(D_METHOD("get_item_icon", "idx"), &OptionButton::get_item_icon);
ClassDB::bind_method(D_METHOD("get_item_icon_max_width", "idx"), &OptionButton::get_item_icon_max_width);
ClassDB::bind_method(D_METHOD("get_item_id", "idx"), &OptionButton::get_item_id);
ClassDB::bind_method(D_METHOD("get_item_index", "id"), &OptionButton::get_item_index);
ClassDB::bind_method(D_METHOD("get_item_metadata", "idx"), &OptionButton::get_item_metadata);
Expand Down
2 changes: 2 additions & 0 deletions scene/gui/option_button.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,15 @@ 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);
void set_item_tooltip(int p_idx, const String &p_tooltip);

String get_item_text(int p_idx) const;
Ref<Texture2D> get_item_icon(int p_idx) const;
int get_item_icon_max_width(int p_idx) const;
int get_item_id(int p_idx) const;
int get_item_index(int p_id) const;
Variant get_item_metadata(int p_idx) const;
Expand Down