Repository navigation
Expand file tree
/
Copy pathfield.cpp
More file actions
432 lines (384 loc) · 14.2 KB
/
Copy pathfield.cpp
File metadata and controls
432 lines (384 loc) · 14.2 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 <data/field.hpp>
#include <data/field/text.hpp>
#include <data/field/choice.hpp>
#include <data/field/multiple_choice.hpp>
#include <data/field/boolean.hpp>
#include <data/field/image.hpp>
#include <data/field/symbol.hpp>
#include <data/field/color.hpp>
#include <data/field/information.hpp>
#include <data/field/package_choice.hpp>
#include <util/error.hpp>
DECLARE_DYNAMIC_ARG(Field const*, field_for_reading);
IMPLEMENT_DYNAMIC_ARG(Field const*, field_for_reading, nullptr);
// ----------------------------------------------------------------------------- : Field
Field::Field()
: index (0) // sensible default?
, editable (true)
, save_value (true)
, show_statistics (true)
, position_hint (0)
, identifying (false)
, card_list_column (100)
, card_list_width (100)
, card_list_visible(false)
, card_list_allow (true)
, card_list_align (ALIGN_LEFT)
, default_name (_("Default"))
, initial (script_default_nil)
{}
Field::~Field() {}
void Field::initDependencies(Context& ctx, const Dependency& dep) const {
script .initDependencies(ctx, dep);
default_script.initDependencies(ctx, dep);
sort_script .initDependencies(ctx, dep);
}
IMPLEMENT_REFLECTION(Field) {
REFLECT_IF_NOT_READING {
String type = typeName();
REFLECT(type);
}
REFLECT(name);
REFLECT(caption);
REFLECT(description);
REFLECT_N("icon", icon_filename);
REFLECT(editable);
REFLECT(save_value);
REFLECT(show_statistics);
REFLECT(position_hint);
REFLECT(identifying);
REFLECT(card_list_column);
REFLECT(card_list_width);
REFLECT(card_list_visible);
REFLECT(card_list_allow);
REFLECT(card_list_name);
REFLECT_N("card_list_alignment", card_list_align);
REFLECT(script);
REFLECT_N("default", default_script);
REFLECT(sort_script);
REFLECT(default_name);
WITH_DYNAMIC_ARG(field_for_reading, this);
REFLECT(initial);
}
void Field::after_reading(Version ver) {
name = canonical_name_form(name);
if(caption.empty()) caption = name_to_caption(name);
if(card_list_name.empty()) card_list_name = capitalize(caption);
}
template <>
intrusive_ptr<Field> read_new<Field>(Reader& reader) {
// there must be a type specified
String type;
reader.handle(_("type"), type);
if (type == _("text")) return intrusive(new TextField());
else if (type == _("choice")) return intrusive(new ChoiceField());
else if (type == _("multiple choice")) return intrusive(new MultipleChoiceField());
else if (type == _("boolean")) return intrusive(new BooleanField());
else if (type == _("image")) return intrusive(new ImageField());
else if (type == _("symbol")) return intrusive(new SymbolField());
else if (type == _("color")) return intrusive(new ColorField());
else if (type == _("info")) return intrusive(new InfoField());
else if (type == _("package choice")) return intrusive(new PackageChoiceField());
else if (type.empty()) {
reader.warning(_ERROR_1_("expected key", _("type")));
throw ParseError(_ERROR_("aborting parsing"));
} else {
reader.warning(_ERROR_1_("unsupported field type", type));
throw ParseError(_ERROR_("aborting parsing"));
}
}
// ----------------------------------------------------------------------------- : Style
Style::Style(const FieldP& field)
: fieldP(field)
, z_index(0)
, tab_index(0)
, left (1000000), top (1000000)
, width(0), height(0)
, right(1000000), bottom(1000000)
, angle(0)
, visible(true)
, automatic_side(AUTO_UNKNOWN)
, content_dependent(false)
{}
Style::~Style() {}
IMPLEMENT_REFLECTION(Style) {
REFLECT(z_index);
REFLECT(tab_index);
REFLECT(left);
REFLECT(width);
REFLECT(right);
REFLECT(top);
REFLECT(height);
REFLECT(bottom);
REFLECT(angle);
REFLECT(visible);
REFLECT(mask);
}
void init_object(const FieldP& field, StyleP& style) {
if (!style) style = field->newStyle();
}
template <> StyleP read_new<Style>(Reader&) {
throw InternalError(_("IndexMap contains nullptr StyleP the application should have crashed already"));
}
inline bool is_set(const Scriptable<double>& x) {
return x.isScripted() || x < 100000;
}
inline bool is_setw(const Scriptable<double>& x) {
return x.isScripted() || fabs(x()) > 0.001;
}
int Style::update(Context& ctx) {
int changed =
( left .update(ctx)
| width .update(ctx)
| right .update(ctx)
| top .update(ctx)
| height .update(ctx)
| bottom .update(ctx)
| angle .update(ctx) ) * CHANGE_SIZE
| visible.update(ctx) * CHANGE_OTHER
| mask .update(ctx) * CHANGE_MASK;
// determine automatic_side and attachment of rotation point
if (automatic_side == AUTO_UNKNOWN) {
if (!is_set (right)) automatic_side = (AutomaticSide)(automatic_side | AUTO_RIGHT);
else if (!is_setw(width)) automatic_side = (AutomaticSide)(automatic_side | AUTO_WIDTH);
else if (!is_set (left)) automatic_side = (AutomaticSide)(automatic_side | AUTO_LEFT);
else automatic_side = (AutomaticSide)(automatic_side | AUTO_LR);
if (!is_set (bottom)) automatic_side = (AutomaticSide)(automatic_side | AUTO_BOTTOM);
else if (!is_setw(height)) automatic_side = (AutomaticSide)(automatic_side | AUTO_HEIGHT);
else if (!is_set (top)) automatic_side = (AutomaticSide)(automatic_side | AUTO_TOP);
else automatic_side = (AutomaticSide)(automatic_side | AUTO_TB);
changed |= CHANGE_SIZE;
}
if (!changed) return CHANGE_NONE;
// update the automatic_side
if (automatic_side & AUTO_LEFT) left = right - width;
else if (automatic_side & AUTO_WIDTH) width = right - left;
else if (automatic_side & AUTO_RIGHT) right = left + width;
else {int lr = int(left + right); left = (lr - width) / 2; right = (lr + width) / 2; }
if (automatic_side & AUTO_TOP) top = bottom - height;
else if (automatic_side & AUTO_HEIGHT) height = bottom - top;
else if (automatic_side & AUTO_BOTTOM) bottom = top + height;
else {int tb = int(top + bottom); top = (tb - height) / 2; bottom = (tb + height) / 2; }
// adjust rotation point
if (angle != 0 && (automatic_side & (AUTO_LEFT | AUTO_TOP))) {
double s = sin(deg_to_rad(angle)), c = cos(deg_to_rad(angle));
if (automatic_side & AUTO_LEFT) { // attach right corner instead of left
left = left + width * (1 - c);
top = top + width * s;
}
if (automatic_side & AUTO_TOP) { // attach botom corner instead of top
left = left - height * s;
top = top + height * (1 - c);
}
}
if (width < 0) width = -width;
if (height < 0) height = -height;
// done
return changed;
}
bool Style::isVisible() const {
return visible
&& (width()) > 0
&& fabs(left()) < 100000
&& fabs(right()) < 100000
&& (height()) > 0
&& fabs(top()) < 100000
&& fabs(bottom()) < 100000;
}
bool Style::hasSize() const {
int h = is_setw(width) + is_set(left) + is_set(right);
int v = is_setw(height) + is_set(top) + is_set(bottom);
return h >= 2 && v >= 2;
}
void Style::initDependencies(Context& ctx, const Dependency& dep) const {
// left .initDependencies(ctx,dep);
// top .initDependencies(ctx,dep);
// width .initDependencies(ctx,dep);
// height .initDependencies(ctx,dep);
// visible.initDependencies(ctx,dep);
}
void Style::checkContentDependencies(Context& ctx, const Dependency& dep) const {
left .initDependencies(ctx,dep);
top .initDependencies(ctx,dep);
width .initDependencies(ctx,dep);
height .initDependencies(ctx,dep);
right .initDependencies(ctx,dep);
bottom .initDependencies(ctx,dep);
visible.initDependencies(ctx,dep);
mask .initDependencies(ctx,dep);
}
void Style::markDependencyMember(const String& name, const Dependency& dep) const {
// mark dependencies on content
if (dep.type == DEP_DUMMY && dep.index == false && starts_with(name, _("content "))) {
// anything that starts with "content_" is a content property
const_cast<Dependency&>(dep).index = true;
}
}
void mark_dependency_member(const Style& style, const String& name, const Dependency& dep) {
style.markDependencyMember(name,dep);
}
// ----------------------------------------------------------------------------- : StyleListener
void Style::addListener(StyleListener* listener) {
listeners.push_back(listener);
}
void Style::removeListener(StyleListener* listener) {
listeners.erase(
std::remove(
listeners.begin(),
listeners.end(),
listener
),
listeners.end()
);
}
void Style::tellListeners(int changes) {
for(auto& l : listeners) l->onStyleChange(changes);
}
StyleListener::StyleListener(const StyleP& style)
: styleP(style)
{
style->addListener(this);
}
StyleListener::~StyleListener() {
styleP->removeListener(this);
}
// ----------------------------------------------------------------------------- : Value : reflecting ScriptValues
// TODO: move this to a more sensible location
// possible values:
// * "quoted string" # a string
// * 123.456 # a number
// * fileref("quoted-string") # reference to a file in the set
// * rgb(123,123,123) # a color
// * nil # nil
// * true/false # a boolean
// * mark_default(value) # a value marked as being default
void parse_errors_to_reader_warnings(Reader& reader, vector<ScriptParseError> const& errors);
ScriptValueP script_local_image_file(LocalFileName const& filename);
ScriptValueP script_local_symbol_file(LocalFileName const& filename);
void Reader::handle(ScriptValueP& value) {
Field const* field = field_for_reading();
if (formatVersion() < 20001 && field) {
// in older versions, the format was based on the type of the field
if (dynamic_cast<BooleanField const*>(field)) {
// boolean field: boolean "yes" or "no"
bool x;
handle(x);
value = to_script(x);
} else if (dynamic_cast<TextField const*>(field) || dynamic_cast<ChoiceField const*>(field) || dynamic_cast<PackageChoiceField const*>(field)) {
// text, choice fields: string
String str;
handle(str);
value = to_script(str);
} else if (dynamic_cast<ColorField const*>(field)) {
// color field: color
Color x;
handle(x);
value = to_script(x);
} else if (dynamic_cast<ImageField const*>(field)) {
// image, symbol fields: string that is a filename in the set
String str;
handle(str);
value = script_local_image_file(LocalFileName::fromReadString(str));
} else if (dynamic_cast<SymbolField const*>(field)) {
// image, symbol fields: string that is a filename in the set
String str;
handle(str);
value = script_local_symbol_file(LocalFileName::fromReadString(str));
} else if (dynamic_cast<InfoField const*>(field)) {
// this should never happen, since info fields were not saved
String str;
handle(str);
} else {
throw InternalError(_("Reader::handle(ScriptValueP)"));
}
} else {
// in the new system, the type is stored in the file.
String unparsed;
handle(unparsed);
if (unparsed.empty()) {
value = script_default_nil;
} else {
vector<ScriptParseError> errors;
value = parse_value(unparsed, this->getPackage(), errors);
if (!value) {
value = script_default_nil;
}
parse_errors_to_reader_warnings(*this, errors);
}
}
}
void Writer::handle(ScriptValueP const& value) {
// TODO: Make a distinction in which values can be saved?
handle(value->toCode());
}
// ----------------------------------------------------------------------------- : Value
IMPLEMENT_DYNAMIC_ARG(Value*, value_being_updated, nullptr);
Value::Value(const FieldP& field)
: fieldP(field), value(field->initial)
{
assert(value);
}
Value::Value(const FieldP& field, const ScriptValueP& value)
: fieldP(field), value(value)
{
assert(value);
}
Value::~Value() {}
ValueP Value::clone() const {
return intrusive(new Value(*this));
}
bool Value::equals(const Value* that) {
return this == that;
}
String Value::toFriendlyString() const {
try {
return value->toFriendlyString();
} catch (...) {
return _("<") + value->typeName() + _(">");
}
}
bool Value::update(Context& ctx, const Action* act) {
WITH_DYNAMIC_ARG(last_update_age, last_modified.get()); // TODO: this is redundant, since it can be got from value_being_updated
WITH_DYNAMIC_ARG(value_being_updated, this);
bool change = false;
if (ScriptDefault const* dv = dynamic_cast<ScriptDefault*>(value.get())) {
ScriptValueP dvv = dv->un_default;
change = fieldP->default_script.invokeOn(ctx, dvv);
change |= fieldP->script.invokeOn(ctx, dvv);
if (change) value = make_default(dvv);
} else {
change = fieldP->script.invokeOn(ctx, value);
}
updateSortValue(ctx);
return change;
}
void Value::updateSortValue(Context& ctx) {
sort_value = fieldP->sort_script.invoke(ctx)->toString();
}
IMPLEMENT_REFLECTION_NAMELESS(Value) {
if (reflector.isWriting() && !fieldP->save_value) return;
if (reflector.isWriting() && is_default(value)) return;
WITH_DYNAMIC_ARG(field_for_reading, fieldP.get());
REFLECT_NAMELESS(value);
}
void init_object(const FieldP& field, ValueP& value) {
if (!value) {
value = field->newValue();
}
}
template <> ValueP read_new<Value>(Reader&) {
throw InternalError(_("IndexMap contains nullptr ValueP the application should have crashed already"));
}
void mark_dependency_member(const IndexMap<FieldP,ValueP>& value, const String& name, const Dependency& dep) {
IndexMap<FieldP,ValueP>::const_iterator it = value.find(name);
if (it != value.end()) {
(*it)->fieldP->dependent_scripts.add(dep);
}
}