Repository navigation
Expand file tree
/
Copy patheditor.cpp
More file actions
432 lines (408 loc) · 14.6 KB
/
Copy patheditor.cpp
File metadata and controls
432 lines (408 loc) · 14.6 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2012 Twan van Laarhoven and Sean Hunt |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
// ----------------------------------------------------------------------------- : Includes
#include <util/prec.hpp>
#include <script/functions/functions.hpp>
#include <script/functions/util.hpp>
#include <util/tagged_string.hpp>
#include <data/set.hpp>
#include <data/game.hpp>
#include <data/card.hpp>
#include <data/stylesheet.hpp>
#include <data/field/text.hpp>
#include <data/field/choice.hpp>
#include <data/field/multiple_choice.hpp>
#include <data/action/value.hpp>
#include <boost/range/adaptor/reversed.hpp>
#include <boost/range/combine.hpp>
using std::get;
using std::make_pair;
using std::min;
using std::pair;
// ----------------------------------------------------------------------------- : Combined editor
// Combining multiple (text) values into a single one
// The combined value is value1 <sep>something</sep> value2 <sep>something</sep> value3
//
SCRIPT_FUNCTION_WITH_DEP(combined_editor) {
// read 'field#' arguments
vector<TextValue*> values;
for (int i = 0 ; ; ++i) {
String name = _("field"); if (i > 0) name = name << i;
SCRIPT_OPTIONAL_PARAM_N(ValueP, name, value) {
TextValue* text_value = dynamic_cast<TextValue*>(value.get());
if (!text_value) throw ScriptError(_("Argument '")+name+_("' should be a text field"));
values.push_back(text_value);
} else if (i > 0) break;
}
if (values.empty()) {
throw ScriptError(_("No fields specified for combined_editor"));
}
// read 'separator#' arguments
vector<String> separators;
for (int i = 0 ; ; ++i) {
String name = _("separator"); if (i > 0) name = name << i;
SCRIPT_OPTIONAL_PARAM_N(String, name, separator) {
separators.push_back(separator);
} else if (i > 0) break;
}
if (separators.size() < values.size() - 1) {
throw ScriptError(String::Format(_("Not enough separators for combine_editor, expected %d"), values.size()-1));
}
// the value
SCRIPT_PARAM_C(String, value);
// remove suffix/prefix
SCRIPT_OPTIONAL_PARAM_(String, prefix);
SCRIPT_OPTIONAL_PARAM_(String, suffix);
if (is_substr(value,0,_("<prefix"))) {
value = value.substr(min(value.size(), match_close_tag_end(value, 0)));
}
size_t pos = value.rfind(_("<suffix"));
if (pos != String::npos && match_close_tag_end(value,pos) >= value.size()) {
value = value.substr(0, pos);
}
// split the value
vector<pair<String,bool> > value_parts; // (value part, is empty)
pos = value.find(_("<sep"));
while (pos != String::npos) {
String part = value.substr(0, pos);
value_parts.push_back(make_pair(part, false));
value = value.substr(min(match_close_tag_end(value,pos), value.size()));
pos = value.find(_("<sep"));
}
value_parts.push_back(make_pair(value, false));
value_parts.resize(values.size()); // TODO: what if there are more value_parts than values?
// update the values if our input value is newer?
Age new_value_update = last_update_age();
for(auto&& vnv : boost::combine(values, value_parts)) {
auto& v = get<0>(vnv);
auto& nv = get<1>(vnv);
if (v->last_modified < new_value_update) {
bool changed = v->value->toString() != nv.first;
if (changed) v->value = to_script(nv.first);
v->last_modified = new_value_update;
changed |= v->update(ctx);
if (changed) { // notify of change
SCRIPT_OPTIONAL_PARAM_(CardP, card);
SCRIPT_PARAM(Set*, set);
ScriptValueEvent change(card.get(), v);
set->actions.tellListeners(change, false);
}
}
nv.first = v->value->toString();
nv.second = index_to_untagged(nv.first, nv.first.size()) == 0;
}
// options
SCRIPT_PARAM_DEFAULT(bool, hide_when_empty, false);
SCRIPT_PARAM_DEFAULT(bool, soft_before_empty, false);
// recombine the parts
String new_value = value_parts.front().first;
bool new_value_empty = value_parts.front().second;
size_t size_before_last = 0;
for (size_t i = 1 ; i < value_parts.size() ; ++i) {
size_before_last = new_value.size();
if (value_parts[i].second && new_value_empty && hide_when_empty) {
// no separator
} else if (value_parts[i].second && soft_before_empty) {
// soft separator
new_value += _("<sep-soft>") + separators[i - 1] + _("</sep-soft>");
new_value_empty = false;
} else {
// normal separator
new_value += _("<sep>") + separators[i - 1] + _("</sep>");
new_value_empty = false;
}
new_value += value_parts[i].first;
}
if (!new_value_empty || !hide_when_empty) {
if (!suffix.empty()) {
if (is_substr(new_value, size_before_last, _("<sep-soft>")) && value_parts.size() >= 2) {
// If the value ends in a soft separator, we have this situation:
// [blah]<sep-soft>ABC</sep-soft><suffix>XYZ</suffix>
// This renderes as:
// [blah] XYZ
// Which looks bad, so instead change the text to
// [blah]<sep>XYZ<soft>ABC</soft></sep>
// Which might be slightly incorrect, but soft text doesn't matter anyway.
size_t after = min(new_value.size(), match_close_tag_end(new_value, size_before_last));
new_value = new_value.substr(0, size_before_last)
+ _("<sep>")
+ suffix
+ _("<soft>")
+ separators[value_parts.size() - 2]
+ _("</soft></sep>")
+ new_value.substr(after);
} else {
new_value += _("<suffix>") + suffix + _("</suffix>");
}
}
if (!prefix.empty()) {
new_value = _("<prefix>") + prefix + _("</prefix>") + new_value;
}
}
SCRIPT_RETURN(new_value);
}
SCRIPT_FUNCTION_DEPENDENCIES(combined_editor) {
// read 'field#' arguments
vector<FieldP> fields;
for (int i = 0 ; ; ++i) {
String name = _("field"); if (i > 0) name = name << i;
SCRIPT_OPTIONAL_PARAM_N(ValueP, name, value) {
fields.push_back(value->fieldP);
} else if (i > 0) break;
}
// Find the target field
SCRIPT_PARAM_C(Set*, set);
GameP game = set->game;
FieldP target_field;
if (dep.type == DEP_CARD_FIELD) target_field = game->card_fields[dep.index];
else if (dep.type == DEP_SET_FIELD) target_field = game->set_fields[dep.index];
else if (dep.type == DEP_EXTRA_CARD_FIELD) {
SCRIPT_PARAM_C(StyleSheetP, stylesheet);
target_field = stylesheet->extra_card_fields[dep.index];
}
else throw InternalError(_("Finding dependencies of combined error for non card/set field"));
// Add dependencies, from target_field on field#
// For card fields
size_t j = 0;
for(auto& f : game->card_fields) {
Dependency dep(DEP_CARD_COPY_DEP, j++);
for(auto& fn : fields) {
if (f == fn) {
target_field->dependent_scripts.add(dep);
break;
}
}
}
// For set fields
j = 0;
for(auto& f : game->set_fields) {
Dependency dep(DEP_SET_COPY_DEP, j++);
for(auto& fn : fields) {
if (f == fn) {
target_field->dependent_scripts.add(dep);
break;
}
}
}
return dependency_dummy;
}
// ----------------------------------------------------------------------------- : Choice values
// convert a full choice name into the name of the top level group it is in
SCRIPT_FUNCTION(primary_choice) {
SCRIPT_PARAM_C(ValueP,input);
ChoiceValueP value = dynamic_pointer_cast<ChoiceValue>(input);
if (!value) {
throw ScriptError(_("Argument to 'primary_choice' should be a choice value"));
}
ChoiceFieldP field = dynamic_pointer_cast<ChoiceField>(value->fieldP);
if (!field) {
throw ScriptError(_("Argument to 'primary_choice' should be a choice value"));
}
// determine choice
int id = field->choices->choiceId(value->value->toString());
// find the last group that still contains id
const vector<ChoiceField::ChoiceP>& choices = field->choices->choices;
for(auto const& c : boost::adaptors::reverse(choices)) {
if (id >= c->first_id) {
SCRIPT_RETURN(c->name);
}
}
SCRIPT_RETURN(_(""));
}
// ----------------------------------------------------------------------------- : Multiple choice values
/// Iterate over a comma separated list
bool next_choice(size_t& start, size_t& end, const String& input) {
if (start >= input.size()) return false;
// ingore whitespace
while (start < input.size() && input.GetChar(start) == _(' ')) ++start;
// find end
end = input.find_first_of(_(','), start);
if (end == String::npos) end = input.size();
return true;
}
/// Is the given choice active?
bool chosen(const String& choice, const String& input) {
for (size_t start = 0, end = 0 ; next_choice(start,end,input) ; start = end+1 ) {
// does this choice match the one asked about?
if (end - start == choice.size() && is_substr(input, start, choice)) {
return true;
}
}
return false;
}
// is the given choice active?
SCRIPT_FUNCTION(chosen) {
SCRIPT_PARAM_C(String,choice);
SCRIPT_PARAM_C(String,input);
SCRIPT_RETURN(chosen(choice, input));
}
/// Filter the choices
/** Keep at most max elements of choices,
* and at least min. Use prefered as choice to add/keep in case of conflicts.
*/
String filter_choices(const String& input, const vector<String>& choices, int min, int max, String prefered) {
if (choices.empty()) return input; // do nothing, shouldn't happen, but better make sure
String ret;
int count = 0;
vector<bool> seen(choices.size()); // which choices have we seen in input?
// walk over the input
for (size_t start = 0, end = 0 ; next_choice(start,end,input) ; start = end +1) {
// is this choice in choices
bool in_choices = false;
for (size_t i = 0 ; i < choices.size() ; ++i) {
if (!seen[i] && is_substr(input, start, choices[i])) {
seen[i] = true; ++count;
in_choices = true;
break;
}
}
// is this choice unaffected?
if (!in_choices) {
if (!ret.empty()) ret += _(", ");
ret += input.substr(start, end - start);
}
}
// keep more choices
if (count < min) {
// add prefered choice
for (size_t i = 0 ; i < choices.size() ; ++i) {
if (choices[i] == prefered) {
if (!seen[i]) {
seen[i] = true; ++count;
}
break;
}
}
// add more choices
for (size_t i = 0 ; i < choices.size() ; ++i) {
if (count >= min) break;
if (!seen[i]) {
seen[i] = true; ++count;
}
}
}
// keep less choices
if (count > max) {
for (size_t i = choices.size(); i > 0 ; --i) {
if (count <= max) break;
if (seen[i+1]) {
// we would rather not remove prefered choice
if (max > 0 && choices[i+1] == prefered) {
continue;
}
seen[i+1] = false; --count;
}
}
}
// add the 'seen' choices to ret
for (size_t i = 0 ; i < choices.size() ; ++i) {
if (seen[i]) {
if (!ret.empty()) ret += _(", ");
ret += choices[i];
}
}
return ret;
}
// read 'choice#' arguments
void read_choices_param(Context& ctx, vector<String>& choices_out) {
SCRIPT_OPTIONAL_PARAM_C(String,choices) {
for (size_t start = 0, end = 0 ; next_choice(start,end,choices) ; start = end + 1) {
choices_out.push_back(choices.substr(start,end-start));
}
} else {
// old style: separate arguments
SCRIPT_OPTIONAL_PARAM_C(String,choice) {
choices_out.push_back(choice);
} else {
for (int i = 1 ; ; ++i) {
String name = _("choice");
SCRIPT_OPTIONAL_PARAM_N(String, name << i, choice) {
choices_out.push_back(choice);
} else break;
}
}
}
}
// add the given choice if it is not already active
SCRIPT_FUNCTION(require_choice) {
SCRIPT_PARAM_C(String,input);
SCRIPT_OPTIONAL_PARAM_(String,last_change);
vector<String> choices;
read_choices_param(ctx, choices);
SCRIPT_RETURN(filter_choices(input, choices, 1, (int)choices.size(), last_change));
}
// make sure at most one of the choices is active
SCRIPT_FUNCTION(exclusive_choice) {
SCRIPT_PARAM_C(String,input);
SCRIPT_OPTIONAL_PARAM_(String,last_change);
vector<String> choices;
read_choices_param(ctx, choices);
SCRIPT_RETURN(filter_choices(input, choices, 0, 1, last_change));
}
// make sure exactly one of the choices is active
SCRIPT_FUNCTION(require_exclusive_choice) {
SCRIPT_PARAM_C(String,input);
SCRIPT_OPTIONAL_PARAM_(String,last_change);
vector<String> choices;
read_choices_param(ctx, choices);
SCRIPT_RETURN(filter_choices(input, choices, 1, 1, last_change));
}
// make sure none of the choices are active
SCRIPT_FUNCTION(remove_choice) {
SCRIPT_PARAM_C(String,input);
vector<String> choices;
read_choices_param(ctx, choices);
SCRIPT_RETURN(filter_choices(input, choices, 0, 0, _("")));
}
// count how many choices are active
SCRIPT_FUNCTION(count_chosen) {
SCRIPT_PARAM_C(String,input);
SCRIPT_OPTIONAL_PARAM_C(String,choices) {
// only count specific choices
int count = 0;
for (size_t start = 0, end = 0 ; next_choice(start,end,choices) ; start = end + 1) {
if (chosen(choices.substr(start,end-start),input)) ++count;
}
SCRIPT_RETURN(count);
} else {
// count all choices => count comma's + 1
if (input.empty()) {
SCRIPT_RETURN(0);
} else {
int count = 1;
for(auto& c : input) {
if (c == _(',')) {
++count;
}
}
SCRIPT_RETURN(count);
}
}
}
// ----------------------------------------------------------------------------- : Default values
// is the given choice active?
SCRIPT_FUNCTION(mark_default) {
SCRIPT_PARAM_C(ScriptValueP,input);
return intrusive(new ScriptDefault(input));
}
SCRIPT_FUNCTION(is_default) {
SCRIPT_PARAM_C(ScriptValueP,input);
return intrusive(new ScriptDefault(input));
}
// ----------------------------------------------------------------------------- : Init
void init_script_editor_functions(Context& ctx) {
ctx.setVariable(_("forward_editor"), script_combined_editor); // compatability
ctx.setVariable(_("combined_editor"), script_combined_editor);
ctx.setVariable(_("primary_choice"), script_primary_choice);
ctx.setVariable(_("chosen"), script_chosen);
ctx.setVariable(_("count_chosen"), script_count_chosen);
ctx.setVariable(_("require_choice"), script_require_choice);
ctx.setVariable(_("exclusive_choice"), script_exclusive_choice);
ctx.setVariable(_("require_exclusive_choice"), script_require_exclusive_choice);
ctx.setVariable(_("remove_choice"), script_remove_choice);
ctx.setVariable(_("is_default"), script_is_default);
ctx.setVariable(_("mark_default"), script_mark_default);
}