From 065fd54c8191a7d341a87df4df2f0d7739aba311 Mon Sep 17 00:00:00 2001 From: Graham Pentheny Date: Mon, 4 Aug 2025 15:54:35 -0400 Subject: [PATCH] Use static arrays of strings for tool and display names --- RecastDemo/Include/Sample.h | 28 ++++-- RecastDemo/Include/Sample_SoloMesh.h | 1 + RecastDemo/Include/Sample_TileMesh.h | 3 +- RecastDemo/Source/Sample_SoloMesh.cpp | 39 ++++++-- RecastDemo/Source/Sample_TempObstacles.cpp | 4 - RecastDemo/Source/Sample_TileMesh.cpp | 101 ++++++++++----------- 6 files changed, 102 insertions(+), 74 deletions(-) diff --git a/RecastDemo/Include/Sample.h b/RecastDemo/Include/Sample.h index 147705ef..4b39f2e9 100644 --- a/RecastDemo/Include/Sample.h +++ b/RecastDemo/Include/Sample.h @@ -33,18 +33,30 @@ enum class SampleToolType : uint8_t { NONE = 0, - TILE_EDIT, - TILE_HIGHLIGHT, - TEMP_OBSTACLE, - NAVMESH_TESTER, - NAVMESH_PRUNE, - OFFMESH_CONNECTION, - CONVEX_VOLUME, - CROWD, + TILE_EDIT = 1, + TILE_HIGHLIGHT = 2, + TEMP_OBSTACLE = 3, + NAVMESH_TESTER = 4, + NAVMESH_PRUNE = 5, + OFFMESH_CONNECTION = 6, + CONVEX_VOLUME = 7, + CROWD = 8, MAX_TOOLS }; +static const char* toolNames[] = { + "None", + "Create Tiles", + "Highlight Tile Cache", + "Create Temp Obstacles", + "Test Navmesh", + "Prune Navmesh", + "Create Off-Mesh Connections", + "Create Convex Volumes", + "Create Crowds", +}; + /// These are just sample areas to use consistent values across the samples. /// The use should specify these base on his needs. enum SamplePolyAreas diff --git a/RecastDemo/Include/Sample_SoloMesh.h b/RecastDemo/Include/Sample_SoloMesh.h index 73edfc65..0391e503 100644 --- a/RecastDemo/Include/Sample_SoloMesh.h +++ b/RecastDemo/Include/Sample_SoloMesh.h @@ -58,6 +58,7 @@ protected: POLYMESH_DETAIL }; DrawMode currentDrawMode = DrawMode::NAVMESH; + static const char* drawModeNames[]; void cleanup(); diff --git a/RecastDemo/Include/Sample_TileMesh.h b/RecastDemo/Include/Sample_TileMesh.h index e3cc0ffc..4a771876 100644 --- a/RecastDemo/Include/Sample_TileMesh.h +++ b/RecastDemo/Include/Sample_TileMesh.h @@ -60,6 +60,7 @@ private: POLYMESH_DETAIL }; DrawMode drawMode = DrawMode::NAVMESH; + static const char* drawModeNames[]; int maxTiles = 0; int maxPolysPerTile = 0; @@ -76,7 +77,7 @@ private: unsigned char* buildTileMesh(int tileX, int tileY, const float* boundsMin, const float* boundsMax, int& outDataSize); void cleanup(); - void UI_DrawModeOption(const char* name, DrawMode drawMode, bool enabled); + void UI_DrawModeOption(DrawMode drawMode, bool enabled); public: Sample_TileMesh(); diff --git a/RecastDemo/Source/Sample_SoloMesh.cpp b/RecastDemo/Source/Sample_SoloMesh.cpp index 6ba446d9..56edac17 100644 --- a/RecastDemo/Source/Sample_SoloMesh.cpp +++ b/RecastDemo/Source/Sample_SoloMesh.cpp @@ -43,6 +43,26 @@ # define snprintf _snprintf #endif +const char* Sample_SoloMesh::drawModeNames[] { + "Navmesh", + "Navmesh Trans", + "Navmesh BVTree", + "Navmesh Nodes", + "Navmesh Invis", + "Input Mesh", + "Voxels", + "Walkable Voxels", + "Compact", + "Compact Distance", + "Compact Regions", + "Region Connections", + "Raw Contours", + "Both Contours", + "Contours", + "Poly Mesh", + "Poly Mesh Detail" +}; + Sample_SoloMesh::Sample_SoloMesh() { setTool(new NavMeshTesterTool); @@ -91,29 +111,28 @@ void Sample_SoloMesh::handleSettings() void Sample_SoloMesh::handleTools() { - const SampleToolType type = !tool ? SampleToolType::NONE : tool->type(); + const SampleToolType currentType = !tool ? SampleToolType::NONE : tool->type(); - if (ImGui::RadioButton("Test Navmesh", type == SampleToolType::NAVMESH_TESTER)) { setTool(new NavMeshTesterTool); } - if (ImGui::RadioButton("Prune Navmesh", type == SampleToolType::NAVMESH_PRUNE)) { setTool(new NavMeshPruneTool); } - if (ImGui::RadioButton("Create Off-Mesh Connections", type == SampleToolType::OFFMESH_CONNECTION)) { setTool(new OffMeshConnectionTool); } - if (ImGui::RadioButton("Create Convex Volumes", type == SampleToolType::CONVEX_VOLUME)) { setTool(new ConvexVolumeTool); } - if (ImGui::RadioButton("Create Crowds", type == SampleToolType::CROWD)) { setTool(new CrowdTool); } +#define TOOL(toolType, toolClass) if (ImGui::RadioButton(toolNames[static_cast(SampleToolType::toolType)], currentType == SampleToolType::toolType)) { setTool(new toolClass{}); } + TOOL(NAVMESH_TESTER, NavMeshTesterTool) + TOOL(NAVMESH_PRUNE, NavMeshPruneTool) + TOOL(OFFMESH_CONNECTION, OffMeshConnectionTool) + TOOL(CONVEX_VOLUME, ConvexVolumeTool) + TOOL(CROWD, CrowdTool) +#undef TOOL ImGui::Separator(); - ImGui::Indent(); if (tool) { tool->handleMenu(); } - ImGui::Unindent(); } void Sample_SoloMesh::UI_DrawModeOption(const char* name, const DrawMode drawMode, const bool enabled) { ImGui::BeginDisabled(!enabled); - bool checked = currentDrawMode == drawMode; - if (ImGui::Checkbox(name, &checked)) + if (ImGui::RadioButton(name, currentDrawMode == drawMode)) { currentDrawMode = drawMode; } diff --git a/RecastDemo/Source/Sample_TempObstacles.cpp b/RecastDemo/Source/Sample_TempObstacles.cpp index 334f4834..5c54118d 100644 --- a/RecastDemo/Source/Sample_TempObstacles.cpp +++ b/RecastDemo/Source/Sample_TempObstacles.cpp @@ -1010,14 +1010,10 @@ void Sample_TempObstacles::handleTools() ImGui::Separator(); - ImGui::Indent(); - if (tool) { tool->handleMenu(); } - - ImGui::Unindent(); } void Sample_TempObstacles::handleDebugMode() diff --git a/RecastDemo/Source/Sample_TileMesh.cpp b/RecastDemo/Source/Sample_TileMesh.cpp index ef728b92..1b8c3e4b 100644 --- a/RecastDemo/Source/Sample_TileMesh.cpp +++ b/RecastDemo/Source/Sample_TileMesh.cpp @@ -82,6 +82,27 @@ unsigned int ilog2(unsigned int v) } } +const char* Sample_TileMesh::drawModeNames[] { + "Navmesh", + "Navmesh Trans", + "Navmesh BVTree", + "Navmesh Nodes", + "Navmesh Portals", + "Navmesh Invis", + "Input Mesh", + "Voxels", + "Walkable Voxels", + "Compact", + "Compact Disatnce", + "Compact Regions", + "Region Connections", + "Raw Contours", + "Both Contours", + "Contours", + "Poly Mesh", + "Poly Mesh Detail" +}; + class NavMeshTileTool : public SampleTool { Sample_TileMesh* m_sample = nullptr; @@ -276,50 +297,28 @@ void Sample_TileMesh::handleSettings() void Sample_TileMesh::handleTools() { - const SampleToolType type = !tool ? SampleToolType::NONE : tool->type(); - - if (ImGui::RadioButton("Test Navmesh", type == SampleToolType::NAVMESH_TESTER)) - { - setTool(new NavMeshTesterTool); - } - if (ImGui::RadioButton("Prune Navmesh", type == SampleToolType::NAVMESH_PRUNE)) - { - setTool(new NavMeshPruneTool); - } - if (ImGui::RadioButton("Create Tiles", type == SampleToolType::TILE_EDIT)) - { - setTool(new NavMeshTileTool); - } - if (ImGui::RadioButton("Create Off-Mesh Links", type == SampleToolType::OFFMESH_CONNECTION)) - { - setTool(new OffMeshConnectionTool); - } - if (ImGui::RadioButton("Create Convex Volumes", type == SampleToolType::CONVEX_VOLUME)) - { - setTool(new ConvexVolumeTool); - } - if (ImGui::RadioButton("Create Crowds", type == SampleToolType::CROWD)) - { - setTool(new CrowdTool); - } + const SampleToolType currentType = !tool ? SampleToolType::NONE : tool->type(); +#define TOOL(toolType, toolClass) if (ImGui::RadioButton(toolNames[static_cast(SampleToolType::toolType)], currentType == SampleToolType::toolType)) { setTool(new toolClass{}); } + TOOL(NAVMESH_TESTER, NavMeshTesterTool) + TOOL(NAVMESH_PRUNE, NavMeshPruneTool) + TOOL(TILE_EDIT, NavMeshTileTool) + TOOL(OFFMESH_CONNECTION, OffMeshConnectionTool) + TOOL(CONVEX_VOLUME, ConvexVolumeTool) + TOOL(CROWD, CrowdTool) +#undef TOOL ImGui::Separator(); - ImGui::Indent(); - if (tool) { tool->handleMenu(); } - - ImGui::Unindent(); } -void Sample_TileMesh::UI_DrawModeOption(const char* name, DrawMode drawMode, bool enabled) +void Sample_TileMesh::UI_DrawModeOption(DrawMode drawMode, bool enabled) { ImGui::BeginDisabled(!enabled); - bool checked = this->drawMode == drawMode; - if (ImGui::Checkbox(name, &checked)) + if (ImGui::RadioButton(drawModeNames[static_cast(drawMode)], this->drawMode == drawMode)) { this->drawMode = drawMode; } @@ -329,24 +328,24 @@ void Sample_TileMesh::UI_DrawModeOption(const char* name, DrawMode drawMode, boo void Sample_TileMesh::handleDebugMode() { ImGui::Text("Draw"); - UI_DrawModeOption("Input Mesh", DrawMode::MESH, true); - UI_DrawModeOption("Navmesh", DrawMode::NAVMESH, navMesh != nullptr); - UI_DrawModeOption("Navmesh Invis", DrawMode::NAVMESH_INVIS, navMesh != nullptr); - UI_DrawModeOption("Navmesh Trans", DrawMode::NAVMESH_TRANS, navMesh != nullptr); - UI_DrawModeOption("Navmesh BVTree", DrawMode::NAVMESH_BVTREE, navMesh != nullptr); - UI_DrawModeOption("Navmesh Nodes", DrawMode::NAVMESH_NODES, navQuery != nullptr); - UI_DrawModeOption("Navmesh Portals", DrawMode::NAVMESH_PORTALS, navMesh != nullptr); - UI_DrawModeOption("Voxels", DrawMode::VOXELS, heightfield != nullptr); - UI_DrawModeOption("Walkable Voxels", DrawMode::VOXELS_WALKABLE, heightfield != nullptr); - UI_DrawModeOption("Compact", DrawMode::COMPACT, compactHeightfield != nullptr); - UI_DrawModeOption("Compact Distance", DrawMode::COMPACT_DISTANCE, compactHeightfield != nullptr); - UI_DrawModeOption("Compact Regions", DrawMode::COMPACT_REGIONS, compactHeightfield != nullptr); - UI_DrawModeOption("Region Connections", DrawMode::REGION_CONNECTIONS, contourSet != nullptr); - UI_DrawModeOption("Raw Contours", DrawMode::RAW_CONTOURS, contourSet != nullptr); - UI_DrawModeOption("Both Contours", DrawMode::BOTH_CONTOURS, contourSet != nullptr); - UI_DrawModeOption("Contours", DrawMode::CONTOURS, contourSet != nullptr); - UI_DrawModeOption("Poly Mesh", DrawMode::POLYMESH, polyMesh != nullptr); - UI_DrawModeOption("Poly Mesh Detail", DrawMode::POLYMESH_DETAIL, detailPolyMesh != nullptr); + UI_DrawModeOption(DrawMode::MESH, true); + UI_DrawModeOption(DrawMode::NAVMESH, navMesh != nullptr); + UI_DrawModeOption(DrawMode::NAVMESH_INVIS, navMesh != nullptr); + UI_DrawModeOption(DrawMode::NAVMESH_TRANS, navMesh != nullptr); + UI_DrawModeOption(DrawMode::NAVMESH_BVTREE, navMesh != nullptr); + UI_DrawModeOption(DrawMode::NAVMESH_NODES, navQuery != nullptr); + UI_DrawModeOption(DrawMode::NAVMESH_PORTALS, navMesh != nullptr); + UI_DrawModeOption(DrawMode::VOXELS, heightfield != nullptr); + UI_DrawModeOption(DrawMode::VOXELS_WALKABLE, heightfield != nullptr); + UI_DrawModeOption(DrawMode::COMPACT, compactHeightfield != nullptr); + UI_DrawModeOption(DrawMode::COMPACT_DISTANCE, compactHeightfield != nullptr); + UI_DrawModeOption(DrawMode::COMPACT_REGIONS, compactHeightfield != nullptr); + UI_DrawModeOption(DrawMode::REGION_CONNECTIONS, contourSet != nullptr); + UI_DrawModeOption(DrawMode::RAW_CONTOURS, contourSet != nullptr); + UI_DrawModeOption(DrawMode::BOTH_CONTOURS, contourSet != nullptr); + UI_DrawModeOption(DrawMode::CONTOURS, contourSet != nullptr); + UI_DrawModeOption(DrawMode::POLYMESH, polyMesh != nullptr); + UI_DrawModeOption(DrawMode::POLYMESH_DETAIL, detailPolyMesh != nullptr); } void Sample_TileMesh::handleRender()