<commit_before>#include "SceneEditor.hpp"

#include <Engine/Hymn.hpp>
#include <Engine/Util/FileSystem.hpp>
#include <Engine/Util/Log.hpp>
#include <imgui.h>

using namespace GUI;

void SceneEditor::Show() {
    if (ImGui::Begin(("Scene: " + Hymn().scenes[sceneIndex] + "###Scene").c_str(), &visible)) {
        ImGui::InputText("Name", name, 128);
        Hymn().scenes[sceneIndex] = name;
        
        // Entities.
        entityPressed = false;
        ShowEntity(Hymn().world.GetRoot());
    }
    ImGui::End();
}

void SceneEditor::SetScene(std::size_t sceneIndex) {
    entityEditor.SetVisible(false);
    
    if (sceneIndex < Hymn().scenes.size()) {
        this->sceneIndex = sceneIndex;
        strcpy(name, Hymn().scenes[sceneIndex].c_str());
    }
}

bool SceneEditor::IsVisible() const {
    return visible;
}

void SceneEditor::SetVisible(bool visible) {
    this->visible = visible;
}

void SceneEditor::Save() const {
    if (sceneIndex < Hymn().scenes.size())
        Hymn().world.Save(Hymn().GetPath() + FileSystem::DELIMITER + "Scenes" + FileSystem::DELIMITER + Hymn().scenes[sceneIndex] + ".json");
}

void SceneEditor::ShowEntity(Entity* entity) {
    if (ImGui::TreeNode(entity->name.c_str())) {
        if (ImGui::Button("Edit")) {
            entityPressed = true;
            entityEditor.SetEntity(entity);
        }
        
        if (!entity->IsScene()) {
            if (ImGui::Button("Add child"))
                entity->AddChild("Entity #" + std::to_string(Hymn().entityNumber++));
            
            if (ImGui::Button("Instantiate scene"))
                ImGui::OpenPopup("Select scene");
            
            if (ImGui::BeginPopup("Select scene")) {
                ImGui::Text("Scenes");
                ImGui::Separator();
                
                for (const std::string& scene : Hymn().scenes) {
                    if (ImGui::Selectable(scene.c_str()))
                        entity->InstantiateScene(scene);
                }
                
                ImGui::EndPopup();
            }
        }
        
        if (entity != Hymn().world.GetRoot())
            if (ImGui::Button("Delete"))
                entity->Kill();
        
        if (!entity->IsScene()) {
            for (Entity* child : entity->GetChildren()) {
                ShowEntity(child);
            }
        }
        
        ImGui::TreePop();
    }
}<commit_msg>Fixed crash when deleting entities<commit_after>#include "SceneEditor.hpp"

#include <Engine/Hymn.hpp>
#include <Engine/Util/FileSystem.hpp>
#include <Engine/Util/Log.hpp>
#include <imgui.h>

using namespace GUI;

void SceneEditor::Show() {
    if (ImGui::Begin(("Scene: " + Hymn().scenes[sceneIndex] + "###Scene").c_str(), &visible)) {
        ImGui::InputText("Name", name, 128);
        Hymn().scenes[sceneIndex] = name;
        
        // Entities.
        entityPressed = false;
        ShowEntity(Hymn().world.GetRoot());
    }
    ImGui::End();
}

void SceneEditor::SetScene(std::size_t sceneIndex) {
    entityEditor.SetVisible(false);
    
    if (sceneIndex < Hymn().scenes.size()) {
        this->sceneIndex = sceneIndex;
        strcpy(name, Hymn().scenes[sceneIndex].c_str());
    }
}

bool SceneEditor::IsVisible() const {
    return visible;
}

void SceneEditor::SetVisible(bool visible) {
    this->visible = visible;
}

void SceneEditor::Save() const {
    if (sceneIndex < Hymn().scenes.size())
        Hymn().world.Save(Hymn().GetPath() + FileSystem::DELIMITER + "Scenes" + FileSystem::DELIMITER + Hymn().scenes[sceneIndex] + ".json");
}

void SceneEditor::ShowEntity(Entity* entity) {
    if (ImGui::TreeNode(entity->name.c_str())) {
        if (ImGui::Button("Edit")) {
            entityPressed = true;
            entityEditor.SetEntity(entity);
        }
        
        if (!entity->IsScene()) {
            if (ImGui::Button("Add child"))
                entity->AddChild("Entity #" + std::to_string(Hymn().entityNumber++));
            
            if (ImGui::Button("Instantiate scene"))
                ImGui::OpenPopup("Select scene");
            
            if (ImGui::BeginPopup("Select scene")) {
                ImGui::Text("Scenes");
                ImGui::Separator();
                
                for (const std::string& scene : Hymn().scenes) {
                    if (ImGui::Selectable(scene.c_str()))
                        entity->InstantiateScene(scene);
                }
                
                ImGui::EndPopup();
            }
        }
        
        if (entity != Hymn().world.GetRoot())
            if (ImGui::Button("Delete"))
                entity->Kill();

        if (!entity->IsScene()) {
            unsigned int size = entity->GetChildren().size();
            std::vector<Entity*> entityChildren = entity->GetChildren();
            for (unsigned int i = 0; i < size; i++) {
                ShowEntity(entityChildren[i]);
            }
        }
        
        ImGui::TreePop();
    }
}
{'fraction_non_alphanumeric': 0.16123843988741454, 'fraction_numerical': 0.0014073180538801769, 'mean_word_length': 2.0577750460971114, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6330241', 'n_tokens_mistral': 1509, 'n_tokens_neox': 1480, 'n_words': 241}