/
FileSelectionRequestHandler.h
91 lines (70 loc) · 2.36 KB
/
FileSelectionRequestHandler.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
#pragma once
#include <map>
#include "imainframe.h"
#include "igame.h"
#include "iradiant.h"
#include "ifiletypes.h"
#include "messages/FileSelectionRequest.h"
#include "wxutil/FileChooser.h"
namespace ui
{
class FileSelectionRequestHandler
{
private:
std::size_t _msgSubscription;
// The mapping between type and last viewed path
// like "map" => "/usr/local/game/doom3/maps/"
// and "prefab" => "~/.doom3/darkmod/prefabs/"
std::map<std::string, std::string> _lastDirs;
public:
FileSelectionRequestHandler()
{
_msgSubscription = GlobalRadiantCore().getMessageBus().addListener(
radiant::IMessage::Type::FileSelectionRequest,
radiant::TypeListener<radiant::FileSelectionRequest>(
sigc::mem_fun(this, &FileSelectionRequestHandler::handleRequest)));
// Load the default values
_lastDirs[filetype::TYPE_MAP] = GlobalGameManager().getMapPath();
_lastDirs[filetype::TYPE_PREFAB] = GlobalGameManager().getPrefabPath();
}
~FileSelectionRequestHandler()
{
GlobalRadiantCore().getMessageBus().removeListener(_msgSubscription);
}
private:
void handleRequest(radiant::FileSelectionRequest& request)
{
radiant::FileSelectionRequest::Result result;
const auto& type = request.getType();
// 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] = GlobalGameManager().getMapPath();
}
// 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(request.getTitle(),
request.getMode() == radiant::FileSelectionRequest::Mode::Open,
type, defaultExt);
fileChooser.setCurrentFile(request.getDefaultFile());
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("/"));
}
result.fullPath = filePath;
result.mapFormatName = fileChooser.getSelectedMapFormat();
request.setResult(result);
}
};
}