Repository navigation
Expand file tree
/
Copy pathAttribute.cpp
More file actions
88 lines (74 loc) · 2.84 KB
/
Copy pathAttribute.cpp
File metadata and controls
88 lines (74 loc) · 2.84 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
#include "Attribute.h"
#include <QJsonObject>
// ─────────────────────────────────────────────
// Attribute
// ─────────────────────────────────────────────
QJsonObject Attribute::saveToJson() const
{
QJsonObject obj;
obj["key"] = key;
obj["displayName"] = displayName;
obj["defaultValue"] = defaultValue;
return obj;
}
void Attribute::loadFromJson(const QJsonObject& obj)
{
key = obj["key"].toString();
displayName = obj["displayName"].toString();
defaultValue = obj["defaultValue"].toInt(0);
}
// ─────────────────────────────────────────────
// AttributeValue
// ─────────────────────────────────────────────
QJsonObject AttributeValue::saveToJson() const
{
QJsonObject obj;
obj["key"] = key;
obj["baseValue"] = baseValue;
obj["currentValue"] = currentValue;
return obj;
}
void AttributeValue::loadFromJson(const QJsonObject& obj)
{
key = obj["key"].toString();
baseValue = obj["baseValue"].toInt(0);
currentValue = obj["currentValue"].toInt(0);
}
// ─────────────────────────────────────────────
// AttributeModifier
// ─────────────────────────────────────────────
int AttributeModifier::apply(int base) const
{
switch (op)
{
case ModifierOp::Plus: return base + value;
case ModifierOp::Minus: return base - value;
case ModifierOp::Multiply: return base * value;
}
return base;
}
QJsonObject AttributeModifier::saveToJson() const
{
QJsonObject obj;
obj["attributeKey"] = attributeKey;
obj["value"] = value;
switch (op)
{
case ModifierOp::Plus: obj["op"] = "Plus"; break;
case ModifierOp::Minus: obj["op"] = "Minus"; break;
case ModifierOp::Multiply: obj["op"] = "Multiply"; break;
}
obj["description"] = description;
return obj;
}
void AttributeModifier::loadFromJson(const QJsonObject& obj)
{
attributeKey = obj["attributeKey"].toString();
value = obj["value"].toInt(0);
const QString opStr = obj["op"].toString();
if (opStr == "Plus") op = ModifierOp::Plus;
else if (opStr == "Minus") op = ModifierOp::Minus;
else if (opStr == "Multiply") op = ModifierOp::Multiply;
else op = ModifierOp::Plus; // safe default
description = obj["description"].toString();
}