Added IDScope helper and c_Window/c_Dialog coroutines.

This commit is contained in:
Patrick Wuttke
2025-11-04 17:52:01 +01:00
parent 561a92d557
commit 4228fe554d

View File

@@ -81,6 +81,33 @@ inline void TextUnformatted(std::string_view stringView)
ImGui::TextUnformatted(stringView.data(), stringView.data() + stringView.size());
}
class IDScope
{
public:
explicit IDScope(int intId)
{
ImGui::PushID(intId);
}
explicit IDScope(const void* ptrId)
{
ImGui::PushID(ptrId);
}
explicit IDScope(const char* strId)
{
ImGui::PushID(strId);
}
IDScope(const char* strId, const char* strIdEnd)
{
ImGui::PushID(strId, strIdEnd);
}
IDScope(const IDScope&) = delete;
~IDScope()
{
ImGui::PopID();
}
IDScope& operator=(const IDScope&) = delete;
};
struct DataTableState
{
std::vector<std::size_t> sortedIndices;
@@ -318,14 +345,78 @@ inline DataTableColumn<TObject> MakeColumn(const char* header, const char* fmt,
}
template<typename TFunc>
mijin::Task<> c_MessageBox(const char* titleId, TFunc&& renderFunc)
mijin::Task<> c_Window(const char* titleId, bool& open, ImGuiWindowFlags flags, TFunc renderFunc)
{
while (open)
{
if (ImGui::Begin(titleId, &open, flags)) {
std::invoke(renderFunc);
}
ImGui::End();
co_await mijin::c_suspend();
}
}
template<typename TFunc>
mijin::Task<> c_Window(const char* titleId, bool& open, TFunc renderFunc)
{
return c_Window(titleId, open, ImGuiWindowFlags_None, std::move(renderFunc));
}
template<typename TFunc>
mijin::Task<> c_Window(const char* titleId, ImGuiWindowFlags flags, TFunc renderFunc)
{
bool open = true;
co_return co_await c_Window(titleId, open, flags, std::move(renderFunc));
}
template<typename TFunc>
mijin::Task<> c_Window(const char* titleId, TFunc renderFunc)
{
return c_Window(titleId, ImGuiWindowFlags_None, std::move(renderFunc));
}
template<typename TFunc>
mijin::Task<> c_Dialog(const char* titleId, bool& open, ImGuiWindowFlags flags, TFunc renderFunc)
{
ImGui::OpenPopup(titleId);
while (ImGui::BeginPopupModal(titleId, &open, flags))
{
std::invoke(renderFunc);
ImGui::EndPopup();
co_await mijin::c_suspend();
}
}
template<typename TFunc>
mijin::Task<> c_Dialog(const char* titleId, bool& open, TFunc renderFunc)
{
return c_Dialog(titleId, open, ImGuiWindowFlags_None, std::move(renderFunc));
}
template<typename TFunc>
mijin::Task<> c_Dialog(const char* titleId, ImGuiWindowFlags flags, TFunc renderFunc)
{
bool open = true;
co_return co_await c_Dialog(titleId, open, flags, std::move(renderFunc));
}
template<typename TFunc>
mijin::Task<> c_Dialog(const char* titleId, TFunc renderFunc)
{
return c_Dialog(titleId, ImGuiWindowFlags_None, std::move(renderFunc));
}
template<typename TFunc>
mijin::Task<> c_MessageBox(const char* titleId, TFunc renderFunc)
{
ImGui::OpenPopup(titleId);
bool open = true;
while (ImGui::BeginPopupModal(titleId, &open, ImGuiWindowFlags_NoResize))
{
std::invoke(std::forward<TFunc>(renderFunc));
std::invoke(renderFunc);
ImGui::EndPopup();
co_await mijin::c_suspend();
}