animation_track_editor_plugins.cpp 47 KB

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