summaryrefslogtreecommitdiffhomepage
path: root/editor/inspect.cpp
blob: f0a5574877a30c0c24609dd52c88232180f01569 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "inspect.hpp"
#include "compat/assert.hpp"
#include "compat/defs.hpp"
#include "entity/accessor.hpp"
#include "imgui-raii.hpp"
#include <Corrade/Containers/ArrayView.h>
#include <Corrade/Containers/String.h>
#include <Corrade/Containers/StringIterable.h>
#include <Magnum/Math/Vector.h>
#include <Magnum/Math/Vector2.h>
#include <Magnum/Math/Vector3.h>
#include <Magnum/Math/Vector4.h>
#include <algorithm>

namespace floormat::entities {

namespace {

using erased_constraints::is_magnum_vector;

String label_left(StringView label)
{
    float width = ImGui::CalcItemWidth(), x = ImGui::GetCursorPosX();
    ImGui::Text("%s", label.data());
    ImGui::SameLine();
    ImGui::SetCursorPosX(x + width*.5f + ImGui::GetStyle().ItemInnerSpacing.x);
    ImGui::SetNextItemWidth(-1);
    return ""_s.join(StringIterable({ "##"_s, label }));
}

template<typename T> struct IGDT_;
template<> struct IGDT_<std::uint8_t> : std::integral_constant<int, ImGuiDataType_U8> {};
template<> struct IGDT_<std::int8_t> : std::integral_constant<int, ImGuiDataType_S8> {};
template<> struct IGDT_<std::uint16_t> : std::integral_constant<int, ImGuiDataType_U16> {};
template<> struct IGDT_<std::int16_t> : std::integral_constant<int, ImGuiDataType_S16> {};
template<> struct IGDT_<std::uint32_t> : std::integral_constant<int, ImGuiDataType_U32> {};
template<> struct IGDT_<std::int32_t> : std::integral_constant<int, ImGuiDataType_S32> {};
template<> struct IGDT_<float> : std::integral_constant<int, ImGuiDataType_Float> {};
template<typename T> constexpr auto IGDT = IGDT_<T>::value;

using namespace imgui;
using namespace entities;

template<typename T> requires std::is_integral_v<T> constexpr bool eqv(T a, T b) { return a == b; }
inline bool eqv(float a, float b) { return std::fabs(a - b) < 1e-8f; }
inline bool eqv(const String& a, const String& b) { return a == b; }
template<typename T, std::size_t N> constexpr bool eqv(const Math::Vector<N, T>& a, const Math::Vector<N, T>& b) { return a == b; }

int corrade_string_resize_callback(ImGuiInputTextCallbackData* data)
{
    if (data->EventFlag == ImGuiInputTextFlags_CallbackResize)
    {
        auto* my_str = reinterpret_cast<String*>(data->UserData);
        fm_assert(my_str->begin() == data->Buf);
        String tmp = std::move(*my_str);
        *my_str = String{ValueInit, (std::size_t)data->BufSize};
        auto len = std::min(tmp.size(), my_str->size());
        for (std::size_t i = 0; i < len; i++)
            (*my_str)[i] = tmp[i];
        data->Buf = my_str->begin();
    }
    return 0;
}

template<typename T>
void do_inspect_field(void* datum, const erased_accessor& accessor, field_repr repr,
                      const ArrayView<const std::pair<StringView, std::size_t>>& list)
{
    if (list.isEmpty())
        fm_assert(accessor.check_field_type<T>());
    fm_assert(!list.isEmpty() == (repr == field_repr::cbx));

    bool should_disable;

    switch (accessor.is_enabled(datum))
    {
    using enum field_status;
    case hidden: return;
    case readonly: should_disable = true; break;
    case enabled: should_disable = false; break;
    }
    should_disable = should_disable || !accessor.can_write();
    [[maybe_unused]] auto disabler = begin_disabled(should_disable);
    bool ret = false;
    const auto label = label_left(accessor.field_name);
    T value{};
    accessor.read_fun(datum, accessor.reader, &value);
    auto orig = value;

    if constexpr(std::is_same_v<T, String>)
    {
        ret = ImGui::InputText(label.data(), value.begin(), value.size(), ImGuiInputTextFlags_CallbackResize, corrade_string_resize_callback, &value);
        if (auto max_len = accessor.get_max_length(datum); value.size() > max_len)
            value = value.prefix(max_len);
    }
    else if constexpr(std::is_same_v<T, bool>)
        ret = ImGui::Checkbox(label.data(), &value);
    else if constexpr (!is_magnum_vector<T>)
    {
        auto [min, max] = accessor.get_range(datum).convert<T>();
        constexpr auto igdt = IGDT<T>;
        T step(1), *step_ = !std::is_floating_point_v<T> ? &step : nullptr;
        switch (repr)
        {
        default: fm_warn_once("invalid repr enum value '%zu'", (std::size_t)repr); break;
        case field_repr::input:  ret = ImGui::InputScalar(label.data(), igdt, &value, &step_); break;
        case field_repr::slider: ret = ImGui::SliderScalar(label.data(), igdt, &value, &min, &max); break;
        case field_repr::drag:   ret = ImGui::DragScalar(label.data(), igdt, &value, 1, &min, &max); break;
        case field_repr::cbx: {
            if constexpr(std::is_integral_v<T>)
            {
                const char* preview = "<invalid>";
                const auto old_value = (std::size_t)static_cast<std::make_unsigned_t<T>>(value);
                for (const auto& [str, x] : list)
                    if (x == old_value)
                    {
                        preview = str.data();
                        break;
                    }
                for (auto b = begin_combo(label.data(), preview);
                     const auto& [str, x] : list)
                {
                    const bool is_selected = x == (std::size_t)old_value;
                    if (ImGui::Selectable(str.data(), is_selected))
                        value = T(x);
                    if (is_selected)
                        ImGui::SetItemDefaultFocus();
                }
                break;
            }
        }
        }
        value = std::clamp(value, min, max);
    }
    else
    {
        using U = typename T::Type;
        auto [min, max] = accessor.get_range(datum).convert<T>();
        constexpr auto igdt = IGDT<U>;
        T step = T(U(1)), *step_ = !std::is_floating_point_v<U> ? &step : nullptr;
        switch (repr)
        {
        default:
            fm_warn_once("invalid repr enum value '%zu'", (std::size_t)repr);
            break;
        case field_repr::input:
            ret = ImGui::InputScalarN(label.data(), igdt, &value, T::Size, &step_);
            break;
        case field_repr::drag:
            fm_warn_once("can't use imgui input drag mode for vector type");
            [[fallthrough]];
        case field_repr::slider:
            ret = ImGui::SliderScalarN(label.data(), igdt, &value, T::Size, &min, &max);
            break;
        }
        for (std::size_t i = 0; i < T::Size; i++)
            value[i] = std::clamp(value[i], min[i], max[i]);
    }

    ImGui::NewLine();

    if (ret && !should_disable && !eqv(value, orig))
        if (accessor.is_enabled(datum) >= field_status::enabled && accessor.can_write())
            accessor.write_fun(datum, accessor.writer, &value);
}

} // namespace

#define MAKE_SPEC(type, repr)                                                                                                   \
    template<>                                                                                                                  \
    void inspect_field<type>(void* datum, const erased_accessor& accessor,                                                      \
                             const ArrayView<const std::pair<StringView, std::size_t>>& list)                                   \
    {                                                                                                                           \
        do_inspect_field<type>(datum, accessor, (repr), list);                                                                  \
    }

#define MAKE_SPEC2(type, repr)                                                                                                  \
    template<>                                                                                                                  \
    void inspect_field<field_repr_<type, field_repr, repr>>(void* datum, const erased_accessor& accessor,                       \
                                                            const ArrayView<const std::pair<StringView, std::size_t>>& list)    \
    {                                                                                                                           \
        do_inspect_field<type>(datum, accessor, (repr), list);                                                                  \
    }

#define MAKE_SPEC_REPRS(type)                                                                                                   \
    MAKE_SPEC2(type, field_repr::input)                                                                                         \
    MAKE_SPEC2(type, field_repr::slider)                                                                                        \
    MAKE_SPEC2(type, field_repr::drag)                                                                                          \
    MAKE_SPEC(type, field_repr::input)

#define MAKE_SPEC_REPRS2(type)                                                                                                  \
    MAKE_SPEC_REPRS(Math::Vector2<type>)                                                                                        \
    MAKE_SPEC_REPRS(Math::Vector3<type>)                                                                                        \
    MAKE_SPEC_REPRS(Math::Vector4<type>)                                                                                        \
    MAKE_SPEC_REPRS(type)                                                                                                       \
    MAKE_SPEC2(type, field_repr::cbx)

MAKE_SPEC_REPRS2(std::uint8_t)
MAKE_SPEC_REPRS2(std::int8_t)
MAKE_SPEC_REPRS2(std::uint16_t)
MAKE_SPEC_REPRS2(std::int16_t)
MAKE_SPEC_REPRS2(std::uint32_t)
MAKE_SPEC_REPRS2(std::int32_t)
MAKE_SPEC_REPRS2(float)
MAKE_SPEC(bool, field_repr::input)
MAKE_SPEC(String, field_repr::input)

} // namespace floormat::entities