animation_state_machine_editor.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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/editor_plugin.h"
  33. #include "editor/plugins/animation_tree_editor_plugin.h"
  34. #include "scene/animation/animation_node_state_machine.h"
  35. #include "scene/gui/button.h"
  36. #include "scene/gui/graph_edit.h"
  37. #include "scene/gui/popup.h"
  38. #include "scene/gui/tree.h"
  39. class EditorFileDialog;
  40. class EditorUndoRedoManager;
  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. Button *tool_group = nullptr;
  49. Button *tool_ungroup = nullptr;
  50. Popup *name_edit_popup = nullptr;
  51. LineEdit *name_edit = nullptr;
  52. HBoxContainer *tool_erase_hb = nullptr;
  53. Button *tool_erase = nullptr;
  54. OptionButton *transition_mode = 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. bool updating = false;
  66. Ref<EditorUndoRedoManager> undo_redo;
  67. static AnimationNodeStateMachineEditor *singleton;
  68. void _state_machine_gui_input(const Ref<InputEvent> &p_event);
  69. void _connection_draw(const Vector2 &p_from, const Vector2 &p_to, AnimationNodeStateMachineTransition::SwitchMode p_mode, bool p_enabled, bool p_selected, bool p_travel, bool p_auto_advance, bool p_multi_transitions);
  70. void _state_machine_draw();
  71. void _state_machine_pos_draw();
  72. void _update_graph();
  73. PopupMenu *menu = nullptr;
  74. PopupMenu *connect_menu = nullptr;
  75. PopupMenu *state_machine_menu = nullptr;
  76. PopupMenu *end_menu = nullptr;
  77. PopupMenu *animations_menu = nullptr;
  78. Vector<String> animations_to_add;
  79. Vector<String> nodes_to_connect;
  80. Vector2 add_node_pos;
  81. ConfirmationDialog *delete_window;
  82. Tree *delete_tree;
  83. bool box_selecting = false;
  84. Point2 box_selecting_from;
  85. Point2 box_selecting_to;
  86. Rect2 box_selecting_rect;
  87. HashSet<StringName> previous_selected;
  88. bool dragging_selected_attempt = false;
  89. bool dragging_selected = false;
  90. Vector2 drag_from;
  91. Vector2 drag_ofs;
  92. StringName snap_x;
  93. StringName snap_y;
  94. bool connecting = false;
  95. StringName connecting_from;
  96. Vector2 connecting_to;
  97. StringName connecting_to_node;
  98. void _add_menu_type(int p_index);
  99. void _add_animation_type(int p_index);
  100. void _connect_to(int p_index);
  101. void _removed_from_graph();
  102. struct NodeRect {
  103. StringName node_name;
  104. Rect2 node;
  105. Rect2 play;
  106. Rect2 name;
  107. Rect2 edit;
  108. };
  109. Vector<NodeRect> node_rects;
  110. struct TransitionLine {
  111. StringName from_node;
  112. StringName to_node;
  113. Vector2 from;
  114. Vector2 to;
  115. AnimationNodeStateMachineTransition::SwitchMode mode;
  116. StringName advance_condition_name;
  117. bool advance_condition_state = false;
  118. bool disabled = false;
  119. bool auto_advance = false;
  120. float width = 0;
  121. bool selected;
  122. bool travel;
  123. bool hidden;
  124. int transition_index;
  125. Vector<TransitionLine> multi_transitions;
  126. };
  127. Vector<TransitionLine> transition_lines;
  128. struct NodeUR {
  129. StringName name;
  130. Ref<AnimationNode> node;
  131. Vector2 position;
  132. };
  133. struct TransitionUR {
  134. StringName new_from;
  135. StringName new_to;
  136. StringName old_from;
  137. StringName old_to;
  138. Ref<AnimationNodeStateMachineTransition> transition;
  139. };
  140. StringName selected_transition_from;
  141. StringName selected_transition_to;
  142. int selected_transition_index;
  143. TransitionLine selected_multi_transition;
  144. void _add_transition(const bool p_nested_action = false);
  145. StringName over_node;
  146. int over_node_what = -1;
  147. String prev_name;
  148. void _name_edited(const String &p_text);
  149. void _name_edited_focus_out();
  150. void _open_editor(const String &p_name);
  151. void _scroll_changed(double);
  152. void _clip_src_line_to_rect(Vector2 &r_from, const Vector2 &p_to, const Rect2 &p_rect);
  153. void _clip_dst_line_to_rect(const Vector2 &p_from, Vector2 &r_to, const Rect2 &p_rect);
  154. void _erase_selected(const bool p_nested_action = false);
  155. void _update_mode();
  156. void _open_menu(const Vector2 &p_position);
  157. void _open_connect_menu(const Vector2 &p_position);
  158. bool _create_submenu(PopupMenu *p_menu, Ref<AnimationNodeStateMachine> p_nodesm, const StringName &p_name, const StringName &p_path, bool from_root = false, Vector<Ref<AnimationNodeStateMachine>> p_parents = Vector<Ref<AnimationNodeStateMachine>>());
  159. void _stop_connecting();
  160. void _group_selected_nodes();
  161. void _ungroup_selected_nodes();
  162. void _delete_selected();
  163. void _delete_all();
  164. void _delete_tree_draw();
  165. bool last_active = false;
  166. StringName last_blend_from_node;
  167. StringName last_current_node;
  168. Vector<StringName> last_travel_path;
  169. float last_play_pos = 0.0f;
  170. float play_pos = 0.0f;
  171. float current_length = 0.0f;
  172. float error_time = 0.0f;
  173. String error_text;
  174. EditorFileDialog *open_file = nullptr;
  175. Ref<AnimationNode> file_loaded;
  176. void _file_opened(const String &p_file);
  177. enum {
  178. MENU_LOAD_FILE = 1000,
  179. MENU_PASTE = 1001,
  180. MENU_LOAD_FILE_CONFIRM = 1002
  181. };
  182. protected:
  183. void _notification(int p_what);
  184. static void _bind_methods();
  185. public:
  186. static AnimationNodeStateMachineEditor *get_singleton() { return singleton; }
  187. virtual bool can_edit(const Ref<AnimationNode> &p_node) override;
  188. virtual void edit(const Ref<AnimationNode> &p_node) override;
  189. virtual CursorShape get_cursor_shape(const Point2 &p_pos) const override;
  190. AnimationNodeStateMachineEditor();
  191. };
  192. class EditorAnimationMultiTransitionEdit : public RefCounted {
  193. GDCLASS(EditorAnimationMultiTransitionEdit, RefCounted);
  194. struct Transition {
  195. StringName from;
  196. StringName to;
  197. Ref<AnimationNodeStateMachineTransition> transition;
  198. };
  199. Vector<Transition> transitions;
  200. protected:
  201. bool _set(const StringName &p_name, const Variant &p_property);
  202. bool _get(const StringName &p_name, Variant &r_property) const;
  203. void _get_property_list(List<PropertyInfo> *p_list) const;
  204. public:
  205. void add_transition(const StringName &p_from, const StringName &p_to, Ref<AnimationNodeStateMachineTransition> p_transition);
  206. EditorAnimationMultiTransitionEdit(){};
  207. };
  208. #endif // ANIMATION_STATE_MACHINE_EDITOR_H