Cleaned some code; added dynamic loading of sprites

This commit is contained in:
2021-08-29 21:12:37 +12:00
parent 9b5f08e86f
commit 453dcd52bd
3 changed files with 229 additions and 162 deletions
+27 -24
View File
@@ -4,7 +4,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="PDG Server" Height="600" Width="800">
Title="PDG Server" Height="768" Width="1024" Background="#FF4B4B4B">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
@@ -16,30 +16,33 @@
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<StackPanel>
<TextBox x:Name="ImageSourceTextBox" Text="Source" TextWrapping="Wrap" LostFocus="ImageSourceTextBox_LostFocus"/>
<Button x:Name="BrowseButton" Content="Browse" Height="20" Width="40" Click="BrowseButton_Click"/>
<TextBlock Text="Number of Rows" TextWrapping="Wrap" Padding="0,10,0,0"/>
<Slider x:Name="NumberOfRows" IsSnapToTickEnabled="True" Minimum="1" TickPlacement="BottomRight" AutoToolTipPlacement="BottomRight" ValueChanged="NumberOfRows_ValueChanged" Value="9"/>
<TextBlock Text="Number of Columns:" TextWrapping="Wrap" Padding="0,10,0,0"/>
<Slider x:Name="NumberOfColumns" IsSnapToTickEnabled="True" Minimum="1" TickPlacement="BottomRight" AutoToolTipPlacement="BottomRight" ValueChanged="NumberOfColumns_ValueChanged" Value="9"/>
<TextBlock Text="Starting Row" TextWrapping="Wrap" Padding="0,10,0,0"/>
<Slider x:Name="StartingRow" IsSnapToTickEnabled="True" Value="8" AutoToolTipPlacement="BottomRight" TickPlacement="BottomRight" ValueChanged="Sliders_ValueChanged" Maximum="9"/>
<TextBlock Text="Starting Column" TextWrapping="Wrap" Padding="0,10,0,0"/>
<Slider x:Name="StartingColumn" IsSnapToTickEnabled="True" AutoToolTipPlacement="BottomRight" TickPlacement="BottomRight" ValueChanged="Sliders_ValueChanged" Maximum="9"/>
<TextBlock Text="Zoom Box Starting Row" TextWrapping="Wrap" Padding="0,10,0,0"/>
<Slider x:Name="ZoomBoxStartRow" IsSnapToTickEnabled="True" AutoToolTipPlacement="BottomRight" TickPlacement="BottomRight" Value="3" ValueChanged="Sliders_ValueChanged" Maximum="9"/>
<TextBlock Text="Zoom Box Starting Column" TextWrapping="Wrap" Padding="0,10,0,0"/>
<Slider x:Name="ZoomBoxStartColumn" IsSnapToTickEnabled="True" AutoToolTipPlacement="BottomRight" TickPlacement="BottomRight" Value="3" ValueChanged="Sliders_ValueChanged" Maximum="9"/>
<TextBlock Text="Zoom Box Span" TextWrapping="Wrap" Padding="0,10,0,0"/>
<Slider x:Name="ZoomBoxSpan" IsSnapToTickEnabled="True" AutoToolTipPlacement="BottomRight" TickPlacement="BottomRight" Value="3" ValueChanged="Sliders_ValueChanged" Minimum="1" Maximum="6"/>
<Button x:Name="GridVisibilityToggleButton" Content="Show/Hide Grid" Click="GridVisibilityToggleButton_Click"/>
<Button x:Name="StartServerButton" Content="Start Server" HorizontalAlignment="Center" Margin="0,25,0,0" Click="StartServerButton_Click"/>
<Button x:Name="DrawRandomizedPlayers" Content="Draw Random Players" Margin="0,25,0,0" Click="DrawRandomizedPlayers_Click"/>
<WrapPanel Height="100">
</WrapPanel>
<TextBlock Text="Game Board File Path" TextWrapping="Wrap" Padding="0,10,0,0" Foreground="White" Margin="2,5,2,0"/>
<TextBox x:Name="ImageSourceTextBox" TextWrapping="Wrap" LostFocus="ImageSourceTextBox_LostFocus" Margin="2,5,2,0"/>
<Button x:Name="BrowseButton" Content="Browse" Click="BrowseButton_Click" Margin="10,5,10,0"/>
<TextBlock Text="Game Sprites Files" TextWrapping="Wrap" Padding="0,10,0,0" Foreground="White" Margin="2,0,2,0"/>
<TextBox x:Name="ImageSourceTextBox_Copy" TextWrapping="Wrap" LostFocus="ImageSourceTextBox_LostFocus" Margin="2,5,2,0"/>
<Button x:Name="GameSpritesBrowser" Content="Browse" Click="GameSpritesBrowser_Click" Margin="10,5,10,0"/>
<TextBlock Text="Number of Rows" TextWrapping="Wrap" Padding="0,10,0,0" Foreground="White" Margin="2,0,2,0"/>
<Slider x:Name="NumberOfRows" IsSnapToTickEnabled="True" Minimum="1" TickPlacement="BottomRight" AutoToolTipPlacement="BottomRight" ValueChanged="NumberOfRows_ValueChanged" Value="9" Margin="2,0,2,0"/>
<TextBlock Text="Number of Columns:" TextWrapping="Wrap" Padding="0,10,0,0" Foreground="White" Margin="2,0,2,0"/>
<Slider x:Name="NumberOfColumns" IsSnapToTickEnabled="True" Minimum="1" TickPlacement="BottomRight" AutoToolTipPlacement="BottomRight" ValueChanged="NumberOfColumns_ValueChanged" Value="9" Margin="2,0,2,0"/>
<TextBlock Text="Starting Row" TextWrapping="Wrap" Padding="0,10,0,0" Foreground="White" Margin="2,0,2,0"/>
<Slider x:Name="StartingRow" IsSnapToTickEnabled="True" Value="8" AutoToolTipPlacement="BottomRight" TickPlacement="BottomRight" ValueChanged="Sliders_ValueChanged" Maximum="9" Margin="2,0,2,0"/>
<TextBlock Text="Starting Column" TextWrapping="Wrap" Padding="0,10,0,0" Foreground="White" Margin="2,0,2,0"/>
<Slider x:Name="StartingColumn" IsSnapToTickEnabled="True" AutoToolTipPlacement="BottomRight" TickPlacement="BottomRight" ValueChanged="Sliders_ValueChanged" Maximum="9" Margin="2,0,2,0"/>
<TextBlock Text="Zoom Box Starting Row" TextWrapping="Wrap" Padding="0,10,0,0" Foreground="White" Margin="2,0,2,0"/>
<Slider x:Name="ZoomBoxStartRow" IsSnapToTickEnabled="True" AutoToolTipPlacement="BottomRight" TickPlacement="BottomRight" Value="3" ValueChanged="Sliders_ValueChanged" Maximum="9" Margin="2,0,2,0"/>
<TextBlock Text="Zoom Box Starting Column" TextWrapping="Wrap" Padding="0,10,0,0" Foreground="White" Margin="2,0,2,0"/>
<Slider x:Name="ZoomBoxStartColumn" IsSnapToTickEnabled="True" AutoToolTipPlacement="BottomRight" TickPlacement="BottomRight" Value="3" ValueChanged="Sliders_ValueChanged" Maximum="9" Margin="2,0,2,0"/>
<TextBlock Text="Zoom Box Span" TextWrapping="Wrap" Padding="0,10,0,0" Foreground="White" Margin="2,0,2,0"/>
<Slider x:Name="ZoomBoxSpan" IsSnapToTickEnabled="True" AutoToolTipPlacement="BottomRight" TickPlacement="BottomRight" Value="3" ValueChanged="Sliders_ValueChanged" Minimum="1" Maximum="6" Margin="2,0,2,0"/>
<Button x:Name="GridVisibilityToggleButton" Content="Show/Hide Grid" Click="GridVisibilityToggleButton_Click" Margin="10,10,10,0"/>
<Button x:Name="StartServerButton" Content="Start Server" HorizontalAlignment="Stretch" Margin="10,25,10,0" Click="StartServerButton_Click" Background="#955FF17A" Foreground="White"/>
<Button x:Name="DrawRandomizedPlayers" Content="Draw Random Players" Margin="10,25,10,0" Click="DrawRandomizedPlayers_Click"/>
<UniformGrid x:Name="LoadedSprites" Columns="6" Margin="2,0,2,0" Rows="6" MinHeight="120" />
</StackPanel>
<Border Grid.Column="1" BorderThickness="1,1,1,1" Background="#54D3D3D3" BorderBrush="DarkGray">
<ListView x:Name="PlayersList">
<Border Grid.Column="1" BorderThickness="1,1,1,1" BorderBrush="DarkGray" Background="#19000000">
<ListView x:Name="PlayersList" Background="{x:Null}">
<ListView.View>
<GridView>
<GridViewColumn/>
+68 -4
View File
@@ -1,7 +1,10 @@
using Microsoft.Win32;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
@@ -22,7 +25,6 @@ namespace RBG_Server.WPF
public MainWindow()
{
InitializeComponent();
//gameServer = new GameServer(8, 0, 9, 9, 3, 3, 3, 3, null);
}
/// <summary>
@@ -33,7 +35,7 @@ namespace RBG_Server.WPF
private void BrowseButton_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new();
ofd.Filter = "Image Files (.png; .jpg)|*.png;*.jpg";
ofd.Filter = "Image Files|*.png;*.jpg";
if (ofd.ShowDialog() == true)
{
ImageSourceTextBox.Text = ofd.FileName;
@@ -81,7 +83,7 @@ namespace RBG_Server.WPF
/// <param name="e"></param>
private void StartServerButton_Click(object sender, RoutedEventArgs e)
{
gameServer = new((int)StartingRow.Value, (int)StartingColumn.Value, (int)NumberOfRows.Value, (int)NumberOfColumns.Value, (int)ZoomBoxStartRow.Value, (int)ZoomBoxStartColumn.Value, (int)ZoomBoxSpan.Value, (int)ZoomBoxSpan.Value, gameBoard);
gameServer = new((byte)StartingRow.Value, (byte)StartingColumn.Value, (byte)NumberOfRows.Value, (byte)NumberOfColumns.Value, (byte)ZoomBoxStartRow.Value, (byte)ZoomBoxStartColumn.Value, (byte)ZoomBoxSpan.Value, (byte)ZoomBoxSpan.Value, gameBoard);
}
@@ -226,7 +228,7 @@ namespace RBG_Server.WPF
}
private void GenerateNewGameBoard(List<Player> players)
private void GenerateNewGameBoard(List<Player> players)
{
SolidColorBrush buttonBrush = new(Color.FromArgb(1, 255, 255, 255));
Grid[] cells = new Grid[PreviewImageOverlay.RowDefinitions.Count * PreviewImageOverlay.ColumnDefinitions.Count];
@@ -285,6 +287,68 @@ namespace RBG_Server.WPF
GenerateGameBoard(randPlayers);
}
ConcurrentDictionary<string, MemoryStream> loadedSprites = new();
Task loadingFiles;
private void GameSpritesBrowser_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new();
ofd.Filter = "Image Files|*.png;*.jpg";
ofd.Multiselect = true;
if (ofd.ShowDialog() == true)
{
string[] resultFiles = ofd.FileNames;
loadedSprites.Clear();
// Run the following work in a separate thread
loadingFiles = Task.Run(() =>
{
// Continue in parallel
_ = Parallel.ForEach(resultFiles, (file) =>
{
MemoryStream ms = new();
using FileStream fs = File.OpenRead(file);
if (new FileInfo(file).Length > 512e6) // greater than 512 kB; resize the sprite to something more reasonable
{
BitmapImage gameBoardImage = new();
gameBoardImage.BeginInit();
gameBoardImage.StreamSource = fs;
gameBoardImage.CacheOption = BitmapCacheOption.Default;
gameBoardImage.EndInit();
double vRate = 128 / (double)gameBoardImage.PixelHeight; // Resize so the largest dimension is 128 px
double hRate = 128 / (double)gameBoardImage.PixelWidth;
TransformedBitmap modified = new(gameBoardImage, new ScaleTransform(gameBoardImage.PixelWidth * Math.Min(vRate, hRate), gameBoardImage.PixelHeight * Math.Min(vRate, hRate)));
PngBitmapEncoder encoder = new();
encoder.Frames.Add(BitmapFrame.Create(modified));
encoder.Save(ms);
}
else
{
fs.CopyTo(ms);
}
_ = loadedSprites.TryAdd(file, ms);
});
// Invoke the dispatcher to add all of the sprites to the loaded sprites list
_ = Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render, new Action(() =>
{
LoadedSprites.Children.Clear();
foreach (KeyValuePair<string, MemoryStream> item in loadedSprites)
{
BitmapImage image = new();
image.BeginInit();
image.StreamSource = item.Value;
image.EndInit();
Image spriteImage = new()
{
Source = image
};
_ = LoadedSprites.Children.Add(spriteImage);
}
}));
});
}
}
}
}