Path: blob/master/editor/scene/sprite_frames_editor_plugin.h
20957 views
/**************************************************************************/1/* sprite_frames_editor_plugin.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "editor/docks/editor_dock.h"33#include "editor/plugins/editor_plugin.h"34#include "scene/gui/button.h"35#include "scene/gui/dialogs.h"36#include "scene/gui/item_list.h"37#include "scene/gui/line_edit.h"38#include "scene/gui/scroll_container.h"39#include "scene/gui/spin_box.h"40#include "scene/gui/texture_rect.h"41#include "scene/gui/tree.h"42#include "scene/resources/image_texture.h"43#include "scene/resources/sprite_frames.h"4445class OptionButton;46class EditorFileDialog;4748class ClipboardSpriteFrames : public Resource {49GDCLASS(ClipboardSpriteFrames, Resource);5051public:52struct Frame {53Ref<Texture2D> texture;54float duration;55};56Vector<Frame> frames;57};5859class ClipboardAnimation : public Resource {60GDCLASS(ClipboardAnimation, Resource);6162public:63String name;64Vector<ClipboardSpriteFrames::Frame> frames;65float speed = 1.0f;66bool loop = false;6768static Ref<ClipboardAnimation> from_sprite_frames(const Ref<SpriteFrames> &p_frames, const String &p_anim);69};7071class SpriteFramesEditor : public EditorDock {72GDCLASS(SpriteFramesEditor, EditorDock);7374Ref<SpriteFrames> frames;75Node *animated_sprite = nullptr;7677enum {78PARAM_USE_CURRENT, // Used in callbacks to indicate `dominant_param` should be not updated.79PARAM_FRAME_COUNT, // Keep "Horizontal" & "Vertical" values.80PARAM_SIZE, // Keep "Size" values.81};82int dominant_param = PARAM_FRAME_COUNT;8384enum {85FRAME_ORDER_SELECTION, // Order frames were selected in.8687// By Row.88FRAME_ORDER_LEFT_RIGHT_TOP_BOTTOM,89FRAME_ORDER_LEFT_RIGHT_BOTTOM_TOP,90FRAME_ORDER_RIGHT_LEFT_TOP_BOTTOM,91FRAME_ORDER_RIGHT_LEFT_BOTTOM_TOP,9293// By Column.94FRAME_ORDER_TOP_BOTTOM_LEFT_RIGHT,95FRAME_ORDER_TOP_BOTTOM_RIGHT_LEFT,96FRAME_ORDER_BOTTOM_TOP_LEFT_RIGHT,97FRAME_ORDER_BOTTOM_TOP_RIGHT_LEFT,98};99100enum {101MENU_SHOW_IN_FILESYSTEM,102};103104int right_clicked_frame = -1;105106bool read_only = false;107108Ref<Texture2D> autoplay_icon;109Ref<Texture2D> stop_icon;110Ref<Texture2D> pause_icon;111Ref<Texture2D> empty_icon = memnew(ImageTexture);112113HBoxContainer *playback_container = nullptr;114Button *stop = nullptr;115Button *play = nullptr;116Button *play_from = nullptr;117Button *play_bw = nullptr;118Button *play_bw_from = nullptr;119120Button *load = nullptr;121Button *load_sheet = nullptr;122Button *delete_frame = nullptr;123Button *copy = nullptr;124Button *paste = nullptr;125Button *empty_before = nullptr;126Button *empty_after = nullptr;127Button *move_up = nullptr;128Button *move_down = nullptr;129Button *zoom_out = nullptr;130Button *zoom_reset = nullptr;131Button *zoom_in = nullptr;132SpinBox *frame_duration = nullptr;133ItemList *frame_list = nullptr;134bool loading_scene;135Vector<int> selection;136137Button *add_anim = nullptr;138Button *duplicate_anim = nullptr;139Button *cut_anim = nullptr;140Button *copy_anim = nullptr;141Button *paste_anim = nullptr;142Button *delete_anim = nullptr;143SpinBox *anim_speed = nullptr;144Button *anim_loop = nullptr;145146HBoxContainer *autoplay_container = nullptr;147Button *autoplay = nullptr;148149LineEdit *anim_search_box = nullptr;150Tree *animations = nullptr;151152Label *missing_anim_label = nullptr;153VBoxContainer *anim_frames_vb = nullptr;154155EditorFileDialog *file = nullptr;156157AcceptDialog *dialog = nullptr;158159PopupMenu *menu = nullptr;160161StringName edited_anim;162163ConfirmationDialog *delete_dialog = nullptr;164165ConfirmationDialog *split_sheet_dialog = nullptr;166ScrollContainer *split_sheet_scroll = nullptr;167TextureRect *split_sheet_preview = nullptr;168VBoxContainer *split_sheet_settings_vb = nullptr;169SpinBox *split_sheet_h = nullptr;170SpinBox *split_sheet_v = nullptr;171SpinBox *split_sheet_size_x = nullptr;172SpinBox *split_sheet_size_y = nullptr;173SpinBox *split_sheet_sep_x = nullptr;174SpinBox *split_sheet_sep_y = nullptr;175SpinBox *split_sheet_offset_x = nullptr;176SpinBox *split_sheet_offset_y = nullptr;177Button *split_sheet_zoom_out = nullptr;178Button *split_sheet_zoom_reset = nullptr;179Button *split_sheet_zoom_in = nullptr;180Button *split_sheet_zoom_fit = nullptr;181Button *toggle_settings_button = nullptr;182OptionButton *split_sheet_order = nullptr;183EditorFileDialog *file_split_sheet = nullptr;184HashMap<int, int> frames_selected; // Key is frame index. Value is selection order.185HashSet<int> frames_toggled_by_mouse_hover;186Vector<Pair<int, int>> frames_ordered; // First is the index to be ordered by. Second is the actual frame index.187int selected_count = 0;188bool frames_need_sort = false;189int last_frame_selected = 0;190191Size2i previous_texture_size;192193float scale_ratio;194int thumbnail_default_size;195float thumbnail_zoom;196float max_thumbnail_zoom;197float min_thumbnail_zoom;198float sheet_zoom;199float max_sheet_zoom;200float min_sheet_zoom;201202Size2i _get_frame_count() const;203Size2i _get_frame_size() const;204Size2i _get_offset() const;205Size2i _get_separation() const;206207void _load_pressed();208void _file_load_request(const Vector<String> &p_path, int p_at_pos = -1);209void _copy_pressed();210void _paste_pressed();211void _paste_frame_array(const Ref<ClipboardSpriteFrames> &p_clipboard_frames);212void _paste_texture(const Ref<Texture2D> &p_texture);213214void _empty_pressed();215void _empty2_pressed();216void _delete_pressed();217void _up_pressed();218void _down_pressed();219void _frame_duration_changed(double p_value);220void _update_library(bool p_skip_selector = false);221void _update_library_impl();222223void _update_stop_icon();224void _play_pressed();225void _play_from_pressed();226void _play_bw_pressed();227void _play_bw_from_pressed();228void _autoplay_pressed();229void _stop_pressed();230231void _animation_selected();232void _animation_name_edited();233void _animation_add();234void _animation_duplicate();235void _animation_cut();236void _animation_copy();237void _animation_paste();238void _animation_remove();239void _animation_remove_confirmed();240void _animation_search_text_changed(const String &p_text);241void _animation_loop_changed();242void _animation_speed_resized();243void _animation_speed_changed(double p_value);244void _animation_remove_undo_redo(const StringName &p_action_name, const Vector<ClipboardSpriteFrames::Frame> *p_frames = nullptr);245246StringName _find_next_animation();247String _generate_unique_animation_name(const String &p_base_name) const;248249void _frame_list_gui_input(const Ref<InputEvent> &p_event);250void _frame_list_item_selected(int p_index, bool p_selected);251252void _menu_selected(int p_id);253254void _zoom_in();255void _zoom_out();256void _zoom_reset();257258bool animations_dirty = false;259bool pending_update = false;260261bool updating;262bool updating_split_settings = false; // Skip SpinBox/Range callback when setting value by code.263264Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);265bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;266void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);267268void _open_sprite_sheet();269void _auto_slice_sprite_sheet();270bool _matches_background_color(const Color &p_background_color, const Color &p_pixel_color);271Size2i _estimate_sprite_sheet_size(const Ref<Texture2D> p_texture);272void _prepare_sprite_sheet(const String &p_file);273int _sheet_preview_position_to_frame_index(const Vector2 &p_position);274void _sheet_preview_draw();275void _sheet_spin_changed(double p_value, int p_dominant_param);276void _sheet_preview_input(const Ref<InputEvent> &p_event);277void _sheet_scroll_input(const Ref<InputEvent> &p_event);278void _sheet_add_frames();279void _sheet_update_zoom_label();280void _sheet_zoom_on_position(float p_zoom, const Vector2 &p_position);281void _sheet_zoom_in();282void _sheet_zoom_out();283void _sheet_zoom_reset();284void _sheet_zoom_fit();285void _sheet_order_selected(int p_option);286void _sheet_select_all_frames();287void _sheet_clear_all_frames();288void _sheet_sort_frames();289void _toggle_show_settings();290void _update_show_settings();291292void _edit();293void _fetch_sprite_node();294void _remove_sprite_node();295296bool sprite_node_updating = false;297void _sync_animation();298299void _select_animation(const String &p_name, bool p_update_node = true);300void _rename_node_animation(EditorUndoRedoManager *undo_redo, bool is_undo, const String &p_filter, const String &p_new_animation, const String &p_new_autoplay);301302protected:303void _notification(int p_what);304void _node_removed(Node *p_node);305static void _bind_methods();306307public:308void edit(Ref<SpriteFrames> p_frames);309Ref<SpriteFrames> get_sprite_frames() const;310311SpriteFramesEditor();312};313314class SpriteFramesEditorPlugin : public EditorPlugin {315GDCLASS(SpriteFramesEditorPlugin, EditorPlugin);316317SpriteFramesEditor *frames_editor = nullptr;318319public:320virtual String get_plugin_name() const override { return "SpriteFrames"; }321bool has_main_screen() const override { return false; }322virtual void edit(Object *p_object) override;323virtual bool handles(Object *p_object) const override;324virtual void make_visible(bool p_visible) override;325326SpriteFramesEditorPlugin();327};328329330