Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add animation preview functionality and button to SceneImportSettings dialog #76381

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 126 additions & 5 deletions 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 @@ -884,6 +904,48 @@ void SceneImportSettings::_scene_tree_selected() {
_select(scene_tree, type, import_id);
}

void SceneImportSettings::_light_button_pressed(Node *p_button) {
if (p_button == light_1_switch) {
light1->set_visible(!light_1_switch->is_pressed());
}

if (p_button == light_2_switch) {
light2->set_visible(!light_2_switch->is_pressed());
}
}

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 @@ -995,6 +1057,18 @@ void SceneImportSettings::_re_import() {
EditorFileSystem::get_singleton()->reimport_file_with_custom_parameters(base_path, editing_animation ? "animation_library" : "scene", main_settings);
}

void SceneImportSettings::_update_theme_item_cache() {
ConfirmationDialog::_update_theme_item_cache();

theme_cache.light_1_on = get_theme_icon(SNAME("MaterialPreviewLight1"), SNAME("EditorIcons"));
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) {
switch (p_what) {
case NOTIFICATION_READY: {
Expand All @@ -1005,6 +1079,14 @@ void SceneImportSettings::_notification(int p_what) {
action_menu->add_theme_style_override("normal", get_theme_stylebox("normal", "Button"));
action_menu->add_theme_style_override("hover", get_theme_stylebox("hover", "Button"));
action_menu->add_theme_style_override("pressed", get_theme_stylebox("pressed", "Button"));

light_1_switch->set_texture_normal(theme_cache.light_1_on);
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 @@ -1347,6 +1429,40 @@ SceneImportSettings::SceneImportSettings() {

base_viewport->set_use_own_world_3d(true);

HBoxContainer *hb = memnew(HBoxContainer);
vp_container->add_child(hb);
hb->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 2);

hb->add_spacer();

VBoxContainer *vb_toolbar = memnew(VBoxContainer);
vb_toolbar->set_v_size_flags(Control::SIZE_EXPAND_FILL);
hb->add_child(vb_toolbar);

light_1_switch = memnew(TextureButton);
light_1_switch->set_toggle_mode(true);
light_1_switch->set_tooltip_text(TTR("Primary Light"));
light_1_switch->set_stretch_mode(TextureButton::STRETCH_KEEP_ASPECT_CENTERED);
light_1_switch->set_custom_minimum_size(Size2(20, 20));
vb_toolbar->add_child(light_1_switch);
light_1_switch->connect("pressed", callable_mp(this, &SceneImportSettings::_light_button_pressed).bind(light_1_switch));

light_2_switch = memnew(TextureButton);
light_2_switch->set_toggle_mode(true);
light_2_switch->set_tooltip_text(TTR("Secondary Light"));
light_2_switch->set_stretch_mode(TextureButton::STRETCH_KEEP_ASPECT_CENTERED);
light_2_switch->set_custom_minimum_size(Size2(20, 20));
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 All @@ -1356,10 +1472,15 @@ SceneImportSettings::SceneImportSettings() {
camera->set_attributes(camera_attributes);
}

light = memnew(DirectionalLight3D);
light->set_transform(Transform3D().looking_at(Vector3(-1, -2, -0.6), Vector3(0, 1, 0)));
base_viewport->add_child(light);
light->set_shadow(true);
light1 = memnew(DirectionalLight3D);
light1->set_transform(Transform3D().looking_at(Vector3(-1, -2, -0.6), Vector3(0, 1, 0)));
base_viewport->add_child(light1);
light1->set_shadow(true);

light2 = memnew(DirectionalLight3D);
light2->set_transform(Transform3D().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0)));
base_viewport->add_child(light2);
light2->set_color(Color(0.7, 0.7, 0.7));

{
Ref<StandardMaterial3D> selection_mat;
Expand Down
22 changes: 21 additions & 1 deletion editor/import/scene_import_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,21 @@ class SceneImportSettings : public ConfirmationDialog {
bool first_aabb = false;
AABB contents_aabb;

DirectionalLight3D *light = nullptr;
TextureButton *light_1_switch = nullptr;
TextureButton *light_2_switch = nullptr;

struct ThemeCache {
Ref<Texture2D> light_1_on;
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;
DirectionalLight3D *light2 = nullptr;
Ref<ArrayMesh> selection_mesh;
MeshInstance3D *node_selected = nullptr;

Expand Down Expand Up @@ -128,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 All @@ -154,6 +170,9 @@ class SceneImportSettings : public ConfirmationDialog {
void _material_tree_selected();
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 Expand Up @@ -196,6 +215,7 @@ class SceneImportSettings : public ConfirmationDialog {
Timer *update_view_timer = nullptr;

protected:
virtual void _update_theme_item_cache() override;
void _notification(int p_what);

public:
Expand Down