animation_tree_editor_plugin.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  1. /*************************************************************************/
  2. /* animation_tree_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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_tree_editor_plugin.h"
  31. #include "core/io/resource_loader.h"
  32. #include "core/project_settings.h"
  33. #include "os/input.h"
  34. #include "os/keyboard.h"
  35. #include "scene/gui/menu_button.h"
  36. #include "scene/gui/panel.h"
  37. #include "scene/main/viewport.h"
  38. void AnimationTreeEditor::edit(AnimationTreePlayer *p_anim_tree) {
  39. anim_tree = p_anim_tree;
  40. if (!anim_tree) {
  41. hide();
  42. } else {
  43. order.clear();
  44. p_anim_tree->get_node_list(&order);
  45. /*
  46. for(List<StringName>::Element* E=order.front();E;E=E->next()) {
  47. if (E->get() >= (int)last_id)
  48. last_id=E->get()+1;
  49. }*/
  50. play_button->set_pressed(p_anim_tree->is_active());
  51. //read the orders
  52. }
  53. }
  54. Size2 AnimationTreeEditor::_get_maximum_size() {
  55. Size2 max;
  56. for (List<StringName>::Element *E = order.front(); E; E = E->next()) {
  57. Point2 pos = anim_tree->node_get_position(E->get());
  58. if (click_type == CLICK_NODE && click_node == E->get()) {
  59. pos += click_motion - click_pos;
  60. }
  61. pos += get_node_size(E->get());
  62. if (pos.x > max.x)
  63. max.x = pos.x;
  64. if (pos.y > max.y)
  65. max.y = pos.y;
  66. }
  67. return max;
  68. }
  69. const char *AnimationTreeEditor::_node_type_names[] = { "Output", "Animation", "OneShot", "Mix", "Blend2", "Blend3", "Blend4", "TimeScale", "TimeSeek", "Transition" };
  70. Size2 AnimationTreeEditor::get_node_size(const StringName &p_node) const {
  71. AnimationTreePlayer::NodeType type = anim_tree->node_get_type(p_node);
  72. Ref<StyleBox> style = get_stylebox("panel", "PopupMenu");
  73. Ref<Font> font = get_font("font", "PopupMenu");
  74. Size2 size = style->get_minimum_size();
  75. int count = 2; // title and name
  76. int inputs = anim_tree->node_get_input_count(p_node);
  77. count += inputs ? inputs : 1;
  78. String name = p_node;
  79. float name_w = font->get_string_size(name).width;
  80. float type_w = font->get_string_size(String(_node_type_names[type])).width;
  81. float max_w = MAX(name_w, type_w);
  82. switch (type) {
  83. case AnimationTreePlayer::NODE_TIMESEEK:
  84. case AnimationTreePlayer::NODE_OUTPUT: {
  85. } break;
  86. case AnimationTreePlayer::NODE_ANIMATION:
  87. case AnimationTreePlayer::NODE_ONESHOT:
  88. case AnimationTreePlayer::NODE_MIX:
  89. case AnimationTreePlayer::NODE_BLEND2:
  90. case AnimationTreePlayer::NODE_BLEND3:
  91. case AnimationTreePlayer::NODE_BLEND4:
  92. case AnimationTreePlayer::NODE_TIMESCALE:
  93. case AnimationTreePlayer::NODE_TRANSITION: {
  94. size.height += font->get_height();
  95. } break;
  96. case AnimationTreePlayer::NODE_MAX: {
  97. }
  98. }
  99. size.x += max_w + 20;
  100. size.y += count * (font->get_height() + get_constant("vseparation", "PopupMenu"));
  101. return size;
  102. }
  103. void AnimationTreeEditor::_edit_dialog_changede(String) {
  104. edit_dialog->hide();
  105. }
  106. void AnimationTreeEditor::_edit_dialog_changeds(String s) {
  107. _edit_dialog_changed();
  108. }
  109. void AnimationTreeEditor::_edit_dialog_changedf(float) {
  110. _edit_dialog_changed();
  111. }
  112. void AnimationTreeEditor::_edit_dialog_changed() {
  113. if (updating_edit)
  114. return;
  115. if (renaming_edit) {
  116. if (anim_tree->node_rename(edited_node, edit_line[0]->get_text()) == OK) {
  117. for (List<StringName>::Element *E = order.front(); E; E = E->next()) {
  118. if (E->get() == edited_node)
  119. E->get() = edit_line[0]->get_text();
  120. }
  121. edited_node = edit_line[0]->get_text();
  122. }
  123. update();
  124. return;
  125. }
  126. AnimationTreePlayer::NodeType type = anim_tree->node_get_type(edited_node);
  127. switch (type) {
  128. case AnimationTreePlayer::NODE_TIMESCALE:
  129. anim_tree->timescale_node_set_scale(edited_node, edit_line[0]->get_text().to_double());
  130. break;
  131. case AnimationTreePlayer::NODE_ONESHOT:
  132. anim_tree->oneshot_node_set_fadein_time(edited_node, edit_line[0]->get_text().to_double());
  133. anim_tree->oneshot_node_set_fadeout_time(edited_node, edit_line[1]->get_text().to_double());
  134. anim_tree->oneshot_node_set_autorestart_delay(edited_node, edit_line[2]->get_text().to_double());
  135. anim_tree->oneshot_node_set_autorestart_random_delay(edited_node, edit_line[3]->get_text().to_double());
  136. anim_tree->oneshot_node_set_autorestart(edited_node, edit_check->is_pressed());
  137. anim_tree->oneshot_node_set_mix_mode(edited_node, edit_option->get_selected());
  138. break;
  139. case AnimationTreePlayer::NODE_MIX:
  140. anim_tree->mix_node_set_amount(edited_node, edit_scroll[0]->get_value());
  141. break;
  142. case AnimationTreePlayer::NODE_BLEND2:
  143. anim_tree->blend2_node_set_amount(edited_node, edit_scroll[0]->get_value());
  144. break;
  145. case AnimationTreePlayer::NODE_BLEND3:
  146. anim_tree->blend3_node_set_amount(edited_node, edit_scroll[0]->get_value());
  147. break;
  148. case AnimationTreePlayer::NODE_BLEND4:
  149. anim_tree->blend4_node_set_amount(edited_node, Point2(edit_scroll[0]->get_value(), edit_scroll[1]->get_value()));
  150. break;
  151. case AnimationTreePlayer::NODE_TRANSITION: {
  152. anim_tree->transition_node_set_xfade_time(edited_node, edit_line[0]->get_text().to_double());
  153. if (anim_tree->transition_node_get_current(edited_node) != edit_option->get_selected())
  154. anim_tree->transition_node_set_current(edited_node, edit_option->get_selected());
  155. } break;
  156. default: {}
  157. }
  158. }
  159. void AnimationTreeEditor::_edit_dialog_animation_changed() {
  160. Ref<Animation> anim = property_editor->get_variant().operator RefPtr();
  161. anim_tree->animation_node_set_animation(edited_node, anim);
  162. update();
  163. }
  164. void AnimationTreeEditor::_edit_dialog_edit_animation() {
  165. if (Engine::get_singleton()->is_editor_hint()) {
  166. get_tree()->get_root()->get_child(0)->call("_resource_selected", property_editor->get_variant().operator RefPtr());
  167. };
  168. };
  169. void AnimationTreeEditor::_edit_oneshot_start() {
  170. anim_tree->oneshot_node_start(edited_node);
  171. }
  172. void AnimationTreeEditor::_play_toggled() {
  173. anim_tree->set_active(play_button->is_pressed());
  174. }
  175. void AnimationTreeEditor::_master_anim_menu_item(int p_item) {
  176. if (p_item == 0)
  177. _edit_filters();
  178. else {
  179. String str = master_anim_popup->get_item_text(p_item);
  180. anim_tree->animation_node_set_master_animation(edited_node, str);
  181. }
  182. update();
  183. }
  184. void AnimationTreeEditor::_popup_edit_dialog() {
  185. updating_edit = true;
  186. for (int i = 0; i < 2; i++)
  187. edit_scroll[i]->hide();
  188. for (int i = 0; i < 4; i++) {
  189. edit_line[i]->hide();
  190. edit_label[i]->hide();
  191. }
  192. edit_option->hide();
  193. edit_button->hide();
  194. filter_button->hide();
  195. edit_check->hide();
  196. Point2 pos = anim_tree->node_get_position(edited_node) - Point2(h_scroll->get_value(), v_scroll->get_value());
  197. Ref<StyleBox> style = get_stylebox("panel", "PopupMenu");
  198. Size2 size = get_node_size(edited_node);
  199. Point2 popup_pos(pos.x + style->get_margin(MARGIN_LEFT), pos.y + size.y - style->get_margin(MARGIN_BOTTOM));
  200. popup_pos += get_global_position();
  201. if (renaming_edit) {
  202. edit_label[0]->set_text(TTR("New name:"));
  203. edit_label[0]->set_position(Point2(5, 5));
  204. edit_label[0]->show();
  205. edit_line[0]->set_begin(Point2(15, 25));
  206. edit_line[0]->set_text(edited_node);
  207. edit_line[0]->show();
  208. edit_dialog->set_size(Size2(150, 50));
  209. } else {
  210. AnimationTreePlayer::NodeType type = anim_tree->node_get_type(edited_node);
  211. switch (type) {
  212. case AnimationTreePlayer::NODE_ANIMATION:
  213. if (anim_tree->get_master_player() != NodePath() && anim_tree->has_node(anim_tree->get_master_player()) && Object::cast_to<AnimationPlayer>(anim_tree->get_node(anim_tree->get_master_player()))) {
  214. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(anim_tree->get_node(anim_tree->get_master_player()));
  215. master_anim_popup->clear();
  216. master_anim_popup->add_item(TTR("Edit Filters"));
  217. master_anim_popup->add_separator();
  218. List<StringName> sn;
  219. ap->get_animation_list(&sn);
  220. sn.sort_custom<StringName::AlphCompare>();
  221. for (List<StringName>::Element *E = sn.front(); E; E = E->next()) {
  222. master_anim_popup->add_item(E->get());
  223. }
  224. master_anim_popup->set_position(popup_pos);
  225. master_anim_popup->popup();
  226. } else {
  227. property_editor->edit(this, "", Variant::OBJECT, anim_tree->animation_node_get_animation(edited_node), PROPERTY_HINT_RESOURCE_TYPE, "Animation");
  228. property_editor->set_position(popup_pos);
  229. property_editor->popup();
  230. updating_edit = false;
  231. }
  232. return;
  233. case AnimationTreePlayer::NODE_TIMESCALE:
  234. edit_label[0]->set_text(TTR("Scale:"));
  235. edit_label[0]->set_position(Point2(5, 5));
  236. edit_label[0]->show();
  237. edit_line[0]->set_begin(Point2(15, 25));
  238. edit_line[0]->set_text(rtos(anim_tree->timescale_node_get_scale(edited_node)));
  239. edit_line[0]->show();
  240. edit_dialog->set_size(Size2(150, 50));
  241. break;
  242. case AnimationTreePlayer::NODE_ONESHOT:
  243. edit_label[0]->set_text(TTR("Fade In (s):"));
  244. edit_label[0]->set_position(Point2(5, 5));
  245. edit_label[0]->show();
  246. edit_line[0]->set_begin(Point2(15, 25));
  247. edit_line[0]->set_text(rtos(anim_tree->oneshot_node_get_fadein_time(edited_node)));
  248. edit_line[0]->show();
  249. edit_label[1]->set_text(TTR("Fade Out (s):"));
  250. edit_label[1]->set_position(Point2(5, 55));
  251. edit_label[1]->show();
  252. edit_line[1]->set_begin(Point2(15, 75));
  253. edit_line[1]->set_text(rtos(anim_tree->oneshot_node_get_fadeout_time(edited_node)));
  254. edit_line[1]->show();
  255. edit_option->clear();
  256. edit_option->add_item(TTR("Blend"), 0);
  257. edit_option->add_item(TTR("Mix"), 1);
  258. edit_option->set_begin(Point2(15, 105));
  259. edit_option->select(anim_tree->oneshot_node_get_mix_mode(edited_node));
  260. edit_option->show();
  261. edit_check->set_text(TTR("Auto Restart:"));
  262. edit_check->set_begin(Point2(15, 125));
  263. edit_check->set_pressed(anim_tree->oneshot_node_has_autorestart(edited_node));
  264. edit_check->show();
  265. edit_label[2]->set_text(TTR("Restart (s):"));
  266. edit_label[2]->set_position(Point2(5, 145));
  267. edit_label[2]->show();
  268. edit_line[2]->set_begin(Point2(15, 165));
  269. edit_line[2]->set_text(rtos(anim_tree->oneshot_node_get_autorestart_delay(edited_node)));
  270. edit_line[2]->show();
  271. edit_label[3]->set_text(TTR("Random Restart (s):"));
  272. edit_label[3]->set_position(Point2(5, 195));
  273. edit_label[3]->show();
  274. edit_line[3]->set_begin(Point2(15, 215));
  275. edit_line[3]->set_text(rtos(anim_tree->oneshot_node_get_autorestart_random_delay(edited_node)));
  276. edit_line[3]->show();
  277. filter_button->set_begin(Point2(10, 245));
  278. filter_button->show();
  279. edit_button->set_begin(Point2(10, 268));
  280. edit_button->set_text(TTR("Start!"));
  281. edit_button->show();
  282. edit_dialog->set_size(Size2(180, 293));
  283. break;
  284. case AnimationTreePlayer::NODE_MIX:
  285. edit_label[0]->set_text(TTR("Amount:"));
  286. edit_label[0]->set_position(Point2(5, 5));
  287. edit_label[0]->show();
  288. edit_scroll[0]->set_min(0);
  289. edit_scroll[0]->set_max(1);
  290. edit_scroll[0]->set_step(0.01);
  291. edit_scroll[0]->set_value(anim_tree->mix_node_get_amount(edited_node));
  292. edit_scroll[0]->set_begin(Point2(15, 25));
  293. edit_scroll[0]->show();
  294. edit_dialog->set_size(Size2(150, 50));
  295. break;
  296. case AnimationTreePlayer::NODE_BLEND2:
  297. edit_label[0]->set_text(TTR("Blend:"));
  298. edit_label[0]->set_position(Point2(5, 5));
  299. edit_label[0]->show();
  300. edit_scroll[0]->set_min(0);
  301. edit_scroll[0]->set_max(1);
  302. edit_scroll[0]->set_step(0.01);
  303. edit_scroll[0]->set_value(anim_tree->blend2_node_get_amount(edited_node));
  304. edit_scroll[0]->set_begin(Point2(15, 25));
  305. edit_scroll[0]->show();
  306. filter_button->set_begin(Point2(10, 47));
  307. filter_button->show();
  308. edit_dialog->set_size(Size2(150, 74));
  309. break;
  310. case AnimationTreePlayer::NODE_BLEND3:
  311. edit_label[0]->set_text(TTR("Blend:"));
  312. edit_label[0]->set_position(Point2(5, 5));
  313. edit_label[0]->show();
  314. edit_scroll[0]->set_min(-1);
  315. edit_scroll[0]->set_max(1);
  316. edit_scroll[0]->set_step(0.01);
  317. edit_scroll[0]->set_value(anim_tree->blend3_node_get_amount(edited_node));
  318. edit_scroll[0]->set_begin(Point2(15, 25));
  319. edit_scroll[0]->show();
  320. edit_dialog->set_size(Size2(150, 50));
  321. break;
  322. case AnimationTreePlayer::NODE_BLEND4:
  323. edit_label[0]->set_text(TTR("Blend 0:"));
  324. edit_label[0]->set_position(Point2(5, 5));
  325. edit_label[0]->show();
  326. edit_scroll[0]->set_min(0);
  327. edit_scroll[0]->set_max(1);
  328. edit_scroll[0]->set_step(0.01);
  329. edit_scroll[0]->set_value(anim_tree->blend4_node_get_amount(edited_node).x);
  330. edit_scroll[0]->set_begin(Point2(15, 25));
  331. edit_scroll[0]->show();
  332. edit_label[1]->set_text(TTR("Blend 1:"));
  333. edit_label[1]->set_position(Point2(5, 55));
  334. edit_label[1]->show();
  335. edit_scroll[1]->set_min(0);
  336. edit_scroll[1]->set_max(1);
  337. edit_scroll[1]->set_step(0.01);
  338. edit_scroll[1]->set_value(anim_tree->blend4_node_get_amount(edited_node).y);
  339. edit_scroll[1]->set_begin(Point2(15, 75));
  340. edit_scroll[1]->show();
  341. edit_dialog->set_size(Size2(150, 100));
  342. break;
  343. case AnimationTreePlayer::NODE_TRANSITION: {
  344. edit_label[0]->set_text(TTR("X-Fade Time (s):"));
  345. edit_label[0]->set_position(Point2(5, 5));
  346. edit_label[0]->show();
  347. edit_line[0]->set_begin(Point2(15, 25));
  348. edit_line[0]->set_text(rtos(anim_tree->transition_node_get_xfade_time(edited_node)));
  349. edit_line[0]->show();
  350. edit_label[1]->set_text(TTR("Current:"));
  351. edit_label[1]->set_position(Point2(5, 55));
  352. edit_label[1]->show();
  353. edit_option->set_begin(Point2(15, 75));
  354. edit_option->clear();
  355. for (int i = 0; i < anim_tree->transition_node_get_input_count(edited_node); i++) {
  356. edit_option->add_item(itos(i), i);
  357. }
  358. edit_option->select(anim_tree->transition_node_get_current(edited_node));
  359. edit_option->show();
  360. edit_dialog->set_size(Size2(150, 100));
  361. } break;
  362. default: {}
  363. }
  364. }
  365. edit_dialog->set_position(popup_pos);
  366. edit_dialog->popup();
  367. updating_edit = false;
  368. }
  369. void AnimationTreeEditor::_draw_node(const StringName &p_node) {
  370. RID ci = get_canvas_item();
  371. AnimationTreePlayer::NodeType type = anim_tree->node_get_type(p_node);
  372. Ref<StyleBox> style = get_stylebox("panel", "PopupMenu");
  373. Ref<Font> font = get_font("font", "PopupMenu");
  374. Color font_color = get_color("font_color", "PopupMenu");
  375. Color font_color_title = get_color("font_color_hover", "PopupMenu");
  376. font_color_title.a *= 0.8;
  377. Ref<Texture> slot_icon = get_icon("VisualShaderPort", "EditorIcons");
  378. Size2 size = get_node_size(p_node);
  379. Point2 pos = anim_tree->node_get_position(p_node);
  380. if (click_type == CLICK_NODE && click_node == p_node) {
  381. pos += click_motion - click_pos;
  382. if (pos.x < 5)
  383. pos.x = 5;
  384. if (pos.y < 5)
  385. pos.y = 5;
  386. }
  387. pos -= Point2(h_scroll->get_value(), v_scroll->get_value());
  388. style->draw(ci, Rect2(pos, size));
  389. float w = size.width - style->get_minimum_size().width;
  390. float h = font->get_height() + get_constant("vseparation", "PopupMenu");
  391. Point2 ofs = style->get_offset() + pos;
  392. Point2 ascofs(0, font->get_ascent());
  393. Color bx = font_color_title;
  394. bx.a *= 0.1;
  395. draw_rect(Rect2(ofs, Size2(size.width - style->get_minimum_size().width, font->get_height())), bx);
  396. font->draw_halign(ci, ofs + ascofs, HALIGN_CENTER, w, String(_node_type_names[type]), font_color_title);
  397. ofs.y += h;
  398. font->draw_halign(ci, ofs + ascofs, HALIGN_CENTER, w, p_node, font_color);
  399. ofs.y += h;
  400. int count = 2; // title and name
  401. int inputs = anim_tree->node_get_input_count(p_node);
  402. count += inputs ? inputs : 1;
  403. float icon_h_ofs = Math::floor((font->get_height() - slot_icon->get_height()) / 2.0) + 1;
  404. if (type != AnimationTreePlayer::NODE_OUTPUT)
  405. slot_icon->draw(ci, ofs + Point2(w, icon_h_ofs)); //output
  406. if (inputs) {
  407. for (int i = 0; i < inputs; i++) {
  408. slot_icon->draw(ci, ofs + Point2(-slot_icon->get_width(), icon_h_ofs));
  409. String text;
  410. switch (type) {
  411. case AnimationTreePlayer::NODE_TIMESCALE:
  412. case AnimationTreePlayer::NODE_TIMESEEK: text = "in"; break;
  413. case AnimationTreePlayer::NODE_OUTPUT: text = "out"; break;
  414. case AnimationTreePlayer::NODE_ANIMATION: break;
  415. case AnimationTreePlayer::NODE_ONESHOT: text = (i == 0 ? "in" : "add"); break;
  416. case AnimationTreePlayer::NODE_BLEND2:
  417. case AnimationTreePlayer::NODE_MIX: text = (i == 0 ? "a" : "b"); break;
  418. case AnimationTreePlayer::NODE_BLEND3:
  419. switch (i) {
  420. case 0: text = "b-"; break;
  421. case 1: text = "a"; break;
  422. case 2: text = "b+"; break;
  423. }
  424. break;
  425. case AnimationTreePlayer::NODE_BLEND4:
  426. switch (i) {
  427. case 0: text = "a0"; break;
  428. case 1: text = "b0"; break;
  429. case 2: text = "a1"; break;
  430. case 3: text = "b1"; break;
  431. }
  432. break;
  433. case AnimationTreePlayer::NODE_TRANSITION:
  434. text = itos(i);
  435. if (anim_tree->transition_node_has_input_auto_advance(p_node, i))
  436. text += "->";
  437. break;
  438. default: {}
  439. }
  440. font->draw(ci, ofs + ascofs + Point2(3, 0), text, font_color);
  441. ofs.y += h;
  442. }
  443. } else {
  444. ofs.y += h;
  445. }
  446. Ref<StyleBox> pg_bg = get_stylebox("bg", "ProgressBar");
  447. Ref<StyleBox> pg_fill = get_stylebox("fill", "ProgressBar");
  448. Rect2 pg_rect(ofs, Size2(w, h));
  449. bool editable = true;
  450. switch (type) {
  451. case AnimationTreePlayer::NODE_ANIMATION: {
  452. Ref<Animation> anim = anim_tree->animation_node_get_animation(p_node);
  453. String text;
  454. if (anim_tree->animation_node_get_master_animation(p_node) != "")
  455. text = anim_tree->animation_node_get_master_animation(p_node);
  456. else if (anim.is_null())
  457. text = "load..";
  458. else
  459. text = anim->get_name();
  460. font->draw_halign(ci, ofs + ascofs, HALIGN_CENTER, w, text, font_color_title);
  461. } break;
  462. case AnimationTreePlayer::NODE_ONESHOT:
  463. case AnimationTreePlayer::NODE_MIX:
  464. case AnimationTreePlayer::NODE_BLEND2:
  465. case AnimationTreePlayer::NODE_BLEND3:
  466. case AnimationTreePlayer::NODE_BLEND4:
  467. case AnimationTreePlayer::NODE_TIMESCALE:
  468. case AnimationTreePlayer::NODE_TRANSITION: {
  469. font->draw_halign(ci, ofs + ascofs, HALIGN_CENTER, w, "edit..", font_color_title);
  470. } break;
  471. default: editable = false;
  472. }
  473. if (editable) {
  474. Ref<Texture> arrow = get_icon("GuiDropdown", "EditorIcons");
  475. Point2 arrow_ofs(w - arrow->get_width(), Math::floor((h - arrow->get_height()) / 2));
  476. arrow->draw(ci, ofs + arrow_ofs);
  477. }
  478. }
  479. AnimationTreeEditor::ClickType AnimationTreeEditor::_locate_click(const Point2 &p_click, StringName *p_node_id, int *p_slot_index) const {
  480. Ref<StyleBox> style = get_stylebox("panel", "PopupMenu");
  481. Ref<Font> font = get_font("font", "PopupMenu");
  482. float h = (font->get_height() + get_constant("vseparation", "PopupMenu"));
  483. for (const List<StringName>::Element *E = order.back(); E; E = E->prev()) {
  484. StringName node = E->get();
  485. AnimationTreePlayer::NodeType type = anim_tree->node_get_type(node);
  486. Point2 pos = anim_tree->node_get_position(node);
  487. Size2 size = get_node_size(node);
  488. pos -= Point2(h_scroll->get_value(), v_scroll->get_value());
  489. if (!Rect2(pos, size).has_point(p_click))
  490. continue;
  491. if (p_node_id)
  492. *p_node_id = node;
  493. pos = p_click - pos;
  494. float y = pos.y - style->get_offset().height;
  495. if (y < 2 * h)
  496. return CLICK_NODE;
  497. y -= 2 * h;
  498. int inputs = anim_tree->node_get_input_count(node);
  499. int count = MAX(inputs, 1);
  500. if (inputs == 0 || (pos.x > size.width / 2 && type != AnimationTreePlayer::NODE_OUTPUT)) {
  501. if (y < count * h) {
  502. if (p_slot_index)
  503. *p_slot_index = 0;
  504. return CLICK_OUTPUT_SLOT;
  505. }
  506. }
  507. for (int i = 0; i < count; i++) {
  508. if (y < h) {
  509. if (p_slot_index)
  510. *p_slot_index = i;
  511. return CLICK_INPUT_SLOT;
  512. }
  513. y -= h;
  514. }
  515. bool has_parameters = type != AnimationTreePlayer::NODE_OUTPUT && type != AnimationTreePlayer::NODE_TIMESEEK;
  516. return has_parameters ? CLICK_PARAMETER : CLICK_NODE;
  517. }
  518. return CLICK_NONE;
  519. }
  520. Point2 AnimationTreeEditor::_get_slot_pos(const StringName &p_node_id, bool p_input, int p_slot) {
  521. Ref<StyleBox> style = get_stylebox("panel", "PopupMenu");
  522. Ref<Font> font = get_font("font", "PopupMenu");
  523. Ref<Texture> slot_icon = get_icon("VisualShaderPort", "EditorIcons");
  524. Size2 size = get_node_size(p_node_id);
  525. Point2 pos = anim_tree->node_get_position(p_node_id);
  526. if (click_type == CLICK_NODE && click_node == p_node_id) {
  527. pos += click_motion - click_pos;
  528. if (pos.x < 5)
  529. pos.x = 5;
  530. if (pos.y < 5)
  531. pos.y = 5;
  532. }
  533. pos -= Point2(h_scroll->get_value(), v_scroll->get_value());
  534. float w = size.width - style->get_minimum_size().width;
  535. float h = font->get_height() + get_constant("vseparation", "PopupMenu");
  536. pos += style->get_offset();
  537. pos.y += h * 2;
  538. pos.y += h * p_slot;
  539. pos += Point2(-slot_icon->get_width() / 2.0, h / 2.0).floor();
  540. if (!p_input) {
  541. pos.x += w + slot_icon->get_width();
  542. }
  543. return pos;
  544. }
  545. void AnimationTreeEditor::_gui_input(Ref<InputEvent> p_event) {
  546. Ref<InputEventMouseButton> mb = p_event;
  547. if (mb.is_valid()) {
  548. if (mb->is_pressed()) {
  549. if (mb->get_button_index() == 1) {
  550. click_pos = Point2(mb->get_position().x, mb->get_position().y);
  551. click_motion = click_pos;
  552. click_type = _locate_click(click_pos, &click_node, &click_slot);
  553. if (click_type != CLICK_NONE) {
  554. order.erase(click_node);
  555. order.push_back(click_node);
  556. update();
  557. }
  558. switch (click_type) {
  559. case CLICK_INPUT_SLOT: {
  560. click_pos = _get_slot_pos(click_node, true, click_slot);
  561. } break;
  562. case CLICK_OUTPUT_SLOT: {
  563. click_pos = _get_slot_pos(click_node, false, click_slot);
  564. } break;
  565. case CLICK_PARAMETER: {
  566. edited_node = click_node;
  567. renaming_edit = false;
  568. _popup_edit_dialog();
  569. //open editor
  570. //_node_edit_property(click_node);
  571. } break;
  572. default: {}
  573. }
  574. }
  575. if (mb->get_button_index() == 2) {
  576. if (click_type != CLICK_NONE) {
  577. click_type = CLICK_NONE;
  578. update();
  579. } else {
  580. // try to disconnect/remove
  581. Point2 rclick_pos = Point2(mb->get_position().x, mb->get_position().y);
  582. rclick_type = _locate_click(rclick_pos, &rclick_node, &rclick_slot);
  583. if (rclick_type == CLICK_INPUT_SLOT || rclick_type == CLICK_OUTPUT_SLOT) {
  584. node_popup->clear();
  585. node_popup->add_item(TTR("Disconnect"), NODE_DISCONNECT);
  586. if (anim_tree->node_get_type(rclick_node) == AnimationTreePlayer::NODE_TRANSITION) {
  587. node_popup->add_item(TTR("Add Input"), NODE_ADD_INPUT);
  588. if (rclick_type == CLICK_INPUT_SLOT) {
  589. if (anim_tree->transition_node_has_input_auto_advance(rclick_node, rclick_slot))
  590. node_popup->add_item(TTR("Clear Auto-Advance"), NODE_CLEAR_AUTOADVANCE);
  591. else
  592. node_popup->add_item(TTR("Set Auto-Advance"), NODE_SET_AUTOADVANCE);
  593. node_popup->add_item(TTR("Delete Input"), NODE_DELETE_INPUT);
  594. }
  595. }
  596. node_popup->set_position(rclick_pos + get_global_position());
  597. node_popup->popup();
  598. }
  599. if (rclick_type == CLICK_NODE) {
  600. node_popup->clear();
  601. node_popup->add_item(TTR("Rename"), NODE_RENAME);
  602. node_popup->add_item(TTR("Remove"), NODE_ERASE);
  603. if (anim_tree->node_get_type(rclick_node) == AnimationTreePlayer::NODE_TRANSITION)
  604. node_popup->add_item(TTR("Add Input"), NODE_ADD_INPUT);
  605. node_popup->set_position(rclick_pos + get_global_position());
  606. node_popup->popup();
  607. }
  608. }
  609. }
  610. } else {
  611. if (mb->get_button_index() == 1 && click_type != CLICK_NONE) {
  612. switch (click_type) {
  613. case CLICK_INPUT_SLOT:
  614. case CLICK_OUTPUT_SLOT: {
  615. Point2 dst_click_pos = Point2(mb->get_position().x, mb->get_position().y);
  616. StringName id;
  617. int slot;
  618. ClickType dst_click_type = _locate_click(dst_click_pos, &id, &slot);
  619. if (dst_click_type == CLICK_INPUT_SLOT && click_type == CLICK_OUTPUT_SLOT) {
  620. anim_tree->connect_nodes(click_node, id, slot);
  621. }
  622. if (click_type == CLICK_INPUT_SLOT && dst_click_type == CLICK_OUTPUT_SLOT) {
  623. anim_tree->connect_nodes(id, click_node, click_slot);
  624. }
  625. } break;
  626. case CLICK_NODE: {
  627. Point2 new_pos = anim_tree->node_get_position(click_node) + (click_motion - click_pos);
  628. if (new_pos.x < 5)
  629. new_pos.x = 5;
  630. if (new_pos.y < 5)
  631. new_pos.y = 5;
  632. anim_tree->node_set_position(click_node, new_pos);
  633. } break;
  634. default: {}
  635. }
  636. click_type = CLICK_NONE;
  637. update();
  638. }
  639. }
  640. }
  641. Ref<InputEventMouseMotion> mm = p_event;
  642. if (mm.is_valid()) {
  643. if (mm->get_button_mask() & 1 && click_type != CLICK_NONE) {
  644. click_motion = Point2(mm->get_position().x, mm->get_position().y);
  645. update();
  646. }
  647. if ((mm->get_button_mask() & 4 || Input::get_singleton()->is_key_pressed(KEY_SPACE))) {
  648. h_scroll->set_value(h_scroll->get_value() - mm->get_relative().x);
  649. v_scroll->set_value(v_scroll->get_value() - mm->get_relative().y);
  650. update();
  651. }
  652. }
  653. }
  654. void AnimationTreeEditor::_draw_cos_line(const Vector2 &p_from, const Vector2 &p_to, const Color &p_color) {
  655. static const int steps = 20;
  656. Rect2 r;
  657. r.position = p_from;
  658. r.expand_to(p_to);
  659. Vector2 sign = Vector2((p_from.x < p_to.x) ? 1 : -1, (p_from.y < p_to.y) ? 1 : -1);
  660. bool flip = sign.x * sign.y < 0;
  661. Vector2 prev;
  662. for (int i = 0; i <= steps; i++) {
  663. float d = i / float(steps);
  664. float c = -Math::cos(d * Math_PI) * 0.5 + 0.5;
  665. if (flip)
  666. c = 1.0 - c;
  667. Vector2 p = r.position + Vector2(d * r.size.width, c * r.size.height);
  668. if (i > 0) {
  669. draw_line(prev, p, p_color, 2);
  670. }
  671. prev = p;
  672. }
  673. }
  674. void AnimationTreeEditor::_notification(int p_what) {
  675. switch (p_what) {
  676. case NOTIFICATION_ENTER_TREE: {
  677. play_button->set_icon(get_icon("Play", "EditorIcons"));
  678. add_menu->set_icon(get_icon("Add", "EditorIcons"));
  679. } break;
  680. case NOTIFICATION_DRAW: {
  681. _update_scrollbars();
  682. //VisualServer::get_singleton()->canvas_item_add_rect(get_canvas_item(),Rect2(Point2(),get_size()),Color(0,0,0,1));
  683. get_stylebox("bg", "Tree")->draw(get_canvas_item(), Rect2(Point2(), get_size()));
  684. for (List<StringName>::Element *E = order.front(); E; E = E->next()) {
  685. _draw_node(E->get());
  686. }
  687. if (click_type == CLICK_INPUT_SLOT || click_type == CLICK_OUTPUT_SLOT) {
  688. _draw_cos_line(click_pos, click_motion, Color(0.5, 1, 0.5, 0.8));
  689. }
  690. List<AnimationTreePlayer::Connection> connections;
  691. anim_tree->get_connection_list(&connections);
  692. for (List<AnimationTreePlayer::Connection>::Element *E = connections.front(); E; E = E->next()) {
  693. const AnimationTreePlayer::Connection &c = E->get();
  694. Point2 source = _get_slot_pos(c.src_node, false, 0);
  695. Point2 dest = _get_slot_pos(c.dst_node, true, c.dst_input);
  696. Color col = Color(1, 1, 0.5, 0.8);
  697. /*
  698. if (click_type==CLICK_NODE && click_node==c.src_node) {
  699. source+=click_motion-click_pos;
  700. }
  701. if (click_type==CLICK_NODE && click_node==c.dst_node) {
  702. dest+=click_motion-click_pos;
  703. }*/
  704. _draw_cos_line(source, dest, col);
  705. }
  706. switch (anim_tree->get_last_error()) {
  707. case AnimationTreePlayer::CONNECT_OK: {
  708. Ref<Font> f = get_font("font", "Label");
  709. f->draw(get_canvas_item(), Point2(5, 25 + f->get_ascent()), TTR("Animation tree is valid."), Color(0, 1, 0.6, 0.8));
  710. } break;
  711. default: {
  712. Ref<Font> f = get_font("font", "Label");
  713. f->draw(get_canvas_item(), Point2(5, 25 + f->get_ascent()), TTR("Animation tree is invalid."), Color(1, 0.6, 0.0, 0.8));
  714. } break;
  715. }
  716. } break;
  717. }
  718. }
  719. void AnimationTreeEditor::_update_scrollbars() {
  720. Size2 size = get_size();
  721. Size2 hmin = h_scroll->get_combined_minimum_size();
  722. Size2 vmin = v_scroll->get_combined_minimum_size();
  723. v_scroll->set_begin(Point2(size.width - vmin.width, 0));
  724. v_scroll->set_end(Point2(size.width, size.height));
  725. h_scroll->set_begin(Point2(0, size.height - hmin.height));
  726. h_scroll->set_end(Point2(size.width - vmin.width, size.height));
  727. Size2 min = _get_maximum_size();
  728. if (min.height < size.height - hmin.height) {
  729. v_scroll->hide();
  730. offset.y = 0;
  731. } else {
  732. v_scroll->show();
  733. v_scroll->set_max(min.height);
  734. v_scroll->set_page(size.height - hmin.height);
  735. offset.y = v_scroll->get_value();
  736. }
  737. if (min.width < size.width - vmin.width) {
  738. h_scroll->hide();
  739. offset.x = 0;
  740. } else {
  741. h_scroll->show();
  742. h_scroll->set_max(min.width);
  743. h_scroll->set_page(size.width - vmin.width);
  744. offset.x = h_scroll->get_value();
  745. }
  746. }
  747. void AnimationTreeEditor::_scroll_moved(float) {
  748. offset.x = h_scroll->get_value();
  749. offset.y = v_scroll->get_value();
  750. update();
  751. }
  752. void AnimationTreeEditor::_node_menu_item(int p_item) {
  753. switch (p_item) {
  754. case NODE_DISCONNECT: {
  755. if (rclick_type == CLICK_INPUT_SLOT) {
  756. anim_tree->disconnect_nodes(rclick_node, rclick_slot);
  757. update();
  758. }
  759. if (rclick_type == CLICK_OUTPUT_SLOT) {
  760. List<AnimationTreePlayer::Connection> connections;
  761. anim_tree->get_connection_list(&connections);
  762. for (List<AnimationTreePlayer::Connection>::Element *E = connections.front(); E; E = E->next()) {
  763. const AnimationTreePlayer::Connection &c = E->get();
  764. if (c.dst_node == rclick_node) {
  765. anim_tree->disconnect_nodes(c.dst_node, c.dst_input);
  766. }
  767. }
  768. update();
  769. }
  770. } break;
  771. case NODE_RENAME: {
  772. renaming_edit = true;
  773. edited_node = rclick_node;
  774. _popup_edit_dialog();
  775. } break;
  776. case NODE_ADD_INPUT: {
  777. anim_tree->transition_node_set_input_count(rclick_node, anim_tree->transition_node_get_input_count(rclick_node) + 1);
  778. update();
  779. } break;
  780. case NODE_DELETE_INPUT: {
  781. anim_tree->transition_node_delete_input(rclick_node, rclick_slot);
  782. update();
  783. } break;
  784. case NODE_SET_AUTOADVANCE: {
  785. anim_tree->transition_node_set_input_auto_advance(rclick_node, rclick_slot, true);
  786. update();
  787. } break;
  788. case NODE_CLEAR_AUTOADVANCE: {
  789. anim_tree->transition_node_set_input_auto_advance(rclick_node, rclick_slot, false);
  790. update();
  791. } break;
  792. case NODE_ERASE: {
  793. if (rclick_node == "out")
  794. break;
  795. order.erase(rclick_node);
  796. anim_tree->remove_node(rclick_node);
  797. update();
  798. } break;
  799. }
  800. }
  801. StringName AnimationTreeEditor::_add_node(int p_item) {
  802. static const char *bname[] = {
  803. "out",
  804. "anim",
  805. "oneshot",
  806. "mix",
  807. "blend2",
  808. "blend3",
  809. "blend4",
  810. "scale",
  811. "seek",
  812. "transition"
  813. };
  814. String name;
  815. int idx = 1;
  816. while (true) {
  817. name = bname[p_item];
  818. if (idx > 1)
  819. name += " " + itos(idx);
  820. if (anim_tree->node_exists(name))
  821. idx++;
  822. else
  823. break;
  824. }
  825. anim_tree->add_node((AnimationTreePlayer::NodeType)p_item, name);
  826. anim_tree->node_set_position(name, Point2(last_x, last_y));
  827. order.push_back(name);
  828. last_x += 10;
  829. last_y += 10;
  830. last_x = last_x % (int)get_size().width;
  831. last_y = last_y % (int)get_size().height;
  832. update();
  833. return name;
  834. };
  835. void AnimationTreeEditor::_file_dialog_selected(String p_path) {
  836. switch (file_op) {
  837. case MENU_IMPORT_ANIMATIONS: {
  838. Vector<String> files = file_dialog->get_selected_files();
  839. for (int i = 0; i < files.size(); i++) {
  840. StringName node = _add_node(AnimationTreePlayer::NODE_ANIMATION);
  841. RES anim = ResourceLoader::load(files[i]);
  842. anim_tree->animation_node_set_animation(node, anim);
  843. //anim_tree->node_set_name(node, files[i].get_file());
  844. };
  845. } break;
  846. default:
  847. break;
  848. };
  849. };
  850. void AnimationTreeEditor::_add_menu_item(int p_item) {
  851. if (p_item == MENU_GRAPH_CLEAR) {
  852. //clear
  853. } else if (p_item == MENU_IMPORT_ANIMATIONS) {
  854. file_op = MENU_IMPORT_ANIMATIONS;
  855. file_dialog->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  856. file_dialog->popup_centered_ratio();
  857. } else {
  858. _add_node(p_item);
  859. }
  860. }
  861. Size2 AnimationTreeEditor::get_minimum_size() const {
  862. return Size2(10, 200);
  863. }
  864. void AnimationTreeEditor::_find_paths_for_filter(const StringName &p_node, Set<String> &paths) {
  865. ERR_FAIL_COND(!anim_tree->node_exists(p_node));
  866. for (int i = 0; i < anim_tree->node_get_input_count(p_node); i++) {
  867. StringName port = anim_tree->node_get_input_source(p_node, i);
  868. if (port == StringName())
  869. continue;
  870. _find_paths_for_filter(port, paths);
  871. }
  872. if (anim_tree->node_get_type(p_node) == AnimationTreePlayer::NODE_ANIMATION) {
  873. Ref<Animation> anim = anim_tree->animation_node_get_animation(p_node);
  874. if (anim.is_valid()) {
  875. for (int i = 0; i < anim->get_track_count(); i++) {
  876. paths.insert(anim->track_get_path(i));
  877. }
  878. }
  879. }
  880. }
  881. void AnimationTreeEditor::_filter_edited() {
  882. TreeItem *ed = filter->get_edited();
  883. if (!ed)
  884. return;
  885. if (anim_tree->node_get_type(edited_node) == AnimationTreePlayer::NODE_ONESHOT) {
  886. anim_tree->oneshot_node_set_filter_path(edited_node, ed->get_metadata(0), ed->is_checked(0));
  887. } else if (anim_tree->node_get_type(edited_node) == AnimationTreePlayer::NODE_BLEND2) {
  888. anim_tree->blend2_node_set_filter_path(edited_node, ed->get_metadata(0), ed->is_checked(0));
  889. } else if (anim_tree->node_get_type(edited_node) == AnimationTreePlayer::NODE_ANIMATION) {
  890. anim_tree->animation_node_set_filter_path(edited_node, ed->get_metadata(0), ed->is_checked(0));
  891. }
  892. }
  893. void AnimationTreeEditor::_edit_filters() {
  894. filter_dialog->popup_centered_ratio();
  895. filter->clear();
  896. Set<String> npb;
  897. _find_paths_for_filter(edited_node, npb);
  898. TreeItem *root = filter->create_item();
  899. filter->set_hide_root(true);
  900. Map<String, TreeItem *> pm;
  901. Node *base = anim_tree->get_node(anim_tree->get_base_path());
  902. for (Set<String>::Element *E = npb.front(); E; E = E->next()) {
  903. TreeItem *parent = root;
  904. String descr = E->get();
  905. if (base) {
  906. NodePath np = E->get();
  907. if (np.get_property() != StringName()) {
  908. Node *n = base->get_node(np);
  909. Skeleton *s = Object::cast_to<Skeleton>(n);
  910. if (s) {
  911. String skelbase = E->get().substr(0, E->get().find(":"));
  912. int bidx = s->find_bone(np.get_property());
  913. if (bidx != -1) {
  914. int bparent = s->get_bone_parent(bidx);
  915. //
  916. if (bparent != -1) {
  917. String bpn = skelbase + ":" + s->get_bone_name(bparent);
  918. if (pm.has(bpn)) {
  919. parent = pm[bpn];
  920. descr = np.get_property();
  921. }
  922. } else {
  923. if (pm.has(skelbase)) {
  924. parent = pm[skelbase];
  925. }
  926. }
  927. }
  928. }
  929. }
  930. }
  931. TreeItem *it = filter->create_item(parent);
  932. it->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  933. it->set_text(0, descr);
  934. it->set_metadata(0, NodePath(E->get()));
  935. it->set_editable(0, true);
  936. if (anim_tree->node_get_type(edited_node) == AnimationTreePlayer::NODE_ONESHOT) {
  937. it->set_checked(0, anim_tree->oneshot_node_is_path_filtered(edited_node, E->get()));
  938. } else if (anim_tree->node_get_type(edited_node) == AnimationTreePlayer::NODE_BLEND2) {
  939. it->set_checked(0, anim_tree->blend2_node_is_path_filtered(edited_node, E->get()));
  940. } else if (anim_tree->node_get_type(edited_node) == AnimationTreePlayer::NODE_ANIMATION) {
  941. it->set_checked(0, anim_tree->animation_node_is_path_filtered(edited_node, E->get()));
  942. }
  943. pm[E->get()] = it;
  944. }
  945. }
  946. void AnimationTreeEditor::_bind_methods() {
  947. ClassDB::bind_method("_add_menu_item", &AnimationTreeEditor::_add_menu_item);
  948. ClassDB::bind_method("_node_menu_item", &AnimationTreeEditor::_node_menu_item);
  949. ClassDB::bind_method("_gui_input", &AnimationTreeEditor::_gui_input);
  950. //ClassDB::bind_method( "_node_param_changed", &AnimationTreeEditor::_node_param_changed );
  951. ClassDB::bind_method("_scroll_moved", &AnimationTreeEditor::_scroll_moved);
  952. ClassDB::bind_method("_edit_dialog_changeds", &AnimationTreeEditor::_edit_dialog_changeds);
  953. ClassDB::bind_method("_edit_dialog_changede", &AnimationTreeEditor::_edit_dialog_changede);
  954. ClassDB::bind_method("_edit_dialog_changedf", &AnimationTreeEditor::_edit_dialog_changedf);
  955. ClassDB::bind_method("_edit_dialog_changed", &AnimationTreeEditor::_edit_dialog_changed);
  956. ClassDB::bind_method("_edit_dialog_animation_changed", &AnimationTreeEditor::_edit_dialog_animation_changed);
  957. ClassDB::bind_method("_edit_dialog_edit_animation", &AnimationTreeEditor::_edit_dialog_edit_animation);
  958. ClassDB::bind_method("_play_toggled", &AnimationTreeEditor::_play_toggled);
  959. ClassDB::bind_method("_edit_oneshot_start", &AnimationTreeEditor::_edit_oneshot_start);
  960. ClassDB::bind_method("_file_dialog_selected", &AnimationTreeEditor::_file_dialog_selected);
  961. ClassDB::bind_method("_master_anim_menu_item", &AnimationTreeEditor::_master_anim_menu_item);
  962. ClassDB::bind_method("_edit_filters", &AnimationTreeEditor::_edit_filters);
  963. ClassDB::bind_method("_filter_edited", &AnimationTreeEditor::_filter_edited);
  964. }
  965. AnimationTreeEditor::AnimationTreeEditor() {
  966. set_focus_mode(FOCUS_ALL);
  967. PopupMenu *p;
  968. List<PropertyInfo> defaults;
  969. add_menu = memnew(MenuButton);
  970. //add_menu->set_
  971. add_menu->set_position(Point2(0, 0));
  972. add_menu->set_size(Point2(25, 15));
  973. add_child(add_menu);
  974. p = add_menu->get_popup();
  975. p->add_item(TTR("Animation Node"), AnimationTreePlayer::NODE_ANIMATION);
  976. p->add_item(TTR("OneShot Node"), AnimationTreePlayer::NODE_ONESHOT);
  977. p->add_item(TTR("Mix Node"), AnimationTreePlayer::NODE_MIX);
  978. p->add_item(TTR("Blend2 Node"), AnimationTreePlayer::NODE_BLEND2);
  979. p->add_item(TTR("Blend3 Node"), AnimationTreePlayer::NODE_BLEND3);
  980. p->add_item(TTR("Blend4 Node"), AnimationTreePlayer::NODE_BLEND4);
  981. p->add_item(TTR("TimeScale Node"), AnimationTreePlayer::NODE_TIMESCALE);
  982. p->add_item(TTR("TimeSeek Node"), AnimationTreePlayer::NODE_TIMESEEK);
  983. p->add_item(TTR("Transition Node"), AnimationTreePlayer::NODE_TRANSITION);
  984. p->add_separator();
  985. p->add_item(TTR("Import Animations.."), MENU_IMPORT_ANIMATIONS); // wtf
  986. p->add_separator();
  987. p->add_item(TTR("Clear"), MENU_GRAPH_CLEAR);
  988. p->connect("id_pressed", this, "_add_menu_item");
  989. play_button = memnew(Button);
  990. play_button->set_position(Point2(25, 0));
  991. play_button->set_size(Point2(25, 15));
  992. add_child(play_button);
  993. play_button->set_toggle_mode(true);
  994. play_button->connect("pressed", this, "_play_toggled");
  995. last_x = 50;
  996. last_y = 50;
  997. property_editor = memnew(CustomPropertyEditor);
  998. add_child(property_editor);
  999. property_editor->connect("variant_changed", this, "_edit_dialog_animation_changed");
  1000. property_editor->connect("resource_edit_request", this, "_edit_dialog_edit_animation");
  1001. h_scroll = memnew(HScrollBar);
  1002. v_scroll = memnew(VScrollBar);
  1003. add_child(h_scroll);
  1004. add_child(v_scroll);
  1005. h_scroll->connect("value_changed", this, "_scroll_moved");
  1006. v_scroll->connect("value_changed", this, "_scroll_moved");
  1007. node_popup = memnew(PopupMenu);
  1008. add_child(node_popup);
  1009. node_popup->set_as_toplevel(true);
  1010. master_anim_popup = memnew(PopupMenu);
  1011. add_child(master_anim_popup);
  1012. master_anim_popup->connect("id_pressed", this, "_master_anim_menu_item");
  1013. node_popup->connect("id_pressed", this, "_node_menu_item");
  1014. updating_edit = false;
  1015. edit_dialog = memnew(PopupPanel);
  1016. //edit_dialog->get_ok()->hide();
  1017. //edit_dialog->get_cancel()->hide();
  1018. add_child(edit_dialog);
  1019. edit_option = memnew(OptionButton);
  1020. edit_option->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  1021. edit_option->set_margin(MARGIN_RIGHT, -10);
  1022. edit_dialog->add_child(edit_option);
  1023. edit_option->connect("item_selected", this, "_edit_dialog_changedf");
  1024. edit_option->hide();
  1025. for (int i = 0; i < 2; i++) {
  1026. edit_scroll[i] = memnew(HSlider);
  1027. edit_scroll[i]->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  1028. edit_scroll[i]->set_margin(MARGIN_RIGHT, -10);
  1029. edit_dialog->add_child(edit_scroll[i]);
  1030. edit_scroll[i]->hide();
  1031. edit_scroll[i]->connect("value_changed", this, "_edit_dialog_changedf");
  1032. }
  1033. for (int i = 0; i < 4; i++) {
  1034. edit_line[i] = memnew(LineEdit);
  1035. edit_line[i]->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  1036. edit_line[i]->set_margin(MARGIN_RIGHT, -10);
  1037. edit_dialog->add_child(edit_line[i]);
  1038. edit_line[i]->hide();
  1039. edit_line[i]->connect("text_changed", this, "_edit_dialog_changeds");
  1040. edit_line[i]->connect("text_entered", this, "_edit_dialog_changede");
  1041. edit_label[i] = memnew(Label);
  1042. edit_dialog->add_child(edit_label[i]);
  1043. edit_label[i]->hide();
  1044. }
  1045. edit_button = memnew(Button);
  1046. edit_button->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  1047. edit_button->set_margin(MARGIN_RIGHT, -10);
  1048. edit_dialog->add_child(edit_button);
  1049. edit_button->hide();
  1050. edit_button->connect("pressed", this, "_edit_oneshot_start");
  1051. edit_check = memnew(CheckButton);
  1052. edit_check->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  1053. edit_check->set_margin(MARGIN_RIGHT, -10);
  1054. edit_dialog->add_child(edit_check);
  1055. edit_check->hide();
  1056. edit_check->connect("pressed", this, "_edit_dialog_changed");
  1057. file_dialog = memnew(EditorFileDialog);
  1058. file_dialog->set_enable_multiple_selection(true);
  1059. file_dialog->set_current_dir(ProjectSettings::get_singleton()->get_resource_path());
  1060. add_child(file_dialog);
  1061. file_dialog->connect("file_selected", this, "_file_dialog_selected");
  1062. filter_dialog = memnew(AcceptDialog);
  1063. filter_dialog->set_title(TTR("Edit Node Filters"));
  1064. add_child(filter_dialog);
  1065. filter = memnew(Tree);
  1066. filter_dialog->add_child(filter);
  1067. //filter_dialog->set_child_rect(filter);
  1068. filter->connect("item_edited", this, "_filter_edited");
  1069. filter_button = memnew(Button);
  1070. filter_button->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  1071. filter_button->set_margin(MARGIN_RIGHT, -10);
  1072. edit_dialog->add_child(filter_button);
  1073. filter_button->hide();
  1074. filter_button->set_text(TTR("Filters.."));
  1075. filter_button->connect("pressed", this, "_edit_filters");
  1076. set_clip_contents(true);
  1077. }
  1078. void AnimationTreeEditorPlugin::edit(Object *p_object) {
  1079. anim_tree_editor->edit(Object::cast_to<AnimationTreePlayer>(p_object));
  1080. }
  1081. bool AnimationTreeEditorPlugin::handles(Object *p_object) const {
  1082. return p_object->is_class("AnimationTreePlayer");
  1083. }
  1084. void AnimationTreeEditorPlugin::make_visible(bool p_visible) {
  1085. if (p_visible) {
  1086. //editor->hide_animation_player_editors();
  1087. //editor->animation_panel_make_visible(true);
  1088. button->show();
  1089. editor->make_bottom_panel_item_visible(anim_tree_editor);
  1090. anim_tree_editor->set_physics_process(true);
  1091. } else {
  1092. if (anim_tree_editor->is_visible_in_tree())
  1093. editor->hide_bottom_panel();
  1094. button->hide();
  1095. anim_tree_editor->set_physics_process(false);
  1096. }
  1097. }
  1098. AnimationTreeEditorPlugin::AnimationTreeEditorPlugin(EditorNode *p_node) {
  1099. editor = p_node;
  1100. anim_tree_editor = memnew(AnimationTreeEditor);
  1101. anim_tree_editor->set_custom_minimum_size(Size2(0, 300));
  1102. button = editor->add_bottom_panel_item("AnimationTree", anim_tree_editor);
  1103. button->hide();
  1104. }
  1105. AnimationTreeEditorPlugin::~AnimationTreeEditorPlugin() {
  1106. }