File size: 8,744 Bytes
17f5b5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Generated with ImRAD 0.9
// github.com/xyz

#include "ui_binding.h"
#include "ui_new_field.h"
#include "ui_message_box.h"
#include "utils.h"

BindingDlg bindingDlg;


void BindingDlg::OpenPopup(std::function<void(ImRad::ModalResult)> clb)
{
    callback = clb;
    modalResult = ImRad::None;
    auto *ioUserData = (ImRad::IOUserData *)ImGui::GetIO().UserData;
    ioUserData->dimBgRatio = 1.f;
    ImGui::OpenPopup(ID);
    Init();
}

void BindingDlg::ClosePopup(ImRad::ModalResult mr)
{
    modalResult = mr;
    auto *ioUserData = (ImRad::IOUserData *)ImGui::GetIO().UserData;
    ioUserData->dimBgRatio = 0.f;
}

void BindingDlg::Draw()
{
    /// @style Dark
    /// @unit px
    /// @begin TopWindow
    auto* ioUserData = (ImRad::IOUserData*)ImGui::GetIO().UserData;
    ID = ImGui::GetID("###BindingDlg");
    ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, { 10, 10 });
    ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, { 7, 7 });
    ImGui::SetNextWindowSize({ 600, 480 }, ImGuiCond_FirstUseEver); //{ 600, 480 }
    ImGui::SetNextWindowSizeConstraints({ 0, 0 }, { FLT_MAX, FLT_MAX });
    bool tmpOpen = true;
    if (ImGui::BeginPopupModal("Binding###BindingDlg", &tmpOpen, ImGuiWindowFlags_NoCollapse))
    {
        if (ioUserData->activeActivity != "")
            ImRad::RenderDimmedBackground(ioUserData->WorkRect(), ioUserData->dimBgRatio);
        if (modalResult != ImRad::None)
        {
            ImGui::CloseCurrentPopup();
            if (modalResult != ImRad::Cancel)
                callback(modalResult);
        }
        /// @separator

        newFieldPopup.Draw();
        messageBox.Draw();

        /// @begin Text
        hb1.BeginLayout();
        ImGui::PushStyleColor(ImGuiCol_Text, 0xff4d4dff);
        ImGui::TextUnformatted(ImRad::Format(" {}=", name).c_str());
        hb1.AddSize(0, ImRad::HBox::ItemSize);
        ImGui::PopStyleColor();
        /// @end Text

        /// @begin Spacer
        ImGui::SameLine(0, 1 * ImGui::GetStyle().ItemSpacing.x);
        ImRad::Dummy({ hb1.GetSize(), 20 });
        hb1.AddSize(1, ImRad::HBox::Stretch(1));
        /// @end Spacer

        /// @begin Text
        ImGui::SameLine(0, 1 * ImGui::GetStyle().ItemSpacing.x);
        ImGui::PushStyleColor(ImGuiCol_Text, ImGui::GetStyleColorVec4(ImGuiCol_TextDisabled));
        ImGui::TextUnformatted(ImRad::Format("{}", type + (forceReference ? " &" : "")).c_str());
        hb1.AddSize(1, ImRad::HBox::ItemSize);
        ImGui::PopStyleColor();
        /// @end Text

        /// @begin Input
        ImGui::PushFont(font);
        ImGui::SetNextItemWidth(-1);
        ImGui::InputText("##expr", &expr, ImGuiInputTextFlags_CallbackCharFilter, IMRAD_INPUTTEXT_EVENT(BindingDlg, OnTextInputFilter));
        if (ImGui::IsItemActive())
            ioUserData->imeType = ImRad::ImeText;
        ImGui::PopFont();
        if (ImGui::IsWindowAppearing())
            ImGui::SetKeyboardFocusHere(-1);
        if (focusExpr)
        {
            //forceFocus
            if (ImGui::IsItemFocused())
                focusExpr = false;
            ImGui::SetKeyboardFocusHere(-1);
        }
        /// @end Input

        /// @begin Text
        hb3.BeginLayout();
        ImRad::Spacing(1);
        ImGui::AlignTextToFramePadding();
        ImGui::TextUnformatted("Available fields");
        hb3.AddSize(0, ImRad::HBox::ItemSize);
        /// @end Text

        /// @begin Spacer
        ImGui::SameLine(0, 1 * ImGui::GetStyle().ItemSpacing.x);
        ImRad::Dummy({ hb3.GetSize(), 20 });
        hb3.AddSize(1, ImRad::HBox::Stretch(1));
        /// @end Spacer

        /// @begin CheckBox
        ImGui::SameLine(0, 1 * ImGui::GetStyle().ItemSpacing.x);
        if (ImGui::Checkbox("show all", &showAll))
            Refresh();
        hb3.AddSize(1, ImRad::HBox::ItemSize);
        /// @end CheckBox

        /// @begin Table
        if (ImGui::BeginTable("table1", 2, ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersInnerH | ImGuiTableFlags_BordersInnerV | ImGuiTableFlags_BordersOuterH | ImGuiTableFlags_BordersOuterV | ImGuiTableFlags_ScrollY, { 0, -48 }))
        {
            ImGui::TableSetupColumn("Name", 0, 0);
            ImGui::TableSetupColumn("Type", 0, 0);
            ImGui::TableSetupScrollFreeze(0, 1);
            ImGui::TableHeadersRow();

            for (int i = 0; i < vars.size(); ++i)
            {
                ImGui::PushID(i);
                ImGui::TableNextRow(0, 0);
                ImGui::TableSetColumnIndex(0);
                /// @separator

                /// @begin Selectable
                ImGui::PushStyleVar(ImGuiStyleVar_SelectableTextAlign, { 0, 0 });
                if (ImRad::Selectable(ImRad::Format("{}", vars[i].first).c_str(), false, ImGuiSelectableFlags_NoAutoClosePopups | ImGuiSelectableFlags_SpanAllColumns, { 0, 0 }))
                {
                    OnVarClicked();
                }
                ImGui::PopStyleVar();
                /// @end Selectable

                /// @begin Selectable
                ImRad::TableNextColumn(1);
                ImGui::BeginDisabled(true);
                ImGui::PushStyleVar(ImGuiStyleVar_SelectableTextAlign, { 0, 0 });
                ImRad::Selectable(ImRad::Format("{}", vars[i].second).c_str(), false, ImGuiSelectableFlags_NoAutoClosePopups, { 0, 0 });
                ImGui::PopStyleVar();
                ImGui::EndDisabled();
                /// @end Selectable

                /// @separator
                ImGui::PopID();
            }
            ImGui::EndTable();
        }
        /// @end Table

        /// @begin Button
        hb5.BeginLayout();
        ImRad::Spacing(1);
        if (ImGui::Button(" New Field... ", { 110, 30 }))
        {
            OnNewField();
        }
        hb5.AddSize(0, 110);
        /// @end Button

        /// @begin Spacer
        ImGui::SameLine(0, 1 * ImGui::GetStyle().ItemSpacing.x);
        ImRad::Dummy({ hb5.GetSize(), 20 });
        hb5.AddSize(1, ImRad::HBox::Stretch(1));
        /// @end Spacer

            bool exprValid = true;
            /*if (type == "bool" || type == "float" || type == "int")
                exprValid = stx::count_if(expr, [](char c) { return !std::isspace(c); });*/
            //combo.items requires carefully embedded nulls so disable editing here
            if (type == "std::vector<std::string>" &&
                (expr.empty() || expr[0] != '{' || expr.find('{', 1) != std::string::npos || expr.back() != '}'))
                exprValid = false;
            if (forceReference && expr.empty())
                exprValid = false;

        /// @begin Button
        ImGui::SameLine(0, 1 * ImGui::GetStyle().ItemSpacing.x);
        ImGui::BeginDisabled(!exprValid);
        if (ImGui::Button("OK", { 90, 30 }))
        {
            OkButton_Change();
        }
        hb5.AddSize(1, 90);
        ImGui::EndDisabled();
        /// @end Button

        /// @begin Button
        ImGui::SameLine(0, 1 * ImGui::GetStyle().ItemSpacing.x);
        if (ImGui::Button("Cancel", { 90, 30 }) ||
            ImGui::Shortcut(ImGuiKey_Escape))
        {
            ClosePopup(ImRad::Cancel);
        }
        hb5.AddSize(1, 90);
        /// @end Button

        /// @separator
        ImGui::EndPopup();
    }
    ImGui::PopStyleVar();
    ImGui::PopStyleVar();
    /// @end TopWindow
}

void BindingDlg::Refresh()
{
    vars = codeGen->GetVarExprs(showAll ? "" : type, true, curArray);
}

void BindingDlg::OnNewField()
{
    newFieldPopup.mode = NewFieldPopup::NewField;
    newFieldPopup.varType = ""; //allow all types f.e. vector<string> for taking its .size()
    newFieldPopup.scope = "";
    newFieldPopup.codeGen = codeGen;
    newFieldPopup.OpenPopup([this] {
        Refresh();
        });
}

void BindingDlg::OnVarClicked()
{
    int idx = ImGui::TableGetRowIndex() - 1; //header row
    if (type == "std::string")
        expr += "{" + vars[idx].first + "}";
    else if (type == "std::vector<std::string>")
        expr = "{" + vars[idx].first + "}";
    else
        expr = vars[idx].first;
    focusExpr = true;
}

int BindingDlg::OnTextInputFilter(ImGuiInputTextCallbackData& data)
{
    return DefaultCharFilter(&data);
}

void BindingDlg::Init()
{
    showAll = type == "std::string";
    Refresh();
}

void BindingDlg::ResetLayout()
{
    // ImGui::GetCurrentWindow()->HiddenFramesCannotSkipItems = 2;
    hb1.Reset();
    hb3.Reset();
    hb5.Reset();
}

void BindingDlg::OkButton_Change()
{
    if (forceReference && !cpp::is_lvalue(expr)) {
        messageBox.title = "Expression";
        messageBox.message = "\"" + name + "\" field requires an l-value expression";
        messageBox.buttons = ImRad::Ok;
        messageBox.OpenPopup();
        return;
    }
    ClosePopup(ImRad::Ok);
}