Path: blob/master/editor/animation/animation_state_machine_editor.h
9902 views
/**************************************************************************/1/* animation_state_machine_editor.h */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#pragma once3132#include "editor/animation/animation_tree_editor_plugin.h"33#include "scene/animation/animation_node_state_machine.h"34#include "scene/gui/graph_edit.h"35#include "scene/gui/popup.h"3637class ConfirmationDialog;38class EditorFileDialog;39class LineEdit;40class OptionButton;41class PanelContainer;4243class AnimationNodeStateMachineEditor : public AnimationTreeNodeEditorPlugin {44GDCLASS(AnimationNodeStateMachineEditor, AnimationTreeNodeEditorPlugin);4546Ref<AnimationNodeStateMachine> state_machine;4748bool read_only = false;4950Button *tool_select = nullptr;51Button *tool_create = nullptr;52Button *tool_connect = nullptr;53Popup *name_edit_popup = nullptr;54LineEdit *name_edit = nullptr;5556HBoxContainer *selection_tools_hb = nullptr;57Button *tool_erase = nullptr;5859HBoxContainer *transition_tools_hb = nullptr;60OptionButton *switch_mode = nullptr;61Button *auto_advance = nullptr;6263OptionButton *play_mode = nullptr;6465PanelContainer *panel = nullptr;6667StringName selected_node;68HashSet<StringName> selected_nodes;6970HScrollBar *h_scroll = nullptr;71VScrollBar *v_scroll = nullptr;7273Control *state_machine_draw = nullptr;74Control *state_machine_play_pos = nullptr;7576PanelContainer *error_panel = nullptr;77Label *error_label = nullptr;7879struct ThemeCache {80Ref<StyleBox> panel_style;81Ref<StyleBox> error_panel_style;82Color error_color;8384Ref<Texture2D> tool_icon_select;85Ref<Texture2D> tool_icon_create;86Ref<Texture2D> tool_icon_connect;87Ref<Texture2D> tool_icon_erase;8889Ref<Texture2D> transition_icon_immediate;90Ref<Texture2D> transition_icon_sync;91Ref<Texture2D> transition_icon_end;9293Ref<Texture2D> play_icon_start;94Ref<Texture2D> play_icon_travel;95Ref<Texture2D> play_icon_auto;9697Ref<Texture2D> animation_icon;9899Ref<StyleBox> node_frame;100Ref<StyleBox> node_frame_selected;101Ref<StyleBox> node_frame_playing;102Ref<StyleBox> node_frame_start;103Ref<StyleBox> node_frame_end;104105Ref<Font> node_title_font;106int node_title_font_size = 0;107Color node_title_font_color;108109Ref<Texture2D> play_node;110Ref<Texture2D> edit_node;111112Color transition_color;113Color transition_disabled_color;114Color transition_icon_color;115Color transition_icon_disabled_color;116Color highlight_color;117Color highlight_disabled_color;118Color focus_color;119Color guideline_color;120121Ref<Texture2D> transition_icons[6]{};122123Color playback_color;124Color playback_background_color;125} theme_cache;126127bool updating = false;128129static AnimationNodeStateMachineEditor *singleton;130131void _state_machine_gui_input(const Ref<InputEvent> &p_event);132void _connection_draw(const Vector2 &p_from, const Vector2 &p_to, AnimationNodeStateMachineTransition::SwitchMode p_mode, bool p_enabled, bool p_selected, bool p_travel, float p_fade_ratio, bool p_auto_advance, bool p_is_across_group, float p_opacity = 1.0);133134void _state_machine_draw();135136void _state_machine_pos_draw_individual(const String &p_name, float p_ratio);137void _state_machine_pos_draw_all();138139void _update_graph();140141PopupMenu *menu = nullptr;142PopupMenu *connect_menu = nullptr;143PopupMenu *state_machine_menu = nullptr;144PopupMenu *end_menu = nullptr;145PopupMenu *animations_menu = nullptr;146Vector<String> animations_to_add;147Vector<String> nodes_to_connect;148149Vector2 add_node_pos;150151bool box_selecting = false;152Point2 box_selecting_from;153Point2 box_selecting_to;154Rect2 box_selecting_rect;155HashSet<StringName> previous_selected;156157bool dragging_selected_attempt = false;158bool dragging_selected = false;159Vector2 drag_from;160Vector2 drag_ofs;161StringName snap_x;162StringName snap_y;163164bool connecting = false;165bool connection_follows_cursor = false;166StringName connecting_from;167Vector2 connecting_to;168StringName connecting_to_node;169170void _add_menu_type(int p_index);171void _add_animation_type(int p_index);172void _connect_to(int p_index);173174struct NodeRect {175StringName node_name;176Rect2 node;177Rect2 play;178Rect2 name;179Rect2 edit;180bool can_edit;181};182183Vector<NodeRect> node_rects;184185struct TransitionLine {186StringName from_node;187StringName to_node;188Vector2 from;189Vector2 to;190AnimationNodeStateMachineTransition::SwitchMode mode;191StringName advance_condition_name;192bool advance_condition_state = false;193bool disabled = false;194bool auto_advance = false;195float width = 0;196bool selected;197bool travel;198float fade_ratio;199bool hidden;200int transition_index;201bool is_across_group = false;202};203204Vector<TransitionLine> transition_lines;205206struct NodeUR {207StringName name;208Ref<AnimationNode> node;209Vector2 position;210};211212struct TransitionUR {213StringName new_from;214StringName new_to;215StringName old_from;216StringName old_to;217Ref<AnimationNodeStateMachineTransition> transition;218};219220StringName selected_transition_from;221StringName selected_transition_to;222int selected_transition_index = -1;223void _add_transition(const bool p_nested_action = false);224225enum HoveredNodeArea {226HOVER_NODE_NONE = -1,227HOVER_NODE_PLAY = 0,228HOVER_NODE_EDIT = 1,229};230231StringName hovered_node_name;232HoveredNodeArea hovered_node_area = HOVER_NODE_NONE;233234String prev_name;235void _name_edited(const String &p_text);236void _name_edited_focus_out();237void _open_editor(const String &p_name);238void _scroll_changed(double);239240String _get_root_playback_path(String &r_node_directory);241242void _clip_src_line_to_rect(Vector2 &r_from, const Vector2 &p_to, const Rect2 &p_rect);243void _clip_dst_line_to_rect(const Vector2 &p_from, Vector2 &r_to, const Rect2 &p_rect);244245void _erase_selected(const bool p_nested_action = false);246void _update_mode();247void _open_menu(const Vector2 &p_position);248bool _create_submenu(PopupMenu *p_menu, Ref<AnimationNodeStateMachine> p_nodesm, const StringName &p_name, const StringName &p_path);249void _stop_connecting();250251bool last_active = false;252StringName last_fading_from_node;253StringName last_current_node;254Vector<StringName> last_travel_path;255256float fade_from_last_play_pos = 0.0f;257float fade_from_current_play_pos = 0.0f;258float fade_from_length = 0.0f;259260float last_play_pos = 0.0f;261float current_play_pos = 0.0f;262float current_length = 0.0f;263264float last_fading_time = 0.0f;265float last_fading_pos = 0.0f;266float fading_time = 0.0f;267float fading_pos = 0.0f;268269float error_time = 0.0f;270String error_text;271272EditorFileDialog *open_file = nullptr;273Ref<AnimationNode> file_loaded;274void _file_opened(const String &p_file);275276enum {277MENU_LOAD_FILE = 1000,278MENU_PASTE = 1001,279MENU_LOAD_FILE_CONFIRM = 1002280};281282HashSet<StringName> connected_nodes;283void _update_connected_nodes(const StringName &p_node);284285Ref<StyleBox> _adjust_stylebox_opacity(Ref<StyleBox> p_style, float p_opacity);286287protected:288void _notification(int p_what);289static void _bind_methods();290291public:292static AnimationNodeStateMachineEditor *get_singleton() { return singleton; }293294virtual bool can_edit(const Ref<AnimationNode> &p_node) override;295virtual void edit(const Ref<AnimationNode> &p_node) override;296297virtual CursorShape get_cursor_shape(const Point2 &p_pos) const override;298virtual String get_tooltip(const Point2 &p_pos) const override;299300AnimationNodeStateMachineEditor();301};302303class EditorAnimationMultiTransitionEdit : public RefCounted {304GDCLASS(EditorAnimationMultiTransitionEdit, RefCounted);305306struct Transition {307StringName from;308StringName to;309Ref<AnimationNodeStateMachineTransition> transition;310};311312Vector<Transition> transitions;313314protected:315bool _set(const StringName &p_name, const Variant &p_property);316bool _get(const StringName &p_name, Variant &r_property) const;317void _get_property_list(List<PropertyInfo> *p_list) const;318319public:320void add_transition(const StringName &p_from, const StringName &p_to, Ref<AnimationNodeStateMachineTransition> p_transition);321};322323324