animation_blend_space_2d_editor.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. /*************************************************************************/
  2. /* animation_blend_space_2d_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_2d_editor.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/input/input.h"
  33. #include "core/io/resource_loader.h"
  34. #include "core/math/geometry_2d.h"
  35. #include "core/os/keyboard.h"
  36. #include "editor/editor_file_dialog.h"
  37. #include "editor/editor_node.h"
  38. #include "editor/editor_scale.h"
  39. #include "editor/editor_settings.h"
  40. #include "editor/editor_undo_redo_manager.h"
  41. #include "scene/animation/animation_blend_tree.h"
  42. #include "scene/animation/animation_player.h"
  43. #include "scene/gui/menu_button.h"
  44. #include "scene/gui/panel.h"
  45. #include "scene/main/window.h"
  46. bool AnimationNodeBlendSpace2DEditor::can_edit(const Ref<AnimationNode> &p_node) {
  47. Ref<AnimationNodeBlendSpace2D> bs2d = p_node;
  48. return bs2d.is_valid();
  49. }
  50. void AnimationNodeBlendSpace2DEditor::_blend_space_changed() {
  51. blend_space_draw->queue_redraw();
  52. }
  53. void AnimationNodeBlendSpace2DEditor::edit(const Ref<AnimationNode> &p_node) {
  54. if (blend_space.is_valid()) {
  55. blend_space->disconnect("triangles_updated", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_blend_space_changed));
  56. }
  57. blend_space = p_node;
  58. read_only = false;
  59. if (!blend_space.is_null()) {
  60. read_only = EditorNode::get_singleton()->is_resource_read_only(blend_space);
  61. blend_space->connect("triangles_updated", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_blend_space_changed));
  62. _update_space();
  63. }
  64. tool_create->set_disabled(read_only);
  65. interpolation->set_disabled(read_only);
  66. max_x_value->set_editable(!read_only);
  67. min_x_value->set_editable(!read_only);
  68. max_y_value->set_editable(!read_only);
  69. min_y_value->set_editable(!read_only);
  70. label_x->set_editable(!read_only);
  71. label_y->set_editable(!read_only);
  72. edit_x->set_editable(!read_only);
  73. edit_y->set_editable(!read_only);
  74. tool_triangle->set_disabled(read_only);
  75. auto_triangles->set_disabled(read_only);
  76. sync->set_disabled(read_only);
  77. interpolation->set_disabled(read_only);
  78. }
  79. StringName AnimationNodeBlendSpace2DEditor::get_blend_position_path() const {
  80. StringName path = AnimationTreeEditor::get_singleton()->get_base_path() + "blend_position";
  81. return path;
  82. }
  83. void AnimationNodeBlendSpace2DEditor::_blend_space_gui_input(const Ref<InputEvent> &p_event) {
  84. Ref<InputEventKey> k = p_event;
  85. if (tool_select->is_pressed() && k.is_valid() && k->is_pressed() && k->get_keycode() == Key::KEY_DELETE && !k->is_echo()) {
  86. if (selected_point != -1 || selected_triangle != -1) {
  87. if (!read_only) {
  88. _erase_selected();
  89. }
  90. accept_event();
  91. }
  92. }
  93. Ref<InputEventMouseButton> mb = p_event;
  94. if (mb.is_valid() && mb->is_pressed() && ((tool_select->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) || (mb->get_button_index() == MouseButton::LEFT && tool_create->is_pressed()))) {
  95. if (!read_only) {
  96. menu->clear();
  97. animations_menu->clear();
  98. animations_to_add.clear();
  99. List<StringName> classes;
  100. classes.sort_custom<StringName::AlphCompare>();
  101. ClassDB::get_inheriters_from_class("AnimationRootNode", &classes);
  102. menu->add_submenu_item(TTR("Add Animation"), "animations");
  103. AnimationTree *gp = AnimationTreeEditor::get_singleton()->get_tree();
  104. ERR_FAIL_COND(!gp);
  105. if (gp && gp->has_node(gp->get_animation_player())) {
  106. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(gp->get_node(gp->get_animation_player()));
  107. if (ap) {
  108. List<StringName> names;
  109. ap->get_animation_list(&names);
  110. for (const StringName &E : names) {
  111. animations_menu->add_icon_item(get_theme_icon(SNAME("Animation"), SNAME("EditorIcons")), E);
  112. animations_to_add.push_back(E);
  113. }
  114. }
  115. }
  116. for (const StringName &E : classes) {
  117. String name = String(E).replace_first("AnimationNode", "");
  118. if (name == "Animation" || name == "StartState" || name == "EndState") {
  119. continue; // nope
  120. }
  121. int idx = menu->get_item_count();
  122. menu->add_item(vformat(TTR("Add %s"), name), idx);
  123. menu->set_item_metadata(idx, E);
  124. }
  125. Ref<AnimationNode> clipb = EditorSettings::get_singleton()->get_resource_clipboard();
  126. if (clipb.is_valid()) {
  127. menu->add_separator();
  128. menu->add_item(TTR("Paste"), MENU_PASTE);
  129. }
  130. menu->add_separator();
  131. menu->add_item(TTR("Load..."), MENU_LOAD_FILE);
  132. menu->set_position(blend_space_draw->get_screen_position() + mb->get_position());
  133. menu->reset_size();
  134. menu->popup();
  135. add_point_pos = (mb->get_position() / blend_space_draw->get_size());
  136. add_point_pos.y = 1.0 - add_point_pos.y;
  137. add_point_pos *= (blend_space->get_max_space() - blend_space->get_min_space());
  138. add_point_pos += blend_space->get_min_space();
  139. if (snap->is_pressed()) {
  140. add_point_pos = add_point_pos.snapped(blend_space->get_snap());
  141. }
  142. }
  143. }
  144. if (mb.is_valid() && mb->is_pressed() && tool_select->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  145. blend_space_draw->queue_redraw(); //update anyway
  146. //try to see if a point can be selected
  147. selected_point = -1;
  148. selected_triangle = -1;
  149. _update_tool_erase();
  150. for (int i = 0; i < points.size(); i++) {
  151. if (points[i].distance_to(mb->get_position()) < 10 * EDSCALE) {
  152. selected_point = i;
  153. Ref<AnimationNode> node = blend_space->get_blend_point_node(i);
  154. EditorNode::get_singleton()->push_item(node.ptr(), "", true);
  155. dragging_selected_attempt = true;
  156. drag_from = mb->get_position();
  157. _update_tool_erase();
  158. _update_edited_point_pos();
  159. return;
  160. }
  161. }
  162. //then try to see if a triangle can be selected
  163. if (!blend_space->get_auto_triangles()) { //if autotriangles use, disable this
  164. for (int i = 0; i < blend_space->get_triangle_count(); i++) {
  165. Vector<Vector2> triangle;
  166. for (int j = 0; j < 3; j++) {
  167. int idx = blend_space->get_triangle_point(i, j);
  168. ERR_FAIL_INDEX(idx, points.size());
  169. triangle.push_back(points[idx]);
  170. }
  171. if (Geometry2D::is_point_in_triangle(mb->get_position(), triangle[0], triangle[1], triangle[2])) {
  172. selected_triangle = i;
  173. _update_tool_erase();
  174. return;
  175. }
  176. }
  177. }
  178. }
  179. if (mb.is_valid() && mb->is_pressed() && tool_triangle->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  180. blend_space_draw->queue_redraw(); //update anyway
  181. //try to see if a point can be selected
  182. selected_point = -1;
  183. for (int i = 0; i < points.size(); i++) {
  184. if (making_triangle.has(i)) {
  185. continue;
  186. }
  187. if (points[i].distance_to(mb->get_position()) < 10 * EDSCALE) {
  188. making_triangle.push_back(i);
  189. if (making_triangle.size() == 3) {
  190. //add triangle!
  191. if (blend_space->has_triangle(making_triangle[0], making_triangle[1], making_triangle[2])) {
  192. making_triangle.clear();
  193. EditorNode::get_singleton()->show_warning(TTR("Triangle already exists."));
  194. return;
  195. }
  196. updating = true;
  197. undo_redo->create_action(TTR("Add Triangle"));
  198. undo_redo->add_do_method(blend_space.ptr(), "add_triangle", making_triangle[0], making_triangle[1], making_triangle[2]);
  199. undo_redo->add_undo_method(blend_space.ptr(), "remove_triangle", blend_space->get_triangle_count());
  200. undo_redo->add_do_method(this, "_update_space");
  201. undo_redo->add_undo_method(this, "_update_space");
  202. undo_redo->commit_action();
  203. updating = false;
  204. making_triangle.clear();
  205. }
  206. return;
  207. }
  208. }
  209. }
  210. if (mb.is_valid() && !mb->is_pressed() && dragging_selected_attempt && mb->get_button_index() == MouseButton::LEFT) {
  211. if (dragging_selected) {
  212. //move
  213. Vector2 point = blend_space->get_blend_point_position(selected_point);
  214. point += drag_ofs;
  215. if (snap->is_pressed()) {
  216. point = point.snapped(blend_space->get_snap());
  217. }
  218. if (!read_only) {
  219. updating = true;
  220. undo_redo->create_action(TTR("Move Node Point"));
  221. undo_redo->add_do_method(blend_space.ptr(), "set_blend_point_position", selected_point, point);
  222. undo_redo->add_undo_method(blend_space.ptr(), "set_blend_point_position", selected_point, blend_space->get_blend_point_position(selected_point));
  223. undo_redo->add_do_method(this, "_update_space");
  224. undo_redo->add_undo_method(this, "_update_space");
  225. undo_redo->add_do_method(this, "_update_edited_point_pos");
  226. undo_redo->add_undo_method(this, "_update_edited_point_pos");
  227. undo_redo->commit_action();
  228. updating = false;
  229. _update_edited_point_pos();
  230. }
  231. }
  232. dragging_selected_attempt = false;
  233. dragging_selected = false;
  234. blend_space_draw->queue_redraw();
  235. }
  236. if (mb.is_valid() && mb->is_pressed() && tool_blend->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  237. Vector2 blend_pos = (mb->get_position() / blend_space_draw->get_size());
  238. blend_pos.y = 1.0 - blend_pos.y;
  239. blend_pos *= (blend_space->get_max_space() - blend_space->get_min_space());
  240. blend_pos += blend_space->get_min_space();
  241. AnimationTreeEditor::get_singleton()->get_tree()->set(get_blend_position_path(), blend_pos);
  242. blend_space_draw->queue_redraw();
  243. }
  244. Ref<InputEventMouseMotion> mm = p_event;
  245. if (mm.is_valid() && !blend_space_draw->has_focus()) {
  246. blend_space_draw->grab_focus();
  247. blend_space_draw->queue_redraw();
  248. }
  249. if (mm.is_valid() && dragging_selected_attempt) {
  250. dragging_selected = true;
  251. if (!read_only) {
  252. drag_ofs = ((mm->get_position() - drag_from) / blend_space_draw->get_size()) * (blend_space->get_max_space() - blend_space->get_min_space()) * Vector2(1, -1);
  253. }
  254. blend_space_draw->queue_redraw();
  255. _update_edited_point_pos();
  256. }
  257. if (mm.is_valid() && tool_triangle->is_pressed() && making_triangle.size()) {
  258. blend_space_draw->queue_redraw();
  259. }
  260. if (mm.is_valid() && !tool_triangle->is_pressed() && making_triangle.size()) {
  261. making_triangle.clear();
  262. blend_space_draw->queue_redraw();
  263. }
  264. if (mm.is_valid() && tool_blend->is_pressed() && (mm->get_button_mask() & MouseButton::MASK_LEFT) != MouseButton::NONE) {
  265. Vector2 blend_pos = (mm->get_position() / blend_space_draw->get_size());
  266. blend_pos.y = 1.0 - blend_pos.y;
  267. blend_pos *= (blend_space->get_max_space() - blend_space->get_min_space());
  268. blend_pos += blend_space->get_min_space();
  269. AnimationTreeEditor::get_singleton()->get_tree()->set(get_blend_position_path(), blend_pos);
  270. blend_space_draw->queue_redraw();
  271. }
  272. }
  273. void AnimationNodeBlendSpace2DEditor::_file_opened(const String &p_file) {
  274. file_loaded = ResourceLoader::load(p_file);
  275. if (file_loaded.is_valid()) {
  276. _add_menu_type(MENU_LOAD_FILE_CONFIRM);
  277. }
  278. }
  279. void AnimationNodeBlendSpace2DEditor::_add_menu_type(int p_index) {
  280. Ref<AnimationRootNode> node;
  281. if (p_index == MENU_LOAD_FILE) {
  282. open_file->clear_filters();
  283. List<String> filters;
  284. ResourceLoader::get_recognized_extensions_for_type("AnimationRootNode", &filters);
  285. for (const String &E : filters) {
  286. open_file->add_filter("*." + E);
  287. }
  288. open_file->popup_file_dialog();
  289. return;
  290. } else if (p_index == MENU_LOAD_FILE_CONFIRM) {
  291. node = file_loaded;
  292. file_loaded.unref();
  293. } else if (p_index == MENU_PASTE) {
  294. node = EditorSettings::get_singleton()->get_resource_clipboard();
  295. } else {
  296. String type = menu->get_item_metadata(p_index);
  297. Object *obj = ClassDB::instantiate(type);
  298. ERR_FAIL_COND(!obj);
  299. AnimationNode *an = Object::cast_to<AnimationNode>(obj);
  300. ERR_FAIL_COND(!an);
  301. node = Ref<AnimationNode>(an);
  302. }
  303. if (!node.is_valid()) {
  304. EditorNode::get_singleton()->show_warning(TTR("This type of node can't be used. Only root nodes are allowed."));
  305. return;
  306. }
  307. updating = true;
  308. undo_redo->create_action(TTR("Add Node Point"));
  309. undo_redo->add_do_method(blend_space.ptr(), "add_blend_point", node, add_point_pos);
  310. undo_redo->add_undo_method(blend_space.ptr(), "remove_blend_point", blend_space->get_blend_point_count());
  311. undo_redo->add_do_method(this, "_update_space");
  312. undo_redo->add_undo_method(this, "_update_space");
  313. undo_redo->commit_action();
  314. updating = false;
  315. blend_space_draw->queue_redraw();
  316. }
  317. void AnimationNodeBlendSpace2DEditor::_add_animation_type(int p_index) {
  318. Ref<AnimationNodeAnimation> anim;
  319. anim.instantiate();
  320. anim->set_animation(animations_to_add[p_index]);
  321. updating = true;
  322. undo_redo->create_action(TTR("Add Animation Point"));
  323. undo_redo->add_do_method(blend_space.ptr(), "add_blend_point", anim, add_point_pos);
  324. undo_redo->add_undo_method(blend_space.ptr(), "remove_blend_point", blend_space->get_blend_point_count());
  325. undo_redo->add_do_method(this, "_update_space");
  326. undo_redo->add_undo_method(this, "_update_space");
  327. undo_redo->commit_action();
  328. updating = false;
  329. blend_space_draw->queue_redraw();
  330. }
  331. void AnimationNodeBlendSpace2DEditor::_update_tool_erase() {
  332. tool_erase->set_disabled(
  333. (!(selected_point >= 0 && selected_point < blend_space->get_blend_point_count()) && !(selected_triangle >= 0 && selected_triangle < blend_space->get_triangle_count())) ||
  334. read_only);
  335. if (selected_point >= 0 && selected_point < blend_space->get_blend_point_count()) {
  336. Ref<AnimationNode> an = blend_space->get_blend_point_node(selected_point);
  337. if (AnimationTreeEditor::get_singleton()->can_edit(an)) {
  338. open_editor->show();
  339. } else {
  340. open_editor->hide();
  341. }
  342. if (!read_only) {
  343. edit_hb->show();
  344. } else {
  345. edit_hb->hide();
  346. }
  347. } else {
  348. edit_hb->hide();
  349. }
  350. }
  351. void AnimationNodeBlendSpace2DEditor::_tool_switch(int p_tool) {
  352. making_triangle.clear();
  353. if (p_tool == 2) {
  354. Vector<Vector2> points;
  355. for (int i = 0; i < blend_space->get_blend_point_count(); i++) {
  356. points.push_back(blend_space->get_blend_point_position(i));
  357. }
  358. Vector<Delaunay2D::Triangle> tr = Delaunay2D::triangulate(points);
  359. for (int i = 0; i < tr.size(); i++) {
  360. blend_space->add_triangle(tr[i].points[0], tr[i].points[1], tr[i].points[2]);
  361. }
  362. }
  363. if (p_tool == 0) {
  364. tool_erase->show();
  365. tool_erase_sep->show();
  366. } else {
  367. tool_erase->hide();
  368. tool_erase_sep->hide();
  369. }
  370. _update_tool_erase();
  371. blend_space_draw->queue_redraw();
  372. }
  373. void AnimationNodeBlendSpace2DEditor::_blend_space_draw() {
  374. Color linecolor = get_theme_color(SNAME("font_color"), SNAME("Label"));
  375. Color linecolor_soft = linecolor;
  376. linecolor_soft.a *= 0.5;
  377. Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Label"));
  378. int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Label"));
  379. Ref<Texture2D> icon = get_theme_icon(SNAME("KeyValue"), SNAME("EditorIcons"));
  380. Ref<Texture2D> icon_selected = get_theme_icon(SNAME("KeySelected"), SNAME("EditorIcons"));
  381. Size2 s = blend_space_draw->get_size();
  382. if (blend_space_draw->has_focus()) {
  383. Color color = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  384. blend_space_draw->draw_rect(Rect2(Point2(), s), color, false);
  385. }
  386. blend_space_draw->draw_line(Point2(1, 0), Point2(1, s.height - 1), linecolor, Math::round(EDSCALE));
  387. blend_space_draw->draw_line(Point2(1, s.height - 1), Point2(s.width - 1, s.height - 1), linecolor, Math::round(EDSCALE));
  388. blend_space_draw->draw_line(Point2(0, 0), Point2(5 * EDSCALE, 0), linecolor, Math::round(EDSCALE));
  389. if (blend_space->get_min_space().y < 0) {
  390. int y = (blend_space->get_max_space().y / (blend_space->get_max_space().y - blend_space->get_min_space().y)) * s.height;
  391. blend_space_draw->draw_line(Point2(0, y), Point2(5 * EDSCALE, y), linecolor, Math::round(EDSCALE));
  392. blend_space_draw->draw_string(font, Point2(2 * EDSCALE, y - font->get_height(font_size) + font->get_ascent(font_size)), "0", HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, linecolor);
  393. blend_space_draw->draw_line(Point2(5 * EDSCALE, y), Point2(s.width, y), linecolor_soft, Math::round(EDSCALE));
  394. }
  395. if (blend_space->get_min_space().x < 0) {
  396. int x = (-blend_space->get_min_space().x / (blend_space->get_max_space().x - blend_space->get_min_space().x)) * s.width;
  397. blend_space_draw->draw_line(Point2(x, s.height - 1), Point2(x, s.height - 5 * EDSCALE), linecolor, Math::round(EDSCALE));
  398. blend_space_draw->draw_string(font, Point2(x + 2 * EDSCALE, s.height - 2 * EDSCALE - font->get_height(font_size) + font->get_ascent(font_size)), "0", HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, linecolor);
  399. blend_space_draw->draw_line(Point2(x, s.height - 5 * EDSCALE), Point2(x, 0), linecolor_soft, Math::round(EDSCALE));
  400. }
  401. if (snap->is_pressed()) {
  402. linecolor_soft.a = linecolor.a * 0.1;
  403. if (blend_space->get_snap().x > 0) {
  404. int prev_idx = 0;
  405. for (int i = 0; i < s.x; i++) {
  406. float v = blend_space->get_min_space().x + i * (blend_space->get_max_space().x - blend_space->get_min_space().x) / s.x;
  407. int idx = int(v / blend_space->get_snap().x);
  408. if (i > 0 && prev_idx != idx) {
  409. blend_space_draw->draw_line(Point2(i, 0), Point2(i, s.height), linecolor_soft, Math::round(EDSCALE));
  410. }
  411. prev_idx = idx;
  412. }
  413. }
  414. if (blend_space->get_snap().y > 0) {
  415. int prev_idx = 0;
  416. for (int i = 0; i < s.y; i++) {
  417. float v = blend_space->get_max_space().y - i * (blend_space->get_max_space().y - blend_space->get_min_space().y) / s.y;
  418. int idx = int(v / blend_space->get_snap().y);
  419. if (i > 0 && prev_idx != idx) {
  420. blend_space_draw->draw_line(Point2(0, i), Point2(s.width, i), linecolor_soft, Math::round(EDSCALE));
  421. }
  422. prev_idx = idx;
  423. }
  424. }
  425. }
  426. //triangles first
  427. for (int i = 0; i < blend_space->get_triangle_count(); i++) {
  428. Vector<Vector2> points;
  429. points.resize(3);
  430. for (int j = 0; j < 3; j++) {
  431. int point_idx = blend_space->get_triangle_point(i, j);
  432. Vector2 point = blend_space->get_blend_point_position(point_idx);
  433. if (dragging_selected && selected_point == point_idx) {
  434. point += drag_ofs;
  435. if (snap->is_pressed()) {
  436. point = point.snapped(blend_space->get_snap());
  437. }
  438. }
  439. point = (point - blend_space->get_min_space()) / (blend_space->get_max_space() - blend_space->get_min_space());
  440. point *= s;
  441. point.y = s.height - point.y;
  442. points.write[j] = point;
  443. }
  444. for (int j = 0; j < 3; j++) {
  445. blend_space_draw->draw_line(points[j], points[(j + 1) % 3], linecolor, Math::round(EDSCALE), true);
  446. }
  447. Color color;
  448. if (i == selected_triangle) {
  449. color = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  450. color.a *= 0.5;
  451. } else {
  452. color = linecolor;
  453. color.a *= 0.2;
  454. }
  455. Vector<Color> colors = {
  456. color,
  457. color,
  458. color
  459. };
  460. blend_space_draw->draw_primitive(points, colors, Vector<Vector2>());
  461. }
  462. points.clear();
  463. for (int i = 0; i < blend_space->get_blend_point_count(); i++) {
  464. Vector2 point = blend_space->get_blend_point_position(i);
  465. if (!read_only) {
  466. if (dragging_selected && selected_point == i) {
  467. point += drag_ofs;
  468. if (snap->is_pressed()) {
  469. point = point.snapped(blend_space->get_snap());
  470. }
  471. }
  472. }
  473. point = (point - blend_space->get_min_space()) / (blend_space->get_max_space() - blend_space->get_min_space());
  474. point *= s;
  475. point.y = s.height - point.y;
  476. points.push_back(point);
  477. point -= (icon->get_size() / 2);
  478. point = point.floor();
  479. if (i == selected_point) {
  480. blend_space_draw->draw_texture(icon_selected, point);
  481. } else {
  482. blend_space_draw->draw_texture(icon, point);
  483. }
  484. }
  485. if (making_triangle.size()) {
  486. Vector<Vector2> points;
  487. for (int i = 0; i < making_triangle.size(); i++) {
  488. Vector2 point = blend_space->get_blend_point_position(making_triangle[i]);
  489. point = (point - blend_space->get_min_space()) / (blend_space->get_max_space() - blend_space->get_min_space());
  490. point *= s;
  491. point.y = s.height - point.y;
  492. points.push_back(point);
  493. }
  494. for (int i = 0; i < points.size() - 1; i++) {
  495. blend_space_draw->draw_line(points[i], points[i + 1], linecolor, Math::round(2 * EDSCALE), true);
  496. }
  497. blend_space_draw->draw_line(points[points.size() - 1], blend_space_draw->get_local_mouse_position(), linecolor, Math::round(2 * EDSCALE), true);
  498. }
  499. ///draw cursor position
  500. {
  501. Color color;
  502. if (tool_blend->is_pressed()) {
  503. color = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  504. } else {
  505. color = linecolor;
  506. color.a *= 0.5;
  507. }
  508. Vector2 blend_pos = AnimationTreeEditor::get_singleton()->get_tree()->get(get_blend_position_path());
  509. Vector2 point = blend_pos;
  510. point = (point - blend_space->get_min_space()) / (blend_space->get_max_space() - blend_space->get_min_space());
  511. point *= s;
  512. point.y = s.height - point.y;
  513. if (blend_space->get_triangle_count()) {
  514. Vector2 closest = blend_space->get_closest_point(blend_pos);
  515. closest = (closest - blend_space->get_min_space()) / (blend_space->get_max_space() - blend_space->get_min_space());
  516. closest *= s;
  517. closest.y = s.height - closest.y;
  518. Color lcol = color;
  519. lcol.a *= 0.4;
  520. blend_space_draw->draw_line(point, closest, lcol, Math::round(2 * EDSCALE), true);
  521. }
  522. float mind = 5 * EDSCALE;
  523. float maxd = 15 * EDSCALE;
  524. blend_space_draw->draw_line(point + Vector2(mind, 0), point + Vector2(maxd, 0), color, Math::round(2 * EDSCALE));
  525. blend_space_draw->draw_line(point + Vector2(-mind, 0), point + Vector2(-maxd, 0), color, Math::round(2 * EDSCALE));
  526. blend_space_draw->draw_line(point + Vector2(0, mind), point + Vector2(0, maxd), color, Math::round(2 * EDSCALE));
  527. blend_space_draw->draw_line(point + Vector2(0, -mind), point + Vector2(0, -maxd), color, Math::round(2 * EDSCALE));
  528. }
  529. }
  530. void AnimationNodeBlendSpace2DEditor::_snap_toggled() {
  531. blend_space_draw->queue_redraw();
  532. }
  533. void AnimationNodeBlendSpace2DEditor::_update_space() {
  534. if (updating) {
  535. return;
  536. }
  537. updating = true;
  538. if (blend_space->get_auto_triangles()) {
  539. tool_triangle->hide();
  540. } else {
  541. tool_triangle->show();
  542. }
  543. auto_triangles->set_pressed(blend_space->get_auto_triangles());
  544. sync->set_pressed(blend_space->is_using_sync());
  545. interpolation->select(blend_space->get_blend_mode());
  546. max_x_value->set_value(blend_space->get_max_space().x);
  547. max_y_value->set_value(blend_space->get_max_space().y);
  548. min_x_value->set_value(blend_space->get_min_space().x);
  549. min_y_value->set_value(blend_space->get_min_space().y);
  550. label_x->set_text(blend_space->get_x_label());
  551. label_y->set_text(blend_space->get_y_label());
  552. snap_x->set_value(blend_space->get_snap().x);
  553. snap_y->set_value(blend_space->get_snap().y);
  554. blend_space_draw->queue_redraw();
  555. updating = false;
  556. }
  557. void AnimationNodeBlendSpace2DEditor::_config_changed(double) {
  558. if (updating) {
  559. return;
  560. }
  561. updating = true;
  562. undo_redo->create_action(TTR("Change BlendSpace2D Config"));
  563. undo_redo->add_do_method(blend_space.ptr(), "set_max_space", Vector2(max_x_value->get_value(), max_y_value->get_value()));
  564. undo_redo->add_undo_method(blend_space.ptr(), "set_max_space", blend_space->get_max_space());
  565. undo_redo->add_do_method(blend_space.ptr(), "set_min_space", Vector2(min_x_value->get_value(), min_y_value->get_value()));
  566. undo_redo->add_undo_method(blend_space.ptr(), "set_min_space", blend_space->get_min_space());
  567. undo_redo->add_do_method(blend_space.ptr(), "set_snap", Vector2(snap_x->get_value(), snap_y->get_value()));
  568. undo_redo->add_undo_method(blend_space.ptr(), "set_snap", blend_space->get_snap());
  569. undo_redo->add_do_method(blend_space.ptr(), "set_use_sync", sync->is_pressed());
  570. undo_redo->add_undo_method(blend_space.ptr(), "set_use_sync", blend_space->is_using_sync());
  571. undo_redo->add_do_method(blend_space.ptr(), "set_blend_mode", interpolation->get_selected());
  572. undo_redo->add_undo_method(blend_space.ptr(), "set_blend_mode", blend_space->get_blend_mode());
  573. undo_redo->add_do_method(this, "_update_space");
  574. undo_redo->add_undo_method(this, "_update_space");
  575. undo_redo->commit_action();
  576. updating = false;
  577. blend_space_draw->queue_redraw();
  578. }
  579. void AnimationNodeBlendSpace2DEditor::_labels_changed(String) {
  580. if (updating) {
  581. return;
  582. }
  583. updating = true;
  584. undo_redo->create_action(TTR("Change BlendSpace2D Labels"), UndoRedo::MERGE_ENDS);
  585. undo_redo->add_do_method(blend_space.ptr(), "set_x_label", label_x->get_text());
  586. undo_redo->add_undo_method(blend_space.ptr(), "set_x_label", blend_space->get_x_label());
  587. undo_redo->add_do_method(blend_space.ptr(), "set_y_label", label_y->get_text());
  588. undo_redo->add_undo_method(blend_space.ptr(), "set_y_label", blend_space->get_y_label());
  589. undo_redo->add_do_method(this, "_update_space");
  590. undo_redo->add_undo_method(this, "_update_space");
  591. undo_redo->commit_action();
  592. updating = false;
  593. }
  594. void AnimationNodeBlendSpace2DEditor::_erase_selected() {
  595. if (selected_point != -1) {
  596. updating = true;
  597. undo_redo->create_action(TTR("Remove BlendSpace2D Point"));
  598. undo_redo->add_do_method(blend_space.ptr(), "remove_blend_point", selected_point);
  599. 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);
  600. //restore triangles using this point
  601. for (int i = 0; i < blend_space->get_triangle_count(); i++) {
  602. for (int j = 0; j < 3; j++) {
  603. if (blend_space->get_triangle_point(i, j) == selected_point) {
  604. undo_redo->add_undo_method(blend_space.ptr(), "add_triangle", blend_space->get_triangle_point(i, 0), blend_space->get_triangle_point(i, 1), blend_space->get_triangle_point(i, 2), i);
  605. break;
  606. }
  607. }
  608. }
  609. undo_redo->add_do_method(this, "_update_space");
  610. undo_redo->add_undo_method(this, "_update_space");
  611. undo_redo->commit_action();
  612. updating = false;
  613. blend_space_draw->queue_redraw();
  614. } else if (selected_triangle != -1) {
  615. updating = true;
  616. undo_redo->create_action(TTR("Remove BlendSpace2D Triangle"));
  617. undo_redo->add_do_method(blend_space.ptr(), "remove_triangle", selected_triangle);
  618. undo_redo->add_undo_method(blend_space.ptr(), "add_triangle", blend_space->get_triangle_point(selected_triangle, 0), blend_space->get_triangle_point(selected_triangle, 1), blend_space->get_triangle_point(selected_triangle, 2), selected_triangle);
  619. undo_redo->add_do_method(this, "_update_space");
  620. undo_redo->add_undo_method(this, "_update_space");
  621. undo_redo->commit_action();
  622. updating = false;
  623. blend_space_draw->queue_redraw();
  624. }
  625. }
  626. void AnimationNodeBlendSpace2DEditor::_update_edited_point_pos() {
  627. if (updating) {
  628. return;
  629. }
  630. if (selected_point >= 0 && selected_point < blend_space->get_blend_point_count()) {
  631. Vector2 pos = blend_space->get_blend_point_position(selected_point);
  632. if (dragging_selected) {
  633. pos += drag_ofs;
  634. if (snap->is_pressed()) {
  635. pos = pos.snapped(blend_space->get_snap());
  636. }
  637. }
  638. updating = true;
  639. edit_x->set_value(pos.x);
  640. edit_y->set_value(pos.y);
  641. updating = false;
  642. }
  643. }
  644. void AnimationNodeBlendSpace2DEditor::_edit_point_pos(double) {
  645. if (updating) {
  646. return;
  647. }
  648. updating = true;
  649. undo_redo->create_action(TTR("Move Node Point"));
  650. undo_redo->add_do_method(blend_space.ptr(), "set_blend_point_position", selected_point, Vector2(edit_x->get_value(), edit_y->get_value()));
  651. undo_redo->add_undo_method(blend_space.ptr(), "set_blend_point_position", selected_point, blend_space->get_blend_point_position(selected_point));
  652. undo_redo->add_do_method(this, "_update_space");
  653. undo_redo->add_undo_method(this, "_update_space");
  654. undo_redo->add_do_method(this, "_update_edited_point_pos");
  655. undo_redo->add_undo_method(this, "_update_edited_point_pos");
  656. undo_redo->commit_action();
  657. updating = false;
  658. blend_space_draw->queue_redraw();
  659. }
  660. void AnimationNodeBlendSpace2DEditor::_notification(int p_what) {
  661. switch (p_what) {
  662. case NOTIFICATION_ENTER_TREE:
  663. case NOTIFICATION_THEME_CHANGED: {
  664. error_panel->add_theme_style_override("panel", get_theme_stylebox(SNAME("bg"), SNAME("Tree")));
  665. error_label->add_theme_color_override("font_color", get_theme_color(SNAME("error_color"), SNAME("Editor")));
  666. panel->add_theme_style_override("panel", get_theme_stylebox(SNAME("bg"), SNAME("Tree")));
  667. tool_blend->set_icon(get_theme_icon(SNAME("EditPivot"), SNAME("EditorIcons")));
  668. tool_select->set_icon(get_theme_icon(SNAME("ToolSelect"), SNAME("EditorIcons")));
  669. tool_create->set_icon(get_theme_icon(SNAME("EditKey"), SNAME("EditorIcons")));
  670. tool_triangle->set_icon(get_theme_icon(SNAME("ToolTriangle"), SNAME("EditorIcons")));
  671. tool_erase->set_icon(get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")));
  672. snap->set_icon(get_theme_icon(SNAME("SnapGrid"), SNAME("EditorIcons")));
  673. open_editor->set_icon(get_theme_icon(SNAME("Edit"), SNAME("EditorIcons")));
  674. auto_triangles->set_icon(get_theme_icon(SNAME("AutoTriangle"), SNAME("EditorIcons")));
  675. interpolation->clear();
  676. interpolation->add_icon_item(get_theme_icon(SNAME("TrackContinuous"), SNAME("EditorIcons")), "", 0);
  677. interpolation->add_icon_item(get_theme_icon(SNAME("TrackDiscrete"), SNAME("EditorIcons")), "", 1);
  678. interpolation->add_icon_item(get_theme_icon(SNAME("TrackCapture"), SNAME("EditorIcons")), "", 2);
  679. } break;
  680. case NOTIFICATION_PROCESS: {
  681. String error;
  682. if (!AnimationTreeEditor::get_singleton()->get_tree()) {
  683. error = TTR("BlendSpace2D does not belong to an AnimationTree node.");
  684. } else if (!AnimationTreeEditor::get_singleton()->get_tree()->is_active()) {
  685. error = TTR("AnimationTree is inactive.\nActivate to enable playback, check node warnings if activation fails.");
  686. } else if (AnimationTreeEditor::get_singleton()->get_tree()->is_state_invalid()) {
  687. error = AnimationTreeEditor::get_singleton()->get_tree()->get_invalid_state_reason();
  688. } else if (blend_space->get_triangle_count() == 0) {
  689. error = TTR("No triangles exist, so no blending can take place.");
  690. }
  691. if (error != error_label->get_text()) {
  692. error_label->set_text(error);
  693. if (!error.is_empty()) {
  694. error_panel->show();
  695. } else {
  696. error_panel->hide();
  697. }
  698. }
  699. } break;
  700. case NOTIFICATION_VISIBILITY_CHANGED: {
  701. set_process(is_visible_in_tree());
  702. } break;
  703. }
  704. }
  705. void AnimationNodeBlendSpace2DEditor::_open_editor() {
  706. if (selected_point >= 0 && selected_point < blend_space->get_blend_point_count()) {
  707. Ref<AnimationNode> an = blend_space->get_blend_point_node(selected_point);
  708. ERR_FAIL_COND(an.is_null());
  709. AnimationTreeEditor::get_singleton()->enter_editor(itos(selected_point));
  710. }
  711. }
  712. void AnimationNodeBlendSpace2DEditor::_removed_from_graph() {
  713. EditorNode::get_singleton()->edit_item(nullptr);
  714. }
  715. void AnimationNodeBlendSpace2DEditor::_auto_triangles_toggled() {
  716. undo_redo->create_action(TTR("Toggle Auto Triangles"));
  717. undo_redo->add_do_method(blend_space.ptr(), "set_auto_triangles", auto_triangles->is_pressed());
  718. undo_redo->add_undo_method(blend_space.ptr(), "set_auto_triangles", blend_space->get_auto_triangles());
  719. undo_redo->add_do_method(this, "_update_space");
  720. undo_redo->add_undo_method(this, "_update_space");
  721. undo_redo->commit_action();
  722. }
  723. void AnimationNodeBlendSpace2DEditor::_bind_methods() {
  724. ClassDB::bind_method("_update_space", &AnimationNodeBlendSpace2DEditor::_update_space);
  725. ClassDB::bind_method("_update_tool_erase", &AnimationNodeBlendSpace2DEditor::_update_tool_erase);
  726. ClassDB::bind_method("_update_edited_point_pos", &AnimationNodeBlendSpace2DEditor::_update_edited_point_pos);
  727. ClassDB::bind_method("_removed_from_graph", &AnimationNodeBlendSpace2DEditor::_removed_from_graph);
  728. }
  729. AnimationNodeBlendSpace2DEditor *AnimationNodeBlendSpace2DEditor::singleton = nullptr;
  730. AnimationNodeBlendSpace2DEditor::AnimationNodeBlendSpace2DEditor() {
  731. singleton = this;
  732. updating = false;
  733. HBoxContainer *top_hb = memnew(HBoxContainer);
  734. add_child(top_hb);
  735. Ref<ButtonGroup> bg;
  736. bg.instantiate();
  737. tool_blend = memnew(Button);
  738. tool_blend->set_flat(true);
  739. tool_blend->set_toggle_mode(true);
  740. tool_blend->set_button_group(bg);
  741. top_hb->add_child(tool_blend);
  742. tool_blend->set_pressed(true);
  743. tool_blend->set_tooltip_text(TTR("Set the blending position within the space"));
  744. tool_blend->connect("pressed", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_tool_switch).bind(3));
  745. tool_select = memnew(Button);
  746. tool_select->set_flat(true);
  747. tool_select->set_toggle_mode(true);
  748. tool_select->set_button_group(bg);
  749. top_hb->add_child(tool_select);
  750. tool_select->set_tooltip_text(TTR("Select and move points, create points with RMB."));
  751. tool_select->connect("pressed", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_tool_switch).bind(0));
  752. tool_create = memnew(Button);
  753. tool_create->set_flat(true);
  754. tool_create->set_toggle_mode(true);
  755. tool_create->set_button_group(bg);
  756. top_hb->add_child(tool_create);
  757. tool_create->set_tooltip_text(TTR("Create points."));
  758. tool_create->connect("pressed", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_tool_switch).bind(1));
  759. tool_triangle = memnew(Button);
  760. tool_triangle->set_flat(true);
  761. tool_triangle->set_toggle_mode(true);
  762. tool_triangle->set_button_group(bg);
  763. top_hb->add_child(tool_triangle);
  764. tool_triangle->set_tooltip_text(TTR("Create triangles by connecting points."));
  765. tool_triangle->connect("pressed", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_tool_switch).bind(2));
  766. tool_erase_sep = memnew(VSeparator);
  767. top_hb->add_child(tool_erase_sep);
  768. tool_erase = memnew(Button);
  769. tool_erase->set_flat(true);
  770. top_hb->add_child(tool_erase);
  771. tool_erase->set_tooltip_text(TTR("Erase points and triangles."));
  772. tool_erase->connect("pressed", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_erase_selected));
  773. tool_erase->set_disabled(true);
  774. top_hb->add_child(memnew(VSeparator));
  775. auto_triangles = memnew(Button);
  776. auto_triangles->set_flat(true);
  777. top_hb->add_child(auto_triangles);
  778. auto_triangles->connect("pressed", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_auto_triangles_toggled));
  779. auto_triangles->set_toggle_mode(true);
  780. auto_triangles->set_tooltip_text(TTR("Generate blend triangles automatically (instead of manually)"));
  781. top_hb->add_child(memnew(VSeparator));
  782. snap = memnew(Button);
  783. snap->set_flat(true);
  784. snap->set_toggle_mode(true);
  785. top_hb->add_child(snap);
  786. snap->set_pressed(true);
  787. snap->set_tooltip_text(TTR("Enable snap and show grid."));
  788. snap->connect("pressed", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_snap_toggled));
  789. snap_x = memnew(SpinBox);
  790. top_hb->add_child(snap_x);
  791. snap_x->set_prefix("x:");
  792. snap_x->set_min(0.01);
  793. snap_x->set_step(0.01);
  794. snap_x->set_max(1000);
  795. snap_y = memnew(SpinBox);
  796. top_hb->add_child(snap_y);
  797. snap_y->set_prefix("y:");
  798. snap_y->set_min(0.01);
  799. snap_y->set_step(0.01);
  800. snap_y->set_max(1000);
  801. top_hb->add_child(memnew(VSeparator));
  802. top_hb->add_child(memnew(Label(TTR("Sync:"))));
  803. sync = memnew(CheckBox);
  804. top_hb->add_child(sync);
  805. sync->connect("toggled", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_config_changed));
  806. top_hb->add_child(memnew(VSeparator));
  807. top_hb->add_child(memnew(Label(TTR("Blend:"))));
  808. interpolation = memnew(OptionButton);
  809. top_hb->add_child(interpolation);
  810. interpolation->connect("item_selected", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_config_changed));
  811. edit_hb = memnew(HBoxContainer);
  812. top_hb->add_child(edit_hb);
  813. edit_hb->add_child(memnew(VSeparator));
  814. edit_hb->add_child(memnew(Label(TTR("Point"))));
  815. edit_x = memnew(SpinBox);
  816. edit_hb->add_child(edit_x);
  817. edit_x->set_min(-1000);
  818. edit_x->set_step(0.01);
  819. edit_x->set_max(1000);
  820. edit_x->connect("value_changed", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_edit_point_pos));
  821. edit_y = memnew(SpinBox);
  822. edit_hb->add_child(edit_y);
  823. edit_y->set_min(-1000);
  824. edit_y->set_step(0.01);
  825. edit_y->set_max(1000);
  826. edit_y->connect("value_changed", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_edit_point_pos));
  827. open_editor = memnew(Button);
  828. edit_hb->add_child(open_editor);
  829. open_editor->set_text(TTR("Open Editor"));
  830. open_editor->connect("pressed", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_open_editor), CONNECT_DEFERRED);
  831. edit_hb->hide();
  832. open_editor->hide();
  833. HBoxContainer *main_hb = memnew(HBoxContainer);
  834. add_child(main_hb);
  835. main_hb->set_v_size_flags(SIZE_EXPAND_FILL);
  836. GridContainer *main_grid = memnew(GridContainer);
  837. main_grid->set_columns(2);
  838. main_hb->add_child(main_grid);
  839. main_grid->set_h_size_flags(SIZE_EXPAND_FILL);
  840. {
  841. VBoxContainer *left_vbox = memnew(VBoxContainer);
  842. main_grid->add_child(left_vbox);
  843. left_vbox->set_v_size_flags(SIZE_EXPAND_FILL);
  844. max_y_value = memnew(SpinBox);
  845. left_vbox->add_child(max_y_value);
  846. left_vbox->add_spacer();
  847. label_y = memnew(LineEdit);
  848. left_vbox->add_child(label_y);
  849. label_y->set_expand_to_text_length_enabled(true);
  850. left_vbox->add_spacer();
  851. min_y_value = memnew(SpinBox);
  852. left_vbox->add_child(min_y_value);
  853. max_y_value->set_max(10000);
  854. max_y_value->set_min(0.01);
  855. max_y_value->set_step(0.01);
  856. min_y_value->set_min(-10000);
  857. min_y_value->set_max(0);
  858. min_y_value->set_step(0.01);
  859. }
  860. panel = memnew(PanelContainer);
  861. panel->set_clip_contents(true);
  862. main_grid->add_child(panel);
  863. panel->set_h_size_flags(SIZE_EXPAND_FILL);
  864. blend_space_draw = memnew(Control);
  865. blend_space_draw->connect("gui_input", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_blend_space_gui_input));
  866. blend_space_draw->connect("draw", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_blend_space_draw));
  867. blend_space_draw->set_focus_mode(FOCUS_ALL);
  868. panel->add_child(blend_space_draw);
  869. main_grid->add_child(memnew(Control)); //empty bottom left
  870. {
  871. HBoxContainer *bottom_vbox = memnew(HBoxContainer);
  872. main_grid->add_child(bottom_vbox);
  873. bottom_vbox->set_h_size_flags(SIZE_EXPAND_FILL);
  874. min_x_value = memnew(SpinBox);
  875. bottom_vbox->add_child(min_x_value);
  876. bottom_vbox->add_spacer();
  877. label_x = memnew(LineEdit);
  878. bottom_vbox->add_child(label_x);
  879. label_x->set_expand_to_text_length_enabled(true);
  880. bottom_vbox->add_spacer();
  881. max_x_value = memnew(SpinBox);
  882. bottom_vbox->add_child(max_x_value);
  883. max_x_value->set_max(10000);
  884. max_x_value->set_min(0.01);
  885. max_x_value->set_step(0.01);
  886. min_x_value->set_min(-10000);
  887. min_x_value->set_max(0);
  888. min_x_value->set_step(0.01);
  889. }
  890. snap_x->connect("value_changed", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_config_changed));
  891. snap_y->connect("value_changed", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_config_changed));
  892. max_x_value->connect("value_changed", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_config_changed));
  893. min_x_value->connect("value_changed", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_config_changed));
  894. max_y_value->connect("value_changed", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_config_changed));
  895. min_y_value->connect("value_changed", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_config_changed));
  896. label_x->connect("text_changed", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_labels_changed));
  897. label_y->connect("text_changed", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_labels_changed));
  898. error_panel = memnew(PanelContainer);
  899. add_child(error_panel);
  900. error_label = memnew(Label);
  901. error_panel->add_child(error_label);
  902. error_label->set_text("eh");
  903. undo_redo = EditorNode::get_undo_redo();
  904. set_custom_minimum_size(Size2(0, 300 * EDSCALE));
  905. menu = memnew(PopupMenu);
  906. add_child(menu);
  907. menu->connect("id_pressed", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_add_menu_type));
  908. animations_menu = memnew(PopupMenu);
  909. menu->add_child(animations_menu);
  910. animations_menu->set_name("animations");
  911. animations_menu->connect("index_pressed", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_add_animation_type));
  912. open_file = memnew(EditorFileDialog);
  913. add_child(open_file);
  914. open_file->set_title(TTR("Open Animation Node"));
  915. open_file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  916. open_file->connect("file_selected", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_file_opened));
  917. undo_redo = EditorNode::get_undo_redo();
  918. selected_point = -1;
  919. selected_triangle = -1;
  920. dragging_selected = false;
  921. dragging_selected_attempt = false;
  922. }