Added DataTable function.

This commit is contained in:
Patrick Wuttke 2025-09-19 13:53:42 +02:00
parent 7470878f9c
commit d3b56d3fd0

View File

@ -4,7 +4,14 @@
#if !defined(RAID_PUBLIC_RAID_IMRAID_HPP_INCLUDED) #if !defined(RAID_PUBLIC_RAID_IMRAID_HPP_INCLUDED)
#define RAID_PUBLIC_RAID_IMRAID_HPP_INCLUDED 1 #define RAID_PUBLIC_RAID_IMRAID_HPP_INCLUDED 1
#include <algorithm>
#include <functional>
#include <span>
#include <string>
#include <vector>
#include <imgui.h> #include <imgui.h>
#include <mijin/debug/assert.hpp>
namespace ImRaid namespace ImRaid
{ {
@ -47,6 +54,106 @@ inline bool ToggleImageButton(const char* strId, ImTextureID textureId, const Im
} }
return clicked; return clicked;
} }
struct DataTableState
{
std::vector<std::size_t> sortedIndices;
std::size_t sortColumn = 0;
bool sortDescending = false;
bool dirty = true;
};
template<typename TObject>
struct DataTableColumn
{
struct CellRendererArgs
{
const TObject& object;
};
using renderer_t = std::function<void(const CellRendererArgs&)>;
using comparator_t = std::function<bool(const TObject&, const TObject&)>;
std::string header;
renderer_t renderer;
comparator_t comparator;
};
template<typename TObject>
struct DataTableOptions
{
std::span<const DataTableColumn<TObject>> columns;
ImGuiTableFlags tableFlags = ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Sortable | ImGuiTableFlags_ScrollY;
};
template<typename TData, typename TObject>
inline void DataTable(const char* strId, const DataTableOptions<TObject>& options, const TData& data, DataTableState& state, ImVec2 outerSize = {})
{
if (outerSize.y <= 0.f) {
outerSize.y = ImGui::GetContentRegionAvail().y;
}
if (!ImGui::BeginTable(strId, static_cast<int>(options.columns.size()), options.tableFlags, outerSize)) {
return;
}
for (const DataTableColumn<TObject>& column : options.columns)
{
ImGuiTableColumnFlags flags = 0;
MIJIN_ASSERT(column.renderer, "Missing column renderer.");
if (!column.comparator) {
flags |= ImGuiTableColumnFlags_NoSort;
}
ImGui::TableSetupColumn(column.header.c_str(), flags);
}
ImGui::TableSetupScrollFreeze(0, 1);
ImGui::TableHeadersRow();
ImGuiTableSortSpecs* sortSpecs = ImGui::TableGetSortSpecs();
if (sortSpecs != nullptr && sortSpecs->SpecsDirty)
{
sortSpecs->SpecsDirty = false;
const ImGuiTableColumnSortSpecs& specs = *sortSpecs->Specs;
state.dirty |= (specs.ColumnIndex != state.sortColumn);
state.sortColumn = specs.ColumnIndex;
state.sortDescending = specs.SortDirection == ImGuiSortDirection_Descending;
}
if (state.sortedIndices.size() != data.size())
{
state.dirty = true;
state.sortedIndices.resize(data.size());
}
if (state.dirty)
{
for (std::size_t idx = 0; idx < data.size(); ++idx) {
state.sortedIndices[idx] = idx;
}
std::ranges::sort(state.sortedIndices, [&](std::size_t leftIdx, std::size_t rightIdx) {
return options.columns[state.sortColumn].comparator(data[leftIdx], data[rightIdx]);
});
state.dirty = false;
}
for (std::size_t indexIdx = 0; indexIdx < state.sortedIndices.size(); ++indexIdx)
{
const std::size_t dataIdx = state.sortDescending ? state.sortedIndices[state.sortedIndices.size() - indexIdx - 1]
: state.sortedIndices[indexIdx];
const TObject& object = data[dataIdx];
ImGui::TableNextRow();
for (const DataTableColumn<TObject>& column : options.columns)
{
ImGui::TableNextColumn();
column.renderer({
.object = object
});
}
}
ImGui::EndTable();
}
} // namespace ImRaid } // namespace ImRaid
#endif // !defined(RAID_PUBLIC_RAID_IMRAID_HPP_INCLUDED) #endif // !defined(RAID_PUBLIC_RAID_IMRAID_HPP_INCLUDED)