diff --git a/src/gui/input.cc b/src/gui/input.cc index ccbe6870..add939c9 100644 --- a/src/gui/input.cc +++ b/src/gui/input.cc @@ -3,6 +3,8 @@ #include #include +#include "absl/strings/string_view.h" + namespace yaze { namespace gui { @@ -21,5 +23,48 @@ bool InputHexShort(const char* label, int* data) { ImGuiInputTextFlags_CharsHexadecimal); } +void ItemLabel(absl::string_view title, ItemLabelFlags flags) { + ImGuiWindow* window = ImGui::GetCurrentWindow(); + const ImVec2 lineStart = ImGui::GetCursorScreenPos(); + const ImGuiStyle& style = ImGui::GetStyle(); + float fullWidth = ImGui::GetContentRegionAvail().x; + float itemWidth = ImGui::CalcItemWidth() + style.ItemSpacing.x; + ImVec2 textSize = ImGui::CalcTextSize(title.begin(), title.end()); + ImRect textRect; + textRect.Min = ImGui::GetCursorScreenPos(); + if (flags & ItemLabelFlag::Right) textRect.Min.x = textRect.Min.x + itemWidth; + textRect.Max = textRect.Min; + textRect.Max.x += fullWidth - itemWidth; + textRect.Max.y += textSize.y; + + ImGui::SetCursorScreenPos(textRect.Min); + + ImGui::AlignTextToFramePadding(); + // Adjust text rect manually because we render it directly into a drawlist + // instead of using public functions. + textRect.Min.y += window->DC.CurrLineTextBaseOffset; + textRect.Max.y += window->DC.CurrLineTextBaseOffset; + + ImGui::ItemSize(textRect); + if (ImGui::ItemAdd( + textRect, window->GetID(title.data(), title.data() + title.size()))) { + ImGui::RenderTextEllipsis( + ImGui::GetWindowDrawList(), textRect.Min, textRect.Max, textRect.Max.x, + textRect.Max.x, title.data(), title.data() + title.size(), &textSize); + + if (textRect.GetWidth() < textSize.x && ImGui::IsItemHovered()) + ImGui::SetTooltip("%.*s", (int)title.size(), title.data()); + } + if (flags & ItemLabelFlag::Left) { + ImVec2 result; + auto other = ImVec2{0, textSize.y + window->DC.CurrLineTextBaseOffset}; + result.x = textRect.Max.x - other.x; + result.y = textRect.Max.y - other.y; + ImGui::SetCursorScreenPos(result); + ImGui::SameLine(); + } else if (flags & ItemLabelFlag::Right) + ImGui::SetCursorScreenPos(lineStart); +} + } // namespace gui } // namespace yaze diff --git a/src/gui/input.h b/src/gui/input.h index c5704258..11be3f2f 100644 --- a/src/gui/input.h +++ b/src/gui/input.h @@ -7,12 +7,22 @@ #include #include +#include "absl/strings/string_view.h" + namespace yaze { namespace gui { IMGUI_API bool InputHex(const char* label, int* data); IMGUI_API bool InputHexShort(const char* label, int* data); +using ItemLabelFlags = enum ItemLabelFlag { + Left = 1u << 0u, + Right = 1u << 1u, + Default = Left, +}; + +IMGUI_API void ItemLabel(absl::string_view title, ItemLabelFlags flags); + } // namespace gui } // namespace yaze