dlxjj/imradv3
0262
1#include "ui_new_field.h"2#include "ui_message_box.h"3#include "cppgen.h"4#include <misc/cpp/imgui_stdlib.h>5#include <imgui_internal.h>6 7NewFieldPopup newFieldPopup;8 9void NewFieldPopup::OpenPopup(std::function<void()> clb)10{11 callback = clb;12 requestClose = false;13 ImGui::OpenPopup(ID);14 15 if (mode == NewStruct)16 varType = "struct";17 varTypeDisabled = varType != "";18 varTypeArray = varType == "[]";19 if (varTypeArray)20 varType = "std::vector<>";21 if (mode != NewEvent)22 varName = "";23 varInit = "";24 varPublic = mode == NewEvent ? false : true;25 if (mode == RenameField) {26 varName = varOldName;27 auto* var = codeGen->GetVar(varOldName);28 if (var) {29 varInit = var->init;30 varType = var->type;31 varPublic = var->flags & CppGen::Var::Interface;32 }33 }34 CheckVarName();35}36 37void NewFieldPopup::ClosePopup()38{39 requestClose = true;40}41 42void NewFieldPopup::Draw()43{44 std::string title = mode == NewField ? "New Field" :45 mode == NewStruct ? "New Struct" :46 mode == NewEvent ? "New Method" :47 mode == RenameField ? "Rename Field" :48 mode == RenameStruct ? "Rename Struct" :49 "Rename Window";50 title += "###NewFieldPopup";51 52 ID = ImGui::GetID("###NewFieldPopup");53 ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, { 12, 12 });54 ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, { 10, 10 });55 ImVec2 center = ImGui::GetMainViewport()->GetCenter();56 ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, { 0.5f, 0.5f });57 ImGui::SetNextWindowSizeConstraints({ 400, 100 }, { FLT_MAX, FLT_MAX });58 bool tmp = true;59 if (ImGui::BeginPopupModal(title.c_str(), &tmp, ImGuiWindowFlags_AlwaysAutoResize))60 {61 if (requestClose) {62 ImGui::CloseCurrentPopup();63 requestClose = false;64 }65 66 messageBox.Draw();67 68 bool change = false;69 70 if (mode == RenameField || mode == RenameStruct || mode == RenameWindow)71 {72 ImGui::Text("Old name");73 ImGui::BeginDisabled(true);74 ImGui::SetNextItemWidth(-1);75 ImGui::InputText("##varOldName", &varOldName, ImGuiInputTextFlags_CharsNoBlank);76 ImGui::EndDisabled();77 ImGui::Spacing();78 }79 80 ImGui::Text(81 mode == RenameWindow || mode == RenameStruct || mode == RenameField ? "New name" :82 mode == NewEvent ? "Method name" :83 mode == NewStruct ? "Struct name" :84 "Field name"85 );86 87 ImGui::SameLine();88 ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true);89 ImGui::PushStyleColor(ImGuiCol_Text, 0xff0000ff);90 ImGui::PushStyleVar(ImGuiStyleVar_SelectableTextAlign, { 1.0f, 0 });91 ImGui::Selectable((nameError + "##nameError").c_str(), false);92 ImGui::PopStyleVar();93 ImGui::PopItemFlag();94 ImGui::PopStyleColor();95 96 if (ImGui::IsWindowAppearing())97 ImGui::SetKeyboardFocusHere();98 ImGui::SetNextItemWidth(-1);99 if (ImGui::InputText("##varName", &varName, ImGuiInputTextFlags_CharsNoBlank))100 change = true;101 102 if (mode == NewField || mode == NewStruct || mode == NewEvent)103 {104 ImGui::Text("Field type");105 106 ImGui::SameLine();107 ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true);108 ImGui::PushStyleColor(ImGuiCol_Text, 0xff0000ff);109 ImGui::PushStyleVar(ImGuiStyleVar_SelectableTextAlign, { 1.0f, 0 });110 ImGui::Selectable((typeError + "##typeError").c_str(), false);111 ImGui::PopStyleVar();112 ImGui::PopStyleColor();113 ImGui::PopItemFlag();114 115 ImGui::BeginDisabled(varTypeDisabled);116 ImGui::SetNextItemWidth(-1);117 if (ImGui::InputText("##varType", &varType, ImGuiInputTextFlags_CharsNoBlank))118 change = true;119 ImGui::EndDisabled();120 }121 122 if (mode == NewField || mode == RenameField)123 {124 ImGui::Spacing();125 ImGui::Text("Initial value (optional)");126 ImGui::SetNextItemWidth(-1);127 if (ImGui::InputText("##init", &varInit, ImGuiInputTextFlags_CallbackCharFilter, DefaultCharFilter))128 change = true;129 }130 131 if (mode == NewField || mode == RenameField)132 {133 ImGui::Spacing();134 ImGui::Text("Access");135 if (ImGui::BeginChild("access", { -1, 0 }, ImGuiChildFlags_Borders | ImGuiChildFlags_AutoResizeY))136 {137 if (ImGui::RadioButton("Interface", varPublic))138 varPublic = true;139 ImGui::SameLine(0, 3 * ImGui::GetStyle().ItemSpacing.x);140 if (ImGui::RadioButton("Implementation", !varPublic))141 varPublic = false;142 ImGui::EndChild();143 }144 }145 146 if (change)147 {148 if (varTypeArray && varName != "") {149 std::string stype = (char)std::toupper(varName[0]) + varName.substr(1);150 if (stype.back() == 's' || stype.back() == 'S')151 stype.pop_back();152 varType = "std::vector<" + stype + ">";153 }154 155 CheckVarName();156 }157 158 ImGui::Spacing();159 ImGui::Spacing();160 161 ImGui::SetCursorPosX((ImGui::GetContentRegionAvail().x - 2 * 100));162 ImGui::BeginDisabled(nameError != "" || typeError != "");163 if (ImGui::Button("OK", { 100, 30 }))164 {165 CheckVarName();166 int flags = varPublic ? CppGen::Var::Interface : CppGen::Var::Impl;167 168 if (mode == NewField || mode == NewStruct || mode == NewEvent)169 {170 std::string type = mode == NewStruct ? "struct" : varType;171 auto ret = codeGen->CheckVarExpr(varName, type, scope);172 if (ret == CppGen::New_ImplicitStruct) {173 messageBox.title = "New";174 messageBox.message = "Create a new struct?";175 messageBox.buttons = ImRad::Yes | ImRad::No;176 messageBox.OpenPopup([this, type, flags](ImRad::ModalResult mr) {177 if (mr != ImRad::Yes)178 return;179 codeGen->CreateVarExpr(varName, type, varInit, flags, scope);180 ClosePopup();181 callback();182 });183 }184 else if (ret == CppGen::New) {185 codeGen->CreateVarExpr(varName, type, varInit, flags, scope);186 ClosePopup();187 callback();188 }189 }190 else if (mode == RenameField)191 {192 assert(varType != "");193 codeGen->ChangeVar(varOldName, varType, varInit, flags, scope);194 codeGen->RenameVar(varOldName, varName, scope);195 ClosePopup();196 callback();197 }198 else if (mode == RenameStruct)199 {200 codeGen->RenameStruct(varOldName, varName);201 ClosePopup();202 callback();203 }204 else if (mode == RenameWindow)205 {206 codeGen->SetNamesFromId(varName);207 ClosePopup();208 callback();209 }210 }211 ImGui::EndDisabled();212 213 ImGui::SameLine();214 if (ImGui::Button("Cancel", { 100, 30 }) || ImGui::Shortcut(ImGuiKey_Escape))215 {216 const auto& g = ImGui::GetCurrentContext();217 ClosePopup();218 }219 220 ImGui::EndPopup();221 }222 ImGui::PopStyleVar();223 ImGui::PopStyleVar();224}225 226void NewFieldPopup::CheckVarName()227{228 nameError = typeError = "";229 if (mode == RenameWindow)230 {231 if (!cpp::is_id(varName)) {232 nameError = "expected simple id";233 }234 }235 else if (mode == RenameStruct)236 {237 if (!cpp::is_id(varName)) {238 nameError = "expected simple id";239 }240 else if (varName != varOldName && stx::count(codeGen->GetStructTypes(), varName)) {241 nameError = "same field already exists";242 }243 }244 else if (mode == RenameField)245 {246 if (!cpp::is_id(varName)) {247 nameError = "expected simple id";248 }249 else if (varName != varOldName && codeGen->CheckVarExpr(varName, varType, scope) != CppGen::New) {250 nameError = "same field already exists";251 }252 }253 else if (mode == NewStruct || mode == NewEvent)254 {255 std::string type = mode == NewStruct ? "struct" : varType;256 if (!cpp::is_id(varName)) {257 nameError = "expected simple id";258 }259 else if (codeGen->CheckVarExpr(varName, type, scope) != CppGen::New) {260 nameError = "same field already exists";261 }262 }263 else if (mode == NewField)264 {265 switch (codeGen->CheckVarExpr(varName, varType, scope))266 {267 case CppGen::SyntaxError:268 nameError = "expected simple id";269 break;270 case CppGen::ConflictError:271 nameError = "field of different type already exists";272 break;273 case CppGen::Existing:274 nameError = "same field already exists";275 break;276 default:277 if (varTypeArray && !cpp::is_id(varName)) {278 nameError = "expected simple id";279 }280 else if (varType == "") {281 typeError = "invalid type";282 }283 break;284 }285 }286}287 