Skip to content

Commit

Permalink
Trait System
Browse files Browse the repository at this point in the history
  • Loading branch information
SeremTitus committed Oct 9, 2024
1 parent e3213aa commit f27a5d9
Show file tree
Hide file tree
Showing 34 changed files with 2,102 additions and 108 deletions.
1 change: 1 addition & 0 deletions core/object/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,7 @@ void Object::set_script(const Variant &p_script) {
ERR_FAIL_COND_MSG(s.is_null(), "Cannot set object script. Parameter should be null or a reference to a valid script.");
ERR_FAIL_COND_MSG(s->is_abstract(), vformat("Cannot set object script. Script '%s' should not be abstract.", s->get_path()));
}
ERR_FAIL_COND_MSG(!s->is_attachable(), vformat("Cannot set object script. Script '%s' is not attachable.", s->get_path()));

script = p_script;

Expand Down
1 change: 1 addition & 0 deletions core/object/script_language.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ void Script::_bind_methods() {

ClassDB::bind_method(D_METHOD("is_tool"), &Script::is_tool);
ClassDB::bind_method(D_METHOD("is_abstract"), &Script::is_abstract);
ClassDB::bind_method(D_METHOD("is_attachable"), &Script::is_attachable);

ClassDB::bind_method(D_METHOD("get_rpc_config"), &Script::get_rpc_config);

Expand Down
2 changes: 2 additions & 0 deletions core/object/script_language.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ class Script : public Resource {
virtual bool is_tool() const = 0;
virtual bool is_valid() const = 0;
virtual bool is_abstract() const = 0;
virtual bool is_attachable() const { return true; }

virtual ScriptLanguage *get_language() const = 0;

Expand Down Expand Up @@ -214,6 +215,7 @@ class ScriptLanguage : public Object {
virtual void init() = 0;
virtual String get_type() const = 0;
virtual String get_extension() const = 0;
virtual bool is_language_script_attachable() const { return true; }
virtual void finish() = 0;

/* EDITOR FUNCTIONS */
Expand Down
8 changes: 7 additions & 1 deletion doc/classes/Script.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<description>
A class stored as a resource. A script extends the functionality of all objects that instantiate it.
This is the base class for all scripts and should not be used directly. Trying to create a new script with this class will result in an error.
The [code]new[/code] method of a script subclass creates a new instance. [method Object.set_script] extends an existing object, if that object's class matches one of the script's base classes.
The [code]new[/code] method of a script subclass creates a new instance. [method Object.set_script] extends an existing object, if that object's class matches one of the script's base classes and if script is attachable check by [method is_attachable].
</description>
<tutorials>
<link title="Scripting documentation index">$DOCS_URL/tutorials/scripting/index.html</link>
Expand Down Expand Up @@ -115,6 +115,12 @@
Returns [code]true[/code] if the script is an abstract script. An abstract script does not have a constructor and cannot be instantiated.
</description>
</method>
<method name="is_attachable" qualifiers="const">
<return type="bool" />
<description>
Returns [code]true[/code] if the script is able to be attached to [Object].
</description>
</method>
<method name="is_tool" qualifiers="const">
<return type="bool" />
<description>
Expand Down
1 change: 1 addition & 0 deletions editor/filesystem_dock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4301,6 +4301,7 @@ FileSystemDock::FileSystemDock() {
make_scene_dialog->connect(SceneStringName(confirmed), callable_mp(this, &FileSystemDock::_make_scene_confirm));

make_script_dialog = memnew(ScriptCreateDialog);
make_script_dialog->set_languages_list(false);
make_script_dialog->set_title(TTR("Create Script"));
add_child(make_script_dialog);

Expand Down
55 changes: 37 additions & 18 deletions editor/script_create_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,16 @@ void ScriptCreateDialog::_notification(int p_what) {
} break;

case NOTIFICATION_THEME_CHANGED: {
for (int i = 0; i < ScriptServer::get_language_count(); i++) {
Ref<Texture2D> language_icon = get_editor_theme_icon(ScriptServer::get_language(i)->get_type());
if (language_icon.is_valid()) {
language_menu->set_item_icon(i, language_icon);
}
}
set_languages_list(is_languages_list_only_attachable);

path_button->set_icon(get_editor_theme_icon(SNAME("Folder")));
parent_browse_button->set_icon(get_editor_theme_icon(SNAME("Folder")));
parent_search_button->set_icon(get_editor_theme_icon(SNAME("ClassList")));
} break;

case NOTIFICATION_POSTINITIALIZE: {
set_languages_list(is_languages_list_only_attachable);
} break;
}
}

Expand Down Expand Up @@ -194,6 +193,38 @@ void ScriptCreateDialog::set_inheritance_base_type(const String &p_base) {
base_type = p_base;
}

void ScriptCreateDialog::set_languages_list(const bool p_only_attachable) {
is_languages_list_only_attachable = p_only_attachable;
String previous_default;
if (language_menu->get_selected_id() > -1) {
previous_default = language_menu->get_item_text(language_menu->get_selected_id());
}
default_language = -1;
language_menu->clear();
int skipped_lang = 0;
for (int i = 0; i < ScriptServer::get_language_count(); i++) {
ScriptLanguage *lang = ScriptServer::get_language(i);
if (p_only_attachable && !lang->is_language_script_attachable()) {
skipped_lang++;
continue;
}
language_menu->add_item(lang->get_name());
Ref<Texture2D> language_icon = get_editor_theme_icon(lang->get_type());
if (language_icon.is_valid()) {
language_menu->set_item_icon(i - skipped_lang, language_icon);
}
if (lang->get_name() == previous_default) {
default_language = i - skipped_lang;
}
if (default_language == -1 && lang->get_name() == "GDScript") {
default_language = i - skipped_lang;
}
}
if (default_language >= 0) {
language_menu->select(default_language);
}
}

bool ScriptCreateDialog::_validate_parent(const String &p_string) {
if (p_string.length() == 0) {
return false;
Expand Down Expand Up @@ -854,18 +885,6 @@ ScriptCreateDialog::ScriptCreateDialog() {
gc->add_child(memnew(Label(TTR("Language:"))));
gc->add_child(language_menu);

default_language = -1;
for (int i = 0; i < ScriptServer::get_language_count(); i++) {
String lang = ScriptServer::get_language(i)->get_name();
language_menu->add_item(lang);
if (lang == "GDScript") {
default_language = i;
}
}
if (default_language >= 0) {
language_menu->select(default_language);
}

language_menu->connect(SceneStringName(item_selected), callable_mp(this, &ScriptCreateDialog::_language_changed));

/* Inherits */
Expand Down
2 changes: 2 additions & 0 deletions editor/script_create_dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class ScriptCreateDialog : public ConfirmationDialog {
AcceptDialog *alert = nullptr;
CreateDialog *select_class = nullptr;

bool is_languages_list_only_attachable = true;
bool is_browsing_parent = false;
String path_error;
String template_inactive_message;
Expand Down Expand Up @@ -124,6 +125,7 @@ class ScriptCreateDialog : public ConfirmationDialog {
public:
void config(const String &p_base_name, const String &p_base_path, bool p_built_in_enabled = true, bool p_load_enabled = true);
void set_inheritance_base_type(const String &p_base);
void set_languages_list(const bool p_only_attachable);
ScriptCreateDialog();
};

Expand Down
4 changes: 4 additions & 0 deletions modules/gdscript/.editorconfig
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[*.gd]
indent_size = 4
trim_trailing_whitespace = true

[*.gdt]
indent_size = 4
trim_trailing_whitespace = true
2 changes: 2 additions & 0 deletions modules/gdscript/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ def configure(env):
def get_doc_classes():
return [
"@GDScript",
"@GDTrait",
"GDScript",
"GDTrait",
"GDScriptSyntaxHighlighter",
]

Expand Down
Loading

0 comments on commit f27a5d9

Please sign in to comment.