animation_track_editor_plugins.cpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  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) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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> lines;
  248. lines.resize((to_x - from_x + 1) * 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. lines.write[idx * 2 + 0] = Vector2(i, rect.position.y + min * rect.size.y);
  257. lines.write[idx * 2 + 1] = Vector2(i, rect.position.y + max * rect.size.y);
  258. }
  259. Vector<Color> color;
  260. color.push_back(Color(0.75, 0.75, 0.75));
  261. RS::get_singleton()->canvas_item_add_multiline(get_canvas_item(), lines, color);
  262. if (p_selected) {
  263. Color accent = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  264. draw_rect(rect, accent, false);
  265. }
  266. } else {
  267. Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Label"));
  268. int font_size = get_theme_font_size(SNAME("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(SNAME("font_color"), SNAME("Label"));
  272. draw_rect_clipped(rect, color);
  273. if (p_selected) {
  274. Color accent = get_theme_color(SNAME("accent_color"), SNAME("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. void AnimationTrackEditAudio::_bind_methods() {
  283. }
  284. AnimationTrackEditAudio::AnimationTrackEditAudio() {
  285. AudioStreamPreviewGenerator::get_singleton()->connect("preview_updated", callable_mp(this, &AnimationTrackEditAudio::_preview_changed));
  286. }
  287. /// SPRITE FRAME / FRAME_COORDS ///
  288. int AnimationTrackEditSpriteFrame::get_key_height() const {
  289. if (!ObjectDB::get_instance(id)) {
  290. return AnimationTrackEdit::get_key_height();
  291. }
  292. Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Label"));
  293. int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Label"));
  294. return int(font->get_height(font_size) * 2);
  295. }
  296. Rect2 AnimationTrackEditSpriteFrame::get_key_rect(int p_index, float p_pixels_sec) {
  297. Object *object = ObjectDB::get_instance(id);
  298. if (!object) {
  299. return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
  300. }
  301. Size2 size;
  302. if (Object::cast_to<Sprite2D>(object) || Object::cast_to<Sprite3D>(object)) {
  303. Ref<Texture2D> texture = object->call("get_texture");
  304. if (!texture.is_valid()) {
  305. return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
  306. }
  307. size = texture->get_size();
  308. if (bool(object->call("is_region"))) {
  309. size = Rect2(object->call("get_region_rect")).size;
  310. }
  311. int hframes = object->call("get_hframes");
  312. int vframes = object->call("get_vframes");
  313. if (hframes > 1) {
  314. size.x /= hframes;
  315. }
  316. if (vframes > 1) {
  317. size.y /= vframes;
  318. }
  319. } else if (Object::cast_to<AnimatedSprite2D>(object) || Object::cast_to<AnimatedSprite3D>(object)) {
  320. Ref<SpriteFrames> sf = object->call("get_sprite_frames");
  321. if (sf.is_null()) {
  322. return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
  323. }
  324. List<StringName> animations;
  325. sf->get_animation_list(&animations);
  326. int frame = get_animation()->track_get_key_value(get_track(), p_index);
  327. String animation_name;
  328. if (animations.size() == 1) {
  329. animation_name = animations.front()->get();
  330. } else {
  331. // Go through other track to find if animation is set
  332. String animation_path = get_animation()->track_get_path(get_track());
  333. animation_path = animation_path.replace(":frame", ":animation");
  334. int animation_track = get_animation()->find_track(animation_path, get_animation()->track_get_type(get_track()));
  335. float track_time = get_animation()->track_get_key_time(get_track(), p_index);
  336. int animaiton_index = get_animation()->track_find_key(animation_track, track_time);
  337. animation_name = get_animation()->track_get_key_value(animation_track, animaiton_index);
  338. }
  339. Ref<Texture2D> texture = sf->get_frame(animation_name, frame);
  340. if (!texture.is_valid()) {
  341. return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
  342. }
  343. size = texture->get_size();
  344. }
  345. size = size.floor();
  346. Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Label"));
  347. int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Label"));
  348. int height = int(font->get_height(font_size) * 2);
  349. int width = height * size.width / size.height;
  350. return Rect2(0, 0, width, get_size().height);
  351. }
  352. bool AnimationTrackEditSpriteFrame::is_key_selectable_by_distance() const {
  353. return false;
  354. }
  355. 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) {
  356. Object *object = ObjectDB::get_instance(id);
  357. if (!object) {
  358. AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right);
  359. return;
  360. }
  361. Ref<Texture2D> texture;
  362. Rect2 region;
  363. if (Object::cast_to<Sprite2D>(object) || Object::cast_to<Sprite3D>(object)) {
  364. texture = object->call("get_texture");
  365. if (!texture.is_valid()) {
  366. AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right);
  367. return;
  368. }
  369. int hframes = object->call("get_hframes");
  370. int vframes = object->call("get_vframes");
  371. Vector2 coords;
  372. if (is_coords) {
  373. coords = get_animation()->track_get_key_value(get_track(), p_index);
  374. } else {
  375. int frame = get_animation()->track_get_key_value(get_track(), p_index);
  376. coords.x = frame % hframes;
  377. coords.y = frame / hframes;
  378. }
  379. region.size = texture->get_size();
  380. if (bool(object->call("is_region"))) {
  381. region = Rect2(object->call("get_region_rect"));
  382. }
  383. if (hframes > 1) {
  384. region.size.x /= hframes;
  385. }
  386. if (vframes > 1) {
  387. region.size.y /= vframes;
  388. }
  389. region.position.x += region.size.x * coords.x;
  390. region.position.y += region.size.y * coords.y;
  391. } else if (Object::cast_to<AnimatedSprite2D>(object) || Object::cast_to<AnimatedSprite3D>(object)) {
  392. Ref<SpriteFrames> sf = object->call("get_sprite_frames");
  393. if (sf.is_null()) {
  394. AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right);
  395. return;
  396. }
  397. List<StringName> animations;
  398. sf->get_animation_list(&animations);
  399. int frame = get_animation()->track_get_key_value(get_track(), p_index);
  400. String animation_name;
  401. if (animations.size() == 1) {
  402. animation_name = animations.front()->get();
  403. } else {
  404. // Go through other track to find if animation is set
  405. String animation_path = get_animation()->track_get_path(get_track());
  406. animation_path = animation_path.replace(":frame", ":animation");
  407. int animation_track = get_animation()->find_track(animation_path, get_animation()->track_get_type(get_track()));
  408. float track_time = get_animation()->track_get_key_time(get_track(), p_index);
  409. int animaiton_index = get_animation()->track_find_key(animation_track, track_time);
  410. animation_name = get_animation()->track_get_key_value(animation_track, animaiton_index);
  411. }
  412. texture = sf->get_frame(animation_name, frame);
  413. if (!texture.is_valid()) {
  414. AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right);
  415. return;
  416. }
  417. region.size = texture->get_size();
  418. }
  419. Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Label"));
  420. int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Label"));
  421. int height = int(font->get_height(font_size) * 2);
  422. int width = height * region.size.width / region.size.height;
  423. Rect2 rect(p_x, int(get_size().height - height) / 2, width, height);
  424. if (rect.position.x + rect.size.x < p_clip_left) {
  425. return;
  426. }
  427. if (rect.position.x > p_clip_right) {
  428. return;
  429. }
  430. Color accent = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  431. Color bg = accent;
  432. bg.a = 0.15;
  433. draw_rect_clipped(rect, bg);
  434. draw_texture_region_clipped(texture, rect, region);
  435. if (p_selected) {
  436. draw_rect_clipped(rect, accent, false);
  437. }
  438. }
  439. void AnimationTrackEditSpriteFrame::set_node(Object *p_object) {
  440. id = p_object->get_instance_id();
  441. }
  442. void AnimationTrackEditSpriteFrame::set_as_coords() {
  443. is_coords = true;
  444. }
  445. /// SUB ANIMATION ///
  446. int AnimationTrackEditSubAnim::get_key_height() const {
  447. if (!ObjectDB::get_instance(id)) {
  448. return AnimationTrackEdit::get_key_height();
  449. }
  450. Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Label"));
  451. int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Label"));
  452. return int(font->get_height(font_size) * 1.5);
  453. }
  454. Rect2 AnimationTrackEditSubAnim::get_key_rect(int p_index, float p_pixels_sec) {
  455. Object *object = ObjectDB::get_instance(id);
  456. if (!object) {
  457. return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
  458. }
  459. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(object);
  460. if (!ap) {
  461. return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
  462. }
  463. String anim = get_animation()->track_get_key_value(get_track(), p_index);
  464. if (anim != "[stop]" && ap->has_animation(anim)) {
  465. float len = ap->get_animation(anim)->get_length();
  466. if (get_animation()->track_get_key_count(get_track()) > p_index + 1) {
  467. len = MIN(len, get_animation()->track_get_key_time(get_track(), p_index + 1) - get_animation()->track_get_key_time(get_track(), p_index));
  468. }
  469. return Rect2(0, 0, len * p_pixels_sec, get_size().height);
  470. } else {
  471. Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Label"));
  472. int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Label"));
  473. int fh = font->get_height(font_size) * 0.8;
  474. return Rect2(0, 0, fh, get_size().height);
  475. }
  476. }
  477. bool AnimationTrackEditSubAnim::is_key_selectable_by_distance() const {
  478. return false;
  479. }
  480. 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) {
  481. Object *object = ObjectDB::get_instance(id);
  482. if (!object) {
  483. AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right);
  484. return;
  485. }
  486. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(object);
  487. if (!ap) {
  488. AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right);
  489. return;
  490. }
  491. String anim = get_animation()->track_get_key_value(get_track(), p_index);
  492. if (anim != "[stop]" && ap->has_animation(anim)) {
  493. float len = ap->get_animation(anim)->get_length();
  494. if (get_animation()->track_get_key_count(get_track()) > p_index + 1) {
  495. len = MIN(len, get_animation()->track_get_key_time(get_track(), p_index + 1) - get_animation()->track_get_key_time(get_track(), p_index));
  496. }
  497. int pixel_len = len * p_pixels_sec;
  498. int pixel_begin = p_x;
  499. int pixel_end = p_x + pixel_len;
  500. if (pixel_end < p_clip_left) {
  501. return;
  502. }
  503. if (pixel_begin > p_clip_right) {
  504. return;
  505. }
  506. int from_x = MAX(pixel_begin, p_clip_left);
  507. int to_x = MIN(pixel_end, p_clip_right);
  508. if (to_x <= from_x) {
  509. return;
  510. }
  511. Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Label"));
  512. int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Label"));
  513. int fh = font->get_height(font_size) * 1.5;
  514. Rect2 rect(from_x, int(get_size().height - fh) / 2, to_x - from_x, fh);
  515. Color color = get_theme_color(SNAME("font_color"), SNAME("Label"));
  516. Color bg = color;
  517. bg.r = 1 - color.r;
  518. bg.g = 1 - color.g;
  519. bg.b = 1 - color.b;
  520. draw_rect(rect, bg);
  521. Vector<Vector2> lines;
  522. Vector<Color> colorv;
  523. {
  524. Ref<Animation> ap_anim = ap->get_animation(anim);
  525. for (int i = 0; i < ap_anim->get_track_count(); i++) {
  526. float h = (rect.size.height - 2) / ap_anim->get_track_count();
  527. int y = 2 + h * i + h / 2;
  528. for (int j = 0; j < ap_anim->track_get_key_count(i); j++) {
  529. float ofs = ap_anim->track_get_key_time(i, j);
  530. int x = p_x + ofs * p_pixels_sec + 2;
  531. if (x < from_x || x >= (to_x - 4)) {
  532. continue;
  533. }
  534. lines.push_back(Point2(x, y));
  535. lines.push_back(Point2(x + 1, y));
  536. }
  537. }
  538. colorv.push_back(color);
  539. }
  540. if (lines.size() > 2) {
  541. RS::get_singleton()->canvas_item_add_multiline(get_canvas_item(), lines, colorv);
  542. }
  543. int limit = to_x - from_x - 4;
  544. if (limit > 0) {
  545. 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);
  546. }
  547. if (p_selected) {
  548. Color accent = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  549. draw_rect(rect, accent, false);
  550. }
  551. } else {
  552. Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Label"));
  553. int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Label"));
  554. int fh = font->get_height(font_size) * 0.8;
  555. Rect2 rect(Vector2(p_x, int(get_size().height - fh) / 2), Size2(fh, fh));
  556. Color color = get_theme_color(SNAME("font_color"), SNAME("Label"));
  557. draw_rect_clipped(rect, color);
  558. if (p_selected) {
  559. Color accent = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  560. draw_rect_clipped(rect, accent, false);
  561. }
  562. }
  563. }
  564. void AnimationTrackEditSubAnim::set_node(Object *p_object) {
  565. id = p_object->get_instance_id();
  566. }
  567. //// VOLUME DB ////
  568. int AnimationTrackEditVolumeDB::get_key_height() const {
  569. Ref<Texture2D> volume_texture = get_theme_icon(SNAME("ColorTrackVu"), SNAME("EditorIcons"));
  570. return volume_texture->get_height() * 1.2;
  571. }
  572. void AnimationTrackEditVolumeDB::draw_bg(int p_clip_left, int p_clip_right) {
  573. Ref<Texture2D> volume_texture = get_theme_icon(SNAME("ColorTrackVu"), SNAME("EditorIcons"));
  574. int tex_h = volume_texture->get_height();
  575. int y_from = (get_size().height - tex_h) / 2;
  576. int y_size = tex_h;
  577. Color color(1, 1, 1, 0.3);
  578. draw_texture_rect(volume_texture, Rect2(p_clip_left, y_from, p_clip_right - p_clip_left, y_from + y_size), false, color);
  579. }
  580. void AnimationTrackEditVolumeDB::draw_fg(int p_clip_left, int p_clip_right) {
  581. Ref<Texture2D> volume_texture = get_theme_icon(SNAME("ColorTrackVu"), SNAME("EditorIcons"));
  582. int tex_h = volume_texture->get_height();
  583. int y_from = (get_size().height - tex_h) / 2;
  584. int db0 = y_from + (24 / 80.0) * tex_h;
  585. draw_line(Vector2(p_clip_left, db0), Vector2(p_clip_right, db0), Color(1, 1, 1, 0.3));
  586. }
  587. 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) {
  588. if (p_x > p_clip_right || p_next_x < p_clip_left) {
  589. return;
  590. }
  591. float db = get_animation()->track_get_key_value(get_track(), p_index);
  592. float db_n = get_animation()->track_get_key_value(get_track(), p_index + 1);
  593. db = CLAMP(db, -60, 24);
  594. db_n = CLAMP(db_n, -60, 24);
  595. float h = 1.0 - ((db + 60) / 84.0);
  596. float h_n = 1.0 - ((db_n + 60) / 84.0);
  597. int from_x = p_x;
  598. int to_x = p_next_x;
  599. if (from_x < p_clip_left) {
  600. h = Math::lerp(h, h_n, float(p_clip_left - from_x) / float(to_x - from_x));
  601. from_x = p_clip_left;
  602. }
  603. if (to_x > p_clip_right) {
  604. h_n = Math::lerp(h, h_n, float(p_clip_right - from_x) / float(to_x - from_x));
  605. to_x = p_clip_right;
  606. }
  607. Ref<Texture2D> volume_texture = get_theme_icon(SNAME("ColorTrackVu"), SNAME("EditorIcons"));
  608. int tex_h = volume_texture->get_height();
  609. int y_from = (get_size().height - tex_h) / 2;
  610. Color color = get_theme_color(SNAME("font_color"), SNAME("Label"));
  611. color.a *= 0.7;
  612. draw_line(Point2(from_x, y_from + h * tex_h), Point2(to_x, y_from + h_n * tex_h), color, 2);
  613. }
  614. ////////////////////////
  615. /// AUDIO ///
  616. void AnimationTrackEditTypeAudio::_preview_changed(ObjectID p_which) {
  617. for (int i = 0; i < get_animation()->track_get_key_count(get_track()); i++) {
  618. Ref<AudioStream> stream = get_animation()->audio_track_get_key_stream(get_track(), i);
  619. if (stream.is_valid() && stream->get_instance_id() == p_which) {
  620. queue_redraw();
  621. return;
  622. }
  623. }
  624. }
  625. int AnimationTrackEditTypeAudio::get_key_height() const {
  626. Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Label"));
  627. int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Label"));
  628. return int(font->get_height(font_size) * 1.5);
  629. }
  630. Rect2 AnimationTrackEditTypeAudio::get_key_rect(int p_index, float p_pixels_sec) {
  631. Ref<AudioStream> stream = get_animation()->audio_track_get_key_stream(get_track(), p_index);
  632. if (!stream.is_valid()) {
  633. return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
  634. }
  635. float start_ofs = get_animation()->audio_track_get_key_start_offset(get_track(), p_index);
  636. float end_ofs = get_animation()->audio_track_get_key_end_offset(get_track(), p_index);
  637. float len = stream->get_length();
  638. if (len == 0) {
  639. Ref<AudioStreamPreview> preview = AudioStreamPreviewGenerator::get_singleton()->generate_preview(stream);
  640. len = preview->get_length();
  641. }
  642. len -= end_ofs;
  643. len -= start_ofs;
  644. if (len <= 0.001) {
  645. len = 0.001;
  646. }
  647. if (get_animation()->track_get_key_count(get_track()) > p_index + 1) {
  648. len = MIN(len, get_animation()->track_get_key_time(get_track(), p_index + 1) - get_animation()->track_get_key_time(get_track(), p_index));
  649. }
  650. return Rect2(0, 0, len * p_pixels_sec, get_size().height);
  651. }
  652. bool AnimationTrackEditTypeAudio::is_key_selectable_by_distance() const {
  653. return false;
  654. }
  655. 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) {
  656. Ref<AudioStream> stream = get_animation()->audio_track_get_key_stream(get_track(), p_index);
  657. if (!stream.is_valid()) {
  658. AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right);
  659. return;
  660. }
  661. float start_ofs = get_animation()->audio_track_get_key_start_offset(get_track(), p_index);
  662. float end_ofs = get_animation()->audio_track_get_key_end_offset(get_track(), p_index);
  663. if (len_resizing && p_index == len_resizing_index) {
  664. float ofs_local = -len_resizing_rel / get_timeline()->get_zoom_scale();
  665. if (len_resizing_start) {
  666. start_ofs += ofs_local;
  667. if (start_ofs < 0) {
  668. start_ofs = 0;
  669. }
  670. } else {
  671. end_ofs += ofs_local;
  672. if (end_ofs < 0) {
  673. end_ofs = 0;
  674. }
  675. }
  676. }
  677. Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Label"));
  678. int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Label"));
  679. float fh = int(font->get_height(font_size) * 1.5);
  680. float len = stream->get_length();
  681. Ref<AudioStreamPreview> preview = AudioStreamPreviewGenerator::get_singleton()->generate_preview(stream);
  682. float preview_len = preview->get_length();
  683. if (len == 0) {
  684. len = preview_len;
  685. }
  686. int pixel_total_len = len * p_pixels_sec;
  687. len -= end_ofs;
  688. len -= start_ofs;
  689. if (len <= 0.001) {
  690. len = 0.001;
  691. }
  692. int pixel_len = len * p_pixels_sec;
  693. int pixel_begin = p_x;
  694. int pixel_end = p_x + pixel_len;
  695. if (pixel_end < p_clip_left) {
  696. return;
  697. }
  698. if (pixel_begin > p_clip_right) {
  699. return;
  700. }
  701. int from_x = MAX(pixel_begin, p_clip_left);
  702. int to_x = MIN(pixel_end, p_clip_right);
  703. if (get_animation()->track_get_key_count(get_track()) > p_index + 1) {
  704. 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));
  705. int limit_x = pixel_begin + limit * p_pixels_sec;
  706. to_x = MIN(limit_x, to_x);
  707. }
  708. if (to_x <= from_x) {
  709. to_x = from_x + 1;
  710. }
  711. int h = get_size().height;
  712. Rect2 rect = Rect2(from_x, (h - fh) / 2, to_x - from_x, fh);
  713. draw_rect(rect, Color(0.25, 0.25, 0.25));
  714. Vector<Vector2> lines;
  715. lines.resize((to_x - from_x + 1) * 2);
  716. preview_len = preview->get_length();
  717. for (int i = from_x; i < to_x; i++) {
  718. float ofs = (i - pixel_begin) * preview_len / pixel_total_len;
  719. float ofs_n = ((i + 1) - pixel_begin) * preview_len / pixel_total_len;
  720. ofs += start_ofs;
  721. ofs_n += start_ofs;
  722. float max = preview->get_max(ofs, ofs_n) * 0.5 + 0.5;
  723. float min = preview->get_min(ofs, ofs_n) * 0.5 + 0.5;
  724. int idx = i - from_x;
  725. lines.write[idx * 2 + 0] = Vector2(i, rect.position.y + min * rect.size.y);
  726. lines.write[idx * 2 + 1] = Vector2(i, rect.position.y + max * rect.size.y);
  727. }
  728. Vector<Color> color;
  729. color.push_back(Color(0.75, 0.75, 0.75));
  730. RS::get_singleton()->canvas_item_add_multiline(get_canvas_item(), lines, color);
  731. Color cut_color = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  732. cut_color.a = 0.7;
  733. if (start_ofs > 0 && pixel_begin > p_clip_left) {
  734. draw_rect(Rect2(pixel_begin, rect.position.y, 1, rect.size.y), cut_color);
  735. }
  736. if (end_ofs > 0 && pixel_end < p_clip_right) {
  737. draw_rect(Rect2(pixel_end, rect.position.y, 1, rect.size.y), cut_color);
  738. }
  739. if (p_selected) {
  740. Color accent = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  741. draw_rect(rect, accent, false);
  742. }
  743. }
  744. void AnimationTrackEditTypeAudio::_bind_methods() {
  745. }
  746. AnimationTrackEditTypeAudio::AnimationTrackEditTypeAudio() {
  747. AudioStreamPreviewGenerator::get_singleton()->connect("preview_updated", callable_mp(this, &AnimationTrackEditTypeAudio::_preview_changed));
  748. }
  749. bool AnimationTrackEditTypeAudio::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  750. if (p_point.x > get_timeline()->get_name_limit() && p_point.x < get_size().width - get_timeline()->get_buttons_width()) {
  751. Dictionary drag_data = p_data;
  752. if (drag_data.has("type") && String(drag_data["type"]) == "resource") {
  753. Ref<AudioStream> res = drag_data["resource"];
  754. if (res.is_valid()) {
  755. return true;
  756. }
  757. }
  758. if (drag_data.has("type") && String(drag_data["type"]) == "files") {
  759. Vector<String> files = drag_data["files"];
  760. if (files.size() == 1) {
  761. String file = files[0];
  762. Ref<AudioStream> res = ResourceLoader::load(file);
  763. if (res.is_valid()) {
  764. return true;
  765. }
  766. }
  767. }
  768. }
  769. return AnimationTrackEdit::can_drop_data(p_point, p_data);
  770. }
  771. void AnimationTrackEditTypeAudio::drop_data(const Point2 &p_point, const Variant &p_data) {
  772. if (p_point.x > get_timeline()->get_name_limit() && p_point.x < get_size().width - get_timeline()->get_buttons_width()) {
  773. Ref<AudioStream> stream;
  774. Dictionary drag_data = p_data;
  775. if (drag_data.has("type") && String(drag_data["type"]) == "resource") {
  776. stream = drag_data["resource"];
  777. } else if (drag_data.has("type") && String(drag_data["type"]) == "files") {
  778. Vector<String> files = drag_data["files"];
  779. if (files.size() == 1) {
  780. String file = files[0];
  781. stream = ResourceLoader::load(file);
  782. }
  783. }
  784. if (stream.is_valid()) {
  785. int x = p_point.x - get_timeline()->get_name_limit();
  786. float ofs = x / get_timeline()->get_zoom_scale();
  787. ofs += get_timeline()->get_value();
  788. ofs = get_editor()->snap_time(ofs);
  789. while (get_animation()->track_find_key(get_track(), ofs, true) != -1) { //make sure insertion point is valid
  790. ofs += 0.001;
  791. }
  792. get_undo_redo()->create_action(TTR("Add Audio Track Clip"));
  793. get_undo_redo()->add_do_method(get_animation().ptr(), "audio_track_insert_key", get_track(), ofs, stream);
  794. get_undo_redo()->add_undo_method(get_animation().ptr(), "track_remove_key_at_time", get_track(), ofs);
  795. get_undo_redo()->commit_action();
  796. queue_redraw();
  797. return;
  798. }
  799. }
  800. AnimationTrackEdit::drop_data(p_point, p_data);
  801. }
  802. void AnimationTrackEditTypeAudio::gui_input(const Ref<InputEvent> &p_event) {
  803. ERR_FAIL_COND(p_event.is_null());
  804. Ref<InputEventMouseMotion> mm = p_event;
  805. if (!len_resizing && mm.is_valid()) {
  806. bool use_hsize_cursor = false;
  807. for (int i = 0; i < get_animation()->track_get_key_count(get_track()); i++) {
  808. Ref<AudioStream> stream = get_animation()->audio_track_get_key_stream(get_track(), i);
  809. if (!stream.is_valid()) {
  810. continue;
  811. }
  812. float start_ofs = get_animation()->audio_track_get_key_start_offset(get_track(), i);
  813. float end_ofs = get_animation()->audio_track_get_key_end_offset(get_track(), i);
  814. float len = stream->get_length();
  815. if (len == 0) {
  816. Ref<AudioStreamPreview> preview = AudioStreamPreviewGenerator::get_singleton()->generate_preview(stream);
  817. float preview_len = preview->get_length();
  818. len = preview_len;
  819. }
  820. len -= end_ofs;
  821. len -= start_ofs;
  822. if (len <= 0.001) {
  823. len = 0.001;
  824. }
  825. if (get_animation()->track_get_key_count(get_track()) > i + 1) {
  826. len = MIN(len, get_animation()->track_get_key_time(get_track(), i + 1) - get_animation()->track_get_key_time(get_track(), i));
  827. }
  828. float ofs = get_animation()->track_get_key_time(get_track(), i);
  829. ofs -= get_timeline()->get_value();
  830. ofs *= get_timeline()->get_zoom_scale();
  831. ofs += get_timeline()->get_name_limit();
  832. int end = ofs + len * get_timeline()->get_zoom_scale();
  833. if (end >= get_timeline()->get_name_limit() && end <= get_size().width - get_timeline()->get_buttons_width() && ABS(mm->get_position().x - end) < 5 * EDSCALE) {
  834. use_hsize_cursor = true;
  835. len_resizing_index = i;
  836. }
  837. }
  838. over_drag_position = use_hsize_cursor;
  839. }
  840. if (len_resizing && mm.is_valid()) {
  841. len_resizing_rel += mm->get_relative().x;
  842. len_resizing_start = mm->is_shift_pressed();
  843. queue_redraw();
  844. accept_event();
  845. return;
  846. }
  847. Ref<InputEventMouseButton> mb = p_event;
  848. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT && over_drag_position) {
  849. len_resizing = true;
  850. len_resizing_start = mb->is_shift_pressed();
  851. len_resizing_from_px = mb->get_position().x;
  852. len_resizing_rel = 0;
  853. queue_redraw();
  854. accept_event();
  855. return;
  856. }
  857. if (len_resizing && mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  858. float ofs_local = -len_resizing_rel / get_timeline()->get_zoom_scale();
  859. if (len_resizing_start) {
  860. float prev_ofs = get_animation()->audio_track_get_key_start_offset(get_track(), len_resizing_index);
  861. get_undo_redo()->create_action(TTR("Change Audio Track Clip Start Offset"));
  862. get_undo_redo()->add_do_method(get_animation().ptr(), "audio_track_set_key_start_offset", get_track(), len_resizing_index, prev_ofs + ofs_local);
  863. get_undo_redo()->add_undo_method(get_animation().ptr(), "audio_track_set_key_start_offset", get_track(), len_resizing_index, prev_ofs);
  864. get_undo_redo()->commit_action();
  865. } else {
  866. float prev_ofs = get_animation()->audio_track_get_key_end_offset(get_track(), len_resizing_index);
  867. get_undo_redo()->create_action(TTR("Change Audio Track Clip End Offset"));
  868. get_undo_redo()->add_do_method(get_animation().ptr(), "audio_track_set_key_end_offset", get_track(), len_resizing_index, prev_ofs + ofs_local);
  869. get_undo_redo()->add_undo_method(get_animation().ptr(), "audio_track_set_key_end_offset", get_track(), len_resizing_index, prev_ofs);
  870. get_undo_redo()->commit_action();
  871. }
  872. len_resizing_index = -1;
  873. queue_redraw();
  874. accept_event();
  875. return;
  876. }
  877. AnimationTrackEdit::gui_input(p_event);
  878. }
  879. Control::CursorShape AnimationTrackEditTypeAudio::get_cursor_shape(const Point2 &p_pos) const {
  880. if (over_drag_position || len_resizing) {
  881. return Control::CURSOR_HSIZE;
  882. } else {
  883. return get_default_cursor_shape();
  884. }
  885. }
  886. ////////////////////
  887. /// SUB ANIMATION ///
  888. int AnimationTrackEditTypeAnimation::get_key_height() const {
  889. if (!ObjectDB::get_instance(id)) {
  890. return AnimationTrackEdit::get_key_height();
  891. }
  892. Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Label"));
  893. int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Label"));
  894. return int(font->get_height(font_size) * 1.5);
  895. }
  896. Rect2 AnimationTrackEditTypeAnimation::get_key_rect(int p_index, float p_pixels_sec) {
  897. Object *object = ObjectDB::get_instance(id);
  898. if (!object) {
  899. return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
  900. }
  901. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(object);
  902. if (!ap) {
  903. return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
  904. }
  905. String anim = get_animation()->animation_track_get_key_animation(get_track(), p_index);
  906. if (anim != "[stop]" && ap->has_animation(anim)) {
  907. float len = ap->get_animation(anim)->get_length();
  908. if (get_animation()->track_get_key_count(get_track()) > p_index + 1) {
  909. len = MIN(len, get_animation()->track_get_key_time(get_track(), p_index + 1) - get_animation()->track_get_key_time(get_track(), p_index));
  910. }
  911. return Rect2(0, 0, len * p_pixels_sec, get_size().height);
  912. } else {
  913. Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Label"));
  914. int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Label"));
  915. int fh = font->get_height(font_size) * 0.8;
  916. return Rect2(0, 0, fh, get_size().height);
  917. }
  918. }
  919. bool AnimationTrackEditTypeAnimation::is_key_selectable_by_distance() const {
  920. return false;
  921. }
  922. 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) {
  923. Object *object = ObjectDB::get_instance(id);
  924. if (!object) {
  925. AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right);
  926. return;
  927. }
  928. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(object);
  929. if (!ap) {
  930. AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right);
  931. return;
  932. }
  933. String anim = get_animation()->animation_track_get_key_animation(get_track(), p_index);
  934. if (anim != "[stop]" && ap->has_animation(anim)) {
  935. float len = ap->get_animation(anim)->get_length();
  936. if (get_animation()->track_get_key_count(get_track()) > p_index + 1) {
  937. len = MIN(len, get_animation()->track_get_key_time(get_track(), p_index + 1) - get_animation()->track_get_key_time(get_track(), p_index));
  938. }
  939. int pixel_len = len * p_pixels_sec;
  940. int pixel_begin = p_x;
  941. int pixel_end = p_x + pixel_len;
  942. if (pixel_end < p_clip_left) {
  943. return;
  944. }
  945. if (pixel_begin > p_clip_right) {
  946. return;
  947. }
  948. int from_x = MAX(pixel_begin, p_clip_left);
  949. int to_x = MIN(pixel_end, p_clip_right);
  950. if (to_x <= from_x) {
  951. return;
  952. }
  953. Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Label"));
  954. int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Label"));
  955. int fh = font->get_height(font_size) * 1.5;
  956. Rect2 rect(from_x, int(get_size().height - fh) / 2, to_x - from_x, fh);
  957. Color color = get_theme_color(SNAME("font_color"), SNAME("Label"));
  958. Color bg = color;
  959. bg.r = 1 - color.r;
  960. bg.g = 1 - color.g;
  961. bg.b = 1 - color.b;
  962. draw_rect(rect, bg);
  963. Vector<Vector2> lines;
  964. Vector<Color> colorv;
  965. {
  966. Ref<Animation> ap_anim = ap->get_animation(anim);
  967. for (int i = 0; i < ap_anim->get_track_count(); i++) {
  968. float h = (rect.size.height - 2) / ap_anim->get_track_count();
  969. int y = 2 + h * i + h / 2;
  970. for (int j = 0; j < ap_anim->track_get_key_count(i); j++) {
  971. float ofs = ap_anim->track_get_key_time(i, j);
  972. int x = p_x + ofs * p_pixels_sec + 2;
  973. if (x < from_x || x >= (to_x - 4)) {
  974. continue;
  975. }
  976. lines.push_back(Point2(x, y));
  977. lines.push_back(Point2(x + 1, y));
  978. }
  979. }
  980. colorv.push_back(color);
  981. }
  982. if (lines.size() > 2) {
  983. RS::get_singleton()->canvas_item_add_multiline(get_canvas_item(), lines, colorv);
  984. }
  985. int limit = to_x - from_x - 4;
  986. if (limit > 0) {
  987. 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);
  988. }
  989. if (p_selected) {
  990. Color accent = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  991. draw_rect(rect, accent, false);
  992. }
  993. } else {
  994. Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Label"));
  995. int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Label"));
  996. int fh = font->get_height(font_size) * 0.8;
  997. Rect2 rect(Vector2(p_x, int(get_size().height - fh) / 2), Size2(fh, fh));
  998. Color color = get_theme_color(SNAME("font_color"), SNAME("Label"));
  999. draw_rect_clipped(rect, color);
  1000. if (p_selected) {
  1001. Color accent = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  1002. draw_rect_clipped(rect, accent, false);
  1003. }
  1004. }
  1005. }
  1006. void AnimationTrackEditTypeAnimation::set_node(Object *p_object) {
  1007. id = p_object->get_instance_id();
  1008. }
  1009. AnimationTrackEditTypeAnimation::AnimationTrackEditTypeAnimation() {
  1010. }
  1011. /////////
  1012. 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) {
  1013. if (p_property == "playing" && (p_object->is_class("AudioStreamPlayer") || p_object->is_class("AudioStreamPlayer2D") || p_object->is_class("AudioStreamPlayer3D"))) {
  1014. AnimationTrackEditAudio *audio = memnew(AnimationTrackEditAudio);
  1015. audio->set_node(p_object);
  1016. return audio;
  1017. }
  1018. if (p_property == "frame" && (p_object->is_class("Sprite2D") || p_object->is_class("Sprite3D") || p_object->is_class("AnimatedSprite2D") || p_object->is_class("AnimatedSprite3D"))) {
  1019. AnimationTrackEditSpriteFrame *sprite = memnew(AnimationTrackEditSpriteFrame);
  1020. sprite->set_node(p_object);
  1021. return sprite;
  1022. }
  1023. if (p_property == "frame_coords" && (p_object->is_class("Sprite2D") || p_object->is_class("Sprite3D"))) {
  1024. AnimationTrackEditSpriteFrame *sprite = memnew(AnimationTrackEditSpriteFrame);
  1025. sprite->set_as_coords();
  1026. sprite->set_node(p_object);
  1027. return sprite;
  1028. }
  1029. if (p_property == "current_animation" && (p_object->is_class("AnimationPlayer"))) {
  1030. AnimationTrackEditSubAnim *player = memnew(AnimationTrackEditSubAnim);
  1031. player->set_node(p_object);
  1032. return player;
  1033. }
  1034. if (p_property == "volume_db") {
  1035. AnimationTrackEditVolumeDB *vu = memnew(AnimationTrackEditVolumeDB);
  1036. return vu;
  1037. }
  1038. if (p_type == Variant::BOOL) {
  1039. return memnew(AnimationTrackEditBool);
  1040. }
  1041. if (p_type == Variant::COLOR) {
  1042. return memnew(AnimationTrackEditColor);
  1043. }
  1044. return nullptr;
  1045. }
  1046. AnimationTrackEdit *AnimationTrackEditDefaultPlugin::create_audio_track_edit() {
  1047. return memnew(AnimationTrackEditTypeAudio);
  1048. }
  1049. AnimationTrackEdit *AnimationTrackEditDefaultPlugin::create_animation_track_edit(Object *p_object) {
  1050. AnimationTrackEditTypeAnimation *an = memnew(AnimationTrackEditTypeAnimation);
  1051. an->set_node(p_object);
  1052. return an;
  1053. }