/src/client/bag_window.cc
Line | Count | Source |
1 | | #include "client/bag_window.h" |
2 | | |
3 | | #include <imgui.h> |
4 | | |
5 | | #include <algorithm> |
6 | | #include <cfloat> |
7 | | #include <cstddef> |
8 | | #include <cstdint> |
9 | | #include <cstring> |
10 | | #include <optional> |
11 | | #include <string> |
12 | | #include <utility> |
13 | | #include <vector> |
14 | | |
15 | | #include "absl/strings/str_cat.h" |
16 | | #include "absl/strings/substitute.h" |
17 | | #include "client/icon.h" |
18 | | #include "client/names.h" |
19 | | #include "client/theme.h" |
20 | | #include "client/translations.h" |
21 | | #include "extras/IconsFontAwesome6.h" |
22 | | #include "protocol/places.h" |
23 | | #include "server/inventory.h" |
24 | | |
25 | | namespace nanommo { |
26 | | |
27 | | namespace { |
28 | | |
29 | | constexpr char kPayload[] = "ITEM"; |
30 | | // In points, at a scale of one. |
31 | | constexpr float kBagSlot = 52.0f; |
32 | | constexpr int kColumns = 6; |
33 | | // Font Awesome's circle-question, for an item this client doesn't have. |
34 | | constexpr uint32_t kUnknownIcon = 0xF059; |
35 | | |
36 | | // Where a dragged item comes from, and where in its slot the mouse took hold |
37 | | // of it. ImGui copies a payload byte for byte, so it's plain data, not the |
38 | | // Place message. |
39 | | struct Dragged { |
40 | | bool worn = false; |
41 | | uint32_t slot = 0; |
42 | | ImVec2 grab; |
43 | | }; |
44 | | |
45 | | // The item the player's holding, while they drag one. |
46 | 0 | std::optional<Dragged> Held() { |
47 | 0 | const ImGuiPayload* payload = ImGui::GetDragDropPayload(); |
48 | 0 | if (payload == nullptr || !payload->IsDataType(kPayload)) { |
49 | 0 | return std::nullopt; |
50 | 0 | } |
51 | 0 | Dragged dragged; |
52 | 0 | std::memcpy(&dragged, payload->Data, sizeof(dragged)); |
53 | 0 | return dragged; |
54 | 0 | } |
55 | | |
56 | 0 | protocol::Place PlaceOf(const Dragged& dragged) { |
57 | 0 | return dragged.worn ? Worn(static_cast<items::Slot>(dragged.slot)) |
58 | 0 | : InBag(dragged.slot); |
59 | 0 | } |
60 | | |
61 | 4 | uint32_t Key(const protocol::Place& place) { |
62 | 4 | return place.has_bag() ? place.bag()3 : static_cast<uint32_t>(place.worn())1 ; |
63 | 4 | } |
64 | | |
65 | 0 | bool SamePlace(const protocol::Place& a, const protocol::Place& b) { |
66 | 0 | return a.where_case() == b.where_case() && Key(a) == Key(b); |
67 | 0 | } |
68 | | |
69 | | const items::Stack* StackAt(const protocol::Inventory& bag, |
70 | 4 | const protocol::Place& place) { |
71 | 4 | const auto& slots = place.has_bag() ? bag.bag()3 : bag.worn()1 ; |
72 | 4 | const auto found = slots.find(Key(place)); |
73 | 4 | return found != slots.end() ? &found->second3 : nullptr1 ; |
74 | 4 | } |
75 | | |
76 | 7 | const items::Item* Find(const ItemTable& items, uint32_t id) { |
77 | 7 | const auto found = items.by_id.find(id); |
78 | 7 | return found != items.by_id.end() ? &found->second6 : nullptr1 ; |
79 | 7 | } |
80 | | |
81 | 3 | const char* StatName(items::Stat stat) { |
82 | 3 | switch (stat) { |
83 | 1 | case items::STAT_DAMAGE: |
84 | 1 | return Tr("damage"); |
85 | 1 | case items::STAT_HEALTH: |
86 | 1 | return Tr("health"); |
87 | 1 | case items::STAT_ARMOUR: |
88 | 1 | return Tr("armour"); |
89 | 0 | default: |
90 | 0 | return Tr("nothing"); |
91 | 3 | } |
92 | 3 | } |
93 | | |
94 | 3 | std::string Added(const items::Modifier& modifier) { |
95 | 3 | return absl::StrCat(modifier.amount() >= 0 ? "+" : ""0 , modifier.amount(), " ", |
96 | 3 | StatName(modifier.stat())); |
97 | 3 | } |
98 | | |
99 | | // An empty slot that's worn shows what goes in it, greyed out. |
100 | 0 | uint32_t SlotIcon(items::Slot slot) { |
101 | 0 | switch (slot) { |
102 | 0 | case items::SLOT_MAIN_HAND: |
103 | 0 | return 0xF6DE; // hand-fist |
104 | 0 | case items::SLOT_OFF_HAND: |
105 | 0 | return 0xF132; // shield |
106 | 0 | case items::SLOT_HEAD: |
107 | 0 | return 0xF8C0; // hat-cowboy |
108 | 0 | case items::SLOT_BODY: |
109 | 0 | return 0xF553; // shirt |
110 | 0 | case items::SLOT_LEGS: |
111 | 0 | return 0xF554; // person-walking |
112 | 0 | case items::SLOT_FEET: |
113 | 0 | return 0xF696; // socks |
114 | 0 | default: |
115 | 0 | return kUnknownIcon; |
116 | 0 | } |
117 | 0 | } |
118 | | |
119 | | // The item's icon's colour, from its tint, as 0xRRGGBBAA, or the paper's. |
120 | 0 | ImU32 TintOf(const items::Item* item) { |
121 | 0 | if (item == nullptr || !item->look().has_tint()) { |
122 | 0 | return colors::kPaper; |
123 | 0 | } |
124 | 0 | const uint32_t rgba = item->look().tint(); |
125 | 0 | return IM_COL32((rgba >> 24) & 0xFF, (rgba >> 16) & 0xFF, (rgba >> 8) & 0xFF, |
126 | 0 | rgba & 0xFF); |
127 | 0 | } |
128 | | |
129 | | protocol::ItemAction MoveTo(const protocol::Place& from, |
130 | 2 | const protocol::Place& to, uint32_t count = 0) { |
131 | 2 | protocol::ItemAction action; |
132 | 2 | *action.mutable_move()->mutable_from() = from; |
133 | 2 | *action.mutable_move()->mutable_to() = to; |
134 | 2 | action.mutable_move()->set_count(count); |
135 | 2 | return action; |
136 | 2 | } |
137 | | |
138 | | // Whether the server would move what's at from to to: the bag's own rules, |
139 | | // which it keeps, tried on a copy. |
140 | | bool Fits(const ItemTable& items, const protocol::Inventory& bag, |
141 | 0 | const protocol::Place& from, const protocol::Place& to) { |
142 | 0 | Inventory copy(bag); |
143 | 0 | return copy.Move(items, from, to).ok(); |
144 | 0 | } |
145 | | |
146 | | } // namespace |
147 | | |
148 | | std::vector<std::string> ItemLines(const ItemTable& items, |
149 | 4 | const items::Stack& stack) { |
150 | 4 | const items::Item* item = Find(items, stack.item()); |
151 | 4 | if (item == nullptr) { |
152 | 1 | return {Tr("An item this client doesn't know")}; |
153 | 1 | } |
154 | 3 | std::vector<std::string> lines = {NameOf(*item)}; |
155 | 3 | if (item->has_equip()) { |
156 | 2 | lines.push_back( |
157 | 2 | absl::Substitute(Tr("Worn: $0"), NameOf(item->equip().slot()))); |
158 | 2 | } |
159 | 3 | for (const items::Modifier& modifier : item->equip().modifiers()) { |
160 | 2 | lines.push_back(Added(modifier)); |
161 | 2 | } |
162 | 3 | for (const items::Modifier& roll : stack.rolls()) { |
163 | 1 | lines.push_back(absl::Substitute(Tr("$0, rolled"), Added(roll))); |
164 | 1 | } |
165 | 3 | if (item->has_use()) { |
166 | 1 | const items::Use& use = item->use(); |
167 | 1 | lines.push_back(use.cooldown_ms() > 0 |
168 | 1 | ? absl::Substitute(Tr("Use: heals $0, one every $1 s"), |
169 | 1 | use.heal(), use.cooldown_ms() / 1000) |
170 | 1 | : absl::Substitute(Tr("Use: heals $0"), use.heal())0 ); |
171 | 1 | } |
172 | 3 | if (StackSize(*item) > 1) { |
173 | 1 | lines.push_back(absl::Substitute(Tr("Stacks up to $0"), StackSize(*item))); |
174 | 1 | } |
175 | 3 | return lines; |
176 | 4 | } |
177 | | |
178 | 4 | std::optional<uint32_t> FirstEmptySlot(const protocol::Inventory& bag) { |
179 | 37 | for (uint32_t slot = 0; slot < kBagSlots; ++slot33 ) { |
180 | 36 | if (!bag.bag().contains(slot)) { |
181 | 3 | return slot; |
182 | 3 | } |
183 | 36 | } |
184 | 1 | return std::nullopt; |
185 | 4 | } |
186 | | |
187 | | std::optional<protocol::ItemAction> QuickAction(const ItemTable& items, |
188 | | const protocol::Inventory& bag, |
189 | 4 | const protocol::Place& place) { |
190 | 4 | const items::Stack* stack = StackAt(bag, place); |
191 | 4 | const items::Item* item = |
192 | 4 | stack != nullptr ? Find(items, stack->item())3 : nullptr1 ; |
193 | 4 | if (item == nullptr) { |
194 | 1 | return std::nullopt; |
195 | 1 | } |
196 | 3 | if (place.has_worn()) { |
197 | 1 | if (const std::optional<uint32_t> empty = FirstEmptySlot(bag)) { |
198 | 1 | return MoveTo(place, InBag(*empty)); |
199 | 1 | } |
200 | 0 | return std::nullopt; |
201 | 1 | } |
202 | 2 | if (item->has_use()) { |
203 | 1 | protocol::ItemAction action; |
204 | 1 | action.set_use(place.bag()); |
205 | 1 | return action; |
206 | 1 | } |
207 | 1 | if (item->has_equip()) { |
208 | 1 | return MoveTo(place, Worn(item->equip().slot())); |
209 | 1 | } |
210 | 0 | return std::nullopt; |
211 | 1 | } |
212 | | |
213 | | std::optional<protocol::ItemAction> ItemSlot(const ItemTable& items, |
214 | | const protocol::Inventory& bag, |
215 | | const protocol::Place& place, |
216 | 0 | float size, bool locked) { |
217 | 0 | const items::Stack* stack = StackAt(bag, place); |
218 | 0 | const items::Item* item = |
219 | 0 | stack != nullptr ? Find(items, stack->item()) : nullptr; |
220 | 0 | std::optional<protocol::ItemAction> action; |
221 | 0 | ImGui::PushID(place.has_bag() ? static_cast<int>(place.bag()) |
222 | 0 | : 100 + static_cast<int>(place.worn())); |
223 | |
|
224 | 0 | ImGui::InvisibleButton("##slot", {size, size}); |
225 | 0 | const bool hovered = ImGui::IsItemHovered(); |
226 | 0 | const ImVec2 min = ImGui::GetItemRectMin(); |
227 | 0 | const ImVec2 max = ImGui::GetItemRectMax(); |
228 | | |
229 | | // Taken hold of, it follows the mouse, from where it was held. Offered in |
230 | | // a trade, it stays, as the trade takes it from here. |
231 | 0 | if (stack != nullptr && !locked && |
232 | 0 | ImGui::BeginDragDropSource(ImGuiDragDropFlags_SourceNoPreviewTooltip)) { |
233 | 0 | const ImVec2 down = ImGui::GetIO().MouseClickedPos[ImGuiMouseButton_Left]; |
234 | 0 | const Dragged dragged{.worn = place.has_worn(), |
235 | 0 | .slot = Key(place), |
236 | 0 | .grab = {down.x - min.x, down.y - min.y}}; |
237 | 0 | ImGui::SetDragDropPayload(kPayload, &dragged, sizeof(dragged), |
238 | 0 | ImGuiCond_Once); |
239 | 0 | ImGui::EndDragDropSource(); |
240 | 0 | } |
241 | 0 | const std::optional<Dragged> held = Held(); |
242 | 0 | const bool source = held && SamePlace(PlaceOf(*held), place); |
243 | | |
244 | | // Lit green where what's held would go, by the server's own rules, and red |
245 | | // where it wouldn't. |
246 | 0 | ImU32 edge = 0; |
247 | 0 | if (held && !source && ImGui::BeginDragDropTarget()) { |
248 | 0 | const protocol::Place from = PlaceOf(*held); |
249 | 0 | const bool fits = !locked && Fits(items, bag, from, place); |
250 | 0 | edge = fits ? colors::kGood : colors::kHealth; |
251 | 0 | if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload( |
252 | 0 | kPayload, ImGuiDragDropFlags_AcceptBeforeDelivery | |
253 | 0 | ImGuiDragDropFlags_AcceptNoDrawDefaultRect); |
254 | 0 | payload != nullptr && payload->IsDelivery() && fits) { |
255 | | // With Shift, half the stack goes, and the rest stays. |
256 | 0 | const items::Stack* moving = StackAt(bag, from); |
257 | 0 | const uint32_t count = |
258 | 0 | ImGui::GetIO().KeyShift && moving != nullptr && moving->count() > 1 |
259 | 0 | ? moving->count() / 2 |
260 | 0 | : 0; |
261 | 0 | action = MoveTo(from, place, count); |
262 | 0 | } |
263 | 0 | ImGui::EndDragDropTarget(); |
264 | 0 | } else if (hovered && !held) { |
265 | 0 | edge = colors::kGold; |
266 | 0 | } |
267 | |
|
268 | 0 | DrawSlot(ImGui::GetWindowDrawList(), min, max, edge); |
269 | 0 | ImDrawList* draw = ImGui::GetWindowDrawList(); |
270 | | // What's held is drawn over the slot under it, so the verdict goes round the |
271 | | // slot as well, where it still shows. |
272 | 0 | if (held && !source && edge != 0) { |
273 | 0 | const float out = Ui(3.0f); |
274 | 0 | draw->AddRect({min.x - out, min.y - out}, {max.x + out, max.y + out}, edge, |
275 | 0 | Ui(2.0f), 0, Ui(2.0f)); |
276 | 0 | } |
277 | 0 | if (stack != nullptr) { |
278 | 0 | const ImU32 tint = TintOf(item); |
279 | 0 | const std::string icon = |
280 | 0 | Icon(item != nullptr ? item->look().icon() : kUnknownIcon); |
281 | | // Its place stays, faint, while it's held, and what's offered in a trade |
282 | | // is faint until it's over. |
283 | 0 | const float alpha = source ? 0.25f : locked ? 0.45f : 1.0f; |
284 | 0 | DrawSlotIcon(draw, min, max, icon.c_str(), tint, alpha); |
285 | 0 | if (stack->count() > 1 && !source) { |
286 | 0 | const std::string count = absl::StrCat(stack->count()); |
287 | 0 | ImFont* bold = GetFonts().bold; |
288 | 0 | const float count_size = Ui(14.0f); |
289 | 0 | const ImVec2 extent = |
290 | 0 | bold->CalcTextSizeA(count_size, FLT_MAX, 0.0f, count.c_str()); |
291 | 0 | DrawShadowedText( |
292 | 0 | draw, bold, count_size, |
293 | 0 | {max.x - extent.x - Ui(4.0f), max.y - extent.y - Ui(2.0f)}, |
294 | 0 | colors::kPaper, count.c_str()); |
295 | 0 | } |
296 | 0 | if (source) { |
297 | 0 | DrawDragged(icon.c_str(), tint, size, held->grab); |
298 | 0 | } |
299 | 0 | } else if (place.has_worn()) { |
300 | | // An empty slot that's worn shows what goes in it, faint. |
301 | 0 | DrawSlotIcon(draw, min, max, Icon(SlotIcon(place.worn())).c_str(), |
302 | 0 | colors::kMuted, 0.3f); |
303 | 0 | } |
304 | |
|
305 | 0 | if (stack != nullptr && !locked && hovered && |
306 | 0 | ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) { |
307 | 0 | action = QuickAction(items, bag, place); |
308 | 0 | } |
309 | 0 | if (hovered && !held && !ImGui::IsPopupOpen("menu")) { |
310 | 0 | ImGui::BeginTooltip(); |
311 | 0 | if (stack == nullptr) { |
312 | 0 | ImGui::TextDisabled( |
313 | 0 | "%s", place.has_worn() ? NameOf(place.worn()) : Tr("Empty")); |
314 | 0 | } else { |
315 | 0 | const std::vector<std::string> lines = ItemLines(items, *stack); |
316 | 0 | ImGui::PushFont(GetFonts().title, 15.0f); |
317 | 0 | ImGui::TextColored(ImGui::ColorConvertU32ToFloat4(colors::kGoldLight), |
318 | 0 | "%s", lines.front().c_str()); |
319 | 0 | ImGui::PopFont(); |
320 | 0 | for (std::size_t i = 1; i < lines.size(); ++i) { |
321 | 0 | ImGui::TextDisabled("%s", lines[i].c_str()); |
322 | 0 | } |
323 | 0 | if (locked) { |
324 | 0 | ImGui::TextDisabled("%s", Tr("Offered in the trade")); |
325 | 0 | } |
326 | 0 | } |
327 | 0 | ImGui::EndTooltip(); |
328 | 0 | } |
329 | |
|
330 | 0 | if (stack != nullptr && !locked && ImGui::BeginPopupContextItem("menu")) { |
331 | 0 | if (place.has_bag() && item != nullptr && item->has_use() && |
332 | 0 | ImGui::MenuItem(TrLabel("Use"))) { |
333 | 0 | action.emplace().set_use(place.bag()); |
334 | 0 | } |
335 | 0 | if (place.has_bag() && item != nullptr && item->has_equip() && |
336 | 0 | ImGui::MenuItem(TrLabel("Wear"))) { |
337 | 0 | action = MoveTo(place, Worn(item->equip().slot())); |
338 | 0 | } |
339 | 0 | const std::optional<uint32_t> empty = FirstEmptySlot(bag); |
340 | | // Greyed out when the bag has no room for it. |
341 | 0 | if (place.has_worn() && |
342 | 0 | ImGui::MenuItem(TrLabel("Take off"), nullptr, false, |
343 | 0 | empty.has_value()) && |
344 | 0 | empty) { |
345 | 0 | action = MoveTo(place, InBag(*empty)); |
346 | 0 | } |
347 | 0 | if (ImGui::MenuItem(TrLabel("Throw away"))) { |
348 | 0 | *action.emplace().mutable_destroy() = place; |
349 | 0 | } |
350 | 0 | ImGui::EndPopup(); |
351 | 0 | } |
352 | 0 | ImGui::PopID(); |
353 | 0 | return action; |
354 | 0 | } |
355 | | |
356 | | bool StackSlot(const ItemTable& items, const items::Stack* stack, float size, |
357 | 0 | uint32_t edge) { |
358 | 0 | const bool clicked = ImGui::InvisibleButton("##stack", {size, size}); |
359 | 0 | const ImVec2 min = ImGui::GetItemRectMin(); |
360 | 0 | const ImVec2 max = ImGui::GetItemRectMax(); |
361 | 0 | ImDrawList* draw = ImGui::GetWindowDrawList(); |
362 | 0 | DrawSlot(draw, min, max, edge); |
363 | 0 | if (stack == nullptr) { |
364 | 0 | return false; |
365 | 0 | } |
366 | 0 | const items::Item* item = Find(items, stack->item()); |
367 | 0 | DrawSlotIcon( |
368 | 0 | draw, min, max, |
369 | 0 | Icon(item != nullptr ? item->look().icon() : kUnknownIcon).c_str(), |
370 | 0 | TintOf(item)); |
371 | 0 | if (stack->count() > 1) { |
372 | 0 | const std::string count = absl::StrCat(stack->count()); |
373 | 0 | ImFont* bold = GetFonts().bold; |
374 | 0 | const float count_size = Ui(14.0f); |
375 | 0 | const ImVec2 extent = |
376 | 0 | bold->CalcTextSizeA(count_size, FLT_MAX, 0.0f, count.c_str()); |
377 | 0 | DrawShadowedText(draw, bold, count_size, |
378 | 0 | {max.x - extent.x - Ui(4.0f), max.y - extent.y - Ui(2.0f)}, |
379 | 0 | colors::kPaper, count.c_str()); |
380 | 0 | } |
381 | 0 | if (ImGui::IsItemHovered()) { |
382 | 0 | const std::vector<std::string> lines = ItemLines(items, *stack); |
383 | 0 | ImGui::BeginTooltip(); |
384 | 0 | ImGui::PushFont(GetFonts().title, 15.0f); |
385 | 0 | ImGui::TextColored(ImGui::ColorConvertU32ToFloat4(colors::kGoldLight), "%s", |
386 | 0 | lines.front().c_str()); |
387 | 0 | ImGui::PopFont(); |
388 | 0 | for (std::size_t i = 1; i < lines.size(); ++i) { |
389 | 0 | ImGui::TextDisabled("%s", lines[i].c_str()); |
390 | 0 | } |
391 | 0 | ImGui::EndTooltip(); |
392 | 0 | } |
393 | 0 | return clicked; |
394 | 0 | } |
395 | | |
396 | 0 | std::optional<protocol::Place> AcceptItem() { |
397 | 0 | const ImGuiPayload* payload = ImGui::AcceptDragDropPayload(kPayload); |
398 | 0 | if (payload == nullptr) { |
399 | 0 | return std::nullopt; |
400 | 0 | } |
401 | 0 | Dragged dragged; |
402 | 0 | std::memcpy(&dragged, payload->Data, sizeof(dragged)); |
403 | 0 | return PlaceOf(dragged); |
404 | 0 | } |
405 | | |
406 | | std::optional<protocol::ItemAction> BagWindow::Build( |
407 | 0 | const protocol::Inventory& bag, const protocol::TradeSide& offered) { |
408 | 0 | std::optional<protocol::ItemAction> action; |
409 | | // Let go of over nothing, from the bag or from what's worn, it's thrown |
410 | | // away, once the player says so. |
411 | 0 | if (const std::optional<Dragged> held = Held(); |
412 | 0 | held && ImGui::IsMouseReleased(ImGuiMouseButton_Left) && |
413 | 0 | !ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow | |
414 | 0 | ImGuiHoveredFlags_AllowWhenBlockedByActiveItem)) { |
415 | 0 | throwing_ = PlaceOf(*held); |
416 | 0 | } |
417 | 0 | if (throwing_) { |
418 | 0 | if (std::optional<protocol::ItemAction> done = BuildThrowAway(bag)) { |
419 | 0 | action = std::move(done); |
420 | 0 | } |
421 | 0 | } |
422 | 0 | if (!visible_) { |
423 | 0 | return action; |
424 | 0 | } |
425 | 0 | const ImVec2 display = ImGui::GetIO().DisplaySize; |
426 | 0 | ImGui::SetNextWindowPos({display.x - Ui(18.0f), display.y * 0.55f}, |
427 | 0 | ImGuiCond_FirstUseEver, {1.0f, 0.5f}); |
428 | 0 | if (BeginPanel("Bag", &visible_, |
429 | 0 | ImGuiWindowFlags_AlwaysAutoResize | |
430 | 0 | ImGuiWindowFlags_NoSavedSettings)) { |
431 | 0 | const float slot = Ui(kBagSlot); |
432 | 0 | ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, Ui(ImVec2{5.0f, 5.0f})); |
433 | 0 | for (uint32_t index = 0; index < kBagSlots; ++index) { |
434 | 0 | if (index % kColumns != 0) { |
435 | 0 | ImGui::SameLine(); |
436 | 0 | } |
437 | 0 | if (std::optional<protocol::ItemAction> done = ItemSlot( |
438 | 0 | items_, bag, InBag(index), slot, offered.bag().contains(index))) { |
439 | 0 | action = std::move(done); |
440 | 0 | } |
441 | 0 | } |
442 | 0 | ImGui::PopStyleVar(); |
443 | | // Gold takes no slot, so it goes under them. |
444 | 0 | ImGui::TextColored( |
445 | 0 | ImGui::ColorConvertU32ToFloat4(colors::kGold), ICON_FA_COINS " %s", |
446 | 0 | absl::Substitute(TrN("$0 gold", "$0 gold", bag.gold()), bag.gold()) |
447 | 0 | .c_str()); |
448 | 0 | } |
449 | 0 | EndPanel(); |
450 | 0 | return action; |
451 | 0 | } |
452 | | |
453 | | std::optional<protocol::ItemAction> BagWindow::BuildThrowAway( |
454 | 0 | const protocol::Inventory& bag) { |
455 | 0 | if (!throwing_) { |
456 | 0 | return std::nullopt; |
457 | 0 | } |
458 | 0 | const protocol::Place place = *throwing_; |
459 | 0 | const items::Stack* stack = StackAt(bag, place); |
460 | 0 | const items::Item* item = |
461 | 0 | stack != nullptr ? Find(items_, stack->item()) : nullptr; |
462 | 0 | if (stack == nullptr) { |
463 | 0 | throwing_.reset(); |
464 | 0 | return std::nullopt; |
465 | 0 | } |
466 | 0 | const ImVec2 display = ImGui::GetIO().DisplaySize; |
467 | 0 | ImGui::SetNextWindowPos({display.x / 2.0f, display.y * 0.42f}, |
468 | 0 | ImGuiCond_Always, {0.5f, 0.5f}); |
469 | 0 | bool open = true; |
470 | 0 | std::optional<protocol::ItemAction> action; |
471 | 0 | BeginPanel("Throw away?", &open, |
472 | 0 | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove | |
473 | 0 | ImGuiWindowFlags_NoSavedSettings); |
474 | 0 | const char* name = item != nullptr ? NameOf(*item) : Tr("this item"); |
475 | 0 | ImGui::TextUnformatted( |
476 | 0 | absl::Substitute( |
477 | 0 | TrN("Throw away the $1, for good?", |
478 | 0 | "Throw away all $0 of the $1, for good?", CountOf(*stack)), |
479 | 0 | CountOf(*stack), name) |
480 | 0 | .c_str()); |
481 | 0 | ImGui::Spacing(); |
482 | 0 | if (GameButton("Throw away", {Ui(150.0f), 0.0f}, true)) { |
483 | 0 | *action.emplace().mutable_destroy() = place; |
484 | 0 | open = false; |
485 | 0 | } |
486 | 0 | ImGui::SameLine(); |
487 | 0 | if (GameButton("Keep it", {Ui(150.0f), 0.0f}) || |
488 | 0 | ImGui::IsKeyPressed(ImGuiKey_Escape)) { |
489 | 0 | open = false; |
490 | 0 | } |
491 | 0 | EndPanel(); |
492 | 0 | if (!open) { |
493 | 0 | throwing_.reset(); |
494 | 0 | } |
495 | 0 | return action; |
496 | 0 | } |
497 | | |
498 | | } // namespace nanommo |