diff --git a/doc/screen1.png b/doc/screen1.png index 72ad812..7d468f7 100644 Binary files a/doc/screen1.png and b/doc/screen1.png differ diff --git a/src/binding.h b/src/binding.h index aef8642..07e3247 100644 --- a/src/binding.h +++ b/src/binding.h @@ -729,7 +729,9 @@ struct bindable : property_base os << val; str = os.str(); } - bool empty() const { return str.empty(); } + bool empty() const { + return str.empty(); + } bool zero() const { if (empty()) return false; diff --git a/src/cppgen.cpp b/src/cppgen.cpp index aee45a4..d6f2785 100644 --- a/src/cppgen.cpp +++ b/src/cppgen.cpp @@ -12,10 +12,10 @@ const std::string SPEC_FUN[] = { "Open", "Close", "OpenPopup", "ClosePopup", "ResetLayout", "Init", "Draw", }; -std::string_view CppGen::INDENT = " "; -std::string_view CppGen::FOR_VAR_NAME = "i"; -std::string_view CppGen::HBOX_NAME = "hb"; -std::string_view CppGen::VBOX_NAME = "vb"; +const std::string_view CppGen::INDENT = " "; +const std::string_view CppGen::FOR_VAR_NAME = "i"; +const std::string_view CppGen::HBOX_NAME = "hb"; +const std::string_view CppGen::VBOX_NAME = "vb"; CppGen::CppGen() : m_name("Untitled"), m_vname("untitled") diff --git a/src/cppgen.h b/src/cppgen.h index c99978b..c3a72fd 100644 --- a/src/cppgen.h +++ b/src/cppgen.h @@ -8,10 +8,10 @@ class CppGen { public: - static std::string_view INDENT; - static std::string_view FOR_VAR_NAME; - static std::string_view HBOX_NAME; - static std::string_view VBOX_NAME; + static const std::string_view INDENT; + static const std::string_view FOR_VAR_NAME; + static const std::string_view HBOX_NAME; + static const std::string_view VBOX_NAME; CppGen(); bool ExportUpdate(const std::string& fname, TopWindow* node, const std::map& params, std::string& err); diff --git a/src/imrad.cpp b/src/imrad.cpp index 1c251f7..f36d96a 100644 --- a/src/imrad.cpp +++ b/src/imrad.cpp @@ -1003,21 +1003,28 @@ void DockspaceUI() if (fileTabs.empty()) { - const std::string label = "Start by clicking the New File button "; - const std::string shortcut = "Ctrl + N"; - ImVec2 size = ImGui::CalcTextSize((label + shortcut).c_str()); - ImGui::SetCursorPos({ (viewport->WorkSize.x - size.x) / 2, (viewport->WorkSize.y - size.y) / 2 }); - ImVec4 clr = ImGui::GetStyleColorVec4(ImGuiCol_Text); - clr.w = 0.5f; - ImGui::PushStyleColor(ImGuiCol_Text, clr); - ImGui::TextUnformatted(label.c_str()); - ImGui::PopStyleColor(); - ImGui::SameLine(0, 0); - //clr = ImGui::GetStyleColorVec4(ImGuiCol_PlotHistogramHovered); - clr.w = 1.f; - ImGui::PushStyleColor(ImGuiCol_Text, clr); - ImGui::TextUnformatted(shortcut.c_str()); - ImGui::PopStyleColor(); + std::pair help[]{ + { "Create a New File", " Ctrl + N" }, + { "Open File", " Ctrl + O" } + }; + float lh = 2 * ImGui::GetTextLineHeight(); + for (size_t i = 0; i < std::size(help); ++i) { + ImVec2 size = ImGui::CalcTextSize((help[i].first + help[i].second).c_str()); + ImGui::SetCursorPos({ + viewport->WorkSize.x / 2.5f, + (viewport->WorkSize.y - std::size(help) * lh) / 2 + i * lh + }); + ImVec4 clr = ImGui::GetStyleColorVec4(ImGuiCol_Text); + clr.w = 0.5f; + ImGui::PushStyleColor(ImGuiCol_Text, clr); + ImGui::TextUnformatted(help[i].first.c_str()); + ImGui::PopStyleColor(); + ImGui::SameLine(0, 0); + clr.w = 1.f; + ImGui::PushStyleColor(ImGuiCol_Text, clr); + ImGui::TextUnformatted(help[i].second.c_str()); + ImGui::PopStyleColor(); + } } ImGui::End(); diff --git a/src/imrad.h b/src/imrad.h index 0b1eb7e..3e4b8cd 100644 --- a/src/imrad.h +++ b/src/imrad.h @@ -212,8 +212,11 @@ struct BoxLayout total += it.spacing; if (it.stretch) stretchTotal += it.size; - else if (it.size < 0) - total = avail + it.size; + else if (it.size < 0) { + //total = avail + it.size; + stretchTotal += 1.0; + total += -it.size; + } else total += it.size; } @@ -221,7 +224,8 @@ struct BoxLayout if (it.stretch) it.size = (float)(int)(it.size * (avail - total) / stretchTotal); else if (it.size < 0) - it.size += avail; + it.size = (float)(int)(1.0 * (avail - total) / stretchTotal); + //it.size += avail; } } //call after a widget call @@ -247,7 +251,7 @@ struct BoxLayout Item& it = items.back(); if (spacing > it.spacing) it.spacing = spacing; - if (!it.stretch && size > it.size) + if (!it.stretch && (size > it.size || (size < 0 && it.size > 0))) it.size = size; } void UpdateSize(float sp, Stretch size) diff --git a/src/node.cpp b/src/node.cpp index e67b7bc..5eb1c9d 100644 --- a/src/node.cpp +++ b/src/node.cpp @@ -720,32 +720,42 @@ void Widget::Draw(UIContext& ctx) { ctx.hovered = this; } - bool hoverSizeX = - (Behavior() & HasSizeX) && - ctx.selected.size() == 1 && ctx.selected[0] == this && - std::abs(ImGui::GetMousePos().x - cached_pos.x - cached_size.x) < 5 && - size_x.has_value(); - bool hoverSizeY = - (Behavior() & HasSizeY) && - ctx.selected.size() == 1 && ctx.selected[0] == this && - std::abs(ImGui::GetMousePos().y - cached_pos.y - cached_size.y) < 5 && - size_y.has_value(); - if (hoverSizeX || hoverSizeY) + int hoverMode = 0; + if ((Behavior() & HasSizeX) && size_x.has_value() && + ctx.selected.size() == 1 && ctx.selected[0] == this) { - ImGui::SetMouseCursor((hoverSizeX && hoverSizeY) ? ImGuiMouseCursor_ResizeNWSE : - hoverSizeX ? ImGuiMouseCursor_ResizeEW : - ImGuiMouseCursor_ResizeNS - ); + if (std::abs(ImGui::GetMousePos().x - cached_pos.x) < 5) + hoverMode |= UIContext::ItemSizingLeft; + else if (std::abs(ImGui::GetMousePos().x - cached_pos.x - cached_size.x) < 5) + hoverMode |= UIContext::ItemSizingRight; + } + if ((Behavior() & HasSizeY) && size_y.has_value() && + ctx.selected.size() == 1 && ctx.selected[0] == this) + { + if (std::abs(ImGui::GetMousePos().y - cached_pos.y) < 5) + hoverMode |= UIContext::ItemSizingTop; + if (std::abs(ImGui::GetMousePos().y - cached_pos.y - cached_size.y) < 5) + hoverMode |= UIContext::ItemSizingBottom; + } + if (hoverMode) + { + if ((hoverMode & UIContext::ItemSizingLeft) && (hoverMode & UIContext::ItemSizingBottom)) + ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeNESW); + else if ((hoverMode & UIContext::ItemSizingRight) && (hoverMode & UIContext::ItemSizingBottom)) + ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeNWSE); + else if (hoverMode & (UIContext::ItemSizingLeft | UIContext::ItemSizingRight)) + ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeEW); + else if (hoverMode & (UIContext::ItemSizingTop | UIContext::ItemSizingBottom)) + ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeNS); + if (ImGui::IsMouseDown(ImGuiMouseButton_Left)) { - ctx.mode = (hoverSizeX && hoverSizeY) ? UIContext::ItemSizingXY : - hoverSizeX ? UIContext::ItemSizingX : - UIContext::ItemSizingY; + ctx.mode = (UIContext::Mode)hoverMode; ctx.dragged = this; ctx.lastSize = { size_x.eval_px(ImGuiAxis_X, ctx), size_y.eval_px(ImGuiAxis_Y, ctx) }; - if (!ctx.lastSize.x) + if (ctx.lastSize.x <= 0) ctx.lastSize.x = cached_size.x; - if (!ctx.lastSize.y) + if (ctx.lastSize.y <= 0) ctx.lastSize.y = cached_size.y; } } @@ -792,31 +802,39 @@ void Widget::Draw(UIContext& ctx) ImGui::GetIO().MouseReleased[ImGuiMouseButton_Left] = false; //eat event } } - else if (ctx.mode >= UIContext::ItemSizingX && ctx.mode <= UIContext::ItemSizingXY && + else if ((ctx.mode & UIContext::ItemSizingMask) && allowed && ctx.dragged == this) { - ImGui::SetMouseCursor( - ctx.mode == UIContext::ItemSizingX ? ImGuiMouseCursor_ResizeEW : - ctx.mode == UIContext::ItemSizingY ? ImGuiMouseCursor_ResizeNS : - ImGuiMouseCursor_ResizeNWSE - ); + if ((ctx.mode & UIContext::ItemSizingLeft) && (ctx.mode & UIContext::ItemSizingBottom)) + ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeNESW); + else if ((ctx.mode & UIContext::ItemSizingRight) && (ctx.mode & UIContext::ItemSizingBottom)) + ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeNWSE); + else if (ctx.mode & (UIContext::ItemSizingLeft | UIContext::ItemSizingRight)) + ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeEW); + else if (ctx.mode & (UIContext::ItemSizingTop | UIContext::ItemSizingBottom)) + ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeNS); + if (ImGui::IsMouseDragging(ImGuiMouseButton_Left)) { *ctx.modified = true; - ImVec2 delta = ImGui::GetMouseDragDelta(); + ImVec2 delta = ImGui::GetMouseDragDelta() / ctx.zoomFactor; ImVec2 sp = ImGui::GetStyle().ItemSpacing; if (!sp.x) sp.x = 5; if (!sp.y) sp.y = 5; ImGuiWindow* win = ImGui::GetCurrentWindow(); - if (ctx.mode != UIContext::ItemSizingY) + if (ctx.mode & (UIContext::ItemSizingLeft | UIContext::ItemSizingRight)) { - float val = ctx.lastSize.x + delta.x / ctx.zoomFactor; + float val = ctx.lastSize.x + + ((ctx.mode & UIContext::ItemSizingLeft) ? -delta.x : delta.x); val = int(val / sp.x) * sp.x; - if (ctx.lastSize.x < 0 && !val) - val = -1; //snap to the end - if (ctx.lastSize.x > 0 && cached_pos.x + val >= win->WorkRect.Max.x) + if ((ctx.mode & UIContext::ItemSizingRight) && size_x.value() < 0 && delta.x > 0) + val = size_x.value(); //removes flicker + /*if (ctx.lastSize.x < 0 && !val) + val = -1; //snap to the end*/ + if ((ctx.mode & UIContext::ItemSizingRight) && + ctx.lastSize.x > 0 && delta.x > 0 && cached_pos.x + val >= win->WorkRect.Max.x) { const ImGuiTable* table = ImGui::GetCurrentTable(); const ImGuiTableColumn* column = table ? &table->Columns[table->CurrentColumn] : nullptr; @@ -831,14 +849,19 @@ void Widget::Draw(UIContext& ctx) } else if (std::signbit(ctx.lastSize.x) == std::signbit(val)) size_x = val; + } - if (ctx.mode != UIContext::ItemSizingX) + if (ctx.mode & (UIContext::ItemSizingTop | UIContext::ItemSizingBottom)) { - float val = ctx.lastSize.y + delta.y / ctx.zoomFactor; + float val = ctx.lastSize.y + + ((ctx.mode & UIContext::ItemSizingTop) ? -delta.y : delta.y); val = int(val / sp.y) * sp.y; - if (ctx.lastSize.y < 0 && !val) - val = -1; //snap to the end - if (ctx.lastSize.y > 0 && cached_pos.y + val >= win->WorkRect.Max.y) + if ((ctx.mode & UIContext::ItemSizingBottom) && size_y.value() < 0 && delta.y > 0) + val = size_y.value(); //removes flicker + /*if (ctx.lastSize.y < 0 && !val) + val = -1; //snap to the end*/ + if ((ctx.mode & UIContext::ItemSizingBottom) && + ctx.lastSize.y > 0 && delta.y > 0 && cached_pos.y + val >= win->WorkRect.Max.y) size_y = -1; //set auto size else if (std::signbit(ctx.lastSize.y) == std::signbit(val)) size_y = val; @@ -893,7 +916,7 @@ void Widget::Draw(UIContext& ctx) } if ((selected || ctx.hovered == this) && ctx.mode != UIContext::ItemDragging && - (ctx.mode < UIContext::ItemSizingX || ctx.mode > UIContext::ItemSizingXY)) + (ctx.mode & UIContext::ItemSizingMask) == 0) { //dl->PushClipRectFullScreen(); drawList->AddRect( diff --git a/src/uicontext.h b/src/uicontext.h index 709504f..ea89ed4 100644 --- a/src/uicontext.h +++ b/src/uicontext.h @@ -15,7 +15,10 @@ struct UIContext //set from outside enum Mode { NormalSelection, RectSelection, Snap, PickPoint, - ItemDragging, ItemSizingX, ItemSizingY, ItemSizingXY + ItemDragging, + ItemSizingLeft = 0x100, ItemSizingRight = 0x200, + ItemSizingTop = 0x400, ItemSizingBottom = 0x800, + ItemSizingMask = ItemSizingLeft | ItemSizingRight | ItemSizingTop | ItemSizingBottom, }; Mode mode = NormalSelection; std::vector selected;