animation_blend_space_2d_editor.cpp 42 KB

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