animation_bezier_editor.h 6.9 KB

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