animation_state_machine_editor.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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_group = nullptr;
  52. Button *tool_ungroup = nullptr;
  53. Button *tool_erase = nullptr;
  54. HBoxContainer *transition_tools_hb = nullptr;
  55. OptionButton *switch_mode = nullptr;
  56. Button *auto_advance = nullptr;
  57. OptionButton *play_mode = nullptr;
  58. PanelContainer *panel = nullptr;
  59. StringName selected_node;
  60. HashSet<StringName> selected_nodes;
  61. HScrollBar *h_scroll = nullptr;
  62. VScrollBar *v_scroll = nullptr;
  63. Control *state_machine_draw = nullptr;
  64. Control *state_machine_play_pos = nullptr;
  65. PanelContainer *error_panel = nullptr;
  66. Label *error_label = nullptr;
  67. bool updating = false;
  68. static AnimationNodeStateMachineEditor *singleton;
  69. void _state_machine_gui_input(const Ref<InputEvent> &p_event);
  70. 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_multi_transitions);
  71. void _state_machine_draw();
  72. void _state_machine_pos_draw_individual(String p_name, float p_ratio);
  73. void _state_machine_pos_draw_all();
  74. void _update_graph();
  75. PopupMenu *menu = nullptr;
  76. PopupMenu *connect_menu = nullptr;
  77. PopupMenu *state_machine_menu = nullptr;
  78. PopupMenu *end_menu = nullptr;
  79. PopupMenu *animations_menu = nullptr;
  80. Vector<String> animations_to_add;
  81. Vector<String> nodes_to_connect;
  82. Vector2 add_node_pos;
  83. ConfirmationDialog *delete_window = nullptr;
  84. Tree *delete_tree = nullptr;
  85. bool box_selecting = false;
  86. Point2 box_selecting_from;
  87. Point2 box_selecting_to;
  88. Rect2 box_selecting_rect;
  89. HashSet<StringName> previous_selected;
  90. bool dragging_selected_attempt = false;
  91. bool dragging_selected = false;
  92. Vector2 drag_from;
  93. Vector2 drag_ofs;
  94. StringName snap_x;
  95. StringName snap_y;
  96. bool connecting = false;
  97. bool connection_follows_cursor = false;
  98. StringName connecting_from;
  99. Vector2 connecting_to;
  100. StringName connecting_to_node;
  101. void _add_menu_type(int p_index);
  102. void _add_animation_type(int p_index);
  103. void _connect_to(int p_index);
  104. struct NodeRect {
  105. StringName node_name;
  106. Rect2 node;
  107. Rect2 play;
  108. Rect2 name;
  109. Rect2 edit;
  110. };
  111. Vector<NodeRect> node_rects;
  112. struct TransitionLine {
  113. StringName from_node;
  114. StringName to_node;
  115. Vector2 from;
  116. Vector2 to;
  117. AnimationNodeStateMachineTransition::SwitchMode mode;
  118. StringName advance_condition_name;
  119. bool advance_condition_state = false;
  120. bool disabled = false;
  121. bool auto_advance = false;
  122. float width = 0;
  123. bool selected;
  124. bool travel;
  125. float fade_ratio;
  126. bool hidden;
  127. int transition_index;
  128. Vector<TransitionLine> multi_transitions;
  129. };
  130. Vector<TransitionLine> transition_lines;
  131. struct NodeUR {
  132. StringName name;
  133. Ref<AnimationNode> node;
  134. Vector2 position;
  135. };
  136. struct TransitionUR {
  137. StringName new_from;
  138. StringName new_to;
  139. StringName old_from;
  140. StringName old_to;
  141. Ref<AnimationNodeStateMachineTransition> transition;
  142. };
  143. StringName selected_transition_from;
  144. StringName selected_transition_to;
  145. int selected_transition_index;
  146. TransitionLine selected_multi_transition;
  147. void _add_transition(const bool p_nested_action = false);
  148. StringName over_node;
  149. int over_node_what = -1;
  150. String prev_name;
  151. void _name_edited(const String &p_text);
  152. void _name_edited_focus_out();
  153. void _open_editor(const String &p_name);
  154. void _scroll_changed(double);
  155. void _clip_src_line_to_rect(Vector2 &r_from, const Vector2 &p_to, const Rect2 &p_rect);
  156. void _clip_dst_line_to_rect(const Vector2 &p_from, Vector2 &r_to, const Rect2 &p_rect);
  157. void _erase_selected(const bool p_nested_action = false);
  158. void _update_mode();
  159. void _open_menu(const Vector2 &p_position);
  160. void _open_connect_menu(const Vector2 &p_position);
  161. 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>>());
  162. void _stop_connecting();
  163. void _group_selected_nodes();
  164. void _ungroup_selected_nodes();
  165. void _delete_selected();
  166. void _delete_all();
  167. void _delete_tree_draw();
  168. bool last_active = false;
  169. StringName last_fading_from_node;
  170. StringName last_current_node;
  171. Vector<StringName> last_travel_path;
  172. float fade_from_last_play_pos = 0.0f;
  173. float fade_from_current_play_pos = 0.0f;
  174. float fade_from_length = 0.0f;
  175. float last_play_pos = 0.0f;
  176. float current_play_pos = 0.0f;
  177. float current_length = 0.0f;
  178. float last_fading_time = 0.0f;
  179. float last_fading_pos = 0.0f;
  180. float fading_time = 0.0f;
  181. float fading_pos = 0.0f;
  182. float error_time = 0.0f;
  183. String error_text;
  184. EditorFileDialog *open_file = nullptr;
  185. Ref<AnimationNode> file_loaded;
  186. void _file_opened(const String &p_file);
  187. enum {
  188. MENU_LOAD_FILE = 1000,
  189. MENU_PASTE = 1001,
  190. MENU_LOAD_FILE_CONFIRM = 1002
  191. };
  192. protected:
  193. void _notification(int p_what);
  194. static void _bind_methods();
  195. public:
  196. static AnimationNodeStateMachineEditor *get_singleton() { return singleton; }
  197. virtual bool can_edit(const Ref<AnimationNode> &p_node) override;
  198. virtual void edit(const Ref<AnimationNode> &p_node) override;
  199. virtual CursorShape get_cursor_shape(const Point2 &p_pos) const override;
  200. AnimationNodeStateMachineEditor();
  201. };
  202. class EditorAnimationMultiTransitionEdit : public RefCounted {
  203. GDCLASS(EditorAnimationMultiTransitionEdit, RefCounted);
  204. struct Transition {
  205. StringName from;
  206. StringName to;
  207. Ref<AnimationNodeStateMachineTransition> transition;
  208. };
  209. Vector<Transition> transitions;
  210. protected:
  211. bool _set(const StringName &p_name, const Variant &p_property);
  212. bool _get(const StringName &p_name, Variant &r_property) const;
  213. void _get_property_list(List<PropertyInfo> *p_list) const;
  214. public:
  215. void add_transition(const StringName &p_from, const StringName &p_to, Ref<AnimationNodeStateMachineTransition> p_transition);
  216. EditorAnimationMultiTransitionEdit(){};
  217. };
  218. #endif // ANIMATION_STATE_MACHINE_EDITOR_H