summaryrefslogtreecommitdiffhomepage
path: root/editor/tests.cpp
blob: a5c2be3c594504e036afefeae8e29e2df56cdd9f (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "tests-private.hpp"
#include "compat/safe-ptr.hpp"
#include "app.hpp"
#include "floormat/main.hpp"
#include "floormat/events.hpp"
#include "imgui-raii.hpp"
#define HAVE_LIBC 1
#include <SDL_keycode.h>

namespace floormat::tests {

static_assert(arraySize(tests_data::fields) == (size_t)Test::COUNT);

Pointer<base_test> tests_data::make_test_none() { return {}; }

} // namespace floormat::tests

namespace floormat {

using namespace floormat::tests;

tests_data_::~tests_data_() noexcept = default;
tests_data_::tests_data_() = default;

tests_data::~tests_data() noexcept = default;

base_test::~base_test() noexcept = default;
base_test::base_test() = default;

using namespace floormat::imgui;

void tests_data::switch_to(Test i)
{
    fm_assert((size_t)i < arraySize(fields));
    current_index = Test::none;
    current_test = make_test_none();
    if (i < Test::COUNT)
        current_test = tests_data::fields[(size_t)i].ctor();
    if (current_test)
        current_index = i;
}

safe_ptr<tests_data_> tests_data_::make()
{
    return safe_ptr<tests_data_>{new tests_data};
}

void app::tests_pre_update()
{
    if (auto& x = tests().current_test)
        x->update_pre(*this);
}

void app::tests_post_update()
{
    if (auto& x = tests().current_test)
        x->update_post(*this);
}

bool app::tests_handle_mouse_click(const mouse_button_event& e, bool is_down)
{
    update_cursor_tile(cursor.pixel);

    if (auto& x = tests().current_test)
        return x->handle_mouse_click(*this, e, is_down);
    else
        return false;
}

bool app::tests_handle_key(const key_event& e, bool is_down)
{
    if (auto& x = tests().current_test)
        return x->handle_key(*this, e, is_down);
    else
        return false;
}

bool app::tests_handle_mouse_move(const mouse_move_event& e)
{
    if (auto& x = tests().current_test)
        return x->handle_mouse_move(*this, e);
    else
        return false;
}

tests_data& app::tests()
{
    return static_cast<tests_data&>(*_tests);
}

void app::tests_reset_mode()
{
    auto mode = tests().current_index;
    tests().switch_to(Test::none);
    tests().switch_to(mode);
}

void app::draw_tests_pane(float width)
{
    ImGui::SeparatorText("Functional tests");
    auto& t = tests();

    constexpr int selectable_flags = ImGuiSelectableFlags_SpanAvailWidth;

    for (auto [str, id, ctor] : tests_data::fields)
        if (ImGui::Selectable(str.data(), id == t.current_index, selectable_flags))
            if (t.current_index != id)
                t.switch_to(id);

    if (t.current_test)
    {
        auto dpi = M->dpi_scale();
        ImGui::NewLine();
        ImGui::SeparatorEx(ImGuiSeparatorFlags_Horizontal, 2*dpi.y());
        auto b = push_id("###test-data");
        t.current_test->draw_ui(*this, width);
    }
}

void app::draw_tests_overlay()
{
    if (auto& x = tests().current_test)
        x->draw_overlay(*this);
}

} // namespace floormat