Skip to content

Instantly share code, notes, and snippets.

@unpacklo
Created February 9, 2015 19:47
Show Gist options
  • Save unpacklo/64c18e50099488837bf5 to your computer and use it in GitHub Desktop.
Save unpacklo/64c18e50099488837bf5 to your computer and use it in GitHub Desktop.
column sort example
int indexCompare(const void *leftIndex, const void *rightIndex)
{
return *(int *)leftIndex - *(int *)rightIndex;
}
int pathCompare(const void *leftIndex, const void *rightIndex)
{
AssetManifest *manifest = pack.manifest;
return strcmp(manifest->paths[*(int *)leftIndex], manifest->paths[*(int *)rightIndex]);
}
int offsetCompare(const void *leftIndex, const void *rightIndex)
{
AssetManifest *manifest = pack.manifest;
return manifest->offsets[*(int *)leftIndex] - manifest->offsets[*(int *)rightIndex];
}
int byteCompare(const void *leftIndex, const void *rightIndex)
{
AssetManifest *manifest = pack.manifest;
return manifest->bytes[*(int *)leftIndex] - manifest->bytes[*(int *)rightIndex];
}
int (*comparison)(const void *leftIndex, const void *rightIndex) = indexCompare;
...........
static bool reverse = false;
if (comparison == indexCompare)
{
const char *direction = "ascending";
if (reverse)
{
direction = "descending";
}
ImGui::Text("Sorting by index (%s)", direction);
}
else if (comparison == pathCompare)
{
const char *direction = "ascending";
if (reverse)
{
direction = "descending";
}
ImGui::Text("Sorting by path (%s)", direction);
}
else if (comparison == offsetCompare)
{
const char *direction = "ascending";
if (reverse)
{
direction = "descending";
}
ImGui::Text("Sorting by offset (%s)", direction);
}
else
{
const char *direction = "ascending";
if (reverse)
{
direction = "descending";
}
ImGui::Text("Sorting by bytes (%s)", direction);
}
if (ImGui::Button("Sort by index"))
{
if (comparison == indexCompare)
{
reverse ^= true;
}
else
{
reverse = false;
}
comparison = indexCompare;
}
ImGui::SameLine();
if (ImGui::Button("Sort by path"))
{
if (comparison == pathCompare)
{
reverse ^= true;
}
else
{
reverse = false;
}
comparison = pathCompare;
}
ImGui::SameLine();
if (ImGui::Button("Sort by offset"))
{
if (comparison == offsetCompare)
{
reverse ^= true;
}
else
{
reverse = false;
}
comparison = offsetCompare;
}
ImGui::SameLine();
if (ImGui::Button("Sort by bytes"))
{
if (comparison == byteCompare)
{
reverse ^= true;
}
else
{
reverse = false;
}
comparison = byteCompare;
}
ImGui::BeginChild("##scrolling");
ImGui::Columns(4);
ImGui::Text("Index");
ImGui::Separator();
ImGui::NextColumn();
ImGui::Text("Path");
ImGui::Separator();
ImGui::NextColumn();
ImGui::Text("Offset");
ImGui::Separator();
ImGui::NextColumn();
ImGui::Text("Bytes");
ImGui::Separator();
ImGui::NextColumn();
int numItems = pack.manifest->numPaths;
int *displayOrder = PushArrayFront(&memory, int, numItems);
// Set up the display order array with indices.
for (int i = 0; i < numItems; ++i)
{
displayOrder[i] = i;
}
qsort(displayOrder, numItems, SIZEOF_ARRAY_ELEMENT(displayOrder), comparison);
if (reverse)
{
int lo = 0, hi = numItems - 1;
while (lo < hi)
{
int temp = displayOrder[lo];
displayOrder[lo] = displayOrder[hi];
displayOrder[hi] = temp;
++lo;
--hi;
}
}
float itemHeight = ImGui::GetTextLineHeightWithSpacing();
int displayStart = 0, displayEnd = numItems;
ImGui::CalcListClipping(numItems, itemHeight, &displayStart, &displayEnd);
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + (displayStart * itemHeight));
for (int i = displayStart; i < displayEnd; ++i)
{
ImGui::Text("%d", displayOrder[i]);
}
ImGui::NextColumn();
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + (displayStart * itemHeight));
for (int i = displayStart; i < displayEnd; ++i)
{
ImGui::Text("\"%s\"", pack.manifest->paths[displayOrder[i]]);
}
ImGui::NextColumn();
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + (displayStart * itemHeight));
for (int i = displayStart; i < displayEnd; ++i)
{
ImGui::Text("%zu", pack.manifest->offsets[displayOrder[i]]);
}
ImGui::NextColumn();
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + (displayStart * itemHeight));
for (int i = displayStart; i < displayEnd; ++i)
{
ImGui::Text("%zu", pack.manifest->bytes[displayOrder[i]]);
}
PopArrayFront(&memory, int, numItems);
ImGui::Columns(1);
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + ((numItems - displayEnd) * itemHeight));
ImGui::Text("END OF LISTING!");
ImGui::EndChild();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment