animation_track_editor_plugins.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  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. }