summaryrefslogtreecommitdiffstats
path: root/src/game/client/gui/imutils_button.cc
blob: 2017e33a46d211d1143b321885d371f9db9d560b (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
89
90
91
92
93
94
95
#include "client/pch.hh"

#include "client/gui/imutils_button.hh"

void imutils::button(const char* title, const ImVec2& size, void (*callback)(void))
{
    assert(title);
    assert(callback);

    if(ImGui::Button(title, size)) {
        callback();
    }
}

void imutils::button(const char* title, void (*callback)(void))
{
    assert(title);
    assert(callback);

    if(ImGui::Button(title)) {
        callback();
    }
}

bool imutils::selectable_button(const char* label, const ImVec2& size, bool value)
{
    assert(label);

    auto value_changed = false;

    if(value) {
        ImVec4 button_color(0.750f, 0.750f, 0.750f, 1.000f);
        ImGui::PushStyleColor(ImGuiCol_Button, button_color);
        ImGui::PushStyleColor(ImGuiCol_ButtonActive, button_color);
        ImGui::PushStyleColor(ImGuiCol_ButtonHovered, button_color);
        ImGui::PushStyleColor(ImGuiCol_Text, ImGui::GetStyleColorVec4(ImGuiCol_WindowBg));
    }
    else {
        ImGui::PushStyleColor(ImGuiCol_Button, ImGui::GetStyleColorVec4(ImGuiCol_WindowBg));
    }

    if(ImGui::Button(label, size)) {
        value_changed = true;
    }

    ImGui::PopStyleColor(value ? 4 : 1);

    return value_changed;
}

bool imutils::toggle_button(const char* label, const ImVec2& size, bool& value)
{
    assert(label);

    auto use_custom_color = !value;
    auto value_changed = false;

    if(use_custom_color) {
        ImGui::PushStyleColor(ImGuiCol_Button, ImGui::GetStyleColorVec4(ImGuiCol_WindowBg));
    }

    if(ImGui::Button(label, size)) {
        value_changed = true;
        value = !value;
    }

    if(use_custom_color) {
        ImGui::PopStyleColor();
    }

    return value_changed;
}

bool imutils::toggle_button(const char* label, bool& value)
{
    assert(label);

    auto use_custom_color = !value;
    auto value_changed = false;

    if(use_custom_color) {
        ImGui::PushStyleColor(ImGuiCol_Button, ImGui::GetStyleColorVec4(ImGuiCol_WindowBg));
    }

    if(ImGui::Button(label)) {
        value_changed = true;
        value = !value;
    }

    if(use_custom_color) {
        ImGui::PopStyleColor();
    }

    return value_changed;
}