Path: blob/master/editor/animation/animation_blend_space_1d_editor.cpp
9896 views
/**************************************************************************/1/* animation_blend_space_1d_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_1d_editor.h"3132#include "core/os/keyboard.h"33#include "editor/editor_node.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/settings/editor_settings.h"38#include "editor/themes/editor_scale.h"39#include "scene/animation/animation_blend_tree.h"40#include "scene/gui/button.h"41#include "scene/gui/check_box.h"42#include "scene/gui/line_edit.h"43#include "scene/gui/option_button.h"44#include "scene/gui/panel_container.h"45#include "scene/gui/separator.h"46#include "scene/gui/spin_box.h"4748StringName AnimationNodeBlendSpace1DEditor::get_blend_position_path() const {49StringName path = AnimationTreeEditor::get_singleton()->get_base_path() + "blend_position";50return path;51}5253void AnimationNodeBlendSpace1DEditor::_blend_space_gui_input(const Ref<InputEvent> &p_event) {54AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();55if (!tree) {56return;57}5859Ref<InputEventKey> k = p_event;6061if (tool_select->is_pressed() && k.is_valid() && k->is_pressed() && k->get_keycode() == Key::KEY_DELETE && !k->is_echo()) {62if (selected_point != -1) {63if (!read_only) {64_erase_selected();65}66accept_event();67}68}6970Ref<InputEventMouseButton> mb = p_event;7172if (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()))) {73if (!read_only) {74menu->clear(false);75animations_menu->clear();76animations_to_add.clear();7778LocalVector<StringName> classes;79ClassDB::get_inheriters_from_class("AnimationRootNode", classes);80classes.sort_custom<StringName::AlphCompare>();8182menu->add_submenu_node_item(TTR("Add Animation"), animations_menu);8384List<StringName> names;85tree->get_animation_list(&names);8687for (const StringName &E : names) {88animations_menu->add_icon_item(get_editor_theme_icon(SNAME("Animation")), E);89animations_to_add.push_back(E);90}9192for (const StringName &E : classes) {93String name = String(E).replace_first("AnimationNode", "");94if (name == "Animation" || name == "StartState" || name == "EndState") {95continue;96}9798int idx = menu->get_item_count();99menu->add_item(vformat(TTR("Add %s"), name), idx);100menu->set_item_metadata(idx, E);101}102103Ref<AnimationNode> clipb = EditorSettings::get_singleton()->get_resource_clipboard();104if (clipb.is_valid()) {105menu->add_separator();106menu->add_item(TTR("Paste"), MENU_PASTE);107}108menu->add_separator();109menu->add_item(TTR("Load..."), MENU_LOAD_FILE);110111menu->set_position(blend_space_draw->get_screen_position() + mb->get_position());112menu->reset_size();113menu->popup();114115add_point_pos = (mb->get_position() / blend_space_draw->get_size()).x;116add_point_pos *= (blend_space->get_max_space() - blend_space->get_min_space());117add_point_pos += blend_space->get_min_space();118119if (snap->is_pressed()) {120add_point_pos = Math::snapped(add_point_pos, blend_space->get_snap());121}122}123}124125if (mb.is_valid() && mb->is_pressed() && tool_select->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {126blend_space_draw->queue_redraw(); // why not127128// try to see if a point can be selected129selected_point = -1;130_update_tool_erase();131132for (int i = 0; i < points.size(); i++) {133if (Math::abs(float(points[i] - mb->get_position().x)) < 10 * EDSCALE) {134selected_point = i;135136Ref<AnimationNode> node = blend_space->get_blend_point_node(i);137EditorNode::get_singleton()->push_item(node.ptr(), "", true);138dragging_selected_attempt = true;139drag_from = mb->get_position();140_update_tool_erase();141_update_edited_point_pos();142return;143}144}145}146147if (mb.is_valid() && !mb->is_pressed() && dragging_selected_attempt && mb->get_button_index() == MouseButton::LEFT) {148if (!read_only) {149if (dragging_selected) {150// move151float point = blend_space->get_blend_point_position(selected_point);152point += drag_ofs.x;153154if (snap->is_pressed()) {155point = Math::snapped(point, blend_space->get_snap());156}157158updating = true;159EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();160undo_redo->create_action(TTR("Move Node Point"));161undo_redo->add_do_method(blend_space.ptr(), "set_blend_point_position", selected_point, point);162undo_redo->add_undo_method(blend_space.ptr(), "set_blend_point_position", selected_point, blend_space->get_blend_point_position(selected_point));163undo_redo->add_do_method(this, "_update_space");164undo_redo->add_undo_method(this, "_update_space");165undo_redo->add_do_method(this, "_update_edited_point_pos");166undo_redo->add_undo_method(this, "_update_edited_point_pos");167undo_redo->commit_action();168updating = false;169}170171dragging_selected_attempt = false;172dragging_selected = false;173blend_space_draw->queue_redraw();174}175}176177// *set* the blend178if (mb.is_valid() && !mb->is_pressed() && tool_blend->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {179float blend_pos = mb->get_position().x / blend_space_draw->get_size().x;180blend_pos *= blend_space->get_max_space() - blend_space->get_min_space();181blend_pos += blend_space->get_min_space();182183tree->set(get_blend_position_path(), blend_pos);184blend_space_draw->queue_redraw();185}186187Ref<InputEventMouseMotion> mm = p_event;188189if (mm.is_valid() && dragging_selected_attempt) {190dragging_selected = true;191drag_ofs = ((mm->get_position() - drag_from) / blend_space_draw->get_size()) * ((blend_space->get_max_space() - blend_space->get_min_space()) * Vector2(1, 0));192blend_space_draw->queue_redraw();193_update_edited_point_pos();194}195196if (mm.is_valid() && tool_blend->is_pressed() && (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {197float blend_pos = mm->get_position().x / blend_space_draw->get_size().x;198blend_pos *= blend_space->get_max_space() - blend_space->get_min_space();199blend_pos += blend_space->get_min_space();200201tree->set(get_blend_position_path(), blend_pos);202203blend_space_draw->queue_redraw();204}205}206207void AnimationNodeBlendSpace1DEditor::_blend_space_draw() {208AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();209if (!tree) {210return;211}212213Color linecolor = get_theme_color(SceneStringName(font_color), SNAME("Label"));214Color linecolor_soft = linecolor;215linecolor_soft.a *= 0.5;216217Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));218int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));219Ref<Texture2D> icon = get_editor_theme_icon(SNAME("KeyValue"));220Ref<Texture2D> icon_selected = get_editor_theme_icon(SNAME("KeySelected"));221222Size2 s = blend_space_draw->get_size();223224if (blend_space_draw->has_focus()) {225Color color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));226blend_space_draw->draw_rect(Rect2(Point2(), s), color, false);227}228229blend_space_draw->draw_line(Point2(1, s.height - 1), Point2(s.width - 1, s.height - 1), linecolor, Math::round(EDSCALE));230231if (blend_space->get_min_space() < 0) {232float point = 0.0;233point = (point - blend_space->get_min_space()) / (blend_space->get_max_space() - blend_space->get_min_space());234point *= s.width;235236float x = point;237238blend_space_draw->draw_line(Point2(x, s.height - 1), Point2(x, s.height - 5 * EDSCALE), linecolor, Math::round(EDSCALE));239blend_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);240blend_space_draw->draw_line(Point2(x, s.height - 5 * EDSCALE), Point2(x, 0), linecolor_soft, Math::round(EDSCALE));241}242243if (snap->is_pressed()) {244linecolor_soft.a = linecolor.a * 0.1;245246if (blend_space->get_snap() > 0) {247int prev_idx = -1;248249for (int i = 0; i < s.x; i++) {250float v = blend_space->get_min_space() + i * (blend_space->get_max_space() - blend_space->get_min_space()) / s.x;251int idx = int(v / blend_space->get_snap());252253if (i > 0 && prev_idx != idx) {254blend_space_draw->draw_line(Point2(i, 0), Point2(i, s.height), linecolor_soft, Math::round(EDSCALE));255}256257prev_idx = idx;258}259}260}261262points.clear();263264for (int i = 0; i < blend_space->get_blend_point_count(); i++) {265float point = blend_space->get_blend_point_position(i);266267if (!read_only) {268if (dragging_selected && selected_point == i) {269point += drag_ofs.x;270if (snap->is_pressed()) {271point = Math::snapped(point, blend_space->get_snap());272}273}274}275276point = (point - blend_space->get_min_space()) / (blend_space->get_max_space() - blend_space->get_min_space());277point *= s.width;278279points.push_back(point);280281Vector2 gui_point = Vector2(point, s.height / 2.0);282283gui_point -= (icon->get_size() / 2.0);284285gui_point = gui_point.floor();286287if (i == selected_point) {288blend_space_draw->draw_texture(icon_selected, gui_point);289} else {290blend_space_draw->draw_texture(icon, gui_point);291}292}293294// blend position295{296Color color;297if (tool_blend->is_pressed()) {298color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));299} else {300color = linecolor;301color.a *= 0.5;302}303304float point = tree->get(get_blend_position_path());305306point = (point - blend_space->get_min_space()) / (blend_space->get_max_space() - blend_space->get_min_space());307point *= s.width;308309Vector2 gui_point = Vector2(point, s.height / 2.0);310311float mind = 5 * EDSCALE;312float maxd = 15 * EDSCALE;313blend_space_draw->draw_line(gui_point + Vector2(mind, 0), gui_point + Vector2(maxd, 0), color, Math::round(2 * EDSCALE));314blend_space_draw->draw_line(gui_point + Vector2(-mind, 0), gui_point + Vector2(-maxd, 0), color, Math::round(2 * EDSCALE));315blend_space_draw->draw_line(gui_point + Vector2(0, mind), gui_point + Vector2(0, maxd), color, Math::round(2 * EDSCALE));316blend_space_draw->draw_line(gui_point + Vector2(0, -mind), gui_point + Vector2(0, -maxd), color, Math::round(2 * EDSCALE));317}318}319320void AnimationNodeBlendSpace1DEditor::_update_space() {321if (updating) {322return;323}324325updating = true;326327max_value->set_value(blend_space->get_max_space());328min_value->set_value(blend_space->get_min_space());329330sync->set_pressed(blend_space->is_using_sync());331interpolation->select(blend_space->get_blend_mode());332333label_value->set_text(blend_space->get_value_label());334335snap_value->set_value(blend_space->get_snap());336337blend_space_draw->queue_redraw();338339updating = false;340}341342void AnimationNodeBlendSpace1DEditor::_config_changed(double) {343if (updating) {344return;345}346347updating = true;348EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();349undo_redo->create_action(TTR("Change BlendSpace1D Config"));350undo_redo->add_do_method(blend_space.ptr(), "set_max_space", max_value->get_value());351undo_redo->add_undo_method(blend_space.ptr(), "set_max_space", blend_space->get_max_space());352undo_redo->add_do_method(blend_space.ptr(), "set_min_space", min_value->get_value());353undo_redo->add_undo_method(blend_space.ptr(), "set_min_space", blend_space->get_min_space());354undo_redo->add_do_method(blend_space.ptr(), "set_snap", snap_value->get_value());355undo_redo->add_undo_method(blend_space.ptr(), "set_snap", blend_space->get_snap());356undo_redo->add_do_method(blend_space.ptr(), "set_use_sync", sync->is_pressed());357undo_redo->add_undo_method(blend_space.ptr(), "set_use_sync", blend_space->is_using_sync());358undo_redo->add_do_method(blend_space.ptr(), "set_blend_mode", interpolation->get_selected());359undo_redo->add_undo_method(blend_space.ptr(), "set_blend_mode", blend_space->get_blend_mode());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 AnimationNodeBlendSpace1DEditor::_labels_changed(String) {369if (updating) {370return;371}372373updating = true;374EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();375undo_redo->create_action(TTR("Change BlendSpace1D Labels"), UndoRedo::MERGE_ENDS);376undo_redo->add_do_method(blend_space.ptr(), "set_value_label", label_value->get_text());377undo_redo->add_undo_method(blend_space.ptr(), "set_value_label", blend_space->get_value_label());378undo_redo->add_do_method(this, "_update_space");379undo_redo->add_undo_method(this, "_update_space");380undo_redo->commit_action();381updating = false;382}383384void AnimationNodeBlendSpace1DEditor::_snap_toggled() {385blend_space_draw->queue_redraw();386}387388void AnimationNodeBlendSpace1DEditor::_file_opened(const String &p_file) {389file_loaded = ResourceLoader::load(p_file);390if (file_loaded.is_valid()) {391_add_menu_type(MENU_LOAD_FILE_CONFIRM);392} else {393EditorNode::get_singleton()->show_warning(TTR("This type of node can't be used. Only animation nodes are allowed."));394}395}396397void AnimationNodeBlendSpace1DEditor::_add_menu_type(int p_index) {398Ref<AnimationRootNode> node;399if (p_index == MENU_LOAD_FILE) {400open_file->clear_filters();401List<String> filters;402ResourceLoader::get_recognized_extensions_for_type("AnimationRootNode", &filters);403for (const String &E : filters) {404open_file->add_filter("*." + E);405}406open_file->popup_file_dialog();407return;408} else if (p_index == MENU_LOAD_FILE_CONFIRM) {409node = file_loaded;410file_loaded.unref();411} else if (p_index == MENU_PASTE) {412node = EditorSettings::get_singleton()->get_resource_clipboard();413} else {414String type = menu->get_item_metadata(p_index);415416Object *obj = ClassDB::instantiate(type);417ERR_FAIL_NULL(obj);418AnimationNode *an = Object::cast_to<AnimationNode>(obj);419ERR_FAIL_NULL(an);420421node = Ref<AnimationNode>(an);422}423424if (node.is_null()) {425EditorNode::get_singleton()->show_warning(TTR("This type of node can't be used. Only root nodes are allowed."));426return;427}428429updating = true;430EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();431undo_redo->create_action(TTR("Add Node Point"));432undo_redo->add_do_method(blend_space.ptr(), "add_blend_point", node, add_point_pos);433undo_redo->add_undo_method(blend_space.ptr(), "remove_blend_point", blend_space->get_blend_point_count());434undo_redo->add_do_method(this, "_update_space");435undo_redo->add_undo_method(this, "_update_space");436undo_redo->commit_action();437updating = false;438439blend_space_draw->queue_redraw();440}441442void AnimationNodeBlendSpace1DEditor::_add_animation_type(int p_index) {443Ref<AnimationNodeAnimation> anim;444anim.instantiate();445446anim->set_animation(animations_to_add[p_index]);447448updating = true;449EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();450undo_redo->create_action(TTR("Add Animation Point"));451undo_redo->add_do_method(blend_space.ptr(), "add_blend_point", anim, add_point_pos);452undo_redo->add_undo_method(blend_space.ptr(), "remove_blend_point", blend_space->get_blend_point_count());453undo_redo->add_do_method(this, "_update_space");454undo_redo->add_undo_method(this, "_update_space");455undo_redo->commit_action();456updating = false;457458blend_space_draw->queue_redraw();459}460461void AnimationNodeBlendSpace1DEditor::_tool_switch(int p_tool) {462if (p_tool == 0) {463tool_erase->show();464tool_erase_sep->show();465} else {466tool_erase->hide();467tool_erase_sep->hide();468}469470_update_tool_erase();471blend_space_draw->queue_redraw();472}473474void AnimationNodeBlendSpace1DEditor::_update_edited_point_pos() {475if (updating) {476return;477}478479if (selected_point >= 0 && selected_point < blend_space->get_blend_point_count()) {480float pos = blend_space->get_blend_point_position(selected_point);481482if (dragging_selected) {483pos += drag_ofs.x;484485if (snap->is_pressed()) {486pos = Math::snapped(pos, blend_space->get_snap());487}488}489490updating = true;491edit_value->set_value(pos);492updating = false;493}494}495496void AnimationNodeBlendSpace1DEditor::_update_tool_erase() {497bool point_valid = selected_point >= 0 && selected_point < blend_space->get_blend_point_count();498tool_erase->set_disabled(!point_valid || read_only);499500if (point_valid) {501Ref<AnimationNode> an = blend_space->get_blend_point_node(selected_point);502503if (AnimationTreeEditor::get_singleton()->can_edit(an)) {504open_editor->show();505} else {506open_editor->hide();507}508509if (!read_only) {510edit_hb->show();511} else {512edit_hb->hide();513}514} else {515edit_hb->hide();516}517}518519void AnimationNodeBlendSpace1DEditor::_erase_selected() {520if (selected_point != -1) {521updating = true;522523EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();524undo_redo->create_action(TTR("Remove BlendSpace1D Point"));525undo_redo->add_do_method(blend_space.ptr(), "remove_blend_point", selected_point);526undo_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);527undo_redo->add_do_method(this, "_update_space");528undo_redo->add_undo_method(this, "_update_space");529undo_redo->commit_action();530531updating = false;532533blend_space_draw->queue_redraw();534}535}536537void AnimationNodeBlendSpace1DEditor::_edit_point_pos(double) {538if (updating) {539return;540}541542updating = true;543EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();544undo_redo->create_action(TTR("Move BlendSpace1D Node Point"));545undo_redo->add_do_method(blend_space.ptr(), "set_blend_point_position", selected_point, edit_value->get_value());546undo_redo->add_undo_method(blend_space.ptr(), "set_blend_point_position", selected_point, blend_space->get_blend_point_position(selected_point));547undo_redo->add_do_method(this, "_update_space");548undo_redo->add_undo_method(this, "_update_space");549undo_redo->add_do_method(this, "_update_edited_point_pos");550undo_redo->add_undo_method(this, "_update_edited_point_pos");551undo_redo->commit_action();552updating = false;553554blend_space_draw->queue_redraw();555}556557void AnimationNodeBlendSpace1DEditor::_open_editor() {558if (selected_point >= 0 && selected_point < blend_space->get_blend_point_count()) {559Ref<AnimationNode> an = blend_space->get_blend_point_node(selected_point);560ERR_FAIL_COND(an.is_null());561AnimationTreeEditor::get_singleton()->enter_editor(itos(selected_point));562}563}564565void AnimationNodeBlendSpace1DEditor::_notification(int p_what) {566switch (p_what) {567case NOTIFICATION_THEME_CHANGED: {568error_panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));569error_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("error_color"), EditorStringName(Editor)));570panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));571tool_blend->set_button_icon(get_editor_theme_icon(SNAME("EditPivot")));572tool_select->set_button_icon(get_editor_theme_icon(SNAME("ToolSelect")));573tool_create->set_button_icon(get_editor_theme_icon(SNAME("EditKey")));574tool_erase->set_button_icon(get_editor_theme_icon(SNAME("Remove")));575snap->set_button_icon(get_editor_theme_icon(SNAME("SnapGrid")));576open_editor->set_button_icon(get_editor_theme_icon(SNAME("Edit")));577interpolation->clear();578interpolation->add_icon_item(get_editor_theme_icon(SNAME("TrackContinuous")), TTR("Continuous"), 0);579interpolation->add_icon_item(get_editor_theme_icon(SNAME("TrackDiscrete")), TTR("Discrete"), 1);580interpolation->add_icon_item(get_editor_theme_icon(SNAME("TrackCapture")), TTR("Capture"), 2);581} break;582583case NOTIFICATION_PROCESS: {584AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();585if (!tree) {586return;587}588589String error;590591if (!tree->is_active()) {592error = TTR("AnimationTree is inactive.\nActivate to enable playback, check node warnings if activation fails.");593} else if (tree->is_state_invalid()) {594error = tree->get_invalid_state_reason();595}596597if (error != error_label->get_text()) {598error_label->set_text(error);599if (!error.is_empty()) {600error_panel->show();601} else {602error_panel->hide();603}604}605} break;606607case NOTIFICATION_VISIBILITY_CHANGED: {608set_process(is_visible_in_tree());609} break;610}611}612613void AnimationNodeBlendSpace1DEditor::_bind_methods() {614ClassDB::bind_method("_update_space", &AnimationNodeBlendSpace1DEditor::_update_space);615ClassDB::bind_method("_update_tool_erase", &AnimationNodeBlendSpace1DEditor::_update_tool_erase);616617ClassDB::bind_method("_update_edited_point_pos", &AnimationNodeBlendSpace1DEditor::_update_edited_point_pos);618}619620bool AnimationNodeBlendSpace1DEditor::can_edit(const Ref<AnimationNode> &p_node) {621Ref<AnimationNodeBlendSpace1D> b1d = p_node;622return b1d.is_valid();623}624625void AnimationNodeBlendSpace1DEditor::edit(const Ref<AnimationNode> &p_node) {626blend_space = p_node;627read_only = false;628629if (blend_space.is_valid()) {630read_only = EditorNode::get_singleton()->is_resource_read_only(blend_space);631632_update_space();633}634635tool_create->set_disabled(read_only);636edit_value->set_editable(!read_only);637label_value->set_editable(!read_only);638min_value->set_editable(!read_only);639max_value->set_editable(!read_only);640sync->set_disabled(read_only);641interpolation->set_disabled(read_only);642}643644AnimationNodeBlendSpace1DEditor *AnimationNodeBlendSpace1DEditor::singleton = nullptr;645646AnimationNodeBlendSpace1DEditor::AnimationNodeBlendSpace1DEditor() {647singleton = this;648649HBoxContainer *top_hb = memnew(HBoxContainer);650add_child(top_hb);651652Ref<ButtonGroup> bg;653bg.instantiate();654655tool_blend = memnew(Button);656tool_blend->set_theme_type_variation(SceneStringName(FlatButton));657tool_blend->set_toggle_mode(true);658tool_blend->set_button_group(bg);659top_hb->add_child(tool_blend);660tool_blend->set_pressed(true);661tool_blend->set_tooltip_text(TTR("Set the blending position within the space"));662tool_blend->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_tool_switch).bind(3));663664tool_select = memnew(Button);665tool_select->set_theme_type_variation(SceneStringName(FlatButton));666tool_select->set_toggle_mode(true);667tool_select->set_button_group(bg);668top_hb->add_child(tool_select);669tool_select->set_tooltip_text(TTR("Select and move points, create points with RMB."));670tool_select->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_tool_switch).bind(0));671672tool_create = memnew(Button);673tool_create->set_theme_type_variation(SceneStringName(FlatButton));674tool_create->set_toggle_mode(true);675tool_create->set_button_group(bg);676top_hb->add_child(tool_create);677tool_create->set_tooltip_text(TTR("Create points."));678tool_create->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_tool_switch).bind(1));679680tool_erase_sep = memnew(VSeparator);681top_hb->add_child(tool_erase_sep);682tool_erase = memnew(Button);683tool_erase->set_theme_type_variation(SceneStringName(FlatButton));684top_hb->add_child(tool_erase);685tool_erase->set_tooltip_text(TTR("Erase points."));686tool_erase->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_erase_selected));687688top_hb->add_child(memnew(VSeparator));689690snap = memnew(Button);691snap->set_theme_type_variation(SceneStringName(FlatButton));692snap->set_toggle_mode(true);693top_hb->add_child(snap);694snap->set_pressed(true);695snap->set_tooltip_text(TTR("Enable snap and show grid."));696snap->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_snap_toggled));697698snap_value = memnew(SpinBox);699top_hb->add_child(snap_value);700snap_value->set_min(0.01);701snap_value->set_step(0.01);702snap_value->set_max(1000);703snap_value->set_accessibility_name(TTRC("Grid Step"));704705top_hb->add_child(memnew(VSeparator));706top_hb->add_child(memnew(Label(TTR("Sync:"))));707sync = memnew(CheckBox);708top_hb->add_child(sync);709sync->connect(SceneStringName(toggled), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_config_changed));710711top_hb->add_child(memnew(VSeparator));712713top_hb->add_child(memnew(Label(TTR("Blend:"))));714interpolation = memnew(OptionButton);715top_hb->add_child(interpolation);716interpolation->connect(SceneStringName(item_selected), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_config_changed));717718edit_hb = memnew(HBoxContainer);719top_hb->add_child(edit_hb);720edit_hb->add_child(memnew(VSeparator));721edit_hb->add_child(memnew(Label(TTR("Point"))));722723edit_value = memnew(SpinBox);724edit_hb->add_child(edit_value);725edit_value->set_min(-1000);726edit_value->set_max(1000);727edit_value->set_step(0.01);728edit_value->set_accessibility_name(TTRC("Blend Value"));729edit_value->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_edit_point_pos));730731open_editor = memnew(Button);732edit_hb->add_child(open_editor);733open_editor->set_text(TTR("Open Editor"));734open_editor->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_open_editor), CONNECT_DEFERRED);735736edit_hb->hide();737open_editor->hide();738739VBoxContainer *main_vb = memnew(VBoxContainer);740add_child(main_vb);741main_vb->set_v_size_flags(SIZE_EXPAND_FILL);742743panel = memnew(PanelContainer);744panel->set_clip_contents(true);745main_vb->add_child(panel);746panel->set_h_size_flags(SIZE_EXPAND_FILL);747panel->set_v_size_flags(SIZE_EXPAND_FILL);748749blend_space_draw = memnew(Control);750blend_space_draw->connect(SceneStringName(gui_input), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_blend_space_gui_input));751blend_space_draw->connect(SceneStringName(draw), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_blend_space_draw));752blend_space_draw->set_focus_mode(FOCUS_ALL);753754panel->add_child(blend_space_draw);755756{757HBoxContainer *bottom_hb = memnew(HBoxContainer);758main_vb->add_child(bottom_hb);759bottom_hb->set_h_size_flags(SIZE_EXPAND_FILL);760761min_value = memnew(SpinBox);762min_value->set_min(-10000);763min_value->set_max(0);764min_value->set_step(0.01);765min_value->set_accessibility_name(TTRC("Min"));766767max_value = memnew(SpinBox);768max_value->set_min(0.01);769max_value->set_max(10000);770max_value->set_step(0.01);771max_value->set_accessibility_name(TTRC("Max"));772773label_value = memnew(LineEdit);774label_value->set_expand_to_text_length_enabled(true);775label_value->set_accessibility_name(TTRC("Value"));776777// now add778779bottom_hb->add_child(min_value);780bottom_hb->add_spacer();781bottom_hb->add_child(label_value);782bottom_hb->add_spacer();783bottom_hb->add_child(max_value);784}785786snap_value->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_config_changed));787min_value->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_config_changed));788max_value->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_config_changed));789label_value->connect(SceneStringName(text_changed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_labels_changed));790791error_panel = memnew(PanelContainer);792add_child(error_panel);793794error_label = memnew(Label);795error_label->set_focus_mode(FOCUS_ACCESSIBILITY);796error_panel->add_child(error_label);797error_panel->hide();798799menu = memnew(PopupMenu);800add_child(menu);801menu->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_add_menu_type));802803animations_menu = memnew(PopupMenu);804animations_menu->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);805menu->add_child(animations_menu);806animations_menu->connect("index_pressed", callable_mp(this, &AnimationNodeBlendSpace1DEditor::_add_animation_type));807808open_file = memnew(EditorFileDialog);809add_child(open_file);810open_file->set_title(TTR("Open Animation Node"));811open_file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);812open_file->connect("file_selected", callable_mp(this, &AnimationNodeBlendSpace1DEditor::_file_opened));813814set_custom_minimum_size(Size2(0, 150 * EDSCALE));815}816817818