Skip to content

Commit

Permalink
Add animation preview functionality and button to SceneImportSettings…
Browse files Browse the repository at this point in the history
… dialog
  • Loading branch information
jeronimo-schreyer committed Apr 24, 2023
1 parent d2357a1 commit f0768f1
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
68 changes: 67 additions & 1 deletion editor/import/scene_import_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ void SceneImportSettings::_fill_scene(Node *p_node, TreeItem *p_parent_item) {
for (const StringName &E : animations) {
_fill_animation(scene_tree, anim_node->get_animation(E), E, item);
}
anim_node->connect("animation_finished", callable_mp(this, &SceneImportSettings::_preview_animation_finished));
}

for (int i = 0; i < p_node->get_child_count(); i++) {
Expand Down Expand Up @@ -582,6 +583,11 @@ void SceneImportSettings::update_view() {
}

void SceneImportSettings::open_settings(const String &p_path, bool p_for_animation) {
if (animation_preview) {
memdelete(animation_preview);
animation_preview = nullptr;
}

if (scene) {
memdelete(scene);
scene = nullptr;
Expand Down Expand Up @@ -689,6 +695,13 @@ Node *SceneImportSettings::get_selected_node() {
void SceneImportSettings::_select(Tree *p_from, String p_type, String p_id) {
selecting = true;
scene_import_settings_data->hide_options = false;
animation_preview_button->set_visible(false);

if (animation_preview) {
animation_preview->stop();
animation_preview->seek(0, true);
animation_preview->clear_queue();
}

if (p_type == "Node") {
node_selected->hide(); //always hide just in case
Expand Down Expand Up @@ -739,13 +752,20 @@ void SceneImportSettings::_select(Tree *p_from, String p_type, String p_id) {
if (Object::cast_to<Node3D>(scene)) {
Object::cast_to<Node3D>(scene)->show();
}
//NodeData &nd=node_map[p_id];
material_tree->deselect_all();
mesh_tree->deselect_all();
AnimationData &ad = animation_map[p_id];

scene_import_settings_data->settings = &ad.settings;
scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_ANIMATION;

String p_parent_id = p_from->get_selected()->get_parent()->get_meta("import_id");
animation_preview = Object::cast_to<AnimationPlayer>(node_map[p_parent_id].node);
animation_preview->play(p_id);
animation_preview->stop();

animation_preview_button->set_visible(true);
animation_preview_button->set_pressed(false);
} else if (p_type == "Mesh") {
node_selected->hide();
if (Object::cast_to<Node3D>(scene)) {
Expand Down Expand Up @@ -894,6 +914,38 @@ void SceneImportSettings::_light_button_pressed(Node *p_button) {
}
}

void SceneImportSettings::_animation_button_pressed() {
DEV_ASSERT(animation_preview);

if (selected_type == "Animation") {
if (animation_preview_button->is_pressed()) {
animation_preview->play(selected_id);
} else {
animation_preview->pause();
}
}
}

void SceneImportSettings::_preview_animation_finished(String anim_name) {
DEV_ASSERT(animation_preview);

HashMap<StringName, Variant> settings = animation_map[selected_id].settings;
Animation::LoopMode loop_mode = static_cast<Animation::LoopMode>((int)settings["settings/loop_mode"]);
if (loop_mode != Animation::LOOP_NONE) {
if (loop_mode == Animation::LOOP_LINEAR) {
animation_preview->play(selected_id);
} else if (loop_mode == Animation::LOOP_PINGPONG) {
if (animation_preview->get_current_animation_position() == animation_preview->get_current_animation_length()) {
animation_preview->play_backwards(selected_id);
} else {
animation_preview->play(selected_id);
}
}
} else {
animation_preview_button->set_pressed(false);
}
}

void SceneImportSettings::_viewport_input(const Ref<InputEvent> &p_input) {
float *rot_x = &cam_rot_x;
float *rot_y = &cam_rot_y;
Expand Down Expand Up @@ -1012,6 +1064,9 @@ void SceneImportSettings::_update_theme_item_cache() {
theme_cache.light_1_off = get_theme_icon(SNAME("MaterialPreviewLight1Off"), SNAME("EditorIcons"));
theme_cache.light_2_on = get_theme_icon(SNAME("MaterialPreviewLight2"), SNAME("EditorIcons"));
theme_cache.light_2_off = get_theme_icon(SNAME("MaterialPreviewLight2Off"), SNAME("EditorIcons"));

theme_cache.animation_pause = get_theme_icon(SNAME("Pause"), SNAME("EditorIcons"));
theme_cache.animation_play = get_theme_icon(SNAME("Play"), SNAME("EditorIcons"));
}

void SceneImportSettings::_notification(int p_what) {
Expand All @@ -1029,6 +1084,9 @@ void SceneImportSettings::_notification(int p_what) {
light_1_switch->set_texture_pressed(theme_cache.light_1_off);
light_2_switch->set_texture_normal(theme_cache.light_2_on);
light_2_switch->set_texture_pressed(theme_cache.light_2_off);

animation_preview_button->set_texture_normal(theme_cache.animation_play);
animation_preview_button->set_texture_pressed(theme_cache.animation_pause);
} break;

case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
Expand Down Expand Up @@ -1397,6 +1455,14 @@ SceneImportSettings::SceneImportSettings() {
vb_toolbar->add_child(light_2_switch);
light_2_switch->connect("pressed", callable_mp(this, &SceneImportSettings::_light_button_pressed).bind(light_2_switch));

animation_preview_button = memnew(TextureButton);
animation_preview_button->set_toggle_mode(true);
animation_preview_button->set_visible(false);
animation_preview_button->set_stretch_mode(TextureButton::STRETCH_KEEP_ASPECT_CENTERED);
animation_preview_button->set_custom_minimum_size(Size2(20, 20));
vb_toolbar->add_child(animation_preview_button);
animation_preview_button->connect("pressed", callable_mp(this, &SceneImportSettings::_animation_button_pressed));

camera = memnew(Camera3D);
base_viewport->add_child(camera);
camera->make_current();
Expand Down
7 changes: 7 additions & 0 deletions editor/import/scene_import_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ class SceneImportSettings : public ConfirmationDialog {
Ref<Texture2D> light_1_off;
Ref<Texture2D> light_2_on;
Ref<Texture2D> light_2_off;

Ref<Texture2D> animation_play;
Ref<Texture2D> animation_pause;
} theme_cache;

DirectionalLight3D *light1 = nullptr;
Expand Down Expand Up @@ -139,6 +142,8 @@ class SceneImportSettings : public ConfirmationDialog {
HashMap<StringName, Variant> settings;
};
HashMap<String, AnimationData> animation_map;
AnimationPlayer *animation_preview = nullptr;
TextureButton *animation_preview_button = nullptr;

struct NodeData {
Node *node = nullptr;
Expand Down Expand Up @@ -166,6 +171,8 @@ class SceneImportSettings : public ConfirmationDialog {
void _mesh_tree_selected();
void _scene_tree_selected();
void _light_button_pressed(Node *p_button);
void _animation_button_pressed();
void _preview_animation_finished(String anim_name);

void _viewport_input(const Ref<InputEvent> &p_input);

Expand Down

0 comments on commit f0768f1

Please sign in to comment.