This repository was archived by the owner on Aug 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathitemtooltip.cpp
442 lines (390 loc) · 15.7 KB
/
itemtooltip.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
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
433
434
435
436
437
438
439
440
441
442
/*
Copyright 2015 Ilya Zhuravlev
This file is part of Acquisition.
Acquisition is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Acquisition is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Acquisition. If not, see <http://www.gnu.org/licenses/>.
*/
#include "itemtooltip.h"
#include <QPainter>
#include <QString>
#include <string>
#include <vector>
#include "QsLog.h"
#include "item.h"
#include "util.h"
int qInitResources_resources();
Q_CONSTRUCTOR_FUNCTION(qInitResources_resources);
static const int LINKH_HEIGHT = 16;
static const int LINKH_WIDTH = 38;
static const int LINKV_HEIGHT = LINKH_WIDTH;
static const int LINKV_WIDTH = LINKH_HEIGHT;
static const QImage link_h(":/sockets/linkH.png");
static const QImage link_v(":/sockets/linkV.png");
static const QImage elder_1x1(":/backgrounds/ElderBackground_1x1.png");
static const QImage elder_1x3(":/backgrounds/ElderBackground_1x3.png");
static const QImage elder_1x4(":/backgrounds/ElderBackground_1x4.png");
static const QImage elder_2x1(":/backgrounds/ElderBackground_2x1.png");
static const QImage elder_2x2(":/backgrounds/ElderBackground_2x2.png");
static const QImage elder_2x3(":/backgrounds/ElderBackground_2x3.png");
static const QImage elder_2x4(":/backgrounds/ElderBackground_2x4.png");
static const QImage shaper_1x1(":/backgrounds/ShaperBackground_1x1.png");
static const QImage shaper_1x3(":/backgrounds/ShaperBackground_1x3.png");
static const QImage shaper_1x4(":/backgrounds/ShaperBackground_1x4.png");
static const QImage shaper_2x1(":/backgrounds/ShaperBackground_2x1.png");
static const QImage shaper_2x2(":/backgrounds/ShaperBackground_2x2.png");
static const QImage shaper_2x3(":/backgrounds/ShaperBackground_2x3.png");
static const QImage shaper_2x4(":/backgrounds/ShaperBackground_2x4.png");
static const QImage shaper_icon(":/tooltip/ShaperItemSymbol.png");
static const QImage elder_icon(":/tooltip/ElderItemSymbol.png");
static const int HEADER_SINGLELINE_WIDTH = 29;
static const int HEADER_SINGLELINE_HEIGHT = 34;
static const int HEADER_DOUBLELINE_WIDTH = 44;
static const int HEADER_DOUBLELINE_HEIGHT = 54;
static const QSize HEADER_SINGLELINE_SIZE(HEADER_SINGLELINE_WIDTH, HEADER_SINGLELINE_HEIGHT);
static const QSize HEADER_DOUBLELINE_SIZE(HEADER_DOUBLELINE_WIDTH, HEADER_DOUBLELINE_HEIGHT);
static const QSize HEADER_OVERLAY_SIZE(27, 27);
/*
PoE colors:
Default: 0
Augmented: 1
Unmet: 2
PhysicalDamage: 3
FireDamage: 4
ColdDamage: 5
LightningDamage: 6
ChaosDamage: 7
MagicItem: 8
RareItem: 9
UniqueItem: 10
*/
static std::vector<std::string> kPoEColors = {
"#fff",
"#88f",
"#d20000",
"#fff",
"#960000",
"#366492",
"gold",
"#d02090"
};
static std::string ColorPropertyValue(const ItemPropertyValue &value) {
size_t type = value.type;
if (type >= kPoEColors.size())
type = 0;
return "<font color='" + kPoEColors[type] + "'>" + value.str + "</font>";
}
static std::string FormatProperty(const ItemProperty &prop) {
if (prop.display_mode == 3) {
QString format(prop.name.c_str());
for (auto &value : prop.values)
format = format.arg(ColorPropertyValue(value).c_str());
return format.toStdString();
}
std::string text = prop.name;
if (prop.values.size()) {
if (prop.name.size() > 0)
text += ": ";
bool first = true;
for (auto &value : prop.values) {
if (!first)
text += ", ";
first = false;
text += ColorPropertyValue(value);
}
}
return text;
}
static std::string GenerateProperties(const Item &item) {
std::string text;
bool first = true;
for (auto &property : item.text_properties()) {
if (!first)
text += "<br>";
first = false;
text += FormatProperty(property);
}
return text;
}
static std::string GenerateRequirements(const Item &item) {
std::string text;
bool first = true;
// Talisman level is not really a requirement but it lives in the requirements section
if (item.talisman_tier())
text += "Talisman Tier: " + std::to_string(item.talisman_tier()) + "<br>";
for (auto &requirement : item.text_requirements()) {
text += first ? "Requires " : ", ";
first = false;
text += requirement.name + " " + ColorPropertyValue(requirement.value);
}
return text;
}
static std::string ModListAsString(const ItemMods &list) {
std::string mods;
bool first = true;
for (auto &mod : list) {
mods += (first ? "" : "<br>") + mod;
first = false;
}
if (mods.empty())
return "";
return ColorPropertyValue(ItemPropertyValue{ mods, 1 });
}
static std::vector<std::string> GenerateMods(const Item &item) {
std::vector<std::string> out;
auto &mods = item.text_mods();
for (auto &mod_type : ITEM_MOD_TYPES) {
std::string mod_list = ModListAsString(mods.at(mod_type));
if (!mod_list.empty())
out.push_back(mod_list);
}
return out;
}
static std::string GenerateItemInfo(const Item &item, const std::string &key, bool fancy) {
std::vector<std::string> sections;
std::string properties_text = GenerateProperties(item);
if (properties_text.size() > 0)
sections.push_back(properties_text);
std::string requirements_text = GenerateRequirements(item);
if (requirements_text.size() > 0)
sections.push_back(requirements_text);
std::vector<std::string> mods = GenerateMods(item);
sections.insert(sections.end(), mods.begin(), mods.end());
std::string unmet;
if (!item.identified())
unmet += "Unidentified";
if (item.corrupted())
unmet += (unmet.empty() ? "" : "<br>") + std::string("Corrupted");
if (!unmet.empty())
sections.push_back(ColorPropertyValue(ItemPropertyValue{ unmet, 2 }));
std::string text;
bool first = true;
for (auto &s : sections) {
if (!first) {
text += "<br>";
if (fancy)
text += "<img src=':/tooltip/Separator" + key + ".png'><br>";
else
text += "<hr>";
}
first = false;
text += s;
}
if (!fancy)
text = ColorPropertyValue(ItemPropertyValue{ item.PrettyName(), 0 }) + "<hr>" + text;
return "<center>" + text + "</center>";
}
static std::vector<std::string> FrameToKey = {
"White",
"Magic",
"Rare",
"Unique",
"Gem",
"Currency"
};
static std::vector<std::string> FrameToColor = {
"#c8c8c8",
"#88f",
"#ff7",
"#af6025",
"#1ba29b",
"#aa9e82"
};
static void UpdateMinimap(const Item &item, Ui::MainWindow *ui) {
QPixmap pixmap(MINIMAP_SIZE, MINIMAP_SIZE);
pixmap.fill(QColor("transparent"));
QPainter painter(&pixmap);
painter.setBrush(QBrush(QColor(0x0c, 0x0b, 0x0b)));
painter.drawRect(0, 0, MINIMAP_SIZE, MINIMAP_SIZE);
const ItemLocation &location = item.location();
painter.setBrush(QBrush(location.socketed() ? Qt::blue : Qt::red));
QRectF rect = location.GetRect();
painter.drawRect(rect);
ui->minimapLabel->setPixmap(pixmap);
}
void GenerateItemHeaderSide(QLabel *itemHeader, bool leftNotRight, std::string header_path_prefix, bool singleline, Item::BASE_TYPES base) {
QImage header(QString::fromStdString(header_path_prefix + (leftNotRight ? "Left.png" : "Right.png")));
QSize header_size = singleline ? HEADER_SINGLELINE_SIZE : HEADER_DOUBLELINE_SIZE;
header = header.scaled(header_size);
QPixmap header_pixmap(header_size);
header_pixmap.fill(Qt::transparent);
QPainter header_painter(&header_pixmap);
header_painter.drawImage(0, 0, header);
if (base == Item::BASE_SHAPER || base == Item::BASE_ELDER) {
QImage overlay_image = base == Item::BASE_SHAPER ? shaper_icon : elder_icon;
overlay_image = overlay_image.scaled(HEADER_OVERLAY_SIZE);
int overlay_x = singleline ? (leftNotRight ? 2: 1) : (leftNotRight ? 2: 15);
int overlay_y = (int) ( 0.5 * (header.height() - overlay_image.height()) );
header_painter.drawImage(overlay_x, overlay_y, overlay_image);
}
itemHeader->setFixedSize(header_size);
itemHeader->setPixmap(header_pixmap);
}
void UpdateItemTooltip(const Item &item, Ui::MainWindow *ui) {
size_t frame = item.frameType();
if (frame >= FrameToKey.size())
frame = 0;
std::string key = FrameToKey[frame];
ui->propertiesLabel->setText(GenerateItemInfo(item, key, true).c_str());
ui->itemTextTooltip->setText(GenerateItemInfo(item, key, false).c_str());
UpdateMinimap(item, ui);
bool singleline = item.name().empty();
if (singleline) {
ui->itemNameFirstLine->hide();
ui->itemNameSecondLine->setAlignment(Qt::AlignCenter);
ui->itemNameContainerWidget->setFixedSize(16777215, HEADER_SINGLELINE_HEIGHT);
} else {
ui->itemNameFirstLine->show();
ui->itemNameFirstLine->setAlignment(Qt::AlignBottom | Qt::AlignHCenter);
ui->itemNameSecondLine->setAlignment(Qt::AlignTop | Qt::AlignHCenter);
ui->itemNameContainerWidget->setFixedSize(16777215, HEADER_DOUBLELINE_HEIGHT);
}
std::string suffix = (singleline && (frame == FRAME_TYPE_RARE || frame == FRAME_TYPE_UNIQUE)) ? "SingleLine" : "";
std::string header_path_prefix = ":/tooltip/ItemsHeader" + key + suffix;
GenerateItemHeaderSide(ui->itemHeaderLeft, true, header_path_prefix, singleline, item.baseType());
GenerateItemHeaderSide(ui->itemHeaderRight, false, header_path_prefix, singleline, item.baseType());
ui->itemNameContainerWidget->setStyleSheet(("border-radius: 0px; border: 0px; border-image: url(" + header_path_prefix + "Middle.png);").c_str());
ui->itemNameFirstLine->setText(item.name().c_str());
ui->itemNameSecondLine->setText(item.typeLine().c_str());
std::string css = "border-image: none; background-color: transparent; font-size: 20px; color: " + FrameToColor[frame];
ui->itemNameFirstLine->setStyleSheet(css.c_str());
ui->itemNameSecondLine->setStyleSheet(css.c_str());
}
QPixmap GenerateItemSockets(const int width, const int height, const std::vector<ItemSocket> &sockets) {
QPixmap pixmap(width * PIXELS_PER_SLOT, height * PIXELS_PER_SLOT); // This will ensure we have enough room to draw the slots
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
int socket_rows = 0;
int socket_columns = 0;
ItemSocket prev = { 255, '-' };
size_t i = 0;
if (sockets.size() == 0) {
// Do nothing
} else if (sockets.size() == 1) {
auto &socket = sockets.front();
QImage socket_image(":/sockets/" + QString(socket.attr) + ".png");
painter.drawImage(0, PIXELS_PER_SLOT * i, socket_image);
socket_rows = 1;
socket_columns = 1;
} else {
for (auto &socket : sockets) {
bool link = socket.group == prev.group;
QImage socket_image(":/sockets/" + QString(socket.attr) + ".png");
if (width == 1) {
painter.drawImage(0, PIXELS_PER_SLOT * i, socket_image);
if (link)
painter.drawImage(16, PIXELS_PER_SLOT * i - 19, link_v);
socket_columns = 1;
socket_rows = i + 1;
} else /* w == 2 */ {
int row = i / 2;
int column = i % 2;
if (row % 2 == 1)
column = 1 - column;
socket_columns = qMax(column + 1, socket_columns);
socket_rows = qMax(row + 1, socket_rows);
painter.drawImage(PIXELS_PER_SLOT * column, PIXELS_PER_SLOT * row, socket_image);
if (link) {
switch(i){
case 1:
case 3:
case 5:
// horizontal link
painter.drawImage(
PIXELS_PER_SLOT - LINKH_WIDTH / 2,
row * PIXELS_PER_SLOT + PIXELS_PER_SLOT / 2 - LINKH_HEIGHT / 2,
link_h
);
break;
case 2:
painter.drawImage(
PIXELS_PER_SLOT * 1.5 - LINKV_WIDTH / 2,
row * PIXELS_PER_SLOT - LINKV_HEIGHT / 2,
link_v
);
break;
case 4:
painter.drawImage(
PIXELS_PER_SLOT / 2 - LINKV_WIDTH / 2,
row * PIXELS_PER_SLOT - LINKV_HEIGHT / 2,
link_v
);
break;
default:
QLOG_ERROR() << "No idea how to draw link for socket " << i;
break;
}
}
}
prev = socket;
++i;
}
}
return pixmap.copy(0, 0, PIXELS_PER_SLOT * socket_columns,
PIXELS_PER_SLOT * socket_rows);
}
QPixmap GenerateItemIcon(const Item &item, const QImage &image) {
const int height = item.h();
const int width = item.w();
QPixmap layered(image.width(), image.height());
layered.fill(Qt::transparent);
QPainter layered_painter(&layered);
if (item.shaper() || item.elder()){
// Assumes width <= 2
const QImage *background_image = nullptr;
if (item.shaper()) {
switch(height) {
case 1:
background_image = width == 1 ? &shaper_1x1 : &shaper_2x1;
break;
case 2:
background_image = width == 1 ? nullptr : &shaper_2x2;
break;
case 3:
background_image = width == 1 ? &shaper_1x3 : &shaper_2x3;
break;
case 4:
background_image = width == 1 ? &shaper_1x4 : &shaper_2x4;
break;
default:
break;
}
} else { // Elder
switch(height) {
case 1:
background_image = width == 1 ? &elder_1x1 : &elder_2x1;
break;
case 2:
background_image = width == 1 ? nullptr : &elder_2x2;
break;
case 3:
background_image = width == 1 ? &elder_1x3 : &elder_2x3;
break;
case 4:
background_image = width == 1 ? &elder_1x4 : &elder_2x4;
break;
default:
break;
}
}
if (background_image) {
layered_painter.drawImage(0, 0, *background_image);
} else {
QLOG_ERROR() << "Problem drawing background for " << item.PrettyName().c_str();
}
}
layered_painter.drawImage(0, 0, image);
if (item.text_sockets().size() > 0) {
QPixmap sockets = GenerateItemSockets(width, height, item.text_sockets());
layered_painter.drawPixmap((int)(0.5*(image.width() - sockets.width())),
(int)(0.5*(image.height() - sockets.height())), sockets); // Center sockets on overall image
}
return layered;
}