summaryrefslogtreecommitdiffhomepage
path: root/editor/inspect.cpp
blob: 0609c4beb6774ee8616e0ab5f91c7bdb68d67a21 (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
209
210
211
#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 {

const char* label_left(StringView label, char* buf, size_t len)
{
    std::snprintf(buf, len, "##%s", label.data());
    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 buf;
}

template<typename T> struct IGDT_;
template<> struct IGDT_<uint8_t> : std::integral_constant<int, ImGuiDataType_U8> {};
template<> struct IGDT_<int8_t> : std::integral_constant<int, ImGuiDataType_S8> {};
template<> struct IGDT_<uint16_t> : std::integral_constant<int, ImGuiDataType_U16> {};
template<> struct IGDT_<int16_t> : std::integral_constant<int, ImGuiDataType_S16> {};
template<> struct IGDT_<uint32_t> : std::integral_constant<int, ImGuiDataType_U32> {};
template<> struct IGDT_<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, 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);
        *my_str = String{ValueInit, (size_t)data->BufSize};
        data->Buf = my_str->begin();
    }
    return 0;
}

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

    bool should_disable;
    char buf[128];

    switch (accessor.is_enabled(datum))
    {
    using enum field_status;
    case hidden: return false;
    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 char* const label = label_left(accessor.field_name, buf, sizeof buf);
    T value{};
    accessor.read_fun(datum, accessor.reader, &value);
    auto orig = value;

    if constexpr(std::is_same_v<T, StringView>)
        ret = ImGui::InputText(label, const_cast<char*>(value.data()), value.size(), ImGuiInputTextFlags_ReadOnly);
    else if constexpr(std::is_same_v<T, String>)
    {
        ret = ImGui::InputText(label, 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, &value);
    else if constexpr (!Math::IsVector<T>())
    {
        auto [min, max] = accessor.get_range(datum).convert<T>();
        constexpr auto igdt = IGDT<T>;
        constexpr T step(!std::is_floating_point_v<T> ? T(1) : T(1e-6f)),
                    step2(!std::is_floating_point_v<T> ? T(10) : T(1e-3f));
        switch (repr)
        {
        default: fm_warn_once("invalid repr enum value '%zu'", (size_t)repr); break;
        case field_repr::input:  ret = ImGui::InputScalar(label, igdt, &value, &step, &step2); break;
        case field_repr::slider: ret = ImGui::SliderScalar(label, igdt, &value, &min, &max); break;
        case field_repr::drag:   ret = ImGui::DragScalar(label, 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 = (size_t)static_cast<std::make_unsigned_t<T>>(value);
                for (const auto& [str, x] : list)
                    if (x == old_value)
                    {
                        preview = str.data();
                        break;
                    }
                if (auto b = begin_combo(label, preview))
                    for (const auto& [str, x] : list)
                    {
                        const bool is_selected = x == (size_t)old_value;
                        if (ImGui::Selectable(str.data(), is_selected))
                            value = T(x), ret = true;
                        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>;
        constexpr T step(!std::is_floating_point_v<U> ? U(1) : U(1e-6f)),
                    step2(!std::is_floating_point_v<U> ? U(10) : U(1e-3f));
        switch (repr)
        {
        default:
            fm_warn_once("invalid repr enum value '%zu'", (size_t)repr);
            break;
        case field_repr::input:
            ret = ImGui::InputScalarN(label, igdt, &value, T::Size, &step, &step2);
            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, igdt, &value, T::Size, &min, &max);
            break;
        }
        for (auto i = 0_uz; i < T::Size; i++)
            value[i] = std::clamp(value[i], min[i], max[i]);
    }

    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);
            return true;
        }
    return false;
}

} // namespace

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

#define MAKE_SPEC2(type, repr)                                                                                                  \
    template<>                                                                                                                  \
    bool inspect_field<field_repr_<type, field_repr, repr>>(void* datum, const erased_accessor& accessor,                       \
                                                            const ArrayView<const std::pair<StringView, size_t>>& list)    \
    {                                                                                                                           \
        return 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(uint8_t)
MAKE_SPEC_REPRS2(int8_t)
MAKE_SPEC_REPRS2(uint16_t)
MAKE_SPEC_REPRS2(int16_t)
MAKE_SPEC_REPRS2(uint32_t)
MAKE_SPEC_REPRS2(int32_t)
MAKE_SPEC_REPRS2(float)
MAKE_SPEC(bool, field_repr::input)
MAKE_SPEC(String, field_repr::input)
MAKE_SPEC(StringView, field_repr::input)

} // namespace floormat::entities