animation_tree_player_editor_plugin.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447
  1. /*************************************************************************/
  2. /* animation_tree_player_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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_player_editor_plugin.h"
  31. #include "core/io/resource_loader.h"
  32. #include "core/os/input.h"
  33. #include "core/os/keyboard.h"
  34. #include "core/project_settings.h"
  35. #include "scene/gui/menu_button.h"
  36. #include "scene/gui/panel.h"
  37. #include "scene/main/viewport.h"
  38. void AnimationTreePlayerEditor::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 AnimationTreePlayerEditor::_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 *AnimationTreePlayerEditor::_node_type_names[] = { "Output", "Animation", "OneShot", "Mix", "Blend2", "Blend3", "Blend4", "TimeScale", "TimeSeek", "Transition" };
  70. Size2 AnimationTreePlayerEditor::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 AnimationTreePlayerEditor::_edit_dialog_changede(String) {
  104. edit_dialog->hide();
  105. }
  106. void AnimationTreePlayerEditor::_edit_dialog_changeds(String s) {
  107. _edit_dialog_changed();
  108. }
  109. void AnimationTreePlayerEditor::_edit_dialog_changedf(float) {
  110. _edit_dialog_changed();
  111. }
  112. void AnimationTreePlayerEditor::_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 AnimationTreePlayerEditor::_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 AnimationTreePlayerEditor::_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 AnimationTreePlayerEditor::_edit_oneshot_start() {
  170. anim_tree->oneshot_node_start(edited_node);
  171. }
  172. void AnimationTreePlayerEditor::_play_toggled() {
  173. anim_tree->set_active(play_button->is_pressed());
  174. }
  175. void AnimationTreePlayerEditor::_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 AnimationTreePlayerEditor::_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 AnimationTreePlayerEditor::_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. AnimationTreePlayerEditor::ClickType AnimationTreePlayerEditor::_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 AnimationTreePlayerEditor::_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 AnimationTreePlayerEditor::_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->set_size(Size2(1, 1));
  586. node_popup->add_item(TTR("Disconnect"), NODE_DISCONNECT);
  587. if (anim_tree->node_get_type(rclick_node) == AnimationTreePlayer::NODE_TRANSITION) {
  588. node_popup->add_item(TTR("Add Input"), NODE_ADD_INPUT);
  589. if (rclick_type == CLICK_INPUT_SLOT) {
  590. if (anim_tree->transition_node_has_input_auto_advance(rclick_node, rclick_slot))
  591. node_popup->add_item(TTR("Clear Auto-Advance"), NODE_CLEAR_AUTOADVANCE);
  592. else
  593. node_popup->add_item(TTR("Set Auto-Advance"), NODE_SET_AUTOADVANCE);
  594. node_popup->add_item(TTR("Delete Input"), NODE_DELETE_INPUT);
  595. }
  596. }
  597. node_popup->set_position(rclick_pos + get_global_position());
  598. node_popup->popup();
  599. }
  600. if (rclick_type == CLICK_NODE) {
  601. node_popup->clear();
  602. node_popup->set_size(Size2(1, 1));
  603. node_popup->add_item(TTR("Rename"), NODE_RENAME);
  604. node_popup->add_item(TTR("Remove"), NODE_ERASE);
  605. if (anim_tree->node_get_type(rclick_node) == AnimationTreePlayer::NODE_TRANSITION)
  606. node_popup->add_item(TTR("Add Input"), NODE_ADD_INPUT);
  607. node_popup->set_position(rclick_pos + get_global_position());
  608. node_popup->popup();
  609. }
  610. }
  611. }
  612. } else {
  613. if (mb->get_button_index() == 1 && click_type != CLICK_NONE) {
  614. switch (click_type) {
  615. case CLICK_INPUT_SLOT:
  616. case CLICK_OUTPUT_SLOT: {
  617. Point2 dst_click_pos = Point2(mb->get_position().x, mb->get_position().y);
  618. StringName id;
  619. int slot;
  620. ClickType dst_click_type = _locate_click(dst_click_pos, &id, &slot);
  621. if (dst_click_type == CLICK_INPUT_SLOT && click_type == CLICK_OUTPUT_SLOT) {
  622. anim_tree->connect_nodes(click_node, id, slot);
  623. }
  624. if (click_type == CLICK_INPUT_SLOT && dst_click_type == CLICK_OUTPUT_SLOT) {
  625. anim_tree->connect_nodes(id, click_node, click_slot);
  626. }
  627. } break;
  628. case CLICK_NODE: {
  629. Point2 new_pos = anim_tree->node_get_position(click_node) + (click_motion - click_pos);
  630. if (new_pos.x < 5)
  631. new_pos.x = 5;
  632. if (new_pos.y < 5)
  633. new_pos.y = 5;
  634. anim_tree->node_set_position(click_node, new_pos);
  635. } break;
  636. default: {}
  637. }
  638. click_type = CLICK_NONE;
  639. update();
  640. }
  641. }
  642. }
  643. Ref<InputEventMouseMotion> mm = p_event;
  644. if (mm.is_valid()) {
  645. if (mm->get_button_mask() & 1 && click_type != CLICK_NONE) {
  646. click_motion = Point2(mm->get_position().x, mm->get_position().y);
  647. update();
  648. }
  649. if ((mm->get_button_mask() & 4 || Input::get_singleton()->is_key_pressed(KEY_SPACE))) {
  650. h_scroll->set_value(h_scroll->get_value() - mm->get_relative().x);
  651. v_scroll->set_value(v_scroll->get_value() - mm->get_relative().y);
  652. update();
  653. }
  654. }
  655. }
  656. void AnimationTreePlayerEditor::_draw_cos_line(const Vector2 &p_from, const Vector2 &p_to, const Color &p_color) {
  657. static const int steps = 20;
  658. Rect2 r;
  659. r.position = p_from;
  660. r.expand_to(p_to);
  661. Vector2 sign = Vector2((p_from.x < p_to.x) ? 1 : -1, (p_from.y < p_to.y) ? 1 : -1);
  662. bool flip = sign.x * sign.y < 0;
  663. Vector2 prev;
  664. for (int i = 0; i <= steps; i++) {
  665. float d = i / float(steps);
  666. float c = -Math::cos(d * Math_PI) * 0.5 + 0.5;
  667. if (flip)
  668. c = 1.0 - c;
  669. Vector2 p = r.position + Vector2(d * r.size.width, c * r.size.height);
  670. if (i > 0) {
  671. draw_line(prev, p, p_color, 2);
  672. }
  673. prev = p;
  674. }
  675. }
  676. void AnimationTreePlayerEditor::_notification(int p_what) {
  677. switch (p_what) {
  678. case NOTIFICATION_ENTER_TREE: {
  679. play_button->set_icon(get_icon("Play", "EditorIcons"));
  680. add_menu->set_icon(get_icon("Add", "EditorIcons"));
  681. } break;
  682. case NOTIFICATION_DRAW: {
  683. _update_scrollbars();
  684. //VisualServer::get_singleton()->canvas_item_add_rect(get_canvas_item(),Rect2(Point2(),get_size()),Color(0,0,0,1));
  685. get_stylebox("bg", "Tree")->draw(get_canvas_item(), Rect2(Point2(), get_size()));
  686. for (List<StringName>::Element *E = order.front(); E; E = E->next()) {
  687. _draw_node(E->get());
  688. }
  689. if (click_type == CLICK_INPUT_SLOT || click_type == CLICK_OUTPUT_SLOT) {
  690. _draw_cos_line(click_pos, click_motion, Color(0.5, 1, 0.5, 0.8));
  691. }
  692. List<AnimationTreePlayer::Connection> connections;
  693. anim_tree->get_connection_list(&connections);
  694. for (List<AnimationTreePlayer::Connection>::Element *E = connections.front(); E; E = E->next()) {
  695. const AnimationTreePlayer::Connection &c = E->get();
  696. Point2 source = _get_slot_pos(c.src_node, false, 0);
  697. Point2 dest = _get_slot_pos(c.dst_node, true, c.dst_input);
  698. Color col = Color(1, 1, 0.5, 0.8);
  699. /*
  700. if (click_type==CLICK_NODE && click_node==c.src_node) {
  701. source+=click_motion-click_pos;
  702. }
  703. if (click_type==CLICK_NODE && click_node==c.dst_node) {
  704. dest+=click_motion-click_pos;
  705. }*/
  706. _draw_cos_line(source, dest, col);
  707. }
  708. switch (anim_tree->get_last_error()) {
  709. case AnimationTreePlayer::CONNECT_OK: {
  710. Ref<Font> f = get_font("font", "Label");
  711. f->draw(get_canvas_item(), Point2(5, 25 + f->get_ascent()), TTR("Animation tree is valid."), Color(0, 1, 0.6, 0.8));
  712. } break;
  713. default: {
  714. Ref<Font> f = get_font("font", "Label");
  715. f->draw(get_canvas_item(), Point2(5, 25 + f->get_ascent()), TTR("Animation tree is invalid."), Color(1, 0.6, 0.0, 0.8));
  716. } break;
  717. }
  718. } break;
  719. }
  720. }
  721. void AnimationTreePlayerEditor::_update_scrollbars() {
  722. Size2 size = get_size();
  723. Size2 hmin = h_scroll->get_combined_minimum_size();
  724. Size2 vmin = v_scroll->get_combined_minimum_size();
  725. v_scroll->set_begin(Point2(size.width - vmin.width, 0));
  726. v_scroll->set_end(Point2(size.width, size.height));
  727. h_scroll->set_begin(Point2(0, size.height - hmin.height));
  728. h_scroll->set_end(Point2(size.width - vmin.width, size.height));
  729. Size2 min = _get_maximum_size();
  730. if (min.height < size.height - hmin.height) {
  731. v_scroll->hide();
  732. offset.y = 0;
  733. } else {
  734. v_scroll->show();
  735. v_scroll->set_max(min.height);
  736. v_scroll->set_page(size.height - hmin.height);
  737. offset.y = v_scroll->get_value();
  738. }
  739. if (min.width < size.width - vmin.width) {
  740. h_scroll->hide();
  741. offset.x = 0;
  742. } else {
  743. h_scroll->show();
  744. h_scroll->set_max(min.width);
  745. h_scroll->set_page(size.width - vmin.width);
  746. offset.x = h_scroll->get_value();
  747. }
  748. }
  749. void AnimationTreePlayerEditor::_scroll_moved(float) {
  750. offset.x = h_scroll->get_value();
  751. offset.y = v_scroll->get_value();
  752. update();
  753. }
  754. void AnimationTreePlayerEditor::_node_menu_item(int p_item) {
  755. switch (p_item) {
  756. case NODE_DISCONNECT: {
  757. if (rclick_type == CLICK_INPUT_SLOT) {
  758. anim_tree->disconnect_nodes(rclick_node, rclick_slot);
  759. update();
  760. }
  761. if (rclick_type == CLICK_OUTPUT_SLOT) {
  762. List<AnimationTreePlayer::Connection> connections;
  763. anim_tree->get_connection_list(&connections);
  764. for (List<AnimationTreePlayer::Connection>::Element *E = connections.front(); E; E = E->next()) {
  765. const AnimationTreePlayer::Connection &c = E->get();
  766. if (c.dst_node == rclick_node) {
  767. anim_tree->disconnect_nodes(c.dst_node, c.dst_input);
  768. }
  769. }
  770. update();
  771. }
  772. } break;
  773. case NODE_RENAME: {
  774. renaming_edit = true;
  775. edited_node = rclick_node;
  776. _popup_edit_dialog();
  777. } break;
  778. case NODE_ADD_INPUT: {
  779. anim_tree->transition_node_set_input_count(rclick_node, anim_tree->transition_node_get_input_count(rclick_node) + 1);
  780. update();
  781. } break;
  782. case NODE_DELETE_INPUT: {
  783. anim_tree->transition_node_delete_input(rclick_node, rclick_slot);
  784. update();
  785. } break;
  786. case NODE_SET_AUTOADVANCE: {
  787. anim_tree->transition_node_set_input_auto_advance(rclick_node, rclick_slot, true);
  788. update();
  789. } break;
  790. case NODE_CLEAR_AUTOADVANCE: {
  791. anim_tree->transition_node_set_input_auto_advance(rclick_node, rclick_slot, false);
  792. update();
  793. } break;
  794. case NODE_ERASE: {
  795. if (rclick_node == "out")
  796. break;
  797. order.erase(rclick_node);
  798. anim_tree->remove_node(rclick_node);
  799. update();
  800. } break;
  801. }
  802. }
  803. StringName AnimationTreePlayerEditor::_add_node(int p_item) {
  804. static const char *bname[] = {
  805. "out",
  806. "anim",
  807. "oneshot",
  808. "mix",
  809. "blend2",
  810. "blend3",
  811. "blend4",
  812. "scale",
  813. "seek",
  814. "transition"
  815. };
  816. String name;
  817. int idx = 1;
  818. while (true) {
  819. name = bname[p_item];
  820. if (idx > 1)
  821. name += " " + itos(idx);
  822. if (anim_tree->node_exists(name))
  823. idx++;
  824. else
  825. break;
  826. }
  827. anim_tree->add_node((AnimationTreePlayer::NodeType)p_item, name);
  828. anim_tree->node_set_position(name, Point2(last_x, last_y));
  829. order.push_back(name);
  830. last_x += 10;
  831. last_y += 10;
  832. last_x = last_x % (int)get_size().width;
  833. last_y = last_y % (int)get_size().height;
  834. update();
  835. return name;
  836. };
  837. void AnimationTreePlayerEditor::_file_dialog_selected(String p_path) {
  838. switch (file_op) {
  839. case MENU_IMPORT_ANIMATIONS: {
  840. Vector<String> files = file_dialog->get_selected_files();
  841. for (int i = 0; i < files.size(); i++) {
  842. StringName node = _add_node(AnimationTreePlayer::NODE_ANIMATION);
  843. RES anim = ResourceLoader::load(files[i]);
  844. anim_tree->animation_node_set_animation(node, anim);
  845. //anim_tree->node_set_name(node, files[i].get_file());
  846. };
  847. } break;
  848. default:
  849. break;
  850. };
  851. };
  852. void AnimationTreePlayerEditor::_add_menu_item(int p_item) {
  853. if (p_item == MENU_GRAPH_CLEAR) {
  854. //clear
  855. } else if (p_item == MENU_IMPORT_ANIMATIONS) {
  856. file_op = MENU_IMPORT_ANIMATIONS;
  857. file_dialog->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  858. file_dialog->popup_centered_ratio();
  859. } else {
  860. _add_node(p_item);
  861. }
  862. }
  863. Size2 AnimationTreePlayerEditor::get_minimum_size() const {
  864. return Size2(10, 200);
  865. }
  866. void AnimationTreePlayerEditor::_find_paths_for_filter(const StringName &p_node, Set<String> &paths) {
  867. ERR_FAIL_COND(!anim_tree->node_exists(p_node));
  868. for (int i = 0; i < anim_tree->node_get_input_count(p_node); i++) {
  869. StringName port = anim_tree->node_get_input_source(p_node, i);
  870. if (port == StringName())
  871. continue;
  872. _find_paths_for_filter(port, paths);
  873. }
  874. if (anim_tree->node_get_type(p_node) == AnimationTreePlayer::NODE_ANIMATION) {
  875. Ref<Animation> anim = anim_tree->animation_node_get_animation(p_node);
  876. if (anim.is_valid()) {
  877. for (int i = 0; i < anim->get_track_count(); i++) {
  878. paths.insert(anim->track_get_path(i));
  879. }
  880. }
  881. }
  882. }
  883. void AnimationTreePlayerEditor::_filter_edited() {
  884. TreeItem *ed = filter->get_edited();
  885. if (!ed)
  886. return;
  887. if (anim_tree->node_get_type(edited_node) == AnimationTreePlayer::NODE_ONESHOT) {
  888. anim_tree->oneshot_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_BLEND2) {
  890. anim_tree->blend2_node_set_filter_path(edited_node, ed->get_metadata(0), ed->is_checked(0));
  891. } else if (anim_tree->node_get_type(edited_node) == AnimationTreePlayer::NODE_ANIMATION) {
  892. anim_tree->animation_node_set_filter_path(edited_node, ed->get_metadata(0), ed->is_checked(0));
  893. }
  894. }
  895. void AnimationTreePlayerEditor::_edit_filters() {
  896. filter_dialog->popup_centered_ratio();
  897. filter->clear();
  898. Set<String> npb;
  899. _find_paths_for_filter(edited_node, npb);
  900. TreeItem *root = filter->create_item();
  901. filter->set_hide_root(true);
  902. Map<String, TreeItem *> pm;
  903. Node *base = anim_tree->get_node(anim_tree->get_base_path());
  904. for (Set<String>::Element *E = npb.front(); E; E = E->next()) {
  905. TreeItem *parent = root;
  906. String descr = E->get();
  907. if (base) {
  908. NodePath np = E->get();
  909. if (np.get_subname_count() == 1) {
  910. Node *n = base->get_node(np);
  911. Skeleton *s = Object::cast_to<Skeleton>(n);
  912. if (s) {
  913. String skelbase = E->get().substr(0, E->get().find(":"));
  914. int bidx = s->find_bone(np.get_subname(0));
  915. if (bidx != -1) {
  916. int bparent = s->get_bone_parent(bidx);
  917. //
  918. if (bparent != -1) {
  919. String bpn = skelbase + ":" + s->get_bone_name(bparent);
  920. if (pm.has(bpn)) {
  921. parent = pm[bpn];
  922. descr = np.get_subname(0);
  923. }
  924. } else {
  925. if (pm.has(skelbase)) {
  926. parent = pm[skelbase];
  927. }
  928. }
  929. }
  930. }
  931. }
  932. }
  933. TreeItem *it = filter->create_item(parent);
  934. it->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  935. it->set_text(0, descr);
  936. it->set_metadata(0, NodePath(E->get()));
  937. it->set_editable(0, true);
  938. if (anim_tree->node_get_type(edited_node) == AnimationTreePlayer::NODE_ONESHOT) {
  939. it->set_checked(0, anim_tree->oneshot_node_is_path_filtered(edited_node, E->get()));
  940. } else if (anim_tree->node_get_type(edited_node) == AnimationTreePlayer::NODE_BLEND2) {
  941. it->set_checked(0, anim_tree->blend2_node_is_path_filtered(edited_node, E->get()));
  942. } else if (anim_tree->node_get_type(edited_node) == AnimationTreePlayer::NODE_ANIMATION) {
  943. it->set_checked(0, anim_tree->animation_node_is_path_filtered(edited_node, E->get()));
  944. }
  945. pm[E->get()] = it;
  946. }
  947. }
  948. void AnimationTreePlayerEditor::_bind_methods() {
  949. ClassDB::bind_method("_add_menu_item", &AnimationTreePlayerEditor::_add_menu_item);
  950. ClassDB::bind_method("_node_menu_item", &AnimationTreePlayerEditor::_node_menu_item);
  951. ClassDB::bind_method("_gui_input", &AnimationTreePlayerEditor::_gui_input);
  952. //ClassDB::bind_method( "_node_param_changed", &AnimationTreeEditor::_node_param_changed );
  953. ClassDB::bind_method("_scroll_moved", &AnimationTreePlayerEditor::_scroll_moved);
  954. ClassDB::bind_method("_edit_dialog_changeds", &AnimationTreePlayerEditor::_edit_dialog_changeds);
  955. ClassDB::bind_method("_edit_dialog_changede", &AnimationTreePlayerEditor::_edit_dialog_changede);
  956. ClassDB::bind_method("_edit_dialog_changedf", &AnimationTreePlayerEditor::_edit_dialog_changedf);
  957. ClassDB::bind_method("_edit_dialog_changed", &AnimationTreePlayerEditor::_edit_dialog_changed);
  958. ClassDB::bind_method("_edit_dialog_animation_changed", &AnimationTreePlayerEditor::_edit_dialog_animation_changed);
  959. ClassDB::bind_method("_edit_dialog_edit_animation", &AnimationTreePlayerEditor::_edit_dialog_edit_animation);
  960. ClassDB::bind_method("_play_toggled", &AnimationTreePlayerEditor::_play_toggled);
  961. ClassDB::bind_method("_edit_oneshot_start", &AnimationTreePlayerEditor::_edit_oneshot_start);
  962. ClassDB::bind_method("_file_dialog_selected", &AnimationTreePlayerEditor::_file_dialog_selected);
  963. ClassDB::bind_method("_master_anim_menu_item", &AnimationTreePlayerEditor::_master_anim_menu_item);
  964. ClassDB::bind_method("_edit_filters", &AnimationTreePlayerEditor::_edit_filters);
  965. ClassDB::bind_method("_filter_edited", &AnimationTreePlayerEditor::_filter_edited);
  966. }
  967. AnimationTreePlayerEditor::AnimationTreePlayerEditor() {
  968. set_focus_mode(FOCUS_ALL);
  969. PopupMenu *p;
  970. List<PropertyInfo> defaults;
  971. add_menu = memnew(MenuButton);
  972. //add_menu->set_
  973. add_menu->set_position(Point2(0, 0));
  974. add_menu->set_size(Point2(25, 15));
  975. add_child(add_menu);
  976. p = add_menu->get_popup();
  977. p->add_item(TTR("Animation Node"), AnimationTreePlayer::NODE_ANIMATION);
  978. p->add_item(TTR("OneShot Node"), AnimationTreePlayer::NODE_ONESHOT);
  979. p->add_item(TTR("Mix Node"), AnimationTreePlayer::NODE_MIX);
  980. p->add_item(TTR("Blend2 Node"), AnimationTreePlayer::NODE_BLEND2);
  981. p->add_item(TTR("Blend3 Node"), AnimationTreePlayer::NODE_BLEND3);
  982. p->add_item(TTR("Blend4 Node"), AnimationTreePlayer::NODE_BLEND4);
  983. p->add_item(TTR("TimeScale Node"), AnimationTreePlayer::NODE_TIMESCALE);
  984. p->add_item(TTR("TimeSeek Node"), AnimationTreePlayer::NODE_TIMESEEK);
  985. p->add_item(TTR("Transition Node"), AnimationTreePlayer::NODE_TRANSITION);
  986. p->add_separator();
  987. p->add_item(TTR("Import Animations..."), MENU_IMPORT_ANIMATIONS); // wtf
  988. p->add_separator();
  989. p->add_item(TTR("Clear"), MENU_GRAPH_CLEAR);
  990. p->connect("id_pressed", this, "_add_menu_item");
  991. play_button = memnew(Button);
  992. play_button->set_position(Point2(25, 0));
  993. play_button->set_size(Point2(25, 15));
  994. add_child(play_button);
  995. play_button->set_toggle_mode(true);
  996. play_button->connect("pressed", this, "_play_toggled");
  997. last_x = 50;
  998. last_y = 50;
  999. property_editor = memnew(CustomPropertyEditor);
  1000. add_child(property_editor);
  1001. property_editor->connect("variant_changed", this, "_edit_dialog_animation_changed");
  1002. property_editor->connect("resource_edit_request", this, "_edit_dialog_edit_animation");
  1003. h_scroll = memnew(HScrollBar);
  1004. v_scroll = memnew(VScrollBar);
  1005. add_child(h_scroll);
  1006. add_child(v_scroll);
  1007. h_scroll->connect("value_changed", this, "_scroll_moved");
  1008. v_scroll->connect("value_changed", this, "_scroll_moved");
  1009. node_popup = memnew(PopupMenu);
  1010. add_child(node_popup);
  1011. node_popup->set_as_toplevel(true);
  1012. master_anim_popup = memnew(PopupMenu);
  1013. add_child(master_anim_popup);
  1014. master_anim_popup->connect("id_pressed", this, "_master_anim_menu_item");
  1015. node_popup->connect("id_pressed", this, "_node_menu_item");
  1016. updating_edit = false;
  1017. edit_dialog = memnew(PopupPanel);
  1018. //edit_dialog->get_ok()->hide();
  1019. //edit_dialog->get_cancel()->hide();
  1020. add_child(edit_dialog);
  1021. edit_option = memnew(OptionButton);
  1022. edit_option->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  1023. edit_option->set_margin(MARGIN_RIGHT, -10);
  1024. edit_dialog->add_child(edit_option);
  1025. edit_option->connect("item_selected", this, "_edit_dialog_changedf");
  1026. edit_option->hide();
  1027. for (int i = 0; i < 2; i++) {
  1028. edit_scroll[i] = memnew(HSlider);
  1029. edit_scroll[i]->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  1030. edit_scroll[i]->set_margin(MARGIN_RIGHT, -10);
  1031. edit_dialog->add_child(edit_scroll[i]);
  1032. edit_scroll[i]->hide();
  1033. edit_scroll[i]->connect("value_changed", this, "_edit_dialog_changedf");
  1034. }
  1035. for (int i = 0; i < 4; i++) {
  1036. edit_line[i] = memnew(LineEdit);
  1037. edit_line[i]->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  1038. edit_line[i]->set_margin(MARGIN_RIGHT, -10);
  1039. edit_dialog->add_child(edit_line[i]);
  1040. edit_line[i]->hide();
  1041. edit_line[i]->connect("text_changed", this, "_edit_dialog_changeds");
  1042. edit_line[i]->connect("text_entered", this, "_edit_dialog_changede");
  1043. edit_label[i] = memnew(Label);
  1044. edit_dialog->add_child(edit_label[i]);
  1045. edit_label[i]->hide();
  1046. }
  1047. edit_button = memnew(Button);
  1048. edit_button->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  1049. edit_button->set_margin(MARGIN_RIGHT, -10);
  1050. edit_dialog->add_child(edit_button);
  1051. edit_button->hide();
  1052. edit_button->connect("pressed", this, "_edit_oneshot_start");
  1053. edit_check = memnew(CheckButton);
  1054. edit_check->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  1055. edit_check->set_margin(MARGIN_RIGHT, -10);
  1056. edit_dialog->add_child(edit_check);
  1057. edit_check->hide();
  1058. edit_check->connect("pressed", this, "_edit_dialog_changed");
  1059. file_dialog = memnew(EditorFileDialog);
  1060. file_dialog->set_enable_multiple_selection(true);
  1061. file_dialog->set_current_dir(ProjectSettings::get_singleton()->get_resource_path());
  1062. add_child(file_dialog);
  1063. file_dialog->connect("file_selected", this, "_file_dialog_selected");
  1064. filter_dialog = memnew(AcceptDialog);
  1065. filter_dialog->set_title(TTR("Edit Node Filters"));
  1066. add_child(filter_dialog);
  1067. filter = memnew(Tree);
  1068. filter_dialog->add_child(filter);
  1069. //filter_dialog->set_child_rect(filter);
  1070. filter->connect("item_edited", this, "_filter_edited");
  1071. filter_button = memnew(Button);
  1072. filter_button->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  1073. filter_button->set_margin(MARGIN_RIGHT, -10);
  1074. edit_dialog->add_child(filter_button);
  1075. filter_button->hide();
  1076. filter_button->set_text(TTR("Filters..."));
  1077. filter_button->connect("pressed", this, "_edit_filters");
  1078. set_clip_contents(true);
  1079. }
  1080. void AnimationTreePlayerEditorPlugin::edit(Object *p_object) {
  1081. anim_tree_editor->edit(Object::cast_to<AnimationTreePlayer>(p_object));
  1082. }
  1083. bool AnimationTreePlayerEditorPlugin::handles(Object *p_object) const {
  1084. return p_object->is_class("AnimationTreePlayer");
  1085. }
  1086. void AnimationTreePlayerEditorPlugin::make_visible(bool p_visible) {
  1087. if (p_visible) {
  1088. //editor->hide_animation_player_editors();
  1089. //editor->animation_panel_make_visible(true);
  1090. button->show();
  1091. editor->make_bottom_panel_item_visible(anim_tree_editor);
  1092. anim_tree_editor->set_physics_process(true);
  1093. } else {
  1094. if (anim_tree_editor->is_visible_in_tree())
  1095. editor->hide_bottom_panel();
  1096. button->hide();
  1097. anim_tree_editor->set_physics_process(false);
  1098. }
  1099. }
  1100. AnimationTreePlayerEditorPlugin::AnimationTreePlayerEditorPlugin(EditorNode *p_node) {
  1101. editor = p_node;
  1102. anim_tree_editor = memnew(AnimationTreePlayerEditor);
  1103. anim_tree_editor->set_custom_minimum_size(Size2(0, 300));
  1104. button = editor->add_bottom_panel_item(TTR("AnimationTree"), anim_tree_editor);
  1105. button->hide();
  1106. }
  1107. AnimationTreePlayerEditorPlugin::~AnimationTreePlayerEditorPlugin() {
  1108. }