Debian Codegraph @

plugins/dm.stimresponse/ResponseEffect.cpp

raw
1 #include "ResponseEffect.h"
2
3 #include "i18n.h"
4 #include "string/convert.h"
5 #include "string/replace.h"
6 #include <regex>
7
8 ResponseEffect::ResponseEffect() :
9 _state(true),
10 _origState(_state),
11 _eclass(IEntityClassPtr()),
12 _argumentListBuilt(false),
13 _inherited(false)
14 {}
15
16 // Copy constructor
17 ResponseEffect::ResponseEffect(const ResponseEffect& other) :
18 _effectName(other._effectName),
19 _origName(other._origName),
20 _state(other._state),
21 _origState(other._origState),
22 _args(other._args),
23 _eclass(other._eclass),
24 _argumentListBuilt(other._argumentListBuilt),
25 _inherited(other._inherited)
26 {}
27
28 std::string ResponseEffect::getName() const {
29 return _effectName;
30 }
31
32 bool ResponseEffect::nameIsOverridden() {
33 return (_inherited && _effectName != _origName);
34 }
35
36 void ResponseEffect::setInherited(bool inherited) {
37 _inherited = inherited;
38 }
39
40 bool ResponseEffect::isInherited() const {
41 return _inherited;
42 }
43
44 bool ResponseEffect::isActive() const {
45 return _state;
46 }
47
48 void ResponseEffect::setActive(bool active, bool inherited) {
49 if (_inherited && !inherited) {
50 // This is an override action, just set the _state, not _origState
51 _state = active;
52 }
53 else {
54 // Ordinary write operation, set both state and origState
55 _state = active;
56 _origState = _state;
57 }
58 }
59
60 bool ResponseEffect::activeIsOverridden() {
61 return (_inherited && _state != _origState);
62 }
63
64 void ResponseEffect::setName(const std::string& name, bool inherited) {
65 if (_inherited && !inherited) {
66 // This is an override action
67 _effectName = name;
68 }
69 else {
70 // Ordinary write operation, save both values
71 _effectName = name;
72 _origName = name;
73 }
74
75 // Update the entityclass pointer
76 _eclass = ResponseEffectTypes::Instance().getEClassForName(_effectName);
77
78 // Build the argument list, if it hasn't been built up till now
79 if (!_argumentListBuilt) {
80 _argumentListBuilt = true;
81 buildArgumentList();
82 }
83 }
84
85 std::string ResponseEffect::getArgument(unsigned int index) const {
86 ArgumentList::const_iterator i = _args.find(index);
87
88 // Return "" if the argument was not found
89 return (i != _args.end()) ? i->second.value : "";
90 }
91
92 bool ResponseEffect::argIsOverridden(unsigned int index) {
93 ArgumentList::const_iterator i = _args.find(index);
94
95 if (i != _args.end()) {
96 return (i->second.value != i->second.origValue);
97 }
98 return false;
99 }
100
101 void ResponseEffect::setArgument(unsigned int index, const std::string& value, bool inherited) {
102 ArgumentList::const_iterator i = _args.find(index);
103
104 if (_inherited && !inherited) {
105 // This is an override operation
106 if (i != _args.end()) {
107 // Value exists, write to the <value> member
108 _args[index].value = value;
109 }
110 else {
111 // Value doesn't exist yet, initialise and write an empty "backup" value
112 // This indicates that the value was not set in the inheritance tree
113 Argument newArgument;
114 newArgument.value = value;
115 newArgument.origValue = "";
116
117 // Store the argument in the map
118 _args[index] = newArgument;
119 }
120 }
121 else {
122 if (i != _args.end()) {
123 // Argument exists
124 _args[index].value = value;
125 _args[index].origValue = value;
126 }
127 else {
128 // Argument doesn't exist, create it
129 Argument newArgument;
130 newArgument.value = value;
131 newArgument.origValue = value;
132
133 // Store the argument in the map
134 _args[index] = newArgument;
135 }
136 }
137 }
138
139 std::string ResponseEffect::getCaption() const {
140 return (_eclass != NULL)
141 ? _eclass->getAttributeValue("editor_caption")
142 : "";
143 }
144
145 IEntityClassPtr ResponseEffect::getEClass() const {
146 return _eclass;
147 }
148
149 ResponseEffect::ArgumentList& ResponseEffect::getArguments() {
150 return _args;
151 }
152
153 void ResponseEffect::buildArgumentList() {
154 if (_eclass == NULL) return;
155
156 for (int i = 1; i < 1000; i++) {
157 std::string argType = _eclass->getAttributeValue("editor_argType" + string::to_string(i));
158 std::string argDesc = _eclass->getAttributeValue("editor_argDesc" + string::to_string(i));
159 std::string argTitle = _eclass->getAttributeValue("editor_argTitle" + string::to_string(i));
160 std::string optional = _eclass->getAttributeValue("editor_argOptional" + string::to_string(i));
161
162 if (argType != "") {
163 // Check if the argument exists
164 ArgumentList::iterator found = _args.find(i);
165 if (found == _args.end()) {
166 Argument newArgument;
167 _args[i] = newArgument;
168 }
169
170 // Load the values into the structure
171 _args[i].type = argType;
172 _args[i].desc = argDesc;
173 _args[i].title = argTitle;
174 _args[i].optional = (optional == "1");
175 }
176 else {
177 // Empty argument type found, break the loop
178 break;
179 }
180 }
181 }
182
183 void ResponseEffect::clearArgumentList() {
184 _args.clear();
185 }
186
187 std::string ResponseEffect::removeMarkup(const std::string& input)
188 {
189 std::regex expr("(<[A-Za-z]+>)|(</[A-Za-z]+>)");
190 return std::regex_replace(input, expr, "");
191 }
192
193 std::string ResponseEffect::getArgumentStr()
194 {
195 if (_eclass == NULL) return _("Error: eclass pointer invalid.");
196
197 std::string returnValue = _eclass->getAttributeValue("editor_argString");
198 returnValue = removeMarkup(returnValue);
199
200 for (ArgumentList::iterator i = _args.begin(); i != _args.end(); i++) {
201 std::string needle = "[arg" + string::to_string(i->first) + "]";
202 std::string replacement = i->second.value;
203
204 // Check for a bool
205 if (i->second.type == "b") {
206 replacement = (i->second.value.empty()) ? _("no") : _("yes");
207 }
208
209 string::replace_all(returnValue, needle, replacement);
210 }
211
212 return returnValue;
213 }