| 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 | |
| 31 | #ifndef VARIANT_UTILITY_H |
| 32 | #define VARIANT_UTILITY_H |
| 33 | |
| 34 | #include "variant.h" |
| 35 | |
| 36 | struct VariantUtilityFunctions { |
| 37 | // Math |
| 38 | static double sin(double arg); |
| 39 | static double cos(double arg); |
| 40 | static double tan(double arg); |
| 41 | static double sinh(double arg); |
| 42 | static double cosh(double arg); |
| 43 | static double tanh(double arg); |
| 44 | static double asin(double arg); |
| 45 | static double acos(double arg); |
| 46 | static double atan(double arg); |
| 47 | static double atan2(double y, double x); |
| 48 | static double asinh(double arg); |
| 49 | static double acosh(double arg); |
| 50 | static double atanh(double arg); |
| 51 | static double sqrt(double x); |
| 52 | static double fmod(double b, double r); |
| 53 | static double fposmod(double b, double r); |
| 54 | static int64_t posmod(int64_t b, int64_t r); |
| 55 | static Variant floor(Variant x, Callable::CallError &r_error); |
| 56 | static double floorf(double x); |
| 57 | static int64_t floori(double x); |
| 58 | static Variant ceil(Variant x, Callable::CallError &r_error); |
| 59 | static double ceilf(double x); |
| 60 | static int64_t ceili(double x); |
| 61 | static Variant round(Variant x, Callable::CallError &r_error); |
| 62 | static double roundf(double x); |
| 63 | static int64_t roundi(double x); |
| 64 | static Variant abs(const Variant &x, Callable::CallError &r_error); |
| 65 | static double absf(double x); |
| 66 | static int64_t absi(int64_t x); |
| 67 | static Variant sign(const Variant &x, Callable::CallError &r_error); |
| 68 | static double signf(double x); |
| 69 | static int64_t signi(int64_t x); |
| 70 | static double pow(double x, double y); |
| 71 | static double log(double x); |
| 72 | static double exp(double x); |
| 73 | static bool is_nan(double x); |
| 74 | static bool is_inf(double x); |
| 75 | static bool is_equal_approx(double x, double y); |
| 76 | static bool is_zero_approx(double x); |
| 77 | static bool is_finite(double x); |
| 78 | static double ease(float x, float curve); |
| 79 | static int step_decimals(float step); |
| 80 | static Variant snapped(const Variant &x, const Variant &step, Callable::CallError &r_error); |
| 81 | static double snappedf(double x, double step); |
| 82 | static int64_t snappedi(double x, int64_t step); |
| 83 | static Variant lerp(const Variant &from, const Variant &to, double weight, Callable::CallError &r_error); |
| 84 | static double lerpf(double from, double to, double weight); |
| 85 | static double cubic_interpolate(double from, double to, double pre, double post, double weight); |
| 86 | static double cubic_interpolate_angle(double from, double to, double pre, double post, double weight); |
| 87 | static double cubic_interpolate_in_time(double from, double to, double pre, double post, double weight, |
| 88 | double to_t, double pre_t, double post_t); |
| 89 | static double cubic_interpolate_angle_in_time(double from, double to, double pre, double post, double weight, |
| 90 | double to_t, double pre_t, double post_t); |
| 91 | static double bezier_interpolate(double p_start, double p_control_1, double p_control_2, double p_end, double p_t); |
| 92 | static double bezier_derivative(double p_start, double p_control_1, double p_control_2, double p_end, double p_t); |
| 93 | static double lerp_angle(double from, double to, double weight); |
| 94 | static double inverse_lerp(double from, double to, double weight); |
| 95 | static double remap(double value, double istart, double istop, double ostart, double ostop); |
| 96 | static double smoothstep(double from, double to, double val); |
| 97 | static double move_toward(double from, double to, double delta); |
| 98 | static double deg_to_rad(double angle_deg); |
| 99 | static double rad_to_deg(double angle_rad); |
| 100 | static double linear_to_db(double linear); |
| 101 | static double db_to_linear(double db); |
| 102 | static Variant wrap(const Variant &p_x, const Variant &p_min, const Variant &p_max, Callable::CallError &r_error); |
| 103 | static int64_t wrapi(int64_t value, int64_t min, int64_t max); |
| 104 | static double wrapf(double value, double min, double max); |
| 105 | static double pingpong(double value, double length); |
| 106 | static Variant max(const Variant **p_args, int p_argcount, Callable::CallError &r_error); |
| 107 | static double maxf(double x, double y); |
| 108 | static int64_t maxi(int64_t x, int64_t y); |
| 109 | static Variant min(const Variant **p_args, int p_argcount, Callable::CallError &r_error); |
| 110 | static double minf(double x, double y); |
| 111 | static int64_t mini(int64_t x, int64_t y); |
| 112 | static Variant clamp(const Variant &x, const Variant &min, const Variant &max, Callable::CallError &r_error); |
| 113 | static double clampf(double x, double min, double max); |
| 114 | static int64_t clampi(int64_t x, int64_t min, int64_t max); |
| 115 | static int64_t nearest_po2(int64_t x); |
| 116 | // Random |
| 117 | static void randomize(); |
| 118 | static int64_t randi(); |
| 119 | static double randf(); |
| 120 | static double randfn(double mean, double deviation); |
| 121 | static int64_t randi_range(int64_t from, int64_t to); |
| 122 | static double randf_range(double from, double to); |
| 123 | static void seed(int64_t s); |
| 124 | static PackedInt64Array rand_from_seed(int64_t seed); |
| 125 | // Utility |
| 126 | static Variant weakref(const Variant &obj, Callable::CallError &r_error); |
| 127 | static int64_t _typeof(const Variant &obj); |
| 128 | static Variant type_convert(const Variant &p_variant, const Variant::Type p_type); |
| 129 | static String str(const Variant **p_args, int p_arg_count, Callable::CallError &r_error); |
| 130 | static String error_string(Error error); |
| 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 | |
| 156 | #endif // VARIANT_UTILITY_H |
| 157 | |