From 96928355c126123b07f38c9bcaec451f0c726058 Mon Sep 17 00:00:00 2001 From: ikpil Date: Sun, 11 Jun 2023 16:27:17 +0900 Subject: [PATCH] transfer the ownership of managing the sample settings to the demo toolkit. --- src/DotRecast.Recast.Demo/RecastDemo.cs | 20 ++++--- src/DotRecast.Recast.Demo/Sample.cs | 11 ++-- src/DotRecast.Recast.Demo/UI/ImFilePicker.cs | 21 ++++++- .../UI/RcSettingsView.cs | 60 ++++++++++--------- .../{RcSettings.cs => RcSampleSettings.cs} | 2 +- 5 files changed, 67 insertions(+), 47 deletions(-) rename src/DotRecast.Recast.DemoTool/{RcSettings.cs => RcSampleSettings.cs} (96%) diff --git a/src/DotRecast.Recast.Demo/RecastDemo.cs b/src/DotRecast.Recast.Demo/RecastDemo.cs index d06b1cd..f619369 100644 --- a/src/DotRecast.Recast.Demo/RecastDemo.cs +++ b/src/DotRecast.Recast.Demo/RecastDemo.cs @@ -300,8 +300,6 @@ public class RecastDemo private DemoInputGeomProvider LoadInputMesh(byte[] stream) { DemoInputGeomProvider geom = DemoObjImporter.Load(stream); - sample = new Sample(geom, ImmutableArray.Empty, null, settingsView, dd); - toolset.SetEnabled(true); return geom; } @@ -368,7 +366,12 @@ public class RecastDemo _imgui = new ImGuiController(_gl, window, _input); + DemoInputGeomProvider geom = LoadInputMesh(Loader.ToBytes("nav_test.obj")); + sample = new Sample(geom, ImmutableArray.Empty, null, dd); + settingsView = new RcSettingsView(); + settingsView.SetSample(sample); + toolset = new RcToolsetView( new TestNavmeshTool(), new OffMeshConnectionTool(), @@ -377,6 +380,7 @@ public class RecastDemo new JumpLinkBuilderTool(), new DynamicUpdateTool() ); + toolset.SetEnabled(true); logView = new RcLogView(); _canvas = new RcCanvas(window, settingsView, toolset, logView); @@ -392,10 +396,6 @@ public class RecastDemo Logger.Information(version); Logger.Information(renderGl); Logger.Information(glslString); - - - DemoInputGeomProvider geom = LoadInputMesh(Loader.ToBytes("nav_test.obj")); - sample = new Sample(geom, ImmutableArray.Empty, null, settingsView, dd); } private void UpdateKeyboard(float dt) @@ -430,7 +430,7 @@ public class RecastDemo */ if (sample.GetInputGeom() != null) { - var settings = settingsView.GetSettings(); + var settings = sample.GetSettings(); RcVec3f bmin = sample.GetInputGeom().GetMeshBoundsMin(); RcVec3f bmax = sample.GetInputGeom().GetMeshBoundsMax(); Recast.CalcGridSize(bmin, bmax, settings.cellSize, out var gw, out var gh); @@ -485,7 +485,9 @@ public class RecastDemo if (settingsView.IsMeshInputTrigerred()) { var bytes = Loader.ToBytes(settingsView.GetMeshInputFilePath()); - sample.Update(LoadInputMesh(bytes), null, null); + var geom = LoadInputMesh(bytes); + + sample.Update(geom, ImmutableArray.Empty, null); } else if (settingsView.IsNavMeshInputTrigerred()) { @@ -516,7 +518,7 @@ public class RecastDemo { if (!building) { - var settings = settingsView.GetSettings(); + var settings = sample.GetSettings(); var partitioning = settings.partitioning; var cellSize = settings.cellSize; var cellHeight = settings.cellHeight; diff --git a/src/DotRecast.Recast.Demo/Sample.cs b/src/DotRecast.Recast.Demo/Sample.cs index 2906d00..f8e9fa9 100644 --- a/src/DotRecast.Recast.Demo/Sample.cs +++ b/src/DotRecast.Recast.Demo/Sample.cs @@ -22,7 +22,6 @@ using System.Collections.Generic; using DotRecast.Detour; using DotRecast.Recast.Demo.Draw; using DotRecast.Recast.DemoTool.Geom; - using DotRecast.Recast.Demo.UI; using DotRecast.Recast.DemoTool; @@ -33,17 +32,17 @@ public class Sample private DemoInputGeomProvider _inputGeom; private DtNavMesh _navMesh; private DtNavMeshQuery _navMeshQuery; - private readonly RcSettings _settings; + private readonly RcSampleSettings _settings; private IList _recastResults; private bool _changed; - public Sample(DemoInputGeomProvider inputGeom, IList recastResults, DtNavMesh navMesh, - RcSettingsView settingsView, RecastDebugDraw debugDraw) + public Sample(DemoInputGeomProvider inputGeom, IList recastResults, DtNavMesh navMesh, RecastDebugDraw debugDraw) { _inputGeom = inputGeom; _recastResults = recastResults; _navMesh = navMesh; - _settings = settingsView.GetSettings(); + _settings = new(); + SetQuery(navMesh); _changed = true; } @@ -68,7 +67,7 @@ public class Sample return _navMesh; } - public RcSettings GetSettings() + public RcSampleSettings GetSettings() { return _settings; } diff --git a/src/DotRecast.Recast.Demo/UI/ImFilePicker.cs b/src/DotRecast.Recast.Demo/UI/ImFilePicker.cs index 522dede..1e4ce78 100644 --- a/src/DotRecast.Recast.Demo/UI/ImFilePicker.cs +++ b/src/DotRecast.Recast.Demo/UI/ImFilePicker.cs @@ -1,15 +1,19 @@ using System; using System.Numerics; using System.Collections.Generic; +using System.Collections.Immutable; using System.Drawing; using System.IO; using ImGuiNET; +using Serilog; namespace DotRecast.Recast.Demo.UI; // original code : https://gist.github.com/prime31/91d1582624eb2635395417393018016e public class ImFilePicker { + private static readonly ILogger Logger = Log.ForContext(); + private static readonly Dictionary _filePickers = new Dictionary(); public string RootFolder; @@ -77,7 +81,7 @@ public class ImFilePicker ImGui.PopStyleColor(); } - + var fileSystemEntries = GetFileSystemEntries(di.FullName); foreach (var fse in fileSystemEntries) { @@ -157,7 +161,20 @@ public class ImFilePicker var files = new List(); var dirs = new List(); - foreach (var fse in Directory.GetFileSystemEntries(fullName, "")) + ImmutableArray fileEntries; + try + { + fileEntries = Directory + .GetFileSystemEntries(fullName, "") + .ToImmutableArray(); + } + catch (Exception e) + { + Logger.Error(e, ""); + return files; + } + + foreach (var fse in fileEntries) { if (Directory.Exists(fse)) { diff --git a/src/DotRecast.Recast.Demo/UI/RcSettingsView.cs b/src/DotRecast.Recast.Demo/UI/RcSettingsView.cs index b00d9ac..ab7d8a6 100644 --- a/src/DotRecast.Recast.Demo/UI/RcSettingsView.cs +++ b/src/DotRecast.Recast.Demo/UI/RcSettingsView.cs @@ -47,12 +47,17 @@ public class RcSettingsView : IRcView private bool _mouseInside; public bool IsMouseInside() => _mouseInside; - private readonly RcSettings _settings; + private Sample _sample; private RcCanvas _canvas; public RcSettingsView() { - _settings = new(); + + } + + public void SetSample(Sample sample) + { + _sample = sample; } public void Bind(RcCanvas canvas) @@ -60,17 +65,14 @@ public class RcSettingsView : IRcView _canvas = canvas; } - public RcSettings GetSettings() - { - return _settings; - } - public void Update(double dt) { } public void Draw(double dt) { + var settings = _sample.GetSettings(); + int width = 310; var posX = _canvas.Size.X - width; ImGui.SetNextWindowPos(new Vector2(posX, 0)); @@ -112,23 +114,23 @@ public class RcSettingsView : IRcView ImGui.Text("Rasterization"); ImGui.Separator(); - ImGui.SliderFloat("Cell Size", ref _settings.cellSize, 0.01f, 1f, "%.2f"); - ImGui.SliderFloat("Cell Height", ref _settings.cellHeight, 0.01f, 1f, "%.2f"); + ImGui.SliderFloat("Cell Size", ref settings.cellSize, 0.01f, 1f, "%.2f"); + ImGui.SliderFloat("Cell Height", ref settings.cellHeight, 0.01f, 1f, "%.2f"); ImGui.Text($"Voxels {voxels[0]} x {voxels[1]}"); ImGui.NewLine(); ImGui.Text("Agent"); ImGui.Separator(); - ImGui.SliderFloat("Height", ref _settings.agentHeight, 0.1f, 5f, "%.1f"); - ImGui.SliderFloat("Radius", ref _settings.agentRadius, 0.1f, 5f, "%.1f"); - ImGui.SliderFloat("Max Climb", ref _settings.agentMaxClimb, 0.1f, 5f, "%.1f"); - ImGui.SliderFloat("Max Slope", ref _settings.agentMaxSlope, 1f, 90f, "%.0f"); + ImGui.SliderFloat("Height", ref settings.agentHeight, 0.1f, 5f, "%.1f"); + ImGui.SliderFloat("Radius", ref settings.agentRadius, 0.1f, 5f, "%.1f"); + ImGui.SliderFloat("Max Climb", ref settings.agentMaxClimb, 0.1f, 5f, "%.1f"); + ImGui.SliderFloat("Max Slope", ref settings.agentMaxSlope, 1f, 90f, "%.0f"); ImGui.NewLine(); ImGui.Text("Region"); ImGui.Separator(); - ImGui.SliderInt("Min Region Size", ref _settings.minRegionSize, 1, 150); - ImGui.SliderInt("Merged Region Size", ref _settings.mergedRegionSize, 1, 150); + ImGui.SliderInt("Min Region Size", ref settings.minRegionSize, 1, 150); + ImGui.SliderInt("Merged Region Size", ref settings.mergedRegionSize, 1, 150); ImGui.NewLine(); ImGui.Text("Partitioning"); @@ -136,38 +138,38 @@ public class RcSettingsView : IRcView PartitionType.Values.ForEach(partition => { var label = partition.Name.Substring(0, 1).ToUpper() + partition.Name.Substring(1).ToLower(); - ImGui.RadioButton(label, ref _settings.partitioningIdx, partition.Idx); + ImGui.RadioButton(label, ref settings.partitioningIdx, partition.Idx); }); ImGui.NewLine(); ImGui.Text("Filtering"); ImGui.Separator(); - ImGui.Checkbox("Low Hanging Obstacles", ref _settings.filterLowHangingObstacles); - ImGui.Checkbox("Ledge Spans", ref _settings.filterLedgeSpans); - ImGui.Checkbox("Walkable Low Height Spans", ref _settings.filterWalkableLowHeightSpans); + ImGui.Checkbox("Low Hanging Obstacles", ref settings.filterLowHangingObstacles); + ImGui.Checkbox("Ledge Spans", ref settings.filterLedgeSpans); + ImGui.Checkbox("Walkable Low Height Spans", ref settings.filterWalkableLowHeightSpans); ImGui.NewLine(); ImGui.Text("Polygonization"); ImGui.Separator(); - ImGui.SliderFloat("Max Edge Length", ref _settings.edgeMaxLen, 0f, 50f, "%.1f"); - ImGui.SliderFloat("Max Edge Error", ref _settings.edgeMaxError, 0.1f, 3f, "%.1f"); - ImGui.SliderInt("Vert Per Poly", ref _settings.vertsPerPoly, 3, 12); + ImGui.SliderFloat("Max Edge Length", ref settings.edgeMaxLen, 0f, 50f, "%.1f"); + ImGui.SliderFloat("Max Edge Error", ref settings.edgeMaxError, 0.1f, 3f, "%.1f"); + ImGui.SliderInt("Vert Per Poly", ref settings.vertsPerPoly, 3, 12); ImGui.NewLine(); ImGui.Text("Detail Mesh"); ImGui.Separator(); - ImGui.SliderFloat("Sample Distance", ref _settings.detailSampleDist, 0f, 16f, "%.1f"); - ImGui.SliderFloat("Max Sample Error", ref _settings.detailSampleMaxError, 0f, 16f, "%.1f"); + ImGui.SliderFloat("Sample Distance", ref settings.detailSampleDist, 0f, 16f, "%.1f"); + ImGui.SliderFloat("Max Sample Error", ref settings.detailSampleMaxError, 0f, 16f, "%.1f"); ImGui.NewLine(); ImGui.Text("Tiling"); ImGui.Separator(); - ImGui.Checkbox("Enable", ref _settings.tiled); - if (_settings.tiled) + ImGui.Checkbox("Enable", ref settings.tiled); + if (settings.tiled) { - if (0 < (_settings.tileSize % 16)) - _settings.tileSize = _settings.tileSize + (16 - (_settings.tileSize % 16)); - ImGui.SliderInt("Tile Size", ref _settings.tileSize, 16, 1024); + if (0 < (settings.tileSize % 16)) + settings.tileSize = settings.tileSize + (16 - (settings.tileSize % 16)); + ImGui.SliderInt("Tile Size", ref settings.tileSize, 16, 1024); ImGui.Text($"Tiles {tiles[0]} x {tiles[1]}"); ImGui.Text($"Max Tiles {maxTiles}"); diff --git a/src/DotRecast.Recast.DemoTool/RcSettings.cs b/src/DotRecast.Recast.DemoTool/RcSampleSettings.cs similarity index 96% rename from src/DotRecast.Recast.DemoTool/RcSettings.cs rename to src/DotRecast.Recast.DemoTool/RcSampleSettings.cs index 2777cf2..4639303 100644 --- a/src/DotRecast.Recast.DemoTool/RcSettings.cs +++ b/src/DotRecast.Recast.DemoTool/RcSampleSettings.cs @@ -1,6 +1,6 @@ namespace DotRecast.Recast.DemoTool { - public class RcSettings + public class RcSampleSettings { public float cellSize = 0.3f; public float cellHeight = 0.2f;