aboutsummaryrefslogtreecommitdiff
path: root/src/client/window/hud_window.cc
blob: 52995ca3f02d0c5d7b1c8b715f6fff9570e037ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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