aboutsummaryrefslogtreecommitdiff
path: root/src/client/window/button_window.cc
blob: 4743c0a631b3935eb691ca3164f3bfa3c0b875dc (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
#include "client/window/button_window.hh"

namespace client {
namespace window {

using bw = class button_window;

void bw::handle_mousebuttondown(const SDL_Event& event) noexcept {
    if (event.button.button != SDL_BUTTON_LEFT) {
        return;
    }
    this->is_pressed = true;
}

void bw::handle_mousebuttonup(const SDL_Event& event) noexcept {
    if (event.button.button != SDL_BUTTON_LEFT) {
        return;
    }
    if (!this->is_pressed) {
        return;
    }
    this->is_pressed = false;
    std::invoke(this->callback); // edge
}

const glm::vec3& bw::get_draw_colour() noexcept {
    if (!this->is_inside(client::input::state.mouse_pos)) {
        this->is_pressed = false;
        return this->primary_clr;
    }

    if (!this->is_pressed) {
        return this->secondary_clr;
    }

    return this->highlight_clr;
}

bool bw::maybe_handle_event(const SDL_Event& event) noexcept {
    if (this->is_inside(client::input::state.mouse_pos)) {
        switch (event.type) {
        case SDL_MOUSEBUTTONDOWN:
            this->handle_mousebuttondown(event);
            return true;
        case SDL_MOUSEBUTTONUP:
            this->handle_mousebuttonup(event);
            return true;
        }
    }

    return basic_window::maybe_handle_event(event);
}

void bw::draw() noexcept {
    basic_window::draw();

    client::render::draw_rectangle({.pos = {.offset = this->pos},
                                    .size = {.offset = this->size},
                                    .colour = {this->get_draw_colour(), 1.0f}});
    client::render::draw_text(
        this->name, {.pos = {.extent = {0.0f, 0.0f},
                             .offset = this->pos + (this->size / 2.0f)},
                     .offset_height = this->size.y / 2.0f,
                     .colour = {this->font_colour, 1.0f},
                     .has_backing = false,
                     .is_centered = true,
                     .is_vcentered = true});
};

} // namespace window
} // namespace client