| 1 | /**************************************************************************/ |
| 2 | /* animation_blend_space_2d.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 ANIMATION_BLEND_SPACE_2D_H |
| 32 | #define ANIMATION_BLEND_SPACE_2D_H |
| 33 | |
| 34 | #include "scene/animation/animation_tree.h" |
| 35 | |
| 36 | class AnimationNodeBlendSpace2D : public AnimationRootNode { |
| 37 | GDCLASS(AnimationNodeBlendSpace2D, AnimationRootNode); |
| 38 | |
| 39 | public: |
| 40 | enum BlendMode { |
| 41 | BLEND_MODE_INTERPOLATED, |
| 42 | BLEND_MODE_DISCRETE, |
| 43 | BLEND_MODE_DISCRETE_CARRY, |
| 44 | }; |
| 45 | |
| 46 | protected: |
| 47 | enum { |
| 48 | MAX_BLEND_POINTS = 64 |
| 49 | }; |
| 50 | |
| 51 | struct BlendPoint { |
| 52 | StringName name; |
| 53 | Ref<AnimationRootNode> node; |
| 54 | Vector2 position; |
| 55 | }; |
| 56 | |
| 57 | BlendPoint blend_points[MAX_BLEND_POINTS]; |
| 58 | int blend_points_used = 0; |
| 59 | |
| 60 | struct BlendTriangle { |
| 61 | int points[3] = {}; |
| 62 | }; |
| 63 | |
| 64 | Vector<BlendTriangle> triangles; |
| 65 | |
| 66 | StringName blend_position = "blend_position" ; |
| 67 | StringName closest = "closest" ; |
| 68 | StringName length_internal = "length_internal" ; |
| 69 | Vector2 max_space = Vector2(1, 1); |
| 70 | Vector2 min_space = Vector2(-1, -1); |
| 71 | Vector2 snap = Vector2(0.1, 0.1); |
| 72 | String x_label = "x" ; |
| 73 | String y_label = "y" ; |
| 74 | BlendMode blend_mode = BLEND_MODE_INTERPOLATED; |
| 75 | |
| 76 | void _add_blend_point(int p_index, const Ref<AnimationRootNode> &p_node); |
| 77 | void _set_triangles(const Vector<int> &p_triangles); |
| 78 | Vector<int> _get_triangles() const; |
| 79 | |
| 80 | void _blend_triangle(const Vector2 &p_pos, const Vector2 *p_points, float *r_weights); |
| 81 | |
| 82 | bool auto_triangles = true; |
| 83 | bool trianges_dirty = false; |
| 84 | |
| 85 | void _update_triangles(); |
| 86 | void _queue_auto_triangles(); |
| 87 | |
| 88 | bool sync = false; |
| 89 | |
| 90 | void _validate_property(PropertyInfo &p_property) const; |
| 91 | static void _bind_methods(); |
| 92 | |
| 93 | virtual void _tree_changed() override; |
| 94 | virtual void _animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name) override; |
| 95 | virtual void _animation_node_removed(const ObjectID &p_oid, const StringName &p_node) override; |
| 96 | |
| 97 | public: |
| 98 | virtual void get_parameter_list(List<PropertyInfo> *r_list) const override; |
| 99 | virtual Variant get_parameter_default_value(const StringName &p_parameter) const override; |
| 100 | |
| 101 | virtual void get_child_nodes(List<ChildNode> *r_child_nodes) override; |
| 102 | |
| 103 | void add_blend_point(const Ref<AnimationRootNode> &p_node, const Vector2 &p_position, int p_at_index = -1); |
| 104 | void set_blend_point_position(int p_point, const Vector2 &p_position); |
| 105 | void set_blend_point_node(int p_point, const Ref<AnimationRootNode> &p_node); |
| 106 | Vector2 get_blend_point_position(int p_point) const; |
| 107 | Ref<AnimationRootNode> get_blend_point_node(int p_point) const; |
| 108 | void remove_blend_point(int p_point); |
| 109 | int get_blend_point_count() const; |
| 110 | |
| 111 | bool has_triangle(int p_x, int p_y, int p_z) const; |
| 112 | void add_triangle(int p_x, int p_y, int p_z, int p_at_index = -1); |
| 113 | int get_triangle_point(int p_triangle, int p_point); |
| 114 | void remove_triangle(int p_triangle); |
| 115 | int get_triangle_count() const; |
| 116 | |
| 117 | void set_min_space(const Vector2 &p_min); |
| 118 | Vector2 get_min_space() const; |
| 119 | |
| 120 | void set_max_space(const Vector2 &p_max); |
| 121 | Vector2 get_max_space() const; |
| 122 | |
| 123 | void set_snap(const Vector2 &p_snap); |
| 124 | Vector2 get_snap() const; |
| 125 | |
| 126 | void set_x_label(const String &p_label); |
| 127 | String get_x_label() const; |
| 128 | |
| 129 | void set_y_label(const String &p_label); |
| 130 | String get_y_label() const; |
| 131 | |
| 132 | virtual double _process(double p_time, bool p_seek, bool p_is_external_seeking, bool p_test_only = false) override; |
| 133 | virtual String get_caption() const override; |
| 134 | |
| 135 | Vector2 get_closest_point(const Vector2 &p_point); |
| 136 | |
| 137 | void set_auto_triangles(bool p_enable); |
| 138 | bool get_auto_triangles() const; |
| 139 | |
| 140 | void set_blend_mode(BlendMode p_blend_mode); |
| 141 | BlendMode get_blend_mode() const; |
| 142 | |
| 143 | void set_use_sync(bool p_sync); |
| 144 | bool is_using_sync() const; |
| 145 | |
| 146 | virtual Ref<AnimationNode> get_child_by_name(const StringName &p_name) const override; |
| 147 | |
| 148 | AnimationNodeBlendSpace2D(); |
| 149 | ~AnimationNodeBlendSpace2D(); |
| 150 | }; |
| 151 | |
| 152 | VARIANT_ENUM_CAST(AnimationNodeBlendSpace2D::BlendMode) |
| 153 | |
| 154 | #endif // ANIMATION_BLEND_SPACE_2D_H |
| 155 | |