animation_track_editor_plugins.cpp 44 KB

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