Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tpecholt committed Aug 29, 2024
1 parent a406496 commit 71523f7
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 67 deletions.
Binary file modified doc/screen1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion src/binding.h
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,9 @@ struct bindable<dimension> : 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;
Expand Down
8 changes: 4 additions & 4 deletions src/cppgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
8 changes: 4 additions & 4 deletions src/cppgen.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::string>& params, std::string& err);
Expand Down
37 changes: 22 additions & 15 deletions src/imrad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::string> 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();
Expand Down
12 changes: 8 additions & 4 deletions src/imrad.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,20 @@ 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;
}
for (Item& it : prevItems) {
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
Expand All @@ -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)
Expand Down
99 changes: 61 additions & 38 deletions src/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
5 changes: 4 additions & 1 deletion src/uicontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<UINode*> selected;
Expand Down

0 comments on commit 71523f7

Please sign in to comment.