Demo: add a small easter egg when the 4x4 board of Selectable is filled + tweaked the demo.

docking
ocornut 4 years ago
parent 01cc666039
commit 014e5078a8

@ -1088,27 +1088,34 @@ static void ShowDemoWindowWidgets()
} }
if (ImGui::TreeNode("Grid")) 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 }; static char 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++)
{ // Add in a bit of silly fun...
ImGui::PushID(i); const float time = (float)ImGui::GetTime();
if (ImGui::Selectable("Sailor", selected[i] != 0, 0, ImVec2(50, 50))) 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++)
{ {
// Toggle if (x > 0)
selected[i] = !selected[i]; ImGui::SameLine();
ImGui::PushID(y * 4 + x);
// Note: We _unnecessarily_ test for both x/y and i here only to silence some static analyzer. if (ImGui::Selectable("Sailor", selected[y][x] != 0, 0, ImVec2(50, 50)))
// The second part of each test is unnecessary. {
int x = i % 4; // Toggle clicked cell + toggle neighbors
int y = i / 4; selected[y][x] ^= 1;
if (x > 0) { selected[i - 1] ^= 1; } if (x > 0) { selected[y][x - 1] ^= 1; }
if (x < 3 && i < 15) { selected[i + 1] ^= 1; } if (x < 3) { selected[y][x + 1] ^= 1; }
if (y > 0 && i > 3) { selected[i - 4] ^= 1; } if (y > 0) { selected[y - 1][x] ^= 1; }
if (y < 3 && i < 12) { selected[i + 4] ^= 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(); ImGui::TreePop();
} }
if (ImGui::TreeNode("Alignment")) if (ImGui::TreeNode("Alignment"))

Loading…
Cancel
Save