Skip to content

Commit

Permalink
Add custom ImGui LabelText implementation for right alignment
Browse files Browse the repository at this point in the history
Signed-off-by: falkTX <[email protected]>
  • Loading branch information
falkTX committed May 6, 2024
1 parent aaf9c67 commit ed26b9a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
46 changes: 46 additions & 0 deletions opengl/DearImGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,3 +596,49 @@ template class ImGuiWidget<StandaloneWindow>;
// --------------------------------------------------------------------------------------------------------------------

END_NAMESPACE_DGL

// --------------------------------------------------------------------------------------------------------------------
// extra ImGui calls

namespace ImGui {

// --------------------------------------------------------------------------------------------------------------------

void RightAlignedLabelText(const char* label, const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
RightAlignedLabelTextV(label, fmt, args);
va_end(args);
}

void RightAlignedLabelTextV(const char* label, const char* fmt, va_list args)
{
ImGuiWindow* window = GetCurrentWindow();
if (window->SkipItems)
return;

ImGuiContext& g = *GImGui;
const ImGuiStyle& style = g.Style;
const float w = CalcItemWidth();

const char* value_text_begin, *value_text_end;
ImFormatStringToTempBufferV(&value_text_begin, &value_text_end, fmt, args);
const ImVec2 value_size = CalcTextSize(value_text_begin, value_text_end, false);
const ImVec2 label_size = CalcTextSize(label, NULL, true);

const ImVec2 pos = window->DC.CursorPos;
const ImRect value_bb(pos, pos + ImVec2(w, value_size.y + style.FramePadding.y * 2));
const ImRect total_bb(pos, pos + ImVec2(w + (label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f), ImMax(value_size.y, label_size.y) + style.FramePadding.y * 2));
ItemSize(total_bb, style.FramePadding.y);
if (!ItemAdd(total_bb, 0))
return;

// Render
RenderTextClipped(value_bb.Max - value_size - style.FramePadding, value_bb.Max, value_text_begin, value_text_end, &value_size, ImVec2(0.0f, 0.0f));
RenderText(ImVec2(value_bb.Max.x + style.ItemInnerSpacing.x, value_bb.Min.y + style.FramePadding.y), label);
}

// --------------------------------------------------------------------------------------------------------------------

}
15 changes: 15 additions & 0 deletions opengl/DearImGui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,18 @@ typedef ImGuiWidget<StandaloneWindow> ImGuiStandaloneWindow;
// --------------------------------------------------------------------------------------------------------------------

END_NAMESPACE_DGL

// --------------------------------------------------------------------------------------------------------------------
// extra ImGui calls

namespace ImGui {

// --------------------------------------------------------------------------------------------------------------------
// custom ImGui LabelText implementation for right alignment

void RightAlignedLabelText(const char* label, const char* fmt, ...);
void RightAlignedLabelTextV(const char* label, const char* fmt, va_list args);

// --------------------------------------------------------------------------------------------------------------------

}
2 changes: 1 addition & 1 deletion opengl/LVGL.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ START_NAMESPACE_DGL
This class exposes the LVGL drawing API inside a DGL Widget.
This class will take care of setting up LVGL for drawing,
and also also user input, resizes and everything in between.
and also user input, resizes and everything in between.
*/
template <class BaseWidget>
class LVGLWidget : public BaseWidget,
Expand Down

0 comments on commit ed26b9a

Please sign in to comment.