diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/input/input_event.cpp | 2 | ||||
-rw-r--r-- | core/math/vector4.cpp | 94 | ||||
-rw-r--r-- | core/math/vector4.h | 25 | ||||
-rw-r--r-- | core/object/object.cpp | 2 | ||||
-rw-r--r-- | core/object/script_language.cpp | 25 | ||||
-rw-r--r-- | core/object/script_language.h | 1 | ||||
-rw-r--r-- | core/object/script_language_extension.h | 8 | ||||
-rw-r--r-- | core/register_core_types.cpp | 1 | ||||
-rw-r--r-- | core/variant/variant_call.cpp | 6 | ||||
-rw-r--r-- | core/variant/variant_utility.cpp | 154 |
10 files changed, 278 insertions, 40 deletions
diff --git a/core/input/input_event.cpp b/core/input/input_event.cpp index 3c104c2c86..8ad2193fca 100644 --- a/core/input/input_event.cpp +++ b/core/input/input_event.cpp @@ -816,7 +816,7 @@ String InputEventMouseMotion::to_string() { // Work around the fact vformat can only take 5 substitutions but 7 need to be passed. String mask_and_position_and_relative = vformat("button_mask=%s, position=(%s), relative=(%s)", button_mask_string, String(get_position()), String(get_relative())); - return vformat("InputEventMouseMotion: %s, velocity=(%s), pressure=%.2f, tilt=(%s), pen_inverted=(%d)", mask_and_position_and_relative, String(get_velocity()), get_pressure(), String(get_tilt()), get_pen_inverted()); + return vformat("InputEventMouseMotion: %s, velocity=(%s), pressure=%.2f, tilt=(%s), pen_inverted=(%s)", mask_and_position_and_relative, String(get_velocity()), get_pressure(), String(get_tilt()), get_pen_inverted()); } bool InputEventMouseMotion::accumulate(const Ref<InputEvent> &p_event) { diff --git a/core/math/vector4.cpp b/core/math/vector4.cpp index cc0d0dcf72..4697c311b4 100644 --- a/core/math/vector4.cpp +++ b/core/math/vector4.cpp @@ -33,6 +33,40 @@ #include "core/math/basis.h" #include "core/string/print_string.h" +void Vector4::set_axis(const int p_axis, const real_t p_value) { + ERR_FAIL_INDEX(p_axis, 4); + components[p_axis] = p_value; +} + +real_t Vector4::get_axis(const int p_axis) const { + ERR_FAIL_INDEX_V(p_axis, 4, 0); + return operator[](p_axis); +} + +Vector4::Axis Vector4::min_axis_index() const { + uint32_t min_index = 0; + real_t min_value = x; + for (uint32_t i = 1; i < 4; i++) { + if (operator[](i) <= min_value) { + min_index = i; + min_value = operator[](i); + } + } + return Vector4::Axis(min_index); +} + +Vector4::Axis Vector4::max_axis_index() const { + uint32_t max_index = 0; + real_t max_value = x; + for (uint32_t i = 1; i < 4; i++) { + if (operator[](i) > max_value) { + max_index = i; + max_value = operator[](i); + } + } + return Vector4::Axis(max_index); +} + bool Vector4::is_equal_approx(const Vector4 &p_vec4) const { return Math::is_equal_approx(x, p_vec4.x) && Math::is_equal_approx(y, p_vec4.y) && Math::is_equal_approx(z, p_vec4.z) && Math::is_equal_approx(w, p_vec4.w); } @@ -53,6 +87,16 @@ bool Vector4::is_normalized() const { return Math::is_equal_approx(length_squared(), 1, (real_t)UNIT_EPSILON); // Use less epsilon. } +real_t Vector4::distance_to(const Vector4 &p_to) const { + return (p_to - *this).length(); +} + +Vector4 Vector4::direction_to(const Vector4 &p_to) const { + Vector4 ret(p_to.x - x, p_to.y - y, p_to.z - z, p_to.w - w); + ret.normalize(); + return ret; +} + Vector4 Vector4::abs() const { return Vector4(Math::abs(x), Math::abs(y), Math::abs(z), Math::abs(w)); } @@ -81,32 +125,38 @@ Vector4 Vector4::lerp(const Vector4 &p_to, const real_t p_weight) const { w + (p_weight * (p_to.w - w))); } -Vector4 Vector4::inverse() const { - return Vector4(1.0f / x, 1.0f / y, 1.0f / z, 1.0f / w); +Vector4 Vector4::cubic_interpolate(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, const real_t p_weight) const { + Vector4 res = *this; + res.x = Math::cubic_interpolate(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight); + res.y = Math::cubic_interpolate(res.y, p_b.y, p_pre_a.y, p_post_b.y, p_weight); + res.z = Math::cubic_interpolate(res.z, p_b.z, p_pre_a.z, p_post_b.z, p_weight); + res.w = Math::cubic_interpolate(res.w, p_b.w, p_pre_a.w, p_post_b.w, p_weight); + return res; } -Vector4::Axis Vector4::min_axis_index() const { - uint32_t min_index = 0; - real_t min_value = x; - for (uint32_t i = 1; i < 4; i++) { - if (operator[](i) <= min_value) { - min_index = i; - min_value = operator[](i); - } - } - return Vector4::Axis(min_index); +Vector4 Vector4::posmod(const real_t p_mod) const { + return Vector4(Math::fposmod(x, p_mod), Math::fposmod(y, p_mod), Math::fposmod(z, p_mod), Math::fposmod(w, p_mod)); } -Vector4::Axis Vector4::max_axis_index() const { - uint32_t max_index = 0; - real_t max_value = x; - for (uint32_t i = 1; i < 4; i++) { - if (operator[](i) > max_value) { - max_index = i; - max_value = operator[](i); - } - } - return Vector4::Axis(max_index); +Vector4 Vector4::posmodv(const Vector4 &p_modv) const { + return Vector4(Math::fposmod(x, p_modv.x), Math::fposmod(y, p_modv.y), Math::fposmod(z, p_modv.z), Math::fposmod(w, p_modv.w)); +} + +void Vector4::snap(const Vector4 &p_step) { + x = Math::snapped(x, p_step.x); + y = Math::snapped(y, p_step.y); + z = Math::snapped(z, p_step.z); + w = Math::snapped(w, p_step.w); +} + +Vector4 Vector4::snapped(const Vector4 &p_step) const { + Vector4 v = *this; + v.snap(p_step); + return v; +} + +Vector4 Vector4::inverse() const { + return Vector4(1.0f / x, 1.0f / y, 1.0f / z, 1.0f / w); } Vector4 Vector4::clamp(const Vector4 &p_min, const Vector4 &p_max) const { diff --git a/core/math/vector4.h b/core/math/vector4.h index 37ddb509d6..373a6a1218 100644 --- a/core/math/vector4.h +++ b/core/math/vector4.h @@ -62,6 +62,15 @@ struct _NO_DISCARD_ Vector4 { DEV_ASSERT((unsigned int)p_axis < 4); return components[p_axis]; } + + _FORCE_INLINE_ void set_all(const real_t p_value); + + void set_axis(const int p_axis, const real_t p_value); + real_t get_axis(const int p_axis) const; + + Vector4::Axis min_axis_index() const; + Vector4::Axis max_axis_index() const; + _FORCE_INLINE_ real_t length_squared() const; bool is_equal_approx(const Vector4 &p_vec4) const; real_t length() const; @@ -69,15 +78,21 @@ struct _NO_DISCARD_ Vector4 { Vector4 normalized() const; bool is_normalized() const; + real_t distance_to(const Vector4 &p_to) const; + Vector4 direction_to(const Vector4 &p_to) const; + Vector4 abs() const; Vector4 sign() const; Vector4 floor() const; Vector4 ceil() const; Vector4 round() const; Vector4 lerp(const Vector4 &p_to, const real_t p_weight) const; + Vector4 cubic_interpolate(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, const real_t p_weight) const; - Vector4::Axis min_axis_index() const; - Vector4::Axis max_axis_index() const; + Vector4 posmod(const real_t p_mod) const; + Vector4 posmodv(const Vector4 &p_modv) const; + void snap(const Vector4 &p_step); + Vector4 snapped(const Vector4 &p_step) const; Vector4 clamp(const Vector4 &p_min, const Vector4 &p_max) const; Vector4 inverse() const; @@ -130,6 +145,10 @@ struct _NO_DISCARD_ Vector4 { } }; +void Vector4::set_all(const real_t p_value) { + x = y = z = p_value; +} + real_t Vector4::dot(const Vector4 &p_vec4) const { return x * p_vec4.x + y * p_vec4.y + z * p_vec4.z + w * p_vec4.w; } @@ -193,7 +212,7 @@ Vector4 Vector4::operator/(const Vector4 &p_vec4) const { } Vector4 Vector4::operator-() const { - return Vector4(x, y, z, w); + return Vector4(-x, -y, -z, -w); } Vector4 Vector4::operator*(const real_t &s) const { diff --git a/core/object/object.cpp b/core/object/object.cpp index 75dbe8872f..0fcd1c0e40 100644 --- a/core/object/object.cpp +++ b/core/object/object.cpp @@ -472,7 +472,6 @@ Variant Object::get_indexed(const Vector<StringName> &p_names, bool *r_valid) co void Object::get_property_list(List<PropertyInfo> *p_list, bool p_reversed) const { if (script_instance && p_reversed) { - p_list->push_back(PropertyInfo(Variant::NIL, "Script Variables", PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_CATEGORY)); script_instance->get_property_list(p_list); } @@ -503,7 +502,6 @@ void Object::get_property_list(List<PropertyInfo> *p_list, bool p_reversed) cons } if (script_instance && !p_reversed) { - p_list->push_back(PropertyInfo(Variant::NIL, "Script Variables", PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_CATEGORY)); script_instance->get_property_list(p_list); } diff --git a/core/object/script_language.cpp b/core/object/script_language.cpp index 226fd8b791..b06c2e8896 100644 --- a/core/object/script_language.cpp +++ b/core/object/script_language.cpp @@ -101,6 +101,31 @@ Dictionary Script::_get_script_constant_map() { return ret; } +#ifdef TOOLS_ENABLED + +PropertyInfo Script::get_class_category() const { + String path = get_path(); + String name; + + if (is_built_in()) { + if (get_name().is_empty()) { + name = TTR("Built-in script"); + } else { + name = vformat("%s (%s)", get_name(), TTR("Built-in")); + } + } else { + if (get_name().is_empty()) { + name = path.get_file(); + } else { + name = get_name(); + } + } + + return PropertyInfo(Variant::NIL, name, PROPERTY_HINT_NONE, path, PROPERTY_USAGE_CATEGORY); +} + +#endif // TOOLS_ENABLED + void Script::_bind_methods() { ClassDB::bind_method(D_METHOD("can_instantiate"), &Script::can_instantiate); //ClassDB::bind_method(D_METHOD("instance_create","base_object"),&Script::instance_create); diff --git a/core/object/script_language.h b/core/object/script_language.h index c9f8a4f828..f5f052b600 100644 --- a/core/object/script_language.h +++ b/core/object/script_language.h @@ -132,6 +132,7 @@ public: #ifdef TOOLS_ENABLED virtual Vector<DocData::ClassDoc> get_documentation() const = 0; + virtual PropertyInfo get_class_category() const; #endif // TOOLS_ENABLED virtual bool has_method(const StringName &p_method) const = 0; diff --git a/core/object/script_language_extension.h b/core/object/script_language_extension.h index 10eacfd9f7..2869f4ad98 100644 --- a/core/object/script_language_extension.h +++ b/core/object/script_language_extension.h @@ -663,6 +663,14 @@ public: if (native_info->get_property_list_func) { uint32_t pcount; const GDNativePropertyInfo *pinfo = native_info->get_property_list_func(instance, &pcount); + +#ifdef TOOLS_ENABLED + Ref<Script> script = get_script(); + if (script->is_valid() && pcount > 0) { + p_list->push_back(script->get_class_category()); + } +#endif // TOOLS_ENABLED + for (uint32_t i = 0; i < pcount; i++) { p_list->push_back(PropertyInfo(pinfo[i])); } diff --git a/core/register_core_types.cpp b/core/register_core_types.cpp index 1ae5d1c83f..9ad6b0ca68 100644 --- a/core/register_core_types.cpp +++ b/core/register_core_types.cpp @@ -269,6 +269,7 @@ void register_core_types() { _marshalls = memnew(core_bind::Marshalls); _engine_debugger = memnew(core_bind::EngineDebugger); + GDREGISTER_NATIVE_STRUCT(ObjectID, "uint64_t id = 0"); GDREGISTER_NATIVE_STRUCT(AudioFrame, "float left;float right"); GDREGISTER_NATIVE_STRUCT(ScriptLanguageExtensionProfilingInfo, "StringName signature;uint64_t call_count;uint64_t total_time;uint64_t self_time"); diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index d1f1b83457..5c298f9b3b 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -1737,9 +1737,15 @@ static void _register_variant_builtin_methods() { bind_method(Vector4, ceil, sarray(), varray()); bind_method(Vector4, round, sarray(), varray()); bind_method(Vector4, lerp, sarray("to", "weight"), varray()); + bind_method(Vector4, cubic_interpolate, sarray("b", "pre_a", "post_b", "weight"), varray()); + bind_method(Vector4, posmod, sarray("mod"), varray()); + bind_method(Vector4, posmodv, sarray("modv"), varray()); + bind_method(Vector4, snapped, sarray("step"), varray()); bind_method(Vector4, clamp, sarray("min", "max"), varray()); bind_method(Vector4, normalized, sarray(), varray()); bind_method(Vector4, is_normalized, sarray(), varray()); + bind_method(Vector4, direction_to, sarray("to"), varray()); + bind_method(Vector4, distance_to, sarray("to"), varray()); bind_method(Vector4, dot, sarray("with"), varray()); bind_method(Vector4, inverse, sarray(), varray()); bind_method(Vector4, is_equal_approx, sarray("with"), varray()); diff --git a/core/variant/variant_utility.cpp b/core/variant/variant_utility.cpp index c1a0ad73b0..1f1439ab24 100644 --- a/core/variant/variant_utility.cpp +++ b/core/variant/variant_utility.cpp @@ -99,18 +99,105 @@ struct VariantUtilityFunctions { return Math::posmod(b, r); } - static inline double floor(double x) { + static inline Variant floor(Variant x, Callable::CallError &r_error) { + r_error.error = Callable::CallError::CALL_OK; + switch (x.get_type()) { + case Variant::INT: { + return VariantInternalAccessor<int64_t>::get(&x); + } break; + case Variant::FLOAT: { + return Math::floor(VariantInternalAccessor<double>::get(&x)); + } break; + case Variant::VECTOR2: { + return VariantInternalAccessor<Vector2>::get(&x).floor(); + } break; + case Variant::VECTOR3: { + return VariantInternalAccessor<Vector3>::get(&x).floor(); + } break; + case Variant::VECTOR4: { + return VariantInternalAccessor<Vector4>::get(&x).floor(); + } break; + default: { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; + return Variant(); + } + } + } + + static inline double floorf(double x) { return Math::floor(x); } - static inline double ceil(double x) { + static inline int floori(double x) { + return int(x); + } + + static inline Variant ceil(Variant x, Callable::CallError &r_error) { + r_error.error = Callable::CallError::CALL_OK; + switch (x.get_type()) { + case Variant::INT: { + return VariantInternalAccessor<int64_t>::get(&x); + } break; + case Variant::FLOAT: { + return Math::ceil(VariantInternalAccessor<double>::get(&x)); + } break; + case Variant::VECTOR2: { + return VariantInternalAccessor<Vector2>::get(&x).ceil(); + } break; + case Variant::VECTOR3: { + return VariantInternalAccessor<Vector3>::get(&x).ceil(); + } break; + case Variant::VECTOR4: { + return VariantInternalAccessor<Vector4>::get(&x).ceil(); + } break; + default: { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; + return Variant(); + } + } + } + + static inline double ceilf(double x) { return Math::ceil(x); } - static inline double round(double x) { + static inline int ceili(double x) { + return int(Math::ceil(x)); + } + + static inline Variant round(Variant x, Callable::CallError &r_error) { + r_error.error = Callable::CallError::CALL_OK; + switch (x.get_type()) { + case Variant::INT: { + return VariantInternalAccessor<int64_t>::get(&x); + } break; + case Variant::FLOAT: { + return Math::round(VariantInternalAccessor<double>::get(&x)); + } break; + case Variant::VECTOR2: { + return VariantInternalAccessor<Vector2>::get(&x).round(); + } break; + case Variant::VECTOR3: { + return VariantInternalAccessor<Vector3>::get(&x).round(); + } break; + case Variant::VECTOR4: { + return VariantInternalAccessor<Vector4>::get(&x).round(); + } break; + default: { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; + return Variant(); + } + } + } + + static inline double roundf(double x) { return Math::round(x); } + static inline int roundi(double x) { + return int(Math::round(x)); + } + static inline Variant abs(const Variant &x, Callable::CallError &r_error) { r_error.error = Callable::CallError::CALL_OK; switch (x.get_type()) { @@ -235,7 +322,44 @@ struct VariantUtilityFunctions { return Math::snapped(value, step); } - static inline double lerp(double from, double to, double weight) { + static inline Variant lerp(const Variant &from, const Variant &to, double weight, Callable::CallError &r_error) { + r_error.error = Callable::CallError::CALL_OK; + if (from.get_type() != to.get_type()) { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument = 1; + return Variant(); + } + + switch (from.get_type()) { + case Variant::FLOAT: { + return lerpf(VariantInternalAccessor<double>::get(&from), to, weight); + } break; + case Variant::VECTOR2: { + return VariantInternalAccessor<Vector2>::get(&from).lerp(VariantInternalAccessor<Vector2>::get(&to), weight); + } break; + case Variant::VECTOR3: { + return VariantInternalAccessor<Vector3>::get(&from).lerp(VariantInternalAccessor<Vector3>::get(&to), weight); + } break; + case Variant::VECTOR4: { + return VariantInternalAccessor<Vector4>::get(&from).lerp(VariantInternalAccessor<Vector4>::get(&to), weight); + } break; + case Variant::QUATERNION: { + return VariantInternalAccessor<Quaternion>::get(&from).slerp(VariantInternalAccessor<Quaternion>::get(&to), weight); + } break; + case Variant::BASIS: { + return VariantInternalAccessor<Basis>::get(&from).slerp(VariantInternalAccessor<Basis>::get(&to), weight); + } break; + case Variant::COLOR: { + return VariantInternalAccessor<Color>::get(&from).lerp(VariantInternalAccessor<Color>::get(&to), weight); + } break; + default: { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; + return Variant(); + } + } + } + + static inline double lerpf(double from, double to, double weight) { return Math::lerp(from, to, weight); } @@ -1252,17 +1376,24 @@ void Variant::_register_variant_utility_functions() { FUNCBINDR(fmod, sarray("x", "y"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(fposmod, sarray("x", "y"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(posmod, sarray("x", "y"), Variant::UTILITY_FUNC_TYPE_MATH); - FUNCBINDR(floor, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); - FUNCBINDR(ceil, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); - FUNCBINDR(round, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); - FUNCBINDVR(abs, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDVR(floor, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(floorf, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(floori, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDVR(ceil, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(ceilf, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(ceili, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDVR(round, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(roundf, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(roundi, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDVR(abs, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(absf, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(absi, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDVR(sign, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); - FUNCBINDR(signf, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(signi, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); @@ -1280,7 +1411,8 @@ void Variant::_register_variant_utility_functions() { FUNCBINDR(step_decimals, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(snapped, sarray("x", "step"), Variant::UTILITY_FUNC_TYPE_MATH); - FUNCBINDR(lerp, sarray("from", "to", "weight"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDVR3(lerp, sarray("from", "to", "weight"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(lerpf, sarray("from", "to", "weight"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(cubic_interpolate, sarray("from", "to", "pre", "post", "weight"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(bezier_interpolate, sarray("start", "control_1", "control_2", "end", "t"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(lerp_angle, sarray("from", "to", "weight"), Variant::UTILITY_FUNC_TYPE_MATH); @@ -1300,12 +1432,10 @@ void Variant::_register_variant_utility_functions() { FUNCBINDR(wrapf, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDVARARG(max, sarray(), Variant::UTILITY_FUNC_TYPE_MATH); - FUNCBINDR(maxi, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(maxf, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDVARARG(min, sarray(), Variant::UTILITY_FUNC_TYPE_MATH); - FUNCBINDR(mini, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(minf, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH); |