animation_track_editor_plugins.cpp 47 KB

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