animation_blend_space_2d_editor.cpp 43 KB

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