From 66ec111f5d12429c7fb557b201a29cc1bf659696 Mon Sep 17 00:00:00 2001 From: Teo-CD Date: Sun, 28 Jan 2024 22:05:20 +0000 Subject: [PATCH] World/Debug: Properly check the MapEdit status If a Dear ImGui window is collapsed or closed, ImGui::Begin returns false. Check it properly to bypass rendering if is indeed not needed. --- World.cpp | 129 +++++++++++++++++++++++++++--------------------------- 1 file changed, 65 insertions(+), 64 deletions(-) diff --git a/World.cpp b/World.cpp index 6bc5e35..7880148 100644 --- a/World.cpp +++ b/World.cpp @@ -274,75 +274,76 @@ void World::step(const float& stepTime) { player.rotate(player.currentRotationSpeed*stepTime); #ifdef IMGUI - ImGui::Begin("MapEdit"); - static int blockToPlace = 1; + if (ImGui::Begin("MapEdit")) { + static int blockToPlace = 1; - /* - * Group all the select boxes together : makes it a big block in the - * Dear ImGui layout an allows adding things on the side. - */ - ImGui::BeginGroup(); - for (int y = 0 ; y < h ; y++) { - for (int x = 0 ; x < w ; x++) { - ImGui::PushID(x + y*w); - if (x > 0) - ImGui::SameLine(); - BlockType currentBlock = map[x + w*y]; + /* + * Group all the select boxes together : makes it a big block in the + * Dear ImGui layout an allows adding things on the side. + */ + ImGui::BeginGroup(); + for (int y = 0; y < h; y++) { + for (int x = 0; x < w; x++) { + ImGui::PushID(x + y * w); + if (x > 0) + ImGui::SameLine(); + BlockType currentBlock = map[x + w * y]; - ImVec4 hoverColor; - switch ((BlockType)blockToPlace) { - case BlockType::WALL: - hoverColor = (ImVec4)Colors::Wall; - break; - case BlockType::DOOR: - hoverColor = (ImVec4)Colors::Door; - break; - case BlockType::WINDOW: - hoverColor = (ImVec4)Colors::Window; - break; - default: - /* Default header color, it seems ? */ - hoverColor = (ImVec4)ImColor(188, 120, 32); - break; + ImVec4 hoverColor; + switch ((BlockType) blockToPlace) { + case BlockType::WALL: + hoverColor = (ImVec4) Colors::Wall; + break; + case BlockType::DOOR: + hoverColor = (ImVec4) Colors::Door; + break; + case BlockType::WINDOW: + hoverColor = (ImVec4) Colors::Window; + break; + default: + /* Default header color, it seems ? */ + hoverColor = (ImVec4) ImColor(188, 120, 32); + break; + } + ImGui::PushStyleColor(ImGuiCol_HeaderHovered, hoverColor); + + ImVec4 blockColor; + switch (currentBlock) { + case BlockType::WALL: + blockColor = (ImVec4) Colors::Wall; + break; + case BlockType::DOOR: + blockColor = (ImVec4) Colors::Door; + break; + case BlockType::WINDOW: + blockColor = (ImVec4) Colors::Window; + break; + default: + blockColor = (ImVec4) ImColor(188, 120, 32); + break; + } + ImGui::PushStyleColor(ImGuiCol_Header, blockColor); + + if (ImGui::Selectable("", currentBlock != BlockType::AIR, 0, ImVec2(10, 10))) { + map[x + w * y] = + currentBlock == (BlockType) blockToPlace ? BlockType::AIR : (BlockType) blockToPlace; + } + ImGui::PopStyleColor(2); + ImGui::PopID(); } - ImGui::PushStyleColor(ImGuiCol_HeaderHovered, hoverColor); - - ImVec4 blockColor; - switch (currentBlock) { - case BlockType::WALL: - blockColor = (ImVec4)Colors::Wall; - break; - case BlockType::DOOR: - blockColor = (ImVec4)Colors::Door; - break; - case BlockType::WINDOW: - blockColor = (ImVec4)Colors::Window; - break; - default: - blockColor = (ImVec4)ImColor(188, 120, 32); - break; - } - ImGui::PushStyleColor(ImGuiCol_Header, blockColor); - - if(ImGui::Selectable("", currentBlock != BlockType::AIR, 0, ImVec2(10, 10))) { - map[x + w*y] = currentBlock == (BlockType)blockToPlace ? BlockType::AIR : (BlockType)blockToPlace; - } - ImGui::PopStyleColor(2); - ImGui::PopID(); } + ImGui::EndGroup(); + + ImGui::SameLine(); + ImGui::BeginGroup(); + ImGui::Indent(); + ImGui::Text("Place :"); + ImGui::RadioButton("Wall", &blockToPlace, (int) BlockType::WALL); + ImGui::RadioButton("Door", &blockToPlace, (int) BlockType::DOOR); + ImGui::RadioButton("Window", &blockToPlace, (int) BlockType::WINDOW); + ImGui::Unindent(); + ImGui::EndGroup(); } - ImGui::EndGroup(); - - ImGui::SameLine(); - ImGui::BeginGroup(); - ImGui::Indent(); - ImGui::Text("Place :"); - ImGui::RadioButton("Wall", &blockToPlace, (int)BlockType::WALL); - ImGui::RadioButton("Door", &blockToPlace, (int)BlockType::DOOR); - ImGui::RadioButton("Window", &blockToPlace, (int)BlockType::WINDOW); - ImGui::Unindent(); - ImGui::EndGroup(); - ImGui::End(); #endif }