animation_blend_space_1d_editor.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. /*************************************************************************/
  2. /* animation_blend_space_1d_editor.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_blend_space_1d_editor.h"
  31. #include "core/os/keyboard.h"
  32. #include "scene/animation/animation_blend_tree.h"
  33. StringName AnimationNodeBlendSpace1DEditor::get_blend_position_path() const {
  34. StringName path = AnimationTreeEditor::get_singleton()->get_base_path() + "blend_position";
  35. return path;
  36. }
  37. void AnimationNodeBlendSpace1DEditor::_blend_space_gui_input(const Ref<InputEvent> &p_event) {
  38. Ref<InputEventKey> k = p_event;
  39. if (tool_select->is_pressed() && k.is_valid() && k->is_pressed() && k->get_scancode() == KEY_DELETE && !k->is_echo()) {
  40. if (selected_point != -1) {
  41. _erase_selected();
  42. accept_event();
  43. }
  44. }
  45. Ref<InputEventMouseButton> mb = p_event;
  46. if (mb.is_valid() && mb->is_pressed() && ((tool_select->is_pressed() && mb->get_button_index() == BUTTON_RIGHT) || (mb->get_button_index() == BUTTON_LEFT && tool_create->is_pressed()))) {
  47. menu->clear();
  48. animations_menu->clear();
  49. animations_to_add.clear();
  50. List<StringName> classes;
  51. ClassDB::get_inheriters_from_class("AnimationRootNode", &classes);
  52. classes.sort_custom<StringName::AlphCompare>();
  53. menu->add_submenu_item(TTR("Add Animation"), "animations");
  54. AnimationTree *gp = AnimationTreeEditor::get_singleton()->get_tree();
  55. ERR_FAIL_COND(!gp);
  56. if (gp->has_node(gp->get_animation_player())) {
  57. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(gp->get_node(gp->get_animation_player()));
  58. if (ap) {
  59. List<StringName> names;
  60. ap->get_animation_list(&names);
  61. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  62. animations_menu->add_icon_item(get_icon("Animation", "EditorIcons"), E->get());
  63. animations_to_add.push_back(E->get());
  64. }
  65. }
  66. }
  67. for (List<StringName>::Element *E = classes.front(); E; E = E->next()) {
  68. String name = String(E->get()).replace_first("AnimationNode", "");
  69. if (name == "Animation")
  70. continue;
  71. int idx = menu->get_item_count();
  72. menu->add_item(vformat("Add %s", name), idx);
  73. menu->set_item_metadata(idx, E->get());
  74. }
  75. Ref<AnimationNode> clipb = EditorSettings::get_singleton()->get_resource_clipboard();
  76. if (clipb.is_valid()) {
  77. menu->add_separator();
  78. menu->add_item(TTR("Paste"), MENU_PASTE);
  79. }
  80. menu->add_separator();
  81. menu->add_item(TTR("Load.."), MENU_LOAD_FILE);
  82. menu->set_global_position(blend_space_draw->get_global_transform().xform(mb->get_position()));
  83. menu->popup();
  84. add_point_pos = (mb->get_position() / blend_space_draw->get_size()).x;
  85. add_point_pos *= (blend_space->get_max_space() - blend_space->get_min_space());
  86. add_point_pos += blend_space->get_min_space();
  87. if (snap->is_pressed()) {
  88. add_point_pos = Math::stepify(add_point_pos, blend_space->get_snap());
  89. }
  90. }
  91. if (mb.is_valid() && mb->is_pressed() && tool_select->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  92. blend_space_draw->update(); // why not
  93. // try to see if a point can be selected
  94. selected_point = -1;
  95. _update_tool_erase();
  96. for (int i = 0; i < points.size(); i++) {
  97. if (Math::abs(float(points[i] - mb->get_position().x)) < 10 * EDSCALE) {
  98. selected_point = i;
  99. Ref<AnimationNode> node = blend_space->get_blend_point_node(i);
  100. EditorNode::get_singleton()->push_item(node.ptr(), "", true);
  101. dragging_selected_attempt = true;
  102. drag_from = mb->get_position();
  103. _update_tool_erase();
  104. _update_edited_point_pos();
  105. return;
  106. }
  107. }
  108. }
  109. if (mb.is_valid() && !mb->is_pressed() && dragging_selected_attempt && mb->get_button_index() == BUTTON_LEFT) {
  110. if (dragging_selected) {
  111. // move
  112. float point = blend_space->get_blend_point_position(selected_point);
  113. point += drag_ofs.x;
  114. if (snap->is_pressed()) {
  115. point = Math::stepify(point, blend_space->get_snap());
  116. }
  117. updating = true;
  118. undo_redo->create_action("Move Node Point");
  119. undo_redo->add_do_method(blend_space.ptr(), "set_blend_point_position", selected_point, point);
  120. undo_redo->add_undo_method(blend_space.ptr(), "set_blend_point_position", selected_point, blend_space->get_blend_point_position(selected_point));
  121. undo_redo->add_do_method(this, "_update_space");
  122. undo_redo->add_undo_method(this, "_update_space");
  123. undo_redo->add_do_method(this, "_update_edited_point_pos");
  124. undo_redo->add_undo_method(this, "_update_edited_point_pos");
  125. undo_redo->commit_action();
  126. updating = false;
  127. _update_edited_point_pos();
  128. }
  129. dragging_selected_attempt = false;
  130. dragging_selected = false;
  131. blend_space_draw->update();
  132. }
  133. // *set* the blend
  134. if (mb.is_valid() && !mb->is_pressed() && tool_blend->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  135. float blend_pos = mb->get_position().x / blend_space_draw->get_size().x;
  136. blend_pos *= blend_space->get_max_space() - blend_space->get_min_space();
  137. blend_pos += blend_space->get_min_space();
  138. AnimationTreeEditor::get_singleton()->get_tree()->set(get_blend_position_path(), blend_pos);
  139. blend_space_draw->update();
  140. }
  141. Ref<InputEventMouseMotion> mm = p_event;
  142. if (mm.is_valid() && !blend_space_draw->has_focus()) {
  143. blend_space_draw->grab_focus();
  144. blend_space_draw->update();
  145. }
  146. if (mm.is_valid() && dragging_selected_attempt) {
  147. dragging_selected = true;
  148. drag_ofs = ((mm->get_position() - drag_from) / blend_space_draw->get_size()) * ((blend_space->get_max_space() - blend_space->get_min_space()) * Vector2(1, 0));
  149. blend_space_draw->update();
  150. _update_edited_point_pos();
  151. }
  152. if (mm.is_valid() && tool_blend->is_pressed() && mm->get_button_mask() & BUTTON_MASK_LEFT) {
  153. float blend_pos = mm->get_position().x / blend_space_draw->get_size().x;
  154. blend_pos *= blend_space->get_max_space() - blend_space->get_min_space();
  155. blend_pos += blend_space->get_min_space();
  156. AnimationTreeEditor::get_singleton()->get_tree()->set(get_blend_position_path(), blend_pos);
  157. blend_space_draw->update();
  158. }
  159. }
  160. void AnimationNodeBlendSpace1DEditor::_blend_space_draw() {
  161. Color linecolor = get_color("font_color", "Label");
  162. Color linecolor_soft = linecolor;
  163. linecolor_soft.a *= 0.5;
  164. Ref<Font> font = get_font("font", "Label");
  165. Ref<Texture> icon = get_icon("KeyValue", "EditorIcons");
  166. Ref<Texture> icon_selected = get_icon("KeySelected", "EditorIcons");
  167. Size2 s = blend_space_draw->get_size();
  168. if (blend_space_draw->has_focus()) {
  169. Color color = get_color("accent_color", "Editor");
  170. blend_space_draw->draw_rect(Rect2(Point2(), s), color, false);
  171. }
  172. blend_space_draw->draw_line(Point2(1, s.height - 1), Point2(s.width - 1, s.height - 1), linecolor);
  173. if (blend_space->get_min_space() < 0) {
  174. float point = 0.0;
  175. point = (point - blend_space->get_min_space()) / (blend_space->get_max_space() - blend_space->get_min_space());
  176. point *= s.width;
  177. float x = point;
  178. blend_space_draw->draw_line(Point2(x, s.height - 1), Point2(x, s.height - 5 * EDSCALE), linecolor);
  179. blend_space_draw->draw_string(font, Point2(x + 2 * EDSCALE, s.height - 2 * EDSCALE - font->get_height() + font->get_ascent()), "0", linecolor);
  180. blend_space_draw->draw_line(Point2(x, s.height - 5 * EDSCALE), Point2(x, 0), linecolor_soft);
  181. }
  182. if (snap->is_pressed()) {
  183. linecolor_soft.a = linecolor.a * 0.1;
  184. if (blend_space->get_snap() > 0) {
  185. int prev_idx = -1;
  186. for (int i = 0; i < s.x; i++) {
  187. float v = blend_space->get_min_space() + i * (blend_space->get_max_space() - blend_space->get_min_space()) / s.x;
  188. int idx = int(v / blend_space->get_snap());
  189. if (i > 0 && prev_idx != idx) {
  190. blend_space_draw->draw_line(Point2(i, 0), Point2(i, s.height), linecolor_soft);
  191. }
  192. prev_idx = idx;
  193. }
  194. }
  195. }
  196. points.clear();
  197. for (int i = 0; i < blend_space->get_blend_point_count(); i++) {
  198. float point = blend_space->get_blend_point_position(i);
  199. if (dragging_selected && selected_point == i) {
  200. point += drag_ofs.x;
  201. if (snap->is_pressed()) {
  202. point = Math::stepify(point, blend_space->get_snap());
  203. }
  204. }
  205. point = (point - blend_space->get_min_space()) / (blend_space->get_max_space() - blend_space->get_min_space());
  206. point *= s.width;
  207. points.push_back(point);
  208. Vector2 gui_point = Vector2(point, s.height / 2.0);
  209. gui_point -= (icon->get_size() / 2.0);
  210. gui_point = gui_point.floor();
  211. if (i == selected_point) {
  212. blend_space_draw->draw_texture(icon_selected, gui_point);
  213. } else {
  214. blend_space_draw->draw_texture(icon, gui_point);
  215. }
  216. }
  217. // blend position
  218. {
  219. Color color;
  220. if (tool_blend->is_pressed()) {
  221. color = get_color("accent_color", "Editor");
  222. } else {
  223. color = linecolor;
  224. color.a *= 0.5;
  225. }
  226. float point = AnimationTreeEditor::get_singleton()->get_tree()->get(get_blend_position_path());
  227. point = (point - blend_space->get_min_space()) / (blend_space->get_max_space() - blend_space->get_min_space());
  228. point *= s.width;
  229. Vector2 gui_point = Vector2(point, s.height / 2.0);
  230. float mind = 5 * EDSCALE;
  231. float maxd = 15 * EDSCALE;
  232. blend_space_draw->draw_line(gui_point + Vector2(mind, 0), gui_point + Vector2(maxd, 0), color, 2);
  233. blend_space_draw->draw_line(gui_point + Vector2(-mind, 0), gui_point + Vector2(-maxd, 0), color, 2);
  234. blend_space_draw->draw_line(gui_point + Vector2(0, mind), gui_point + Vector2(0, maxd), color, 2);
  235. blend_space_draw->draw_line(gui_point + Vector2(0, -mind), gui_point + Vector2(0, -maxd), color, 2);
  236. }
  237. }
  238. void AnimationNodeBlendSpace1DEditor::_update_space() {
  239. if (updating)
  240. return;
  241. updating = true;
  242. max_value->set_value(blend_space->get_max_space());
  243. min_value->set_value(blend_space->get_min_space());
  244. label_value->set_text(blend_space->get_value_label());
  245. snap_value->set_value(blend_space->get_snap());
  246. blend_space_draw->update();
  247. updating = false;
  248. }
  249. void AnimationNodeBlendSpace1DEditor::_config_changed(double) {
  250. if (updating)
  251. return;
  252. updating = true;
  253. undo_redo->create_action("Change BlendSpace1D Limits");
  254. undo_redo->add_do_method(blend_space.ptr(), "set_max_space", max_value->get_value());
  255. undo_redo->add_undo_method(blend_space.ptr(), "set_max_space", blend_space->get_max_space());
  256. undo_redo->add_do_method(blend_space.ptr(), "set_min_space", min_value->get_value());
  257. undo_redo->add_undo_method(blend_space.ptr(), "set_min_space", blend_space->get_min_space());
  258. undo_redo->add_do_method(blend_space.ptr(), "set_snap", snap_value->get_value());
  259. undo_redo->add_undo_method(blend_space.ptr(), "set_snap", blend_space->get_snap());
  260. undo_redo->add_do_method(this, "_update_space");
  261. undo_redo->add_undo_method(this, "_update_space");
  262. undo_redo->commit_action();
  263. updating = false;
  264. blend_space_draw->update();
  265. }
  266. void AnimationNodeBlendSpace1DEditor::_labels_changed(String) {
  267. if (updating)
  268. return;
  269. updating = true;
  270. undo_redo->create_action("Change BlendSpace1D Labels", UndoRedo::MERGE_ENDS);
  271. undo_redo->add_do_method(blend_space.ptr(), "set_value_label", label_value->get_text());
  272. undo_redo->add_undo_method(blend_space.ptr(), "set_value_label", blend_space->get_value_label());
  273. undo_redo->add_do_method(this, "_update_space");
  274. undo_redo->add_undo_method(this, "_update_space");
  275. undo_redo->commit_action();
  276. updating = false;
  277. }
  278. void AnimationNodeBlendSpace1DEditor::_snap_toggled() {
  279. blend_space_draw->update();
  280. }
  281. void AnimationNodeBlendSpace1DEditor::_file_opened(const String &p_file) {
  282. file_loaded = ResourceLoader::load(p_file);
  283. if (file_loaded.is_valid()) {
  284. _add_menu_type(MENU_LOAD_FILE_CONFIRM);
  285. }
  286. }
  287. void AnimationNodeBlendSpace1DEditor::_add_menu_type(int p_index) {
  288. Ref<AnimationRootNode> node;
  289. if (p_index == MENU_LOAD_FILE) {
  290. open_file->clear_filters();
  291. List<String> filters;
  292. ResourceLoader::get_recognized_extensions_for_type("AnimationRootNode", &filters);
  293. for (List<String>::Element *E = filters.front(); E; E = E->next()) {
  294. open_file->add_filter("*." + E->get());
  295. }
  296. open_file->popup_centered_ratio();
  297. return;
  298. } else if (p_index == MENU_LOAD_FILE_CONFIRM) {
  299. node = file_loaded;
  300. file_loaded.unref();
  301. } else if (p_index == MENU_PASTE) {
  302. node = EditorSettings::get_singleton()->get_resource_clipboard();
  303. } else {
  304. String type = menu->get_item_metadata(p_index);
  305. Object *obj = ClassDB::instance(type);
  306. ERR_FAIL_COND(!obj);
  307. AnimationNode *an = Object::cast_to<AnimationNode>(obj);
  308. ERR_FAIL_COND(!an);
  309. node = Ref<AnimationNode>(an);
  310. }
  311. if (!node.is_valid()) {
  312. EditorNode::get_singleton()->show_warning(TTR("This type of node can't be used. Only root nodes are allowed."));
  313. return;
  314. }
  315. updating = true;
  316. undo_redo->create_action("Add Node Point");
  317. undo_redo->add_do_method(blend_space.ptr(), "add_blend_point", node, add_point_pos);
  318. undo_redo->add_undo_method(blend_space.ptr(), "remove_blend_point", blend_space->get_blend_point_count());
  319. undo_redo->add_do_method(this, "_update_space");
  320. undo_redo->add_undo_method(this, "_update_space");
  321. undo_redo->commit_action();
  322. updating = false;
  323. blend_space_draw->update();
  324. }
  325. void AnimationNodeBlendSpace1DEditor::_add_animation_type(int p_index) {
  326. Ref<AnimationNodeAnimation> anim;
  327. anim.instance();
  328. anim->set_animation(animations_to_add[p_index]);
  329. updating = true;
  330. undo_redo->create_action("Add Animation Point");
  331. undo_redo->add_do_method(blend_space.ptr(), "add_blend_point", anim, add_point_pos);
  332. undo_redo->add_undo_method(blend_space.ptr(), "remove_blend_point", blend_space->get_blend_point_count());
  333. undo_redo->add_do_method(this, "_update_space");
  334. undo_redo->add_undo_method(this, "_update_space");
  335. undo_redo->commit_action();
  336. updating = false;
  337. blend_space_draw->update();
  338. }
  339. void AnimationNodeBlendSpace1DEditor::_tool_switch(int p_tool) {
  340. if (p_tool == 0) {
  341. tool_erase->show();
  342. tool_erase_sep->show();
  343. } else {
  344. tool_erase->hide();
  345. tool_erase_sep->hide();
  346. }
  347. _update_tool_erase();
  348. blend_space_draw->update();
  349. }
  350. void AnimationNodeBlendSpace1DEditor::_update_edited_point_pos() {
  351. if (updating)
  352. return;
  353. if (selected_point >= 0 && selected_point < blend_space->get_blend_point_count()) {
  354. float pos = blend_space->get_blend_point_position(selected_point);
  355. if (dragging_selected) {
  356. pos += drag_ofs.x;
  357. if (snap->is_pressed()) {
  358. pos = Math::stepify(pos, blend_space->get_snap());
  359. }
  360. }
  361. updating = true;
  362. edit_value->set_value(pos);
  363. updating = false;
  364. }
  365. }
  366. void AnimationNodeBlendSpace1DEditor::_update_tool_erase() {
  367. bool point_valid = selected_point >= 0 && selected_point < blend_space->get_blend_point_count();
  368. tool_erase->set_disabled(!point_valid);
  369. if (point_valid) {
  370. Ref<AnimationNode> an = blend_space->get_blend_point_node(selected_point);
  371. if (AnimationTreeEditor::get_singleton()->can_edit(an)) {
  372. open_editor->show();
  373. } else {
  374. open_editor->hide();
  375. }
  376. edit_hb->show();
  377. } else {
  378. edit_hb->hide();
  379. }
  380. }
  381. void AnimationNodeBlendSpace1DEditor::_erase_selected() {
  382. if (selected_point != -1) {
  383. updating = true;
  384. undo_redo->create_action("Remove BlendSpace1D Point");
  385. undo_redo->add_do_method(blend_space.ptr(), "remove_blend_point", selected_point);
  386. undo_redo->add_undo_method(blend_space.ptr(), "add_blend_point", blend_space->get_blend_point_node(selected_point), blend_space->get_blend_point_position(selected_point), selected_point);
  387. undo_redo->add_do_method(this, "_update_space");
  388. undo_redo->add_undo_method(this, "_update_space");
  389. undo_redo->commit_action();
  390. updating = false;
  391. blend_space_draw->update();
  392. }
  393. }
  394. void AnimationNodeBlendSpace1DEditor::_edit_point_pos(double) {
  395. if (updating)
  396. return;
  397. updating = true;
  398. undo_redo->create_action("Move BlendSpace1D Node Point");
  399. undo_redo->add_do_method(blend_space.ptr(), "set_blend_point_position", selected_point, edit_value->get_value());
  400. undo_redo->add_undo_method(blend_space.ptr(), "set_blend_point_position", selected_point, blend_space->get_blend_point_position(selected_point));
  401. undo_redo->add_do_method(this, "_update_space");
  402. undo_redo->add_undo_method(this, "_update_space");
  403. undo_redo->add_do_method(this, "_update_edited_point_pos");
  404. undo_redo->add_undo_method(this, "_update_edited_point_pos");
  405. undo_redo->commit_action();
  406. updating = false;
  407. blend_space_draw->update();
  408. }
  409. void AnimationNodeBlendSpace1DEditor::_open_editor() {
  410. if (selected_point >= 0 && selected_point < blend_space->get_blend_point_count()) {
  411. Ref<AnimationNode> an = blend_space->get_blend_point_node(selected_point);
  412. ERR_FAIL_COND(an.is_null());
  413. AnimationTreeEditor::get_singleton()->enter_editor(itos(selected_point));
  414. }
  415. }
  416. void AnimationNodeBlendSpace1DEditor::_notification(int p_what) {
  417. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  418. error_panel->add_style_override("panel", get_stylebox("bg", "Tree"));
  419. error_label->add_color_override("font_color", get_color("error_color", "Editor"));
  420. panel->add_style_override("panel", get_stylebox("bg", "Tree"));
  421. tool_blend->set_icon(get_icon("EditPivot", "EditorIcons"));
  422. tool_select->set_icon(get_icon("ToolSelect", "EditorIcons"));
  423. tool_create->set_icon(get_icon("EditKey", "EditorIcons"));
  424. tool_erase->set_icon(get_icon("Remove", "EditorIcons"));
  425. snap->set_icon(get_icon("SnapGrid", "EditorIcons"));
  426. open_editor->set_icon(get_icon("Edit", "EditorIcons"));
  427. }
  428. if (p_what == NOTIFICATION_PROCESS) {
  429. String error;
  430. if (!AnimationTreeEditor::get_singleton()->get_tree()->is_active()) {
  431. error = TTR("AnimationTree is inactive.\nActivate to enable playback, check node warnings if activation fails.");
  432. } else if (AnimationTreeEditor::get_singleton()->get_tree()->is_state_invalid()) {
  433. error = AnimationTreeEditor::get_singleton()->get_tree()->get_invalid_state_reason();
  434. }
  435. if (error != error_label->get_text()) {
  436. error_label->set_text(error);
  437. if (error != String()) {
  438. error_panel->show();
  439. } else {
  440. error_panel->hide();
  441. }
  442. }
  443. }
  444. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  445. set_process(is_visible_in_tree());
  446. }
  447. }
  448. void AnimationNodeBlendSpace1DEditor::_bind_methods() {
  449. ClassDB::bind_method("_blend_space_gui_input", &AnimationNodeBlendSpace1DEditor::_blend_space_gui_input);
  450. ClassDB::bind_method("_blend_space_draw", &AnimationNodeBlendSpace1DEditor::_blend_space_draw);
  451. ClassDB::bind_method("_config_changed", &AnimationNodeBlendSpace1DEditor::_config_changed);
  452. ClassDB::bind_method("_labels_changed", &AnimationNodeBlendSpace1DEditor::_labels_changed);
  453. ClassDB::bind_method("_update_space", &AnimationNodeBlendSpace1DEditor::_update_space);
  454. ClassDB::bind_method("_snap_toggled", &AnimationNodeBlendSpace1DEditor::_snap_toggled);
  455. ClassDB::bind_method("_tool_switch", &AnimationNodeBlendSpace1DEditor::_tool_switch);
  456. ClassDB::bind_method("_erase_selected", &AnimationNodeBlendSpace1DEditor::_erase_selected);
  457. ClassDB::bind_method("_update_tool_erase", &AnimationNodeBlendSpace1DEditor::_update_tool_erase);
  458. ClassDB::bind_method("_edit_point_pos", &AnimationNodeBlendSpace1DEditor::_edit_point_pos);
  459. ClassDB::bind_method("_add_menu_type", &AnimationNodeBlendSpace1DEditor::_add_menu_type);
  460. ClassDB::bind_method("_add_animation_type", &AnimationNodeBlendSpace1DEditor::_add_animation_type);
  461. ClassDB::bind_method("_update_edited_point_pos", &AnimationNodeBlendSpace1DEditor::_update_edited_point_pos);
  462. ClassDB::bind_method("_open_editor", &AnimationNodeBlendSpace1DEditor::_open_editor);
  463. ClassDB::bind_method("_file_opened", &AnimationNodeBlendSpace1DEditor::_file_opened);
  464. }
  465. bool AnimationNodeBlendSpace1DEditor::can_edit(const Ref<AnimationNode> &p_node) {
  466. Ref<AnimationNodeBlendSpace1D> b1d = p_node;
  467. return b1d.is_valid();
  468. }
  469. void AnimationNodeBlendSpace1DEditor::edit(const Ref<AnimationNode> &p_node) {
  470. blend_space = p_node;
  471. if (!blend_space.is_null()) {
  472. _update_space();
  473. }
  474. }
  475. AnimationNodeBlendSpace1DEditor *AnimationNodeBlendSpace1DEditor::singleton = NULL;
  476. AnimationNodeBlendSpace1DEditor::AnimationNodeBlendSpace1DEditor() {
  477. singleton = this;
  478. updating = false;
  479. HBoxContainer *top_hb = memnew(HBoxContainer);
  480. add_child(top_hb);
  481. Ref<ButtonGroup> bg;
  482. bg.instance();
  483. tool_blend = memnew(ToolButton);
  484. tool_blend->set_toggle_mode(true);
  485. tool_blend->set_button_group(bg);
  486. top_hb->add_child(tool_blend);
  487. tool_blend->set_pressed(true);
  488. tool_blend->set_tooltip(TTR("Set the blending position within the space"));
  489. tool_blend->connect("pressed", this, "_tool_switch", varray(3));
  490. tool_select = memnew(ToolButton);
  491. tool_select->set_toggle_mode(true);
  492. tool_select->set_button_group(bg);
  493. top_hb->add_child(tool_select);
  494. tool_select->set_tooltip(TTR("Select and move points, create points with RMB."));
  495. tool_select->connect("pressed", this, "_tool_switch", varray(0));
  496. tool_create = memnew(ToolButton);
  497. tool_create->set_toggle_mode(true);
  498. tool_create->set_button_group(bg);
  499. top_hb->add_child(tool_create);
  500. tool_create->set_tooltip(TTR("Create points."));
  501. tool_create->connect("pressed", this, "_tool_switch", varray(1));
  502. tool_erase_sep = memnew(VSeparator);
  503. top_hb->add_child(tool_erase_sep);
  504. tool_erase = memnew(ToolButton);
  505. top_hb->add_child(tool_erase);
  506. tool_erase->set_tooltip(TTR("Erase points."));
  507. tool_erase->connect("pressed", this, "_erase_selected");
  508. top_hb->add_child(memnew(VSeparator));
  509. snap = memnew(ToolButton);
  510. snap->set_toggle_mode(true);
  511. top_hb->add_child(snap);
  512. snap->set_pressed(true);
  513. snap->set_tooltip(TTR("Enable snap and show grid."));
  514. snap->connect("pressed", this, "_snap_toggled");
  515. snap_value = memnew(SpinBox);
  516. top_hb->add_child(snap_value);
  517. snap_value->set_min(0.01);
  518. snap_value->set_step(0.01);
  519. snap_value->set_max(1000);
  520. edit_hb = memnew(HBoxContainer);
  521. top_hb->add_child(edit_hb);
  522. edit_hb->add_child(memnew(VSeparator));
  523. edit_hb->add_child(memnew(Label(TTR("Point"))));
  524. edit_value = memnew(SpinBox);
  525. edit_hb->add_child(edit_value);
  526. edit_value->set_min(-1000);
  527. edit_value->set_max(1000);
  528. edit_value->set_step(0.01);
  529. edit_value->connect("value_changed", this, "_edit_point_pos");
  530. open_editor = memnew(Button);
  531. edit_hb->add_child(open_editor);
  532. open_editor->set_text(TTR("Open Editor"));
  533. open_editor->connect("pressed", this, "_open_editor", varray(), CONNECT_DEFERRED);
  534. edit_hb->hide();
  535. open_editor->hide();
  536. VBoxContainer *main_vb = memnew(VBoxContainer);
  537. add_child(main_vb);
  538. main_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  539. panel = memnew(PanelContainer);
  540. panel->set_clip_contents(true);
  541. main_vb->add_child(panel);
  542. panel->set_h_size_flags(SIZE_EXPAND_FILL);
  543. panel->set_v_size_flags(SIZE_EXPAND_FILL);
  544. blend_space_draw = memnew(Control);
  545. blend_space_draw->connect("gui_input", this, "_blend_space_gui_input");
  546. blend_space_draw->connect("draw", this, "_blend_space_draw");
  547. blend_space_draw->set_focus_mode(FOCUS_ALL);
  548. panel->add_child(blend_space_draw);
  549. {
  550. HBoxContainer *bottom_hb = memnew(HBoxContainer);
  551. main_vb->add_child(bottom_hb);
  552. bottom_hb->set_h_size_flags(SIZE_EXPAND_FILL);
  553. min_value = memnew(SpinBox);
  554. min_value->set_max(0);
  555. min_value->set_min(-10000);
  556. min_value->set_step(0.01);
  557. max_value = memnew(SpinBox);
  558. max_value->set_max(10000);
  559. max_value->set_min(0.01);
  560. max_value->set_step(0.01);
  561. label_value = memnew(LineEdit);
  562. label_value->set_expand_to_text_length(true);
  563. // now add
  564. bottom_hb->add_child(min_value);
  565. bottom_hb->add_spacer();
  566. bottom_hb->add_child(label_value);
  567. bottom_hb->add_spacer();
  568. bottom_hb->add_child(max_value);
  569. }
  570. snap_value->connect("value_changed", this, "_config_changed");
  571. min_value->connect("value_changed", this, "_config_changed");
  572. max_value->connect("value_changed", this, "_config_changed");
  573. label_value->connect("text_changed", this, "_labels_changed");
  574. error_panel = memnew(PanelContainer);
  575. add_child(error_panel);
  576. error_label = memnew(Label);
  577. error_panel->add_child(error_label);
  578. error_label->set_text("hmmm");
  579. undo_redo = EditorNode::get_singleton()->get_undo_redo();
  580. menu = memnew(PopupMenu);
  581. add_child(menu);
  582. menu->connect("id_pressed", this, "_add_menu_type");
  583. animations_menu = memnew(PopupMenu);
  584. menu->add_child(animations_menu);
  585. animations_menu->set_name("animations");
  586. animations_menu->connect("index_pressed", this, "_add_animation_type");
  587. open_file = memnew(EditorFileDialog);
  588. add_child(open_file);
  589. open_file->set_title(TTR("Open Animation Node"));
  590. open_file->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  591. open_file->connect("file_selected", this, "_file_opened");
  592. undo_redo = EditorNode::get_singleton()->get_undo_redo();
  593. selected_point = -1;
  594. dragging_selected = false;
  595. dragging_selected_attempt = false;
  596. set_custom_minimum_size(Size2(0, 150 * EDSCALE));
  597. }