/
MapFileManager.cpp
93 lines (74 loc) · 2.74 KB
/
MapFileManager.cpp
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
#include "MapFileManager.h"
#include "i18n.h"
#include "iregistry.h"
#include "ifiletypes.h"
#include "imainframe.h"
#include "modulesystem/ApplicationContextImpl.h"
#include "wxutil/FileChooser.h"
#include "wxutil/IConv.h"
#include "os/path.h"
namespace map
{
MapFileManager::MapFileManager()
{
// Load the default values
_lastDirs[filetype::TYPE_MAP] = GlobalRegistry().get(RKEY_MAP_PATH);
_lastDirs[filetype::TYPE_PREFAB] = GlobalRegistry().get(RKEY_PREFAB_PATH);
}
// Instance owner method
MapFileManager& MapFileManager::getInstance()
{
static MapFileManager _instance;
return _instance;
}
void MapFileManager::registerFileTypes()
{
// Register the map file extension in the FileTypeRegistry
GlobalFiletypes().registerPattern(filetype::TYPE_MAP, FileTypePattern(_("Map"), "map", "*.map"));
GlobalFiletypes().registerPattern(filetype::TYPE_REGION, FileTypePattern(_("Region"), "reg", "*.reg"));
GlobalFiletypes().registerPattern(filetype::TYPE_PREFAB, FileTypePattern(_("Prefab"), "pfb", "*.pfb"));
GlobalFiletypes().registerPattern(filetype::TYPE_MAP, FileTypePattern(_("Portable Map File"), "mapx", "*.mapx"));
}
// Utility method to select a map file
MapFileSelection MapFileManager::selectFile(bool open,
const std::string& title, const std::string& type, const std::string& defaultFile)
{
MapFileSelection fileInfo;
// Check, if the lastdir contains at least anything and load
// the default map path if it's empty
if (_lastDirs.find(type) == _lastDirs.end())
{
// Default to the map path, if the type is not yet associated
_lastDirs[type] = GlobalRegistry().get(RKEY_MAP_PATH);
}
// Get the first extension from the list of possible patterns (e.g. *.pfb or *.map)
FileTypePatterns patterns = GlobalFiletypes().getPatternsForType(type);
std::string defaultExt = "";
if (!patterns.empty())
{
defaultExt = "." + patterns.begin()->extension; // ".map"
}
// Display a file chooser dialog to get a new path
wxutil::FileChooser fileChooser(title, open, type, defaultExt);
fileChooser.setCurrentFile(defaultFile);
fileChooser.setCurrentPath(_lastDirs[type]);
std::string filePath = fileChooser.display();
// If a filename was chosen, update the last path
if (!filePath.empty())
{
_lastDirs[type] = filePath.substr(0, filePath.rfind("/"));
}
fileInfo.fullPath = filePath;
fileInfo.mapFormatName = fileChooser.getSelectedMapFormat();
fileInfo.mapFormat = GlobalMapFormatManager().getMapFormatByName(fileInfo.mapFormatName);
return fileInfo;
}
/* PUBLIC INTERFACE METHODS */
MapFileSelection MapFileManager::getMapFileSelection(bool open,
const std::string& title,
const std::string& type,
const std::string& defaultFile)
{
return getInstance().selectFile(open, title, type, defaultFile);
}
} // namespace map