animation_blend_space_2d_editor.cpp 42 KB

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