OpenShaderDesigner 0.0.1
Loading...
Searching...
No Matches
FileManager.h
1// =====================================================================================================================
2// OpenShaderDesigner, an open source software utility to create materials and shaders.
3// Copyright (C) 2024 Medusa Slockbower
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <https://www.gnu.org/licenses/>.
17// =====================================================================================================================
18
19#ifndef FILESYSTEM_H
20#define FILESYSTEM_H
21
22#include <Editor/EditorWindow.h>
23
24#include <open-cpp-utils/filesystem.h>
25#include <open-cpp-utils/map.h>
26#include <open-cpp-utils/startup.h>
27
28#define RegisterAsset(Name, Type, ...) \
29 STARTUP(_Register##Type) { FileManager::Register(Name, { __VA_ARGS__ }, ##Type::Create, ##Type::Load, ##Type::Import); }
30
31namespace ocu = open_cpp_utils;
32
33namespace OpenShaderDesigner
34{
35
37{
38public:
39 class Asset;
40 using FileSystem = ocu::filesystem<Asset, FileManager>;
41 using File = FileSystem::file;
42 using Path = std::filesystem::path;
43 using FileID = FileSystem::file_id;
44 using CreateFunc = Asset* (*)(const Path&);
45 using LoadFunc = Asset* (*)(const Path&);
46 using ImportFunc = Asset* (*)(const Path&, const Path&);
47 friend FileSystem;
48
49private:
50 struct AssetDetail
51 {
52 std::string Name;
53 std::vector<std::string> Extensions;
54 CreateFunc Create;
55 LoadFunc Load;
56 ImportFunc Import;
57
58 AssetDetail() : Create(nullptr), Load(nullptr), Import(nullptr) { }
59 AssetDetail(const std::string& name) : Name(name), Create(nullptr), Load(nullptr), Import(nullptr) {}
60
61
62 AssetDetail(const std::string& name, const std::vector<std::string>& exts,
63 const CreateFunc create, const LoadFunc load, const ImportFunc import)
64 : Name(name), Extensions(exts), Create(create), Load(load), Import(import) {}
65 };
66
67 using AssetMenuHierarchy = ocu::directed_tree<AssetDetail>;
68 using AssetType = AssetMenuHierarchy::node;
69 using ExtensionMapping = ocu::map<std::string, AssetType>;
70
71 static AssetMenuHierarchy& AssetMenu() { static AssetMenuHierarchy Menu; return Menu; }
72 static ExtensionMapping& ExtensionMap() { static ExtensionMapping Map; return Map; }
73
74public:
75
76 class Asset
77 {
78 public:
79 Asset(const Path& path) : Dirty_(false) { }
80 virtual ~Asset() = default;
81
82 bool Dirty() const { return Dirty_; }
83
84 virtual void Open() { };
85 virtual void Save(const Path& path) { Dirty_ = false; }
86
87 File& GetFile() { return Manager_->Get(File_); }
88 FileID GetID() const { return File_; }
89
90 protected:
91 void MakeDirty() { Dirty_ = true; }
92 FileManager* Parent() const { return Manager_; }
93
94 private:
95 FileManager* Manager_;
96 FileID File_;
97 bool Dirty_;
98 friend FileManager;
99 };
100
101 struct Folder : Asset
102 {
103 Folder(const std::filesystem::path& p) : Asset(p) { };
104 virtual ~Folder() = default;
105
106 void Open() override { Manager_->CurrentDirectory_ = GetID(); }
107 };
108
109private:
110 static Asset* load(const Path& file, FileID id);
111 static Asset* import(const Path& src, const Path& dst, FileID id);
112 static Asset* create(const Path& file, FileID id);
113
114public:
115 FileManager();
116 virtual ~FileManager() = default;
117
118 void DrawMenu() override;
119 void DrawWindow() override;
120
121 FileID CurrentDirectory() const { return CurrentDirectory_; }
122 void CurrentDirectory(FileID id) { CurrentDirectory_ = id; }
123
124 FileID Create(const std::string& name) { return Filesystem_.create(name, CurrentDirectory_); }
125 FileID Import(const Path& path) { return Filesystem_.import(path, CurrentDirectory_); }
126 FileID LoadDirectory(const Path& path) { return Filesystem_.load_directory(path); }
127 void CloseDirectory(FileID dir) { Filesystem_.close_directory(dir); }
128
129 FileID Get(const Path& path) const { return Filesystem_.find(path); }
130 File& Get(FileID id) { return Filesystem_[id]; }
131 const File& Get(FileID id) const { return Filesystem_[id]; }
132
133 FileID Parent(FileID id) const { return Filesystem_.parent(id); }
134
135 bool AnyDirty();
136 void SaveAll();
137
138 static Path GetHomeDirectory();
139 static void Register(const std::filesystem::path& path,
140 const std::vector<std::string>& extension,
141 CreateFunc create, LoadFunc load, ImportFunc import);
142private:
143 FileSystem Filesystem_;
144 FileID CurrentDirectory_, Selected_;
145 bool Rename_, FocusRename_;
146 std::string RenameBuffer_;
147};
148
149}
150
151
152#endif //FILESYSTEM_H
EditorWindow class for wrapping ImGui window functionality.
Definition EditorWindow.h:32
Definition FileManager.h:77
Definition FileManager.h:37
void DrawMenu() override
DrawMenu function for when the EditorWindow Menu is being drawn.
Definition FileManager.cpp:104
void DrawWindow() override
DrawWindow function for when the EditorWindow is being drawn.
Definition FileManager.cpp:175
Definition FileManager.h:102