variant_utility.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**************************************************************************/
  2. /* variant_utility.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef VARIANT_UTILITY_H
  31. #define VARIANT_UTILITY_H
  32. #include "variant.h"
  33. struct VariantUtilityFunctions {
  34. // Math
  35. static double sin(double arg);
  36. static double cos(double arg);
  37. static double tan(double arg);
  38. static double sinh(double arg);
  39. static double cosh(double arg);
  40. static double tanh(double arg);
  41. static double asin(double arg);
  42. static double acos(double arg);
  43. static double atan(double arg);
  44. static double atan2(double y, double x);
  45. static double asinh(double arg);
  46. static double acosh(double arg);
  47. static double atanh(double arg);
  48. static double sqrt(double x);
  49. static double fmod(double b, double r);
  50. static double fposmod(double b, double r);
  51. static int64_t posmod(int64_t b, int64_t r);
  52. static Variant floor(const Variant &x, Callable::CallError &r_error);
  53. static double floorf(double x);
  54. static int64_t floori(double x);
  55. static Variant ceil(const Variant &x, Callable::CallError &r_error);
  56. static double ceilf(double x);
  57. static int64_t ceili(double x);
  58. static Variant round(const Variant &x, Callable::CallError &r_error);
  59. static double roundf(double x);
  60. static int64_t roundi(double x);
  61. static Variant abs(const Variant &x, Callable::CallError &r_error);
  62. static double absf(double x);
  63. static int64_t absi(int64_t x);
  64. static Variant sign(const Variant &x, Callable::CallError &r_error);
  65. static double signf(double x);
  66. static int64_t signi(int64_t x);
  67. static double pow(double x, double y);
  68. static double log(double x);
  69. static double exp(double x);
  70. static bool is_nan(double x);
  71. static bool is_inf(double x);
  72. static bool is_equal_approx(double x, double y);
  73. static bool is_zero_approx(double x);
  74. static bool is_finite(double x);
  75. static double ease(float x, float curve);
  76. static int step_decimals(float step);
  77. static Variant snapped(const Variant &x, const Variant &step, Callable::CallError &r_error);
  78. static double snappedf(double x, double step);
  79. static int64_t snappedi(double x, int64_t step);
  80. static Variant lerp(const Variant &from, const Variant &to, double weight, Callable::CallError &r_error);
  81. static double lerpf(double from, double to, double weight);
  82. static double cubic_interpolate(double from, double to, double pre, double post, double weight);
  83. static double cubic_interpolate_angle(double from, double to, double pre, double post, double weight);
  84. static double cubic_interpolate_in_time(double from, double to, double pre, double post, double weight,
  85. double to_t, double pre_t, double post_t);
  86. static double cubic_interpolate_angle_in_time(double from, double to, double pre, double post, double weight,
  87. double to_t, double pre_t, double post_t);
  88. static double bezier_interpolate(double p_start, double p_control_1, double p_control_2, double p_end, double p_t);
  89. static double bezier_derivative(double p_start, double p_control_1, double p_control_2, double p_end, double p_t);
  90. static double angle_difference(double from, double to);
  91. static double lerp_angle(double from, double to, double weight);
  92. static double inverse_lerp(double from, double to, double weight);
  93. static double remap(double value, double istart, double istop, double ostart, double ostop);
  94. static double smoothstep(double from, double to, double val);
  95. static double move_toward(double from, double to, double delta);
  96. static double rotate_toward(double from, double to, double delta);
  97. static double deg_to_rad(double angle_deg);
  98. static double rad_to_deg(double angle_rad);
  99. static double linear_to_db(double linear);
  100. static double db_to_linear(double db);
  101. static Variant wrap(const Variant &p_x, const Variant &p_min, const Variant &p_max, Callable::CallError &r_error);
  102. static int64_t wrapi(int64_t value, int64_t min, int64_t max);
  103. static double wrapf(double value, double min, double max);
  104. static double pingpong(double value, double length);
  105. static Variant max(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  106. static double maxf(double x, double y);
  107. static int64_t maxi(int64_t x, int64_t y);
  108. static Variant min(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  109. static double minf(double x, double y);
  110. static int64_t mini(int64_t x, int64_t y);
  111. static Variant clamp(const Variant &x, const Variant &min, const Variant &max, Callable::CallError &r_error);
  112. static double clampf(double x, double min, double max);
  113. static int64_t clampi(int64_t x, int64_t min, int64_t max);
  114. static int64_t nearest_po2(int64_t x);
  115. // Random
  116. static void randomize();
  117. static int64_t randi();
  118. static double randf();
  119. static double randfn(double mean, double deviation);
  120. static int64_t randi_range(int64_t from, int64_t to);
  121. static double randf_range(double from, double to);
  122. static void seed(int64_t s);
  123. static PackedInt64Array rand_from_seed(int64_t seed);
  124. // Utility
  125. static Variant weakref(const Variant &obj, Callable::CallError &r_error);
  126. static int64_t _typeof(const Variant &obj);
  127. static Variant type_convert(const Variant &p_variant, const Variant::Type p_type);
  128. static String str(const Variant **p_args, int p_arg_count, Callable::CallError &r_error);
  129. static String error_string(Error error);
  130. static String type_string(Variant::Type p_type);
  131. static void print(const Variant **p_args, int p_arg_count, Callable::CallError &r_error);
  132. static void print_rich(const Variant **p_args, int p_arg_count, Callable::CallError &r_error);
  133. #undef print_verbose
  134. static void print_verbose(const Variant **p_args, int p_arg_count, Callable::CallError &r_error);
  135. static void printerr(const Variant **p_args, int p_arg_count, Callable::CallError &r_error);
  136. static void printt(const Variant **p_args, int p_arg_count, Callable::CallError &r_error);
  137. static void prints(const Variant **p_args, int p_arg_count, Callable::CallError &r_error);
  138. static void printraw(const Variant **p_args, int p_arg_count, Callable::CallError &r_error);
  139. static void push_error(const Variant **p_args, int p_arg_count, Callable::CallError &r_error);
  140. static void push_warning(const Variant **p_args, int p_arg_count, Callable::CallError &r_error);
  141. static String var_to_str(const Variant &p_var);
  142. static Variant str_to_var(const String &p_var);
  143. static PackedByteArray var_to_bytes(const Variant &p_var);
  144. static PackedByteArray var_to_bytes_with_objects(const Variant &p_var);
  145. static Variant bytes_to_var(const PackedByteArray &p_arr);
  146. static Variant bytes_to_var_with_objects(const PackedByteArray &p_arr);
  147. static int64_t hash(const Variant &p_arr);
  148. static Object *instance_from_id(int64_t p_id);
  149. static bool is_instance_id_valid(int64_t p_id);
  150. static bool is_instance_valid(const Variant &p_instance);
  151. static uint64_t rid_allocate_id();
  152. static RID rid_from_int64(uint64_t p_base);
  153. static bool is_same(const Variant &p_a, const Variant &p_b);
  154. };
  155. #endif // VARIANT_UTILITY_H