File: ResponseEffect.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 (213 lines) | stat: -rw-r--r-- 5,389 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "ResponseEffect.h"

#include "i18n.h"
#include "string/convert.h"
#include "string/replace.h"
#include <regex>

ResponseEffect::ResponseEffect() :
	_state(true),
	_origState(_state),
	_eclass(IEntityClassPtr()),
	_argumentListBuilt(false),
	_inherited(false)
{}

// Copy constructor
ResponseEffect::ResponseEffect(const ResponseEffect& other) :
	_effectName(other._effectName),
	_origName(other._origName),
	_state(other._state),
	_origState(other._origState),
	_args(other._args),
	_eclass(other._eclass),
	_argumentListBuilt(other._argumentListBuilt),
	_inherited(other._inherited)
{}

std::string ResponseEffect::getName() const {
	return _effectName;
}

bool ResponseEffect::nameIsOverridden() {
	return (_inherited && _effectName != _origName);
}

void ResponseEffect::setInherited(bool inherited) {
	_inherited = inherited;
}

bool ResponseEffect::isInherited() const {
	return _inherited;
}

bool ResponseEffect::isActive() const {
	return _state;
}

void ResponseEffect::setActive(bool active, bool inherited) {
	if (_inherited && !inherited) {
		// This is an override action, just set the _state, not _origState
		_state = active;
	}
	else {
		// Ordinary write operation, set both state and origState
		_state = active;
		_origState = _state;
	}
}

bool ResponseEffect::activeIsOverridden() {
	return (_inherited && _state != _origState);
}

void ResponseEffect::setName(const std::string& name, bool inherited) {
	if (_inherited && !inherited) {
		// This is an override action
		_effectName = name;
	}
	else {
		// Ordinary write operation, save both values
		_effectName = name;
		_origName = name;
	}

	// Update the entityclass pointer
	_eclass = ResponseEffectTypes::Instance().getEClassForName(_effectName);

	// Build the argument list, if it hasn't been built up till now
	if (!_argumentListBuilt) {
		_argumentListBuilt = true;
		buildArgumentList();
	}
}

std::string ResponseEffect::getArgument(unsigned int index) const {
	ArgumentList::const_iterator i = _args.find(index);

	// Return "" if the argument was not found
	return (i != _args.end()) ? i->second.value : "";
}

bool ResponseEffect::argIsOverridden(unsigned int index) {
	ArgumentList::const_iterator i = _args.find(index);

	if (i != _args.end()) {
		return (i->second.value != i->second.origValue);
	}
	return false;
}

void ResponseEffect::setArgument(unsigned int index, const std::string& value, bool inherited) {
	ArgumentList::const_iterator i = _args.find(index);

	if (_inherited && !inherited) {
		// This is an override operation
		if (i != _args.end()) {
			// Value exists, write to the <value> member
			_args[index].value = value;
		}
		else {
			// Value doesn't exist yet, initialise and write an empty "backup" value
			// This indicates that the value was not set in the inheritance tree
			Argument newArgument;
			newArgument.value = value;
			newArgument.origValue = "";

			// Store the argument in the map
			_args[index] = newArgument;
		}
	}
	else {
		if (i != _args.end()) {
			// Argument exists
			_args[index].value = value;
			_args[index].origValue = value;
		}
		else {
			// Argument doesn't exist, create it
			Argument newArgument;
			newArgument.value = value;
			newArgument.origValue = value;

			// Store the argument in the map
			_args[index] = newArgument;
		}
	}
}

std::string ResponseEffect::getCaption() const {
	return (_eclass != NULL)
		   ? _eclass->getAttributeValue("editor_caption")
		   : "";
}

IEntityClassPtr ResponseEffect::getEClass() const {
	return _eclass;
}

ResponseEffect::ArgumentList& ResponseEffect::getArguments() {
	return _args;
}

void ResponseEffect::buildArgumentList() {
	if (_eclass == NULL) return;

	for (int i = 1; i < 1000; i++) {
		std::string argType = _eclass->getAttributeValue("editor_argType" + string::to_string(i));
		std::string argDesc = _eclass->getAttributeValue("editor_argDesc" + string::to_string(i));
		std::string argTitle = _eclass->getAttributeValue("editor_argTitle" + string::to_string(i));
		std::string optional = _eclass->getAttributeValue("editor_argOptional" + string::to_string(i));

		if (argType != "") {
			// Check if the argument exists
			ArgumentList::iterator found = _args.find(i);
			if (found == _args.end()) {
				Argument newArgument;
				_args[i] = newArgument;
			}

			// Load the values into the structure
			_args[i].type = argType;
			_args[i].desc = argDesc;
			_args[i].title = argTitle;
			_args[i].optional = (optional == "1");
		}
		else {
			// Empty argument type found, break the loop
			break;
		}
	}
}

void ResponseEffect::clearArgumentList() {
	_args.clear();
}

std::string ResponseEffect::removeMarkup(const std::string& input)
{
	std::regex expr("(<[A-Za-z]+>)|(</[A-Za-z]+>)");
	return std::regex_replace(input, expr, "");
}

std::string ResponseEffect::getArgumentStr()
{
	if (_eclass == NULL) return _("Error: eclass pointer invalid.");

	std::string returnValue = _eclass->getAttributeValue("editor_argString");
	returnValue = removeMarkup(returnValue);

	for (ArgumentList::iterator i = _args.begin(); i != _args.end(); i++) {
		std::string needle = "[arg" + string::to_string(i->first) + "]";
		std::string replacement = i->second.value;

		// Check for a bool
		if (i->second.type == "b") {
			replacement = (i->second.value.empty()) ? _("no") : _("yes");
		}

		string::replace_all(returnValue, needle, replacement);
	}

	return returnValue;
}