diff options
-rw-r--r-- | core/math/a_star_grid_2d.cpp | 32 | ||||
-rw-r--r-- | core/math/a_star_grid_2d.h | 10 | ||||
-rw-r--r-- | doc/classes/AStarGrid2D.xml | 7 | ||||
-rw-r--r-- | modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/VariantUtils.cs | 45 | ||||
-rw-r--r-- | modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2.cs | 2 | ||||
-rw-r--r-- | modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2i.cs | 2 | ||||
-rw-r--r-- | tests/scene/test_visual_shader.h | 151 | ||||
-rw-r--r-- | tests/test_main.cpp | 1 |
8 files changed, 224 insertions, 26 deletions
diff --git a/core/math/a_star_grid_2d.cpp b/core/math/a_star_grid_2d.cpp index 968102e323..cdcd0ed747 100644 --- a/core/math/a_star_grid_2d.cpp +++ b/core/math/a_star_grid_2d.cpp @@ -134,13 +134,22 @@ AStarGrid2D::DiagonalMode AStarGrid2D::get_diagonal_mode() const { return diagonal_mode; } -void AStarGrid2D::set_default_heuristic(Heuristic p_heuristic) { +void AStarGrid2D::set_default_compute_heuristic(Heuristic p_heuristic) { ERR_FAIL_INDEX((int)p_heuristic, (int)HEURISTIC_MAX); - default_heuristic = p_heuristic; + default_compute_heuristic = p_heuristic; } -AStarGrid2D::Heuristic AStarGrid2D::get_default_heuristic() const { - return default_heuristic; +AStarGrid2D::Heuristic AStarGrid2D::get_default_compute_heuristic() const { + return default_compute_heuristic; +} + +void AStarGrid2D::set_default_estimate_heuristic(Heuristic p_heuristic) { + ERR_FAIL_INDEX((int)p_heuristic, (int)HEURISTIC_MAX); + default_estimate_heuristic = p_heuristic; +} + +AStarGrid2D::Heuristic AStarGrid2D::get_default_estimate_heuristic() const { + return default_estimate_heuristic; } void AStarGrid2D::set_point_solid(const Vector2i &p_id, bool p_solid) { @@ -447,7 +456,7 @@ real_t AStarGrid2D::_estimate_cost(const Vector2i &p_from_id, const Vector2i &p_ if (GDVIRTUAL_CALL(_estimate_cost, p_from_id, p_to_id, scost)) { return scost; } - return heuristics[default_heuristic](p_from_id, p_to_id); + return heuristics[default_estimate_heuristic](p_from_id, p_to_id); } real_t AStarGrid2D::_compute_cost(const Vector2i &p_from_id, const Vector2i &p_to_id) { @@ -455,7 +464,7 @@ real_t AStarGrid2D::_compute_cost(const Vector2i &p_from_id, const Vector2i &p_t if (GDVIRTUAL_CALL(_compute_cost, p_from_id, p_to_id, scost)) { return scost; } - return heuristics[default_heuristic](p_from_id, p_to_id); + return heuristics[default_compute_heuristic](p_from_id, p_to_id); } void AStarGrid2D::clear() { @@ -578,8 +587,10 @@ void AStarGrid2D::_bind_methods() { ClassDB::bind_method(D_METHOD("is_jumping_enabled"), &AStarGrid2D::is_jumping_enabled); ClassDB::bind_method(D_METHOD("set_diagonal_mode", "mode"), &AStarGrid2D::set_diagonal_mode); ClassDB::bind_method(D_METHOD("get_diagonal_mode"), &AStarGrid2D::get_diagonal_mode); - ClassDB::bind_method(D_METHOD("set_default_heuristic", "heuristic"), &AStarGrid2D::set_default_heuristic); - ClassDB::bind_method(D_METHOD("get_default_heuristic"), &AStarGrid2D::get_default_heuristic); + ClassDB::bind_method(D_METHOD("set_default_compute_heuristic", "heuristic"), &AStarGrid2D::set_default_compute_heuristic); + ClassDB::bind_method(D_METHOD("get_default_compute_heuristic"), &AStarGrid2D::get_default_compute_heuristic); + ClassDB::bind_method(D_METHOD("set_default_estimate_heuristic", "heuristic"), &AStarGrid2D::set_default_estimate_heuristic); + ClassDB::bind_method(D_METHOD("get_default_estimate_heuristic"), &AStarGrid2D::get_default_estimate_heuristic); ClassDB::bind_method(D_METHOD("set_point_solid", "id", "solid"), &AStarGrid2D::set_point_solid, DEFVAL(true)); ClassDB::bind_method(D_METHOD("is_point_solid", "id"), &AStarGrid2D::is_point_solid); ClassDB::bind_method(D_METHOD("set_point_weight_scale", "id", "weight_scale"), &AStarGrid2D::set_point_weight_scale); @@ -598,8 +609,9 @@ void AStarGrid2D::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "cell_size"), "set_cell_size", "get_cell_size"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "jumping_enabled"), "set_jumping_enabled", "is_jumping_enabled"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "default_heuristic", PROPERTY_HINT_ENUM, "Euclidean,Manhattan,Octile,Chebyshev,Max"), "set_default_heuristic", "get_default_heuristic"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "diagonal_mode", PROPERTY_HINT_ENUM, "Never,Always,At Least One Walkable,Only If No Obstacles,Max"), "set_diagonal_mode", "get_diagonal_mode"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "default_compute_heuristic", PROPERTY_HINT_ENUM, "Euclidean,Manhattan,Octile,Chebyshev"), "set_default_compute_heuristic", "get_default_compute_heuristic"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "default_estimate_heuristic", PROPERTY_HINT_ENUM, "Euclidean,Manhattan,Octile,Chebyshev"), "set_default_estimate_heuristic", "get_default_estimate_heuristic"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "diagonal_mode", PROPERTY_HINT_ENUM, "Never,Always,At Least One Walkable,Only If No Obstacles"), "set_diagonal_mode", "get_diagonal_mode"); BIND_ENUM_CONSTANT(HEURISTIC_EUCLIDEAN); BIND_ENUM_CONSTANT(HEURISTIC_MANHATTAN); diff --git a/core/math/a_star_grid_2d.h b/core/math/a_star_grid_2d.h index 3b39d46a20..ca36013b61 100644 --- a/core/math/a_star_grid_2d.h +++ b/core/math/a_star_grid_2d.h @@ -65,7 +65,8 @@ private: bool jumping_enabled = false; DiagonalMode diagonal_mode = DIAGONAL_MODE_ALWAYS; - Heuristic default_heuristic = HEURISTIC_EUCLIDEAN; + Heuristic default_compute_heuristic = HEURISTIC_EUCLIDEAN; + Heuristic default_estimate_heuristic = HEURISTIC_EUCLIDEAN; struct Point { Vector2i id; @@ -161,8 +162,11 @@ public: void set_diagonal_mode(DiagonalMode p_diagonal_mode); DiagonalMode get_diagonal_mode() const; - void set_default_heuristic(Heuristic p_heuristic); - Heuristic get_default_heuristic() const; + void set_default_compute_heuristic(Heuristic p_heuristic); + Heuristic get_default_compute_heuristic() const; + + void set_default_estimate_heuristic(Heuristic p_heuristic); + Heuristic get_default_estimate_heuristic() const; void set_point_solid(const Vector2i &p_id, bool p_solid = true); bool is_point_solid(const Vector2i &p_id) const; diff --git a/doc/classes/AStarGrid2D.xml b/doc/classes/AStarGrid2D.xml index cba783246f..32599d7f7d 100644 --- a/doc/classes/AStarGrid2D.xml +++ b/doc/classes/AStarGrid2D.xml @@ -140,8 +140,11 @@ <member name="cell_size" type="Vector2" setter="set_cell_size" getter="get_cell_size" default="Vector2(1, 1)"> The size of the point cell which will be applied to calculate the resulting point position returned by [method get_point_path]. If changed, [method update] needs to be called before finding the next path. </member> - <member name="default_heuristic" type="int" setter="set_default_heuristic" getter="get_default_heuristic" enum="AStarGrid2D.Heuristic" default="0"> - The default [enum Heuristic] which will be used to calculate the path if [method _compute_cost] and/or [method _estimate_cost] were not overridden. + <member name="default_compute_heuristic" type="int" setter="set_default_compute_heuristic" getter="get_default_compute_heuristic" enum="AStarGrid2D.Heuristic" default="0"> + The default [enum Heuristic] which will be used to calculate the cost between two points if [method _compute_cost] was not overridden. + </member> + <member name="default_estimate_heuristic" type="int" setter="set_default_estimate_heuristic" getter="get_default_estimate_heuristic" enum="AStarGrid2D.Heuristic" default="0"> + The default [enum Heuristic] which will be used to calculate the cost between the point and the end point if [method _estimate_cost] was not overridden. </member> <member name="diagonal_mode" type="int" setter="set_diagonal_mode" getter="get_diagonal_mode" enum="AStarGrid2D.DiagonalMode" default="0"> A specific [enum DiagonalMode] mode which will force the path to avoid or accept the specified diagonals. diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/VariantUtils.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/VariantUtils.cs index f65f926b22..9c9258dd9e 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/VariantUtils.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/VariantUtils.cs @@ -170,39 +170,66 @@ namespace Godot.NativeInterop [MethodImpl(MethodImplOptions.AggressiveInlining)] public static godot_variant CreateFromPackedByteArray(Span<byte> from) - => CreateFromPackedByteArray(Marshaling.ConvertSystemArrayToNativePackedByteArray(from)); + { + using var nativePackedArray = Marshaling.ConvertSystemArrayToNativePackedByteArray(from); + return CreateFromPackedByteArray(nativePackedArray); + } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static godot_variant CreateFromPackedInt32Array(Span<int> from) - => CreateFromPackedInt32Array(Marshaling.ConvertSystemArrayToNativePackedInt32Array(from)); + { + using var nativePackedArray = Marshaling.ConvertSystemArrayToNativePackedInt32Array(from); + return CreateFromPackedInt32Array(nativePackedArray); + } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static godot_variant CreateFromPackedInt64Array(Span<long> from) - => CreateFromPackedInt64Array(Marshaling.ConvertSystemArrayToNativePackedInt64Array(from)); + { + using var nativePackedArray = Marshaling.ConvertSystemArrayToNativePackedInt64Array(from); + return CreateFromPackedInt64Array(nativePackedArray); + } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static godot_variant CreateFromPackedFloat32Array(Span<float> from) - => CreateFromPackedFloat32Array(Marshaling.ConvertSystemArrayToNativePackedFloat32Array(from)); + { + using var nativePackedArray = Marshaling.ConvertSystemArrayToNativePackedFloat32Array(from); + return CreateFromPackedFloat32Array(nativePackedArray); + } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static godot_variant CreateFromPackedFloat64Array(Span<double> from) - => CreateFromPackedFloat64Array(Marshaling.ConvertSystemArrayToNativePackedFloat64Array(from)); + { + using var nativePackedArray = Marshaling.ConvertSystemArrayToNativePackedFloat64Array(from); + return CreateFromPackedFloat64Array(nativePackedArray); + } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static godot_variant CreateFromPackedStringArray(Span<string> from) - => CreateFromPackedStringArray(Marshaling.ConvertSystemArrayToNativePackedStringArray(from)); + { + using var nativePackedArray = Marshaling.ConvertSystemArrayToNativePackedStringArray(from); + return CreateFromPackedStringArray(nativePackedArray); + } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static godot_variant CreateFromPackedVector2Array(Span<Vector2> from) - => CreateFromPackedVector2Array(Marshaling.ConvertSystemArrayToNativePackedVector2Array(from)); + { + using var nativePackedArray = Marshaling.ConvertSystemArrayToNativePackedVector2Array(from); + return CreateFromPackedVector2Array(nativePackedArray); + } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static godot_variant CreateFromPackedVector3Array(Span<Vector3> from) - => CreateFromPackedVector3Array(Marshaling.ConvertSystemArrayToNativePackedVector3Array(from)); + { + using var nativePackedArray = Marshaling.ConvertSystemArrayToNativePackedVector3Array(from); + return CreateFromPackedVector3Array(nativePackedArray); + } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static godot_variant CreateFromPackedColorArray(Span<Color> from) - => CreateFromPackedColorArray(Marshaling.ConvertSystemArrayToNativePackedColorArray(from)); + { + using var nativePackedArray = Marshaling.ConvertSystemArrayToNativePackedColorArray(from); + return CreateFromPackedColorArray(nativePackedArray); + } public static godot_variant CreateFromSystemArrayOfStringName(Span<StringName> from) => CreateFromArray(new Collections.Array(from)); diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2.cs index c471eceded..4c60080ee9 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2.cs @@ -139,7 +139,7 @@ namespace Godot /// <returns>The angle between the two vectors, in radians.</returns> public readonly real_t AngleToPoint(Vector2 to) { - return Mathf.Atan2(y - to.y, x - to.x); + return Mathf.Atan2(to.y - y, to.x - x); } /// <summary> diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2i.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2i.cs index 08f2a3a8af..91be548a21 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2i.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2i.cs @@ -123,7 +123,7 @@ namespace Godot /// <returns>The angle between the two vectors, in radians.</returns> public readonly real_t AngleToPoint(Vector2i to) { - return Mathf.Atan2(y - to.y, x - to.x); + return Mathf.Atan2(to.y - y, to.x - x); } /// <summary> diff --git a/tests/scene/test_visual_shader.h b/tests/scene/test_visual_shader.h new file mode 100644 index 0000000000..457f9ad6ac --- /dev/null +++ b/tests/scene/test_visual_shader.h @@ -0,0 +1,151 @@ +/*************************************************************************/ +/* test_visual_shader.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef TEST_VISUAL_SHADER_H +#define TEST_VISUAL_SHADER_H + +#include "scene/resources/visual_shader.h" + +#include "tests/test_macros.h" + +namespace TestVisualArray { + +TEST_CASE("[SceneTree][VisualShader] Object creation and parameter") { + Ref<VisualShader> vs = memnew(VisualShader); + CHECK(vs.is_valid()); + + CHECK(vs->get_mode() == Shader::MODE_SPATIAL); + + for (int i = 1; i < Shader::MODE_MAX; i++) { + vs->set_mode((Shader::Mode)i); + CHECK(vs->get_mode() == i); + } +} + +TEST_CASE("[SceneTree][VisualShader] Testing VisualShaderNodes") { + SUBCASE("Testing Node Creation") { + Ref<VisualShader> vs = memnew(VisualShader); + CHECK(vs.is_valid()); + + for (int i = 0; i < VisualShader::TYPE_MAX; i++) { + Ref<VisualShaderNode> vsn = memnew(VisualShaderNodeInput); + CHECK(vsn.is_valid()); + vs->add_node(VisualShader::Type(i), vsn, Vector2(1, 10), i + 2); + CHECK(vs->get_node(VisualShader::Type(i), i + 2) == vsn); + } + + ERR_PRINT_OFF; + + // Testing for Invalid entries. + Ref<VisualShaderNode> vsn5 = memnew(VisualShaderNodeInput); + Ref<VisualShaderNode> vsn6 = memnew(VisualShaderNodeInput); + CHECK(vsn6.is_valid()); + CHECK(vsn5.is_valid()); + + vs->add_node(VisualShader::TYPE_SKY, vsn5, Vector2(1, 10), 0); + CHECK_FALSE(vs->get_node(VisualShader::TYPE_SKY, 0) == vsn6); + vs->add_node(VisualShader::TYPE_MAX, vsn6, Vector2(1, 10), 7); + CHECK_FALSE(vs->get_node(VisualShader::TYPE_SKY, 7) == vsn6); + + ERR_PRINT_ON; + } + + SUBCASE("Testing VisualShaderNode position getter and setter") { + Ref<VisualShader> vs = memnew(VisualShader); + CHECK(vs.is_valid()); + + Ref<VisualShaderNode> vsn1 = memnew(VisualShaderNodeInput); + CHECK(vsn1.is_valid()); + vs->add_node(VisualShader::TYPE_COLLIDE, vsn1, Vector2(0, 0), 3); + CHECK(vs->get_node_position(VisualShader::TYPE_COLLIDE, 3) == Vector2(0, 0)); + vs->set_node_position(VisualShader::TYPE_COLLIDE, 3, Vector2(1, 1)); + CHECK(vs->get_node_position(VisualShader::TYPE_COLLIDE, 3) == Vector2(1, 1)); + + Ref<VisualShaderNode> vsn2 = memnew(VisualShaderNodeInput); + CHECK(vsn2.is_valid()); + vs->add_node(VisualShader::TYPE_FOG, vsn2, Vector2(1, 2), 4); + CHECK(vs->get_node_position(VisualShader::TYPE_FOG, 4) == Vector2(1, 2)); + vs->set_node_position(VisualShader::TYPE_FOG, 4, Vector2(2, 2)); + CHECK(vs->get_node_position(VisualShader::TYPE_FOG, 4) == Vector2(2, 2)); + } + + SUBCASE("Testing VisualShaderNode ID") { + Ref<VisualShader> vs = memnew(VisualShader); + CHECK(vs.is_valid()); + + for (int i = 0; i < VisualShader::TYPE_MAX; i++) { + Ref<VisualShaderNode> vsn = memnew(VisualShaderNodeInput); + CHECK(vsn.is_valid()); + vs->add_node(VisualShader::Type(i), vsn, Vector2(1, 10), i + 2); + CHECK(vs->get_valid_node_id(VisualShader::Type(i)) - 1 == i + 2); + } + } + + SUBCASE("Testing remove and replace VisualShaderNode") { + Ref<VisualShader> vs = memnew(VisualShader); + CHECK(vs.is_valid()); + + ERR_PRINT_OFF; + + for (int i = 0; i < VisualShader::TYPE_MAX; i++) { + Ref<VisualShaderNode> vsn = memnew(VisualShaderNodeInput); + CHECK(vsn.is_valid()); + vs->add_node(VisualShader::Type(i), vsn, Vector2(1, 10), i + 2); + CHECK(vs->get_node(VisualShader::Type(i), i + 2) == vsn); + vs->remove_node(VisualShader::Type(i), i + 2); + CHECK_FALSE(vs->get_node(VisualShader::Type(i), i + 2) == vsn); + } + + ERR_PRINT_ON; + } +} + +TEST_CASE("[SceneTree][VisualShader] Testing Varyings") { + Ref<VisualShader> vs = memnew(VisualShader); + + vs->add_varying("Test1", VisualShader::VARYING_MODE_FRAG_TO_LIGHT, VisualShader::VARYING_TYPE_TRANSFORM); + CHECK(vs->has_varying("Test1") == true); + + vs->add_varying("Test2", VisualShader::VARYING_MODE_VERTEX_TO_FRAG_LIGHT, VisualShader::VARYING_TYPE_VECTOR_2D); + CHECK(vs->has_varying("Test2")); + + CHECK_FALSE(vs->has_varying("Does_not_exits")); + ERR_PRINT_OFF; + vs->add_varying("Test3", VisualShader::VARYING_MODE_MAX, VisualShader::VARYING_TYPE_INT); + CHECK_FALSE(vs->has_varying("Test3")); + + vs->add_varying("Test4", VisualShader::VARYING_MODE_FRAG_TO_LIGHT, VisualShader::VARYING_TYPE_MAX); + CHECK_FALSE(vs->has_varying("Test4")); + ERR_PRINT_ON; +} + +} //namespace TestVisualArray + +#endif // TEST_VISUAL_SHADER_H diff --git a/tests/test_main.cpp b/tests/test_main.cpp index 4c4d47a7ae..f51d7c8e4d 100644 --- a/tests/test_main.cpp +++ b/tests/test_main.cpp @@ -98,6 +98,7 @@ #include "tests/scene/test_sprite_frames.h" #include "tests/scene/test_text_edit.h" #include "tests/scene/test_theme.h" +#include "tests/scene/test_visual_shader.h" #include "tests/servers/test_text_server.h" #include "tests/test_validate_testing.h" |