#include "node_standard.h" #include "node_container.h" #include "node_extra.h" #include "stx.h" #include "cppgen.h" #include "binding_input.h" #include "binding_eval.h" #include "ui_message_box.h" #include "ui_combo_dlg.h" #include #include #include #include const std::string TMP_LAST_ITEM_VAR = "tmpLastItem"; void toggle(std::vector& c, UINode* val) { auto it = stx::find(c, val); if (it == c.end()) c.push_back(val); else c.erase(it); } // for compatibility with ImRAD 0.7 std::string ParseShortcutOld(const std::string& line) { std::string sh; size_t i = -1; while (true) { size_t j1 = line.find("ImGui::IsKeyPressed(ImGuiKey_", i + 1); size_t j2 = line.find("ImGuiMod_", i + 1); if (j1 == std::string::npos && j2 == std::string::npos) break; if (j1 < j2) { j1 += 29; size_t e = line.find_first_of(",)", j1); if (e == std::string::npos) break; sh += "+"; sh += line.substr(j1, e - j1); i = j1; } else { j2 += 9; sh += "+"; size_t end = std::find_if(line.begin() + j2, line.end(), [](char c) { return !std::isalpha(c); }) - line.begin(); sh += line.substr(j2, end - j2); i = j2; } } if (sh.size()) sh.erase(sh.begin()); return sh; } void TreeNodeProp(const char* name, ImFont* font, const std::string& label, std::function f) { ImVec2 pad = ImGui::GetStyle().FramePadding; ImGui::Unindent(); ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, { 0.0f, pad.y }); ImGui::PushStyleColor(ImGuiCol_NavCursor, { 0, 0, 0, 0 }); ImGui::SetNextItemAllowOverlap(); if (ImGui::TreeNodeEx(name, ImGuiTreeNodeFlags_SpanAllColumns)) { ImGui::PopStyleVar(); ImGui::Indent(); ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, { pad.x, 0 }); //row packing //ImGui::TableNextColumn(); //ImGui::Spacing(); f(); ImGui::PopStyleVar(); ImGui::TreePop(); ImGui::Unindent(); } else { ImGui::PopStyleVar(); ImGui::TableNextColumn(); ImGui::SetNextItemWidth(-ImGui::GetFrameHeight()); ImGui::PushFont(font); ImGui::Text("%s", label.c_str()); ImGui::PopFont(); } ImGui::Indent(); ImGui::PopStyleColor(); } //1. limits length of lengthy {} expression (like in case it contains ?:) //2. formats {{, }} into {, } //3. errors out on incorrect format string PreparedString PrepareString(std::string_view s) { const int n = 25; PreparedString ps; ps.error = false; ps.pos = ImGui::GetCursorScreenPos(); ps.pos.y += ImGui::GetCurrentWindow()->DC.CurrLineTextBaseOffset; ps.label.reserve(s.size() + 10); size_t argFrom = 0; for (size_t i = 0; i < s.size(); ++i) { if (s[i] == '{') { ps.label += "{"; if (i + 1 < s.size() && s[i + 1] == '{') ++i; else { if (argFrom) { ps.error = true; break; } argFrom = i + 1; } } else if (s[i] == '}') { if (!argFrom) { if (i + 1 == s.size() || s[i + 1] != '}') { ps.error = true; break; } ps.label += "}"; ++i; } else { if (i == argFrom) { ps.error = true; break; } if (i - argFrom > n) { ps.label += s.substr(argFrom, n - 3); while (ps.label.back() < 0) //strip incomplete unicode char ps.label.pop_back(); ps.label += "...}"; } else { ps.label += s.substr(argFrom, i - argFrom); ps.label += "}"; } ps.fmtArgs.push_back({ argFrom - 1, ps.label.size() }); argFrom = 0; } } else if (!argFrom) { ps.label += s[i]; } } if (argFrom) ps.error = true; if (ps.error) { ps.label = "error"; ps.fmtArgs.clear(); } return ps; } ImVec2 IncWrapText(const ImVec2& dpos, const char* s, const char* text_end, float wrap_width, float scale) { ImFont* font = ImGui::GetFont(); const float line_height = font->FontSize * scale; float line_width = dpos.x; float text_height = dpos.y; const char* word_wrap_eol = NULL; const char* real_end = s + strlen(s); while (s < text_end) { // Calculate how far we can render. Requires two passes on the string data but keeps the code simple and not intrusive for what's essentially an uncommon feature. if (!word_wrap_eol) { word_wrap_eol = font->CalcWordWrapPositionA(scale, s, real_end, wrap_width - line_width); } if (s >= word_wrap_eol) { text_height += line_height; line_width = 0.0f; word_wrap_eol = NULL; //Wrapping skips upcoming blanks while (s < text_end && ImCharIsBlankA(*s)) s++; if (*s == '\n') s++; continue; } // Decode and advance source const char* prev_s = s; unsigned int c = (unsigned int)*s; if (c < 0x80) s += 1; else s += ImTextCharFromUtf8(&c, s, text_end); if (c < 32) { if (c == '\n') { text_height += line_height; line_width = 0.0f; continue; } if (c == '\r') continue; } const float char_width = scale * ((int)c < font->IndexAdvanceX.Size ? font->IndexAdvanceX.Data[c] : font->FallbackAdvanceX); /*if (line_width + char_width >= max_width) { s = prev_s; break; }*/ line_width += char_width; } if (s == word_wrap_eol) { text_height += line_height; line_width = 0.0f; } return { line_width, text_height }; } void DrawTextArgs(const PreparedString& ps, UIContext& ctx, const ImVec2& offset, const ImVec2& size, const ImVec2& align) { if (ctx.beingResized) return; ImVec2 pos = ps.pos; uint32_t clr = ctx.colors[UIContext::Color::Selected]; clr = (clr & 0x00ffffff) | 0xb0000000; float wrapPos = ImGui::GetCurrentWindow()->DC.TextWrapPos; if (wrapPos < 0 || size.x || size.y || offset.x || offset.y) { if (align.x || align.y) { ImVec2 textSize = ImGui::CalcTextSize(ps.label.data(), ps.label.data() + ps.label.size()); ImVec2 sz = ImGui::CalcItemSize(size, textSize.x, textSize.y); ImVec2 dp{ (sz.x - textSize.x) * align.x, (sz.y - textSize.y) * align.y }; pos += dp; } pos += offset; size_t i = 0; for (const auto& arg : ps.fmtArgs) { pos.x += ImGui::CalcTextSize(ps.label.data() + i, ps.label.data() + arg.first).x; ImGui::GetWindowDrawList()->AddText(pos, clr, "{"); //ImVec2 sz = ImGui::CalcTextSize(ps.label.data() + arg.first, ps.label.data() + arg.second); //ImGui::GetWindowDrawList()->AddRectFilled(pos, pos + sz, 0x50808080); ImVec2 sz = ImGui::CalcTextSize(ps.label.data() + arg.first, ps.label.data() + arg.second - 1); pos.x += sz.x; ImGui::GetWindowDrawList()->AddText(pos, clr, "}"); i = arg.second - 1; } if (ps.error) ImGui::GetWindowDrawList()->AddText(pos, clr, ps.label.c_str()); } else { float wrapWidth = ImGui::CalcWrapWidthForPos(ps.pos, wrapPos); const char* text = ps.label.data(); ImVec2 dp{ 0, 0 }; for (const auto& arg : ps.fmtArgs) { const char* text_end = ps.label.data() + arg.first; dp = IncWrapText(dp, text, text_end, wrapWidth, ctx.zoomFactor); ImGui::GetWindowDrawList()->AddText(pos + dp, clr, "{"); text = text_end; text_end = ps.label.data() + arg.second - 1; dp = IncWrapText(dp, text, text_end, wrapWidth, ctx.zoomFactor); ImGui::GetWindowDrawList()->AddText(pos + dp, clr, "}"); text = text_end; } if (ps.error) ImGui::GetWindowDrawList()->AddText(pos, clr, ps.label.c_str()); } } //---------------------------------------------------- UINode::child_iterator::iter::iter() : children(), idx() {} UINode::child_iterator::iter::iter(children_type& ch, bool freePos) : children(&ch), freePos(freePos), idx() { while (!end() && !valid()) ++idx; } UINode::child_iterator::iter& UINode::child_iterator::iter::operator++ () { if (end()) return *this; ++idx; while (!end() && !valid()) ++idx; return *this; } UINode::child_iterator::iter UINode::child_iterator::iter::operator++ (int) { iter it(*this); ++(*this); return it; } bool UINode::child_iterator::iter::operator== (const iter& it) const { if (end() != it.end()) return false; if (!end()) return idx == it.idx; return true; } bool UINode::child_iterator::iter::operator!= (const iter& it) const { return !(*this == it); } UINode::child_iterator::children_type::value_type& UINode::child_iterator::iter::operator* () { static children_type::value_type dummy; if (end()) return dummy; return children->at(idx); } const UINode::child_iterator::children_type::value_type& UINode::child_iterator::iter::operator* () const { static children_type::value_type dummy; if (end()) return dummy; return children->at(idx); } size_t UINode::child_iterator::iter::index() const { return idx; } bool UINode::child_iterator::iter::end() const { return !children || idx >= children->size(); } bool UINode::child_iterator::iter::valid() const { if (end()) return false; bool fp = children->at(idx)->hasPos; return freePos == fp; } UINode::child_iterator::child_iterator(children_type& children, bool freePos) : children(children), freePos(freePos) {} UINode::child_iterator::iter UINode::child_iterator::begin() const { return iter(children, freePos); } UINode::child_iterator::iter UINode::child_iterator::end() const { return iter(); } UINode::child_iterator::operator bool() const { return begin() != end(); } //-------------------------------------------------------------------- void UINode::CloneChildrenFrom(const UINode& node, UIContext& ctx) { children.resize(node.children.size()); for (size_t i = 0; i < node.children.size(); ++i) children[i] = node.children[i]->Clone(ctx); } void UINode::DrawInteriorRect(UIContext& ctx) { size_t level = ctx.parents.size() - 1; ImDrawList* dl = ImGui::GetWindowDrawList(); int snapCount = UIContext::Color::COUNT - UIContext::Color::Snap1; dl->AddRect(cached_pos, cached_pos + cached_size, ctx.colors[UIContext::Snap1 + level % snapCount], 0, 0, 3); } void UINode::DrawSnap(UIContext& ctx) { ctx.snapNextColumn = 0; ctx.snapSameLine = false; ctx.snapUseNextSpacing = false; ctx.snapSetNextSameLine = false; const float MARGIN = 7; assert(ctx.parents.back() == this); size_t level = ctx.parents.size() - 1; int snapOp = Behavior(); ImVec2 m = ImGui::GetMousePos(); ImVec2 d1 = m - cached_pos; ImVec2 d2 = cached_pos + cached_size - m; float mind = std::min({ d1.x, d1.y, d2.x, d2.y }); //snap interior (first child) if ((snapOp & (SnapInterior | SnapItemInterior)) && mind >= 3 && //allow snapping sides with zero border !stx::count_if(children, [](const auto& ch) { return ch->Behavior() & SnapSides; })) { ctx.snapParent = this; child_iterator it(children, true); if (it) ctx.snapIndex = it.begin().index(); else ctx.snapIndex = children.size(); DrawInteriorRect(ctx); return; } if (!level || !(snapOp & SnapSides)) return; //snap side UINode* parent = ctx.parents[ctx.parents.size() - 2]; const auto& pchildren = parent->children; size_t i = stx::find_if(pchildren, [&](const auto& ch) { return ch.get() == this; }) - pchildren.begin(); if (i == pchildren.size()) return; UINode* clip = parent; if (clip->Behavior() & SnapGrandparentClip) clip = ctx.parents[ctx.parents.size() - 3]; if (m.x < clip->cached_pos.x || m.y < clip->cached_pos.y || m.x > clip->cached_pos.x + clip->cached_size.x || m.y > clip->cached_pos.y + clip->cached_size.y) return; int ncols = parent->ColumnCount(ctx); int col = 0; if (ncols > 1) { for (size_t j = 0; j <= i; ++j) col = (col + pchildren[j]->nextColumn) % ncols; } bool lastItem = i + 1 == pchildren.size(); ImGuiDir snapDir = ImGuiDir_None; //allow to snap in space between widgets too (don't check all coordinates) //works well with parent clip if (mind > MARGIN) snapDir = ImGuiDir_None; else if (d1.x == mind && d1.y >= 0 && d2.y >= 0) snapDir = ImGuiDir_Left; else if (d2.x == mind && d1.y >= 0 && d2.y >= 0) snapDir = ImGuiDir_Right; else if (d1.y == mind && d1.x >= 0 && d2.x >= 0) snapDir = ImGuiDir_Up; else if (d2.y == mind && d1.x >= 0 && d2.x >= 0) snapDir = ImGuiDir_Down; else if (lastItem && d2.x < 0 && d2.y < 0) snapDir = ImGuiDir_Down; if (snapDir == ImGuiDir_None) { if (ImRect(cached_pos, cached_pos + cached_size).Contains(m)) { //end of search with no result (interpreted by TopWindow) //children were already snapped so we can safely end search here ctx.snapParent = parent; ctx.snapIndex = -1; } return; } ImVec2 p; float w = 0, h = 0; //snapRight and snapDown will extend the checked area to the next widget switch (snapDir) { case ImGuiDir_Left: { p = cached_pos; h = cached_size.y; auto& ch = pchildren[i]; //check we are not pointing into widget on left const auto* lch = i && (pchildren[i - 1]->Behavior() & SnapSides) ? pchildren[i - 1].get() : nullptr; if (ncols > 1 && ch->nextColumn) { if (m.x < cached_pos.x) return; } if (ch->sameLine && lch) { float xm = (p.x + lch->cached_pos.x + lch->cached_size.x) / 2.f; //avg marker if (ch->spacing <= 1) { p.x = xm; h = std::max(h, lch->cached_size.y); } if (m.x < xm) return; } ctx.snapParent = parent; ctx.snapIndex = i; ctx.snapNextColumn = pchildren[i]->nextColumn; ctx.snapSameLine = pchildren[i]->sameLine; ctx.snapUseNextSpacing = true; ctx.snapSetNextSameLine = true; break; } case ImGuiDir_Right: { p = cached_pos + ImVec2(cached_size.x, 0); h = cached_size.y; //check we are not pointing into widget on right const Widget* rch = nullptr; if (i + 1 < pchildren.size() && !pchildren[i + 1]->nextColumn && pchildren[i + 1]->sameLine) rch = pchildren[i + 1].get(); if (rch) { float xm = (p.x + rch->cached_pos.x) / 2.f; //avg marker if (rch->spacing <= 1) { p.x = xm; h = std::max(h, rch->cached_size.y); } if (m.x > xm) return; } if (!rch && ncols > 1) { for (size_t j = i + 1; j < pchildren.size(); ++j) { if (pchildren[j]->nextColumn) { if ((col + pchildren[j]->nextColumn) % ncols > col) rch = pchildren[j].get(); break; } } if (rch && m.x >= rch->cached_pos.x) return; } const auto* nch = i + 1 < pchildren.size() ? pchildren[i + 1].get() : nullptr; ctx.snapParent = parent; ctx.snapIndex = i + 1; ctx.snapNextColumn = 0; ctx.snapSameLine = true; ctx.snapUseNextSpacing = false; break; } case ImGuiDir_Up: case ImGuiDir_Down: { bool down = snapDir == ImGuiDir_Down; p = cached_pos; if (down) p.y += cached_size.y; float x2 = p.x + cached_size.x; //find range of widgets in the same row and column size_t i1 = i, i2 = i; for (int j = (int)i - 1; j >= 0; --j) { if (ncols > 1 && pchildren[j + 1]->nextColumn) break; if (!pchildren[j + 1]->sameLine) break; const auto& ch = pchildren[j]; i1 = j; p.x = ch->cached_pos.x; if (down) p.y = std::max(p.y, ch->cached_pos.y + ch->cached_size.y); } for (size_t j = i + 1; j < pchildren.size(); ++j) { const auto& ch = pchildren[j]; if (ncols > 1 && ch->nextColumn) break; if (!ch->sameLine) break; i2 = j; x2 = ch->cached_pos.x + ch->cached_size.x; if (down) p.y = std::max(p.y, ch->cached_pos.y + ch->cached_size.y); } //find a widget from next/prev column-row size_t inr = -1, ipr = -1; if (ncols >= 2) { int nc = ncols - col; for (size_t j = i + 1; j < pchildren.size(); ++j) { nc -= pchildren[j]->nextColumn; if (nc <= 0) { inr = j; break; } } nc = ncols; for (int j = (int)i; j >= 0; --j) { if (nc <= 0) { ipr = j; break; } nc -= pchildren[j]->nextColumn; } } w = x2 - p.x; if (down) { //check m.y not pointing in next column-row const Widget* nch = inr < pchildren.size() ? pchildren[inr].get() : nullptr; if (nch) { if (m.y >= nch->cached_pos.y) return; } //check m.y not pointing in next row nch = i2 + 1 < pchildren.size() ? pchildren[i2 + 1].get() : nullptr; if (nch && (ncols <= 1 || !nch->nextColumn)) { if (m.y >= (p.y + nch->cached_pos.y) / 2.f) return; } ctx.snapParent = parent; ctx.snapIndex = i2 + 1; ctx.snapNextColumn = 0; ctx.snapSameLine = false; ctx.snapUseNextSpacing = false; } else { //check m.y is not pointing in previous column-row const Widget* pch = ipr < pchildren.size() ? pchildren[ipr].get() : nullptr; if (pch) { if (m.y <= pch->cached_pos.y + pch->cached_size.y) return; } //check m.y not pointing in prev row pch = i1 > 0 ? pchildren[i1 - 1].get() : nullptr; if (pch && (ncols <= 1 || !pchildren[i1]->nextColumn)) { if (m.y <= pch->cached_pos.y + pch->cached_size.y) return; } ctx.snapParent = parent; ctx.snapIndex = i1; ctx.snapSameLine = false; ctx.snapNextColumn = pchildren[i1]->nextColumn; ctx.snapUseNextSpacing = true; } break; } default: return; } ImDrawList* dl = ImGui::GetWindowDrawList(); dl->AddLine(p, p + ImVec2(w, h), ctx.colors[UIContext::Snap1 + (level - 1)], 3); } std::optional> UINode::FindChild(const UINode* ch) { if (ch == this) return std::pair{ nullptr, 0 }; for (size_t i = 0; i < children.size(); ++i) { const auto& child = children[i]; if (child.get() == ch) return std::pair{ this, (int)i }; auto tmp = child->FindChild(ch); if (tmp) return tmp; } return {}; } std::vector UINode::FindInRect(const ImRect& r) { std::vector sel; if (cached_size.x && cached_size.y && //skip contextMenu cached_pos.x > r.Min.x && cached_pos.y > r.Min.y && cached_pos.x + cached_size.x < r.Max.x && cached_pos.y + cached_size.y < r.Max.y) sel.push_back(this); for (const auto& child : children) { auto chsel = child->FindInRect(r); sel.insert(sel.end(), chsel.begin(), chsel.end()); } return sel; } std::vector UINode::GetAllChildren() { std::vector chs; chs.reserve(children.size() * 2); chs.push_back(this); for (const auto& child : children) { auto vec = child->GetAllChildren(); chs.insert(chs.end(), vec.begin(), vec.end()); } return chs; } //this must return exact class id std::string UINode::GetTypeName() { std::string name = typeid(*this).name(); //erase "struct" auto i = name.rfind(' '); if (i != std::string::npos) name.erase(0, i + 1); //erase encoded name length auto it = stx::find_if(name, [](char c) { return isalpha(c); }); if (it != name.end()) name.erase(0, it - name.begin()); return name; } int GetTotalIndex(UINode* parent, UINode* child) { std::string tname = child->GetTypeName(); int idx = 0; for (const auto& ch : parent->children) { if (ch.get() == child) return idx; idx += GetTotalIndex(ch.get(), child); if (ch->GetTypeName() == tname) ++idx; } return idx; } void UINode::PushError(UIContext& ctx, const std::string& err) { std::string name = GetTypeName(); if (this != ctx.root) { int idx = GetTotalIndex(ctx.parents[0], this); name = name + " #" + std::to_string(idx+1); } ctx.errors.push_back(name + " : " + err); } std::string UINode::GetParentIndexes(UIContext& ctx) { std::string id; UINode* node = ctx.parents.back(); for (auto it = ++ctx.parents.rbegin(); it != ctx.parents.rend(); ++it) { size_t i = stx::find_if((*it)->children, [=](const auto& ch) { return ch.get() == node; }) - (*it)->children.begin(); id = std::to_string(i) + id; node = *it; } return id; } void UINode::ResetLayout() { hbox.clear(); vbox.clear(); for (auto& ch : children) ch->ResetLayout(); } std::vector UINode::UsedFieldVars() { std::vector used; auto props = Properties(); for (auto& p : props) { if (!p.property) continue; auto us = p.property->used_variables(); used.insert(used.end(), us.begin(), us.end()); } for (auto& child : children) { auto us = child->UsedFieldVars(); used.insert(used.end(), us.begin(), us.end()); } stx::sort(used); used.erase(stx::unique(used), used.end()); return used; } void UINode::RenameFieldVars(const std::string& oldn, const std::string& newn) { for (int i = 0; i < 2; ++i) { auto props = i ? Events() : Properties(); for (auto& p : props) { if (!p.property) continue; p.property->rename_variable(oldn, newn); } } for (auto& child : children) child->RenameFieldVars(oldn, newn); } //---------------------------------------------------- std::unique_ptr Widget::Create(const std::string& name, UIContext& ctx) { if (name == "Text") return std::make_unique(ctx); else if (name == "Selectable") return std::make_unique(ctx); else if (name == "Button") return std::make_unique