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
|
#include "StarActionBar.hpp"
#include "StarJsonExtra.hpp"
#include "StarRoot.hpp"
#include "StarGuiReader.hpp"
#include "StarItemTooltip.hpp"
#include "StarUniverseClient.hpp"
#include "StarItemGridWidget.hpp"
#include "StarItemSlotWidget.hpp"
#include "StarButtonWidget.hpp"
#include "StarImageWidget.hpp"
#include "StarPaneManager.hpp"
#include "StarPlayer.hpp"
#include "StarPlayerInventory.hpp"
#include "StarAssets.hpp"
#include "StarImageMetadataDatabase.hpp"
#include "StarItem.hpp"
#include "StarMerchantInterface.hpp"
namespace Star {
ActionBar::ActionBar(MainInterfacePaneManager* paneManager, PlayerPtr player) {
m_paneManager = paneManager;
m_player = std::move(player);
auto assets = Root::singleton().assets();
m_config = assets->json("/interface/windowconfig/actionbar.config");
m_actionBarSelectOffset = jsonToVec2I(m_config.get("actionBarSelectOffset"));
m_switchSounds = jsonToStringList(m_config.get("sounds").get("switch"));
GuiReader reader;
for (uint8_t i = 0; i < m_player->inventory()->customBarIndexes(); ++i) {
reader.registerCallback(strf("customBar{}L", i + 1), bind(&ActionBar::customBarClick, this, i, true));
reader.registerCallback(strf("customBar{}R", i + 1), bind(&ActionBar::customBarClick, this, i, false));
reader.registerCallback(strf("customBar{}L.right", i + 1), bind(&ActionBar::customBarClickRight, this, i, true));
reader.registerCallback(strf("customBar{}R.right", i + 1), bind(&ActionBar::customBarClickRight, this, i, false));
}
for (uint8_t i = 0; i < EssentialItemCount; ++i)
reader.registerCallback(strf("essentialBar{}", i + 1), bind(&ActionBar::essentialBarClick, this, i));
reader.registerCallback("pickupToActionBar", [=](Widget* widget) {
auto button = as<ButtonWidget>(widget);
Root::singleton().configuration()->setPath("inventory.pickupToActionBar", button->isChecked());
});
reader.registerCallback("swapCustomBar", [this](Widget*) {
swapCustomBar();
});
auto configuration = Root::singleton().configuration();
bool bottomBar = configuration->getPath("inventory.bottomActionBar").optBool().value(false);
if (bottomBar)
m_config = jsonMerge(m_config, assets->json("/interface/windowconfig/actionbarbottom.config"));
reader.construct(m_config.get("paneLayout"), this);
if (bottomBar) {
setAnchor(PaneAnchor::CenterBottom);
m_anchorOffset[1] *= -1;
if (!m_bgBody.empty())
m_bgBody += "?flipy";
if (!m_bgHeader.empty())
m_bgHeader += "?flipy";
if (!m_bgFooter.empty())
m_bgFooter += "?flipy";
swap(m_bgHeader, m_bgFooter);
for (auto& child : m_members) {
auto position = child->relativePosition();
child->setPosition({position[0], m_bodySize[1] - position[1] - child->size()[1] + 1});
}
}
for (uint8_t i = 0; i < m_player->inventory()->customBarIndexes(); ++i) {
auto customBarLeft = fetchChild<ItemSlotWidget>(strf("customBar{}L", i + 1));
auto customBarRight = fetchChild<ItemSlotWidget>(strf("customBar{}R", i + 1));
auto customBarLeftOverlay = fetchChild<ImageWidget>(strf("customBar{}LOverlay", i + 1));
auto customBarRightOverlay = fetchChild<ImageWidget>(strf("customBar{}ROverlay", i + 1));
TextPositioning countPosition = {jsonToVec2F(m_config.get("countMidAnchor")), HorizontalAnchor::HMidAnchor};
customBarLeft->setCountPosition(countPosition);
customBarRight->setCountPosition(countPosition);
m_customBarWidgets.append({customBarLeft, customBarRight, customBarLeftOverlay, customBarRightOverlay});
}
m_customSelectedWidget = fetchChild<ImageWidget>("customSelect");
for (uint8_t i = 0; i < EssentialItemCount; ++i)
m_essentialBarWidgets.append(fetchChild<ItemSlotWidget>(strf("essentialBar{}", i + 1)));
m_essentialSelectedWidget = fetchChild<ImageWidget>("essentialSelect");
}
PanePtr ActionBar::createTooltip(Vec2I const& screenPosition) {
ItemPtr item;
auto tryItemWidget = [&](ItemSlotWidgetPtr const& isw) {
if (isw->screenBoundRect().contains(screenPosition))
item = isw->item();
};
for (auto const& p : m_customBarWidgets) {
tryItemWidget(p.left);
tryItemWidget(p.right);
}
for (auto const& w : m_essentialBarWidgets)
tryItemWidget(w);
if (!item)
return {};
return ItemTooltipBuilder::buildItemTooltip(item, m_player);
}
bool ActionBar::sendEvent(InputEvent const& event) {
if (Pane::sendEvent(event))
return true;
auto inventory = m_player->inventory();
auto customBarIndexes = inventory->customBarIndexes();
if (auto mouseWheel = event.ptr<MouseWheelEvent>()) {
auto abl = inventory->selectedActionBarLocation();
int index = 0;
if (!abl) {
if (mouseWheel->mouseWheel == MouseWheel::Down)
index = 0;
else
index = customBarIndexes + EssentialItemCount - 1;
} else {
if (auto cbi = abl.ptr<CustomBarIndex>()) {
if (*cbi < customBarIndexes / 2)
index = *cbi;
else
index = *cbi + EssentialItemCount;
} else {
index = customBarIndexes / 2 + (int)abl.get<EssentialItem>();
}
if (mouseWheel->mouseWheel == MouseWheel::Down)
index = pmod(index + 1, customBarIndexes + EssentialItemCount);
else
index = pmod(index - 1, customBarIndexes + EssentialItemCount);
}
if (index < customBarIndexes / 2)
abl = (CustomBarIndex)index;
else if (index < customBarIndexes / 2 + EssentialItemCount)
abl = (EssentialItem)(index - customBarIndexes / 2);
else
abl = (CustomBarIndex)(index - EssentialItemCount);
inventory->selectActionBarLocation(abl);
context()->playAudio(RandomSource().randFrom(m_switchSounds));
return true;
}
if (event.is<MouseMoveEvent>()) {
m_customBarHover.reset();
Vec2I screenPosition = *GuiContext::singleton().mousePosition(event);
for (uint8_t i = 0; i < customBarIndexes; ++i) {
if (m_customBarWidgets[i].left->screenBoundRect().contains(screenPosition))
m_customBarHover = make_pair((CustomBarIndex)i, false);
else if (m_customBarWidgets[i].right->screenBoundRect().contains(screenPosition))
m_customBarHover = make_pair((CustomBarIndex)i, true);
}
}
for (auto action : context()->actions(event)) {
if (action >= InterfaceAction::InterfaceBar1 && action <= InterfaceAction::InterfaceBar10
&& ((int)action - (int)InterfaceAction::InterfaceBar1) < inventory->customBarIndexes())
inventory->selectActionBarLocation((CustomBarIndex)((int)action - (int)InterfaceAction::InterfaceBar1));
if (action >= InterfaceAction::EssentialBar1 && action <= InterfaceAction::EssentialBar4)
inventory->selectActionBarLocation((EssentialItem)((int)action - (int)InterfaceAction::EssentialBar1));
if (action == InterfaceAction::InterfaceDeselectHands) {
if (auto previousSelectedLocation = inventory->selectedActionBarLocation()) {
m_emptyHandsPreviousActionBarLocation = inventory->selectedActionBarLocation();
inventory->selectActionBarLocation({});
} else {
inventory->selectActionBarLocation(take(m_emptyHandsPreviousActionBarLocation));
}
}
if (action == InterfaceAction::InterfaceChangeBarGroup)
swapCustomBar();
}
return false;
}
void ActionBar::update(float) {
auto inventory = m_player->inventory();
auto abl = inventory->selectedActionBarLocation();
if (abl.is<CustomBarIndex>()) {
auto overlayLoc = m_customBarWidgets.at(abl.get<CustomBarIndex>()).left->position();
m_customSelectedWidget->setPosition(overlayLoc + m_actionBarSelectOffset);
m_customSelectedWidget->show();
m_essentialSelectedWidget->hide();
} else if (abl.is<EssentialItem>()) {
auto overlayLoc = m_essentialBarWidgets.at((size_t)abl.get<EssentialItem>())->position();
m_essentialSelectedWidget->setPosition(overlayLoc + m_actionBarSelectOffset);
m_essentialSelectedWidget->show();
m_customSelectedWidget->hide();
} else {
m_essentialSelectedWidget->hide();
m_customSelectedWidget->hide();
}
for (uint8_t i = 0; i < EssentialItemCount; ++i)
m_essentialBarWidgets[i]->setItem(inventory->essentialItem((EssentialItem)i));
for (uint8_t i = 0; i < inventory->customBarIndexes(); ++i) {
// If there is no swap slot item being hovered over the custom bar, then
// simply set the left and right item widgets in the custom bar to the
// primary and secondary items. If the primary item is two handed, the
// secondary item will be null and the primary hand item is drawn dimmed in
// the right hand slot. If there IS a swap slot item being hovered over
// this spot in the custom bar, things are more complex. Instead of
// showing what is currently in the custom bar, a preview of what WOULD
// happen when linking is shown, except both the left and right item
// widgets are always shown with no count and always dimmed to indicate
// that it is just a preview.
ItemPtr primaryItem;
ItemPtr secondaryItem;
if (auto slot = inventory->customBarPrimarySlot(i))
primaryItem = inventory->itemsAt(*slot);
if (auto slot = inventory->customBarSecondarySlot(i))
secondaryItem = inventory->itemsAt(*slot);
bool primaryPreview = false;
bool secondaryPreview = false;
ItemPtr swapSlotItem = inventory->swapSlotItem();
if (swapSlotItem && m_customBarHover && m_customBarHover->first == i) {
if (!m_customBarHover->second || swapSlotItem->twoHanded()) {
if (!primaryItem && swapSlotItem == secondaryItem)
secondaryItem = {};
primaryItem = swapSlotItem;
primaryPreview = true;
} else {
if (itemSafeTwoHanded(primaryItem))
primaryItem = {};
if (!secondaryItem && swapSlotItem == primaryItem)
primaryItem = {};
secondaryItem = swapSlotItem;
secondaryPreview = true;
}
}
auto& widgets = m_customBarWidgets[i];
widgets.left->setItem(primaryItem);
if (primaryPreview) {
widgets.left->showDurability(false);
widgets.left->showCount(false);
widgets.leftOverlay->show();
} else {
widgets.left->showDurability(true);
widgets.left->showCount(true);
widgets.leftOverlay->hide();
}
if (itemSafeTwoHanded(primaryItem)) {
widgets.right->setItem(primaryItem);
widgets.right->showDurability(false);
widgets.right->showCount(false);
widgets.rightOverlay->show();
} else {
widgets.right->setItem(secondaryItem);
if (secondaryPreview) {
widgets.right->showDurability(false);
widgets.right->showCount(false);
widgets.rightOverlay->show();
} else {
widgets.right->showDurability(true);
widgets.right->showCount(true);
widgets.rightOverlay->hide();
}
}
widgets.left->setHighlightEnabled(!widgets.left->item() && swapSlotItem);
widgets.right->setHighlightEnabled(!widgets.right->item() && swapSlotItem);
}
fetchChild<ButtonWidget>("pickupToActionBar")->setChecked(Root::singleton().configuration()->getPath("inventory.pickupToActionBar").toBool());
fetchChild<ButtonWidget>("swapCustomBar")->setChecked(m_player->inventory()->customBarGroup() != 0);
}
Maybe<String> ActionBar::cursorOverride(Vec2I const&) {
if (m_customBarHover && m_player->inventory()->swapSlotItem())
return m_config.getString("linkCursor");
return {};
}
void ActionBar::customBarClick(uint8_t index, bool primary) {
if (auto swapItem = m_player->inventory()->swapSlotItem()) {
if (primary || itemSafeTwoHanded(swapItem))
m_player->inventory()->setCustomBarPrimarySlot(index, InventorySlot(SwapSlot()));
else
m_player->inventory()->setCustomBarSecondarySlot(index, InventorySlot(SwapSlot()));
m_player->inventory()->clearSwap();
} else {
m_player->inventory()->selectActionBarLocation(index);
}
}
void ActionBar::customBarClickRight(uint8_t index, bool primary) {
if (m_paneManager->registeredPaneIsDisplayed(MainInterfacePanes::Inventory)) {
auto inventory = m_player->inventory();
auto primarySlot = inventory->customBarPrimarySlot(index);
auto secondarySlot = inventory->customBarSecondarySlot(index);
if (primary || (primarySlot && itemSafeTwoHanded(inventory->itemsAt(*primarySlot))))
inventory->setCustomBarPrimarySlot(index, {});
else
inventory->setCustomBarSecondarySlot(index, {});
}
}
void ActionBar::essentialBarClick(uint8_t index) {
m_player->inventory()->selectActionBarLocation((EssentialItem)index);
}
void ActionBar::swapCustomBar() {
m_player->inventory()->setCustomBarGroup((m_player->inventory()->customBarGroup() + 1) % m_player->inventory()->customBarGroups());
}
}
|