nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
hotbar.cc
Go to the documentation of this file.
1#include "client/hotbar.h"
2
3#include <imgui.h>
4
5#include <algorithm>
6#include <cfloat>
7#include <cmath>
8#include <cstddef>
9#include <cstdint>
10#include <cstring>
11#include <numbers>
12#include <optional>
13#include <set>
14#include <string>
15#include <utility>
16#include <vector>
17
18#include "absl/strings/substitute.h"
19#include "client/icon.h"
20#include "client/input.h"
21#include "client/names.h"
22#include "client/picking.h"
23#include "client/theme.h"
24#include "client/translations.h"
25
26namespace nanommo {
27
28namespace {
29
30// In points, at a scale of one.
31constexpr float kSlot = 54.0f;
32constexpr float kGap = 6.0f;
33constexpr float kMargin = 14.0f;
34constexpr float kPadding = 12.0f;
35
36constexpr char kPayload[] = "SKILL";
37
38// As most games show a skill without a target.
39constexpr ImU32 kNoTarget = IM_COL32(242, 77, 64, 255);
40
41// A skill's colour, which its icon's in: its first fx's, as it looks in the
42// world, or else the paper of the words.
43ImU32 SkillColor(const skills::Skill& skill) {
44 for (const skills::Fx& fx : skill.look().fx()) {
45 if (fx.color() != 0) {
46 const uint32_t rgba = fx.color();
47 return IM_COL32((rgba >> 24) & 0xFF, (rgba >> 16) & 0xFF,
48 (rgba >> 8) & 0xFF, 255);
49 }
50 }
51 return colors::kPaper;
52}
53
54void DrawCentered(ImDrawList* draw, ImFont* font, ImVec2 at, float size,
55 ImU32 color, const std::string& text) {
56 const ImVec2 extent = font->CalcTextSizeA(size, FLT_MAX, 0.0f, text.c_str());
57 DrawShadowedText(draw, font, size, {at.x - extent.x / 2, at.y - extent.y / 2},
58 color, text.c_str());
59}
60
61// Shades the share of the slot that's still cooling down, as a slice that
62// shrinks clockwise from the top, with the seconds left in the middle.
63void DrawCooldown(ImDrawList* draw, ImVec2 min, ImVec2 max, float share,
64 uint32_t milliseconds) {
65 constexpr float kTop = -std::numbers::pi_v<float> / 2;
66 constexpr float kTurn = 2 * std::numbers::pi_v<float>;
67 const ImVec2 middle{(min.x + max.x) / 2, (min.y + max.y) / 2};
68 // Inside the slot's brass.
69 const float inset = Ui(2.5f);
70 draw->PushClipRect({min.x + inset, min.y + inset},
71 {max.x - inset, max.y - inset}, true);
72 draw->PathLineTo(middle);
73 draw->PathArcTo(middle, max.x - min.x, kTop + kTurn * (1.0f - share),
74 kTop + kTurn, 32);
75 draw->PathFillConcave(IM_COL32(0, 0, 0, 175));
76 // The edge of the slice, bright, as it sweeps round.
77 const float edge = kTop + kTurn * (1.0f - share);
78 draw->AddLine(middle,
79 {middle.x + std::cos(edge) * (max.x - min.x),
80 middle.y + std::sin(edge) * (max.x - min.x)},
81 Faded(colors::kGold, 0.6f), Ui(1.2f));
82 draw->PopClipRect();
83 DrawCentered(draw, GetFonts().bold, middle, Ui(22.0f), colors::kGoldLight,
84 std::to_string(static_cast<int>(
85 std::ceil(static_cast<float>(milliseconds) / 1000.0f))));
86}
87
88// Which slot a payload came from. ImGui copies it byte for byte.
89struct Dragged {
90 int slot = 0;
91 ImVec2 grab;
92};
93
94} // namespace
95
96Hotbar::Hotbar(const SkillTable& skills) {
97 // The swings Space goes on into are Space's, so they get no slot.
98 std::set<uint32_t> follow;
99 for (const auto& [id, skill] : skills.by_id) {
100 follow.insert(skill.next());
101 }
102 for (const auto& [id, skill] : skills.by_id) {
103 if (id != skills.attack && !follow.contains(id)) {
104 slots_.push_back(skill);
105 }
106 }
107 std::ranges::stable_sort(slots_, {}, &skills::Skill::level);
108 if (std::ssize(slots_) > kMostSlots) {
109 slots_.resize(kMostSlots);
110 }
111 const auto swing = skills.by_id.find(skills.attack);
112 if (swing != skills.by_id.end()) {
113 slots_.insert(slots_.begin(), swing->second);
114 swing_ = true;
115 }
116}
117
118std::vector<uint32_t> Hotbar::Order() const {
119 std::vector<uint32_t> order;
120 for (std::size_t slot = swing_ ? 1 : 0; slot < slots_.size(); ++slot) {
121 order.push_back(slots_[slot].id());
122 }
123 return order;
124}
125
126void Hotbar::Arrange(const std::vector<uint32_t>& order) {
127 const auto first = slots_.begin() + (swing_ ? 1 : 0);
128 // Each skill's place in the order, or after them all if it isn't there.
129 const auto place = [&order](const skills::Skill& skill) {
130 const auto found = std::ranges::find(order, skill.id());
131 return std::distance(order.begin(), found);
132 };
133 std::stable_sort(first, slots_.end(),
134 [&](const skills::Skill& a, const skills::Skill& b) {
135 return place(a) < place(b);
136 });
137}
138
139bool Hotbar::TakeRearranged() { return std::exchange(rearranged_, false); }
140
141std::optional<uint32_t> Hotbar::Build(const std::vector<Figure>& figures,
142 const protocol::World& world,
143 uint32_t target, uint32_t target_player) {
144 const auto you = std::ranges::find_if(figures, &Figure::you);
145 // Our level comes with our name, once a second, so the bar waits for it.
146 if (you == figures.end() || you->level == 0 || slots_.empty()) {
147 return std::nullopt;
148 }
149 const bool picked = std::ranges::any_of(figures, [&](const Figure& f) {
150 return Picked(f, target, target_player) && f.to != protocol::MOTION_DEAD;
151 });
152 const float slot_size = Ui(kSlot);
153 const float gap = Ui(kGap);
154 const float padding = Ui(kPadding);
155 // The swing and the number keys along the bottom, and the row on Shift
156 // above them, past the swing's slot.
157 const std::size_t first = swing_ ? 1 : 0;
158 const std::size_t keyed = slots_.size() - first;
159 const bool two_rows = keyed > kRow;
160 const float columns =
161 static_cast<float>(std::min(slots_.size(), first + kRow));
162 const float rows = two_rows ? 2.0f : 1.0f;
163 const ImVec2 size{
164 columns * slot_size + (columns - 1.0f) * gap + 2.0f * padding,
165 rows * slot_size + (rows - 1.0f) * gap + 2.0f * padding};
166 const ImVec2 screen = ImGui::GetIO().DisplaySize;
167 ImGui::SetNextWindowPos({screen.x / 2, screen.y - Ui(kMargin)},
168 ImGuiCond_Always, {0.5f, 1.0f});
169 ImGui::SetNextWindowSize(size);
170 ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {0.0f, 0.0f});
171 ImGui::Begin("Hotbar", nullptr,
172 ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove |
173 ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoNav |
174 ImGuiWindowFlags_NoFocusOnAppearing |
175 ImGuiWindowFlags_NoSavedSettings);
176 ImGui::PopStyleVar();
177 ImDrawList* draw = ImGui::GetWindowDrawList();
178 const ImVec2 corner = ImGui::GetWindowPos();
179 DrawPanel(draw, corner, {corner.x + size.x, corner.y + size.y}, 0.9f);
180
181 const std::optional<int> key = HotbarKey();
182 std::optional<uint32_t> used;
183 const ImGuiPayload* payload = ImGui::GetDragDropPayload();
184 const bool dragging = payload != nullptr && payload->IsDataType(kPayload);
185 Dragged from;
186 if (dragging) {
187 std::memcpy(&from, payload->Data, sizeof(from));
188 }
189 for (std::size_t slot = 0; slot < slots_.size(); ++slot) {
190 const skills::Skill& skill = slots_[slot];
191 const bool reached = you->level >= static_cast<int>(skill.level());
192 const bool known =
193 std::ranges::find(world.skills(), skill.id()) != world.skills().end();
194 const skills::Effect& effect = skill.effect();
195 const bool no_target =
196 known && effect.damage() > 0 && !effect.all() && !picked;
197 // The swing stays on Space, at the front.
198 const bool swing = swing_ && slot == 0;
199 const bool above = slot >= first + kRow;
200 const std::size_t column = above ? slot - kRow : slot;
201 const float row = above || !two_rows ? 0.0f : 1.0f;
202 const ImVec2 min{
203 corner.x + padding + static_cast<float>(column) * (slot_size + gap),
204 corner.y + padding + row * (slot_size + gap)};
205 const ImVec2 max{min.x + slot_size, min.y + slot_size};
206 ImGui::PushID(static_cast<int>(skill.id()));
207 ImGui::SetCursorScreenPos(min);
208 const bool clicked =
209 ImGui::InvisibleButton("##slot", {slot_size, slot_size}) && known;
210 const bool hovered = ImGui::IsItemHovered();
211 const bool held = ImGui::IsItemActive();
212
213 // Dragged onto another, two skills swap.
214 if (!swing &&
215 ImGui::BeginDragDropSource(ImGuiDragDropFlags_SourceNoPreviewTooltip)) {
216 // Held where the mouse went down on it.
217 const ImVec2 down = ImGui::GetIO().MouseClickedPos[ImGuiMouseButton_Left];
218 const Dragged dragged{.slot = static_cast<int>(slot),
219 .grab = {down.x - min.x, down.y - min.y}};
220 ImGui::SetDragDropPayload(kPayload, &dragged, sizeof(dragged),
221 ImGuiCond_Once);
222 ImGui::EndDragDropSource();
223 }
224 ImU32 edge = 0;
225 bool source = false;
226 if (dragging) {
227 source = from.slot == static_cast<int>(slot);
228 if (!swing && !source && ImGui::BeginDragDropTarget()) {
229 edge = colors::kGood;
230 if (ImGui::AcceptDragDropPayload(
231 kPayload, ImGuiDragDropFlags_AcceptNoDrawDefaultRect)) {
232 std::swap(slots_[slot], slots_[static_cast<std::size_t>(from.slot)]);
233 rearranged_ = true;
234 }
235 ImGui::EndDragDropTarget();
236 }
237 } else if (hovered) {
238 edge = held ? colors::kEmber : colors::kGold;
239 }
240
241 DrawSlot(draw, min, max, edge);
242 const ImU32 color = no_target ? kNoTarget : SkillColor(skill);
243 const float alpha = !known ? 0.3f : (source ? 0.25f : 1.0f);
244 DrawSlotIcon(draw, min, max, Icon(skill.look().icon()).c_str(), color,
245 alpha);
246 if (source) {
247 DrawDragged(Icon(skill.look().icon()).c_str(), color, slot_size,
248 from.grab);
249 }
250 if (hovered && !dragging) {
251 if (effect.weapon()) {
252 ImGui::SetTooltip("%s\n%s", NameOf(skill),
253 Tr("A swing of what you hold."));
254 } else {
255 ImGui::SetTooltip(
256 "%s\n%s", NameOf(skill),
257 absl::Substitute(
258 Tr("From a trainer, at level $0. Ready again after $1 s."),
259 skill.level(), skill.cooldown_ms() / 1000)
260 .c_str());
261 }
262 }
263 const auto left = world.cooldowns().find(skill.id());
264 if (!known) {
265 DrawCentered(
266 draw, GetFonts().bold, {(min.x + max.x) / 2, max.y - Ui(11.0f)},
267 Ui(13.0f), reached ? colors::kGold : colors::kMuted,
268 reached ? Tr("Learn") : absl::Substitute(Tr("Lv $0"), skill.level()));
269 } else if (left != world.cooldowns().end() && skill.cooldown_ms() > 0) {
270 DrawCooldown(draw, min, max,
271 std::min(static_cast<float>(left->second) /
272 static_cast<float>(skill.cooldown_ms()),
273 1.0f),
274 left->second);
275 }
276 // The swing's on Space, and the number keys count from the next, with
277 // Shift for the row above.
278 const int index = static_cast<int>(slot - first);
279 const std::string label =
280 swing ? Tr("Space")
281 : (above ? "s" : "") + std::to_string(index % kRow + 1);
282 DrawShadowedText(draw, GetFonts().bold, Ui(13.0f),
283 {min.x + Ui(5.0f), min.y + Ui(2.0f)}, colors::kPaper,
284 label.c_str());
285 if (clicked || (!swing && key == index)) {
286 used = skill.id();
287 }
288 ImGui::PopID();
289 }
290 ImGui::End();
291 return used;
292}
293
294} // namespace nanommo
static constexpr int kMostSlots
Definition hotbar.h:23
static constexpr int kRow
A row's worth of number keys.
Definition hotbar.h:22
void Arrange(const std::vector< uint32_t > &order)
Definition hotbar.cc:126
std::vector< uint32_t > Order() const
The skills after the swing, by their ids, in their slots' order.
Definition hotbar.cc:118
std::optional< uint32_t > Build(const std::vector< Figure > &figures, const protocol::World &world, uint32_t target, uint32_t target_player=0)
Definition hotbar.cc:141
bool TakeRearranged()
Whether the player has swapped two skills since this was last asked.
Definition hotbar.cc:139
Hotbar(const SkillTable &skills)
Definition hotbar.cc:96
constexpr ImU32 kEmber
Definition theme.h:19
constexpr ImU32 kGood
Definition theme.h:32
constexpr ImU32 kGoldLight
Definition theme.h:22
constexpr ImU32 kMuted
Definition theme.h:27
constexpr ImU32 kPaper
Definition theme.h:26
constexpr ImU32 kGold
Definition theme.h:21
Definition chat.cc:9
void DrawPanel(ImDrawList *draw, ImVec2 min, ImVec2 max, float alpha)
Definition theme.cc:223
float Ui(float points)
Definition theme.h:67
const char * Tr(const char *text)
std::string Icon(uint32_t code)
Definition icon.h:12
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.
Definition theme.cc:519
bool Picked(const Figure &figure, uint32_t target, uint32_t target_player)
Definition picking.cc:86
void DrawDragged(const char *icon, ImU32 color, float size, ImVec2 grab)
Definition theme.cc:536
ImU32 Faded(ImU32 color, float share)
The same colour, with its alpha times the share.
Definition theme.cc:59
void DrawShadowedText(ImDrawList *draw, ImFont *font, float size, ImVec2 at, ImU32 color, const char *text)
Text with a shadow under it.
Definition theme.cc:272
void DrawSlot(ImDrawList *draw, ImVec2 min, ImVec2 max, ImU32 edge)
Definition theme.cc:480
const Fonts & GetFonts()
Definition theme.cc:66
std::optional< int > HotbarKey()
Definition input.cc:54
const char * NameOf(const items::Item &item)
Definition names.cc:14
protocol::Motion to
Definition animator.h:77
uint32_t attack
Definition skills.h:19
std::map< uint32_t, skills::Skill > by_id
Definition skills.h:17