aboutsummaryrefslogtreecommitdiff
path: root/src/client/window/hud_window.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/window/hud_window.cc')
-rw-r--r--src/client/window/hud_window.cc88
1 files changed, 88 insertions, 0 deletions
diff --git a/src/client/window/hud_window.cc b/src/client/window/hud_window.cc
new file mode 100644
index 0000000..52995ca
--- /dev/null
+++ b/src/client/window/hud_window.cc
@@ -0,0 +1,88 @@
+#include "client/window/hud_window.hh"
+
+namespace client {
+namespace window {
+
+using hw = class hud_window;
+bool hw::maybe_handle_keydown(const SDL_Event& event) noexcept {
+ const auto& sym = event.key.keysym.sym;
+ const auto index = static_cast<int>(sym);
+
+ const auto min = static_cast<int>(SDLK_0);
+ const auto max = static_cast<int>(SDLK_9);
+ if (index < min || index > max) {
+ return false;
+ }
+
+ const auto active =
+ static_cast<std::uint32_t>((((sym - min) - 1) + 10) % 10);
+ client::get_localplayer().get_mutable_active_item() = active;
+ return true;
+}
+
+bool hw::maybe_handle_event(const SDL_Event& event) noexcept {
+ if (is_open()) {
+ return false;
+ }
+
+ switch (event.type) {
+ case SDL_KEYDOWN:
+ return this->maybe_handle_keydown(event);
+ default:
+ break;
+ }
+ return false;
+}
+
+void hw::draw() noexcept {
+ this->basic_window::draw();
+
+ const float item_size = this->get_item_size();
+
+ const auto& localplayer = client::get_localplayer();
+ client::render::draw_rectangle(
+ {.pos = {.offset = {this->pos.x + static_cast<float>(
+ localplayer.get_active_item()) *
+ item_size,
+ this->pos.y}},
+ .size = {.offset = {item_size, item_size}},
+ .colour = {this->secondary_clr, 1.0f}});
+
+ for (int i = 1; i < shared::player::INVENTORY_COLS; ++i) {
+ const float off = item_size * static_cast<float>(i);
+ client::render::draw_rectangle(
+ {.pos = {.offset = {this->pos.x + off,
+ this->pos.y + OUTLINE_WIDTH}},
+ .size = {.offset = {OUTLINE_WIDTH,
+ item_size - 2.0f * OUTLINE_WIDTH}},
+ .colour = {this->tertiary_clr, 1.0f}});
+ }
+
+ const auto& inventory = localplayer.inventory;
+ for (int x = 0; x < shared::player::INVENTORY_COLS; ++x) {
+ const auto& item = inventory.contents[static_cast<unsigned long>(x)];
+ if (item == nullptr) {
+ continue;
+ }
+
+ const glm::vec2 off{glm::vec2{x, 0} * item_size};
+ const auto item_ptr = dynamic_cast<client::item::item*>(&*item);
+ if (item_ptr == nullptr) {
+ continue;
+ }
+ item_ptr->draw(this->pos + off, glm::vec2{item_size, item_size});
+
+ client::render::draw_text(
+ std::to_string(item->quantity),
+ {.pos = {.offset = this->pos + off +
+ glm::vec2{item_size * 0.75f, item_size * 0.2f}},
+ .offset_height = item_size * 0.40f,
+ .colour = {this->font_colour, 1.0f},
+ .has_backing = false,
+ .is_centered = true,
+ .is_vcentered = true});
+ }
+}
+
+} // namespace window
+} // namespace client