animation_blend_space_1d_editor.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. /**************************************************************************/
  2. /* animation_blend_space_1d_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_1d_editor.h"
  31. #include "core/os/keyboard.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_settings.h"
  34. #include "editor/editor_string_names.h"
  35. #include "editor/editor_undo_redo_manager.h"
  36. #include "editor/gui/editor_file_dialog.h"
  37. #include "editor/themes/editor_scale.h"
  38. #include "scene/animation/animation_blend_tree.h"
  39. #include "scene/gui/button.h"
  40. #include "scene/gui/check_box.h"
  41. #include "scene/gui/line_edit.h"
  42. #include "scene/gui/option_button.h"
  43. #include "scene/gui/panel_container.h"
  44. #include "scene/gui/separator.h"
  45. #include "scene/gui/spin_box.h"
  46. StringName AnimationNodeBlendSpace1DEditor::get_blend_position_path() const {
  47. StringName path = AnimationTreeEditor::get_singleton()->get_base_path() + "blend_position";
  48. return path;
  49. }
  50. void AnimationNodeBlendSpace1DEditor::_blend_space_gui_input(const Ref<InputEvent> &p_event) {
  51. AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
  52. if (!tree) {
  53. return;
  54. }
  55. Ref<InputEventKey> k = p_event;
  56. if (tool_select->is_pressed() && k.is_valid() && k->is_pressed() && k->get_keycode() == Key::KEY_DELETE && !k->is_echo()) {
  57. if (selected_point != -1) {
  58. if (!read_only) {
  59. _erase_selected();
  60. }
  61. accept_event();
  62. }
  63. }
  64. Ref<InputEventMouseButton> mb = p_event;
  65. 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()))) {
  66. if (!read_only) {
  67. menu->clear(false);
  68. animations_menu->clear();
  69. animations_to_add.clear();
  70. List<StringName> classes;
  71. ClassDB::get_inheriters_from_class("AnimationRootNode", &classes);
  72. classes.sort_custom<StringName::AlphCompare>();
  73. menu->add_submenu_node_item(TTR("Add Animation"), animations_menu);
  74. List<StringName> names;
  75. tree->get_animation_list(&names);
  76. for (const StringName &E : names) {
  77. animations_menu->add_icon_item(get_editor_theme_icon(SNAME("Animation")), E);
  78. animations_to_add.push_back(E);
  79. }
  80. for (const StringName &E : classes) {
  81. String name = String(E).replace_first("AnimationNode", "");
  82. if (name == "Animation" || name == "StartState" || name == "EndState") {
  83. continue;
  84. }
  85. int idx = menu->get_item_count();
  86. menu->add_item(vformat(TTR("Add %s"), name), idx);
  87. menu->set_item_metadata(idx, E);
  88. }
  89. Ref<AnimationNode> clipb = EditorSettings::get_singleton()->get_resource_clipboard();
  90. if (clipb.is_valid()) {
  91. menu->add_separator();
  92. menu->add_item(TTR("Paste"), MENU_PASTE);
  93. }
  94. menu->add_separator();
  95. menu->add_item(TTR("Load..."), MENU_LOAD_FILE);
  96. menu->set_position(blend_space_draw->get_screen_position() + mb->get_position());
  97. menu->reset_size();
  98. menu->popup();
  99. add_point_pos = (mb->get_position() / blend_space_draw->get_size()).x;
  100. add_point_pos *= (blend_space->get_max_space() - blend_space->get_min_space());
  101. add_point_pos += blend_space->get_min_space();
  102. if (snap->is_pressed()) {
  103. add_point_pos = Math::snapped(add_point_pos, blend_space->get_snap());
  104. }
  105. }
  106. }
  107. if (mb.is_valid() && mb->is_pressed() && tool_select->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  108. blend_space_draw->queue_redraw(); // why not
  109. // try to see if a point can be selected
  110. selected_point = -1;
  111. _update_tool_erase();
  112. for (int i = 0; i < points.size(); i++) {
  113. if (Math::abs(float(points[i] - mb->get_position().x)) < 10 * EDSCALE) {
  114. selected_point = i;
  115. Ref<AnimationNode> node = blend_space->get_blend_point_node(i);
  116. EditorNode::get_singleton()->push_item(node.ptr(), "", true);
  117. dragging_selected_attempt = true;
  118. drag_from = mb->get_position();
  119. _update_tool_erase();
  120. _update_edited_point_pos();
  121. return;
  122. }
  123. }
  124. }
  125. if (mb.is_valid() && !mb->is_pressed() && dragging_selected_attempt && mb->get_button_index() == MouseButton::LEFT) {
  126. if (!read_only) {
  127. if (dragging_selected) {
  128. // move
  129. float point = blend_space->get_blend_point_position(selected_point);
  130. point += drag_ofs.x;
  131. if (snap->is_pressed()) {
  132. point = Math::snapped(point, blend_space->get_snap());
  133. }
  134. updating = true;
  135. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  136. undo_redo->create_action(TTR("Move Node Point"));
  137. undo_redo->add_do_method(blend_space.ptr(), "set_blend_point_position", selected_point, point);
  138. undo_redo->add_undo_method(blend_space.ptr(), "set_blend_point_position", selected_point, blend_space->get_blend_point_position(selected_point));
  139. undo_redo->add_do_method(this, "_update_space");
  140. undo_redo->add_undo_method(this, "_update_space");
  141. undo_redo->add_do_method(this, "_update_edited_point_pos");
  142. undo_redo->add_undo_method(this, "_update_edited_point_pos");
  143. undo_redo->commit_action();
  144. updating = false;
  145. _update_edited_point_pos();
  146. }
  147. dragging_selected_attempt = false;
  148. dragging_selected = false;
  149. blend_space_draw->queue_redraw();
  150. }
  151. }
  152. // *set* the blend
  153. if (mb.is_valid() && !mb->is_pressed() && tool_blend->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  154. float blend_pos = mb->get_position().x / blend_space_draw->get_size().x;
  155. blend_pos *= blend_space->get_max_space() - blend_space->get_min_space();
  156. blend_pos += blend_space->get_min_space();
  157. tree->set(get_blend_position_path(), blend_pos);
  158. blend_space_draw->queue_redraw();
  159. }
  160. Ref<InputEventMouseMotion> mm = p_event;
  161. if (mm.is_valid() && !blend_space_draw->has_focus()) {
  162. blend_space_draw->grab_focus();
  163. blend_space_draw->queue_redraw();
  164. }
  165. if (mm.is_valid() && dragging_selected_attempt) {
  166. dragging_selected = true;
  167. drag_ofs = ((mm->get_position() - drag_from) / blend_space_draw->get_size()) * ((blend_space->get_max_space() - blend_space->get_min_space()) * Vector2(1, 0));
  168. blend_space_draw->queue_redraw();
  169. _update_edited_point_pos();
  170. }
  171. if (mm.is_valid() && tool_blend->is_pressed() && (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
  172. float blend_pos = mm->get_position().x / blend_space_draw->get_size().x;
  173. blend_pos *= blend_space->get_max_space() - blend_space->get_min_space();
  174. blend_pos += blend_space->get_min_space();
  175. tree->set(get_blend_position_path(), blend_pos);
  176. blend_space_draw->queue_redraw();
  177. }
  178. }
  179. void AnimationNodeBlendSpace1DEditor::_blend_space_draw() {
  180. AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
  181. if (!tree) {
  182. return;
  183. }
  184. Color linecolor = get_theme_color(SceneStringName(font_color), SNAME("Label"));
  185. Color linecolor_soft = linecolor;
  186. linecolor_soft.a *= 0.5;
  187. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
  188. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
  189. Ref<Texture2D> icon = get_editor_theme_icon(SNAME("KeyValue"));
  190. Ref<Texture2D> icon_selected = get_editor_theme_icon(SNAME("KeySelected"));
  191. Size2 s = blend_space_draw->get_size();
  192. if (blend_space_draw->has_focus()) {
  193. Color color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  194. blend_space_draw->draw_rect(Rect2(Point2(), s), color, false);
  195. }
  196. blend_space_draw->draw_line(Point2(1, s.height - 1), Point2(s.width - 1, s.height - 1), linecolor, Math::round(EDSCALE));
  197. if (blend_space->get_min_space() < 0) {
  198. float point = 0.0;
  199. point = (point - blend_space->get_min_space()) / (blend_space->get_max_space() - blend_space->get_min_space());
  200. point *= s.width;
  201. float x = point;
  202. blend_space_draw->draw_line(Point2(x, s.height - 1), Point2(x, s.height - 5 * EDSCALE), linecolor, Math::round(EDSCALE));
  203. 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);
  204. blend_space_draw->draw_line(Point2(x, s.height - 5 * EDSCALE), Point2(x, 0), linecolor_soft, Math::round(EDSCALE));
  205. }
  206. if (snap->is_pressed()) {
  207. linecolor_soft.a = linecolor.a * 0.1;
  208. if (blend_space->get_snap() > 0) {
  209. int prev_idx = -1;
  210. for (int i = 0; i < s.x; i++) {
  211. float v = blend_space->get_min_space() + i * (blend_space->get_max_space() - blend_space->get_min_space()) / s.x;
  212. int idx = int(v / blend_space->get_snap());
  213. if (i > 0 && prev_idx != idx) {
  214. blend_space_draw->draw_line(Point2(i, 0), Point2(i, s.height), linecolor_soft, Math::round(EDSCALE));
  215. }
  216. prev_idx = idx;
  217. }
  218. }
  219. }
  220. points.clear();
  221. for (int i = 0; i < blend_space->get_blend_point_count(); i++) {
  222. float point = blend_space->get_blend_point_position(i);
  223. if (!read_only) {
  224. if (dragging_selected && selected_point == i) {
  225. point += drag_ofs.x;
  226. if (snap->is_pressed()) {
  227. point = Math::snapped(point, blend_space->get_snap());
  228. }
  229. }
  230. }
  231. point = (point - blend_space->get_min_space()) / (blend_space->get_max_space() - blend_space->get_min_space());
  232. point *= s.width;
  233. points.push_back(point);
  234. Vector2 gui_point = Vector2(point, s.height / 2.0);
  235. gui_point -= (icon->get_size() / 2.0);
  236. gui_point = gui_point.floor();
  237. if (i == selected_point) {
  238. blend_space_draw->draw_texture(icon_selected, gui_point);
  239. } else {
  240. blend_space_draw->draw_texture(icon, gui_point);
  241. }
  242. }
  243. // blend position
  244. {
  245. Color color;
  246. if (tool_blend->is_pressed()) {
  247. color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  248. } else {
  249. color = linecolor;
  250. color.a *= 0.5;
  251. }
  252. float point = tree->get(get_blend_position_path());
  253. point = (point - blend_space->get_min_space()) / (blend_space->get_max_space() - blend_space->get_min_space());
  254. point *= s.width;
  255. Vector2 gui_point = Vector2(point, s.height / 2.0);
  256. float mind = 5 * EDSCALE;
  257. float maxd = 15 * EDSCALE;
  258. blend_space_draw->draw_line(gui_point + Vector2(mind, 0), gui_point + Vector2(maxd, 0), color, Math::round(2 * EDSCALE));
  259. blend_space_draw->draw_line(gui_point + Vector2(-mind, 0), gui_point + Vector2(-maxd, 0), color, Math::round(2 * EDSCALE));
  260. blend_space_draw->draw_line(gui_point + Vector2(0, mind), gui_point + Vector2(0, maxd), color, Math::round(2 * EDSCALE));
  261. blend_space_draw->draw_line(gui_point + Vector2(0, -mind), gui_point + Vector2(0, -maxd), color, Math::round(2 * EDSCALE));
  262. }
  263. }
  264. void AnimationNodeBlendSpace1DEditor::_update_space() {
  265. if (updating) {
  266. return;
  267. }
  268. updating = true;
  269. max_value->set_value(blend_space->get_max_space());
  270. min_value->set_value(blend_space->get_min_space());
  271. sync->set_pressed(blend_space->is_using_sync());
  272. interpolation->select(blend_space->get_blend_mode());
  273. label_value->set_text(blend_space->get_value_label());
  274. snap_value->set_value(blend_space->get_snap());
  275. blend_space_draw->queue_redraw();
  276. updating = false;
  277. }
  278. void AnimationNodeBlendSpace1DEditor::_config_changed(double) {
  279. if (updating) {
  280. return;
  281. }
  282. updating = true;
  283. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  284. undo_redo->create_action(TTR("Change BlendSpace1D Config"));
  285. undo_redo->add_do_method(blend_space.ptr(), "set_max_space", max_value->get_value());
  286. undo_redo->add_undo_method(blend_space.ptr(), "set_max_space", blend_space->get_max_space());
  287. undo_redo->add_do_method(blend_space.ptr(), "set_min_space", min_value->get_value());
  288. undo_redo->add_undo_method(blend_space.ptr(), "set_min_space", blend_space->get_min_space());
  289. undo_redo->add_do_method(blend_space.ptr(), "set_snap", snap_value->get_value());
  290. undo_redo->add_undo_method(blend_space.ptr(), "set_snap", blend_space->get_snap());
  291. undo_redo->add_do_method(blend_space.ptr(), "set_use_sync", sync->is_pressed());
  292. undo_redo->add_undo_method(blend_space.ptr(), "set_use_sync", blend_space->is_using_sync());
  293. undo_redo->add_do_method(blend_space.ptr(), "set_blend_mode", interpolation->get_selected());
  294. undo_redo->add_undo_method(blend_space.ptr(), "set_blend_mode", blend_space->get_blend_mode());
  295. undo_redo->add_do_method(this, "_update_space");
  296. undo_redo->add_undo_method(this, "_update_space");
  297. undo_redo->commit_action();
  298. updating = false;
  299. blend_space_draw->queue_redraw();
  300. }
  301. void AnimationNodeBlendSpace1DEditor::_labels_changed(String) {
  302. if (updating) {
  303. return;
  304. }
  305. updating = true;
  306. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  307. undo_redo->create_action(TTR("Change BlendSpace1D Labels"), UndoRedo::MERGE_ENDS);
  308. undo_redo->add_do_method(blend_space.ptr(), "set_value_label", label_value->get_text());
  309. undo_redo->add_undo_method(blend_space.ptr(), "set_value_label", blend_space->get_value_label());
  310. undo_redo->add_do_method(this, "_update_space");
  311. undo_redo->add_undo_method(this, "_update_space");
  312. undo_redo->commit_action();
  313. updating = false;
  314. }
  315. void AnimationNodeBlendSpace1DEditor::_snap_toggled() {
  316. blend_space_draw->queue_redraw();
  317. }
  318. void AnimationNodeBlendSpace1DEditor::_file_opened(const String &p_file) {
  319. file_loaded = ResourceLoader::load(p_file);
  320. if (file_loaded.is_valid()) {
  321. _add_menu_type(MENU_LOAD_FILE_CONFIRM);
  322. } else {
  323. EditorNode::get_singleton()->show_warning(TTR("This type of node can't be used. Only animation nodes are allowed."));
  324. }
  325. }
  326. void AnimationNodeBlendSpace1DEditor::_add_menu_type(int p_index) {
  327. Ref<AnimationRootNode> node;
  328. if (p_index == MENU_LOAD_FILE) {
  329. open_file->clear_filters();
  330. List<String> filters;
  331. ResourceLoader::get_recognized_extensions_for_type("AnimationRootNode", &filters);
  332. for (const String &E : filters) {
  333. open_file->add_filter("*." + E);
  334. }
  335. open_file->popup_file_dialog();
  336. return;
  337. } else if (p_index == MENU_LOAD_FILE_CONFIRM) {
  338. node = file_loaded;
  339. file_loaded.unref();
  340. } else if (p_index == MENU_PASTE) {
  341. node = EditorSettings::get_singleton()->get_resource_clipboard();
  342. } else {
  343. String type = menu->get_item_metadata(p_index);
  344. Object *obj = ClassDB::instantiate(type);
  345. ERR_FAIL_NULL(obj);
  346. AnimationNode *an = Object::cast_to<AnimationNode>(obj);
  347. ERR_FAIL_NULL(an);
  348. node = Ref<AnimationNode>(an);
  349. }
  350. if (!node.is_valid()) {
  351. EditorNode::get_singleton()->show_warning(TTR("This type of node can't be used. Only root nodes are allowed."));
  352. return;
  353. }
  354. updating = true;
  355. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  356. undo_redo->create_action(TTR("Add Node Point"));
  357. undo_redo->add_do_method(blend_space.ptr(), "add_blend_point", node, add_point_pos);
  358. undo_redo->add_undo_method(blend_space.ptr(), "remove_blend_point", blend_space->get_blend_point_count());
  359. undo_redo->add_do_method(this, "_update_space");
  360. undo_redo->add_undo_method(this, "_update_space");
  361. undo_redo->commit_action();
  362. updating = false;
  363. blend_space_draw->queue_redraw();
  364. }
  365. void AnimationNodeBlendSpace1DEditor::_add_animation_type(int p_index) {
  366. Ref<AnimationNodeAnimation> anim;
  367. anim.instantiate();
  368. anim->set_animation(animations_to_add[p_index]);
  369. updating = true;
  370. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  371. undo_redo->create_action(TTR("Add Animation Point"));
  372. undo_redo->add_do_method(blend_space.ptr(), "add_blend_point", anim, add_point_pos);
  373. undo_redo->add_undo_method(blend_space.ptr(), "remove_blend_point", blend_space->get_blend_point_count());
  374. undo_redo->add_do_method(this, "_update_space");
  375. undo_redo->add_undo_method(this, "_update_space");
  376. undo_redo->commit_action();
  377. updating = false;
  378. blend_space_draw->queue_redraw();
  379. }
  380. void AnimationNodeBlendSpace1DEditor::_tool_switch(int p_tool) {
  381. if (p_tool == 0) {
  382. tool_erase->show();
  383. tool_erase_sep->show();
  384. } else {
  385. tool_erase->hide();
  386. tool_erase_sep->hide();
  387. }
  388. _update_tool_erase();
  389. blend_space_draw->queue_redraw();
  390. }
  391. void AnimationNodeBlendSpace1DEditor::_update_edited_point_pos() {
  392. if (updating) {
  393. return;
  394. }
  395. if (selected_point >= 0 && selected_point < blend_space->get_blend_point_count()) {
  396. float pos = blend_space->get_blend_point_position(selected_point);
  397. if (dragging_selected) {
  398. pos += drag_ofs.x;
  399. if (snap->is_pressed()) {
  400. pos = Math::snapped(pos, blend_space->get_snap());
  401. }
  402. }
  403. updating = true;
  404. edit_value->set_value(pos);
  405. updating = false;
  406. }
  407. }
  408. void AnimationNodeBlendSpace1DEditor::_update_tool_erase() {
  409. bool point_valid = selected_point >= 0 && selected_point < blend_space->get_blend_point_count();
  410. tool_erase->set_disabled(!point_valid || read_only);
  411. if (point_valid) {
  412. Ref<AnimationNode> an = blend_space->get_blend_point_node(selected_point);
  413. if (AnimationTreeEditor::get_singleton()->can_edit(an)) {
  414. open_editor->show();
  415. } else {
  416. open_editor->hide();
  417. }
  418. if (!read_only) {
  419. edit_hb->show();
  420. } else {
  421. edit_hb->hide();
  422. }
  423. } else {
  424. edit_hb->hide();
  425. }
  426. }
  427. void AnimationNodeBlendSpace1DEditor::_erase_selected() {
  428. if (selected_point != -1) {
  429. updating = true;
  430. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  431. undo_redo->create_action(TTR("Remove BlendSpace1D Point"));
  432. undo_redo->add_do_method(blend_space.ptr(), "remove_blend_point", selected_point);
  433. 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);
  434. undo_redo->add_do_method(this, "_update_space");
  435. undo_redo->add_undo_method(this, "_update_space");
  436. undo_redo->commit_action();
  437. updating = false;
  438. blend_space_draw->queue_redraw();
  439. }
  440. }
  441. void AnimationNodeBlendSpace1DEditor::_edit_point_pos(double) {
  442. if (updating) {
  443. return;
  444. }
  445. updating = true;
  446. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  447. undo_redo->create_action(TTR("Move BlendSpace1D Node Point"));
  448. undo_redo->add_do_method(blend_space.ptr(), "set_blend_point_position", selected_point, edit_value->get_value());
  449. undo_redo->add_undo_method(blend_space.ptr(), "set_blend_point_position", selected_point, blend_space->get_blend_point_position(selected_point));
  450. undo_redo->add_do_method(this, "_update_space");
  451. undo_redo->add_undo_method(this, "_update_space");
  452. undo_redo->add_do_method(this, "_update_edited_point_pos");
  453. undo_redo->add_undo_method(this, "_update_edited_point_pos");
  454. undo_redo->commit_action();
  455. updating = false;
  456. blend_space_draw->queue_redraw();
  457. }
  458. void AnimationNodeBlendSpace1DEditor::_open_editor() {
  459. if (selected_point >= 0 && selected_point < blend_space->get_blend_point_count()) {
  460. Ref<AnimationNode> an = blend_space->get_blend_point_node(selected_point);
  461. ERR_FAIL_COND(an.is_null());
  462. AnimationTreeEditor::get_singleton()->enter_editor(itos(selected_point));
  463. }
  464. }
  465. void AnimationNodeBlendSpace1DEditor::_notification(int p_what) {
  466. switch (p_what) {
  467. case NOTIFICATION_ENTER_TREE:
  468. case NOTIFICATION_THEME_CHANGED: {
  469. error_panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));
  470. error_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  471. panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));
  472. tool_blend->set_button_icon(get_editor_theme_icon(SNAME("EditPivot")));
  473. tool_select->set_button_icon(get_editor_theme_icon(SNAME("ToolSelect")));
  474. tool_create->set_button_icon(get_editor_theme_icon(SNAME("EditKey")));
  475. tool_erase->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
  476. snap->set_button_icon(get_editor_theme_icon(SNAME("SnapGrid")));
  477. open_editor->set_button_icon(get_editor_theme_icon(SNAME("Edit")));
  478. interpolation->clear();
  479. interpolation->add_icon_item(get_editor_theme_icon(SNAME("TrackContinuous")), TTR("Continuous"), 0);
  480. interpolation->add_icon_item(get_editor_theme_icon(SNAME("TrackDiscrete")), TTR("Discrete"), 1);
  481. interpolation->add_icon_item(get_editor_theme_icon(SNAME("TrackCapture")), TTR("Capture"), 2);
  482. } break;
  483. case NOTIFICATION_PROCESS: {
  484. AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
  485. if (!tree) {
  486. return;
  487. }
  488. String error;
  489. if (!tree->is_active()) {
  490. error = TTR("AnimationTree is inactive.\nActivate to enable playback, check node warnings if activation fails.");
  491. } else if (tree->is_state_invalid()) {
  492. error = tree->get_invalid_state_reason();
  493. }
  494. if (error != error_label->get_text()) {
  495. error_label->set_text(error);
  496. if (!error.is_empty()) {
  497. error_panel->show();
  498. } else {
  499. error_panel->hide();
  500. }
  501. }
  502. } break;
  503. case NOTIFICATION_VISIBILITY_CHANGED: {
  504. set_process(is_visible_in_tree());
  505. } break;
  506. }
  507. }
  508. void AnimationNodeBlendSpace1DEditor::_bind_methods() {
  509. ClassDB::bind_method("_update_space", &AnimationNodeBlendSpace1DEditor::_update_space);
  510. ClassDB::bind_method("_update_tool_erase", &AnimationNodeBlendSpace1DEditor::_update_tool_erase);
  511. ClassDB::bind_method("_update_edited_point_pos", &AnimationNodeBlendSpace1DEditor::_update_edited_point_pos);
  512. }
  513. bool AnimationNodeBlendSpace1DEditor::can_edit(const Ref<AnimationNode> &p_node) {
  514. Ref<AnimationNodeBlendSpace1D> b1d = p_node;
  515. return b1d.is_valid();
  516. }
  517. void AnimationNodeBlendSpace1DEditor::edit(const Ref<AnimationNode> &p_node) {
  518. blend_space = p_node;
  519. read_only = false;
  520. if (!blend_space.is_null()) {
  521. read_only = EditorNode::get_singleton()->is_resource_read_only(blend_space);
  522. _update_space();
  523. }
  524. tool_create->set_disabled(read_only);
  525. edit_value->set_editable(!read_only);
  526. label_value->set_editable(!read_only);
  527. min_value->set_editable(!read_only);
  528. max_value->set_editable(!read_only);
  529. sync->set_disabled(read_only);
  530. interpolation->set_disabled(read_only);
  531. }
  532. AnimationNodeBlendSpace1DEditor *AnimationNodeBlendSpace1DEditor::singleton = nullptr;
  533. AnimationNodeBlendSpace1DEditor::AnimationNodeBlendSpace1DEditor() {
  534. singleton = this;
  535. HBoxContainer *top_hb = memnew(HBoxContainer);
  536. add_child(top_hb);
  537. Ref<ButtonGroup> bg;
  538. bg.instantiate();
  539. tool_blend = memnew(Button);
  540. tool_blend->set_theme_type_variation("FlatButton");
  541. tool_blend->set_toggle_mode(true);
  542. tool_blend->set_button_group(bg);
  543. top_hb->add_child(tool_blend);
  544. tool_blend->set_pressed(true);
  545. tool_blend->set_tooltip_text(TTR("Set the blending position within the space"));
  546. tool_blend->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_tool_switch).bind(3));
  547. tool_select = memnew(Button);
  548. tool_select->set_theme_type_variation("FlatButton");
  549. tool_select->set_toggle_mode(true);
  550. tool_select->set_button_group(bg);
  551. top_hb->add_child(tool_select);
  552. tool_select->set_tooltip_text(TTR("Select and move points, create points with RMB."));
  553. tool_select->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_tool_switch).bind(0));
  554. tool_create = memnew(Button);
  555. tool_create->set_theme_type_variation("FlatButton");
  556. tool_create->set_toggle_mode(true);
  557. tool_create->set_button_group(bg);
  558. top_hb->add_child(tool_create);
  559. tool_create->set_tooltip_text(TTR("Create points."));
  560. tool_create->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_tool_switch).bind(1));
  561. tool_erase_sep = memnew(VSeparator);
  562. top_hb->add_child(tool_erase_sep);
  563. tool_erase = memnew(Button);
  564. tool_erase->set_theme_type_variation("FlatButton");
  565. top_hb->add_child(tool_erase);
  566. tool_erase->set_tooltip_text(TTR("Erase points."));
  567. tool_erase->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_erase_selected));
  568. top_hb->add_child(memnew(VSeparator));
  569. snap = memnew(Button);
  570. snap->set_theme_type_variation("FlatButton");
  571. snap->set_toggle_mode(true);
  572. top_hb->add_child(snap);
  573. snap->set_pressed(true);
  574. snap->set_tooltip_text(TTR("Enable snap and show grid."));
  575. snap->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_snap_toggled));
  576. snap_value = memnew(SpinBox);
  577. top_hb->add_child(snap_value);
  578. snap_value->set_min(0.01);
  579. snap_value->set_step(0.01);
  580. snap_value->set_max(1000);
  581. top_hb->add_child(memnew(VSeparator));
  582. top_hb->add_child(memnew(Label(TTR("Sync:"))));
  583. sync = memnew(CheckBox);
  584. top_hb->add_child(sync);
  585. sync->connect(SceneStringName(toggled), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_config_changed));
  586. top_hb->add_child(memnew(VSeparator));
  587. top_hb->add_child(memnew(Label(TTR("Blend:"))));
  588. interpolation = memnew(OptionButton);
  589. top_hb->add_child(interpolation);
  590. interpolation->connect(SceneStringName(item_selected), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_config_changed));
  591. edit_hb = memnew(HBoxContainer);
  592. top_hb->add_child(edit_hb);
  593. edit_hb->add_child(memnew(VSeparator));
  594. edit_hb->add_child(memnew(Label(TTR("Point"))));
  595. edit_value = memnew(SpinBox);
  596. edit_hb->add_child(edit_value);
  597. edit_value->set_min(-1000);
  598. edit_value->set_max(1000);
  599. edit_value->set_step(0.01);
  600. edit_value->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_edit_point_pos));
  601. open_editor = memnew(Button);
  602. edit_hb->add_child(open_editor);
  603. open_editor->set_text(TTR("Open Editor"));
  604. open_editor->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_open_editor), CONNECT_DEFERRED);
  605. edit_hb->hide();
  606. open_editor->hide();
  607. VBoxContainer *main_vb = memnew(VBoxContainer);
  608. add_child(main_vb);
  609. main_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  610. panel = memnew(PanelContainer);
  611. panel->set_clip_contents(true);
  612. main_vb->add_child(panel);
  613. panel->set_h_size_flags(SIZE_EXPAND_FILL);
  614. panel->set_v_size_flags(SIZE_EXPAND_FILL);
  615. blend_space_draw = memnew(Control);
  616. blend_space_draw->connect(SceneStringName(gui_input), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_blend_space_gui_input));
  617. blend_space_draw->connect(SceneStringName(draw), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_blend_space_draw));
  618. blend_space_draw->set_focus_mode(FOCUS_ALL);
  619. panel->add_child(blend_space_draw);
  620. {
  621. HBoxContainer *bottom_hb = memnew(HBoxContainer);
  622. main_vb->add_child(bottom_hb);
  623. bottom_hb->set_h_size_flags(SIZE_EXPAND_FILL);
  624. min_value = memnew(SpinBox);
  625. min_value->set_min(-10000);
  626. min_value->set_max(0);
  627. min_value->set_step(0.01);
  628. max_value = memnew(SpinBox);
  629. max_value->set_min(0.01);
  630. max_value->set_max(10000);
  631. max_value->set_step(0.01);
  632. label_value = memnew(LineEdit);
  633. label_value->set_expand_to_text_length_enabled(true);
  634. // now add
  635. bottom_hb->add_child(min_value);
  636. bottom_hb->add_spacer();
  637. bottom_hb->add_child(label_value);
  638. bottom_hb->add_spacer();
  639. bottom_hb->add_child(max_value);
  640. }
  641. snap_value->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_config_changed));
  642. min_value->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_config_changed));
  643. max_value->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_config_changed));
  644. label_value->connect(SceneStringName(text_changed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_labels_changed));
  645. error_panel = memnew(PanelContainer);
  646. add_child(error_panel);
  647. error_label = memnew(Label);
  648. error_panel->add_child(error_label);
  649. menu = memnew(PopupMenu);
  650. add_child(menu);
  651. menu->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_add_menu_type));
  652. animations_menu = memnew(PopupMenu);
  653. animations_menu->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  654. menu->add_child(animations_menu);
  655. animations_menu->connect("index_pressed", callable_mp(this, &AnimationNodeBlendSpace1DEditor::_add_animation_type));
  656. open_file = memnew(EditorFileDialog);
  657. add_child(open_file);
  658. open_file->set_title(TTR("Open Animation Node"));
  659. open_file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  660. open_file->connect("file_selected", callable_mp(this, &AnimationNodeBlendSpace1DEditor::_file_opened));
  661. set_custom_minimum_size(Size2(0, 150 * EDSCALE));
  662. }