sprite_frames_editor_plugin.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /**************************************************************************/
  2. /* sprite_frames_editor_plugin.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 SPRITE_FRAMES_EDITOR_PLUGIN_H
  31. #define SPRITE_FRAMES_EDITOR_PLUGIN_H
  32. #include "editor/plugins/editor_plugin.h"
  33. #include "scene/2d/animated_sprite_2d.h"
  34. #include "scene/3d/sprite_3d.h"
  35. #include "scene/gui/button.h"
  36. #include "scene/gui/check_button.h"
  37. #include "scene/gui/dialogs.h"
  38. #include "scene/gui/item_list.h"
  39. #include "scene/gui/line_edit.h"
  40. #include "scene/gui/scroll_container.h"
  41. #include "scene/gui/spin_box.h"
  42. #include "scene/gui/split_container.h"
  43. #include "scene/gui/texture_rect.h"
  44. #include "scene/gui/tree.h"
  45. #include "scene/resources/image_texture.h"
  46. class OptionButton;
  47. class EditorFileDialog;
  48. class ClipboardSpriteFrames : public Resource {
  49. GDCLASS(ClipboardSpriteFrames, Resource);
  50. public:
  51. struct Frame {
  52. Ref<Texture2D> texture;
  53. float duration;
  54. };
  55. Vector<Frame> frames;
  56. };
  57. class SpriteFramesEditor : public HSplitContainer {
  58. GDCLASS(SpriteFramesEditor, HSplitContainer);
  59. Ref<SpriteFrames> frames;
  60. Node *animated_sprite = nullptr;
  61. enum {
  62. PARAM_USE_CURRENT, // Used in callbacks to indicate `dominant_param` should be not updated.
  63. PARAM_FRAME_COUNT, // Keep "Horizontal" & "Vertical" values.
  64. PARAM_SIZE, // Keep "Size" values.
  65. };
  66. int dominant_param = PARAM_FRAME_COUNT;
  67. enum {
  68. FRAME_ORDER_SELECTION, // Order frames were selected in.
  69. // By Row.
  70. FRAME_ORDER_LEFT_RIGHT_TOP_BOTTOM,
  71. FRAME_ORDER_LEFT_RIGHT_BOTTOM_TOP,
  72. FRAME_ORDER_RIGHT_LEFT_TOP_BOTTOM,
  73. FRAME_ORDER_RIGHT_LEFT_BOTTOM_TOP,
  74. // By Column.
  75. FRAME_ORDER_TOP_BOTTOM_LEFT_RIGHT,
  76. FRAME_ORDER_TOP_BOTTOM_RIGHT_LEFT,
  77. FRAME_ORDER_BOTTOM_TOP_LEFT_RIGHT,
  78. FRAME_ORDER_BOTTOM_TOP_RIGHT_LEFT,
  79. };
  80. bool read_only = false;
  81. Ref<Texture2D> autoplay_icon;
  82. Ref<Texture2D> stop_icon;
  83. Ref<Texture2D> pause_icon;
  84. Ref<Texture2D> empty_icon = memnew(ImageTexture);
  85. HBoxContainer *playback_container = nullptr;
  86. Button *stop = nullptr;
  87. Button *play = nullptr;
  88. Button *play_from = nullptr;
  89. Button *play_bw = nullptr;
  90. Button *play_bw_from = nullptr;
  91. Button *load = nullptr;
  92. Button *load_sheet = nullptr;
  93. Button *delete_frame = nullptr;
  94. Button *copy = nullptr;
  95. Button *paste = nullptr;
  96. Button *empty_before = nullptr;
  97. Button *empty_after = nullptr;
  98. Button *move_up = nullptr;
  99. Button *move_down = nullptr;
  100. Button *zoom_out = nullptr;
  101. Button *zoom_reset = nullptr;
  102. Button *zoom_in = nullptr;
  103. SpinBox *frame_duration = nullptr;
  104. ItemList *frame_list = nullptr;
  105. bool loading_scene;
  106. Vector<int> selection;
  107. Button *add_anim = nullptr;
  108. Button *duplicate_anim = nullptr;
  109. Button *delete_anim = nullptr;
  110. SpinBox *anim_speed = nullptr;
  111. Button *anim_loop = nullptr;
  112. HBoxContainer *autoplay_container = nullptr;
  113. Button *autoplay = nullptr;
  114. LineEdit *anim_search_box = nullptr;
  115. Tree *animations = nullptr;
  116. Label *missing_anim_label = nullptr;
  117. VBoxContainer *anim_frames_vb = nullptr;
  118. EditorFileDialog *file = nullptr;
  119. AcceptDialog *dialog = nullptr;
  120. StringName edited_anim;
  121. ConfirmationDialog *delete_dialog = nullptr;
  122. ConfirmationDialog *split_sheet_dialog = nullptr;
  123. ScrollContainer *split_sheet_scroll = nullptr;
  124. TextureRect *split_sheet_preview = nullptr;
  125. VBoxContainer *split_sheet_settings_vb = nullptr;
  126. SpinBox *split_sheet_h = nullptr;
  127. SpinBox *split_sheet_v = nullptr;
  128. SpinBox *split_sheet_size_x = nullptr;
  129. SpinBox *split_sheet_size_y = nullptr;
  130. SpinBox *split_sheet_sep_x = nullptr;
  131. SpinBox *split_sheet_sep_y = nullptr;
  132. SpinBox *split_sheet_offset_x = nullptr;
  133. SpinBox *split_sheet_offset_y = nullptr;
  134. Button *split_sheet_zoom_out = nullptr;
  135. Button *split_sheet_zoom_reset = nullptr;
  136. Button *split_sheet_zoom_in = nullptr;
  137. Button *toggle_settings_button = nullptr;
  138. OptionButton *split_sheet_order = nullptr;
  139. EditorFileDialog *file_split_sheet = nullptr;
  140. HashMap<int, int> frames_selected; // Key is frame index. Value is selection order.
  141. HashSet<int> frames_toggled_by_mouse_hover;
  142. Vector<Pair<int, int>> frames_ordered; // First is the index to be ordered by. Second is the actual frame index.
  143. int selected_count = 0;
  144. bool frames_need_sort = false;
  145. int last_frame_selected = 0;
  146. Size2i previous_texture_size;
  147. float scale_ratio;
  148. int thumbnail_default_size;
  149. float thumbnail_zoom;
  150. float max_thumbnail_zoom;
  151. float min_thumbnail_zoom;
  152. float sheet_zoom;
  153. float max_sheet_zoom;
  154. float min_sheet_zoom;
  155. Size2i _get_frame_count() const;
  156. Size2i _get_frame_size() const;
  157. Size2i _get_offset() const;
  158. Size2i _get_separation() const;
  159. void _load_pressed();
  160. void _file_load_request(const Vector<String> &p_path, int p_at_pos = -1);
  161. void _copy_pressed();
  162. void _paste_pressed();
  163. void _paste_frame_array(const Ref<ClipboardSpriteFrames> &p_clipboard_frames);
  164. void _paste_texture(const Ref<Texture2D> &p_texture);
  165. void _empty_pressed();
  166. void _empty2_pressed();
  167. void _delete_pressed();
  168. void _up_pressed();
  169. void _down_pressed();
  170. void _frame_duration_changed(double p_value);
  171. void _update_library(bool p_skip_selector = false);
  172. void _update_library_impl();
  173. void _update_stop_icon();
  174. void _play_pressed();
  175. void _play_from_pressed();
  176. void _play_bw_pressed();
  177. void _play_bw_from_pressed();
  178. void _autoplay_pressed();
  179. void _stop_pressed();
  180. void _animation_selected();
  181. void _animation_name_edited();
  182. void _animation_add();
  183. void _animation_duplicate();
  184. void _animation_remove();
  185. void _animation_remove_confirmed();
  186. void _animation_search_text_changed(const String &p_text);
  187. void _animation_loop_changed();
  188. void _animation_speed_changed(double p_value);
  189. void _frame_list_gui_input(const Ref<InputEvent> &p_event);
  190. void _frame_list_item_selected(int p_index, bool p_selected);
  191. void _zoom_in();
  192. void _zoom_out();
  193. void _zoom_reset();
  194. bool animations_dirty = false;
  195. bool pending_update = false;
  196. bool updating;
  197. bool updating_split_settings = false; // Skip SpinBox/Range callback when setting value by code.
  198. Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
  199. bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
  200. void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
  201. void _open_sprite_sheet();
  202. void _auto_slice_sprite_sheet();
  203. bool _matches_background_color(const Color &p_background_color, const Color &p_pixel_color);
  204. Size2i _estimate_sprite_sheet_size(const Ref<Texture2D> p_texture);
  205. void _prepare_sprite_sheet(const String &p_file);
  206. int _sheet_preview_position_to_frame_index(const Vector2 &p_position);
  207. void _sheet_preview_draw();
  208. void _sheet_spin_changed(double p_value, int p_dominant_param);
  209. void _sheet_preview_input(const Ref<InputEvent> &p_event);
  210. void _sheet_scroll_input(const Ref<InputEvent> &p_event);
  211. void _sheet_add_frames();
  212. void _sheet_zoom_on_position(float p_zoom, const Vector2 &p_position);
  213. void _sheet_zoom_in();
  214. void _sheet_zoom_out();
  215. void _sheet_zoom_reset();
  216. void _sheet_order_selected(int p_option);
  217. void _sheet_select_all_frames();
  218. void _sheet_clear_all_frames();
  219. void _sheet_sort_frames();
  220. void _toggle_show_settings();
  221. void _update_show_settings();
  222. void _edit();
  223. void _fetch_sprite_node();
  224. void _remove_sprite_node();
  225. bool sprite_node_updating = false;
  226. void _sync_animation();
  227. void _select_animation(const String &p_name, bool p_update_node = true);
  228. void _rename_node_animation(EditorUndoRedoManager *undo_redo, bool is_undo, const String &p_filter, const String &p_new_animation, const String &p_new_autoplay);
  229. protected:
  230. void _notification(int p_what);
  231. void _node_removed(Node *p_node);
  232. static void _bind_methods();
  233. public:
  234. void edit(Ref<SpriteFrames> p_frames);
  235. Ref<SpriteFrames> get_sprite_frames() const;
  236. SpriteFramesEditor();
  237. };
  238. class SpriteFramesEditorPlugin : public EditorPlugin {
  239. GDCLASS(SpriteFramesEditorPlugin, EditorPlugin);
  240. SpriteFramesEditor *frames_editor = nullptr;
  241. Button *button = nullptr;
  242. public:
  243. virtual String get_name() const override { return "SpriteFrames"; }
  244. bool has_main_screen() const override { return false; }
  245. virtual void edit(Object *p_object) override;
  246. virtual bool handles(Object *p_object) const override;
  247. virtual void make_visible(bool p_visible) override;
  248. SpriteFramesEditorPlugin();
  249. ~SpriteFramesEditorPlugin();
  250. };
  251. #endif // SPRITE_FRAMES_EDITOR_PLUGIN_H