Debian Codegraph @

plugins/dm.conversation/ConversationCommandLibrary.cpp

raw
1 #include "ConversationCommandLibrary.h"
2
3 #include "ieclass.h"
4 #include "iregistry.h"
5 #include "gamelib.h"
6 #include "string/convert.h"
7 #include "string/predicate.h"
8 #include <wx/choice.h>
9
10 namespace conversation {
11
12 /** greebo: The visitor class that stores all the relevant eclassptrs
13 * into the given target map if the prefix matches.
14 */
15 class ConversationCommandInfoLoader :
16 public EntityClassVisitor
17 {
18 // The target map to populate
19 ConversationCommandInfoMap& _map;
20
21 // The entityDef prefix (e.g. "atdm:conversation_command_")
22 std::string _prefix;
23
24 public:
25 /**
26 * greebo: Pass the target map where all the eclassptrs should be stored into.
27 */
28 ConversationCommandInfoLoader(ConversationCommandInfoMap& map) :
29 _map(map),
30 _prefix(game::current::getValue<std::string>(GKEY_CONVERSATION_COMMAND_INFO_PREFIX))
31 {}
32
33 void visit(const IEntityClassPtr& eclass)
34 {
35 if (string::starts_with(eclass->getDeclName(), _prefix))
36 {
37 // We have a match, create a new structure
38 ConversationCommandInfoPtr commandInfo(new ConversationCommandInfo);
39
40 // Fill the values from the found entityDef
41 commandInfo->parseFromEntityClass(eclass);
42
43 // Store the structure to the target map
44 _map[commandInfo->name] = commandInfo;
45 }
46 }
47 };
48
49 ConversationCommandLibrary::ConversationCommandLibrary() {
50 loadConversationCommands();
51 }
52
53 const ConversationCommandInfo& ConversationCommandLibrary::findCommandInfo(const std::string& name) {
54 ConversationCommandInfoMap::const_iterator i = _commandInfo.find(name);
55
56 if (i == _commandInfo.end()) {
57 throw std::runtime_error(std::string("Could not find command info with the given name: ") + name);
58 }
59
60 return *(i->second);
61 }
62
63 const ConversationCommandInfo& ConversationCommandLibrary::findCommandInfo(int id) {
64
65 for (ConversationCommandInfoMap::const_iterator i = _commandInfo.begin();
66 i != _commandInfo.end();
67 ++i)
68 {
69 if (i->second->id == id) {
70 return *(i->second);
71 }
72 }
73
74 throw std::runtime_error(std::string("Could not find command info with the given ID: ") + string::to_string(id));
75 }
76
77 void ConversationCommandLibrary::loadConversationCommands()
78 {
79 // Load the possible command types
80 ConversationCommandInfoLoader loader(_commandInfo);
81 GlobalEntityClassManager().forEachEntityClass(loader);
82 }
83
84 void ConversationCommandLibrary::populateChoice(wxChoice* choice)
85 {
86 // Iterate over everything and push the data into the liststore
87 for (ConversationCommandInfoMap::const_iterator i = _commandInfo.begin();
88 i != _commandInfo.end();
89 ++i)
90 {
91 // Store the actor ID into a client data object and pass it along
92 choice->Append(i->second->name, new wxStringClientData(string::to_string(i->second->id)));
93 }
94 }
95
96 // Static accessor
97 ConversationCommandLibrary& ConversationCommandLibrary::Instance()
98 {
99 static ConversationCommandLibrary _instance;
100 return _instance;
101 }
102
103 } // namespace conversation