animation_track_editor_plugins.cpp 40 KB

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