summaryrefslogtreecommitdiffstats
path: root/src/game/client/gui/status_lines.cc
blob: ce661a3d37c3ffed95cf14a98ea5a1bcceb891f9 (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
#include "client/pch.hh"

#include "client/gui/status_lines.hh"

#include "client/gui/imutils_text.hh"

#include "client/globals.hh"

static float line_offsets[STATUS_COUNT];
static ImFont* line_fonts[STATUS_COUNT];
static float line_sizes[STATUS_COUNT];

static ImVec4 line_text_colors[STATUS_COUNT];
static ImVec4 line_shadow_colors[STATUS_COUNT];
static std::string line_strings[STATUS_COUNT];
static std::uint64_t line_spawns[STATUS_COUNT];
static float line_fadeouts[STATUS_COUNT];

void status_lines::init(void)
{
    for(unsigned int i = 0U; i < STATUS_COUNT; ++i) {
        line_text_colors[i] = ImVec4(0.0f, 0.0f, 0.0f, 0.0f);
        line_shadow_colors[i] = ImVec4(0.0f, 0.0f, 0.0f, 0.0f);
        line_strings[i] = std::string();
        line_spawns[i] = UINT64_MAX;
        line_fadeouts[i] = 0.0f;
    }
}

void status_lines::init_late(void)
{
    line_offsets[STATUS_DEBUG] = 128.0f;
    line_offsets[STATUS_HOTBAR] = 80.0f;
}

void status_lines::layout(void)
{
    line_fonts[STATUS_DEBUG] = globals::font_unscii8;
    line_sizes[STATUS_DEBUG] = 8.0f;

    line_fonts[STATUS_HOTBAR] = globals::font_unscii16;
    line_sizes[STATUS_HOTBAR] = 16.0f;

    auto viewport = ImGui::GetMainViewport();
    auto draw_list = ImGui::GetForegroundDrawList();

    for(unsigned int i = 0U; i < STATUS_COUNT; ++i) {
        auto offset = line_offsets[i] * globals::gui_scale;
        auto& text = line_strings[i];
        auto* font = line_fonts[i];

        auto size = font->CalcTextSizeA(line_sizes[i] * globals::gui_scale, FLT_MAX, 0.0f, text.c_str(), text.c_str() + text.size());
        auto pos = ImVec2(0.5f * (viewport->Size.x - size.x), viewport->Size.y - offset);

        auto spawn = line_spawns[i];
        auto fadeout = line_fadeouts[i];
        auto alpha = std::exp(-1.0f * std::pow(1.0e-6f * static_cast<float>(globals::curtime - spawn) / fadeout, 10.0f));

        auto& color = line_text_colors[i];
        auto& shadow = line_shadow_colors[i];
        auto color_U32 = ImGui::GetColorU32(ImVec4(color.x, color.y, color.z, color.w * alpha));
        auto shadow_U32 = ImGui::GetColorU32(ImVec4(shadow.x, shadow.y, shadow.z, color.w * alpha));

        imutils::text_nw(draw_list, text, pos, color_U32, shadow_U32, font, line_sizes[i] * globals::gui_scale);
    }
}

void status_lines::set(unsigned int line, std::string_view text, const ImVec4& color, float fadeout)
{
    line_text_colors[line] = ImVec4(color.x, color.y, color.z, color.w);
    line_shadow_colors[line] = ImVec4(color.x * 0.1f, color.y * 0.1f, color.z * 0.1f, color.w);
    line_strings[line] = text;
    line_spawns[line] = globals::curtime;
    line_fadeouts[line] = fadeout;
}

void status_lines::unset(unsigned int line)
{
    line_text_colors[line] = ImVec4(0.0f, 0.0f, 0.0f, 0.0f);
    line_shadow_colors[line] = ImVec4(0.0f, 0.0f, 0.0f, 0.0f);
    line_strings[line] = std::string();
    line_spawns[line] = UINT64_C(0);
    line_fadeouts[line] = 0.0f;
}