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
|
#include "StarItemSlotWidget.hpp"
#include "StarRoot.hpp"
#include "StarJsonExtra.hpp"
#include "StarWidgetParsing.hpp"
#include "StarImageMetadataDatabase.hpp"
#include "StarItem.hpp"
#include "StarDurabilityItem.hpp"
#include "StarAssets.hpp"
#include "StarGameTypes.hpp"
namespace Star {
static String formatShortSize(uint64_t n) {
if (n < 10000)
return toString(n);
uint64_t divisor = 1000ull;
char suffix = 'k';
if (n >= 1000000000000000000ull) {
divisor = 1000000000000000000ull;
suffix = 'Q';
} else if (n >= 1000000000000000ull) {
divisor = 1000000000000000ull;
suffix = 'q';
} else if (n >= 1000000000000ull) {
divisor = 1000000000000ull;
suffix = 't';
} else if (n >= 1000000000ull) {
divisor = 1000000000ull;
suffix = 'b';
} else if (n >= 1000000ull) {
divisor = 1000000ull;
suffix = 'm';
}
uint64_t whole = n / divisor;
if (whole >= 100ull)
return strf("{}{:c}", whole, suffix);
uint64_t remainder = n - (whole * divisor);
uint64_t frac = (remainder * 1000) / divisor;
if (frac == 0)
return strf("{}{:c}", whole, suffix);
else if (whole >= 10)
return strf("{}.{}{:c}", whole, frac / 100, suffix);
else
return strf("{}.{:02d}{:c}", whole, frac / 10, suffix);
}
ItemSlotWidget::ItemSlotWidget(ItemPtr const& item, String const& backingImage)
: m_item(item), m_backingImage(backingImage) {
m_drawBackingImageWhenFull = false;
m_drawBackingImageWhenEmpty = true;
m_progress = 1;
auto assets = Root::singleton().assets();
auto interfaceConfig = assets->json("/interface.config");
m_countPosition = TextPositioning(jsonToVec2F(interfaceConfig.get("itemCountRightAnchor")), HorizontalAnchor::RightAnchor);
m_countFontMode = FontMode::Normal;
m_textStyle = interfaceConfig.get("itemSlotTextStyle");
m_itemDraggableArea = jsonToRectI(interfaceConfig.get("itemDraggableArea"));
m_durabilityOffset = jsonToVec2I(interfaceConfig.get("itemIconDurabilityOffset"));
auto newItemIndicatorConfig = interfaceConfig.get("newItemAnimation");
m_newItemIndicator = Animation(newItemIndicatorConfig);
// End animation before it begins, only display when triggered
m_newItemIndicator.update(newItemIndicatorConfig.getDouble("animationCycle") * newItemIndicatorConfig.getDouble("loops", 1.0f));
Json highlightAnimationConfig = interfaceConfig.get("highlightAnimation");
m_highlightAnimation = Animation(highlightAnimationConfig);
m_highlightEnabled = false;
Vec2I backingImageSize;
if (m_backingImage.size()) {
auto imgMetadata = Root::singleton().imageMetadataDatabase();
backingImageSize = Vec2I(imgMetadata->imageSize(m_backingImage));
}
setSize(m_itemDraggableArea.max().piecewiseMax(backingImageSize));
WidgetParser parser;
parser.construct(assets->json("/interface/itemSlot.config").get("config"), this);
m_durabilityBar = fetchChild<ProgressWidget>("durabilityBar");
m_durabilityBar->hide();
m_showDurability = false;
m_showCount = true;
m_showRarity = true;
m_showLinkIndicator = false;
disableScissoring();
}
void ItemSlotWidget::update(float dt) {
if (m_item)
m_newItemIndicator.update(dt);
if (m_highlightEnabled)
m_highlightAnimation.update(dt);
Widget::update(dt);
}
bool ItemSlotWidget::sendEvent(InputEvent const& event) {
if (m_visible) {
if (auto mouseButton = event.ptr<MouseButtonDownEvent>()) {
if (mouseButton->mouseButton == MouseButton::Left
|| (m_rightClickCallback && mouseButton->mouseButton == MouseButton::Right)
|| (m_middleClickCallback && mouseButton->mouseButton == MouseButton::Middle)) {
Vec2I mousePos = *context()->mousePosition(event);
RectI itemArea = m_itemDraggableArea.translated(screenPosition());
if (itemArea.contains(mousePos)) {
if (mouseButton->mouseButton == MouseButton::Right)
m_rightClickCallback(this);
else if (mouseButton->mouseButton == MouseButton::Middle)
m_middleClickCallback(this);
else
m_callback(this);
return true;
}
}
}
}
return false;
}
void ItemSlotWidget::setCallback(WidgetCallbackFunc callback) {
m_callback = callback;
}
void ItemSlotWidget::setRightClickCallback(WidgetCallbackFunc callback) {
m_rightClickCallback = callback;
}
void ItemSlotWidget::setMiddleClickCallback(WidgetCallbackFunc callback) {
m_middleClickCallback = callback;
}
void ItemSlotWidget::setItem(ItemPtr const& item) {
m_item = item;
}
ItemPtr ItemSlotWidget::item() const {
return m_item;
}
void ItemSlotWidget::setProgress(float progress) {
m_progress = progress;
}
void ItemSlotWidget::setBackingImageAffinity(bool full, bool empty) {
m_drawBackingImageWhenFull = full;
m_drawBackingImageWhenEmpty = empty;
}
void ItemSlotWidget::setCountPosition(TextPositioning textPositioning) {
m_countPosition = textPositioning;
}
void ItemSlotWidget::setCountFontMode(FontMode fontMode) {
m_countFontMode = fontMode;
}
void ItemSlotWidget::showDurability(bool show) {
m_showDurability = show;
}
void ItemSlotWidget::showCount(bool show) {
m_showCount = show;
}
void ItemSlotWidget::showRarity(bool showRarity) {
m_showRarity = showRarity;
}
void ItemSlotWidget::showLinkIndicator(bool showLinkIndicator) {
m_showLinkIndicator = showLinkIndicator;
}
void ItemSlotWidget::indicateNew() {
m_newItemIndicator.reset();
}
void ItemSlotWidget::setHighlightEnabled(bool highlight) {
if (!m_highlightEnabled && highlight)
m_highlightAnimation.reset();
m_highlightEnabled = highlight;
}
void ItemSlotWidget::renderImpl() {
auto drawCooldown = [this]() {
int frame = (int)roundf(m_progress * 18);// TODO: Hardcoded lol
context()->drawInterfaceQuad(String(strf("/interface/cooldown.png:{}", frame)), Vec2F(screenPosition()));
};
if (m_item) {
if (m_drawBackingImageWhenFull && m_backingImage != "")
context()->drawInterfaceQuad(m_backingImage, Vec2F(screenPosition()));
List<Drawable> iconDrawables = m_item->iconDrawables();
if (m_showRarity) {
String border = rarityBorder(m_item->rarity());
context()->drawInterfaceQuad(border, Vec2F(screenPosition()));
}
if (m_showLinkIndicator) {
// TODO: Hardcoded
context()->drawInterfaceQuad(String("/interface/inventory/itemlinkindicator.png"), Vec2F(screenPosition() - Vec2I(1, 1)));
}
for (auto i : iconDrawables)
context()->drawInterfaceDrawable(i, Vec2F(screenPosition() + size() / 2));
if (!m_newItemIndicator.isComplete())
context()->drawInterfaceDrawable(m_newItemIndicator.drawable(1.0), Vec2F(screenPosition() + size() / 2), Color::White.toRgba());
if (m_showDurability) {
if (auto durabilityItem = as<DurabilityItem>(m_item)) {
float amount = durabilityItem->durabilityStatus();
m_durabilityBar->setCurrentProgressLevel(amount);
if (amount < 1)
m_durabilityBar->show();
else
m_durabilityBar->hide();
} else {
m_durabilityBar->hide();
}
}
drawCooldown();
if (m_item->count() > 1 && m_showCount) {// we don't need to tell people that there's only 1 of something
context()->setTextStyle(m_textStyle);
context()->setFontMode(m_countFontMode);
context()->renderInterfaceText(formatShortSize(m_item->count()), m_countPosition.translated(Vec2F(screenPosition())));
context()->clearTextStyle();
}
} else if (m_drawBackingImageWhenEmpty && m_backingImage != "") {
context()->drawInterfaceQuad(m_backingImage, Vec2F(screenPosition()));
drawCooldown();
}
if (m_highlightEnabled) {
context()->drawInterfaceDrawable(m_highlightAnimation.drawable(1.0), Vec2F(screenPosition() + size() / 2), Color::White.toRgba());
}
if (!m_item)
m_durabilityBar->hide();
}
}
|