dlxjj/imradv3
0263
1// Generated with imgui-designer 0.12// github.com/xyz3#include "ui_class_wizard.h"4#include "ui_new_field.h"5#include "ui_message_box.h"6#include <misc/cpp/imgui_stdlib.h>7#include <IconsFontAwesome6.h>8 9ClassWizard classWizard;10 11//todo: need to call our own copy otherwise ImGui thinks messageBox is not ClassWizard's child12//and hides CW when OpenPopup13//static MessageBox msgBox;14 15void ClassWizard::OpenPopup()16{17 requestClose = false;18 ImGui::OpenPopup(ID);19 Refresh();20}21 22void ClassWizard::ClosePopup()23{24 requestClose = true;25}26 27void ClassWizard::Refresh()28{29 className = codeGen->GetName();30 varName = codeGen->GetVName();31 32 std::string stype = className;33 if (stypeIdx < stypes.size())34 stype = stypes[stypeIdx];35 stypes = codeGen->GetStructTypes();36 stypes.insert(stypes.begin(), className);37 stypeIdx = stx::find(stypes, stype) - stypes.begin();38 if (stypeIdx == stypes.size())39 stypeIdx = 0;40 41 fields = codeGen->GetVars(stypeIdx ? stypes[stypeIdx] : "");42 stx::erase_if(fields, [](CppGen::Var& var) {43 //CppGen exports user code as-is so no modification allowed here44 if (var.flags & CppGen::Var::UserCode)45 return true;46 //skip member functions47 if (var.type.back() == ')')48 return true;49 return false;50 });51 52 used.clear();53 FindUsed(root, used);54}55 56void ClassWizard::FindUsed(UINode* node, std::vector<std::string>& used)57{58 for (int i = 0; i < 2; ++i)59 {60 const auto& props = i ? node->Events() : node->Properties();61 for (const auto& p : props) {62 if (!p.property)63 continue;64 auto vars = p.property->used_variables();65 for (const auto& var : vars) {66 assert(var.find_first_of("[.") == std::string::npos);67 used.push_back(var);68 }69 }70 }71 for (const auto& child : node->children)72 FindUsed(child.get(), used);73}74 75void ClassWizard::Draw()76{77 const float BWIDTH = 150;78 bool doRenameField = false;79 /// @begin TopWindow80 ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, { 12, 12 });81 ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, { 12, 5 });82 ImGui::SetNextWindowSize({ 800, 600 });83 ID = ImGui::GetID("Class Wizard");84 bool tmpOpen = true;85 if (ImGui::BeginPopupModal("Class Wizard", &tmpOpen, ImGuiWindowFlags_None))86 {87 if (requestClose)88 ImGui::CloseCurrentPopup();89 /// @separator90 91 newFieldPopup.Draw();92 messageBox.Draw(); //doesn't work from here93 94 /// @begin Text95 ImGui::Text("Class Name");96 /// @end Text97 98 /// @begin ComboBox99 ImGui::SetNextItemWidth(200);100 if (ImGui::BeginCombo("##className", stypes[stypeIdx].c_str()))101 {102 for (size_t i = 0; i < stypes.size(); ++i)103 {104 if (ImGui::Selectable(stypes[i].c_str(), i == stypeIdx)) {105 stypeIdx = i;106 Refresh();107 }108 }109 ImGui::EndCombo();110 }111 /// @end ComboBox112 113 /// @begin Button114 ImGui::SameLine();115 if (ImGui::Button(" Rename... "))116 {117 newFieldPopup.codeGen = codeGen;118 newFieldPopup.mode = stypeIdx ? NewFieldPopup::RenameStruct : NewFieldPopup::RenameWindow;119 newFieldPopup.varOldName = stypes[stypeIdx];120 newFieldPopup.OpenPopup([this] {121 *modified = true;122 stypes[stypeIdx] = newFieldPopup.varName; //Refresh will keep the selection123 Refresh();124 });125 }126 /// @end Button127 128 /// @being Button129 ImGui::SameLine();130 if (ImGui::Button(" Add New... "))131 {132 newFieldPopup.codeGen = codeGen;133 newFieldPopup.mode = NewFieldPopup::NewStruct;134 newFieldPopup.OpenPopup([this] {135 *modified = true;136 stypes.push_back(newFieldPopup.varName);137 stypeIdx = stypes.size() - 1; //Refresh will keep selection138 Refresh();139 });140 }141 /// @end Button142 143 /// @begin Child144 ImGui::Spacing();145 ImGui::Spacing();146 ImGui::BeginChild("child2551", { 0, -45 }, ImGuiChildFlags_Border | ImGuiChildFlags_NavFlattened, 0);147 float autoSizeW = (ImGui::GetContentRegionAvail().x - 150) / 1;148 ImGui::Columns(2, "", false);149 ImGui::SetColumnWidth(0, autoSizeW);150 ImGui::SetColumnWidth(1, BWIDTH + ImGui::GetStyle().ItemSpacing.x);151 152 /// @begin Text153 ImGui::Text("Fields");154 /// @end Text155 156 /// @begin Table157 if (ImGui::BeginTable("table", 4, ImGuiTableFlags_BordersInner | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_SizingStretchProp | ImGuiTableFlags_Resizable /*| ImGuiTableFlags_RowBg*/, { 0, -1 }))158 {159 ImGui::TableSetupColumn("Section", ImGuiTableColumnFlags_WidthFixed, 20);160 ImGui::TableSetupColumn("Name", ImGuiTableColumnFlags_WidthStretch, 1);161 ImGui::TableSetupColumn("Type", ImGuiTableColumnFlags_WidthStretch, 1);162 ImGui::TableSetupColumn("Init", ImGuiTableColumnFlags_WidthStretch, 1);163 ImGui::TableSetupScrollFreeze(0, 1);164 ImGui::TableHeadersRow();165 166 for (int i = 0; i < fields.size(); ++i)167 {168 ImGui::TableNextRow();169 ImGui::TableSetColumnIndex(0);170 ImGui::PushID(i);171 /// @separator172 173 const auto& var = fields[i];174 175 //ImGui::TableSetBgColor(ImGuiTableBgTarget_RowBg0, IM_COL32(255, 255, 255, 255));176 //ImGui::TableSetBgColor(ImGuiTableBgTarget_RowBg1, IM_COL32(192, 192, 192, 255));177 178 bool unused = !stx::count(used, var.name);179 if (unused)180 ImGui::PushStyleColor(ImGuiCol_Text, 0xff400040);181 182 const char* icon =183 (var.flags & CppGen::Var::Impl) ? ICON_FA_LOCK :184 (var.flags & CppGen::Var::Interface) ? ICON_FA_CUBE :185 "";186 const char* tooltip =187 (var.flags & CppGen::Var::Impl) ? "private" :188 (var.flags & CppGen::Var::Interface) ? "public" :189 "unknown";190 /// @begin Selectable191 if (ImGui::Selectable(icon, selRow == i, ImGuiSelectableFlags_SpanAllColumns))192 selRow = i;193 if (ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left) && ImGui::IsItemHovered())194 doRenameField = true;195 //if (ImGui::IsItemHovered() && ImGui::GetMousePos().x < 20)196 // ImGui::SetTooltip(tooltip);197 /// @end Selectable198 199 /// @begin Selectable200 ImGui::TableNextColumn();201 ImGui::Selectable(var.name.c_str(), selRow == i);202 /// @end Selectable203 204 /// @begin Selectable205 ImGui::TableNextColumn();206 ImGui::Selectable(var.type.c_str(), selRow == i);207 /// @end Selectable208 209 /// @begin Selectable210 ImGui::TableNextColumn();211 ImGui::Selectable(var.init.c_str(), selRow == i);212 /// @end Selectable213 214 if (unused)215 ImGui::PopStyleColor();216 217 /// @separator218 ImGui::PopID();219 }220 221 ImGui::EndTable();222 }223 /// @end Table224 225 /// @begin Button226 ImGui::NextColumn();227 if (ImGui::Button("Add Field...", { BWIDTH, 0 }))228 {229 newFieldPopup.codeGen = codeGen;230 newFieldPopup.mode = NewFieldPopup::NewField;231 newFieldPopup.scope = stypeIdx ? stypes[stypeIdx] : "";232 newFieldPopup.varType = "";233 newFieldPopup.OpenPopup([this] {234 *modified = true;235 Refresh();236 });237 }238 /// @end Button239 240 /// @begin Button241 ImRad::Spacing(4);242 ImGui::BeginDisabled(selRow < 0 || selRow >= (int)fields.size());243 if (ImGui::Button("Rename Field...", { BWIDTH, 0 }) ||244 (!ImRad::IsCurrentItemDisabled() && doRenameField))245 {246 newFieldPopup.codeGen = codeGen;247 newFieldPopup.mode = NewFieldPopup::RenameField;248 newFieldPopup.scope = stypeIdx ? stypes[stypeIdx] : "";249 newFieldPopup.varOldName = fields[selRow].name;250 newFieldPopup.OpenPopup([this] {251 *modified = true;252 root->RenameFieldVars(newFieldPopup.varOldName, newFieldPopup.varName);253 Refresh();254 });255 }256 ImGui::EndDisabled();257 /// @end Button258 259 /// @begin Button260 ImRad::Spacing(4);261 ImGui::BeginDisabled(selRow < 0 || selRow >= (int)fields.size());262 if (ImGui::Button("Remove Field", { BWIDTH, 0 }))263 {264 std::string name = fields[selRow].name;265 if (!stypeIdx && stx::count(used, name))266 {267 messageBox.title = "Remove variable";268 messageBox.message = "Remove used variable '" + name + "' ?";269 messageBox.buttons = ImRad::Yes | ImRad::No;270 messageBox.OpenPopup([this,name](ImRad::ModalResult mr) {271 if (mr == ImRad::Yes) {272 *modified = true;273 codeGen->RemoveVar(name);274 Refresh();275 }276 });277 }278 else279 {280 *modified = true;281 codeGen->RemoveVar(name, stypeIdx ? stypes[stypeIdx] : "");282 Refresh();283 }284 }285 ImGui::EndDisabled();286 /// @end Button287 288 /// @begin Button289 ImRad::Spacing(2);290 ImGui::BeginDisabled(stypeIdx);291 if (ImGui::Button("Remove Unused", { BWIDTH, 0 }))292 {293 for (const auto& fi : fields)294 {295 if (!stx::count(used, fi.name)) {296 *modified = true;297 codeGen->RemoveVar(fi.name);298 }299 }300 Refresh();301 }302 ImGui::EndDisabled();303 /// @end Button304 305 ImGui::EndChild();306 /// @end Child307 308 /// @begin Dummy309 ImRad::Spacing(2);310 ImGui::Dummy({ ImGui::GetContentRegionAvail().x - 110, 0 });311 /// @end Dummy312 313 /// @begin Button314 ImGui::SameLine(0, 0);315 if (ImGui::Button("OK", { 110, 30 }) || ImGui::Shortcut(ImGuiKey_Escape))316 {317 ClosePopup();318 }319 /// @end Button320 321 /// @separator322 ImGui::End();323 /// @end TopWindow324 }325 ImGui::PopStyleVar();326 ImGui::PopStyleVar();327}