Disable signal callback generation in C#

This commit is contained in:
Paul Joannon 2024-02-04 19:08:41 +01:00
parent d3352813ea
commit 9fa2355cef
No known key found for this signature in database
GPG Key ID: C12F69B0AD0390DD
10 changed files with 50 additions and 18 deletions

View File

@ -256,6 +256,7 @@ public:
virtual bool can_inherit_from_file() const { return false; } virtual bool can_inherit_from_file() const { return false; }
virtual int find_function(const String &p_function, const String &p_code) const = 0; virtual int find_function(const String &p_function, const String &p_code) const = 0;
virtual String make_function(const String &p_class, const String &p_name, const PackedStringArray &p_args) const = 0; virtual String make_function(const String &p_class, const String &p_name, const PackedStringArray &p_args) const = 0;
virtual bool can_make_function() const { return true; }
virtual Error open_in_external_editor(const Ref<Script> &p_script, int p_line, int p_col) { return ERR_UNAVAILABLE; } virtual Error open_in_external_editor(const Ref<Script> &p_script, int p_line, int p_col) { return ERR_UNAVAILABLE; }
virtual bool overrides_external_editor() { return false; } virtual bool overrides_external_editor() { return false; }

View File

@ -109,6 +109,7 @@ void ScriptLanguageExtension::_bind_methods() {
GDVIRTUAL_BIND(_can_inherit_from_file); GDVIRTUAL_BIND(_can_inherit_from_file);
GDVIRTUAL_BIND(_find_function, "function", "code"); GDVIRTUAL_BIND(_find_function, "function", "code");
GDVIRTUAL_BIND(_make_function, "class_name", "function_name", "function_args"); GDVIRTUAL_BIND(_make_function, "class_name", "function_name", "function_args");
GDVIRTUAL_BIND(_can_make_function);
GDVIRTUAL_BIND(_open_in_external_editor, "script", "line", "column"); GDVIRTUAL_BIND(_open_in_external_editor, "script", "line", "column");
GDVIRTUAL_BIND(_overrides_external_editor); GDVIRTUAL_BIND(_overrides_external_editor);

View File

@ -373,6 +373,7 @@ public:
EXBIND2RC(int, find_function, const String &, const String &) EXBIND2RC(int, find_function, const String &, const String &)
EXBIND3RC(String, make_function, const String &, const String &, const PackedStringArray &) EXBIND3RC(String, make_function, const String &, const String &, const PackedStringArray &)
EXBIND0RC(bool, can_make_function)
EXBIND3R(Error, open_in_external_editor, const Ref<Script> &, int, int) EXBIND3R(Error, open_in_external_editor, const Ref<Script> &, int, int)
EXBIND0R(bool, overrides_external_editor) EXBIND0R(bool, overrides_external_editor)

View File

@ -34,6 +34,11 @@
<description> <description>
</description> </description>
</method> </method>
<method name="_can_make_function" qualifiers="virtual const">
<return type="bool" />
<description>
</description>
</method>
<method name="_complete_code" qualifiers="virtual const"> <method name="_complete_code" qualifiers="virtual const">
<return type="Dictionary" /> <return type="Dictionary" />
<param index="0" name="code" type="String" /> <param index="0" name="code" type="String" />

View File

@ -178,6 +178,7 @@ void ConnectDialog::_tree_node_selected() {
set_dst_method(generate_method_callback_name(source, signal, current)); set_dst_method(generate_method_callback_name(source, signal, current));
} }
_update_method_tree(); _update_method_tree();
_update_warning_label();
_update_ok_enabled(); _update_ok_enabled();
} }
@ -433,6 +434,23 @@ void ConnectDialog::_update_ok_enabled() {
get_ok_button()->set_disabled(false); get_ok_button()->set_disabled(false);
} }
void ConnectDialog::_update_warning_label() {
Ref<Script> scr = source->get_node(dst_path)->get_script();
if (scr.is_null()) {
warning_label->set_visible(false);
return;
}
ScriptLanguage *language = scr->get_language();
if (language->can_make_function()) {
warning_label->set_visible(false);
return;
}
warning_label->set_text(vformat(TTR("%s: Callback code won't be generated, please add it manually."), language->get_name()));
warning_label->set_visible(true);
}
void ConnectDialog::_notification(int p_what) { void ConnectDialog::_notification(int p_what) {
switch (p_what) { switch (p_what) {
case NOTIFICATION_ENTER_TREE: { case NOTIFICATION_ENTER_TREE: {
@ -617,6 +635,7 @@ void ConnectDialog::init(const ConnectionData &p_cd, const PackedStringArray &p_
void ConnectDialog::popup_dialog(const String p_for_signal) { void ConnectDialog::popup_dialog(const String p_for_signal) {
from_signal->set_text(p_for_signal); from_signal->set_text(p_for_signal);
warning_label->add_theme_color_override("font_color", warning_label->get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
error_label->add_theme_color_override("font_color", error_label->get_theme_color(SNAME("error_color"), EditorStringName(Editor))); error_label->add_theme_color_override("font_color", error_label->get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
filter_nodes->clear(); filter_nodes->clear();
@ -694,6 +713,10 @@ ConnectDialog::ConnectDialog() {
connect_to_label = Object::cast_to<Label>(vbc_left->get_child(mc->get_index() - 1)); connect_to_label = Object::cast_to<Label>(vbc_left->get_child(mc->get_index() - 1));
vbc_left->add_child(tree); vbc_left->add_child(tree);
warning_label = memnew(Label);
vbc_left->add_child(warning_label);
warning_label->hide();
error_label = memnew(Label); error_label = memnew(Label);
error_label->set_text(TTR("Scene does not contain any script.")); error_label->set_text(TTR("Scene does not contain any script."));
vbc_left->add_child(error_label); vbc_left->add_child(error_label);

View File

@ -134,6 +134,7 @@ private:
CheckButton *advanced = nullptr; CheckButton *advanced = nullptr;
Vector<Control *> bind_controls; Vector<Control *> bind_controls;
Label *warning_label = nullptr;
Label *error_label = nullptr; Label *error_label = nullptr;
void ok_pressed() override; void ok_pressed() override;
@ -155,6 +156,7 @@ private:
void _remove_bind(); void _remove_bind();
void _advanced_pressed(); void _advanced_pressed();
void _update_ok_enabled(); void _update_ok_enabled();
void _update_warning_label();
protected: protected:
void _notification(int p_what); void _notification(int p_what);

View File

@ -2726,6 +2726,10 @@ void ScriptEditor::_add_callback(Object *p_obj, const String &p_function, const
Ref<Script> scr = p_obj->get_script(); Ref<Script> scr = p_obj->get_script();
ERR_FAIL_COND(!scr.is_valid()); ERR_FAIL_COND(!scr.is_valid());
if (!scr->get_language()->can_make_function()) {
return;
}
EditorNode::get_singleton()->push_item(scr.ptr()); EditorNode::get_singleton()->push_item(scr.ptr());
for (int i = 0; i < tab_container->get_tab_count(); i++) { for (int i = 0; i < tab_container->get_tab_count(); i++) {

View File

@ -345,14 +345,19 @@ void ScriptTextEditor::reload_text() {
} }
void ScriptTextEditor::add_callback(const String &p_function, PackedStringArray p_args) { void ScriptTextEditor::add_callback(const String &p_function, PackedStringArray p_args) {
ScriptLanguage *language = script->get_language();
if (!language->can_make_function()) {
return;
}
String code = code_editor->get_text_editor()->get_text(); String code = code_editor->get_text_editor()->get_text();
int pos = script->get_language()->find_function(p_function, code); int pos = language->find_function(p_function, code);
code_editor->get_text_editor()->remove_secondary_carets(); code_editor->get_text_editor()->remove_secondary_carets();
if (pos == -1) { if (pos == -1) {
//does not exist //does not exist
code_editor->get_text_editor()->deselect(); code_editor->get_text_editor()->deselect();
pos = code_editor->get_text_editor()->get_line_count() + 2; pos = code_editor->get_text_editor()->get_line_count() + 2;
String func = script->get_language()->make_function("", p_function, p_args); String func = language->make_function("", p_function, p_args);
//code=code+func; //code=code+func;
code_editor->get_text_editor()->set_caret_line(pos + 1); code_editor->get_text_editor()->set_caret_line(pos + 1);
code_editor->get_text_editor()->set_caret_column(1000000); //none shall be that big code_editor->get_text_editor()->set_caret_column(1000000); //none shall be that big

View File

@ -516,22 +516,11 @@ static String variant_type_to_managed_name(const String &p_var_type_name) {
} }
String CSharpLanguage::make_function(const String &, const String &p_name, const PackedStringArray &p_args) const { String CSharpLanguage::make_function(const String &, const String &p_name, const PackedStringArray &p_args) const {
// FIXME // The make_function() API does not work for C# scripts.
// - Due to Godot's API limitation this just appends the function to the end of the file // It will always append the generated function at the very end of the script. In C#, it will break compilation by
// - Use fully qualified name if there is ambiguity // appending code after the final closing bracket (either the class' or the namespace's).
String s = "private void " + p_name + "("; // To prevent issues, we have can_make_function() returning false, and make_function() is never implemented.
for (int i = 0; i < p_args.size(); i++) { return String();
const String &arg = p_args[i];
if (i > 0) {
s += ", ";
}
s += variant_type_to_managed_name(arg.get_slice(":", 1)) + " " + escape_csharp_keyword(arg.get_slice(":", 0));
}
s += ")\n{\n // Replace with function body.\n}\n";
return s;
} }
#else #else
String CSharpLanguage::make_function(const String &, const String &, const PackedStringArray &) const { String CSharpLanguage::make_function(const String &, const String &, const PackedStringArray &) const {

View File

@ -439,6 +439,7 @@ public:
return -1; return -1;
} }
String make_function(const String &p_class, const String &p_name, const PackedStringArray &p_args) const override; String make_function(const String &p_class, const String &p_name, const PackedStringArray &p_args) const override;
virtual bool can_make_function() const override { return false; }
virtual String _get_indentation() const; virtual String _get_indentation() const;
/* TODO? */ void auto_indent_code(String &p_code, int p_from_line, int p_to_line) const override {} /* TODO? */ void auto_indent_code(String &p_code, int p_from_line, int p_to_line) const override {}
/* TODO */ void add_global_constant(const StringName &p_variable, const Variant &p_value) override {} /* TODO */ void add_global_constant(const StringName &p_variable, const Variant &p_value) override {}