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