Merge pull request #53957 from fabriceci/new-template-workflow

This commit is contained in:
Rémi Verschelde 2022-01-03 20:40:33 +01:00 committed by GitHub
commit 98b3ba1842
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
29 changed files with 944 additions and 528 deletions

View file

@ -63,3 +63,5 @@ elif env["platform"] == "android":
if env["tools"]:
env_mono.add_source_files(env.modules_sources, "editor/*.cpp")
SConscript("editor_templates/SCsub")

View file

@ -55,6 +55,7 @@
#endif
#include "editor/editor_internal_calls.h"
#include "editor_templates/templates.gen.h"
#include "godotsharp_dirs.h"
#include "mono_gd/gd_mono_cache.h"
#include "mono_gd/gd_mono_class.h"
@ -351,57 +352,33 @@ static String get_base_class_name(const String &p_base_class_name, const String
return base_class;
}
Ref<Script> CSharpLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const {
String script_template = "using " BINDINGS_NAMESPACE ";\n"
"using System;\n"
"\n"
"public partial class %CLASS% : %BASE%\n"
"{\n"
" // Declare member variables here. Examples:\n"
" // private int a = 2;\n"
" // private string b = \"text\";\n"
"\n"
" // Called when the node enters the scene tree for the first time.\n"
" public override void _Ready()\n"
" {\n"
" \n"
" }\n"
"\n"
"// // Called every frame. 'delta' is the elapsed time since the previous frame.\n"
"// public override void _Process(float delta)\n"
"// {\n"
"// \n"
"// }\n"
"}\n";
// Replaces all spaces in p_class_name with underscores to prevent
// invalid C# Script templates from being generated when the object name
// has spaces in it.
String class_name_no_spaces = p_class_name.replace(" ", "_");
String base_class_name = get_base_class_name(p_base_class_name, class_name_no_spaces);
script_template = script_template.replace("%BASE%", base_class_name)
.replace("%CLASS%", class_name_no_spaces);
Ref<CSharpScript> script;
script.instantiate();
script->set_source_code(script_template);
script->set_name(class_name_no_spaces);
return script;
}
bool CSharpLanguage::is_using_templates() {
return true;
}
void CSharpLanguage::make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script) {
String src = p_script->get_source_code();
Ref<Script> CSharpLanguage::make_template(const String &p_template, const String &p_class_name, const String &p_base_class_name) const {
Ref<CSharpScript> script;
script.instantiate();
String class_name_no_spaces = p_class_name.replace(" ", "_");
String base_class_name = get_base_class_name(p_base_class_name, class_name_no_spaces);
src = src.replace("%BASE%", base_class_name)
.replace("%CLASS%", class_name_no_spaces)
.replace("%TS%", _get_indentation());
p_script->set_source_code(src);
String processed_template = p_template;
processed_template = processed_template.replace("_BINDINGS_NAMESPACE_", BINDINGS_NAMESPACE)
.replace("_BASE_", base_class_name)
.replace("_CLASS_", class_name_no_spaces)
.replace("_TS_", _get_indentation());
script->set_source_code(processed_template);
return script;
}
Vector<ScriptLanguage::ScriptTemplate> CSharpLanguage::get_built_in_templates(StringName p_object) {
Vector<ScriptLanguage::ScriptTemplate> templates;
for (int i = 0; i < TEMPLATES_ARRAY_SIZE; i++) {
if (TEMPLATES[i].inherit == p_object) {
templates.append(TEMPLATES[i]);
}
}
return templates;
}
String CSharpLanguage::validate_path(const String &p_path) const {

View file

@ -463,9 +463,9 @@ public:
bool is_control_flow_keyword(String p_keyword) const override;
void get_comment_delimiters(List<String> *p_delimiters) const override;
void get_string_delimiters(List<String> *p_delimiters) const override;
Ref<Script> get_template(const String &p_class_name, const String &p_base_class_name) const override;
bool is_using_templates() override;
void make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script) override;
virtual Ref<Script> make_template(const String &p_template, const String &p_class_name, const String &p_base_class_name) const override;
virtual Vector<ScriptTemplate> get_built_in_templates(StringName p_object) override;
/* TODO */ bool validate(const String &p_script, const String &p_path, List<String> *r_functions,
List<ScriptLanguage::ScriptError> *r_errors = nullptr, List<ScriptLanguage::Warning> *r_warnings = nullptr, Set<int> *r_safe_lines = nullptr) const override {
return true;

View file

@ -0,0 +1,41 @@
// meta-description: Classic movement for gravity games (platformer, ...)
using _BINDINGS_NAMESPACE_;
using System;
public partial class _CLASS_ : _BASE_
{
public const float Speed = 300.0f;
public const float JumpForce = -400.0f;
// Get the gravity from the project settings to be synced with RigidDynamicBody nodes.
public float gravity = (float)ProjectSettings.GetSetting("physics/2d/default_gravity");
public override void _PhysicsProcess(float delta)
{
Vector2 motionVelocity = MotionVelocity;
// Add the gravity.
if (!IsOnFloor())
motionVelocity.y += gravity * delta;
// Handle Jump.
if (Input.IsActionJustPressed("ui_accept") && IsOnFloor())
motionVelocity.y = JumpForce;
// Get the input direction and handle the movement/deceleration.
// As good practice, you should replace UI actions with custom gameplay actions.
Vector2 direction = Input.GetVector("ui_left", "ui_right", "ui_up", "ui_down");
if (direction != Vector2.Zero)
{
motionVelocity.x = direction.x * Speed;
}
else
{
motionVelocity.x = Mathf.MoveToward(MotionVelocity.x, 0, Speed);
}
MotionVelocity = motionVelocity;
MoveAndSlide();
}
}

View file

@ -0,0 +1,44 @@
// meta-description: Classic movement for gravity games (FPS, TPS, ...)
using _BINDINGS_NAMESPACE_;
using System;
public partial class _CLASS_ : _BASE_
{
public const float Speed = 5.0f;
public const float JumpForce = 4.5f;
// Get the gravity from the project settings to be synced with RigidDynamicBody nodes.
public float gravity = (float)ProjectSettings.GetSetting("physics/3d/default_gravity");
public override void _PhysicsProcess(float delta)
{
Vector3 motionVelocity = MotionVelocity;
// Add the gravity.
if (!IsOnFloor())
motionVelocity.y -= gravity * delta;
// Handle Jump.
if (Input.IsActionJustPressed("ui_accept") && IsOnFloor())
motionVelocity.y = JumpForce;
// Get the input direction and handle the movement/deceleration.
// As good practice, you should replace UI actions with custom gameplay actions.
Vector2 inputDir = Input.GetVector("ui_left", "ui_right", "ui_up", "ui_down");
Vector3 direction = Transform.basis.Xform(new Vector3(inputDir.x, 0, inputDir.y)).Normalized();
if (direction != Vector3.Zero)
{
motionVelocity.x = direction.x * Speed;
motionVelocity.z = direction.z * Speed;
}
else
{
motionVelocity.x = Mathf.MoveToward(MotionVelocity.x, 0, Speed);
motionVelocity.z = Mathf.MoveToward(MotionVelocity.z, 0, Speed);
}
MotionVelocity = motionVelocity;
MoveAndSlide();
}
}

View file

@ -0,0 +1,19 @@
// meta-description: Basic plugin template
#if TOOLS
using _BINDINGS_NAMESPACE_;
using System;
[Tool]
public partial class _CLASS_ : _BASE_
{
public override void _EnterTree()
{
// Initialization of the plugin goes here.
}
public override void _ExitTree()
{
// Clean-up of the plugin goes here.
}
}
#endif

View file

@ -0,0 +1,14 @@
// meta-description: Basic editor script template
#if TOOLS
using _BINDINGS_NAMESPACE_;
using System;
[Tool]
public partial class _CLASS_ : _BASE_
{
public override void _Run()
{
// Called when the script is executed (using File -> Run in Script Editor).
}
}
#endif

View file

@ -0,0 +1,19 @@
// meta-description: Base template for Node with default Godot cycle methods
using _BINDINGS_NAMESPACE_;
using System;
public partial class _CLASS_ : _BASE_
{
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(float delta)
{
}
}

View file

@ -0,0 +1,9 @@
// meta-description: Empty template suitable for all Objects
using _BINDINGS_NAMESPACE_;
using System;
public partial class _CLASS_ : _BASE_
{
}

View file

@ -0,0 +1,16 @@
#!/usr/bin/env python
Import("env")
import editor.template_builders as build_template_cs
env["BUILDERS"]["MakeCSharpTemplateBuilder"] = Builder(
action=env.Run(build_template_cs.make_templates, "Generating C# templates header."),
suffix=".h",
src_suffix=".cs",
)
# Template files
templates_sources = Glob("*/*.cs")
env.Alias("editor_template_cs", [env.MakeCSharpTemplateBuilder("templates.gen.h", templates_sources)])