animation_bezier_editor.h 7.8 KB

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