Skip to content

Commit

Permalink
added param to search
Browse files Browse the repository at this point in the history
  • Loading branch information
Redcrafter committed Jul 30, 2024
1 parent 725af71 commit bebeebf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ static ImGuiID DockSpaceOverViewport(ShaderProgram& textured_shader) {
return dockspace_id;
}

static void HelpMarker(const char* desc) {
void HelpMarker(const char* desc) {
ImGui::TextDisabled("(?)");
if(ImGui::BeginItemTooltip()) {
ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f);
Expand Down
47 changes: 30 additions & 17 deletions src/windows/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@

ImGuiTableFlags flags = ImGuiTableFlags_Hideable | ImGuiTableFlags_Sortable | ImGuiTableFlags_ScrollY | ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_Resizable;

void HelpMarker(const char* desc);

static int search_order(const SearchResult& el, int ColumnIndex, const GameData& game_data) {
switch(ColumnIndex) {
case 0: return el.map;
case 1: return el.layer;
case 2: return el.room_pos.x * 40 + el.tile_pos.x;
case 3: return el.room_pos.y * 22 + el.tile_pos.y;
case 4: return game_data.maps[el.map].getRoom(el.room_pos)->tiles[el.layer][el.tile_pos.y][el.tile_pos.x].param;
case 5: return el.room_pos.x;
case 6: return el.room_pos.y;
case 7: return el.tile_pos.x;
case 8: return el.tile_pos.y;
}
return 0;
}

void SearchWindow::draw(const GameData& game_data, std::function<void(int, glm::ivec2)> callback) {
if(ImGui::Begin("Search")) {
ImGui::InputInt("tile_id", &tile_id);
Expand All @@ -26,13 +43,17 @@ void SearchWindow::draw(const GameData& game_data, std::function<void(int, glm::
ImGui::Checkbox("Highlight", &show_on_map);

ImGui::Text("%zu results", results.size());
if(ImGui::BeginTable("search_results", 9, flags)) {
ImGui::SameLine();
HelpMarker("Right click the table header to add additional rows.\nLeft click a row header to sort the table.");

if(ImGui::BeginTable("search_results", 10, flags)) {
ImGui::TableSetupScrollFreeze(0, 1); // Make top row always visible

ImGui::TableSetupColumn("map", ImGuiTableColumnFlags_None);
ImGui::TableSetupColumn("layer", ImGuiTableColumnFlags_None);
ImGui::TableSetupColumn("x", ImGuiTableColumnFlags_None);
ImGui::TableSetupColumn("y", ImGuiTableColumnFlags_None);
ImGui::TableSetupColumn("param", ImGuiTableColumnFlags_None);

ImGui::TableSetupColumn("room_x", ImGuiTableColumnFlags_DefaultHide);
ImGui::TableSetupColumn("room_y", ImGuiTableColumnFlags_DefaultHide);
Expand All @@ -48,17 +69,7 @@ void SearchWindow::draw(const GameData& game_data, std::function<void(int, glm::
auto spec = sort_specs->Specs[0];

std::stable_sort(results.begin(), results.end(), [&](const SearchResult& lhs, const SearchResult& rhs) {
int delta = 0;
switch(spec.ColumnIndex) {
case 0: delta = lhs.map - rhs.map; break;
case 1: delta = lhs.layer - rhs.layer; break;
case 2: delta = (lhs.room_pos.x * 40 + lhs.tile_pos.x) - (rhs.room_pos.x * 40 + rhs.tile_pos.x); break;
case 3: delta = (lhs.room_pos.y * 22 + lhs.tile_pos.y) - (rhs.room_pos.y * 22 + rhs.tile_pos.y); break;
case 4: delta = lhs.room_pos.x - rhs.room_pos.x; break;
case 5: delta = lhs.room_pos.y - rhs.room_pos.y; break;
case 6: delta = lhs.tile_pos.x - rhs.tile_pos.x; break;
case 7: delta = lhs.tile_pos.y - rhs.tile_pos.y; break;
}
auto delta = search_order(lhs, spec.ColumnIndex, game_data) - search_order(rhs, spec.ColumnIndex, game_data);

if(spec.SortDirection == ImGuiSortDirection_Ascending)
return delta < 0;
Expand All @@ -81,21 +92,23 @@ void SearchWindow::draw(const GameData& game_data, std::function<void(int, glm::

auto el = results[row];
auto pos = el.room_pos * glm::ivec2(40, 22) + el.tile_pos;
auto tile = game_data.maps[el.map].getTile(el.layer, pos.x, pos.y);

// clang-format off
ImGui::TableSetColumnIndex(0); ImGui::Text("%s", mapNames[el.map]);
ImGui::TableSetColumnIndex(1); ImGui::Text("%i", el.layer);
ImGui::TableSetColumnIndex(2); ImGui::Text("%i", pos.x);
ImGui::TableSetColumnIndex(3); ImGui::Text("%i", pos.y);
ImGui::TableSetColumnIndex(4); ImGui::Text("%i", tile.has_value() ? tile->param : 0);

ImGui::TableSetColumnIndex(4); ImGui::Text("%i", el.room_pos.x);
ImGui::TableSetColumnIndex(5); ImGui::Text("%i", el.room_pos.y);
ImGui::TableSetColumnIndex(5); ImGui::Text("%i", el.room_pos.x);
ImGui::TableSetColumnIndex(6); ImGui::Text("%i", el.room_pos.y);

ImGui::TableSetColumnIndex(6); ImGui::Text("%i", el.tile_pos.x);
ImGui::TableSetColumnIndex(7); ImGui::Text("%i", el.tile_pos.y);
ImGui::TableSetColumnIndex(7); ImGui::Text("%i", el.tile_pos.x);
ImGui::TableSetColumnIndex(8); ImGui::Text("%i", el.tile_pos.y);
// clang-format on

ImGui::TableSetColumnIndex(8);
ImGui::TableSetColumnIndex(9);
ImGui::PushID(row);
if(ImGui::SmallButton("goto")) {
callback(el.map, pos);
Expand Down

0 comments on commit bebeebf

Please sign in to comment.