Skip to content

Commit

Permalink
Refactor ImGuiTableSortSpecs into ui::TableColumnSortSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkewley committed Sep 14, 2024
1 parent 2a9d1c4 commit ebf1df4
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 11 deletions.
15 changes: 7 additions & 8 deletions src/OpenSimCreator/UI/ModelEditor/CoordinateEditorPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,22 @@ class osc::CoordinateEditorPanel::Impl final : public StandardPanelImpl {
ui::table_setup_scroll_freeze(0, 1);
ui::table_headers_row();

if (ImGuiTableSortSpecs* p = ui::table_get_sort_specs(); p && p->SpecsDirty)
{
std::span<const ImGuiTableColumnSortSpecs> specs(p->Specs, p->SpecsCount);
if (ui::table_column_sort_specs_are_dirty()) {
const auto specs = ui::get_table_column_sort_specs();

// we know the user can only sort one column (name) so we don't need to permute
// through the entire specs structure
if (specs.size() == 1 && specs.front().ColumnIndex == 0 && specs.front().SortOrder == 0)
if (specs.size() == 1 && specs.front().column_index == 0 && specs.front().sort_order == 0)
{
switch (specs.front().SortDirection)
switch (specs.front().sort_direction)
{
case ImGuiSortDirection_Ascending:
case ui::SortDirection::Ascending:
rgs::sort(coordPtrs, rgs::less{}, [](const auto& ptr) { return ptr->getName(); });
break;
case ImGuiSortDirection_Descending:
case ui::SortDirection::Descending:
rgs::sort(coordPtrs, rgs::greater{}, [](const auto& ptr) { return ptr->getName(); });
break;
case ImGuiSortDirection_None:
case ui::SortDirection::None:
default:
break; // just use them as-is
}
Expand Down
46 changes: 44 additions & 2 deletions src/oscar/UI/oscimgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,34 @@ struct osc::Converter<ui::StyleVar, ImGuiStyleVar> final {
}
};

template<>
struct osc::Converter<ui::SortDirection, ImGuiSortDirection> final {
ImGuiSortDirection operator()(ui::SortDirection option) const
{
static_assert(num_options<ui::SortDirection>() == 3);

switch (option) {
case ui::SortDirection::None: return ImGuiSortDirection_None;
case ui::SortDirection::Ascending: return ImGuiSortDirection_Ascending;
case ui::SortDirection::Descending: return ImGuiSortDirection_Descending;
default: return ImGuiSortDirection_None;
}
}
};

template<>
struct osc::Converter<ImGuiTableColumnSortSpecs, ui::TableColumnSortSpec> final {
ui::TableColumnSortSpec operator()(ImGuiTableColumnSortSpecs specs) const
{
return {
.column_id = ui::ID{specs.ColumnUserID},
.column_index = static_cast<size_t>(specs.ColumnIndex),
.sort_order = static_cast<size_t>(specs.SortOrder),
.sort_direction = to<ui::SortDirection>(specs.SortDirection),
};
}
};

void osc::ui::align_text_to_frame_padding()
{
ImGui::AlignTextToFramePadding();
Expand Down Expand Up @@ -1115,9 +1143,23 @@ void osc::ui::table_setup_scroll_freeze(int cols, int rows)
ImGui::TableSetupScrollFreeze(cols, rows);
}

ImGuiTableSortSpecs* osc::ui::table_get_sort_specs()
bool osc::ui::table_column_sort_specs_are_dirty()
{
const ImGuiTableSortSpecs* specs = ImGui::TableGetSortSpecs();
return specs and specs->SpecsDirty;
}

std::vector<ui::TableColumnSortSpec> osc::ui::get_table_column_sort_specs()
{
return ImGui::TableGetSortSpecs();
std::vector<TableColumnSortSpec> rv;
const ImGuiTableSortSpecs* specs = ImGui::TableGetSortSpecs();
if (specs) {
rv.reserve(specs->SpecsCount);
for (int i = 0; i < specs->SpecsCount; ++i) {
rv.push_back(to<TableColumnSortSpec>(specs->Specs[i]));
}
}
return rv;
}

void osc::ui::table_headers_row()
Expand Down
17 changes: 16 additions & 1 deletion src/oscar/UI/oscimgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,22 @@ namespace osc::ui
bool begin_table(CStringView str_id, int column, TableFlags = {}, const Vec2& outer_size = {}, float inner_width = 0.0f);
void table_setup_scroll_freeze(int cols, int rows);

ImGuiTableSortSpecs* table_get_sort_specs();
enum class SortDirection {
None,
Ascending,
Descending,
NUM_OPTIONS,
};

struct TableColumnSortSpec final {
ID column_id{};
size_t column_index = 0;
size_t sort_order = 0;
SortDirection sort_direction = SortDirection::None;
};

bool table_column_sort_specs_are_dirty();
std::vector<TableColumnSortSpec> get_table_column_sort_specs();

enum class ColumnFlag : unsigned {
None = 0,
Expand Down

0 comments on commit ebf1df4

Please sign in to comment.