Added TextWithBackground(), Text() (using std::format), and BeginSmallPopupButton() render functions.

This commit is contained in:
Patrick Wuttke
2026-02-17 10:35:25 +01:00
parent 85f75d9814
commit a193826eb1

View File

@@ -9,10 +9,12 @@
#include <cstring>
#include <format>
#include <functional>
#include <memory>
#include <ranges>
#include <span>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>
#include <imgui.h>
@@ -26,6 +28,48 @@
namespace ImRaid
{
#if 0
struct StyleVar
{
};
inline StyleVar ImRaidStyleVar_
namespace impl
{
struct StyleVarStorage
{
};
struct Context
{
ImGuiContext* imguiContext;
};
inline std::unordered_map<ImGuiContext*, std::unique_ptr<Context>> gContexts;
inline Context* gCurrentContext = nullptr;
[[nodiscard]]
inline Context& getContext() noexcept
{
ImGuiContext* imguiContext = ImGui::GetCurrentContext();
MIJIN_ASSERT(imguiContext != nullptr, "Cannot use ImRaid without a current ImGui context.");
if (gCurrentContext == nullptr || gCurrentContext->imguiContext != imguiContext)
{
auto itContext = gContexts.find(imguiContext);
if (itContext == gContexts.end()) {
itContext = gContexts.emplace(imguiContext, std::make_unique<Context>()).first;
}
gCurrentContext = itContext.get();
}
return *gCurrentContext;
}
}
#endif
inline bool ToggleButton(const char* label, bool& toggled, const ImVec2& size = ImVec2(0, 0))
{
if (toggled)
@@ -66,13 +110,15 @@ inline bool ToggleImageButton(const char* strId, ImTextureID textureId, const Im
return clicked;
}
inline bool BeginPopupButton(const char* label, const ImVec2& size = {} , ImGuiWindowFlags flags = ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoSavedSettings)
template<bool small>
inline bool BeginPopupButtonImpl(const char* label, const ImVec2& size = {} , ImGuiWindowFlags flags = ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoSavedSettings)
{
char popupId[128] = {"popup##"};
std::strcat(popupId, label);
const float popupX = ImGui::GetCursorScreenPos().x;
if (ImGui::Button(label)) {
const bool open = small ? ImGui::SmallButton(label) : ImGui::Button(label);
if (open) {
ImGui::OpenPopup(popupId);
}
@@ -82,11 +128,36 @@ inline bool BeginPopupButton(const char* label, const ImVec2& size = {} , ImGuiW
return ImGui::BeginPopup(popupId, flags);
}
inline bool BeginPopupButton(const char* label, const ImVec2& size = {} , ImGuiWindowFlags flags = ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoSavedSettings)
{
return BeginPopupButtonImpl<false>(label, size, flags);
}
inline bool BeginSmallPopupButton(const char* label, const ImVec2& size = {} , ImGuiWindowFlags flags = ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoSavedSettings)
{
return BeginPopupButtonImpl<true>(label, size, flags);
}
inline void TextUnformatted(std::string_view stringView)
{
ImGui::TextUnformatted(stringView.data(), stringView.data() + stringView.size());
}
template<typename... TArgs>
void Text(std::format_string<TArgs...> fmt, TArgs&&... args)
{
ImGui::TextUnformatted(raid::formatTemp(fmt, std::forward<TArgs>(args)...));
}
inline void TextWithBackground(std::string_view stringView, const ImU32 color, float border = 0.f, float rounding = 0.f)
{
const ImVec2 textSize = ImGui::CalcTextSize(stringView.data(), stringView.data() + stringView.size());
const ImVec2 topLeft = ImGui::GetCursorScreenPos() - ImVec2(border, border);
const ImVec2 bottomRight = topLeft + textSize + ImVec2(2.f * border, 2.f * border);
ImGui::GetWindowDrawList()->AddRectFilled(topLeft, bottomRight, color, rounding);
TextUnformatted(stringView);
}
inline float CalcButtonWidth(const char* text)
{
return ImGui::CalcTextSize(text).x + (2.f * ImGui::GetStyle().FramePadding.x);