Demo: add a small easter egg when the 4x4 board of Selectable is filled + tweaked the demo.
diff --git a/imgui_demo.cpp b/imgui_demo.cpp
index 96c0034..eb7e24b 100644
--- a/imgui_demo.cpp
+++ b/imgui_demo.cpp
@@ -1088,27 +1088,34 @@
}
if (ImGui::TreeNode("Grid"))
{
- static int selected[4 * 4] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
- for (int i = 0; i < 4 * 4; i++)
- {
- ImGui::PushID(i);
- if (ImGui::Selectable("Sailor", selected[i] != 0, 0, ImVec2(50, 50)))
- {
- // Toggle
- selected[i] = !selected[i];
+ static char selected[4][4] = { { 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 1 } };
- // Note: We _unnecessarily_ test for both x/y and i here only to silence some static analyzer.
- // The second part of each test is unnecessary.
- int x = i % 4;
- int y = i / 4;
- if (x > 0) { selected[i - 1] ^= 1; }
- if (x < 3 && i < 15) { selected[i + 1] ^= 1; }
- if (y > 0 && i > 3) { selected[i - 4] ^= 1; }
- if (y < 3 && i < 12) { selected[i + 4] ^= 1; }
+ // Add in a bit of silly fun...
+ const float time = (float)ImGui::GetTime();
+ const bool winning_state = memchr(selected, 0, sizeof(selected)) == NULL; // If all cells are selected...
+ if (winning_state)
+ ImGui::PushStyleVar(ImGuiStyleVar_SelectableTextAlign, ImVec2(0.5f + 0.5f * cosf(time * 2.0f), 0.5f + 0.5f * sinf(time * 3.0f)));
+
+ for (int y = 0; y < 4; y++)
+ for (int x = 0; x < 4; x++)
+ {
+ if (x > 0)
+ ImGui::SameLine();
+ ImGui::PushID(y * 4 + x);
+ if (ImGui::Selectable("Sailor", selected[y][x] != 0, 0, ImVec2(50, 50)))
+ {
+ // Toggle clicked cell + toggle neighbors
+ selected[y][x] ^= 1;
+ if (x > 0) { selected[y][x - 1] ^= 1; }
+ if (x < 3) { selected[y][x + 1] ^= 1; }
+ if (y > 0) { selected[y - 1][x] ^= 1; }
+ if (y < 3) { selected[y + 1][x] ^= 1; }
+ }
+ ImGui::PopID();
}
- if ((i % 4) < 3) ImGui::SameLine();
- ImGui::PopID();
- }
+
+ if (winning_state)
+ ImGui::PopStyleVar();
ImGui::TreePop();
}
if (ImGui::TreeNode("Alignment"))