/
ModelPopulator.h
125 lines (100 loc) · 3.12 KB
/
ModelPopulator.h
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
#pragma once
#include "wxutil/dataview/VFSTreePopulator.h"
#include "wxutil/dataview/ThreadedResourceTreePopulator.h"
#include "iregistry.h"
#include "igame.h"
#include "EventRateLimiter.h"
#include "ifilesystem.h"
#include "i18n.h"
#include "string/string.h"
#include "os/path.h"
#include "string/split.h"
#include "string/case_conv.h"
#include <fmt/format.h>
#include "ModelSelector.h"
#include "ModelDataInserter.h"
namespace ui
{
/**
* Threaded functor object to visit the global VFS and add model paths
* to a new TreeModel object. Fires a PopulationFinished event once
* its work is done.
*/
class ModelPopulator final :
public wxutil::ThreadedResourceTreePopulator
{
private:
const ModelTreeView::TreeColumns& _columns;
// Progress dialog and model count
std::size_t _count;
// Event rate limiter for progress dialog
EventRateLimiter _evLimiter;
std::set<std::string> _allowedExtensions;
public:
// Constructor sets the populator
ModelPopulator(const ModelTreeView::TreeColumns& columns,
wxEvtHandler* finishedHandler) :
ThreadedResourceTreePopulator(columns),
_columns(columns),
_count(0),
_evLimiter(50)
{
// Load the allowed extensions
std::string extensions = GlobalGameManager().currentGame()->getKeyValue("modeltypes");
string::split(_allowedExtensions, extensions, " ");
}
~ModelPopulator()
{
EnsureStopped();
}
protected:
void PopulateModel(const wxutil::TreeModel::Ptr& model) override
{
wxutil::VFSTreePopulator populator(model);
// Search for model files
GlobalFileSystem().forEachFile(
MODELS_FOLDER, "*",
[&](const vfs::FileInfo& fileInfo)
{
// Only add visible models
if (fileInfo.visibility == vfs::Visibility::NORMAL)
{
visitModelFile(fileInfo.name, populator);
}
},
0
);
ThrowIfCancellationRequested();
reportProgress(_("Building tree..."));
// Fill in the column data (TRUE = including skins)
ModelDataInserter inserterSkins(_columns, true);
populator.forEachNode(inserterSkins);
}
void SortModel(const wxutil::TreeModel::Ptr& model) override
{
// Sort the model before returning it
model->SortModelFoldersFirst(_columns.iconAndName, _columns.isFolder);
}
void visitModelFile(const std::string& file, wxutil::VFSTreePopulator& populator)
{
ThrowIfCancellationRequested();
std::string ext = os::getExtension(file);
string::to_lower(ext);
// Test the extension. If it is not matching any of the known extensions,
// not interested
if (_allowedExtensions.find(ext) != _allowedExtensions.end())
{
_count++;
populator.addPath(file);
if (_evLimiter.readyForEvent())
{
reportProgress(fmt::format(_("{0:d} models loaded"), _count));
}
}
}
void reportProgress(const std::string& message)
{
PostEvent(new wxutil::TreeModel::PopulationProgressEvent(fmt::format(_("{0:d} models loaded"), _count)));
}
};
}