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