animation_blend_space_2d_editor.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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_settings.h"
  36. #include "editor/editor_string_names.h"
  37. #include "editor/editor_undo_redo_manager.h"
  38. #include "editor/gui/editor_file_dialog.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. List<StringName> classes;
  110. classes.sort_custom<StringName::AlphCompare>();
  111. ClassDB::get_inheriters_from_class("AnimationRootNode", &classes);
  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_ENTER_TREE:
  680. case NOTIFICATION_THEME_CHANGED: {
  681. error_panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));
  682. error_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  683. panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));
  684. tool_blend->set_button_icon(get_editor_theme_icon(SNAME("EditPivot")));
  685. tool_select->set_button_icon(get_editor_theme_icon(SNAME("ToolSelect")));
  686. tool_create->set_button_icon(get_editor_theme_icon(SNAME("EditKey")));
  687. tool_triangle->set_button_icon(get_editor_theme_icon(SNAME("ToolTriangle")));
  688. tool_erase->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
  689. snap->set_button_icon(get_editor_theme_icon(SNAME("SnapGrid")));
  690. open_editor->set_button_icon(get_editor_theme_icon(SNAME("Edit")));
  691. auto_triangles->set_button_icon(get_editor_theme_icon(SNAME("AutoTriangle")));
  692. interpolation->clear();
  693. interpolation->add_icon_item(get_editor_theme_icon(SNAME("TrackContinuous")), TTR("Continuous"), 0);
  694. interpolation->add_icon_item(get_editor_theme_icon(SNAME("TrackDiscrete")), TTR("Discrete"), 1);
  695. interpolation->add_icon_item(get_editor_theme_icon(SNAME("TrackCapture")), TTR("Capture"), 2);
  696. } break;
  697. case NOTIFICATION_PROCESS: {
  698. AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
  699. if (!tree) {
  700. return;
  701. }
  702. String error;
  703. if (!tree->is_active()) {
  704. error = TTR("AnimationTree is inactive.\nActivate to enable playback, check node warnings if activation fails.");
  705. } else if (tree->is_state_invalid()) {
  706. error = tree->get_invalid_state_reason();
  707. } else if (blend_space->get_triangle_count() == 0) {
  708. error = TTR("No triangles exist, so no blending can take place.");
  709. }
  710. if (error != error_label->get_text()) {
  711. error_label->set_text(error);
  712. if (!error.is_empty()) {
  713. error_panel->show();
  714. } else {
  715. error_panel->hide();
  716. }
  717. }
  718. } break;
  719. case NOTIFICATION_VISIBILITY_CHANGED: {
  720. set_process(is_visible_in_tree());
  721. } break;
  722. }
  723. }
  724. void AnimationNodeBlendSpace2DEditor::_open_editor() {
  725. if (selected_point >= 0 && selected_point < blend_space->get_blend_point_count()) {
  726. Ref<AnimationNode> an = blend_space->get_blend_point_node(selected_point);
  727. ERR_FAIL_COND(an.is_null());
  728. AnimationTreeEditor::get_singleton()->enter_editor(itos(selected_point));
  729. }
  730. }
  731. void AnimationNodeBlendSpace2DEditor::_auto_triangles_toggled() {
  732. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  733. undo_redo->create_action(TTR("Toggle Auto Triangles"));
  734. undo_redo->add_do_method(blend_space.ptr(), "set_auto_triangles", auto_triangles->is_pressed());
  735. undo_redo->add_undo_method(blend_space.ptr(), "set_auto_triangles", blend_space->get_auto_triangles());
  736. undo_redo->add_do_method(this, "_update_space");
  737. undo_redo->add_undo_method(this, "_update_space");
  738. undo_redo->commit_action();
  739. }
  740. void AnimationNodeBlendSpace2DEditor::_bind_methods() {
  741. ClassDB::bind_method("_update_space", &AnimationNodeBlendSpace2DEditor::_update_space);
  742. ClassDB::bind_method("_update_tool_erase", &AnimationNodeBlendSpace2DEditor::_update_tool_erase);
  743. ClassDB::bind_method("_update_edited_point_pos", &AnimationNodeBlendSpace2DEditor::_update_edited_point_pos);
  744. }
  745. AnimationNodeBlendSpace2DEditor *AnimationNodeBlendSpace2DEditor::singleton = nullptr;
  746. AnimationNodeBlendSpace2DEditor::AnimationNodeBlendSpace2DEditor() {
  747. singleton = this;
  748. updating = false;
  749. HBoxContainer *top_hb = memnew(HBoxContainer);
  750. add_child(top_hb);
  751. Ref<ButtonGroup> bg;
  752. bg.instantiate();
  753. tool_blend = memnew(Button);
  754. tool_blend->set_theme_type_variation(SceneStringName(FlatButton));
  755. tool_blend->set_toggle_mode(true);
  756. tool_blend->set_button_group(bg);
  757. top_hb->add_child(tool_blend);
  758. tool_blend->set_pressed(true);
  759. tool_blend->set_tooltip_text(TTR("Set the blending position within the space"));
  760. tool_blend->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_tool_switch).bind(3));
  761. tool_select = memnew(Button);
  762. tool_select->set_theme_type_variation(SceneStringName(FlatButton));
  763. tool_select->set_toggle_mode(true);
  764. tool_select->set_button_group(bg);
  765. top_hb->add_child(tool_select);
  766. tool_select->set_tooltip_text(TTR("Select and move points, create points with RMB."));
  767. tool_select->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_tool_switch).bind(0));
  768. tool_create = memnew(Button);
  769. tool_create->set_theme_type_variation(SceneStringName(FlatButton));
  770. tool_create->set_toggle_mode(true);
  771. tool_create->set_button_group(bg);
  772. top_hb->add_child(tool_create);
  773. tool_create->set_tooltip_text(TTR("Create points."));
  774. tool_create->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_tool_switch).bind(1));
  775. tool_triangle = memnew(Button);
  776. tool_triangle->set_theme_type_variation(SceneStringName(FlatButton));
  777. tool_triangle->set_toggle_mode(true);
  778. tool_triangle->set_button_group(bg);
  779. top_hb->add_child(tool_triangle);
  780. tool_triangle->set_tooltip_text(TTR("Create triangles by connecting points."));
  781. tool_triangle->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_tool_switch).bind(2));
  782. tool_erase_sep = memnew(VSeparator);
  783. top_hb->add_child(tool_erase_sep);
  784. tool_erase = memnew(Button);
  785. tool_erase->set_theme_type_variation(SceneStringName(FlatButton));
  786. top_hb->add_child(tool_erase);
  787. tool_erase->set_tooltip_text(TTR("Erase points and triangles."));
  788. tool_erase->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_erase_selected));
  789. tool_erase->set_disabled(true);
  790. top_hb->add_child(memnew(VSeparator));
  791. auto_triangles = memnew(Button);
  792. auto_triangles->set_theme_type_variation(SceneStringName(FlatButton));
  793. top_hb->add_child(auto_triangles);
  794. auto_triangles->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_auto_triangles_toggled));
  795. auto_triangles->set_toggle_mode(true);
  796. auto_triangles->set_tooltip_text(TTR("Generate blend triangles automatically (instead of manually)"));
  797. top_hb->add_child(memnew(VSeparator));
  798. snap = memnew(Button);
  799. snap->set_theme_type_variation(SceneStringName(FlatButton));
  800. snap->set_toggle_mode(true);
  801. top_hb->add_child(snap);
  802. snap->set_pressed(true);
  803. snap->set_tooltip_text(TTR("Enable snap and show grid."));
  804. snap->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_snap_toggled));
  805. snap_x = memnew(SpinBox);
  806. top_hb->add_child(snap_x);
  807. snap_x->set_prefix("x:");
  808. snap_x->set_min(0.01);
  809. snap_x->set_step(0.01);
  810. snap_x->set_max(1000);
  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. top_hb->add_child(memnew(VSeparator));
  818. top_hb->add_child(memnew(Label(TTR("Sync:"))));
  819. sync = memnew(CheckBox);
  820. top_hb->add_child(sync);
  821. sync->connect(SceneStringName(toggled), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_config_changed));
  822. top_hb->add_child(memnew(VSeparator));
  823. top_hb->add_child(memnew(Label(TTR("Blend:"))));
  824. interpolation = memnew(OptionButton);
  825. top_hb->add_child(interpolation);
  826. interpolation->connect(SceneStringName(item_selected), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_config_changed));
  827. edit_hb = memnew(HBoxContainer);
  828. top_hb->add_child(edit_hb);
  829. edit_hb->add_child(memnew(VSeparator));
  830. edit_hb->add_child(memnew(Label(TTR("Point"))));
  831. edit_x = memnew(SpinBox);
  832. edit_hb->add_child(edit_x);
  833. edit_x->set_min(-1000);
  834. edit_x->set_step(0.01);
  835. edit_x->set_max(1000);
  836. edit_x->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_edit_point_pos));
  837. edit_y = memnew(SpinBox);
  838. edit_hb->add_child(edit_y);
  839. edit_y->set_min(-1000);
  840. edit_y->set_step(0.01);
  841. edit_y->set_max(1000);
  842. edit_y->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_edit_point_pos));
  843. open_editor = memnew(Button);
  844. edit_hb->add_child(open_editor);
  845. open_editor->set_text(TTR("Open Editor"));
  846. open_editor->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_open_editor), CONNECT_DEFERRED);
  847. edit_hb->hide();
  848. open_editor->hide();
  849. HBoxContainer *main_hb = memnew(HBoxContainer);
  850. add_child(main_hb);
  851. main_hb->set_v_size_flags(SIZE_EXPAND_FILL);
  852. GridContainer *main_grid = memnew(GridContainer);
  853. main_grid->set_columns(2);
  854. main_hb->add_child(main_grid);
  855. main_grid->set_h_size_flags(SIZE_EXPAND_FILL);
  856. {
  857. VBoxContainer *left_vbox = memnew(VBoxContainer);
  858. main_grid->add_child(left_vbox);
  859. left_vbox->set_v_size_flags(SIZE_EXPAND_FILL);
  860. max_y_value = memnew(SpinBox);
  861. left_vbox->add_child(max_y_value);
  862. left_vbox->add_spacer();
  863. label_y = memnew(LineEdit);
  864. left_vbox->add_child(label_y);
  865. label_y->set_expand_to_text_length_enabled(true);
  866. left_vbox->add_spacer();
  867. min_y_value = memnew(SpinBox);
  868. left_vbox->add_child(min_y_value);
  869. max_y_value->set_max(10000);
  870. max_y_value->set_min(0.01);
  871. max_y_value->set_step(0.01);
  872. min_y_value->set_min(-10000);
  873. min_y_value->set_max(0);
  874. min_y_value->set_step(0.01);
  875. }
  876. panel = memnew(PanelContainer);
  877. panel->set_clip_contents(true);
  878. main_grid->add_child(panel);
  879. panel->set_h_size_flags(SIZE_EXPAND_FILL);
  880. blend_space_draw = memnew(Control);
  881. blend_space_draw->connect(SceneStringName(gui_input), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_blend_space_gui_input));
  882. blend_space_draw->connect(SceneStringName(draw), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_blend_space_draw));
  883. blend_space_draw->set_focus_mode(FOCUS_ALL);
  884. panel->add_child(blend_space_draw);
  885. main_grid->add_child(memnew(Control)); //empty bottom left
  886. {
  887. HBoxContainer *bottom_vbox = memnew(HBoxContainer);
  888. main_grid->add_child(bottom_vbox);
  889. bottom_vbox->set_h_size_flags(SIZE_EXPAND_FILL);
  890. min_x_value = memnew(SpinBox);
  891. bottom_vbox->add_child(min_x_value);
  892. bottom_vbox->add_spacer();
  893. label_x = memnew(LineEdit);
  894. bottom_vbox->add_child(label_x);
  895. label_x->set_expand_to_text_length_enabled(true);
  896. bottom_vbox->add_spacer();
  897. max_x_value = memnew(SpinBox);
  898. bottom_vbox->add_child(max_x_value);
  899. max_x_value->set_max(10000);
  900. max_x_value->set_min(0.01);
  901. max_x_value->set_step(0.01);
  902. min_x_value->set_min(-10000);
  903. min_x_value->set_max(0);
  904. min_x_value->set_step(0.01);
  905. }
  906. snap_x->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_config_changed));
  907. snap_y->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_config_changed));
  908. max_x_value->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_config_changed));
  909. min_x_value->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_config_changed));
  910. max_y_value->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_config_changed));
  911. min_y_value->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_config_changed));
  912. label_x->connect(SceneStringName(text_changed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_labels_changed));
  913. label_y->connect(SceneStringName(text_changed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_labels_changed));
  914. error_panel = memnew(PanelContainer);
  915. add_child(error_panel);
  916. error_label = memnew(Label);
  917. error_panel->add_child(error_label);
  918. set_custom_minimum_size(Size2(0, 300 * EDSCALE));
  919. menu = memnew(PopupMenu);
  920. add_child(menu);
  921. menu->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_add_menu_type));
  922. animations_menu = memnew(PopupMenu);
  923. animations_menu->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  924. menu->add_child(animations_menu);
  925. animations_menu->connect("index_pressed", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_add_animation_type));
  926. open_file = memnew(EditorFileDialog);
  927. add_child(open_file);
  928. open_file->set_title(TTR("Open Animation Node"));
  929. open_file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  930. open_file->connect("file_selected", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_file_opened));
  931. selected_point = -1;
  932. selected_triangle = -1;
  933. dragging_selected = false;
  934. dragging_selected_attempt = false;
  935. }