diff --git a/public/raid/imraid.hpp b/public/raid/imraid.hpp index 6df05a9..bd4afd4 100644 --- a/public/raid/imraid.hpp +++ b/public/raid/imraid.hpp @@ -9,10 +9,12 @@ #include #include #include +#include #include #include #include #include +#include #include #include @@ -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> 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()).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 +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(label, size, flags); +} + +inline bool BeginSmallPopupButton(const char* label, const ImVec2& size = {} , ImGuiWindowFlags flags = ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoSavedSettings) +{ + return BeginPopupButtonImpl(label, size, flags); +} + inline void TextUnformatted(std::string_view stringView) { ImGui::TextUnformatted(stringView.data(), stringView.data() + stringView.size()); } +template +void Text(std::format_string fmt, TArgs&&... args) +{ + ImGui::TextUnformatted(raid::formatTemp(fmt, std::forward(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);