animation_blend_space_1d_editor.cpp 26 KB

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