diff options
Diffstat (limited to 'editor/plugins/sprite_frames_editor_plugin.cpp')
-rw-r--r-- | editor/plugins/sprite_frames_editor_plugin.cpp | 162 |
1 files changed, 72 insertions, 90 deletions
diff --git a/editor/plugins/sprite_frames_editor_plugin.cpp b/editor/plugins/sprite_frames_editor_plugin.cpp index 34ff34d45b..859fec1628 100644 --- a/editor/plugins/sprite_frames_editor_plugin.cpp +++ b/editor/plugins/sprite_frames_editor_plugin.cpp @@ -41,7 +41,6 @@ void SpriteFramesEditor::_gui_input(Ref<InputEvent> p_event) { } void SpriteFramesEditor::_open_sprite_sheet() { - file_split_sheet->clear_filters(); List<String> extensions; ResourceLoader::get_recognized_extensions_for_type("Texture2D", &extensions); @@ -53,7 +52,6 @@ void SpriteFramesEditor::_open_sprite_sheet() { } void SpriteFramesEditor::_sheet_preview_draw() { - Size2i size = split_sheet_preview->get_size(); int h = split_sheet_h->get_value(); int v = split_sheet_v->get_value(); @@ -61,13 +59,11 @@ void SpriteFramesEditor::_sheet_preview_draw() { int height = size.height / v; const float a = 0.3; for (int i = 1; i < h; i++) { - int x = i * width; split_sheet_preview->draw_line(Point2(x, 0), Point2(x, size.height), Color(1, 1, 1, a)); split_sheet_preview->draw_line(Point2(x + 1, 0), Point2(x + 1, size.height), Color(0, 0, 0, a)); for (int j = 1; j < v; j++) { - int y = j * height; split_sheet_preview->draw_line(Point2(0, y), Point2(size.width, y), Color(1, 1, 1, a)); @@ -102,8 +98,8 @@ void SpriteFramesEditor::_sheet_preview_draw() { split_sheet_dialog->get_ok()->set_disabled(false); split_sheet_dialog->get_ok()->set_text(vformat(TTR("Add %d Frame(s)"), frames_selected.size())); } -void SpriteFramesEditor::_sheet_preview_input(const Ref<InputEvent> &p_event) { +void SpriteFramesEditor::_sheet_preview_input(const Ref<InputEvent> &p_event) { Ref<InputEventMouseButton> mb = p_event; if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) { @@ -145,7 +141,6 @@ void SpriteFramesEditor::_sheet_preview_input(const Ref<InputEvent> &p_event) { } void SpriteFramesEditor::_sheet_add_frames() { - Size2i size = split_sheet_preview->get_size(); int h = split_sheet_h->get_value(); int v = split_sheet_v->get_value(); @@ -158,8 +153,9 @@ void SpriteFramesEditor::_sheet_add_frames() { Rect2 region_rect = Rect2(); - if (atlas_source && atlas_source->get_atlas().is_valid()) + if (atlas_source && atlas_source->get_atlas().is_valid()) { region_rect = atlas_source->get_region(); + } for (Set<int>::Element *E = frames_selected.front(); E; E = E->next()) { int idx = E->get(); @@ -185,7 +181,6 @@ void SpriteFramesEditor::_sheet_add_frames() { } void SpriteFramesEditor::_sheet_select_clear_all_frames() { - bool should_clear = true; for (int i = 0; i < split_sheet_h->get_value() * split_sheet_v->get_value(); i++) { if (!frames_selected.has(i)) { @@ -201,14 +196,12 @@ void SpriteFramesEditor::_sheet_select_clear_all_frames() { } void SpriteFramesEditor::_sheet_spin_changed(double) { - frames_selected.clear(); last_frame_selected = -1; split_sheet_preview->update(); } void SpriteFramesEditor::_prepare_sprite_sheet(const String &p_file) { - Ref<Resource> texture = ResourceLoader::load(p_file); if (!texture.is_valid()) { EditorNode::get_singleton()->show_warning(TTR("Unable to load images")); @@ -227,7 +220,6 @@ void SpriteFramesEditor::_prepare_sprite_sheet(const String &p_file) { } void SpriteFramesEditor::_notification(int p_what) { - switch (p_what) { case NOTIFICATION_ENTER_TREE: { load->set_icon(get_theme_icon("Load", "EditorIcons")); @@ -253,13 +245,11 @@ void SpriteFramesEditor::_notification(int p_what) { } void SpriteFramesEditor::_file_load_request(const Vector<String> &p_path, int p_at_pos) { - ERR_FAIL_COND(!frames->has_animation(edited_anim)); List<Ref<Texture2D>> resources; for (int i = 0; i < p_path.size(); i++) { - Ref<Texture2D> resource; resource = ResourceLoader::load(p_path[i]); @@ -286,7 +276,6 @@ void SpriteFramesEditor::_file_load_request(const Vector<String> &p_path, int p_ int count = 0; for (List<Ref<Texture2D>>::Element *E = resources.front(); E; E = E->next()) { - undo_redo->add_do_method(frames, "add_frame", edited_anim, E->get(), p_at_pos == -1 ? -1 : p_at_pos + count); undo_redo->add_undo_method(frames, "remove_frame", edited_anim, p_at_pos == -1 ? fc : p_at_pos); count++; @@ -298,15 +287,15 @@ void SpriteFramesEditor::_file_load_request(const Vector<String> &p_path, int p_ } void SpriteFramesEditor::_load_pressed() { - ERR_FAIL_COND(!frames->has_animation(edited_anim)); loading_scene = false; file->clear_filters(); List<String> extensions; ResourceLoader::get_recognized_extensions_for_type("Texture2D", &extensions); - for (int i = 0; i < extensions.size(); i++) + for (int i = 0; i < extensions.size(); i++) { file->add_filter("*." + extensions[i]); + } file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES); @@ -314,7 +303,6 @@ void SpriteFramesEditor::_load_pressed() { } void SpriteFramesEditor::_paste_pressed() { - ERR_FAIL_COND(!frames->has_animation(edited_anim)); Ref<Texture2D> r = EditorSettings::get_singleton()->get_resource_clipboard(); @@ -338,8 +326,9 @@ void SpriteFramesEditor::_paste_pressed() { void SpriteFramesEditor::_copy_pressed() { ERR_FAIL_COND(!frames->has_animation(edited_anim)); - if (tree->get_current() < 0) + if (tree->get_current() < 0) { return; + } Ref<Texture2D> r = frames->get_frame(edited_anim, tree->get_current()); if (!r.is_valid()) { return; @@ -349,13 +338,11 @@ void SpriteFramesEditor::_copy_pressed() { } void SpriteFramesEditor::_empty_pressed() { - ERR_FAIL_COND(!frames->has_animation(edited_anim)); int from = -1; if (tree->get_current() >= 0) { - from = tree->get_current(); sel = from; @@ -374,13 +361,11 @@ void SpriteFramesEditor::_empty_pressed() { } void SpriteFramesEditor::_empty2_pressed() { - ERR_FAIL_COND(!frames->has_animation(edited_anim)); int from = -1; if (tree->get_current() >= 0) { - from = tree->get_current(); sel = from; @@ -399,15 +384,16 @@ void SpriteFramesEditor::_empty2_pressed() { } void SpriteFramesEditor::_up_pressed() { - ERR_FAIL_COND(!frames->has_animation(edited_anim)); - if (tree->get_current() < 0) + if (tree->get_current() < 0) { return; + } int to_move = tree->get_current(); - if (to_move < 1) + if (to_move < 1) { return; + } sel = to_move; sel -= 1; @@ -423,15 +409,16 @@ void SpriteFramesEditor::_up_pressed() { } void SpriteFramesEditor::_down_pressed() { - ERR_FAIL_COND(!frames->has_animation(edited_anim)); - if (tree->get_current() < 0) + if (tree->get_current() < 0) { return; + } int to_move = tree->get_current(); - if (to_move < 0 || to_move >= frames->get_frame_count(edited_anim) - 1) + if (to_move < 0 || to_move >= frames->get_frame_count(edited_anim) - 1) { return; + } sel = to_move; sel += 1; @@ -447,11 +434,11 @@ void SpriteFramesEditor::_down_pressed() { } void SpriteFramesEditor::_delete_pressed() { - ERR_FAIL_COND(!frames->has_animation(edited_anim)); - if (tree->get_current() < 0) + if (tree->get_current() < 0) { return; + } int to_delete = tree->get_current(); if (to_delete < 0 || to_delete >= frames->get_frame_count(edited_anim)) { @@ -467,14 +454,15 @@ void SpriteFramesEditor::_delete_pressed() { } void SpriteFramesEditor::_animation_select() { - - if (updating) + if (updating) { return; + } if (frames->has_animation(edited_anim)) { double value = anim_speed->get_line_edit()->get_text().to_double(); - if (!Math::is_equal_approx(value, frames->get_animation_speed(edited_anim))) + if (!Math::is_equal_approx(value, frames->get_animation_speed(edited_anim))) { _animation_fps_changed(value); + } } TreeItem *selected = animations->get_selected(); @@ -484,12 +472,13 @@ void SpriteFramesEditor::_animation_select() { } static void _find_anim_sprites(Node *p_node, List<Node *> *r_nodes, Ref<SpriteFrames> p_sfames) { - Node *edited = EditorNode::get_singleton()->get_edited_scene(); - if (!edited) + if (!edited) { return; - if (p_node != edited && p_node->get_owner() != edited) + } + if (p_node != edited && p_node->get_owner() != edited) { return; + } { AnimatedSprite2D *as = Object::cast_to<AnimatedSprite2D>(p_node); @@ -511,21 +500,24 @@ static void _find_anim_sprites(Node *p_node, List<Node *> *r_nodes, Ref<SpriteFr } void SpriteFramesEditor::_animation_name_edited() { - - if (updating) + if (updating) { return; + } - if (!frames->has_animation(edited_anim)) + if (!frames->has_animation(edited_anim)) { return; + } TreeItem *edited = animations->get_edited(); - if (!edited) + if (!edited) { return; + } String new_name = edited->get_text(0); - if (new_name == String(edited_anim)) + if (new_name == String(edited_anim)) { return; + } new_name = new_name.replace("/", "_").replace(",", " "); @@ -544,7 +536,6 @@ void SpriteFramesEditor::_animation_name_edited() { undo_redo->add_undo_method(frames, "rename_animation", name, edited_anim); for (List<Node *>::Element *E = nodes.front(); E; E = E->next()) { - String current = E->get()->call("get_animation"); undo_redo->add_do_method(E->get(), "set_animation", name); undo_redo->add_undo_method(E->get(), "set_animation", edited_anim); @@ -559,7 +550,6 @@ void SpriteFramesEditor::_animation_name_edited() { } void SpriteFramesEditor::_animation_add() { - String name = "New Anim"; int counter = 0; while (frames->has_animation(name)) { @@ -577,7 +567,6 @@ void SpriteFramesEditor::_animation_add() { undo_redo->add_undo_method(this, "_update_library"); for (List<Node *>::Element *E = nodes.front(); E; E = E->next()) { - String current = E->get()->call("get_animation"); undo_redo->add_do_method(E->get(), "set_animation", name); undo_redo->add_undo_method(E->get(), "set_animation", current); @@ -590,19 +579,19 @@ void SpriteFramesEditor::_animation_add() { } void SpriteFramesEditor::_animation_remove() { - - if (updating) + if (updating) { return; + } - if (!frames->has_animation(edited_anim)) + if (!frames->has_animation(edited_anim)) { return; + } delete_dialog->set_text(TTR("Delete Animation?")); delete_dialog->popup_centered(); } void SpriteFramesEditor::_animation_remove_confirmed() { - undo_redo->create_action(TTR("Remove Animation")); undo_redo->add_do_method(frames, "remove_animation", edited_anim); undo_redo->add_undo_method(frames, "add_animation", edited_anim); @@ -622,9 +611,9 @@ void SpriteFramesEditor::_animation_remove_confirmed() { } void SpriteFramesEditor::_animation_loop_changed() { - - if (updating) + if (updating) { return; + } undo_redo->create_action(TTR("Change Animation Loop")); undo_redo->add_do_method(frames, "set_animation_loop", edited_anim, anim_loop->is_pressed()); @@ -635,9 +624,9 @@ void SpriteFramesEditor::_animation_loop_changed() { } void SpriteFramesEditor::_animation_fps_changed(double p_value) { - - if (updating) + if (updating) { return; + } undo_redo->create_action(TTR("Change Animation FPS"), UndoRedo::MERGE_ENDS); undo_redo->add_do_method(frames, "set_animation_speed", edited_anim, p_value); @@ -649,7 +638,6 @@ void SpriteFramesEditor::_animation_fps_changed(double p_value) { } void SpriteFramesEditor::_update_library(bool p_skip_selector) { - updating = true; if (!p_skip_selector) { @@ -664,7 +652,6 @@ void SpriteFramesEditor::_update_library(bool p_skip_selector) { anim_names.sort_custom<StringName::AlphCompare>(); for (List<StringName>::Element *E = anim_names.front(); E; E = E->next()) { - String name = E->get(); TreeItem *it = animations->create_item(anim_root); @@ -687,18 +674,17 @@ void SpriteFramesEditor::_update_library(bool p_skip_selector) { return; } - if (sel >= frames->get_frame_count(edited_anim)) + if (sel >= frames->get_frame_count(edited_anim)) { sel = frames->get_frame_count(edited_anim) - 1; - else if (sel < 0 && frames->get_frame_count(edited_anim)) + } else if (sel < 0 && frames->get_frame_count(edited_anim)) { sel = 0; + } for (int i = 0; i < frames->get_frame_count(edited_anim); i++) { - String name; Ref<Texture2D> icon; if (frames->get_frame(edited_anim, i).is_null()) { - name = itos(i) + ": " + TTR("(empty)"); } else { @@ -707,10 +693,12 @@ void SpriteFramesEditor::_update_library(bool p_skip_selector) { } tree->add_item(name, icon); - if (frames->get_frame(edited_anim, i).is_valid()) + if (frames->get_frame(edited_anim, i).is_valid()) { tree->set_item_tooltip(tree->get_item_count() - 1, frames->get_frame(edited_anim, i)->get_path()); - if (sel == i) + } + if (sel == i) { tree->select(tree->get_item_count() - 1); + } } anim_speed->set_value(frames->get_animation_speed(edited_anim)); @@ -721,16 +709,14 @@ void SpriteFramesEditor::_update_library(bool p_skip_selector) { } void SpriteFramesEditor::edit(SpriteFrames *p_frames) { - - if (frames == p_frames) + if (frames == p_frames) { return; + } frames = p_frames; if (p_frames) { - if (!p_frames->has_animation(edited_anim)) { - List<StringName> anim_names; frames->get_animation_list(&anim_names); anim_names.sort_custom<StringName::AlphCompare>(); @@ -743,25 +729,26 @@ void SpriteFramesEditor::edit(SpriteFrames *p_frames) { _update_library(); } else { - hide(); } } Variant SpriteFramesEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) { - - if (!frames->has_animation(edited_anim)) + if (!frames->has_animation(edited_anim)) { return false; + } int idx = tree->get_item_at_position(p_point, true); - if (idx < 0 || idx >= frames->get_frame_count(edited_anim)) + if (idx < 0 || idx >= frames->get_frame_count(edited_anim)) { return Variant(); + } RES frame = frames->get_frame(edited_anim, idx); - if (frame.is_null()) + if (frame.is_null()) { return Variant(); + } Dictionary drag_data = EditorNode::get_singleton()->drag_resource(frame, p_from); drag_data["frame"] = idx; // store the frame, in case we want to reorder frames inside 'drop_data_fw' @@ -769,15 +756,16 @@ Variant SpriteFramesEditor::get_drag_data_fw(const Point2 &p_point, Control *p_f } bool SpriteFramesEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const { - Dictionary d = p_data; - if (!d.has("type")) + if (!d.has("type")) { return false; + } // reordering frames - if (d.has("from") && (Object *)(d["from"]) == tree) + if (d.has("from") && (Object *)(d["from"]) == tree) { return true; + } if (String(d["type"]) == "resource" && d.has("resource")) { RES r = d["resource"]; @@ -785,17 +773,16 @@ bool SpriteFramesEditor::can_drop_data_fw(const Point2 &p_point, const Variant & Ref<Texture2D> texture = r; if (texture.is_valid()) { - return true; } } if (String(d["type"]) == "files") { - Vector<String> files = d["files"]; - if (files.size() == 0) + if (files.size() == 0) { return false; + } for (int i = 0; i < files.size(); i++) { String file = files[i]; @@ -812,14 +799,15 @@ bool SpriteFramesEditor::can_drop_data_fw(const Point2 &p_point, const Variant & } void SpriteFramesEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) { - - if (!can_drop_data_fw(p_point, p_data, p_from)) + if (!can_drop_data_fw(p_point, p_data, p_from)) { return; + } Dictionary d = p_data; - if (!d.has("type")) + if (!d.has("type")) { return; + } int at_pos = tree->get_item_at_position(p_point, true); @@ -830,13 +818,15 @@ void SpriteFramesEditor::drop_data_fw(const Point2 &p_point, const Variant &p_da if (texture.is_valid()) { bool reorder = false; - if (d.has("from") && (Object *)(d["from"]) == tree) + if (d.has("from") && (Object *)(d["from"]) == tree) { reorder = true; + } if (reorder) { //drop is from reordering frames int from_frame = -1; - if (d.has("frame")) + if (d.has("frame")) { from_frame = d["frame"]; + } undo_redo->create_action(TTR("Move Frame")); undo_redo->add_do_method(frames, "remove_frame", edited_anim, from_frame == -1 ? frames->get_frame_count(edited_anim) : from_frame); @@ -858,7 +848,6 @@ void SpriteFramesEditor::drop_data_fw(const Point2 &p_point, const Variant &p_da } if (String(d["type"]) == "files") { - Vector<String> files = d["files"]; _file_load_request(files, at_pos); @@ -866,7 +855,6 @@ void SpriteFramesEditor::drop_data_fw(const Point2 &p_point, const Variant &p_da } void SpriteFramesEditor::_bind_methods() { - ClassDB::bind_method(D_METHOD("_update_library", "skipsel"), &SpriteFramesEditor::_update_library, DEFVAL(false)); ClassDB::bind_method(D_METHOD("get_drag_data_fw"), &SpriteFramesEditor::get_drag_data_fw); ClassDB::bind_method(D_METHOD("can_drop_data_fw"), &SpriteFramesEditor::can_drop_data_fw); @@ -874,7 +862,6 @@ void SpriteFramesEditor::_bind_methods() { } SpriteFramesEditor::SpriteFramesEditor() { - VBoxContainer *vbc_animlist = memnew(VBoxContainer); add_child(vbc_animlist); vbc_animlist->set_custom_minimum_size(Size2(150, 0) * EDSCALE); @@ -1071,7 +1058,6 @@ SpriteFramesEditor::SpriteFramesEditor() { } void SpriteFramesEditorPlugin::edit(Object *p_object) { - frames_editor->set_undo_redo(&get_undo_redo()); SpriteFrames *s; @@ -1083,7 +1069,6 @@ void SpriteFramesEditorPlugin::edit(Object *p_object) { if (animated_sprite_3d) { s = *animated_sprite_3d->get_sprite_frames(); } else { - s = Object::cast_to<SpriteFrames>(p_object); } } @@ -1092,7 +1077,6 @@ void SpriteFramesEditorPlugin::edit(Object *p_object) { } bool SpriteFramesEditorPlugin::handles(Object *p_object) const { - AnimatedSprite2D *animated_sprite = Object::cast_to<AnimatedSprite2D>(p_object); AnimatedSprite3D *animated_sprite_3d = Object::cast_to<AnimatedSprite3D>(p_object); if (animated_sprite && *animated_sprite->get_sprite_frames()) { @@ -1105,20 +1089,18 @@ bool SpriteFramesEditorPlugin::handles(Object *p_object) const { } void SpriteFramesEditorPlugin::make_visible(bool p_visible) { - if (p_visible) { button->show(); editor->make_bottom_panel_item_visible(frames_editor); } else { - button->hide(); - if (frames_editor->is_visible_in_tree()) + if (frames_editor->is_visible_in_tree()) { editor->hide_bottom_panel(); + } } } SpriteFramesEditorPlugin::SpriteFramesEditorPlugin(EditorNode *p_node) { - editor = p_node; frames_editor = memnew(SpriteFramesEditor); frames_editor->set_custom_minimum_size(Size2(0, 300) * EDSCALE); |