animation_track_editor_plugins.cpp 42 KB

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