Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenkernel/BKE_asset_catalog.hh
| Show All 34 Lines | |||||
| #include <map> | #include <map> | ||||
| #include <memory> | #include <memory> | ||||
| #include <set> | #include <set> | ||||
| #include <string> | #include <string> | ||||
| namespace blender::bke { | namespace blender::bke { | ||||
| class AssetCatalog; | |||||
| class AssetCatalogCollection; | |||||
| class AssetCatalogDefinitionFile; | |||||
| class AssetCatalogFilter; | |||||
| class AssetCatalogTree; | |||||
| using CatalogID = bUUID; | using CatalogID = bUUID; | ||||
| using CatalogPathComponent = std::string; | using CatalogPathComponent = std::string; | ||||
| /* Would be nice to be able to use `std::filesystem::path` for this, but it's currently not | /* Would be nice to be able to use `std::filesystem::path` for this, but it's currently not | ||||
| * available on the minimum macOS target version. */ | * available on the minimum macOS target version. */ | ||||
| using CatalogFilePath = std::string; | using CatalogFilePath = std::string; | ||||
| using OwningAssetCatalogMap = Map<CatalogID, std::unique_ptr<AssetCatalog>>; | |||||
| class AssetCatalog; | |||||
| class AssetCatalogDefinitionFile; | |||||
| class AssetCatalogFilter; | |||||
| class AssetCatalogTree; | |||||
| /* Manages the asset catalogs of a single asset library (i.e. of catalogs defined in a single | /* Manages the asset catalogs of a single asset library (i.e. of catalogs defined in a single | ||||
| * directory hierarchy). */ | * directory hierarchy). */ | ||||
| class AssetCatalogService { | class AssetCatalogService { | ||||
| public: | public: | ||||
| static const CatalogFilePath DEFAULT_CATALOG_FILENAME; | static const CatalogFilePath DEFAULT_CATALOG_FILENAME; | ||||
| public: | public: | ||||
| AssetCatalogService() = default; | AssetCatalogService(); | ||||
| explicit AssetCatalogService(const CatalogFilePath &asset_library_root); | explicit AssetCatalogService(const CatalogFilePath &asset_library_root); | ||||
| /** Load asset catalog definitions from the files found in the asset library. */ | /** Load asset catalog definitions from the files found in the asset library. */ | ||||
| void load_from_disk(); | void load_from_disk(); | ||||
| /** Load asset catalog definitions from the given file or directory. */ | /** Load asset catalog definitions from the given file or directory. */ | ||||
| void load_from_disk(const CatalogFilePath &file_or_directory_path); | void load_from_disk(const CatalogFilePath &file_or_directory_path); | ||||
| /** | /** | ||||
| ▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | public: | ||||
| */ | */ | ||||
| void update_catalog_path(CatalogID catalog_id, const AssetCatalogPath &new_catalog_path); | void update_catalog_path(CatalogID catalog_id, const AssetCatalogPath &new_catalog_path); | ||||
| AssetCatalogTree *get_catalog_tree(); | AssetCatalogTree *get_catalog_tree(); | ||||
| /** Return true only if there are no catalogs known. */ | /** Return true only if there are no catalogs known. */ | ||||
| bool is_empty() const; | bool is_empty() const; | ||||
| /** | |||||
| * Store the current catalogs in the undo stack. | |||||
| * This snapshots everything in the #AssetCatalogCollection. */ | |||||
| void store_undo_snapshot(); | |||||
| /** | |||||
| * Restore the last-saved undo snapshot, pushing the current state onto the redo stack. | |||||
| * The caller is responsible for first checking that undoing is possible. | |||||
| */ | |||||
| void undo(); | |||||
| bool is_undo_possbile() const; | |||||
| /** | |||||
| * Restore the last-saved redo snapshot, pushing the current state onto the undo stack. | |||||
| * The caller is responsible for first checking that undoing is possible. */ | |||||
| void redo(); | |||||
| bool is_redo_possbile() const; | |||||
| protected: | protected: | ||||
| /* These pointers are owned by this AssetCatalogService. */ | std::unique_ptr<AssetCatalogCollection> catalog_collection_; | ||||
| Map<CatalogID, std::unique_ptr<AssetCatalog>> catalogs_; | |||||
| Map<CatalogID, std::unique_ptr<AssetCatalog>> deleted_catalogs_; | |||||
| std::unique_ptr<AssetCatalogDefinitionFile> catalog_definition_file_; | |||||
| std::unique_ptr<AssetCatalogTree> catalog_tree_ = std::make_unique<AssetCatalogTree>(); | std::unique_ptr<AssetCatalogTree> catalog_tree_ = std::make_unique<AssetCatalogTree>(); | ||||
| CatalogFilePath asset_library_root_; | CatalogFilePath asset_library_root_; | ||||
| Vector<std::unique_ptr<AssetCatalogCollection>> undo_snapshots_; | |||||
| Vector<std::unique_ptr<AssetCatalogCollection>> redo_snapshots_; | |||||
Severin: Could/should this maybe be moved to a different class, say `AssetCatalogUndoHistory`? The… | |||||
sybrenAuthorUnsubmitted Done Inline ActionsYup, that's why I created T92114. sybren: Yup, that's why I created T92114.
| |||||
| void load_directory_recursive(const CatalogFilePath &directory_path); | void load_directory_recursive(const CatalogFilePath &directory_path); | ||||
| void load_single_file(const CatalogFilePath &catalog_definition_file_path); | void load_single_file(const CatalogFilePath &catalog_definition_file_path); | ||||
| std::unique_ptr<AssetCatalogDefinitionFile> parse_catalog_file( | std::unique_ptr<AssetCatalogDefinitionFile> parse_catalog_file( | ||||
| const CatalogFilePath &catalog_definition_file_path); | const CatalogFilePath &catalog_definition_file_path); | ||||
| /** | /** | ||||
| * Construct an in-memory catalog definition file (CDF) from the currently known catalogs. | * Construct an in-memory catalog definition file (CDF) from the currently known catalogs. | ||||
| Show All 12 Lines | protected: | ||||
| std::unique_ptr<AssetCatalogTree> read_into_tree(); | std::unique_ptr<AssetCatalogTree> read_into_tree(); | ||||
| void rebuild_tree(); | void rebuild_tree(); | ||||
| /** | /** | ||||
| * For every catalog, ensure that its parent path also has a known catalog. | * For every catalog, ensure that its parent path also has a known catalog. | ||||
| */ | */ | ||||
| void create_missing_catalogs(); | void create_missing_catalogs(); | ||||
| /* For access by subclasses, as those will not be marked as friend by #AssetCatalogCollection. */ | |||||
| AssetCatalogDefinitionFile *get_catalog_definition_file(); | |||||
| OwningAssetCatalogMap &get_catalogs(); | |||||
| }; | |||||
| /** | |||||
| * All catalogs that are owned by a single asset library, and managed by a single instance of | |||||
| * #AssetCatalogService. The undo system for asset catalog edits contains historical copies of this | |||||
| * struct. | |||||
| */ | |||||
| class AssetCatalogCollection { | |||||
| friend AssetCatalogService; | |||||
| public: | |||||
| AssetCatalogCollection() = default; | |||||
| AssetCatalogCollection(const AssetCatalogCollection &other) = delete; | |||||
| AssetCatalogCollection(AssetCatalogCollection &&other) noexcept = default; | |||||
| std::unique_ptr<AssetCatalogCollection> deep_copy() const; | |||||
| protected: | |||||
| /** All catalogs known, except the known-but-deleted ones. */ | |||||
| OwningAssetCatalogMap catalogs_; | |||||
| /** Catalogs that have been deleted. They are kept around so that the load-merge-save of catalog | |||||
| * definition files can actually delete them if they already existed on disk (instead of the | |||||
| * merge operation resurrecting them). */ | |||||
| OwningAssetCatalogMap deleted_catalogs_; | |||||
| /* For now only a single catalog definition file is supported. | |||||
| * The aim is to support an arbitrary number of such files per asset library in the future. */ | |||||
| std::unique_ptr<AssetCatalogDefinitionFile> catalog_definition_file_; | |||||
| static OwningAssetCatalogMap copy_catalog_map(const OwningAssetCatalogMap &orig); | |||||
| }; | }; | ||||
| /** | /** | ||||
| * Representation of a catalog path in the #AssetCatalogTree. | * Representation of a catalog path in the #AssetCatalogTree. | ||||
| */ | */ | ||||
| class AssetCatalogTreeItem { | class AssetCatalogTreeItem { | ||||
| friend class AssetCatalogTree; | friend class AssetCatalogTree; | ||||
| ▲ Show 20 Lines • Show All 97 Lines • ▼ Show 20 Lines | public: | ||||
| bool contains(CatalogID catalog_id) const; | bool contains(CatalogID catalog_id) const; | ||||
| /* Add a new catalog. Undefined behavior if a catalog with the same ID was already added. */ | /* Add a new catalog. Undefined behavior if a catalog with the same ID was already added. */ | ||||
| void add_new(AssetCatalog *catalog); | void add_new(AssetCatalog *catalog); | ||||
| using AssetCatalogParsedFn = FunctionRef<bool(std::unique_ptr<AssetCatalog>)>; | using AssetCatalogParsedFn = FunctionRef<bool(std::unique_ptr<AssetCatalog>)>; | ||||
| void parse_catalog_file(const CatalogFilePath &catalog_definition_file_path, | void parse_catalog_file(const CatalogFilePath &catalog_definition_file_path, | ||||
| AssetCatalogParsedFn callback); | AssetCatalogParsedFn callback); | ||||
| std::unique_ptr<AssetCatalogDefinitionFile> copy_and_remap( | |||||
| const OwningAssetCatalogMap &catalogs, const OwningAssetCatalogMap &deleted_catalogs) const; | |||||
| protected: | protected: | ||||
| /* Catalogs stored in this file. They are mapped by ID to make it possible to query whether a | /* Catalogs stored in this file. They are mapped by ID to make it possible to query whether a | ||||
| * catalog is already known, without having to find the corresponding `AssetCatalog*`. */ | * catalog is already known, without having to find the corresponding `AssetCatalog*`. */ | ||||
| Map<CatalogID, AssetCatalog *> catalogs_; | Map<CatalogID, AssetCatalog *> catalogs_; | ||||
| bool parse_version_line(StringRef line); | bool parse_version_line(StringRef line); | ||||
| std::unique_ptr<AssetCatalog> parse_catalog_line(StringRef line); | std::unique_ptr<AssetCatalog> parse_catalog_line(StringRef line); | ||||
| ▲ Show 20 Lines • Show All 76 Lines • Show Last 20 Lines | |||||
Could/should this maybe be moved to a different class, say AssetCatalogUndoHistory? The catalog service turns more and more into a god object :) I'd rather have it delegate tasks some more.