forked from mrkite/minutor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentityidentifier.cpp
196 lines (169 loc) · 5.91 KB
/
entityidentifier.cpp
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
/** Copyright (c) 2014, EtlamGit */
#include <QDebug>
#include <assert.h>
#include "./entityidentifier.h"
#include "./json.h"
EntityInfo::EntityInfo(QString name, QString category, QColor brushColor,
QColor penColor) : name(name), category(category),
brushColor(brushColor), penColor(penColor) {}
// dummy for entities not found in *_entity.json
static EntityInfo entityDummy("Name unknown", "Others", // Name Category
Qt::black, Qt::black); // Black circle with Black border
// --------- --------- --------- ---------
// EntityIdentifier
// --------- --------- --------- ---------
EntityIdentifier::EntityIdentifier() {}
EntityIdentifier::~EntityIdentifier() {}
EntityIdentifier& EntityIdentifier::Instance() {
static EntityIdentifier singleton;
return singleton;
}
void EntityIdentifier::enableDefinitions(int packID) {
if (packID < 0) return;
for (auto it = packs.begin(); it != packs.end(); ++it) {
if (it->packID == packID) {
it->enabled = true;
return;
}
}
}
void EntityIdentifier::disableDefinitions(int packID) {
if (packID < 0) return;
for (auto it = packs.begin(); it != packs.end(); ++it) {
if (it->packID == packID)
it->enabled = false;
}
}
int EntityIdentifier::addDefinitions(JSONArray *defs, int packID) {
if (packID == -1) {
// find largest used packID
for (auto it = packs.constBegin(); it != packs.constEnd(); ++it) {
if (it->packID > packID)
packID = it->packID;
}
packID++; // use one higher than largest found
packs.append(TpackInfo(packID));
}
int len = defs->length();
for (int i = 0; i < len; i++)
parseCategoryDefinition(dynamic_cast<JSONObject *>(defs->at(i)), packID);
return packID;
}
EntityIdentifier::TentityMap& EntityIdentifier::getMapForPackID(int packID) {
for (auto it = packs.begin(); it != packs.end(); ++it) {
if (it->packID == packID)
return it->map;
}
return dummyMap;
}
void EntityIdentifier::parseCategoryDefinition(JSONObject *data, int packID) {
QString category;
if (data->has("category"))
category = data->at("category")->asString();
else
category = "Unknown";
QColor catcolor;
if (data->has("catcolor")) {
QString colorname = data->at("catcolor")->asString();
catcolor.setNamedColor(colorname);
assert(catcolor.isValid());
} else { // use hashed by name instead
quint32 hue = qHash(category);
catcolor.setHsv(hue % 360, 255, 255);
}
addCategory(qMakePair(category, catcolor));
if (data->has("entity")) {
JSONArray *entities = dynamic_cast<JSONArray *>(data->at("entity"));
int len = entities->length();
for (int e = 0; e < len; e++)
parseEntityDefinition(dynamic_cast<JSONObject *>(entities->at(e)),
category, catcolor, packID);
}
}
void EntityIdentifier::parseEntityDefinition(JSONObject *entity,
QString const &category,
QColor catcolor, int packID) {
QString id("unknown");
if (entity->has("id"))
id = entity->at("id")->asString().toLower();
if (entity->has("catcolor")) {
QString colorname = entity->at("catcolor")->asString();
catcolor.setNamedColor(colorname);
assert(catcolor.isValid());
}
QColor color;
if (entity->has("color")) {
QString colorname = entity->at("color")->asString();
color.setNamedColor(colorname);
assert(color.isValid());
} else { // use hashed by name instead
quint32 hue = qHash(id);
color.setHsv(hue % 360, 255, 255);
}
QString name;
if (entity->has("name")) {
// use provided name
name = entity->at("name")->asString();
} else {
// or try to build name automatically
// split at underscores
QStringList tokens = id.toLower().replace('_',' ').split(" ");
// make first character uppercase
for (QList<QString>::iterator tokItr = tokens.begin(); tokItr != tokens.end(); ++tokItr) {
(*tokItr) = (*tokItr).at(0).toUpper() + (*tokItr).mid(1);
}
name = tokens.join(" ");
}
// enter entity into manager
TentityMap& map = getMapForPackID(packID);
map.insert(id, EntityInfo(name, category, catcolor, color));
// add duplicates: when new 1.11+ or 1.13+ id definitions are available
// legacy id is stored in own definition element (duplicates automatically)
if (entity->has("idlist")) {
JSONArray *idlist = dynamic_cast<JSONArray *>(entity->at("idlist"));
int len = idlist->length();
for (int j = 0; j < len; j++) {
QString idl = entity->at("idlist")->at(j)->asString().toLower();
map.insert(idl, EntityInfo(name, category, catcolor, color));
}
}
}
bool EntityIdentifier::addCategory(QPair<QString, QColor> cat) {
for (auto it = categories.constBegin(); it != categories.constEnd(); ++it) {
if (it->first == cat.first)
return false;
}
categories.append(cat);
return true;
}
int EntityIdentifier::getNumCategories() const {
return categories.size();
}
EntityIdentifier::TcatList const &EntityIdentifier::getCategoryList() const {
return categories;
}
QColor EntityIdentifier::getCategoryColor(const QString name) const {
for (auto it = categories.constBegin(); it != categories.constEnd(); ++it) {
if (it->first == name)
return it->second;
}
return Qt::black; // dummy
}
EntityInfo const &EntityIdentifier::getEntityInfo(const QString id) const {
for (auto it = packs.constBegin(); it != packs.constEnd(); ++it) {
if (it->enabled) {
// convert ID to lower case and strip "minecraft:" if exists
TentityMap::const_iterator info = it->map.find(id.toLower().remove("minecraft:"));
if (info != it->map.end()) { // found it
return info.value();
}
}
}
return entityDummy;
}
QColor EntityIdentifier::getBrushColor(const QString id) const {
return getEntityInfo(id).brushColor;
}
QColor EntityIdentifier::getPenColor(const QString id) const {
return getEntityInfo(id).penColor;
}