CoolFace
Datasetpublic

dlxjj/imradv3

sourceHugging Faceupdated 8mo agoView on Hugging Face
0likes263downloads
ui_binding.cpp270 linesDownload Raw Back to src
1// Generated with ImRAD 0.92// github.com/xyz3 4#include "ui_binding.h"5#include "ui_new_field.h"6#include "ui_message_box.h"7#include "utils.h"8 9BindingDlg bindingDlg;10 11 12void BindingDlg::OpenPopup(std::function<void(ImRad::ModalResult)> clb)13{14    callback = clb;15    modalResult = ImRad::None;16    auto *ioUserData = (ImRad::IOUserData *)ImGui::GetIO().UserData;17    ioUserData->dimBgRatio = 1.f;18    ImGui::OpenPopup(ID);19    Init();20}21 22void BindingDlg::ClosePopup(ImRad::ModalResult mr)23{24    modalResult = mr;25    auto *ioUserData = (ImRad::IOUserData *)ImGui::GetIO().UserData;26    ioUserData->dimBgRatio = 0.f;27}28 29void BindingDlg::Draw()30{31    /// @style Dark32    /// @unit px33    /// @begin TopWindow34    auto* ioUserData = (ImRad::IOUserData*)ImGui::GetIO().UserData;35    ID = ImGui::GetID("###BindingDlg");36    ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, { 10, 10 });37    ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, { 7, 7 });38    ImGui::SetNextWindowSize({ 600, 480 }, ImGuiCond_FirstUseEver); //{ 600, 480 }39    ImGui::SetNextWindowSizeConstraints({ 0, 0 }, { FLT_MAX, FLT_MAX });40    bool tmpOpen = true;41    if (ImGui::BeginPopupModal("Binding###BindingDlg", &tmpOpen, ImGuiWindowFlags_NoCollapse))42    {43        if (ioUserData->activeActivity != "")44            ImRad::RenderDimmedBackground(ioUserData->WorkRect(), ioUserData->dimBgRatio);45        if (modalResult != ImRad::None)46        {47            ImGui::CloseCurrentPopup();48            if (modalResult != ImRad::Cancel)49                callback(modalResult);50        }51        /// @separator52 53        newFieldPopup.Draw();54        messageBox.Draw();55 56        /// @begin Text57        hb1.BeginLayout();58        ImGui::PushStyleColor(ImGuiCol_Text, 0xff4d4dff);59        ImGui::TextUnformatted(ImRad::Format(" {}=", name).c_str());60        hb1.AddSize(0, ImRad::HBox::ItemSize);61        ImGui::PopStyleColor();62        /// @end Text63 64        /// @begin Spacer65        ImGui::SameLine(0, 1 * ImGui::GetStyle().ItemSpacing.x);66        ImRad::Dummy({ hb1.GetSize(), 20 });67        hb1.AddSize(1, ImRad::HBox::Stretch(1));68        /// @end Spacer69 70        /// @begin Text71        ImGui::SameLine(0, 1 * ImGui::GetStyle().ItemSpacing.x);72        ImGui::PushStyleColor(ImGuiCol_Text, ImGui::GetStyleColorVec4(ImGuiCol_TextDisabled));73        ImGui::TextUnformatted(ImRad::Format("{}", type + (forceReference ? " &" : "")).c_str());74        hb1.AddSize(1, ImRad::HBox::ItemSize);75        ImGui::PopStyleColor();76        /// @end Text77 78        /// @begin Input79        ImGui::PushFont(font);80        ImGui::SetNextItemWidth(-1);81        ImGui::InputText("##expr", &expr, ImGuiInputTextFlags_CallbackCharFilter, IMRAD_INPUTTEXT_EVENT(BindingDlg, OnTextInputFilter));82        if (ImGui::IsItemActive())83            ioUserData->imeType = ImRad::ImeText;84        ImGui::PopFont();85        if (ImGui::IsWindowAppearing())86            ImGui::SetKeyboardFocusHere(-1);87        if (focusExpr)88        {89            //forceFocus90            if (ImGui::IsItemFocused())91                focusExpr = false;92            ImGui::SetKeyboardFocusHere(-1);93        }94        /// @end Input95 96        /// @begin Text97        hb3.BeginLayout();98        ImRad::Spacing(1);99        ImGui::AlignTextToFramePadding();100        ImGui::TextUnformatted("Available fields");101        hb3.AddSize(0, ImRad::HBox::ItemSize);102        /// @end Text103 104        /// @begin Spacer105        ImGui::SameLine(0, 1 * ImGui::GetStyle().ItemSpacing.x);106        ImRad::Dummy({ hb3.GetSize(), 20 });107        hb3.AddSize(1, ImRad::HBox::Stretch(1));108        /// @end Spacer109 110        /// @begin CheckBox111        ImGui::SameLine(0, 1 * ImGui::GetStyle().ItemSpacing.x);112        if (ImGui::Checkbox("show all", &showAll))113            Refresh();114        hb3.AddSize(1, ImRad::HBox::ItemSize);115        /// @end CheckBox116 117        /// @begin Table118        if (ImGui::BeginTable("table1", 2, ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersInnerH | ImGuiTableFlags_BordersInnerV | ImGuiTableFlags_BordersOuterH | ImGuiTableFlags_BordersOuterV | ImGuiTableFlags_ScrollY, { 0, -48 }))119        {120            ImGui::TableSetupColumn("Name", 0, 0);121            ImGui::TableSetupColumn("Type", 0, 0);122            ImGui::TableSetupScrollFreeze(0, 1);123            ImGui::TableHeadersRow();124 125            for (int i = 0; i < vars.size(); ++i)126            {127                ImGui::PushID(i);128                ImGui::TableNextRow(0, 0);129                ImGui::TableSetColumnIndex(0);130                /// @separator131 132                /// @begin Selectable133                ImGui::PushStyleVar(ImGuiStyleVar_SelectableTextAlign, { 0, 0 });134                if (ImRad::Selectable(ImRad::Format("{}", vars[i].first).c_str(), false, ImGuiSelectableFlags_NoAutoClosePopups | ImGuiSelectableFlags_SpanAllColumns, { 0, 0 }))135                {136                    OnVarClicked();137                }138                ImGui::PopStyleVar();139                /// @end Selectable140 141                /// @begin Selectable142                ImRad::TableNextColumn(1);143                ImGui::BeginDisabled(true);144                ImGui::PushStyleVar(ImGuiStyleVar_SelectableTextAlign, { 0, 0 });145                ImRad::Selectable(ImRad::Format("{}", vars[i].second).c_str(), false, ImGuiSelectableFlags_NoAutoClosePopups, { 0, 0 });146                ImGui::PopStyleVar();147                ImGui::EndDisabled();148                /// @end Selectable149 150                /// @separator151                ImGui::PopID();152            }153            ImGui::EndTable();154        }155        /// @end Table156 157        /// @begin Button158        hb5.BeginLayout();159        ImRad::Spacing(1);160        if (ImGui::Button(" New Field... ", { 110, 30 }))161        {162            OnNewField();163        }164        hb5.AddSize(0, 110);165        /// @end Button166 167        /// @begin Spacer168        ImGui::SameLine(0, 1 * ImGui::GetStyle().ItemSpacing.x);169        ImRad::Dummy({ hb5.GetSize(), 20 });170        hb5.AddSize(1, ImRad::HBox::Stretch(1));171        /// @end Spacer172 173            bool exprValid = true;174            /*if (type == "bool" || type == "float" || type == "int")175                exprValid = stx::count_if(expr, [](char c) { return !std::isspace(c); });*/176            //combo.items requires carefully embedded nulls so disable editing here177            if (type == "std::vector<std::string>" &&178                (expr.empty() || expr[0] != '{' || expr.find('{', 1) != std::string::npos || expr.back() != '}'))179                exprValid = false;180            if (forceReference && expr.empty())181                exprValid = false;182 183        /// @begin Button184        ImGui::SameLine(0, 1 * ImGui::GetStyle().ItemSpacing.x);185        ImGui::BeginDisabled(!exprValid);186        if (ImGui::Button("OK", { 90, 30 }))187        {188            OkButton_Change();189        }190        hb5.AddSize(1, 90);191        ImGui::EndDisabled();192        /// @end Button193 194        /// @begin Button195        ImGui::SameLine(0, 1 * ImGui::GetStyle().ItemSpacing.x);196        if (ImGui::Button("Cancel", { 90, 30 }) ||197            ImGui::Shortcut(ImGuiKey_Escape))198        {199            ClosePopup(ImRad::Cancel);200        }201        hb5.AddSize(1, 90);202        /// @end Button203 204        /// @separator205        ImGui::EndPopup();206    }207    ImGui::PopStyleVar();208    ImGui::PopStyleVar();209    /// @end TopWindow210}211 212void BindingDlg::Refresh()213{214    vars = codeGen->GetVarExprs(showAll ? "" : type, true, curArray);215}216 217void BindingDlg::OnNewField()218{219    newFieldPopup.mode = NewFieldPopup::NewField;220    newFieldPopup.varType = ""; //allow all types f.e. vector<string> for taking its .size()221    newFieldPopup.scope = "";222    newFieldPopup.codeGen = codeGen;223    newFieldPopup.OpenPopup([this] {224        Refresh();225        });226}227 228void BindingDlg::OnVarClicked()229{230    int idx = ImGui::TableGetRowIndex() - 1; //header row231    if (type == "std::string")232        expr += "{" + vars[idx].first + "}";233    else if (type == "std::vector<std::string>")234        expr = "{" + vars[idx].first + "}";235    else236        expr = vars[idx].first;237    focusExpr = true;238}239 240int BindingDlg::OnTextInputFilter(ImGuiInputTextCallbackData& data)241{242    return DefaultCharFilter(&data);243}244 245void BindingDlg::Init()246{247    showAll = type == "std::string";248    Refresh();249}250 251void BindingDlg::ResetLayout()252{253    // ImGui::GetCurrentWindow()->HiddenFramesCannotSkipItems = 2;254    hb1.Reset();255    hb3.Reset();256    hb5.Reset();257}258 259void BindingDlg::OkButton_Change()260{261    if (forceReference && !cpp::is_lvalue(expr)) {262        messageBox.title = "Expression";263        messageBox.message = "\"" + name + "\" field requires an l-value expression";264        messageBox.buttons = ImRad::Ok;265        messageBox.OpenPopup();266        return;267    }268    ClosePopup(ImRad::Ok);269}270