animation_state_machine_editor.h 9.8 KB

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