15#include "absl/strings/str_cat.h"
16#include "absl/strings/substitute.h"
21#include "extras/IconsFontAwesome6.h"
29constexpr char kPayload[] =
"ITEM";
31constexpr float kBagSlot = 52.0f;
32constexpr int kColumns = 6;
34constexpr uint32_t kUnknownIcon = 0xF059;
46std::optional<Dragged> Held() {
47 const ImGuiPayload* payload = ImGui::GetDragDropPayload();
48 if (payload ==
nullptr || !payload->IsDataType(kPayload)) {
52 std::memcpy(&dragged, payload->Data,
sizeof(dragged));
56protocol::Place PlaceOf(
const Dragged& dragged) {
57 return dragged.worn ?
Worn(
static_cast<items::Slot
>(dragged.slot))
58 :
InBag(dragged.slot);
61uint32_t Key(
const protocol::Place& place) {
62 return place.has_bag() ? place.bag() :
static_cast<uint32_t
>(place.worn());
65bool SamePlace(
const protocol::Place& a,
const protocol::Place& b) {
66 return a.where_case() == b.where_case() && Key(a) == Key(b);
69const items::Stack* StackAt(
const protocol::Inventory& bag,
70 const protocol::Place& place) {
71 const auto& slots = place.has_bag() ? bag.bag() : bag.worn();
72 const auto found = slots.find(Key(place));
73 return found != slots.end() ? &found->second :
nullptr;
76const items::Item* Find(
const ItemTable& items, uint32_t
id) {
77 const auto found = items.by_id.find(
id);
78 return found != items.by_id.end() ? &found->second :
nullptr;
81const char* StatName(items::Stat stat) {
83 case items::STAT_DAMAGE:
85 case items::STAT_HEALTH:
87 case items::STAT_ARMOUR:
94std::string Added(
const items::Modifier& modifier) {
95 return absl::StrCat(modifier.amount() >= 0 ?
"+" :
"", modifier.amount(),
" ",
96 StatName(modifier.stat()));
100uint32_t SlotIcon(items::Slot slot) {
102 case items::SLOT_MAIN_HAND:
104 case items::SLOT_OFF_HAND:
106 case items::SLOT_HEAD:
108 case items::SLOT_BODY:
110 case items::SLOT_LEGS:
112 case items::SLOT_FEET:
120ImU32 TintOf(
const items::Item* item) {
121 if (item ==
nullptr || !item->look().has_tint()) {
124 const uint32_t rgba = item->look().tint();
125 return IM_COL32((rgba >> 24) & 0xFF, (rgba >> 16) & 0xFF, (rgba >> 8) & 0xFF,
129protocol::ItemAction MoveTo(
const protocol::Place& from,
130 const protocol::Place& to, uint32_t count = 0) {
131 protocol::ItemAction action;
132 *action.mutable_move()->mutable_from() = from;
133 *action.mutable_move()->mutable_to() = to;
134 action.mutable_move()->set_count(count);
140bool Fits(
const ItemTable& items,
const protocol::Inventory& bag,
141 const protocol::Place& from,
const protocol::Place& to) {
143 return copy.Move(items, from, to).ok();
149 const items::Stack& stack) {
150 const items::Item* item = Find(items, stack.item());
151 if (item ==
nullptr) {
152 return {
Tr(
"An item this client doesn't know")};
154 std::vector<std::string> lines = {
NameOf(*item)};
155 if (item->has_equip()) {
157 absl::Substitute(
Tr(
"Worn: $0"),
NameOf(item->equip().slot())));
159 for (
const items::Modifier& modifier : item->equip().modifiers()) {
160 lines.push_back(Added(modifier));
162 for (
const items::Modifier& roll : stack.rolls()) {
163 lines.push_back(absl::Substitute(
Tr(
"$0, rolled"), Added(roll)));
165 if (item->has_use()) {
166 const items::Use& use = item->use();
167 lines.push_back(use.cooldown_ms() > 0
168 ? absl::Substitute(
Tr(
"Use: heals $0, one every $1 s"),
169 use.heal(), use.cooldown_ms() / 1000)
170 : absl::Substitute(
Tr(
"Use: heals $0"), use.heal()));
173 lines.push_back(absl::Substitute(
Tr(
"Stacks up to $0"),
StackSize(*item)));
179 for (uint32_t slot = 0; slot <
kBagSlots; ++slot) {
180 if (!bag.bag().contains(slot)) {
188 const protocol::Inventory& bag,
189 const protocol::Place& place) {
190 const items::Stack* stack = StackAt(bag, place);
191 const items::Item* item =
192 stack !=
nullptr ? Find(items, stack->item()) :
nullptr;
193 if (item ==
nullptr) {
196 if (place.has_worn()) {
198 return MoveTo(place,
InBag(*empty));
202 if (item->has_use()) {
203 protocol::ItemAction action;
204 action.set_use(place.bag());
207 if (item->has_equip()) {
208 return MoveTo(place,
Worn(item->equip().slot()));
214 const protocol::Inventory& bag,
215 const protocol::Place& place,
216 float size,
bool locked) {
217 const items::Stack* stack = StackAt(bag, place);
218 const items::Item* item =
219 stack !=
nullptr ? Find(items, stack->item()) :
nullptr;
220 std::optional<protocol::ItemAction> action;
221 ImGui::PushID(place.has_bag() ?
static_cast<int>(place.bag())
222 : 100 +
static_cast<int>(place.worn()));
224 ImGui::InvisibleButton(
"##slot", {size, size});
225 const bool hovered = ImGui::IsItemHovered();
226 const ImVec2 min = ImGui::GetItemRectMin();
227 const ImVec2 max = ImGui::GetItemRectMax();
231 if (stack !=
nullptr && !locked &&
232 ImGui::BeginDragDropSource(ImGuiDragDropFlags_SourceNoPreviewTooltip)) {
233 const ImVec2 down = ImGui::GetIO().MouseClickedPos[ImGuiMouseButton_Left];
234 const Dragged dragged{.worn = place.has_worn(),
236 .grab = {down.x - min.x, down.y - min.y}};
237 ImGui::SetDragDropPayload(kPayload, &dragged,
sizeof(dragged),
239 ImGui::EndDragDropSource();
241 const std::optional<Dragged> held = Held();
242 const bool source = held && SamePlace(PlaceOf(*held), place);
247 if (held && !source && ImGui::BeginDragDropTarget()) {
248 const protocol::Place from = PlaceOf(*held);
249 const bool fits = !locked && Fits(items, bag, from, place);
251 if (
const ImGuiPayload* payload = ImGui::AcceptDragDropPayload(
252 kPayload, ImGuiDragDropFlags_AcceptBeforeDelivery |
253 ImGuiDragDropFlags_AcceptNoDrawDefaultRect);
254 payload !=
nullptr && payload->IsDelivery() && fits) {
256 const items::Stack* moving = StackAt(bag, from);
257 const uint32_t count =
258 ImGui::GetIO().KeyShift && moving !=
nullptr && moving->count() > 1
259 ? moving->count() / 2
261 action = MoveTo(from, place, count);
263 ImGui::EndDragDropTarget();
264 }
else if (hovered && !held) {
268 DrawSlot(ImGui::GetWindowDrawList(), min, max, edge);
269 ImDrawList* draw = ImGui::GetWindowDrawList();
272 if (held && !source && edge != 0) {
273 const float out =
Ui(3.0f);
274 draw->AddRect({min.x - out, min.y - out}, {max.x + out, max.y + out}, edge,
275 Ui(2.0f), 0,
Ui(2.0f));
277 if (stack !=
nullptr) {
278 const ImU32 tint = TintOf(item);
279 const std::string icon =
280 Icon(item !=
nullptr ? item->look().icon() : kUnknownIcon);
283 const float alpha = source ? 0.25f : locked ? 0.45f : 1.0f;
284 DrawSlotIcon(draw, min, max, icon.c_str(), tint, alpha);
285 if (stack->count() > 1 && !source) {
286 const std::string count = absl::StrCat(stack->count());
288 const float count_size =
Ui(14.0f);
289 const ImVec2 extent =
290 bold->CalcTextSizeA(count_size, FLT_MAX, 0.0f, count.c_str());
292 draw, bold, count_size,
293 {max.x - extent.x -
Ui(4.0f), max.y - extent.y -
Ui(2.0f)},
299 }
else if (place.has_worn()) {
305 if (stack !=
nullptr && !locked && hovered &&
306 ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) {
309 if (hovered && !held && !ImGui::IsPopupOpen(
"menu")) {
310 ImGui::BeginTooltip();
311 if (stack ==
nullptr) {
313 "%s", place.has_worn() ?
NameOf(place.worn()) :
Tr(
"Empty"));
315 const std::vector<std::string> lines =
ItemLines(items, *stack);
316 ImGui::PushFont(
GetFonts().title, 15.0f);
318 "%s", lines.front().c_str());
320 for (std::size_t i = 1; i < lines.size(); ++i) {
321 ImGui::TextDisabled(
"%s", lines[i].c_str());
324 ImGui::TextDisabled(
"%s",
Tr(
"Offered in the trade"));
330 if (stack !=
nullptr && !locked && ImGui::BeginPopupContextItem(
"menu")) {
331 if (place.has_bag() && item !=
nullptr && item->has_use() &&
332 ImGui::MenuItem(
TrLabel(
"Use"))) {
333 action.emplace().set_use(place.bag());
335 if (place.has_bag() && item !=
nullptr && item->has_equip() &&
336 ImGui::MenuItem(
TrLabel(
"Wear"))) {
337 action = MoveTo(place,
Worn(item->equip().slot()));
341 if (place.has_worn() &&
342 ImGui::MenuItem(
TrLabel(
"Take off"),
nullptr,
false,
343 empty.has_value()) &&
345 action = MoveTo(place,
InBag(*empty));
347 if (ImGui::MenuItem(
TrLabel(
"Throw away"))) {
348 *action.emplace().mutable_destroy() = place;
358 const bool clicked = ImGui::InvisibleButton(
"##stack", {size, size});
359 const ImVec2 min = ImGui::GetItemRectMin();
360 const ImVec2 max = ImGui::GetItemRectMax();
361 ImDrawList* draw = ImGui::GetWindowDrawList();
363 if (stack ==
nullptr) {
366 const items::Item* item = Find(items, stack->item());
369 Icon(item !=
nullptr ? item->look().icon() : kUnknownIcon).c_str(),
371 if (stack->count() > 1) {
372 const std::string count = absl::StrCat(stack->count());
374 const float count_size =
Ui(14.0f);
375 const ImVec2 extent =
376 bold->CalcTextSizeA(count_size, FLT_MAX, 0.0f, count.c_str());
378 {max.x - extent.x -
Ui(4.0f), max.y - extent.y -
Ui(2.0f)},
381 if (ImGui::IsItemHovered()) {
382 const std::vector<std::string> lines =
ItemLines(items, *stack);
383 ImGui::BeginTooltip();
384 ImGui::PushFont(
GetFonts().title, 15.0f);
386 lines.front().c_str());
388 for (std::size_t i = 1; i < lines.size(); ++i) {
389 ImGui::TextDisabled(
"%s", lines[i].c_str());
397 const ImGuiPayload* payload = ImGui::AcceptDragDropPayload(kPayload);
398 if (payload ==
nullptr) {
402 std::memcpy(&dragged, payload->Data,
sizeof(dragged));
403 return PlaceOf(dragged);
407 const protocol::Inventory& bag,
const protocol::TradeSide& offered) {
408 std::optional<protocol::ItemAction> action;
411 if (
const std::optional<Dragged> held = Held();
412 held && ImGui::IsMouseReleased(ImGuiMouseButton_Left) &&
413 !ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow |
414 ImGuiHoveredFlags_AllowWhenBlockedByActiveItem)) {
415 throwing_ = PlaceOf(*held);
418 if (std::optional<protocol::ItemAction> done = BuildThrowAway(bag)) {
419 action = std::move(done);
425 const ImVec2 display = ImGui::GetIO().DisplaySize;
426 ImGui::SetNextWindowPos({display.x -
Ui(18.0f), display.y * 0.55f},
427 ImGuiCond_FirstUseEver, {1.0f, 0.5f});
429 ImGuiWindowFlags_AlwaysAutoResize |
430 ImGuiWindowFlags_NoSavedSettings)) {
431 const float slot =
Ui(kBagSlot);
432 ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing,
Ui(ImVec2{5.0f, 5.0f}));
433 for (uint32_t index = 0; index <
kBagSlots; ++index) {
434 if (index % kColumns != 0) {
437 if (std::optional<protocol::ItemAction> done =
ItemSlot(
438 items_, bag,
InBag(index), slot, offered.bag().contains(index))) {
439 action = std::move(done);
442 ImGui::PopStyleVar();
445 ImGui::ColorConvertU32ToFloat4(
colors::kGold), ICON_FA_COINS
" %s",
446 absl::Substitute(
TrN(
"$0 gold",
"$0 gold", bag.gold()), bag.gold())
453std::optional<protocol::ItemAction> BagWindow::BuildThrowAway(
454 const protocol::Inventory& bag) {
458 const protocol::Place place = *throwing_;
459 const items::Stack* stack = StackAt(bag, place);
460 const items::Item* item =
461 stack !=
nullptr ? Find(items_, stack->item()) : nullptr;
462 if (stack ==
nullptr) {
466 const ImVec2 display = ImGui::GetIO().DisplaySize;
467 ImGui::SetNextWindowPos({display.x / 2.0f, display.y * 0.42f},
468 ImGuiCond_Always, {0.5f, 0.5f});
470 std::optional<protocol::ItemAction> action;
472 ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove |
473 ImGuiWindowFlags_NoSavedSettings);
474 const char* name = item !=
nullptr ?
NameOf(*item) :
Tr(
"this item");
475 ImGui::TextUnformatted(
477 TrN(
"Throw away the $1, for good?",
478 "Throw away all $0 of the $1, for good?",
CountOf(*stack)),
482 if (
GameButton(
"Throw away", {
Ui(150.0f), 0.0f},
true)) {
483 *action.emplace().mutable_destroy() = place;
488 ImGui::IsKeyPressed(ImGuiKey_Escape)) {
std::optional< protocol::ItemAction > Build(const protocol::Inventory &bag, const protocol::TradeSide &offered={})
What's offered in a trade stays in its slot until the trade's over.
constexpr ImU32 kGoldLight
bool BeginPanel(const char *name, bool *open, ImGuiWindowFlags flags, float alpha)
uint32_t CountOf(const items::Stack &stack)
How many are in the stack, which is one when it doesn't say.
const char * Tr(const char *text)
std::string Icon(uint32_t code)
const char * TrN(const char *one, const char *many, uint64_t n)
The form for the count: in English, one for 1 and many for the rest.
const char * TrLabel(const char *label)
void DrawSlotIcon(ImDrawList *draw, ImVec2 min, ImVec2 max, const char *icon, ImU32 color, float alpha)
An icon in the middle of a slot, in its colour, on a glow of that colour.
constexpr uint32_t kBagSlots
How many slots each player's bag has.
std::optional< uint32_t > FirstEmptySlot(const protocol::Inventory &bag)
The first slot of the bag with nothing in it, if it has one.
uint32_t StackSize(const items::Item &item)
How many of the item go in one slot of a bag.
protocol::Place Worn(items::Slot slot)
A slot that's worn.
protocol::Place InBag(uint32_t slot)
A place in the bag, by its slot from zero.
std::optional< protocol::ItemAction > QuickAction(const ItemTable &items, const protocol::Inventory &bag, const protocol::Place &place)
std::optional< protocol::Place > AcceptItem()
std::optional< protocol::ItemAction > ItemSlot(const ItemTable &items, const protocol::Inventory &bag, const protocol::Place &place, float size, bool locked)
bool StackSlot(const ItemTable &items, const items::Stack *stack, float size, uint32_t edge)
void DrawDragged(const char *icon, ImU32 color, float size, ImVec2 grab)
bool GameButton(const char *label, ImVec2 size, bool primary, bool enabled)
void DrawShadowedText(ImDrawList *draw, ImFont *font, float size, ImVec2 at, ImU32 color, const char *text)
Text with a shadow under it.
void DrawSlot(ImDrawList *draw, ImVec2 min, ImVec2 max, ImU32 edge)
const char * NameOf(const items::Item &item)
std::vector< std::string > ItemLines(const ItemTable &items, const items::Stack &stack)
The items in the items file, by id.