animation_track_editor_plugins.cpp 47 KB

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