animation_tree_player_editor_plugin.cpp 43 KB

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