Skip to content

Instantly share code, notes, and snippets.

@kudaba
Created April 29, 2020 18:53
Show Gist options
  • Save kudaba/7c932246debdb49ad7121022d6ddb04c to your computer and use it in GitHub Desktop.
Save kudaba/7c932246debdb49ad7121022d6ddb04c to your computer and use it in GitHub Desktop.
namespace ImGui
{
ImVec2 const AlignedCenter(0.5f, 0.5f);
ImVec2 const AlignedLeft(0.0f, 0.5f);
ImVec2 const AlignedTop(0.5f, 0.0f);
ImVec2 const AlignedRight(1.0f, 0.5f);
ImVec2 const AlignedBottom(0.5f, 1.0f);
ImVec2 const AlignedTopLeft(0.0f, 0.0f);
ImVec2 const AlignedTopRight(1.0f, 0.0f);
ImVec2 const AlignedBottomLeft(1.0f, 1.0f);
ImVec2 const AlignedBottomRight(1.0f, 1.0f);
ImVec2 AlignWindowPercent(ImVec2 aPosPercent, ImVec2 aSize, ImVec2 anAchorPercent)
{
GC_Vector2f availableSize = ToVec2f(GetContentRegionAvail());
GC_Vector2f startPos = ToVec2f(GetWindowPos());
GC_Vector2f size = ToVec2f(aSize);
GC_Vector2f offset = ToVec2f(anAchorPercent) * size;
GC_Vector2f pos = startPos + availableSize * ToVec2f(aPosPercent) - offset;
SetNextWindowPos(ToVec2(pos));
return ToVec2(size);
}
void SetNextAlignment(ImVec2 aPercentAlignment, ImVec2 aSize)
{
SetNextTextAlignment(nullptr, aPercentAlignment, aSize);
}
void SetNextTextAlignment(char const* someText, ImVec2 aPercentAlignment, ImVec2 aBorder)
{
// in some cases cursorpos is less than the content region (status/menu bar) try to preserve it
ImVec2 min = ImMin(ImGui::GetCursorPos(), ImGui::GetWindowContentRegionMin());
ImVec2 max = ImGui::GetWindowContentRegionMax();
if (someText && someText[0])
{
ImVec2 textSize = CalcTextSize(someText, someText + strlen(someText), false, max.x - min.x) + aBorder;
max -= textSize;
}
else
{
max -= aBorder;
}
ImVec2 cursorPos = ImGui::GetCursorPos();
if (aPercentAlignment.x != -FLT_MAX) // main
cursorPos.x = ImLerp(min.x, max.x, aPercentAlignment.x);
if (aPercentAlignment.y != -FLT_MAX)
cursorPos.y = ImLerp(min.y, max.y, aPercentAlignment.y);
ImGui::SetCursorPos(cursorPos);
}
void SetNextButtonAlignment(char const* someText, ImVec2 aPercentAlignment, ImVec2 aBorder)
{
SetNextTextAlignment(someText, aPercentAlignment, GetStyle().FramePadding * 2 + aBorder);
}
void AlignedText(char const* someText, ImVec2 aPercentAlignment, ImVec2 aBorder)
{
SetNextTextAlignment(someText, aPercentAlignment, aBorder);
ImGui::Text("%s", someText);
}
bool AlignedButton(char const* someText, ImVec2 aPercentAlignment, ImVec2 aBorder)
{
SetNextButtonAlignment(someText, aPercentAlignment, aBorder);
return ImGui::Button(someText);
}
}
namespace ImGui
{
extern ImVec2 const AlignedCenter;
extern ImVec2 const AlignedLeft;
extern ImVec2 const AlignedTop;
extern ImVec2 const AlignedRight;
extern ImVec2 const AlignedBottom;
extern ImVec2 const AlignedTopLeft;
extern ImVec2 const AlignedTopRight;
extern ImVec2 const AlignedBottomLeft;
extern ImVec2 const AlignedBottomRight;
float const NoAlign = -FLT_MAX;
ImVec2 AlignWindowPercent(ImVec2 aPosPercent, ImVec2 aSize, ImVec2 anAchorPercent = ImVec2(0.5f, 0.5f));
void SetNextAlignment(ImVec2 aPercentAlignment, ImVec2 aSize = ImVec2(0,0));
void SetNextTextAlignment(char const* someText, ImVec2 aPercentAlignment, ImVec2 aBorder = ImVec2(0,0));
void SetNextButtonAlignment(char const* someText, ImVec2 aPercentAlignment, ImVec2 aBorder = ImVec2(0,0));
inline void ResetAlignment() { SetNextAlignment(ImVec2(0,0)); }
void AlignedText(char const* someText, ImVec2 aPercentAlignment, ImVec2 aBorder = ImVec2(0,0));
bool AlignedButton(char const* someText, ImVec2 aPercentAlignment, ImVec2 aBorder = ImVec2(0,0));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment