animation_bezier_editor.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**************************************************************************/
  2. /* animation_bezier_editor.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 ANIMATION_BEZIER_EDITOR_H
  31. #define ANIMATION_BEZIER_EDITOR_H
  32. #include "animation_track_editor.h"
  33. #include "core/templates/hashfuncs.h"
  34. class ViewPanner;
  35. class AnimationBezierTrackEdit : public Control {
  36. GDCLASS(AnimationBezierTrackEdit, Control);
  37. enum {
  38. MENU_KEY_INSERT,
  39. MENU_KEY_DUPLICATE,
  40. MENU_KEY_CUT,
  41. MENU_KEY_COPY,
  42. MENU_KEY_PASTE,
  43. MENU_KEY_DELETE,
  44. MENU_KEY_SET_HANDLE_FREE,
  45. MENU_KEY_SET_HANDLE_LINEAR,
  46. MENU_KEY_SET_HANDLE_BALANCED,
  47. MENU_KEY_SET_HANDLE_MIRRORED,
  48. MENU_KEY_SET_HANDLE_AUTO_BALANCED,
  49. MENU_KEY_SET_HANDLE_AUTO_MIRRORED,
  50. };
  51. AnimationTimelineEdit *timeline = nullptr;
  52. Node *root = nullptr;
  53. Control *play_position = nullptr; //separate control used to draw so updates for only position changed are much faster
  54. real_t play_position_pos = 0;
  55. Ref<Animation> animation;
  56. bool read_only = false;
  57. int selected_track = 0;
  58. Vector<Rect2> view_rects;
  59. Ref<Texture2D> bezier_icon;
  60. Ref<Texture2D> bezier_handle_icon;
  61. Ref<Texture2D> selected_icon;
  62. RBMap<int, Rect2> subtracks;
  63. enum {
  64. REMOVE_ICON,
  65. LOCK_ICON,
  66. SOLO_ICON,
  67. VISIBILITY_ICON
  68. };
  69. RBMap<int, RBMap<int, Rect2>> subtrack_icons;
  70. HashSet<int> locked_tracks;
  71. HashSet<int> hidden_tracks;
  72. int solo_track = -1;
  73. bool is_filtered = false;
  74. float track_v_scroll = 0;
  75. float track_v_scroll_max = 0;
  76. float timeline_v_scroll = 0;
  77. float timeline_v_zoom = 1;
  78. PopupMenu *menu = nullptr;
  79. void _zoom_changed();
  80. void _update_locked_tracks_after(int p_track);
  81. void _update_hidden_tracks_after(int p_track);
  82. virtual void gui_input(const Ref<InputEvent> &p_event) override;
  83. void _menu_selected(int p_index);
  84. void _play_position_draw();
  85. bool _is_track_displayed(int p_track_index);
  86. bool _is_track_curves_displayed(int p_track_index);
  87. Vector2 insert_at_pos;
  88. typedef Pair<int, int> IntPair;
  89. bool moving_selection_attempt = false;
  90. float moving_selection_mouse_begin_x = 0.0;
  91. IntPair select_single_attempt;
  92. bool moving_selection = false;
  93. int moving_selection_from_key = 0;
  94. int moving_selection_from_track = 0;
  95. Vector2 moving_selection_offset;
  96. bool box_selecting_attempt = false;
  97. bool box_selecting = false;
  98. bool box_selecting_add = false;
  99. Vector2 box_selection_from;
  100. Vector2 box_selection_to;
  101. int moving_handle = 0; //0 no move -1 or +1 out, 2 both (drawing only)
  102. int moving_handle_key = 0;
  103. int moving_handle_track = 0;
  104. Vector2 moving_handle_left;
  105. Vector2 moving_handle_right;
  106. int moving_handle_mode = 0; // value from Animation::HandleMode
  107. struct PairHasher {
  108. static _FORCE_INLINE_ uint32_t hash(const Pair<int, int> &p_value) {
  109. int32_t hash = 23;
  110. hash = hash * 31 * hash_one_uint64(p_value.first);
  111. hash = hash * 31 * hash_one_uint64(p_value.second);
  112. return hash;
  113. }
  114. };
  115. HashMap<Pair<int, int>, Vector2, PairHasher> additional_moving_handle_lefts;
  116. HashMap<Pair<int, int>, Vector2, PairHasher> additional_moving_handle_rights;
  117. void _clear_selection();
  118. void _clear_selection_for_anim(const Ref<Animation> &p_anim);
  119. void _select_at_anim(const Ref<Animation> &p_anim, int p_track, real_t p_pos);
  120. bool _try_select_at_ui_pos(const Point2 &p_pos, bool p_aggregate, bool p_deselectable);
  121. void _change_selected_keys_handle_mode(Animation::HandleMode p_mode, bool p_auto = false);
  122. Vector2 menu_insert_key;
  123. struct AnimMoveRestore {
  124. int track = 0;
  125. double time = 0;
  126. Variant key;
  127. real_t transition = 0;
  128. };
  129. AnimationTrackEditor *editor = nullptr;
  130. struct EditPoint {
  131. Rect2 point_rect;
  132. Rect2 in_rect;
  133. Rect2 out_rect;
  134. int track = 0;
  135. int key = 0;
  136. };
  137. Vector<EditPoint> edit_points;
  138. struct PairCompare {
  139. bool operator()(const IntPair &lh, const IntPair &rh) {
  140. if (lh.first == rh.first) {
  141. return lh.second < rh.second;
  142. } else {
  143. return lh.first < rh.first;
  144. }
  145. }
  146. };
  147. typedef RBSet<IntPair, PairCompare> SelectionSet;
  148. SelectionSet selection;
  149. Ref<ViewPanner> panner;
  150. void _pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event);
  151. void _zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event);
  152. void _draw_line_clipped(const Vector2 &p_from, const Vector2 &p_to, const Color &p_color, int p_clip_left, int p_clip_right);
  153. void _draw_track(int p_track, const Color &p_color);
  154. float _bezier_h_to_pixel(float p_h);
  155. void _zoom_vertically(real_t p_minimum_value, real_t p_maximum_value);
  156. protected:
  157. static void _bind_methods();
  158. void _notification(int p_what);
  159. public:
  160. static Array make_default_bezier_key(float p_value);
  161. static float get_bezier_key_value(Array p_bezier_key_array);
  162. virtual String get_tooltip(const Point2 &p_pos) const override;
  163. Ref<Animation> get_animation() const;
  164. void set_animation_and_track(const Ref<Animation> &p_animation, int p_track, bool p_read_only);
  165. virtual Size2 get_minimum_size() const override;
  166. void set_timeline(AnimationTimelineEdit *p_timeline);
  167. void set_editor(AnimationTrackEditor *p_editor);
  168. void set_root(Node *p_root);
  169. void set_filtered(bool p_filtered);
  170. void auto_fit_vertically();
  171. void set_play_position(real_t p_pos);
  172. void update_play_position();
  173. void duplicate_selected_keys(real_t p_ofs, bool p_ofs_valid);
  174. void copy_selected_keys(bool p_cut);
  175. void paste_keys(real_t p_ofs, bool p_ofs_valid);
  176. void delete_selection();
  177. void _bezier_track_insert_key(int p_track, double p_time, real_t p_value, const Vector2 &p_in_handle, const Vector2 &p_out_handle, const Animation::HandleMode p_handle_mode);
  178. AnimationBezierTrackEdit();
  179. };
  180. #endif // ANIMATION_BEZIER_EDITOR_H