diff --git a/PDGServer_WPF/App.xaml.cs b/PDGServer_WPF/App.xaml.cs index a2b6928..8d26332 100644 --- a/PDGServer_WPF/App.xaml.cs +++ b/PDGServer_WPF/App.xaml.cs @@ -188,7 +188,15 @@ namespace RBG_Server source.CopyTo(ms); _ = GameCommunicationHandler.ImageCollection.TryAdd(Path.GetFileNameWithoutExtension(path) + MIP_RAW + Path.GetExtension(path), new CachedByteArray(ms.ToArray())); } + // Destroy old board if we are replacing + if (!string.IsNullOrEmpty(GameCommunicationHandler.BoardName)) + { + GameCommunicationHandler.ImageCollection.Remove(GameCommunicationHandler.BoardName, out CachedByteArray val); + val.Dispose(); + } + GameCommunicationHandler.BoardName = path; } + GameCommunicationHandler.ImageList.Add(path); } } diff --git a/PDGServer_WPF/ConnectionPage.xaml.cs b/PDGServer_WPF/ConnectionPage.xaml.cs index 9137b2c..f337a10 100644 --- a/PDGServer_WPF/ConnectionPage.xaml.cs +++ b/PDGServer_WPF/ConnectionPage.xaml.cs @@ -16,6 +16,8 @@ using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; +// Not using Shapes.Path so specify Path = System.IO +using Path = System.IO.Path; namespace RBG_Server_WPF { @@ -343,9 +345,36 @@ namespace RBG_Server_WPF } } } - + /// + /// Loads the selected sprites into the image list + /// + /// + /// + private void GameSpritesBrowser_Click(object sender, RoutedEventArgs e) + { + CommunicationHandler host = App.Context.GameCommunicationHandler; + OpenFileDialog ofd = new(); + ofd.Filter = "Image Files|*.png;*.jpg;*.gif"; + ofd.Multiselect = true; + if (ofd.ShowDialog() == true) + { + string[] resultFiles = ofd.FileNames; + // Clear existing sprites + host.ClearSprites(); + // Finally, load the sprites + foreach (string file in resultFiles) + { + using FileStream fs = File.OpenRead(file); + string fileName = Path.GetFileName(file); + host.ImageList.Add(fileName); + App.Context.LoadStream(fs, fileName, true); + } + } + } #endregion + + } } diff --git a/RBG_Server.Core/CommunicationHandler.cs b/RBG_Server.Core/CommunicationHandler.cs index 9e5f7e2..a8963b1 100644 --- a/RBG_Server.Core/CommunicationHandler.cs +++ b/RBG_Server.Core/CommunicationHandler.cs @@ -87,6 +87,24 @@ namespace RBG_Server }); } } + /// + /// Clears all sprites, but not the board image, from the collection; disposing data as required + /// + public void ClearSprites() + { + // Remove all existing except the board image + foreach (string image in ImageList) + { + if (image != BoardName) + { + ImageCollection.Remove(image, out CachedByteArray removed); + removed.Dispose(); + } + } + // Reset the image list + ImageList.Clear(); + ImageList.Add(BoardName); + } private void AcceptConnections(TcpClient client) {