animation_track_editor_plugins.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  1. /**************************************************************************/
  2. /* animation_track_editor_plugins.cpp */
  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. #include "animation_track_editor_plugins.h"
  31. #include "editor/audio_stream_preview.h"
  32. #include "editor/editor_resource_preview.h"
  33. #include "editor/editor_string_names.h"
  34. #include "editor/editor_undo_redo_manager.h"
  35. #include "editor/themes/editor_scale.h"
  36. #include "scene/2d/animated_sprite_2d.h"
  37. #include "scene/2d/sprite_2d.h"
  38. #include "scene/3d/sprite_3d.h"
  39. #include "scene/animation/animation_player.h"
  40. #include "scene/resources/text_line.h"
  41. #include "servers/audio/audio_stream.h"
  42. /// BOOL ///
  43. int AnimationTrackEditBool::get_key_height() const {
  44. Ref<Texture2D> checked = get_theme_icon(SNAME("checked"), SNAME("CheckBox"));
  45. return checked->get_height();
  46. }
  47. Rect2 AnimationTrackEditBool::get_key_rect(int p_index, float p_pixels_sec) {
  48. Ref<Texture2D> checked = get_theme_icon(SNAME("checked"), SNAME("CheckBox"));
  49. return Rect2(-checked->get_width() / 2, 0, checked->get_width(), get_size().height);
  50. }
  51. bool AnimationTrackEditBool::is_key_selectable_by_distance() const {
  52. return false;
  53. }
  54. void AnimationTrackEditBool::draw_key(int p_index, float p_pixels_sec, int p_x, bool p_selected, int p_clip_left, int p_clip_right) {
  55. bool checked = get_animation()->track_get_key_value(get_track(), p_index);
  56. Ref<Texture2D> icon = get_theme_icon(checked ? "checked" : "unchecked", "CheckBox");
  57. Vector2 ofs(p_x - icon->get_width() / 2, int(get_size().height - icon->get_height()) / 2);
  58. if (ofs.x + icon->get_width() / 2 < p_clip_left) {
  59. return;
  60. }
  61. if (ofs.x + icon->get_width() / 2 > p_clip_right) {
  62. return;
  63. }
  64. draw_texture(icon, ofs);
  65. if (p_selected) {
  66. Color color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  67. draw_rect_clipped(Rect2(ofs, icon->get_size()), color, false);
  68. }
  69. }
  70. /// COLOR ///
  71. int AnimationTrackEditColor::get_key_height() const {
  72. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
  73. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
  74. return font->get_height(font_size) * 0.8;
  75. }
  76. Rect2 AnimationTrackEditColor::get_key_rect(int p_index, float p_pixels_sec) {
  77. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
  78. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
  79. int fh = font->get_height(font_size) * 0.8;
  80. return Rect2(-fh / 2, 0, fh, get_size().height);
  81. }
  82. bool AnimationTrackEditColor::is_key_selectable_by_distance() const {
  83. return false;
  84. }
  85. void AnimationTrackEditColor::draw_key_link(int p_index, float p_pixels_sec, int p_x, int p_next_x, int p_clip_left, int p_clip_right) {
  86. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
  87. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
  88. int fh = (font->get_height(font_size) * 0.8);
  89. fh /= 3;
  90. int x_from = p_x + fh / 2 - 1;
  91. int x_to = p_next_x - fh / 2 + 1;
  92. x_from = MAX(x_from, p_clip_left);
  93. x_to = MIN(x_to, p_clip_right);
  94. int y_from = (get_size().height - fh) / 2;
  95. if (x_from > p_clip_right || x_to < p_clip_left) {
  96. return;
  97. }
  98. Vector<Color> color_samples;
  99. color_samples.append(get_animation()->track_get_key_value(get_track(), p_index));
  100. if (get_animation()->track_get_type(get_track()) == Animation::TYPE_VALUE) {
  101. if (get_animation()->track_get_interpolation_type(get_track()) != Animation::INTERPOLATION_NEAREST &&
  102. (get_animation()->value_track_get_update_mode(get_track()) == Animation::UPDATE_CONTINUOUS ||
  103. get_animation()->value_track_get_update_mode(get_track()) == Animation::UPDATE_CAPTURE) &&
  104. !Math::is_zero_approx(get_animation()->track_get_key_transition(get_track(), p_index))) {
  105. float start_time = get_animation()->track_get_key_time(get_track(), p_index);
  106. float end_time = get_animation()->track_get_key_time(get_track(), p_index + 1);
  107. Color color_next = get_animation()->value_track_interpolate(get_track(), end_time);
  108. if (!color_samples[0].is_equal_approx(color_next)) {
  109. color_samples.resize(1 + (x_to - x_from) / 64); // Make a color sample every 64 px.
  110. for (int i = 1; i < color_samples.size(); i++) {
  111. float j = i;
  112. color_samples.write[i] = get_animation()->value_track_interpolate(
  113. get_track(),
  114. Math::lerp(start_time, end_time, j / color_samples.size()));
  115. }
  116. }
  117. color_samples.append(color_next);
  118. } else {
  119. color_samples.append(color_samples[0]);
  120. }
  121. } else {
  122. color_samples.append(get_animation()->track_get_key_value(get_track(), p_index + 1));
  123. }
  124. for (int i = 0; i < color_samples.size() - 1; i++) {
  125. Vector<Vector2> points = {
  126. Vector2(Math::lerp(x_from, x_to, float(i) / (color_samples.size() - 1)), y_from),
  127. Vector2(Math::lerp(x_from, x_to, float(i + 1) / (color_samples.size() - 1)), y_from),
  128. Vector2(Math::lerp(x_from, x_to, float(i + 1) / (color_samples.size() - 1)), y_from + fh),
  129. Vector2(Math::lerp(x_from, x_to, float(i) / (color_samples.size() - 1)), y_from + fh)
  130. };
  131. Vector<Color> colors = {
  132. color_samples[i],
  133. color_samples[i + 1],
  134. color_samples[i + 1],
  135. color_samples[i]
  136. };
  137. draw_primitive(points, colors, Vector<Vector2>());
  138. }
  139. }
  140. void AnimationTrackEditColor::draw_key(int p_index, float p_pixels_sec, int p_x, bool p_selected, int p_clip_left, int p_clip_right) {
  141. Color color = get_animation()->track_get_key_value(get_track(), p_index);
  142. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
  143. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
  144. int fh = font->get_height(font_size) * 0.8;
  145. Rect2 rect(Vector2(p_x - fh / 2, int(get_size().height - fh) / 2), Size2(fh, fh));
  146. draw_rect_clipped(Rect2(rect.position, rect.size / 2), Color(0.4, 0.4, 0.4));
  147. draw_rect_clipped(Rect2(rect.position + rect.size / 2, rect.size / 2), Color(0.4, 0.4, 0.4));
  148. draw_rect_clipped(Rect2(rect.position + Vector2(rect.size.x / 2, 0), rect.size / 2), Color(0.6, 0.6, 0.6));
  149. draw_rect_clipped(Rect2(rect.position + Vector2(0, rect.size.y / 2), rect.size / 2), Color(0.6, 0.6, 0.6));
  150. draw_rect_clipped(rect, color);
  151. if (p_selected) {
  152. Color accent = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  153. draw_rect_clipped(rect, accent, false);
  154. }
  155. }
  156. /// AUDIO ///
  157. void AnimationTrackEditAudio::_preview_changed(ObjectID p_which) {
  158. Object *object = ObjectDB::get_instance(id);
  159. if (!object) {
  160. return;
  161. }
  162. Ref<AudioStream> stream = object->call("get_stream");
  163. if (stream.is_valid() && stream->get_instance_id() == p_which) {
  164. queue_redraw();
  165. }
  166. }
  167. int AnimationTrackEditAudio::get_key_height() const {
  168. if (!ObjectDB::get_instance(id)) {
  169. return AnimationTrackEdit::get_key_height();
  170. }
  171. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
  172. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
  173. return int(font->get_height(font_size) * 1.5);
  174. }
  175. Rect2 AnimationTrackEditAudio::get_key_rect(int p_index, float p_pixels_sec) {
  176. Object *object = ObjectDB::get_instance(id);
  177. if (!object) {
  178. return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
  179. }
  180. Ref<AudioStream> stream = object->call("get_stream");
  181. if (!stream.is_valid()) {
  182. return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
  183. }
  184. bool play = get_animation()->track_get_key_value(get_track(), p_index);
  185. if (play) {
  186. float len = stream->get_length();
  187. if (len == 0) {
  188. Ref<AudioStreamPreview> preview = AudioStreamPreviewGenerator::get_singleton()->generate_preview(stream);
  189. len = preview->get_length();
  190. }
  191. if (get_animation()->track_get_key_count(get_track()) > p_index + 1) {
  192. len = MIN(len, get_animation()->track_get_key_time(get_track(), p_index + 1) - get_animation()->track_get_key_time(get_track(), p_index));
  193. }
  194. return Rect2(0, 0, len * p_pixels_sec, get_size().height);
  195. } else {
  196. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
  197. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
  198. int fh = font->get_height(font_size) * 0.8;
  199. return Rect2(0, 0, fh, get_size().height);
  200. }
  201. }
  202. bool AnimationTrackEditAudio::is_key_selectable_by_distance() const {
  203. return false;
  204. }
  205. void AnimationTrackEditAudio::draw_key(int p_index, float p_pixels_sec, int p_x, bool p_selected, int p_clip_left, int p_clip_right) {
  206. Object *object = ObjectDB::get_instance(id);
  207. if (!object) {
  208. AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right);
  209. return;
  210. }
  211. Ref<AudioStream> stream = object->call("get_stream");
  212. if (!stream.is_valid()) {
  213. AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right);
  214. return;
  215. }
  216. bool play = get_animation()->track_get_key_value(get_track(), p_index);
  217. if (play) {
  218. float len = stream->get_length();
  219. Ref<AudioStreamPreview> preview = AudioStreamPreviewGenerator::get_singleton()->generate_preview(stream);
  220. float preview_len = preview->get_length();
  221. if (len == 0) {
  222. len = preview_len;
  223. }
  224. int pixel_len = len * p_pixels_sec;
  225. int pixel_begin = p_x;
  226. int pixel_end = p_x + pixel_len;
  227. if (pixel_end < p_clip_left) {
  228. return;
  229. }
  230. if (pixel_begin > p_clip_right) {
  231. return;
  232. }
  233. int from_x = MAX(pixel_begin, p_clip_left);
  234. int to_x = MIN(pixel_end, p_clip_right);
  235. if (get_animation()->track_get_key_count(get_track()) > p_index + 1) {
  236. float limit = MIN(len, get_animation()->track_get_key_time(get_track(), p_index + 1) - get_animation()->track_get_key_time(get_track(), p_index));
  237. int limit_x = pixel_begin + limit * p_pixels_sec;
  238. to_x = MIN(limit_x, to_x);
  239. }
  240. if (to_x <= from_x) {
  241. return;
  242. }
  243. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
  244. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
  245. float fh = int(font->get_height(font_size) * 1.5);
  246. Rect2 rect = Rect2(from_x, (get_size().height - fh) / 2, to_x - from_x, fh);
  247. draw_rect(rect, Color(0.25, 0.25, 0.25));
  248. Vector<Vector2> points;
  249. points.resize((to_x - from_x) * 2);
  250. preview_len = preview->get_length();
  251. for (int i = from_x; i < to_x; i++) {
  252. float ofs = (i - pixel_begin) * preview_len / pixel_len;
  253. float ofs_n = ((i + 1) - pixel_begin) * preview_len / pixel_len;
  254. float max = preview->get_max(ofs, ofs_n) * 0.5 + 0.5;
  255. float min = preview->get_min(ofs, ofs_n) * 0.5 + 0.5;
  256. int idx = i - from_x;
  257. points.write[idx * 2 + 0] = Vector2(i, rect.position.y + min * rect.size.y);
  258. points.write[idx * 2 + 1] = Vector2(i, rect.position.y + max * rect.size.y);
  259. }
  260. Vector<Color> colors = { Color(0.75, 0.75, 0.75) };
  261. RS::get_singleton()->canvas_item_add_multiline(get_canvas_item(), points, colors);
  262. if (p_selected) {
  263. Color accent = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  264. draw_rect(rect, accent, false);
  265. }
  266. } else {
  267. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
  268. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
  269. int fh = font->get_height(font_size) * 0.8;
  270. Rect2 rect(Vector2(p_x, int(get_size().height - fh) / 2), Size2(fh, fh));
  271. Color color = get_theme_color(SceneStringName(font_color), SNAME("Label"));
  272. draw_rect_clipped(rect, color);
  273. if (p_selected) {
  274. Color accent = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  275. draw_rect_clipped(rect, accent, false);
  276. }
  277. }
  278. }
  279. void AnimationTrackEditAudio::set_node(Object *p_object) {
  280. id = p_object->get_instance_id();
  281. }
  282. AnimationTrackEditAudio::AnimationTrackEditAudio() {
  283. AudioStreamPreviewGenerator::get_singleton()->connect("preview_updated", callable_mp(this, &AnimationTrackEditAudio::_preview_changed));
  284. }
  285. /// SPRITE FRAME / FRAME_COORDS ///
  286. int AnimationTrackEditSpriteFrame::get_key_height() const {
  287. if (!ObjectDB::get_instance(id)) {
  288. return AnimationTrackEdit::get_key_height();
  289. }
  290. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
  291. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
  292. return int(font->get_height(font_size) * 2);
  293. }
  294. Rect2 AnimationTrackEditSpriteFrame::get_key_rect(int p_index, float p_pixels_sec) {
  295. Object *object = ObjectDB::get_instance(id);
  296. if (!object) {
  297. return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
  298. }
  299. Size2 size;
  300. if (Object::cast_to<Sprite2D>(object) || Object::cast_to<Sprite3D>(object)) {
  301. Ref<Texture2D> texture = object->call("get_texture");
  302. if (!texture.is_valid()) {
  303. return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
  304. }
  305. size = texture->get_size();
  306. if (bool(object->call("is_region_enabled"))) {
  307. size = Rect2(object->call("get_region_rect")).size;
  308. }
  309. int hframes = object->call("get_hframes");
  310. int vframes = object->call("get_vframes");
  311. if (hframes > 1) {
  312. size.x /= hframes;
  313. }
  314. if (vframes > 1) {
  315. size.y /= vframes;
  316. }
  317. } else if (Object::cast_to<AnimatedSprite2D>(object) || Object::cast_to<AnimatedSprite3D>(object)) {
  318. Ref<SpriteFrames> sf = object->call("get_sprite_frames");
  319. if (sf.is_null()) {
  320. return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
  321. }
  322. List<StringName> animations;
  323. sf->get_animation_list(&animations);
  324. int frame = get_animation()->track_get_key_value(get_track(), p_index);
  325. String animation_name;
  326. if (animations.size() == 1) {
  327. animation_name = animations.front()->get();
  328. } else {
  329. // Go through other track to find if animation is set
  330. String animation_path = get_animation()->track_get_path(get_track());
  331. animation_path = animation_path.replace(":frame", ":animation");
  332. int animation_track = get_animation()->find_track(animation_path, get_animation()->track_get_type(get_track()));
  333. float track_time = get_animation()->track_get_key_time(get_track(), p_index);
  334. int animaiton_index = get_animation()->track_find_key(animation_track, track_time);
  335. animation_name = get_animation()->track_get_key_value(animation_track, animaiton_index);
  336. }
  337. Ref<Texture2D> texture = sf->get_frame_texture(animation_name, frame);
  338. if (!texture.is_valid()) {
  339. return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
  340. }
  341. size = texture->get_size();
  342. }
  343. size = size.floor();
  344. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
  345. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
  346. int height = int(font->get_height(font_size) * 2);
  347. int width = height * size.width / size.height;
  348. return Rect2(0, 0, width, get_size().height);
  349. }
  350. bool AnimationTrackEditSpriteFrame::is_key_selectable_by_distance() const {
  351. return false;
  352. }
  353. void AnimationTrackEditSpriteFrame::draw_key(int p_index, float p_pixels_sec, int p_x, bool p_selected, int p_clip_left, int p_clip_right) {
  354. Object *object = ObjectDB::get_instance(id);
  355. if (!object) {
  356. AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right);
  357. return;
  358. }
  359. Ref<Texture2D> texture;
  360. Rect2 region;
  361. if (Object::cast_to<Sprite2D>(object) || Object::cast_to<Sprite3D>(object)) {
  362. texture = object->call("get_texture");
  363. if (!texture.is_valid()) {
  364. AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right);
  365. return;
  366. }
  367. int hframes = object->call("get_hframes");
  368. int vframes = object->call("get_vframes");
  369. Vector2 coords;
  370. if (is_coords) {
  371. coords = get_animation()->track_get_key_value(get_track(), p_index);
  372. } else {
  373. int frame = get_animation()->track_get_key_value(get_track(), p_index);
  374. coords.x = frame % hframes;
  375. coords.y = frame / hframes;
  376. }
  377. region.size = texture->get_size();
  378. if (bool(object->call("is_region_enabled"))) {
  379. region = Rect2(object->call("get_region_rect"));
  380. }
  381. if (hframes > 1) {
  382. region.size.x /= hframes;
  383. }
  384. if (vframes > 1) {
  385. region.size.y /= vframes;
  386. }
  387. region.position.x += region.size.x * coords.x;
  388. region.position.y += region.size.y * coords.y;
  389. } else if (Object::cast_to<AnimatedSprite2D>(object) || Object::cast_to<AnimatedSprite3D>(object)) {
  390. Ref<SpriteFrames> sf = object->call("get_sprite_frames");
  391. if (sf.is_null()) {
  392. AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right);
  393. return;
  394. }
  395. List<StringName> animations;
  396. sf->get_animation_list(&animations);
  397. int frame = get_animation()->track_get_key_value(get_track(), p_index);
  398. String animation_name;
  399. if (animations.size() == 1) {
  400. animation_name = animations.front()->get();
  401. } else {
  402. // Go through other track to find if animation is set
  403. String animation_path = get_animation()->track_get_path(get_track());
  404. animation_path = animation_path.replace(":frame", ":animation");
  405. int animation_track = get_animation()->find_track(animation_path, get_animation()->track_get_type(get_track()));
  406. float track_time = get_animation()->track_get_key_time(get_track(), p_index);
  407. int animaiton_index = get_animation()->track_find_key(animation_track, track_time);
  408. animation_name = get_animation()->track_get_key_value(animation_track, animaiton_index);
  409. }
  410. texture = sf->get_frame_texture(animation_name, frame);
  411. if (!texture.is_valid()) {
  412. AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right);
  413. return;
  414. }
  415. region.size = texture->get_size();
  416. }
  417. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
  418. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
  419. int height = int(font->get_height(font_size) * 2);
  420. int width = height * region.size.width / region.size.height;
  421. Rect2 rect(p_x, int(get_size().height - height) / 2, width, height);
  422. if (rect.position.x + rect.size.x < p_clip_left) {
  423. return;
  424. }
  425. if (rect.position.x > p_clip_right) {
  426. return;
  427. }
  428. Color accent = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  429. Color bg = accent;
  430. bg.a = 0.15;
  431. draw_rect_clipped(rect, bg);
  432. draw_texture_region_clipped(texture, rect, region);
  433. if (p_selected) {
  434. draw_rect_clipped(rect, accent, false);
  435. }
  436. }
  437. void AnimationTrackEditSpriteFrame::set_node(Object *p_object) {
  438. id = p_object->get_instance_id();
  439. }
  440. void AnimationTrackEditSpriteFrame::set_as_coords() {
  441. is_coords = true;
  442. }
  443. /// SUB ANIMATION ///
  444. int AnimationTrackEditSubAnim::get_key_height() const {
  445. if (!ObjectDB::get_instance(id)) {
  446. return AnimationTrackEdit::get_key_height();
  447. }
  448. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
  449. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
  450. return int(font->get_height(font_size) * 1.5);
  451. }
  452. Rect2 AnimationTrackEditSubAnim::get_key_rect(int p_index, float p_pixels_sec) {
  453. Object *object = ObjectDB::get_instance(id);
  454. if (!object) {
  455. return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
  456. }
  457. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(object);
  458. if (!ap) {
  459. return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
  460. }
  461. String anim = get_animation()->track_get_key_value(get_track(), p_index);
  462. if (anim != "[stop]" && ap->has_animation(anim)) {
  463. float len = ap->get_animation(anim)->get_length();
  464. if (get_animation()->track_get_key_count(get_track()) > p_index + 1) {
  465. len = MIN(len, get_animation()->track_get_key_time(get_track(), p_index + 1) - get_animation()->track_get_key_time(get_track(), p_index));
  466. }
  467. return Rect2(0, 0, len * p_pixels_sec, get_size().height);
  468. } else {
  469. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
  470. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
  471. int fh = font->get_height(font_size) * 0.8;
  472. return Rect2(0, 0, fh, get_size().height);
  473. }
  474. }
  475. bool AnimationTrackEditSubAnim::is_key_selectable_by_distance() const {
  476. return false;
  477. }
  478. void AnimationTrackEditSubAnim::draw_key(int p_index, float p_pixels_sec, int p_x, bool p_selected, int p_clip_left, int p_clip_right) {
  479. Object *object = ObjectDB::get_instance(id);
  480. if (!object) {
  481. AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right);
  482. return;
  483. }
  484. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(object);
  485. if (!ap) {
  486. AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right);
  487. return;
  488. }
  489. String anim = get_animation()->track_get_key_value(get_track(), p_index);
  490. if (anim != "[stop]" && ap->has_animation(anim)) {
  491. float len = ap->get_animation(anim)->get_length();
  492. if (get_animation()->track_get_key_count(get_track()) > p_index + 1) {
  493. len = MIN(len, get_animation()->track_get_key_time(get_track(), p_index + 1) - get_animation()->track_get_key_time(get_track(), p_index));
  494. }
  495. int pixel_len = len * p_pixels_sec;
  496. int pixel_begin = p_x;
  497. int pixel_end = p_x + pixel_len;
  498. if (pixel_end < p_clip_left) {
  499. return;
  500. }
  501. if (pixel_begin > p_clip_right) {
  502. return;
  503. }
  504. int from_x = MAX(pixel_begin, p_clip_left);
  505. int to_x = MIN(pixel_end, p_clip_right);
  506. if (to_x <= from_x) {
  507. return;
  508. }
  509. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
  510. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
  511. int fh = font->get_height(font_size) * 1.5;
  512. Rect2 rect(from_x, int(get_size().height - fh) / 2, to_x - from_x, fh);
  513. Color color = get_theme_color(SceneStringName(font_color), SNAME("Label"));
  514. Color bg = color;
  515. bg.r = 1 - color.r;
  516. bg.g = 1 - color.g;
  517. bg.b = 1 - color.b;
  518. draw_rect(rect, bg);
  519. Vector<Vector2> points;
  520. Vector<Color> colors = { color };
  521. {
  522. Ref<Animation> ap_anim = ap->get_animation(anim);
  523. for (int i = 0; i < ap_anim->get_track_count(); i++) {
  524. float h = (rect.size.height - 2) / ap_anim->get_track_count();
  525. int y = 2 + h * i + h / 2;
  526. for (int j = 0; j < ap_anim->track_get_key_count(i); j++) {
  527. float ofs = ap_anim->track_get_key_time(i, j);
  528. int x = p_x + ofs * p_pixels_sec + 2;
  529. if (x < from_x || x >= (to_x - 4)) {
  530. continue;
  531. }
  532. points.push_back(Point2(x, y));
  533. points.push_back(Point2(x + 1, y));
  534. }
  535. }
  536. }
  537. if (points.size() > 2) {
  538. RS::get_singleton()->canvas_item_add_multiline(get_canvas_item(), points, colors);
  539. }
  540. int limit = to_x - from_x - 4;
  541. if (limit > 0) {
  542. draw_string(font, Point2(from_x + 2, int(get_size().height - font->get_height(font_size)) / 2 + font->get_ascent(font_size)), anim, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, color);
  543. }
  544. if (p_selected) {
  545. Color accent = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  546. draw_rect(rect, accent, false);
  547. }
  548. } else {
  549. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
  550. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
  551. int fh = font->get_height(font_size) * 0.8;
  552. Rect2 rect(Vector2(p_x, int(get_size().height - fh) / 2), Size2(fh, fh));
  553. Color color = get_theme_color(SceneStringName(font_color), SNAME("Label"));
  554. draw_rect_clipped(rect, color);
  555. if (p_selected) {
  556. Color accent = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  557. draw_rect_clipped(rect, accent, false);
  558. }
  559. }
  560. }
  561. void AnimationTrackEditSubAnim::set_node(Object *p_object) {
  562. id = p_object->get_instance_id();
  563. }
  564. //// VOLUME DB ////
  565. int AnimationTrackEditVolumeDB::get_key_height() const {
  566. Ref<Texture2D> volume_texture = get_editor_theme_icon(SNAME("ColorTrackVu"));
  567. return volume_texture->get_height() * 1.2;
  568. }
  569. void AnimationTrackEditVolumeDB::draw_bg(int p_clip_left, int p_clip_right) {
  570. Ref<Texture2D> volume_texture = get_editor_theme_icon(SNAME("ColorTrackVu"));
  571. int tex_h = volume_texture->get_height();
  572. int y_from = (get_size().height - tex_h) / 2;
  573. int y_size = tex_h;
  574. Color color(1, 1, 1, 0.3);
  575. draw_texture_rect(volume_texture, Rect2(p_clip_left, y_from, p_clip_right - p_clip_left, y_from + y_size), false, color);
  576. }
  577. void AnimationTrackEditVolumeDB::draw_fg(int p_clip_left, int p_clip_right) {
  578. Ref<Texture2D> volume_texture = get_editor_theme_icon(SNAME("ColorTrackVu"));
  579. int tex_h = volume_texture->get_height();
  580. int y_from = (get_size().height - tex_h) / 2;
  581. int db0 = y_from + (24 / 80.0) * tex_h;
  582. draw_line(Vector2(p_clip_left, db0), Vector2(p_clip_right, db0), Color(1, 1, 1, 0.3));
  583. }
  584. void AnimationTrackEditVolumeDB::draw_key_link(int p_index, float p_pixels_sec, int p_x, int p_next_x, int p_clip_left, int p_clip_right) {
  585. if (p_x > p_clip_right || p_next_x < p_clip_left) {
  586. return;
  587. }
  588. float db = get_animation()->track_get_key_value(get_track(), p_index);
  589. float db_n = get_animation()->track_get_key_value(get_track(), p_index + 1);
  590. db = CLAMP(db, -60, 24);
  591. db_n = CLAMP(db_n, -60, 24);
  592. float h = 1.0 - ((db + 60) / 84.0);
  593. float h_n = 1.0 - ((db_n + 60) / 84.0);
  594. int from_x = p_x;
  595. int to_x = p_next_x;
  596. if (from_x < p_clip_left) {
  597. h = Math::lerp(h, h_n, float(p_clip_left - from_x) / float(to_x - from_x));
  598. from_x = p_clip_left;
  599. }
  600. if (to_x > p_clip_right) {
  601. h_n = Math::lerp(h, h_n, float(p_clip_right - from_x) / float(to_x - from_x));
  602. to_x = p_clip_right;
  603. }
  604. Ref<Texture2D> volume_texture = get_editor_theme_icon(SNAME("ColorTrackVu"));
  605. int tex_h = volume_texture->get_height();
  606. int y_from = (get_size().height - tex_h) / 2;
  607. Color color = get_theme_color(SceneStringName(font_color), SNAME("Label"));
  608. color.a *= 0.7;
  609. draw_line(Point2(from_x, y_from + h * tex_h), Point2(to_x, y_from + h_n * tex_h), color, 2);
  610. }
  611. ////////////////////////
  612. /// AUDIO ///
  613. void AnimationTrackEditTypeAudio::_preview_changed(ObjectID p_which) {
  614. for (int i = 0; i < get_animation()->track_get_key_count(get_track()); i++) {
  615. Ref<AudioStream> stream = get_animation()->audio_track_get_key_stream(get_track(), i);
  616. if (stream.is_valid() && stream->get_instance_id() == p_which) {
  617. queue_redraw();
  618. return;
  619. }
  620. }
  621. }
  622. int AnimationTrackEditTypeAudio::get_key_height() const {
  623. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
  624. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
  625. return int(font->get_height(font_size) * 1.5);
  626. }
  627. Rect2 AnimationTrackEditTypeAudio::get_key_rect(int p_index, float p_pixels_sec) {
  628. Ref<AudioStream> stream = get_animation()->audio_track_get_key_stream(get_track(), p_index);
  629. if (!stream.is_valid()) {
  630. return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
  631. }
  632. float start_ofs = get_animation()->audio_track_get_key_start_offset(get_track(), p_index);
  633. float end_ofs = get_animation()->audio_track_get_key_end_offset(get_track(), p_index);
  634. float len = stream->get_length();
  635. if (len == 0) {
  636. Ref<AudioStreamPreview> preview = AudioStreamPreviewGenerator::get_singleton()->generate_preview(stream);
  637. len = preview->get_length();
  638. }
  639. len -= end_ofs;
  640. len -= start_ofs;
  641. if (len <= 0.0001) {
  642. len = 0.0001;
  643. }
  644. if (get_animation()->track_get_key_count(get_track()) > p_index + 1) {
  645. len = MIN(len, get_animation()->track_get_key_time(get_track(), p_index + 1) - get_animation()->track_get_key_time(get_track(), p_index));
  646. }
  647. return Rect2(0, 0, len * p_pixels_sec, get_size().height);
  648. }
  649. bool AnimationTrackEditTypeAudio::is_key_selectable_by_distance() const {
  650. return false;
  651. }
  652. void AnimationTrackEditTypeAudio::draw_key(int p_index, float p_pixels_sec, int p_x, bool p_selected, int p_clip_left, int p_clip_right) {
  653. Ref<AudioStream> stream = get_animation()->audio_track_get_key_stream(get_track(), p_index);
  654. if (!stream.is_valid()) {
  655. AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right); // Draw diamond.
  656. return;
  657. }
  658. float len = stream->get_length();
  659. if (len == 0) {
  660. AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right); // Draw diamond.
  661. return;
  662. }
  663. float start_ofs = get_animation()->audio_track_get_key_start_offset(get_track(), p_index);
  664. float end_ofs = get_animation()->audio_track_get_key_end_offset(get_track(), p_index);
  665. int px_offset = 0;
  666. if (len_resizing && p_index == len_resizing_index) {
  667. float ofs_local = len_resizing_rel / get_timeline()->get_zoom_scale();
  668. if (len_resizing_start) {
  669. start_ofs += ofs_local;
  670. px_offset = ofs_local * p_pixels_sec;
  671. } else {
  672. end_ofs -= ofs_local;
  673. }
  674. }
  675. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
  676. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
  677. float fh = int(font->get_height(font_size) * 1.5);
  678. Ref<AudioStreamPreview> preview = AudioStreamPreviewGenerator::get_singleton()->generate_preview(stream);
  679. int pixel_total_len = len * p_pixels_sec;
  680. len -= end_ofs;
  681. len -= start_ofs;
  682. if (len <= 0.0001) {
  683. len = 0.0001;
  684. }
  685. int pixel_len = len * p_pixels_sec;
  686. int pixel_begin = px_offset + p_x;
  687. int pixel_end = px_offset + p_x + pixel_len;
  688. if (pixel_end < p_clip_left) {
  689. return;
  690. }
  691. if (pixel_begin > p_clip_right) {
  692. return;
  693. }
  694. int from_x = MAX(pixel_begin, p_clip_left);
  695. int to_x = MIN(pixel_end, p_clip_right);
  696. if (get_animation()->track_get_key_count(get_track()) > p_index + 1) {
  697. float limit = MIN(len, get_animation()->track_get_key_time(get_track(), p_index + 1) - get_animation()->track_get_key_time(get_track(), p_index));
  698. int limit_x = pixel_begin + limit * p_pixels_sec;
  699. to_x = MIN(limit_x, to_x);
  700. }
  701. if (to_x <= from_x) {
  702. to_x = from_x + 1;
  703. }
  704. int h = get_size().height;
  705. Rect2 rect = Rect2(from_x, (h - fh) / 2, to_x - from_x, fh);
  706. draw_rect(rect, Color(0.25, 0.25, 0.25));
  707. Vector<Vector2> points;
  708. points.resize((to_x - from_x) * 2);
  709. float preview_len = preview->get_length();
  710. for (int i = from_x; i < to_x; i++) {
  711. float ofs = (i - pixel_begin) * preview_len / pixel_total_len;
  712. float ofs_n = ((i + 1) - pixel_begin) * preview_len / pixel_total_len;
  713. ofs += start_ofs;
  714. ofs_n += start_ofs;
  715. float max = preview->get_max(ofs, ofs_n) * 0.5 + 0.5;
  716. float min = preview->get_min(ofs, ofs_n) * 0.5 + 0.5;
  717. int idx = i - from_x;
  718. points.write[idx * 2 + 0] = Vector2(i, rect.position.y + min * rect.size.y);
  719. points.write[idx * 2 + 1] = Vector2(i, rect.position.y + max * rect.size.y);
  720. }
  721. Vector<Color> colors = { Color(0.75, 0.75, 0.75) };
  722. RS::get_singleton()->canvas_item_add_multiline(get_canvas_item(), points, colors);
  723. Color cut_color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  724. cut_color.a = 0.7;
  725. if (start_ofs > 0 && pixel_begin > p_clip_left) {
  726. draw_rect(Rect2(pixel_begin, rect.position.y, 1, rect.size.y), cut_color);
  727. }
  728. if (end_ofs > 0 && pixel_end < p_clip_right) {
  729. draw_rect(Rect2(pixel_end, rect.position.y, 1, rect.size.y), cut_color);
  730. }
  731. if (p_selected) {
  732. Color accent = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  733. draw_rect(rect, accent, false);
  734. }
  735. }
  736. AnimationTrackEditTypeAudio::AnimationTrackEditTypeAudio() {
  737. AudioStreamPreviewGenerator::get_singleton()->connect("preview_updated", callable_mp(this, &AnimationTrackEditTypeAudio::_preview_changed));
  738. }
  739. bool AnimationTrackEditTypeAudio::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  740. if (p_point.x > get_timeline()->get_name_limit() && p_point.x < get_size().width - get_timeline()->get_buttons_width()) {
  741. Dictionary drag_data = p_data;
  742. if (drag_data.has("type") && String(drag_data["type"]) == "resource") {
  743. Ref<AudioStream> res = drag_data["resource"];
  744. if (res.is_valid()) {
  745. return true;
  746. }
  747. }
  748. if (drag_data.has("type") && String(drag_data["type"]) == "files") {
  749. Vector<String> files = drag_data["files"];
  750. if (files.size() == 1) {
  751. Ref<AudioStream> res = ResourceLoader::load(files[0]);
  752. if (res.is_valid()) {
  753. return true;
  754. }
  755. }
  756. }
  757. }
  758. return AnimationTrackEdit::can_drop_data(p_point, p_data);
  759. }
  760. void AnimationTrackEditTypeAudio::drop_data(const Point2 &p_point, const Variant &p_data) {
  761. if (p_point.x > get_timeline()->get_name_limit() && p_point.x < get_size().width - get_timeline()->get_buttons_width()) {
  762. Ref<AudioStream> stream;
  763. Dictionary drag_data = p_data;
  764. if (drag_data.has("type") && String(drag_data["type"]) == "resource") {
  765. stream = drag_data["resource"];
  766. } else if (drag_data.has("type") && String(drag_data["type"]) == "files") {
  767. Vector<String> files = drag_data["files"];
  768. if (files.size() == 1) {
  769. stream = ResourceLoader::load(files[0]);
  770. }
  771. }
  772. if (stream.is_valid()) {
  773. int x = p_point.x - get_timeline()->get_name_limit();
  774. float ofs = x / get_timeline()->get_zoom_scale();
  775. ofs += get_timeline()->get_value();
  776. ofs = get_editor()->snap_time(ofs);
  777. while (get_animation()->track_find_key(get_track(), ofs, Animation::FIND_MODE_APPROX) != -1) { //make sure insertion point is valid
  778. ofs += 0.0001;
  779. }
  780. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  781. undo_redo->create_action(TTR("Add Audio Track Clip"));
  782. undo_redo->add_do_method(get_animation().ptr(), "audio_track_insert_key", get_track(), ofs, stream);
  783. undo_redo->add_undo_method(get_animation().ptr(), "track_remove_key_at_time", get_track(), ofs);
  784. undo_redo->commit_action();
  785. queue_redraw();
  786. return;
  787. }
  788. }
  789. AnimationTrackEdit::drop_data(p_point, p_data);
  790. }
  791. void AnimationTrackEditTypeAudio::gui_input(const Ref<InputEvent> &p_event) {
  792. ERR_FAIL_COND(p_event.is_null());
  793. Ref<InputEventMouseMotion> mm = p_event;
  794. if (!len_resizing && mm.is_valid()) {
  795. bool use_hsize_cursor = false;
  796. for (int i = 0; i < get_animation()->track_get_key_count(get_track()); i++) {
  797. Ref<AudioStream> stream = get_animation()->audio_track_get_key_stream(get_track(), i);
  798. if (!stream.is_valid()) {
  799. continue;
  800. }
  801. float len = stream->get_length();
  802. if (len == 0) {
  803. continue;
  804. }
  805. float start_ofs = get_animation()->audio_track_get_key_start_offset(get_track(), i);
  806. float end_ofs = get_animation()->audio_track_get_key_end_offset(get_track(), i);
  807. len -= end_ofs;
  808. len -= start_ofs;
  809. if (get_animation()->track_get_key_count(get_track()) > i + 1) {
  810. len = MIN(len, get_animation()->track_get_key_time(get_track(), i + 1) - get_animation()->track_get_key_time(get_track(), i));
  811. }
  812. float ofs = get_animation()->track_get_key_time(get_track(), i);
  813. ofs -= get_timeline()->get_value();
  814. ofs *= get_timeline()->get_zoom_scale();
  815. ofs += get_timeline()->get_name_limit();
  816. int end = ofs + len * get_timeline()->get_zoom_scale();
  817. if (end >= get_timeline()->get_name_limit() && end <= get_size().width - get_timeline()->get_buttons_width() && ABS(mm->get_position().x - end) < 5 * EDSCALE) {
  818. len_resizing_start = false;
  819. use_hsize_cursor = true;
  820. len_resizing_index = i;
  821. }
  822. if (ofs >= get_timeline()->get_name_limit() && ofs <= get_size().width - get_timeline()->get_buttons_width() && ABS(mm->get_position().x - ofs) < 5 * EDSCALE) {
  823. len_resizing_start = true;
  824. use_hsize_cursor = true;
  825. len_resizing_index = i;
  826. }
  827. }
  828. over_drag_position = use_hsize_cursor;
  829. }
  830. if (len_resizing && mm.is_valid()) {
  831. // Rezising index is some.
  832. len_resizing_rel += mm->get_relative().x;
  833. float ofs_local = len_resizing_rel / get_timeline()->get_zoom_scale();
  834. float prev_ofs_start = get_animation()->audio_track_get_key_start_offset(get_track(), len_resizing_index);
  835. float prev_ofs_end = get_animation()->audio_track_get_key_end_offset(get_track(), len_resizing_index);
  836. Ref<AudioStream> stream = get_animation()->audio_track_get_key_stream(get_track(), len_resizing_index);
  837. float len = stream->get_length();
  838. if (len == 0) {
  839. Ref<AudioStreamPreview> preview = AudioStreamPreviewGenerator::get_singleton()->generate_preview(stream);
  840. float preview_len = preview->get_length();
  841. len = preview_len;
  842. }
  843. if (len_resizing_start) {
  844. len_resizing_rel = CLAMP(ofs_local, -prev_ofs_start, len - prev_ofs_end - prev_ofs_start) * get_timeline()->get_zoom_scale();
  845. } else {
  846. len_resizing_rel = CLAMP(ofs_local, -(len - prev_ofs_end - prev_ofs_start), prev_ofs_end) * get_timeline()->get_zoom_scale();
  847. }
  848. queue_redraw();
  849. accept_event();
  850. return;
  851. }
  852. Ref<InputEventMouseButton> mb = p_event;
  853. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT && over_drag_position) {
  854. len_resizing = true;
  855. // In case if resizing index is not set yet reset the flag.
  856. if (len_resizing_index < 0) {
  857. len_resizing = false;
  858. return;
  859. }
  860. len_resizing_from_px = mb->get_position().x;
  861. len_resizing_rel = 0;
  862. queue_redraw();
  863. accept_event();
  864. return;
  865. }
  866. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  867. if (len_resizing && mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  868. if (len_resizing_rel == 0 || len_resizing_index < 0) {
  869. len_resizing = false;
  870. return;
  871. }
  872. if (len_resizing_start) {
  873. float ofs_local = len_resizing_rel / get_timeline()->get_zoom_scale();
  874. float prev_ofs = get_animation()->audio_track_get_key_start_offset(get_track(), len_resizing_index);
  875. float prev_time = get_animation()->track_get_key_time(get_track(), len_resizing_index);
  876. float new_ofs = prev_ofs + ofs_local;
  877. float new_time = prev_time + ofs_local;
  878. if (prev_time != new_time) {
  879. undo_redo->create_action(TTR("Change Audio Track Clip Start Offset"));
  880. undo_redo->add_do_method(get_animation().ptr(), "track_set_key_time", get_track(), len_resizing_index, new_time);
  881. undo_redo->add_undo_method(get_animation().ptr(), "track_set_key_time", get_track(), len_resizing_index, prev_time);
  882. undo_redo->add_do_method(get_animation().ptr(), "audio_track_set_key_start_offset", get_track(), len_resizing_index, new_ofs);
  883. undo_redo->add_undo_method(get_animation().ptr(), "audio_track_set_key_start_offset", get_track(), len_resizing_index, prev_ofs);
  884. undo_redo->commit_action();
  885. }
  886. } else {
  887. float ofs_local = -len_resizing_rel / get_timeline()->get_zoom_scale();
  888. float prev_ofs = get_animation()->audio_track_get_key_end_offset(get_track(), len_resizing_index);
  889. float new_ofs = prev_ofs + ofs_local;
  890. if (prev_ofs != new_ofs) {
  891. undo_redo->create_action(TTR("Change Audio Track Clip End Offset"));
  892. undo_redo->add_do_method(get_animation().ptr(), "audio_track_set_key_end_offset", get_track(), len_resizing_index, new_ofs);
  893. undo_redo->add_undo_method(get_animation().ptr(), "audio_track_set_key_end_offset", get_track(), len_resizing_index, prev_ofs);
  894. undo_redo->commit_action();
  895. }
  896. }
  897. len_resizing_index = -1;
  898. len_resizing = false;
  899. queue_redraw();
  900. accept_event();
  901. return;
  902. }
  903. AnimationTrackEdit::gui_input(p_event);
  904. }
  905. Control::CursorShape AnimationTrackEditTypeAudio::get_cursor_shape(const Point2 &p_pos) const {
  906. if (over_drag_position || len_resizing) {
  907. return Control::CURSOR_HSIZE;
  908. } else {
  909. return get_default_cursor_shape();
  910. }
  911. }
  912. ////////////////////
  913. /// SUB ANIMATION ///
  914. int AnimationTrackEditTypeAnimation::get_key_height() const {
  915. if (!ObjectDB::get_instance(id)) {
  916. return AnimationTrackEdit::get_key_height();
  917. }
  918. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
  919. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
  920. return int(font->get_height(font_size) * 1.5);
  921. }
  922. Rect2 AnimationTrackEditTypeAnimation::get_key_rect(int p_index, float p_pixels_sec) {
  923. Object *object = ObjectDB::get_instance(id);
  924. if (!object) {
  925. return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
  926. }
  927. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(object);
  928. if (!ap) {
  929. return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
  930. }
  931. String anim = get_animation()->animation_track_get_key_animation(get_track(), p_index);
  932. if (anim != "[stop]" && ap->has_animation(anim)) {
  933. float len = ap->get_animation(anim)->get_length();
  934. if (get_animation()->track_get_key_count(get_track()) > p_index + 1) {
  935. len = MIN(len, get_animation()->track_get_key_time(get_track(), p_index + 1) - get_animation()->track_get_key_time(get_track(), p_index));
  936. }
  937. return Rect2(0, 0, len * p_pixels_sec, get_size().height);
  938. } else {
  939. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
  940. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
  941. int fh = font->get_height(font_size) * 0.8;
  942. return Rect2(0, 0, fh, get_size().height);
  943. }
  944. }
  945. bool AnimationTrackEditTypeAnimation::is_key_selectable_by_distance() const {
  946. return false;
  947. }
  948. void AnimationTrackEditTypeAnimation::draw_key(int p_index, float p_pixels_sec, int p_x, bool p_selected, int p_clip_left, int p_clip_right) {
  949. Object *object = ObjectDB::get_instance(id);
  950. if (!object) {
  951. AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right);
  952. return;
  953. }
  954. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(object);
  955. if (!ap) {
  956. AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right);
  957. return;
  958. }
  959. String anim = get_animation()->animation_track_get_key_animation(get_track(), p_index);
  960. if (anim != "[stop]" && ap->has_animation(anim)) {
  961. float len = ap->get_animation(anim)->get_length();
  962. if (get_animation()->track_get_key_count(get_track()) > p_index + 1) {
  963. len = MIN(len, get_animation()->track_get_key_time(get_track(), p_index + 1) - get_animation()->track_get_key_time(get_track(), p_index));
  964. }
  965. int pixel_len = len * p_pixels_sec;
  966. int pixel_begin = p_x;
  967. int pixel_end = p_x + pixel_len;
  968. if (pixel_end < p_clip_left) {
  969. return;
  970. }
  971. if (pixel_begin > p_clip_right) {
  972. return;
  973. }
  974. int from_x = MAX(pixel_begin, p_clip_left);
  975. int to_x = MIN(pixel_end, p_clip_right);
  976. if (to_x <= from_x) {
  977. return;
  978. }
  979. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
  980. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
  981. int fh = font->get_height(font_size) * 1.5;
  982. Rect2 rect(from_x, int(get_size().height - fh) / 2, to_x - from_x, fh);
  983. Color color = get_theme_color(SceneStringName(font_color), SNAME("Label"));
  984. Color bg = color;
  985. bg.r = 1 - color.r;
  986. bg.g = 1 - color.g;
  987. bg.b = 1 - color.b;
  988. draw_rect(rect, bg);
  989. Vector<Vector2> points;
  990. Vector<Color> colors = { color };
  991. {
  992. Ref<Animation> ap_anim = ap->get_animation(anim);
  993. for (int i = 0; i < ap_anim->get_track_count(); i++) {
  994. float h = (rect.size.height - 2) / ap_anim->get_track_count();
  995. int y = 2 + h * i + h / 2;
  996. for (int j = 0; j < ap_anim->track_get_key_count(i); j++) {
  997. float ofs = ap_anim->track_get_key_time(i, j);
  998. int x = p_x + ofs * p_pixels_sec + 2;
  999. if (x < from_x || x >= (to_x - 4)) {
  1000. continue;
  1001. }
  1002. points.push_back(Point2(x, y));
  1003. points.push_back(Point2(x + 1, y));
  1004. }
  1005. }
  1006. }
  1007. if (points.size() > 2) {
  1008. RS::get_singleton()->canvas_item_add_multiline(get_canvas_item(), points, colors);
  1009. }
  1010. int limit = to_x - from_x - 4;
  1011. if (limit > 0) {
  1012. draw_string(font, Point2(from_x + 2, int(get_size().height - font->get_height(font_size)) / 2 + font->get_ascent(font_size)), anim, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, color);
  1013. }
  1014. if (p_selected) {
  1015. Color accent = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  1016. draw_rect(rect, accent, false);
  1017. }
  1018. } else {
  1019. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
  1020. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
  1021. int fh = font->get_height(font_size) * 0.8;
  1022. Rect2 rect(Vector2(p_x, int(get_size().height - fh) / 2), Size2(fh, fh));
  1023. Color color = get_theme_color(SceneStringName(font_color), SNAME("Label"));
  1024. draw_rect_clipped(rect, color);
  1025. if (p_selected) {
  1026. Color accent = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  1027. draw_rect_clipped(rect, accent, false);
  1028. }
  1029. }
  1030. }
  1031. void AnimationTrackEditTypeAnimation::set_node(Object *p_object) {
  1032. id = p_object->get_instance_id();
  1033. }
  1034. AnimationTrackEditTypeAnimation::AnimationTrackEditTypeAnimation() {
  1035. }
  1036. /////////
  1037. AnimationTrackEdit *AnimationTrackEditDefaultPlugin::create_value_track_edit(Object *p_object, Variant::Type p_type, const String &p_property, PropertyHint p_hint, const String &p_hint_string, int p_usage) {
  1038. if (p_property == "playing" && (p_object->is_class("AudioStreamPlayer") || p_object->is_class("AudioStreamPlayer2D") || p_object->is_class("AudioStreamPlayer3D"))) {
  1039. AnimationTrackEditAudio *audio = memnew(AnimationTrackEditAudio);
  1040. audio->set_node(p_object);
  1041. return audio;
  1042. }
  1043. if (p_property == "frame" && (p_object->is_class("Sprite2D") || p_object->is_class("Sprite3D") || p_object->is_class("AnimatedSprite2D") || p_object->is_class("AnimatedSprite3D"))) {
  1044. AnimationTrackEditSpriteFrame *sprite = memnew(AnimationTrackEditSpriteFrame);
  1045. sprite->set_node(p_object);
  1046. return sprite;
  1047. }
  1048. if (p_property == "frame_coords" && (p_object->is_class("Sprite2D") || p_object->is_class("Sprite3D"))) {
  1049. AnimationTrackEditSpriteFrame *sprite = memnew(AnimationTrackEditSpriteFrame);
  1050. sprite->set_as_coords();
  1051. sprite->set_node(p_object);
  1052. return sprite;
  1053. }
  1054. if (p_property == "current_animation" && (p_object->is_class("AnimationPlayer"))) {
  1055. AnimationTrackEditSubAnim *player = memnew(AnimationTrackEditSubAnim);
  1056. player->set_node(p_object);
  1057. return player;
  1058. }
  1059. if (p_property == "volume_db") {
  1060. AnimationTrackEditVolumeDB *vu = memnew(AnimationTrackEditVolumeDB);
  1061. return vu;
  1062. }
  1063. if (p_type == Variant::BOOL) {
  1064. return memnew(AnimationTrackEditBool);
  1065. }
  1066. if (p_type == Variant::COLOR) {
  1067. return memnew(AnimationTrackEditColor);
  1068. }
  1069. return nullptr;
  1070. }
  1071. AnimationTrackEdit *AnimationTrackEditDefaultPlugin::create_audio_track_edit() {
  1072. return memnew(AnimationTrackEditTypeAudio);
  1073. }
  1074. AnimationTrackEdit *AnimationTrackEditDefaultPlugin::create_animation_track_edit(Object *p_object) {
  1075. AnimationTrackEditTypeAnimation *an = memnew(AnimationTrackEditTypeAnimation);
  1076. an->set_node(p_object);
  1077. return an;
  1078. }