animation_track_editor_plugins.cpp 40 KB

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