nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
trade_window.cc
Go to the documentation of this file.
2
3#include <imgui.h>
4
5#include <algorithm>
6#include <cstddef>
7#include <cstdint>
8#include <optional>
9#include <string>
10#include <vector>
11
12#include "absl/strings/str_cat.h"
13#include "absl/strings/substitute.h"
14#include "client/bag_window.h"
15#include "client/theme.h"
16#include "client/translations.h"
17#include "extras/IconsFontAwesome6.h"
18
19namespace nanommo {
20
21namespace {
22
23// In points, at a scale of one.
24constexpr float kSlot = 46.0f;
25constexpr float kBetween = 28.0f;
26// Each side shows eight slots, or as many as it offers, four to a row.
27constexpr std::size_t kSlots = 8;
28constexpr std::size_t kColumns = 4;
29
30// The side's places in the bag, in order, as a map's come in none.
31std::vector<uint32_t> PlacesOf(const protocol::TradeSide& side) {
32 std::vector<uint32_t> places;
33 for (const auto& [place, stack] : side.bag()) {
34 places.push_back(place);
35 }
36 std::ranges::sort(places);
37 return places;
38}
39
40std::size_t SlotsFor(std::size_t offered) {
41 return std::max(kSlots, (offered + kColumns - 1) / kColumns * kColumns);
42}
43
44} // namespace
45
46protocol::TradeOffer OfferOf(const protocol::TradeSide& side) {
47 protocol::TradeOffer offer;
48 const std::vector<uint32_t> places = PlacesOf(side);
49 offer.mutable_bag()->Add(places.begin(), places.end());
50 offer.set_gold(side.gold());
51 return offer;
52}
53
54std::optional<protocol::TradeAction> TradeWindow::Build(
55 const protocol::Trade& trade) {
56 if (trade.partner().empty()) {
57 typing_ = false;
58 return std::nullopt;
59 }
60 const protocol::TradeSide& yours = trade.yours();
61 const protocol::TradeSide& theirs = trade.theirs();
62 const char* partner = trade.partner().c_str();
63 std::optional<protocol::TradeAction> action;
64 const ImVec2 display = ImGui::GetIO().DisplaySize;
65 ImGui::SetNextWindowPos({display.x * 0.5f, display.y * 0.45f},
66 ImGuiCond_FirstUseEver, {0.5f, 0.5f});
67 bool open = true;
68 // The same window, whoever it's with.
69 const std::string title =
70 absl::StrCat(absl::Substitute(Tr("Trade with $0"), partner), "###Trade");
71 if (BeginPanel(title.c_str(), &open,
72 ImGuiWindowFlags_AlwaysAutoResize |
73 ImGuiWindowFlags_NoSavedSettings)) {
74 const float slot = Ui(kSlot);
75 ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, Ui(ImVec2{5.0f, 5.0f}));
76
77 // Yours: what's dragged onto it from the bag goes in, and a click on one
78 // takes it back. Each change is a new offer, in place of this one.
79 protocol::TradeOffer offer = OfferOf(yours);
80 bool changed = false;
81 const std::vector<uint32_t> offered = PlacesOf(yours);
82 ImGui::BeginGroup();
83 ImGui::TextUnformatted(Tr("You offer"));
84 for (std::size_t i = 0; i < SlotsFor(offered.size()); ++i) {
85 if (i % kColumns != 0) {
86 ImGui::SameLine();
87 }
88 ImGui::PushID(static_cast<int>(i));
89 const items::Stack* stack =
90 i < offered.size() ? &yours.bag().at(offered[i]) : nullptr;
91 if (StackSlot(items_, stack, slot, yours.ready() ? colors::kGood : 0) &&
92 stack != nullptr) {
93 offer.clear_bag();
94 for (const uint32_t place : offered) {
95 if (place != offered[i]) {
96 offer.add_bag(place);
97 }
98 }
99 changed = true;
100 }
101 if (ImGui::BeginDragDropTarget()) {
102 if (const std::optional<protocol::Place> from = AcceptItem();
103 from && from->has_bag() && !yours.bag().contains(from->bag())) {
104 offer.add_bag(from->bag());
105 changed = true;
106 }
107 ImGui::EndDragDropTarget();
108 }
109 ImGui::PopID();
110 }
111 // What's typed stays as it's typed, and goes once it's done with.
112 if (!typing_) {
113 gold_ = yours.gold();
114 }
115 ImGui::TextColored(ImGui::ColorConvertU32ToFloat4(colors::kGold),
116 ICON_FA_COINS);
117 ImGui::SameLine();
118 ImGui::SetNextItemWidth(Ui(110.0f));
119 ImGui::InputScalar(TrLabel("gold##yours"), ImGuiDataType_U32, &gold_);
120 typing_ = ImGui::IsItemActive();
121 if (ImGui::IsItemDeactivatedAfterEdit()) {
122 offer.set_gold(gold_);
123 changed = true;
124 }
125 ImGui::EndGroup();
126 if (changed) {
127 *action.emplace().mutable_offer() = offer;
128 }
129
130 // Theirs, to look at.
131 ImGui::SameLine(0.0f, Ui(kBetween));
132 ImGui::BeginGroup();
133 ImGui::TextUnformatted(absl::Substitute(Tr("$0 offers"), partner).c_str());
134 const std::vector<uint32_t> places = PlacesOf(theirs);
135 for (std::size_t i = 0; i < SlotsFor(places.size()); ++i) {
136 if (i % kColumns != 0) {
137 ImGui::SameLine();
138 }
139 ImGui::PushID(static_cast<int>(100 + i));
140 StackSlot(items_,
141 i < places.size() ? &theirs.bag().at(places[i]) : nullptr, slot,
142 theirs.ready() ? colors::kGood : 0);
143 ImGui::PopID();
144 }
145 ImGui::TextColored(
146 ImGui::ColorConvertU32ToFloat4(colors::kGold), ICON_FA_COINS " %s",
147 absl::Substitute(TrN("$0 gold", "$0 gold", theirs.gold()),
148 theirs.gold())
149 .c_str());
150 ImGui::EndGroup();
151 ImGui::PopStyleVar();
152
153 ImGui::Spacing();
154 if (GameButton(yours.ready() ? "Not ready" : "Ready", {Ui(150.0f), 0.0f},
155 !yours.ready())) {
156 action.emplace().set_ready(!yours.ready());
157 action->set_revision(trade.revision());
158 }
159 ImGui::SameLine();
160 if (theirs.ready()) {
161 ImGui::TextColored(ImGui::ColorConvertU32ToFloat4(colors::kGood), "%s",
162 absl::Substitute(Tr("$0 is ready."), partner).c_str());
163 } else {
164 ImGui::TextDisabled(
165 "%s", absl::Substitute(Tr("$0 isn't ready yet."), partner).c_str());
166 }
167 }
168 EndPanel();
169 if (!open) {
170 action.emplace().set_cancel(true);
171 }
172 return action;
173}
174
175} // namespace nanommo
std::optional< protocol::TradeAction > Build(const protocol::Trade &trade)
constexpr ImU32 kGood
Definition theme.h:32
constexpr ImU32 kGold
Definition theme.h:21
Definition chat.cc:9
bool BeginPanel(const char *name, bool *open, ImGuiWindowFlags flags, float alpha)
Definition theme.cc:320
float Ui(float points)
Definition theme.h:67
void EndPanel()
Definition theme.cc:379
const char * Tr(const char *text)
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)
protocol::TradeOffer OfferOf(const protocol::TradeSide &side)
std::optional< protocol::Place > AcceptItem()
bool StackSlot(const ItemTable &items, const items::Stack *stack, float size, uint32_t edge)
bool GameButton(const char *label, ImVec2 size, bool primary, bool enabled)
Definition theme.cc:381