|
| 1 | +//// FileDrop.cpp ///////////////////////////////////////////////////////////// |
| 2 | +// |
| 3 | +// zzzzz zzz zzzzzzzzzzzzz zzzz zzzz zzzzzz zzzzz |
| 4 | +// zzzzzzz zzz zzzz zzzz zzzz zzzz |
| 5 | +// zzz zzz zzz zzzzzzzzzzzzz zzzz zzzz zzz |
| 6 | +// zzz zzz zzz z zzzz zzzz zzzz zzzz |
| 7 | +// zzz zzz zzzzzzzzzzzzz zzzz zzz zzzzzzz zzzzz |
| 8 | +// |
| 9 | +// Author: Jean CARDONNE |
| 10 | +// Date: 30/06/2025 |
| 11 | +// Description: Implementation of file drop handling for asset manager |
| 12 | +// |
| 13 | +/////////////////////////////////////////////////////////////////////////////// |
| 14 | + |
| 15 | +#include "AssetManagerWindow.hpp" |
| 16 | +#include "assets/Asset.hpp" |
| 17 | +#include "assets/AssetImporter.hpp" |
| 18 | +#include "assets/AssetLocation.hpp" |
| 19 | +#include "assets/Assets/Model/Model.hpp" |
| 20 | +#include "assets/Assets/Texture/Texture.hpp" |
| 21 | +#include "Logger.hpp" |
| 22 | +#include <filesystem> |
| 23 | +#include <algorithm> |
| 24 | + |
| 25 | +namespace nexo::editor { |
| 26 | + |
| 27 | + static assets::AssetType getAssetTypeFromExtension(const std::string &extension) |
| 28 | + { |
| 29 | + static const std::set<std::string> imageExtensions = { |
| 30 | + ".png", ".jpg", ".jpeg", ".bmp", ".tga", ".gif", ".psd", ".hdr", ".pic", ".pnm", ".ppm", ".pgm" |
| 31 | + }; |
| 32 | + if (imageExtensions.contains(extension)) |
| 33 | + return assets::AssetType::TEXTURE; |
| 34 | + static const std::set<std::string> modelExtensions = { |
| 35 | + ".gltf", ".glb", ".fbx", ".obj", ".dae", ".3ds", ".stl", ".ply", ".blend", ".x3d", ".ifc" |
| 36 | + }; |
| 37 | + if (modelExtensions.contains(extension)) |
| 38 | + return assets::AssetType::MODEL; |
| 39 | + return assets::AssetType::UNKNOWN; |
| 40 | + } |
| 41 | + |
| 42 | + const assets::AssetLocation AssetManagerWindow::getAssetLocation(const std::filesystem::path &path) const |
| 43 | + { |
| 44 | + std::string assetName = path.stem().string(); |
| 45 | + std::filesystem::path folderPath; |
| 46 | + std::string targetFolder = !m_hoveredFolder.empty() ? m_hoveredFolder : m_currentFolder; |
| 47 | + |
| 48 | + std::string assetPath = targetFolder; |
| 49 | + std::string locationString = assetName + "@" + assetPath; |
| 50 | + |
| 51 | + LOG(NEXO_DEV, |
| 52 | + "Creating asset location: {} (current folder: '{}', hovered: '{}')", |
| 53 | + locationString, |
| 54 | + m_currentFolder, |
| 55 | + m_hoveredFolder); |
| 56 | + |
| 57 | + assets::AssetLocation location(locationString); |
| 58 | + return location; |
| 59 | + } |
| 60 | + |
| 61 | + void AssetManagerWindow::handleEvent(event::EventFileDrop& event) |
| 62 | + { |
| 63 | + m_pendingDroppedFiles.insert(m_pendingDroppedFiles.end(), |
| 64 | + event.files.begin(), |
| 65 | + event.files.end()); |
| 66 | + } |
| 67 | + |
| 68 | + void AssetManagerWindow::handleDroppedFiles() |
| 69 | + { |
| 70 | + if (m_pendingDroppedFiles.empty()) |
| 71 | + return; |
| 72 | + |
| 73 | + for (const auto& filePath : m_pendingDroppedFiles) |
| 74 | + importDroppedFile(filePath); |
| 75 | + m_pendingDroppedFiles.clear(); |
| 76 | + |
| 77 | + m_folderStructure.clear(); |
| 78 | + buildFolderStructure(); |
| 79 | + } |
| 80 | + |
| 81 | + void AssetManagerWindow::importDroppedFile(const std::string& filePath) |
| 82 | + { |
| 83 | + std::filesystem::path path(filePath); |
| 84 | + |
| 85 | + if (!std::filesystem::exists(path)) { |
| 86 | + LOG(NEXO_WARN, "Dropped file does not exist: {}", filePath); |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + std::string extension = path.extension().string(); |
| 91 | + std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower); |
| 92 | + |
| 93 | + assets::AssetType assetType = getAssetTypeFromExtension(extension); |
| 94 | + if (assetType == assets::AssetType::UNKNOWN) { |
| 95 | + LOG(NEXO_WARN, "Unsupported file type: {}", extension); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + assets::AssetLocation location = getAssetLocation(path); |
| 100 | + |
| 101 | + assets::AssetImporter importer; |
| 102 | + assets::ImporterFileInput fileInput{path}; |
| 103 | + try { |
| 104 | + auto assetRef = importer.importAssetAuto(location, fileInput); |
| 105 | + if (!assetRef) |
| 106 | + LOG(NEXO_ERROR, "Failed to import asset: {}", location.getPath().data()); |
| 107 | + } catch (const std::exception& e) { |
| 108 | + LOG(NEXO_ERROR, "Exception while importing {}: {}", location.getPath().data(), e.what()); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments