animation_track_editor_plugins.cpp 42 KB

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