Add a script method to get its class icon

Co-authored-by: Danil Alexeev <danil@alexeev.xyz>
This commit is contained in:
Yuri Sizov 2023-08-24 12:49:20 +02:00
parent 6758a7f8c0
commit 2c77f07aaa
13 changed files with 52 additions and 12 deletions

View file

@ -595,7 +595,7 @@
<return type="void" />
<param index="0" name="icon_path" type="String" />
<description>
Add a custom icon to the current script. The script must be registered as a global class using the [code]class_name[/code] keyword for this to have a visible effect. The icon specified at [param icon_path] is displayed in the Scene dock for every node of that class, as well as in various editor dialogs.
Add a custom icon to the current script. The icon specified at [param icon_path] is displayed in the Scene dock for every node of that class, as well as in various editor dialogs.
[codeblock]
@icon("res://path/to/class/icon.svg")
[/codeblock]

View file

@ -482,6 +482,10 @@ void GDScript::_clear_doc() {
docs.clear();
doc = DocData::ClassDoc();
}
String GDScript::get_class_icon_path() const {
return simplified_icon_path;
}
#endif
bool GDScript::_update_exports(bool *r_err, bool p_recursive_call, PlaceHolderScriptInstance *p_instance_to_update) {
@ -2527,13 +2531,6 @@ String GDScriptLanguage::get_global_class_name(const String &p_path, String *r_b
* Before changing this function, please ask the current maintainer of EditorFileSystem.
*/
if (r_icon_path) {
if (c->icon_path.is_empty() || c->icon_path.is_absolute_path()) {
*r_icon_path = c->icon_path.simplify_path();
} else if (c->icon_path.is_relative_path()) {
*r_icon_path = p_path.get_base_dir().path_join(c->icon_path).simplify_path();
}
}
if (r_base_type) {
const GDScriptParser::ClassNode *subclass = c;
String path = p_path;
@ -2601,6 +2598,9 @@ String GDScriptLanguage::get_global_class_name(const String &p_path, String *r_b
}
}
}
if (r_icon_path) {
*r_icon_path = c->simplified_icon_path;
}
return c->identifier != nullptr ? String(c->identifier->name) : String();
}

View file

@ -144,6 +144,7 @@ class GDScript : public Script {
String path;
String name;
String fully_qualified_name;
String simplified_icon_path;
SelfList<GDScript> script_list;
SelfList<GDScriptFunctionState>::List pending_func_states;
@ -250,6 +251,7 @@ public:
virtual Vector<DocData::ClassDoc> get_documentation() const override {
return docs;
}
virtual String get_class_icon_path() const override;
#endif // TOOLS_ENABLED
virtual Error reload(bool p_keep_state = false) override;

View file

@ -2928,6 +2928,7 @@ void GDScriptCompiler::convert_to_initializer_type(Variant &p_variant, const GDS
void GDScriptCompiler::make_scripts(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state) {
p_script->fully_qualified_name = p_class->fqcn;
p_script->name = p_class->identifier ? p_class->identifier->name : "";
p_script->simplified_icon_path = p_class->simplified_icon_path;
HashMap<StringName, Ref<GDScript>> old_subclasses;

View file

@ -3836,18 +3836,31 @@ bool GDScriptParser::tool_annotation(const AnnotationNode *p_annotation, Node *p
bool GDScriptParser::icon_annotation(const AnnotationNode *p_annotation, Node *p_node) {
ERR_FAIL_COND_V_MSG(p_node->type != Node::CLASS, false, R"("@icon" annotation can only be applied to classes.)");
ERR_FAIL_COND_V(p_annotation->resolved_arguments.is_empty(), false);
ClassNode *p_class = static_cast<ClassNode *>(p_node);
String path = p_annotation->resolved_arguments[0];
#ifdef DEBUG_ENABLED
if (!p_class->icon_path.is_empty()) {
push_error(R"("@icon" annotation can only be used once.)", p_annotation);
return false;
}
if (String(p_annotation->resolved_arguments[0]).is_empty()) {
if (path.is_empty()) {
push_error(R"("@icon" annotation argument must contain the path to the icon.)", p_annotation->arguments[0]);
return false;
}
#endif // DEBUG_ENABLED
p_class->icon_path = p_annotation->resolved_arguments[0];
p_class->icon_path = path;
if (path.is_empty() || path.is_absolute_path()) {
p_class->simplified_icon_path = path.simplify_path();
} else if (path.is_relative_path()) {
p_class->simplified_icon_path = script_path.get_base_dir().path_join(path).simplify_path();
} else {
p_class->simplified_icon_path = path;
}
return true;
}

View file

@ -724,6 +724,7 @@ public:
IdentifierNode *identifier = nullptr;
String icon_path;
String simplified_icon_path;
Vector<Member> members;
HashMap<StringName, int> members_indices;
ClassNode *outer = nullptr;