diff options
author | Andreas Haas <hinsbart@gmail.com> | 2017-06-13 20:03:08 +0000 |
---|---|---|
committer | Andreas Haas <hinsbart@gmail.com> | 2017-06-13 20:03:08 +0000 |
commit | 8361b1ce07266350ef6b6a2d34411030b7e587b2 (patch) | |
tree | ff9cab7037410224c1b76aab30f394305b523fe8 /modules | |
parent | a8a1f2e2a864e6b58d5bcf1c7e53a43cdb6d95d9 (diff) |
Add ability to use custom script templates.
Templates will be loaded from .godot/script_templates
For now they're disabled for GDNative.
Ideas for further improvements:
- Add a "Save as Template" option to the script editor, as it can normally only save to res://
- Support more placeholders / custom placeholders
Diffstat (limited to 'modules')
-rw-r--r-- | modules/gdscript/gd_editor.cpp | 13 | ||||
-rw-r--r-- | modules/gdscript/gd_script.cpp | 5 | ||||
-rw-r--r-- | modules/gdscript/gd_script.h | 2 | ||||
-rw-r--r-- | modules/visual_script/visual_script.cpp | 11 | ||||
-rw-r--r-- | modules/visual_script/visual_script.h | 2 |
5 files changed, 33 insertions, 0 deletions
diff --git a/modules/gdscript/gd_editor.cpp b/modules/gdscript/gd_editor.cpp index eea5b15236..5e3ce31dd6 100644 --- a/modules/gdscript/gd_editor.cpp +++ b/modules/gdscript/gd_editor.cpp @@ -69,6 +69,19 @@ Ref<Script> GDScriptLanguage::get_template(const String &p_class_name, const Str return script; } +bool GDScriptLanguage::is_using_templates() { + + return true; +} + +void GDScriptLanguage::make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script) { + + String src = p_script->get_source_code(); + src = src.replace("%BASE%", p_base_class_name); + src = src.replace("%TS%", _get_indentation()); + p_script->set_source_code(src); +} + bool GDScriptLanguage::validate(const String &p_script, int &r_line_error, int &r_col_error, String &r_test_error, const String &p_path, List<String> *r_functions) const { GDParser parser; diff --git a/modules/gdscript/gd_script.cpp b/modules/gdscript/gd_script.cpp index dcc0e24098..1dcc442234 100644 --- a/modules/gdscript/gd_script.cpp +++ b/modules/gdscript/gd_script.cpp @@ -615,6 +615,11 @@ Error GDScript::reload(bool p_keep_state) { if (basedir != "") basedir = basedir.get_base_dir(); + if (basedir.find("res://") == -1 && basedir.find("user://") == -1) { + //loading a template, don't parse + return OK; + } + valid = false; GDParser parser; Error err = parser.parse(source, basedir, false, path); diff --git a/modules/gdscript/gd_script.h b/modules/gdscript/gd_script.h index 00ae136790..ebef4fed74 100644 --- a/modules/gdscript/gd_script.h +++ b/modules/gdscript/gd_script.h @@ -381,6 +381,8 @@ public: virtual void get_comment_delimiters(List<String> *p_delimiters) const; virtual void get_string_delimiters(List<String> *p_delimiters) const; virtual Ref<Script> get_template(const String &p_class_name, const String &p_base_class_name) const; + virtual bool is_using_templates(); + virtual void make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script); virtual bool validate(const String &p_script, int &r_line_error, int &r_col_error, String &r_test_error, const String &p_path = "", List<String> *r_functions = NULL) const; virtual Script *create_script() const; virtual bool has_named_classes() const; diff --git a/modules/visual_script/visual_script.cpp b/modules/visual_script/visual_script.cpp index aec60391d3..bb8111ce99 100644 --- a/modules/visual_script/visual_script.cpp +++ b/modules/visual_script/visual_script.cpp @@ -2369,6 +2369,17 @@ Ref<Script> VisualScriptLanguage::get_template(const String &p_class_name, const script->set_instance_base_type(p_base_class_name); return script; } + +bool VisualScriptLanguage::is_using_templates() { + + return true; +} + +void VisualScriptLanguage::make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script) { + Ref<VisualScript> script = p_script; + script->set_instance_base_type(p_base_class_name); +} + bool VisualScriptLanguage::validate(const String &p_script, int &r_line_error, int &r_col_error, String &r_test_error, const String &p_path, List<String> *r_functions) const { return false; diff --git a/modules/visual_script/visual_script.h b/modules/visual_script/visual_script.h index 72843099c7..1ccc358342 100644 --- a/modules/visual_script/visual_script.h +++ b/modules/visual_script/visual_script.h @@ -564,6 +564,8 @@ public: virtual void get_comment_delimiters(List<String> *p_delimiters) const; virtual void get_string_delimiters(List<String> *p_delimiters) const; virtual Ref<Script> get_template(const String &p_class_name, const String &p_base_class_name) const; + virtual bool is_using_templates(); + virtual void make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script); virtual bool validate(const String &p_script, int &r_line_error, int &r_col_error, String &r_test_error, const String &p_path = "", List<String> *r_functions = NULL) const; virtual Script *create_script() const; virtual bool has_named_classes() const; |