Path: blob/master/editor/animation/animation_blend_space_2d_editor.cpp
9902 views
/**************************************************************************/1/* animation_blend_space_2d_editor.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "animation_blend_space_2d_editor.h"3132#include "core/io/resource_loader.h"33#include "core/math/geometry_2d.h"34#include "core/os/keyboard.h"35#include "editor/editor_node.h"36#include "editor/editor_string_names.h"37#include "editor/editor_undo_redo_manager.h"38#include "editor/gui/editor_file_dialog.h"39#include "editor/settings/editor_settings.h"40#include "editor/themes/editor_scale.h"41#include "scene/animation/animation_blend_tree.h"42#include "scene/animation/animation_player.h"43#include "scene/gui/button.h"44#include "scene/gui/check_box.h"45#include "scene/gui/grid_container.h"46#include "scene/gui/line_edit.h"47#include "scene/gui/menu_button.h"48#include "scene/gui/option_button.h"49#include "scene/gui/panel.h"50#include "scene/gui/panel_container.h"51#include "scene/gui/separator.h"52#include "scene/gui/spin_box.h"53#include "scene/main/window.h"5455bool AnimationNodeBlendSpace2DEditor::can_edit(const Ref<AnimationNode> &p_node) {56Ref<AnimationNodeBlendSpace2D> bs2d = p_node;57return bs2d.is_valid();58}5960void AnimationNodeBlendSpace2DEditor::_blend_space_changed() {61blend_space_draw->queue_redraw();62}6364void AnimationNodeBlendSpace2DEditor::edit(const Ref<AnimationNode> &p_node) {65if (blend_space.is_valid()) {66blend_space->disconnect("triangles_updated", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_blend_space_changed));67}68blend_space = p_node;69read_only = false;7071if (blend_space.is_valid()) {72read_only = EditorNode::get_singleton()->is_resource_read_only(blend_space);7374blend_space->connect("triangles_updated", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_blend_space_changed));75_update_space();76}7778tool_create->set_disabled(read_only);79max_x_value->set_editable(!read_only);80min_x_value->set_editable(!read_only);81max_y_value->set_editable(!read_only);82min_y_value->set_editable(!read_only);83label_x->set_editable(!read_only);84label_y->set_editable(!read_only);85edit_x->set_editable(!read_only);86edit_y->set_editable(!read_only);87tool_triangle->set_disabled(read_only);88auto_triangles->set_disabled(read_only);89sync->set_disabled(read_only);90interpolation->set_disabled(read_only);91}9293StringName AnimationNodeBlendSpace2DEditor::get_blend_position_path() const {94StringName path = AnimationTreeEditor::get_singleton()->get_base_path() + "blend_position";95return path;96}9798void AnimationNodeBlendSpace2DEditor::_blend_space_gui_input(const Ref<InputEvent> &p_event) {99AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();100if (!tree) {101return;102}103104Ref<InputEventKey> k = p_event;105if (tool_select->is_pressed() && k.is_valid() && k->is_pressed() && k->get_keycode() == Key::KEY_DELETE && !k->is_echo()) {106if (selected_point != -1 || selected_triangle != -1) {107if (!read_only) {108_erase_selected();109}110accept_event();111}112}113114Ref<InputEventMouseButton> mb = p_event;115116if (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()))) {117if (!read_only) {118menu->clear(false);119animations_menu->clear();120animations_to_add.clear();121122LocalVector<StringName> classes;123ClassDB::get_inheriters_from_class("AnimationRootNode", classes);124classes.sort_custom<StringName::AlphCompare>();125126menu->add_submenu_node_item(TTR("Add Animation"), animations_menu);127128List<StringName> names;129tree->get_animation_list(&names);130for (const StringName &E : names) {131animations_menu->add_icon_item(get_editor_theme_icon(SNAME("Animation")), E);132animations_to_add.push_back(E);133}134135for (const StringName &E : classes) {136String name = String(E).replace_first("AnimationNode", "");137if (name == "Animation" || name == "StartState" || name == "EndState") {138continue; // nope139}140int idx = menu->get_item_count();141menu->add_item(vformat(TTR("Add %s"), name), idx);142menu->set_item_metadata(idx, E);143}144145Ref<AnimationNode> clipb = EditorSettings::get_singleton()->get_resource_clipboard();146if (clipb.is_valid()) {147menu->add_separator();148menu->add_item(TTR("Paste"), MENU_PASTE);149}150menu->add_separator();151menu->add_item(TTR("Load..."), MENU_LOAD_FILE);152153menu->set_position(blend_space_draw->get_screen_position() + mb->get_position());154menu->reset_size();155menu->popup();156add_point_pos = (mb->get_position() / blend_space_draw->get_size());157add_point_pos.y = 1.0 - add_point_pos.y;158add_point_pos *= (blend_space->get_max_space() - blend_space->get_min_space());159add_point_pos += blend_space->get_min_space();160161if (snap->is_pressed()) {162add_point_pos = add_point_pos.snapped(blend_space->get_snap());163}164}165}166167if (mb.is_valid() && mb->is_pressed() && tool_select->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {168blend_space_draw->queue_redraw(); //update anyway169//try to see if a point can be selected170selected_point = -1;171selected_triangle = -1;172_update_tool_erase();173174for (int i = 0; i < points.size(); i++) {175if (points[i].distance_to(mb->get_position()) < 10 * EDSCALE) {176selected_point = i;177Ref<AnimationNode> node = blend_space->get_blend_point_node(i);178EditorNode::get_singleton()->push_item(node.ptr(), "", true);179dragging_selected_attempt = true;180drag_from = mb->get_position();181_update_tool_erase();182_update_edited_point_pos();183return;184}185}186187//then try to see if a triangle can be selected188if (!blend_space->get_auto_triangles()) { //if autotriangles use, disable this189for (int i = 0; i < blend_space->get_triangle_count(); i++) {190Vector<Vector2> triangle;191192for (int j = 0; j < 3; j++) {193int idx = blend_space->get_triangle_point(i, j);194ERR_FAIL_INDEX(idx, points.size());195triangle.push_back(points[idx]);196}197198if (Geometry2D::is_point_in_triangle(mb->get_position(), triangle[0], triangle[1], triangle[2])) {199selected_triangle = i;200_update_tool_erase();201return;202}203}204}205}206207if (mb.is_valid() && mb->is_pressed() && tool_triangle->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {208blend_space_draw->queue_redraw(); //update anyway209//try to see if a point can be selected210selected_point = -1;211212for (int i = 0; i < points.size(); i++) {213if (making_triangle.has(i)) {214continue;215}216217if (points[i].distance_to(mb->get_position()) < 10 * EDSCALE) {218making_triangle.push_back(i);219if (making_triangle.size() == 3) {220//add triangle!221if (blend_space->has_triangle(making_triangle[0], making_triangle[1], making_triangle[2])) {222making_triangle.clear();223EditorNode::get_singleton()->show_warning(TTR("Triangle already exists."));224return;225}226227updating = true;228EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();229undo_redo->create_action(TTR("Add Triangle"));230undo_redo->add_do_method(blend_space.ptr(), "add_triangle", making_triangle[0], making_triangle[1], making_triangle[2]);231undo_redo->add_undo_method(blend_space.ptr(), "remove_triangle", blend_space->get_triangle_count());232undo_redo->add_do_method(this, "_update_space");233undo_redo->add_undo_method(this, "_update_space");234undo_redo->commit_action();235updating = false;236making_triangle.clear();237}238return;239}240}241}242243if (mb.is_valid() && !mb->is_pressed() && dragging_selected_attempt && mb->get_button_index() == MouseButton::LEFT) {244if (dragging_selected) {245//move246Vector2 point = blend_space->get_blend_point_position(selected_point);247point += drag_ofs;248if (snap->is_pressed()) {249point = point.snapped(blend_space->get_snap());250}251252if (!read_only) {253updating = true;254EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();255undo_redo->create_action(TTR("Move Node Point"));256undo_redo->add_do_method(blend_space.ptr(), "set_blend_point_position", selected_point, point);257undo_redo->add_undo_method(blend_space.ptr(), "set_blend_point_position", selected_point, blend_space->get_blend_point_position(selected_point));258undo_redo->add_do_method(this, "_update_space");259undo_redo->add_undo_method(this, "_update_space");260undo_redo->add_do_method(this, "_update_edited_point_pos");261undo_redo->add_undo_method(this, "_update_edited_point_pos");262undo_redo->commit_action();263updating = false;264}265}266dragging_selected_attempt = false;267dragging_selected = false;268blend_space_draw->queue_redraw();269}270271if (mb.is_valid() && mb->is_pressed() && tool_blend->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {272Vector2 blend_pos = (mb->get_position() / blend_space_draw->get_size());273blend_pos.y = 1.0 - blend_pos.y;274blend_pos *= (blend_space->get_max_space() - blend_space->get_min_space());275blend_pos += blend_space->get_min_space();276277tree->set(get_blend_position_path(), blend_pos);278279blend_space_draw->queue_redraw();280}281282Ref<InputEventMouseMotion> mm = p_event;283284if (mm.is_valid() && dragging_selected_attempt) {285dragging_selected = true;286if (!read_only) {287drag_ofs = ((mm->get_position() - drag_from) / blend_space_draw->get_size()) * (blend_space->get_max_space() - blend_space->get_min_space()) * Vector2(1, -1);288}289blend_space_draw->queue_redraw();290_update_edited_point_pos();291}292293if (mm.is_valid() && tool_triangle->is_pressed() && making_triangle.size()) {294blend_space_draw->queue_redraw();295}296297if (mm.is_valid() && !tool_triangle->is_pressed() && making_triangle.size()) {298making_triangle.clear();299blend_space_draw->queue_redraw();300}301302if (mm.is_valid() && tool_blend->is_pressed() && (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {303Vector2 blend_pos = (mm->get_position() / blend_space_draw->get_size());304blend_pos.y = 1.0 - blend_pos.y;305blend_pos *= (blend_space->get_max_space() - blend_space->get_min_space());306blend_pos += blend_space->get_min_space();307308tree->set(get_blend_position_path(), blend_pos);309310blend_space_draw->queue_redraw();311}312}313314void AnimationNodeBlendSpace2DEditor::_file_opened(const String &p_file) {315file_loaded = ResourceLoader::load(p_file);316if (file_loaded.is_valid()) {317_add_menu_type(MENU_LOAD_FILE_CONFIRM);318} else {319EditorNode::get_singleton()->show_warning(TTR("This type of node can't be used. Only animation nodes are allowed."));320}321}322323void AnimationNodeBlendSpace2DEditor::_add_menu_type(int p_index) {324Ref<AnimationRootNode> node;325if (p_index == MENU_LOAD_FILE) {326open_file->clear_filters();327List<String> filters;328ResourceLoader::get_recognized_extensions_for_type("AnimationRootNode", &filters);329for (const String &E : filters) {330open_file->add_filter("*." + E);331}332open_file->popup_file_dialog();333return;334} else if (p_index == MENU_LOAD_FILE_CONFIRM) {335node = file_loaded;336file_loaded.unref();337} else if (p_index == MENU_PASTE) {338node = EditorSettings::get_singleton()->get_resource_clipboard();339} else {340String type = menu->get_item_metadata(p_index);341342Object *obj = ClassDB::instantiate(type);343ERR_FAIL_NULL(obj);344AnimationNode *an = Object::cast_to<AnimationNode>(obj);345ERR_FAIL_NULL(an);346347node = Ref<AnimationNode>(an);348}349350if (node.is_null()) {351EditorNode::get_singleton()->show_warning(TTR("This type of node can't be used. Only root nodes are allowed."));352return;353}354355updating = true;356EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();357undo_redo->create_action(TTR("Add Node Point"));358undo_redo->add_do_method(blend_space.ptr(), "add_blend_point", node, add_point_pos);359undo_redo->add_undo_method(blend_space.ptr(), "remove_blend_point", blend_space->get_blend_point_count());360undo_redo->add_do_method(this, "_update_space");361undo_redo->add_undo_method(this, "_update_space");362undo_redo->commit_action();363updating = false;364365blend_space_draw->queue_redraw();366}367368void AnimationNodeBlendSpace2DEditor::_add_animation_type(int p_index) {369Ref<AnimationNodeAnimation> anim;370anim.instantiate();371372anim->set_animation(animations_to_add[p_index]);373374updating = true;375EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();376undo_redo->create_action(TTR("Add Animation Point"));377undo_redo->add_do_method(blend_space.ptr(), "add_blend_point", anim, add_point_pos);378undo_redo->add_undo_method(blend_space.ptr(), "remove_blend_point", blend_space->get_blend_point_count());379undo_redo->add_do_method(this, "_update_space");380undo_redo->add_undo_method(this, "_update_space");381undo_redo->commit_action();382updating = false;383384blend_space_draw->queue_redraw();385}386387void AnimationNodeBlendSpace2DEditor::_update_tool_erase() {388tool_erase->set_disabled(389(!(selected_point >= 0 && selected_point < blend_space->get_blend_point_count()) && !(selected_triangle >= 0 && selected_triangle < blend_space->get_triangle_count())) ||390read_only);391392if (selected_point >= 0 && selected_point < blend_space->get_blend_point_count()) {393Ref<AnimationNode> an = blend_space->get_blend_point_node(selected_point);394if (AnimationTreeEditor::get_singleton()->can_edit(an)) {395open_editor->show();396} else {397open_editor->hide();398}399if (!read_only) {400edit_hb->show();401} else {402edit_hb->hide();403}404} else {405edit_hb->hide();406}407}408409void AnimationNodeBlendSpace2DEditor::_tool_switch(int p_tool) {410making_triangle.clear();411412if (p_tool == 2) {413Vector<Vector2> bl_points;414for (int i = 0; i < blend_space->get_blend_point_count(); i++) {415bl_points.push_back(blend_space->get_blend_point_position(i));416}417Vector<Delaunay2D::Triangle> tr = Delaunay2D::triangulate(bl_points);418for (int i = 0; i < tr.size(); i++) {419blend_space->add_triangle(tr[i].points[0], tr[i].points[1], tr[i].points[2]);420}421}422423if (p_tool == 0) {424tool_erase->show();425tool_erase_sep->show();426} else {427tool_erase->hide();428tool_erase_sep->hide();429}430_update_tool_erase();431blend_space_draw->queue_redraw();432}433434void AnimationNodeBlendSpace2DEditor::_blend_space_draw() {435AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();436if (!tree) {437return;438}439440Color linecolor = get_theme_color(SceneStringName(font_color), SNAME("Label"));441Color linecolor_soft = linecolor;442linecolor_soft.a *= 0.5;443Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));444int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));445Ref<Texture2D> icon = get_editor_theme_icon(SNAME("KeyValue"));446Ref<Texture2D> icon_selected = get_editor_theme_icon(SNAME("KeySelected"));447448Size2 s = blend_space_draw->get_size();449450if (blend_space_draw->has_focus()) {451Color color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));452blend_space_draw->draw_rect(Rect2(Point2(), s), color, false);453}454blend_space_draw->draw_line(Point2(1, 0), Point2(1, s.height - 1), linecolor, Math::round(EDSCALE));455blend_space_draw->draw_line(Point2(1, s.height - 1), Point2(s.width - 1, s.height - 1), linecolor, Math::round(EDSCALE));456457blend_space_draw->draw_line(Point2(0, 0), Point2(5 * EDSCALE, 0), linecolor, Math::round(EDSCALE));458if (blend_space->get_min_space().y < 0) {459int y = (blend_space->get_max_space().y / (blend_space->get_max_space().y - blend_space->get_min_space().y)) * s.height;460blend_space_draw->draw_line(Point2(0, y), Point2(5 * EDSCALE, y), linecolor, Math::round(EDSCALE));461blend_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);462blend_space_draw->draw_line(Point2(5 * EDSCALE, y), Point2(s.width, y), linecolor_soft, Math::round(EDSCALE));463}464465if (blend_space->get_min_space().x < 0) {466int x = (-blend_space->get_min_space().x / (blend_space->get_max_space().x - blend_space->get_min_space().x)) * s.width;467blend_space_draw->draw_line(Point2(x, s.height - 1), Point2(x, s.height - 5 * EDSCALE), linecolor, Math::round(EDSCALE));468blend_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);469blend_space_draw->draw_line(Point2(x, s.height - 5 * EDSCALE), Point2(x, 0), linecolor_soft, Math::round(EDSCALE));470}471472if (snap->is_pressed()) {473linecolor_soft.a = linecolor.a * 0.1;474475if (blend_space->get_snap().x > 0) {476int prev_idx = 0;477for (int i = 0; i < s.x; i++) {478float v = blend_space->get_min_space().x + i * (blend_space->get_max_space().x - blend_space->get_min_space().x) / s.x;479int idx = int(v / blend_space->get_snap().x);480481if (i > 0 && prev_idx != idx) {482blend_space_draw->draw_line(Point2(i, 0), Point2(i, s.height), linecolor_soft, Math::round(EDSCALE));483}484485prev_idx = idx;486}487}488489if (blend_space->get_snap().y > 0) {490int prev_idx = 0;491for (int i = 0; i < s.y; i++) {492float v = blend_space->get_max_space().y - i * (blend_space->get_max_space().y - blend_space->get_min_space().y) / s.y;493int idx = int(v / blend_space->get_snap().y);494495if (i > 0 && prev_idx != idx) {496blend_space_draw->draw_line(Point2(0, i), Point2(s.width, i), linecolor_soft, Math::round(EDSCALE));497}498499prev_idx = idx;500}501}502}503504//triangles first505for (int i = 0; i < blend_space->get_triangle_count(); i++) {506Vector<Vector2> bl_points;507bl_points.resize(3);508509for (int j = 0; j < 3; j++) {510int point_idx = blend_space->get_triangle_point(i, j);511Vector2 point = blend_space->get_blend_point_position(point_idx);512if (dragging_selected && selected_point == point_idx) {513point += drag_ofs;514if (snap->is_pressed()) {515point = point.snapped(blend_space->get_snap());516}517}518point = (point - blend_space->get_min_space()) / (blend_space->get_max_space() - blend_space->get_min_space());519point *= s;520point.y = s.height - point.y;521bl_points.write[j] = point;522}523524for (int j = 0; j < 3; j++) {525blend_space_draw->draw_line(bl_points[j], bl_points[(j + 1) % 3], linecolor, Math::round(EDSCALE), true);526}527528Color color;529if (i == selected_triangle) {530color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));531color.a *= 0.5;532} else {533color = linecolor;534color.a *= 0.2;535}536537Vector<Color> colors = {538color,539color,540color541};542blend_space_draw->draw_primitive(bl_points, colors, Vector<Vector2>());543}544545points.clear();546for (int i = 0; i < blend_space->get_blend_point_count(); i++) {547Vector2 point = blend_space->get_blend_point_position(i);548if (!read_only) {549if (dragging_selected && selected_point == i) {550point += drag_ofs;551if (snap->is_pressed()) {552point = point.snapped(blend_space->get_snap());553}554}555}556point = (point - blend_space->get_min_space()) / (blend_space->get_max_space() - blend_space->get_min_space());557point *= s;558point.y = s.height - point.y;559560points.push_back(point);561point -= (icon->get_size() / 2);562point = point.floor();563564if (i == selected_point) {565blend_space_draw->draw_texture(icon_selected, point);566} else {567blend_space_draw->draw_texture(icon, point);568}569}570571if (making_triangle.size()) {572Vector<Vector2> bl_points;573for (int i = 0; i < making_triangle.size(); i++) {574Vector2 point = blend_space->get_blend_point_position(making_triangle[i]);575point = (point - blend_space->get_min_space()) / (blend_space->get_max_space() - blend_space->get_min_space());576point *= s;577point.y = s.height - point.y;578bl_points.push_back(point);579}580581for (int i = 0; i < bl_points.size() - 1; i++) {582blend_space_draw->draw_line(bl_points[i], bl_points[i + 1], linecolor, Math::round(2 * EDSCALE), true);583}584blend_space_draw->draw_line(bl_points[bl_points.size() - 1], blend_space_draw->get_local_mouse_position(), linecolor, Math::round(2 * EDSCALE), true);585}586587///draw cursor position588589{590Color color;591if (tool_blend->is_pressed()) {592color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));593} else {594color = linecolor;595color.a *= 0.5;596}597598Vector2 blend_pos = tree->get(get_blend_position_path());599Vector2 point = blend_pos;600601point = (point - blend_space->get_min_space()) / (blend_space->get_max_space() - blend_space->get_min_space());602point *= s;603point.y = s.height - point.y;604605if (blend_space->get_triangle_count()) {606Vector2 closest = blend_space->get_closest_point(blend_pos);607closest = (closest - blend_space->get_min_space()) / (blend_space->get_max_space() - blend_space->get_min_space());608closest *= s;609closest.y = s.height - closest.y;610611Color lcol = color;612lcol.a *= 0.4;613blend_space_draw->draw_line(point, closest, lcol, Math::round(2 * EDSCALE), true);614}615616float mind = 5 * EDSCALE;617float maxd = 15 * EDSCALE;618blend_space_draw->draw_line(point + Vector2(mind, 0), point + Vector2(maxd, 0), color, Math::round(2 * EDSCALE));619blend_space_draw->draw_line(point + Vector2(-mind, 0), point + Vector2(-maxd, 0), color, Math::round(2 * EDSCALE));620blend_space_draw->draw_line(point + Vector2(0, mind), point + Vector2(0, maxd), color, Math::round(2 * EDSCALE));621blend_space_draw->draw_line(point + Vector2(0, -mind), point + Vector2(0, -maxd), color, Math::round(2 * EDSCALE));622}623}624625void AnimationNodeBlendSpace2DEditor::_snap_toggled() {626blend_space_draw->queue_redraw();627}628629void AnimationNodeBlendSpace2DEditor::_update_space() {630if (updating) {631return;632}633634updating = true;635636if (blend_space->get_auto_triangles()) {637tool_triangle->hide();638} else {639tool_triangle->show();640}641642auto_triangles->set_pressed(blend_space->get_auto_triangles());643644sync->set_pressed(blend_space->is_using_sync());645interpolation->select(blend_space->get_blend_mode());646647max_x_value->set_value(blend_space->get_max_space().x);648max_y_value->set_value(blend_space->get_max_space().y);649650min_x_value->set_value(blend_space->get_min_space().x);651min_y_value->set_value(blend_space->get_min_space().y);652653label_x->set_text(blend_space->get_x_label());654label_y->set_text(blend_space->get_y_label());655656snap_x->set_value(blend_space->get_snap().x);657snap_y->set_value(blend_space->get_snap().y);658659blend_space_draw->queue_redraw();660661updating = false;662}663664void AnimationNodeBlendSpace2DEditor::_config_changed(double) {665if (updating) {666return;667}668669updating = true;670EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();671undo_redo->create_action(TTR("Change BlendSpace2D Config"));672undo_redo->add_do_method(blend_space.ptr(), "set_max_space", Vector2(max_x_value->get_value(), max_y_value->get_value()));673undo_redo->add_undo_method(blend_space.ptr(), "set_max_space", blend_space->get_max_space());674undo_redo->add_do_method(blend_space.ptr(), "set_min_space", Vector2(min_x_value->get_value(), min_y_value->get_value()));675undo_redo->add_undo_method(blend_space.ptr(), "set_min_space", blend_space->get_min_space());676undo_redo->add_do_method(blend_space.ptr(), "set_snap", Vector2(snap_x->get_value(), snap_y->get_value()));677undo_redo->add_undo_method(blend_space.ptr(), "set_snap", blend_space->get_snap());678undo_redo->add_do_method(blend_space.ptr(), "set_use_sync", sync->is_pressed());679undo_redo->add_undo_method(blend_space.ptr(), "set_use_sync", blend_space->is_using_sync());680undo_redo->add_do_method(blend_space.ptr(), "set_blend_mode", interpolation->get_selected());681undo_redo->add_undo_method(blend_space.ptr(), "set_blend_mode", blend_space->get_blend_mode());682undo_redo->add_do_method(this, "_update_space");683undo_redo->add_undo_method(this, "_update_space");684undo_redo->commit_action();685updating = false;686687blend_space_draw->queue_redraw();688}689690void AnimationNodeBlendSpace2DEditor::_labels_changed(String) {691if (updating) {692return;693}694695updating = true;696EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();697undo_redo->create_action(TTR("Change BlendSpace2D Labels"), UndoRedo::MERGE_ENDS);698undo_redo->add_do_method(blend_space.ptr(), "set_x_label", label_x->get_text());699undo_redo->add_undo_method(blend_space.ptr(), "set_x_label", blend_space->get_x_label());700undo_redo->add_do_method(blend_space.ptr(), "set_y_label", label_y->get_text());701undo_redo->add_undo_method(blend_space.ptr(), "set_y_label", blend_space->get_y_label());702undo_redo->add_do_method(this, "_update_space");703undo_redo->add_undo_method(this, "_update_space");704undo_redo->commit_action();705updating = false;706}707708void AnimationNodeBlendSpace2DEditor::_erase_selected() {709EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();710if (selected_point != -1) {711updating = true;712undo_redo->create_action(TTR("Remove BlendSpace2D Point"));713undo_redo->add_do_method(blend_space.ptr(), "remove_blend_point", selected_point);714undo_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);715716//restore triangles using this point717for (int i = 0; i < blend_space->get_triangle_count(); i++) {718for (int j = 0; j < 3; j++) {719if (blend_space->get_triangle_point(i, j) == selected_point) {720undo_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);721break;722}723}724}725726undo_redo->add_do_method(this, "_update_space");727undo_redo->add_undo_method(this, "_update_space");728undo_redo->commit_action();729updating = false;730731blend_space_draw->queue_redraw();732} else if (selected_triangle != -1) {733updating = true;734undo_redo->create_action(TTR("Remove BlendSpace2D Triangle"));735undo_redo->add_do_method(blend_space.ptr(), "remove_triangle", selected_triangle);736undo_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);737738undo_redo->add_do_method(this, "_update_space");739undo_redo->add_undo_method(this, "_update_space");740undo_redo->commit_action();741updating = false;742743blend_space_draw->queue_redraw();744}745}746747void AnimationNodeBlendSpace2DEditor::_update_edited_point_pos() {748if (updating) {749return;750}751752if (selected_point >= 0 && selected_point < blend_space->get_blend_point_count()) {753Vector2 pos = blend_space->get_blend_point_position(selected_point);754if (dragging_selected) {755pos += drag_ofs;756if (snap->is_pressed()) {757pos = pos.snapped(blend_space->get_snap());758}759}760updating = true;761edit_x->set_value(pos.x);762edit_y->set_value(pos.y);763updating = false;764}765}766767void AnimationNodeBlendSpace2DEditor::_edit_point_pos(double) {768if (updating) {769return;770}771updating = true;772EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();773undo_redo->create_action(TTR("Move Node Point"));774undo_redo->add_do_method(blend_space.ptr(), "set_blend_point_position", selected_point, Vector2(edit_x->get_value(), edit_y->get_value()));775undo_redo->add_undo_method(blend_space.ptr(), "set_blend_point_position", selected_point, blend_space->get_blend_point_position(selected_point));776undo_redo->add_do_method(this, "_update_space");777undo_redo->add_undo_method(this, "_update_space");778undo_redo->add_do_method(this, "_update_edited_point_pos");779undo_redo->add_undo_method(this, "_update_edited_point_pos");780undo_redo->commit_action();781updating = false;782783blend_space_draw->queue_redraw();784}785786void AnimationNodeBlendSpace2DEditor::_notification(int p_what) {787switch (p_what) {788case NOTIFICATION_THEME_CHANGED: {789error_panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));790error_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("error_color"), EditorStringName(Editor)));791panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));792tool_blend->set_button_icon(get_editor_theme_icon(SNAME("EditPivot")));793tool_select->set_button_icon(get_editor_theme_icon(SNAME("ToolSelect")));794tool_create->set_button_icon(get_editor_theme_icon(SNAME("EditKey")));795tool_triangle->set_button_icon(get_editor_theme_icon(SNAME("ToolTriangle")));796tool_erase->set_button_icon(get_editor_theme_icon(SNAME("Remove")));797snap->set_button_icon(get_editor_theme_icon(SNAME("SnapGrid")));798open_editor->set_button_icon(get_editor_theme_icon(SNAME("Edit")));799auto_triangles->set_button_icon(get_editor_theme_icon(SNAME("AutoTriangle")));800interpolation->clear();801interpolation->add_icon_item(get_editor_theme_icon(SNAME("TrackContinuous")), TTR("Continuous"), 0);802interpolation->add_icon_item(get_editor_theme_icon(SNAME("TrackDiscrete")), TTR("Discrete"), 1);803interpolation->add_icon_item(get_editor_theme_icon(SNAME("TrackCapture")), TTR("Capture"), 2);804} break;805806case NOTIFICATION_PROCESS: {807AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();808if (!tree) {809return;810}811812String error;813814if (!tree->is_active()) {815error = TTR("AnimationTree is inactive.\nActivate to enable playback, check node warnings if activation fails.");816} else if (tree->is_state_invalid()) {817error = tree->get_invalid_state_reason();818} else if (blend_space->get_triangle_count() == 0) {819error = TTR("No triangles exist, so no blending can take place.");820}821822if (error != error_label->get_text()) {823error_label->set_text(error);824if (!error.is_empty()) {825error_panel->show();826} else {827error_panel->hide();828}829}830} break;831832case NOTIFICATION_VISIBILITY_CHANGED: {833set_process(is_visible_in_tree());834} break;835}836}837838void AnimationNodeBlendSpace2DEditor::_open_editor() {839if (selected_point >= 0 && selected_point < blend_space->get_blend_point_count()) {840Ref<AnimationNode> an = blend_space->get_blend_point_node(selected_point);841ERR_FAIL_COND(an.is_null());842AnimationTreeEditor::get_singleton()->enter_editor(itos(selected_point));843}844}845846void AnimationNodeBlendSpace2DEditor::_auto_triangles_toggled() {847EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();848undo_redo->create_action(TTR("Toggle Auto Triangles"));849undo_redo->add_do_method(blend_space.ptr(), "set_auto_triangles", auto_triangles->is_pressed());850undo_redo->add_undo_method(blend_space.ptr(), "set_auto_triangles", blend_space->get_auto_triangles());851undo_redo->add_do_method(this, "_update_space");852undo_redo->add_undo_method(this, "_update_space");853undo_redo->commit_action();854}855856void AnimationNodeBlendSpace2DEditor::_bind_methods() {857ClassDB::bind_method("_update_space", &AnimationNodeBlendSpace2DEditor::_update_space);858ClassDB::bind_method("_update_tool_erase", &AnimationNodeBlendSpace2DEditor::_update_tool_erase);859860ClassDB::bind_method("_update_edited_point_pos", &AnimationNodeBlendSpace2DEditor::_update_edited_point_pos);861}862863AnimationNodeBlendSpace2DEditor *AnimationNodeBlendSpace2DEditor::singleton = nullptr;864865AnimationNodeBlendSpace2DEditor::AnimationNodeBlendSpace2DEditor() {866singleton = this;867updating = false;868869HBoxContainer *top_hb = memnew(HBoxContainer);870add_child(top_hb);871872Ref<ButtonGroup> bg;873bg.instantiate();874875tool_blend = memnew(Button);876tool_blend->set_theme_type_variation(SceneStringName(FlatButton));877tool_blend->set_toggle_mode(true);878tool_blend->set_button_group(bg);879top_hb->add_child(tool_blend);880tool_blend->set_pressed(true);881tool_blend->set_tooltip_text(TTR("Set the blending position within the space"));882tool_blend->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_tool_switch).bind(3));883884tool_select = memnew(Button);885tool_select->set_theme_type_variation(SceneStringName(FlatButton));886tool_select->set_toggle_mode(true);887tool_select->set_button_group(bg);888top_hb->add_child(tool_select);889tool_select->set_tooltip_text(TTR("Select and move points, create points with RMB."));890tool_select->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_tool_switch).bind(0));891892tool_create = memnew(Button);893tool_create->set_theme_type_variation(SceneStringName(FlatButton));894tool_create->set_toggle_mode(true);895tool_create->set_button_group(bg);896top_hb->add_child(tool_create);897tool_create->set_tooltip_text(TTR("Create points."));898tool_create->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_tool_switch).bind(1));899900tool_triangle = memnew(Button);901tool_triangle->set_theme_type_variation(SceneStringName(FlatButton));902tool_triangle->set_toggle_mode(true);903tool_triangle->set_button_group(bg);904top_hb->add_child(tool_triangle);905tool_triangle->set_tooltip_text(TTR("Create triangles by connecting points."));906tool_triangle->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_tool_switch).bind(2));907908tool_erase_sep = memnew(VSeparator);909top_hb->add_child(tool_erase_sep);910tool_erase = memnew(Button);911tool_erase->set_theme_type_variation(SceneStringName(FlatButton));912top_hb->add_child(tool_erase);913tool_erase->set_tooltip_text(TTR("Erase points and triangles."));914tool_erase->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_erase_selected));915tool_erase->set_disabled(true);916917top_hb->add_child(memnew(VSeparator));918919auto_triangles = memnew(Button);920auto_triangles->set_theme_type_variation(SceneStringName(FlatButton));921top_hb->add_child(auto_triangles);922auto_triangles->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_auto_triangles_toggled));923auto_triangles->set_toggle_mode(true);924auto_triangles->set_tooltip_text(TTR("Generate blend triangles automatically (instead of manually)"));925926top_hb->add_child(memnew(VSeparator));927928snap = memnew(Button);929snap->set_theme_type_variation(SceneStringName(FlatButton));930snap->set_toggle_mode(true);931top_hb->add_child(snap);932snap->set_pressed(true);933snap->set_tooltip_text(TTR("Enable snap and show grid."));934snap->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_snap_toggled));935936snap_x = memnew(SpinBox);937top_hb->add_child(snap_x);938snap_x->set_prefix("x:");939snap_x->set_min(0.01);940snap_x->set_step(0.01);941snap_x->set_max(1000);942snap_x->set_accessibility_name(TTRC("Grid X Step"));943944snap_y = memnew(SpinBox);945top_hb->add_child(snap_y);946snap_y->set_prefix("y:");947snap_y->set_min(0.01);948snap_y->set_step(0.01);949snap_y->set_max(1000);950snap_y->set_accessibility_name(TTRC("Grid Y Step"));951952top_hb->add_child(memnew(VSeparator));953954top_hb->add_child(memnew(Label(TTR("Sync:"))));955sync = memnew(CheckBox);956top_hb->add_child(sync);957sync->connect(SceneStringName(toggled), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_config_changed));958959top_hb->add_child(memnew(VSeparator));960961top_hb->add_child(memnew(Label(TTR("Blend:"))));962interpolation = memnew(OptionButton);963top_hb->add_child(interpolation);964interpolation->connect(SceneStringName(item_selected), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_config_changed));965966edit_hb = memnew(HBoxContainer);967top_hb->add_child(edit_hb);968edit_hb->add_child(memnew(VSeparator));969edit_hb->add_child(memnew(Label(TTR("Point"))));970edit_x = memnew(SpinBox);971edit_hb->add_child(edit_x);972edit_x->set_min(-1000);973edit_x->set_step(0.01);974edit_x->set_max(1000);975edit_x->set_accessibility_name(TTRC("Blend X Value"));976edit_x->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_edit_point_pos));977edit_y = memnew(SpinBox);978edit_hb->add_child(edit_y);979edit_y->set_min(-1000);980edit_y->set_step(0.01);981edit_y->set_max(1000);982edit_y->set_accessibility_name(TTRC("Blend X Value"));983edit_y->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_edit_point_pos));984open_editor = memnew(Button);985edit_hb->add_child(open_editor);986open_editor->set_text(TTR("Open Editor"));987open_editor->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_open_editor), CONNECT_DEFERRED);988edit_hb->hide();989open_editor->hide();990991HBoxContainer *main_hb = memnew(HBoxContainer);992add_child(main_hb);993main_hb->set_v_size_flags(SIZE_EXPAND_FILL);994995GridContainer *main_grid = memnew(GridContainer);996main_grid->set_columns(2);997main_hb->add_child(main_grid);998main_grid->set_h_size_flags(SIZE_EXPAND_FILL);999{1000VBoxContainer *left_vbox = memnew(VBoxContainer);1001main_grid->add_child(left_vbox);1002left_vbox->set_v_size_flags(SIZE_EXPAND_FILL);1003max_y_value = memnew(SpinBox);1004max_y_value->set_accessibility_name(TTRC("Max Y"));1005left_vbox->add_child(max_y_value);1006left_vbox->add_spacer();1007label_y = memnew(LineEdit);1008label_y->set_accessibility_name(TTRC("Y Value"));1009left_vbox->add_child(label_y);1010label_y->set_expand_to_text_length_enabled(true);1011left_vbox->add_spacer();1012min_y_value = memnew(SpinBox);1013min_y_value->set_accessibility_name(TTRC("Min Y"));1014left_vbox->add_child(min_y_value);10151016max_y_value->set_max(10000);1017max_y_value->set_min(0.01);1018max_y_value->set_step(0.01);10191020min_y_value->set_min(-10000);1021min_y_value->set_max(0);1022min_y_value->set_step(0.01);1023}10241025panel = memnew(PanelContainer);1026panel->set_clip_contents(true);1027main_grid->add_child(panel);1028panel->set_h_size_flags(SIZE_EXPAND_FILL);10291030blend_space_draw = memnew(Control);1031blend_space_draw->connect(SceneStringName(gui_input), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_blend_space_gui_input));1032blend_space_draw->connect(SceneStringName(draw), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_blend_space_draw));1033blend_space_draw->set_focus_mode(FOCUS_ALL);10341035panel->add_child(blend_space_draw);1036main_grid->add_child(memnew(Control)); //empty bottom left10371038{1039HBoxContainer *bottom_vbox = memnew(HBoxContainer);1040main_grid->add_child(bottom_vbox);1041bottom_vbox->set_h_size_flags(SIZE_EXPAND_FILL);1042min_x_value = memnew(SpinBox);1043min_x_value->set_accessibility_name(TTRC("Min X"));1044bottom_vbox->add_child(min_x_value);1045bottom_vbox->add_spacer();1046label_x = memnew(LineEdit);1047label_y->set_accessibility_name(TTRC("X Value"));1048bottom_vbox->add_child(label_x);1049label_x->set_expand_to_text_length_enabled(true);1050bottom_vbox->add_spacer();1051max_x_value = memnew(SpinBox);1052max_x_value->set_accessibility_name(TTRC("Max Y"));1053bottom_vbox->add_child(max_x_value);10541055max_x_value->set_max(10000);1056max_x_value->set_min(0.01);1057max_x_value->set_step(0.01);10581059min_x_value->set_min(-10000);1060min_x_value->set_max(0);1061min_x_value->set_step(0.01);1062}10631064snap_x->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_config_changed));1065snap_y->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_config_changed));1066max_x_value->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_config_changed));1067min_x_value->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_config_changed));1068max_y_value->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_config_changed));1069min_y_value->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_config_changed));1070label_x->connect(SceneStringName(text_changed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_labels_changed));1071label_y->connect(SceneStringName(text_changed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_labels_changed));10721073error_panel = memnew(PanelContainer);1074add_child(error_panel);1075error_label = memnew(Label);1076error_label->set_focus_mode(FOCUS_ACCESSIBILITY);1077error_panel->add_child(error_label);1078error_panel->hide();10791080set_custom_minimum_size(Size2(0, 300 * EDSCALE));10811082menu = memnew(PopupMenu);1083add_child(menu);1084menu->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationNodeBlendSpace2DEditor::_add_menu_type));10851086animations_menu = memnew(PopupMenu);1087animations_menu->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);1088menu->add_child(animations_menu);1089animations_menu->connect("index_pressed", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_add_animation_type));10901091open_file = memnew(EditorFileDialog);1092add_child(open_file);1093open_file->set_title(TTR("Open Animation Node"));1094open_file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);1095open_file->connect("file_selected", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_file_opened));10961097selected_point = -1;1098selected_triangle = -1;10991100dragging_selected = false;1101dragging_selected_attempt = false;1102}110311041105