animation_state_machine_editor.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /**************************************************************************/
  2. /* animation_state_machine_editor.h */
  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. #ifndef ANIMATION_STATE_MACHINE_EDITOR_H
  31. #define ANIMATION_STATE_MACHINE_EDITOR_H
  32. #include "editor/plugins/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"
  36. #include "scene/gui/tree.h"
  37. class ConfirmationDialog;
  38. class EditorFileDialog;
  39. class OptionButton;
  40. class PanelContainer;
  41. class AnimationNodeStateMachineEditor : public AnimationTreeNodeEditorPlugin {
  42. GDCLASS(AnimationNodeStateMachineEditor, AnimationTreeNodeEditorPlugin);
  43. Ref<AnimationNodeStateMachine> state_machine;
  44. bool read_only = false;
  45. Button *tool_select = nullptr;
  46. Button *tool_create = nullptr;
  47. Button *tool_connect = nullptr;
  48. Popup *name_edit_popup = nullptr;
  49. LineEdit *name_edit = nullptr;
  50. HBoxContainer *selection_tools_hb = nullptr;
  51. Button *tool_erase = nullptr;
  52. HBoxContainer *transition_tools_hb = nullptr;
  53. OptionButton *switch_mode = nullptr;
  54. Button *auto_advance = nullptr;
  55. OptionButton *play_mode = nullptr;
  56. PanelContainer *panel = nullptr;
  57. StringName selected_node;
  58. HashSet<StringName> selected_nodes;
  59. HScrollBar *h_scroll = nullptr;
  60. VScrollBar *v_scroll = nullptr;
  61. Control *state_machine_draw = nullptr;
  62. Control *state_machine_play_pos = nullptr;
  63. PanelContainer *error_panel = nullptr;
  64. Label *error_label = nullptr;
  65. struct ThemeCache {
  66. Ref<StyleBox> panel_style;
  67. Ref<StyleBox> error_panel_style;
  68. Color error_color;
  69. Ref<Texture2D> tool_icon_select;
  70. Ref<Texture2D> tool_icon_create;
  71. Ref<Texture2D> tool_icon_connect;
  72. Ref<Texture2D> tool_icon_erase;
  73. Ref<Texture2D> transition_icon_immediate;
  74. Ref<Texture2D> transition_icon_sync;
  75. Ref<Texture2D> transition_icon_end;
  76. Ref<Texture2D> play_icon_start;
  77. Ref<Texture2D> play_icon_travel;
  78. Ref<Texture2D> play_icon_auto;
  79. Ref<Texture2D> animation_icon;
  80. Ref<StyleBox> node_frame;
  81. Ref<StyleBox> node_frame_selected;
  82. Ref<StyleBox> node_frame_playing;
  83. Ref<StyleBox> node_frame_start;
  84. Ref<StyleBox> node_frame_end;
  85. Ref<Font> node_title_font;
  86. int node_title_font_size = 0;
  87. Color node_title_font_color;
  88. Ref<Texture2D> play_node;
  89. Ref<Texture2D> edit_node;
  90. Color transition_color;
  91. Color transition_disabled_color;
  92. Color transition_icon_color;
  93. Color transition_icon_disabled_color;
  94. Color highlight_color;
  95. Color highlight_disabled_color;
  96. Color focus_color;
  97. Color guideline_color;
  98. Ref<Texture2D> transition_icons[6]{};
  99. Color playback_color;
  100. Color playback_background_color;
  101. } theme_cache;
  102. bool updating = false;
  103. static AnimationNodeStateMachineEditor *singleton;
  104. void _state_machine_gui_input(const Ref<InputEvent> &p_event);
  105. void _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);
  106. void _state_machine_draw();
  107. void _state_machine_pos_draw_individual(const String &p_name, float p_ratio);
  108. void _state_machine_pos_draw_all();
  109. void _update_graph();
  110. PopupMenu *menu = nullptr;
  111. PopupMenu *connect_menu = nullptr;
  112. PopupMenu *state_machine_menu = nullptr;
  113. PopupMenu *end_menu = nullptr;
  114. PopupMenu *animations_menu = nullptr;
  115. Vector<String> animations_to_add;
  116. Vector<String> nodes_to_connect;
  117. Vector2 add_node_pos;
  118. ConfirmationDialog *delete_window = nullptr;
  119. Tree *delete_tree = nullptr;
  120. bool box_selecting = false;
  121. Point2 box_selecting_from;
  122. Point2 box_selecting_to;
  123. Rect2 box_selecting_rect;
  124. HashSet<StringName> previous_selected;
  125. bool dragging_selected_attempt = false;
  126. bool dragging_selected = false;
  127. Vector2 drag_from;
  128. Vector2 drag_ofs;
  129. StringName snap_x;
  130. StringName snap_y;
  131. bool connecting = false;
  132. bool connection_follows_cursor = false;
  133. StringName connecting_from;
  134. Vector2 connecting_to;
  135. StringName connecting_to_node;
  136. void _add_menu_type(int p_index);
  137. void _add_animation_type(int p_index);
  138. void _connect_to(int p_index);
  139. struct NodeRect {
  140. StringName node_name;
  141. Rect2 node;
  142. Rect2 play;
  143. Rect2 name;
  144. Rect2 edit;
  145. bool can_edit;
  146. };
  147. Vector<NodeRect> node_rects;
  148. struct TransitionLine {
  149. StringName from_node;
  150. StringName to_node;
  151. Vector2 from;
  152. Vector2 to;
  153. AnimationNodeStateMachineTransition::SwitchMode mode;
  154. StringName advance_condition_name;
  155. bool advance_condition_state = false;
  156. bool disabled = false;
  157. bool auto_advance = false;
  158. float width = 0;
  159. bool selected;
  160. bool travel;
  161. float fade_ratio;
  162. bool hidden;
  163. int transition_index;
  164. bool is_across_group = false;
  165. };
  166. Vector<TransitionLine> transition_lines;
  167. struct NodeUR {
  168. StringName name;
  169. Ref<AnimationNode> node;
  170. Vector2 position;
  171. };
  172. struct TransitionUR {
  173. StringName new_from;
  174. StringName new_to;
  175. StringName old_from;
  176. StringName old_to;
  177. Ref<AnimationNodeStateMachineTransition> transition;
  178. };
  179. StringName selected_transition_from;
  180. StringName selected_transition_to;
  181. int selected_transition_index = -1;
  182. void _add_transition(const bool p_nested_action = false);
  183. enum HoveredNodeArea {
  184. HOVER_NODE_NONE = -1,
  185. HOVER_NODE_PLAY = 0,
  186. HOVER_NODE_EDIT = 1,
  187. };
  188. StringName hovered_node_name;
  189. HoveredNodeArea hovered_node_area = HOVER_NODE_NONE;
  190. String prev_name;
  191. void _name_edited(const String &p_text);
  192. void _name_edited_focus_out();
  193. void _open_editor(const String &p_name);
  194. void _scroll_changed(double);
  195. String _get_root_playback_path(String &r_node_directory);
  196. void _clip_src_line_to_rect(Vector2 &r_from, const Vector2 &p_to, const Rect2 &p_rect);
  197. void _clip_dst_line_to_rect(const Vector2 &p_from, Vector2 &r_to, const Rect2 &p_rect);
  198. void _erase_selected(const bool p_nested_action = false);
  199. void _update_mode();
  200. void _open_menu(const Vector2 &p_position);
  201. bool _create_submenu(PopupMenu *p_menu, Ref<AnimationNodeStateMachine> p_nodesm, const StringName &p_name, const StringName &p_path);
  202. void _stop_connecting();
  203. void _delete_selected();
  204. void _delete_all();
  205. void _delete_tree_draw();
  206. bool last_active = false;
  207. StringName last_fading_from_node;
  208. StringName last_current_node;
  209. Vector<StringName> last_travel_path;
  210. float fade_from_last_play_pos = 0.0f;
  211. float fade_from_current_play_pos = 0.0f;
  212. float fade_from_length = 0.0f;
  213. float last_play_pos = 0.0f;
  214. float current_play_pos = 0.0f;
  215. float current_length = 0.0f;
  216. float last_fading_time = 0.0f;
  217. float last_fading_pos = 0.0f;
  218. float fading_time = 0.0f;
  219. float fading_pos = 0.0f;
  220. float error_time = 0.0f;
  221. String error_text;
  222. EditorFileDialog *open_file = nullptr;
  223. Ref<AnimationNode> file_loaded;
  224. void _file_opened(const String &p_file);
  225. enum {
  226. MENU_LOAD_FILE = 1000,
  227. MENU_PASTE = 1001,
  228. MENU_LOAD_FILE_CONFIRM = 1002
  229. };
  230. HashSet<StringName> connected_nodes;
  231. void _update_connected_nodes(const StringName &p_node);
  232. Ref<StyleBox> _adjust_stylebox_opacity(Ref<StyleBox> p_style, float p_opacity);
  233. protected:
  234. void _notification(int p_what);
  235. static void _bind_methods();
  236. public:
  237. static AnimationNodeStateMachineEditor *get_singleton() { return singleton; }
  238. virtual bool can_edit(const Ref<AnimationNode> &p_node) override;
  239. virtual void edit(const Ref<AnimationNode> &p_node) override;
  240. virtual CursorShape get_cursor_shape(const Point2 &p_pos) const override;
  241. virtual String get_tooltip(const Point2 &p_pos) const override;
  242. AnimationNodeStateMachineEditor();
  243. };
  244. class EditorAnimationMultiTransitionEdit : public RefCounted {
  245. GDCLASS(EditorAnimationMultiTransitionEdit, RefCounted);
  246. struct Transition {
  247. StringName from;
  248. StringName to;
  249. Ref<AnimationNodeStateMachineTransition> transition;
  250. };
  251. Vector<Transition> transitions;
  252. protected:
  253. bool _set(const StringName &p_name, const Variant &p_property);
  254. bool _get(const StringName &p_name, Variant &r_property) const;
  255. void _get_property_list(List<PropertyInfo> *p_list) const;
  256. public:
  257. void add_transition(const StringName &p_from, const StringName &p_to, Ref<AnimationNodeStateMachineTransition> p_transition);
  258. EditorAnimationMultiTransitionEdit() {}
  259. };
  260. #endif // ANIMATION_STATE_MACHINE_EDITOR_H