sprite_frames_editor_plugin.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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) 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 SPRITE_FRAMES_EDITOR_PLUGIN_H
  31. #define SPRITE_FRAMES_EDITOR_PLUGIN_H
  32. #include "editor/editor_plugin.h"
  33. #include "scene/2d/animated_sprite_2d.h"
  34. #include "scene/gui/button.h"
  35. #include "scene/gui/check_button.h"
  36. #include "scene/gui/dialogs.h"
  37. #include "scene/gui/item_list.h"
  38. #include "scene/gui/line_edit.h"
  39. #include "scene/gui/scroll_container.h"
  40. #include "scene/gui/spin_box.h"
  41. #include "scene/gui/split_container.h"
  42. #include "scene/gui/texture_rect.h"
  43. #include "scene/gui/tree.h"
  44. class EditorFileDialog;
  45. class EditorUndoRedoManager;
  46. class SpriteFramesEditor : public HSplitContainer {
  47. GDCLASS(SpriteFramesEditor, HSplitContainer);
  48. enum {
  49. PARAM_USE_CURRENT, // Used in callbacks to indicate `dominant_param` should be not updated.
  50. PARAM_FRAME_COUNT, // Keep "Horizontal" & "Vertical" values.
  51. PARAM_SIZE, // Keep "Size" values.
  52. };
  53. int dominant_param = PARAM_FRAME_COUNT;
  54. Button *load = nullptr;
  55. Button *load_sheet = nullptr;
  56. Button *_delete = nullptr;
  57. Button *copy = nullptr;
  58. Button *paste = nullptr;
  59. Button *empty = nullptr;
  60. Button *empty2 = nullptr;
  61. Button *move_up = nullptr;
  62. Button *move_down = nullptr;
  63. Button *zoom_out = nullptr;
  64. Button *zoom_reset = nullptr;
  65. Button *zoom_in = nullptr;
  66. ItemList *tree = nullptr;
  67. bool loading_scene;
  68. int sel;
  69. Button *new_anim = nullptr;
  70. Button *remove_anim = nullptr;
  71. LineEdit *anim_search_box = nullptr;
  72. Tree *animations = nullptr;
  73. SpinBox *anim_speed = nullptr;
  74. CheckButton *anim_loop = nullptr;
  75. EditorFileDialog *file = nullptr;
  76. AcceptDialog *dialog = nullptr;
  77. SpriteFrames *frames = nullptr;
  78. StringName edited_anim;
  79. ConfirmationDialog *delete_dialog = nullptr;
  80. ConfirmationDialog *split_sheet_dialog = nullptr;
  81. ScrollContainer *split_sheet_scroll = nullptr;
  82. TextureRect *split_sheet_preview = nullptr;
  83. SpinBox *split_sheet_h = nullptr;
  84. SpinBox *split_sheet_v = nullptr;
  85. SpinBox *split_sheet_size_x = nullptr;
  86. SpinBox *split_sheet_size_y = nullptr;
  87. SpinBox *split_sheet_sep_x = nullptr;
  88. SpinBox *split_sheet_sep_y = nullptr;
  89. SpinBox *split_sheet_offset_x = nullptr;
  90. SpinBox *split_sheet_offset_y = nullptr;
  91. Button *split_sheet_zoom_out = nullptr;
  92. Button *split_sheet_zoom_reset = nullptr;
  93. Button *split_sheet_zoom_in = nullptr;
  94. EditorFileDialog *file_split_sheet = nullptr;
  95. HashSet<int> frames_selected;
  96. HashSet<int> frames_toggled_by_mouse_hover;
  97. int last_frame_selected = 0;
  98. float scale_ratio;
  99. int thumbnail_default_size;
  100. float thumbnail_zoom;
  101. float max_thumbnail_zoom;
  102. float min_thumbnail_zoom;
  103. float sheet_zoom;
  104. float max_sheet_zoom;
  105. float min_sheet_zoom;
  106. Size2i _get_frame_count() const;
  107. Size2i _get_frame_size() const;
  108. Size2i _get_offset() const;
  109. Size2i _get_separation() const;
  110. void _load_pressed();
  111. void _file_load_request(const Vector<String> &p_path, int p_at_pos = -1);
  112. void _copy_pressed();
  113. void _paste_pressed();
  114. void _empty_pressed();
  115. void _empty2_pressed();
  116. void _delete_pressed();
  117. void _up_pressed();
  118. void _down_pressed();
  119. void _update_library(bool p_skip_selector = false);
  120. void _animation_select();
  121. void _animation_name_edited();
  122. void _animation_add();
  123. void _animation_remove();
  124. void _animation_remove_confirmed();
  125. void _animation_search_text_changed(const String &p_text);
  126. void _animation_loop_changed();
  127. void _animation_fps_changed(double p_value);
  128. void _tree_input(const Ref<InputEvent> &p_event);
  129. void _zoom_in();
  130. void _zoom_out();
  131. void _zoom_reset();
  132. bool updating;
  133. bool updating_split_settings = false; // Skip SpinBox/Range callback when setting value by code.
  134. Ref<EditorUndoRedoManager> undo_redo;
  135. Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
  136. bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
  137. void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
  138. void _open_sprite_sheet();
  139. void _prepare_sprite_sheet(const String &p_file);
  140. int _sheet_preview_position_to_frame_index(const Vector2 &p_position);
  141. void _sheet_preview_draw();
  142. void _sheet_spin_changed(double p_value, int p_dominant_param);
  143. void _sheet_preview_input(const Ref<InputEvent> &p_event);
  144. void _sheet_scroll_input(const Ref<InputEvent> &p_event);
  145. void _sheet_add_frames();
  146. void _sheet_zoom_on_position(float p_zoom, const Vector2 &p_position);
  147. void _sheet_zoom_in();
  148. void _sheet_zoom_out();
  149. void _sheet_zoom_reset();
  150. void _sheet_select_clear_all_frames();
  151. protected:
  152. void _notification(int p_what);
  153. static void _bind_methods();
  154. public:
  155. void set_undo_redo(Ref<EditorUndoRedoManager> p_undo_redo);
  156. void edit(SpriteFrames *p_frames);
  157. SpriteFramesEditor();
  158. };
  159. class SpriteFramesEditorPlugin : public EditorPlugin {
  160. GDCLASS(SpriteFramesEditorPlugin, EditorPlugin);
  161. SpriteFramesEditor *frames_editor = nullptr;
  162. Button *button = nullptr;
  163. public:
  164. virtual String get_name() const override { return "SpriteFrames"; }
  165. bool has_main_screen() const override { return false; }
  166. virtual void edit(Object *p_object) override;
  167. virtual bool handles(Object *p_object) const override;
  168. virtual void make_visible(bool p_visible) override;
  169. SpriteFramesEditorPlugin();
  170. ~SpriteFramesEditorPlugin();
  171. };
  172. #endif // SPRITE_FRAMES_EDITOR_PLUGIN_H