animation_track_editor_plugins.cpp 40 KB

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