File: ConversationCommandLibrary.cpp

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (103 lines) | stat: -rw-r--r-- 2,865 bytes parent folder | download | duplicates (3)
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
#include "ConversationCommandLibrary.h"

#include "ieclass.h"
#include "iregistry.h"
#include "gamelib.h"
#include "string/convert.h"
#include "string/predicate.h"
#include <wx/choice.h>

namespace conversation {

/** greebo: The visitor class that stores all the relevant eclassptrs
 * 			into the given target map if the prefix matches.
 */
class ConversationCommandInfoLoader :
	public EntityClassVisitor
{
	// The target map to populate
	ConversationCommandInfoMap& _map;

	// The entityDef prefix (e.g. "atdm:conversation_command_")
	std::string _prefix;

public:
	/**
	 * greebo: Pass the target map where all the eclassptrs should be stored into.
	 */
	ConversationCommandInfoLoader(ConversationCommandInfoMap& map) :
		_map(map),
		_prefix(game::current::getValue<std::string>(GKEY_CONVERSATION_COMMAND_INFO_PREFIX))
	{}

	void visit(const IEntityClassPtr& eclass)
	{
		if (string::starts_with(eclass->getDeclName(), _prefix))
		{
			// We have a match, create a new structure
			ConversationCommandInfoPtr commandInfo(new ConversationCommandInfo);

			// Fill the values from the found entityDef
			commandInfo->parseFromEntityClass(eclass);

			// Store the structure to the target map
			_map[commandInfo->name] = commandInfo;
		}
	}
};

ConversationCommandLibrary::ConversationCommandLibrary() {
	loadConversationCommands();
}

const ConversationCommandInfo& ConversationCommandLibrary::findCommandInfo(const std::string& name) {
	ConversationCommandInfoMap::const_iterator i = _commandInfo.find(name);

	if (i == _commandInfo.end()) {
		throw std::runtime_error(std::string("Could not find command info with the given name: ") + name);
	}

	return *(i->second);
}

const ConversationCommandInfo& ConversationCommandLibrary::findCommandInfo(int id) {

	for (ConversationCommandInfoMap::const_iterator i = _commandInfo.begin();
		 i != _commandInfo.end();
		 ++i)
	{
		if (i->second->id == id) {
			return *(i->second);
		}
	}

	throw std::runtime_error(std::string("Could not find command info with the given ID: ") + string::to_string(id));
}

void ConversationCommandLibrary::loadConversationCommands()
{
	// Load the possible command types
	ConversationCommandInfoLoader loader(_commandInfo);
	GlobalEntityClassManager().forEachEntityClass(loader);
}

void ConversationCommandLibrary::populateChoice(wxChoice* choice)
{
	// Iterate over everything and push the data into the liststore
	for (ConversationCommandInfoMap::const_iterator i = _commandInfo.begin();
		 i != _commandInfo.end();
		 ++i)
	{
		// Store the actor ID into a client data object and pass it along
		choice->Append(i->second->name, new wxStringClientData(string::to_string(i->second->id)));
	}
}

// Static accessor
ConversationCommandLibrary& ConversationCommandLibrary::Instance()
{
	static ConversationCommandLibrary _instance;
	return _instance;
}

} // namespace conversation