summaryrefslogtreecommitdiffhomepage
path: root/editor/imgui-misc.cpp
blob: 9add31b1b418020b7478406bb395d8f171b8b781 (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
#include "app.hpp"
#include "floormat/main.hpp"
#include "imgui-raii.hpp"
#include "compat/format.hpp"

namespace floormat {
using namespace floormat::imgui;

void app::draw_fps()
{
    const auto dpi = M->dpi_scale();
    const auto frame_time = M->smoothed_frame_time();
    char buf[16];
    const double hz = frame_time > 1e-6f ? (int)std::round(10./(double)frame_time + .05) * .1 : 9999;
    snformat(buf, "{:.1f} FPS"_cf, hz);
    const ImVec2 size = ImGui::CalcTextSize(buf);
    ImDrawList& draw = *ImGui::GetForegroundDrawList();
    draw.AddText(nullptr, ImGui::GetCurrentContext()->FontSize,
                 {M->window_size()[0] - size.x - 3.5f*dpi[0], 3*dpi[1]}, ImGui::ColorConvertFloat4ToU32({0, 1, 0, 1}), buf);
#if 0
    static auto timer = []{ Timeline t; t.start(); return t; }();
    if (timer.currentFrameDuration() >= 2)
    {
        timer.start();
        std::printf("FPS %f\n", (double)(frame_time > 1e-6f ? 1/frame_time : 0));
        std::fflush(stdout);
    }
#endif
}

void app::draw_tile_under_cursor()
{
    if (!cursor.tile)
        return;

    const auto dpi = M->dpi_scale();
    char buf[64];
    const auto coord = *cursor.tile;
    const auto chunk = coord.chunk();
    const auto local = coord.local();
    const auto z = coord.z();
    if (z == 0)
        snformat(buf, "{}x{} - {}:{}"_cf, chunk.x, chunk.y, local.x, local.y);
    else
        snformat(buf, "{}x{}:{} - {}:{}"_cf, chunk.x, chunk.y, (int)z, local.x, local.y);
    const auto size = ImGui::CalcTextSize(buf);
    const auto window_size = M->window_size();

    ImDrawList& draw = *ImGui::GetForegroundDrawList();
    draw.AddText(nullptr, ImGui::GetCurrentContext()->FontSize,
                 {window_size[0]*.5f - size.x/2, 3*dpi[1]}, (unsigned)-1, buf);
}

void app::draw_z_level()
{
    if (_z_level == 0)
        return;

    if (cursor.pixel && cursor.tile && !cursor.in_imgui)
    {
        const auto dpi = M->dpi_scale();
        const auto offset = Vector2(4, -3) * dpi;
        char buf[32];
        ImDrawList& draw = *ImGui::GetForegroundDrawList();
        snformat(buf, " +{:d}"_cf, _z_level);
        const auto font_size = ImGui::GetCurrentContext()->FontSize+3;
        auto shadow_offset = Vector2(1, 1)/* * dpi */;
        auto px = Vector2(*cursor.pixel) + offset, px2 = px + shadow_offset;
        draw.AddText(nullptr, font_size, {px2[0], px2[1]}, ImGui::ColorConvertFloat4ToU32({0, 0, 0, .75f}), buf);
        draw.AddText(nullptr, font_size, {px[0], px[1]}, ImGui::ColorConvertFloat4ToU32({1, 0, 1, 1}), buf);
    }
}

} // namespace floormat