-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssetBrowserController.cpp
More file actions
311 lines (260 loc) · 8.59 KB
/
AssetBrowserController.cpp
File metadata and controls
311 lines (260 loc) · 8.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "AssetBrowserController.h"
#include "MaterialPreviewRenderer.h"
#include "SentryReporter.h"
#include <Ogre.h>
#include <OgreScriptCompiler.h>
#include <QDir>
#include <QFileInfo>
#include <QSettings>
#include <QStandardPaths>
AssetBrowserController* AssetBrowserController::m_pSingleton = nullptr;
const QStringList AssetBrowserController::s_meshExtensions = {
"fbx", "gltf", "glb", "gltf2", "vrm", "obj", "dae", "stl", "mesh", "3ds", "blend", "ply",
"x", "x3d", "lwo", "lws", "ac", "ms3d", "cob", "scn", "bvh", "irrmesh", "irr",
"mdl", "md2", "md3", "md5mesh", "smd", "ogex", "b3d", "q3d", "nff", "off",
"raw", "ter", "hmp", "assbin", "mesh.xml", "tmd"
};
const QStringList AssetBrowserController::s_textureExtensions = {
"png", "jpg", "jpeg", "tga", "bmp", "dds", "hdr", "exr", "tif", "tiff"
};
const QStringList AssetBrowserController::s_materialExtensions = {
"material"
};
AssetBrowserController::AssetBrowserController()
: QObject(nullptr)
, m_watcher(new QFileSystemWatcher(this))
{
// Restore last browsed directory from QSettings
QSettings settings;
QString savedPath = settings.value("AssetBrowser/rootPath").toString();
if (!savedPath.isEmpty() && QDir(savedPath).exists()) {
m_rootPath = savedPath;
} else {
m_rootPath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
}
connect(m_watcher, &QFileSystemWatcher::directoryChanged, this, [this](const QString&) {
refreshFiles();
});
setupWatcher();
refreshFiles();
}
AssetBrowserController::~AssetBrowserController()
{
// m_watcher is a child QObject, cleaned up automatically
}
AssetBrowserController* AssetBrowserController::instance()
{
if (!m_pSingleton)
m_pSingleton = new AssetBrowserController();
return m_pSingleton;
}
AssetBrowserController* AssetBrowserController::qmlInstance(QQmlEngine* engine, QJSEngine* /*scriptEngine*/)
{
auto* inst = instance();
engine->setObjectOwnership(inst, QQmlEngine::CppOwnership);
return inst;
}
void AssetBrowserController::kill()
{
delete m_pSingleton;
m_pSingleton = nullptr;
}
QString AssetBrowserController::rootPath() const
{
return m_rootPath;
}
void AssetBrowserController::setRootPath(const QString& path)
{
if (m_rootPath == path)
return;
QDir dir(path);
if (!dir.exists())
return;
m_rootPath = dir.absolutePath();
SentryReporter::addBreadcrumb("ui.action", "Root directory changed: " + m_rootPath);
// Persist to settings
QSettings settings;
settings.setValue("AssetBrowser/rootPath", m_rootPath);
emit rootPathChanged();
setupWatcher();
refreshFiles();
}
QVariantList AssetBrowserController::files() const
{
return m_files;
}
QString AssetBrowserController::filter() const
{
return m_filter;
}
void AssetBrowserController::setFilter(const QString& filter)
{
if (m_filter == filter)
return;
m_filter = filter;
emit filterChanged();
refreshFiles();
}
QString AssetBrowserController::searchQuery() const
{
return m_searchQuery;
}
void AssetBrowserController::setSearchQuery(const QString& query)
{
if (m_searchQuery == query)
return;
m_searchQuery = query;
emit searchQueryChanged();
refreshFiles();
}
void AssetBrowserController::browseForDirectory()
{
SentryReporter::addBreadcrumb("ui.action", "Browse for directory requested");
emit browseRequested();
}
void AssetBrowserController::openFile(const QString& path)
{
QFileInfo fi(path);
if (!fi.exists())
return;
if (fi.isDir()) {
SentryReporter::addBreadcrumb("ui.action",
QString("Navigate into directory: %1").arg(fi.fileName()));
navigateToDirectory(path);
return;
}
QString type = classifyExtension(fi.suffix().toLower());
SentryReporter::addBreadcrumb("ui.action",
QString("Open file: %1 (type: %2)").arg(fi.fileName(), type));
if (type == "mesh") {
emit importMeshRequested(QStringList{path});
}
// Texture and material handling can be added as stretch goals
}
void AssetBrowserController::navigateToDirectory(const QString& path)
{
setRootPath(path);
}
void AssetBrowserController::navigateUp()
{
QDir dir(m_rootPath);
if (dir.cdUp()) {
setRootPath(dir.absolutePath());
}
}
QString AssetBrowserController::fileTypeForPath(const QString& path) const
{
QFileInfo fi(path);
if (fi.isDir())
return "directory";
return classifyExtension(fi.suffix().toLower());
}
void AssetBrowserController::refreshFiles()
{
m_files.clear();
QDir dir(m_rootPath);
if (!dir.exists()) {
emit filesChanged();
return;
}
// List directories first, then files
QFileInfoList entries = dir.entryInfoList(
QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot,
QDir::DirsFirst | QDir::Name | QDir::IgnoreCase);
for (const QFileInfo& fi : entries) {
QString type;
if (fi.isDir()) {
type = "directory";
} else {
type = classifyExtension(fi.suffix().toLower());
}
// Apply filter
if (m_filter != "all") {
if (fi.isDir()) {
// Always show directories regardless of filter
} else if (m_filter == "meshes" && type != "mesh") {
continue;
} else if (m_filter == "textures" && type != "texture") {
continue;
} else if (m_filter == "materials" && type != "material") {
continue;
}
}
// Apply search query
if (!m_searchQuery.isEmpty()) {
if (!fi.fileName().contains(m_searchQuery, Qt::CaseInsensitive))
continue;
}
QVariantMap entry;
entry["name"] = fi.fileName();
entry["path"] = fi.absoluteFilePath();
entry["type"] = type;
entry["size"] = fi.isDir() ? 0 : fi.size();
entry["isDir"] = fi.isDir();
// Generate a material preview for .material files
if (type == "material") {
QString previewUrl = materialPreview(fi.absoluteFilePath());
if (!previewUrl.isEmpty())
entry["previewUrl"] = previewUrl;
}
m_files.append(entry);
}
emit filesChanged();
}
void AssetBrowserController::setupWatcher()
{
// Remove old watched directories
QStringList watched = m_watcher->directories();
if (!watched.isEmpty())
m_watcher->removePaths(watched);
// Watch the current root path
if (!m_rootPath.isEmpty() && QDir(m_rootPath).exists())
m_watcher->addPath(m_rootPath);
}
QString AssetBrowserController::materialPreview(const QString& filePath) const
{
// Parse the .material file for the first material name
QString matName = MaterialPreviewRenderer::firstMaterialNameInFile(filePath);
if (matName.isEmpty())
return {};
// Try to load the material if it doesn't already exist in Ogre
auto* matMgr = Ogre::MaterialManager::getSingletonPtr();
if (!matMgr)
return {};
if (!matMgr->resourceExists(matName.toStdString())) {
QFileInfo fi(filePath);
try {
// Register the material's directory so Ogre can find referenced textures
Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
fi.absolutePath().toStdString(), "FileSystem",
Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, false);
} catch (...) {}
// Parse the .material script via Ogre's script compiler
try {
std::string scriptContent;
QFile f(filePath);
if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
scriptContent = f.readAll().toStdString();
f.close();
}
if (!scriptContent.empty()) {
Ogre::DataStreamPtr ds(new Ogre::MemoryDataStream(
const_cast<char*>(scriptContent.c_str()),
scriptContent.size(), false));
Ogre::ScriptCompilerManager::getSingleton().parseScript(
ds, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
}
} catch (...) {}
}
return MaterialPreviewRenderer::instance()->renderPreviewAsDataUri(matName);
}
QString AssetBrowserController::classifyExtension(const QString& ext) const
{
if (s_meshExtensions.contains(ext, Qt::CaseInsensitive))
return "mesh";
if (s_textureExtensions.contains(ext, Qt::CaseInsensitive))
return "texture";
if (s_materialExtensions.contains(ext, Qt::CaseInsensitive))
return "material";
return "other";
}