Refactored and added base projects & code
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
<Application x:Class="RBG_Server.App"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="clr-namespace:RBG_Server"
|
||||
StartupUri="MainWindow.xaml">
|
||||
</Application>
|
||||
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
|
||||
namespace RBG_Server
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for App.xaml
|
||||
/// </summary>
|
||||
public partial class App : Application
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using System.Windows;
|
||||
|
||||
[assembly: ThemeInfo(
|
||||
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
|
||||
//(used if a resource is not found in the page,
|
||||
// or application resource dictionaries)
|
||||
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
|
||||
//(used if a resource is not found in the page,
|
||||
// app, or any theme specific resource dictionaries)
|
||||
)]
|
||||
@@ -0,0 +1,56 @@
|
||||
<Window x:Class="RBG_Server.WPF.MainWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
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">
|
||||
<Grid>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition/>
|
||||
<ColumnDefinition/>
|
||||
</Grid.ColumnDefinitions>
|
||||
<Grid>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition/>
|
||||
<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>
|
||||
</StackPanel>
|
||||
<Border Grid.Column="1" BorderThickness="1,1,1,1" Background="#54D3D3D3" BorderBrush="DarkGray">
|
||||
<ListView x:Name="PlayersList">
|
||||
<ListView.View>
|
||||
<GridView>
|
||||
<GridViewColumn/>
|
||||
</GridView>
|
||||
</ListView.View>
|
||||
</ListView>
|
||||
</Border>
|
||||
</Grid>
|
||||
<Grid Grid.Column="1" VerticalAlignment="Center">
|
||||
<Image x:Name="PreviewImage" MinHeight="150" Stretch="Uniform" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
|
||||
<Grid x:Name="PreviewImageOverlay"/>
|
||||
</Grid>
|
||||
</Grid>
|
||||
</Window>
|
||||
@@ -0,0 +1,290 @@
|
||||
using Microsoft.Win32;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Controls.Primitives;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Effects;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace RBG_Server.WPF
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for MainWindow.xaml
|
||||
/// </summary>
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
GameServer gameServer;
|
||||
MemoryStream gameBoard;
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
//gameServer = new GameServer(8, 0, 9, 9, 3, 3, 3, 3, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Browse for file button
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void BrowseButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
OpenFileDialog ofd = new();
|
||||
ofd.Filter = "Image Files (.png; .jpg)|*.png;*.jpg";
|
||||
if (ofd.ShowDialog() == true)
|
||||
{
|
||||
ImageSourceTextBox.Text = ofd.FileName;
|
||||
UpdateImage();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update the image when the text box looses focus
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ImageSourceTextBox_LostFocus(object sender, RoutedEventArgs e)
|
||||
{
|
||||
UpdateImage();
|
||||
}
|
||||
|
||||
private void UpdateImage()
|
||||
{
|
||||
try
|
||||
{
|
||||
using FileStream fs = File.OpenRead(ImageSourceTextBox.Text);
|
||||
gameBoard = new();
|
||||
fs.CopyTo(gameBoard); // Load into memory, so the server can also utilise the stream
|
||||
|
||||
BitmapImage gameBoardImage = new();
|
||||
gameBoardImage.BeginInit();
|
||||
gameBoardImage.StreamSource = gameBoard;
|
||||
//gameBoardImage.CacheOption = BitmapCacheOption.OnLoad; // Must preload the image into memory, before it is unloaded
|
||||
gameBoardImage.EndInit();
|
||||
PreviewImage.Source = gameBoardImage;
|
||||
RedrawGrid();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// Creates and starts the game server
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <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);
|
||||
}
|
||||
|
||||
|
||||
private void NumberOfRows_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
|
||||
{
|
||||
if (IsInitialized)
|
||||
{
|
||||
StartingRow.Maximum = NumberOfRows.Value;
|
||||
ZoomBoxStartRow.Maximum = NumberOfRows.Value;
|
||||
ZoomBoxSpan.Maximum = Math.Min(NumberOfRows.Value - ZoomBoxStartRow.Value, NumberOfColumns.Value - ZoomBoxStartColumn.Value);
|
||||
RedrawGrid();
|
||||
}
|
||||
}
|
||||
|
||||
private void NumberOfColumns_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
|
||||
{
|
||||
if (IsInitialized)
|
||||
{
|
||||
StartingColumn.Maximum = NumberOfColumns.Value;
|
||||
ZoomBoxStartColumn.Maximum = NumberOfColumns.Value;
|
||||
ZoomBoxSpan.Maximum = Math.Min(NumberOfRows.Value - ZoomBoxStartRow.Value, NumberOfColumns.Value - ZoomBoxStartColumn.Value);
|
||||
RedrawGrid();
|
||||
}
|
||||
}
|
||||
|
||||
private void RedrawGrid()
|
||||
{
|
||||
SolidColorBrush borderBrush = new(Color.FromRgb(255, 0, 0));
|
||||
PreviewImageOverlay.RowDefinitions.Clear();
|
||||
PreviewImageOverlay.ColumnDefinitions.Clear();
|
||||
PreviewImageOverlay.Children.Clear();
|
||||
for (int i = 0; i < NumberOfColumns.Value; i++)
|
||||
{
|
||||
PreviewImageOverlay.ColumnDefinitions.Add(new ColumnDefinition());
|
||||
}
|
||||
for (int i = 0; i < NumberOfRows.Value; i++)
|
||||
{
|
||||
PreviewImageOverlay.RowDefinitions.Add(new RowDefinition());
|
||||
}
|
||||
// Draw the overlay
|
||||
Rectangle overlay = new();
|
||||
overlay.Fill = new SolidColorBrush(Color.FromArgb(128, 0, 128, 255));
|
||||
_ = PreviewImageOverlay.Children.Add(overlay);
|
||||
Grid.SetRow(overlay, (int)ZoomBoxStartRow.Value);
|
||||
Grid.SetColumn(overlay, (int)ZoomBoxStartColumn.Value);
|
||||
Grid.SetRowSpan(overlay, (int)ZoomBoxSpan.Value);
|
||||
Grid.SetColumnSpan(overlay, (int)ZoomBoxSpan.Value);
|
||||
// Draw the starting position
|
||||
overlay = new();
|
||||
overlay.Fill = new SolidColorBrush(Color.FromArgb(64, 0, 255, 128));
|
||||
_ = PreviewImageOverlay.Children.Add(overlay);
|
||||
Grid.SetRow(overlay, (int)StartingRow.Value);
|
||||
Grid.SetColumn(overlay, (int)StartingColumn.Value);
|
||||
// Draw grids onto the preview image
|
||||
for (int v = 0; v < PreviewImageOverlay.RowDefinitions.Count; v++)
|
||||
{
|
||||
for (int u = 0; u < PreviewImageOverlay.ColumnDefinitions.Count; u++)
|
||||
{
|
||||
Border border = new();
|
||||
border.BorderBrush = borderBrush;
|
||||
border.BorderThickness = new Thickness(2);
|
||||
_ = PreviewImageOverlay.Children.Add(border);
|
||||
Grid.SetRow(border, v);
|
||||
Grid.SetColumn(border, u);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates elements to go inside the game grid from the provided list of players
|
||||
/// The grid should be appropriately sized before using this
|
||||
/// </summary>
|
||||
private void GenerateGameBoard(List<Player> players)
|
||||
{
|
||||
SolidColorBrush buttonBrush = new(Color.FromArgb(1, 255, 255, 255));
|
||||
Grid[] cells = new Grid[PreviewImageOverlay.RowDefinitions.Count * PreviewImageOverlay.ColumnDefinitions.Count];
|
||||
List<Player> unaddedPlayers = players;
|
||||
for (int i = 0; i < cells.Length; i++)
|
||||
{
|
||||
cells[i] = new Grid();
|
||||
// Each cell has several components:
|
||||
// Stackpanel, which has each sprite in the cell
|
||||
UniformGrid cellStack = new();
|
||||
cellStack.Columns = 4;
|
||||
//cellStack.ItemWidth = cellStack.Width / 4;
|
||||
Queue<Player> removedPlayers = new();
|
||||
foreach (Player item in unaddedPlayers)
|
||||
{
|
||||
if ((item.Row * PreviewImageOverlay.ColumnDefinitions.Count) + item.Column == i)
|
||||
{
|
||||
removedPlayers.Enqueue(item);
|
||||
}
|
||||
}
|
||||
while (removedPlayers.Count > 0)
|
||||
{
|
||||
Player removed = removedPlayers.Dequeue();
|
||||
_ = unaddedPlayers.Remove(removed);
|
||||
_ = cellStack.Children.Add(removed);
|
||||
}
|
||||
_ = cells[i].Children.Add(cellStack);
|
||||
// Button, whose content is empty
|
||||
Button cellButton = new()
|
||||
{
|
||||
Content = ""
|
||||
};
|
||||
cellButton.Background = buttonBrush;
|
||||
cellButton.Tag = new Tuple<int, int>(i / PreviewImageOverlay.ColumnDefinitions.Count, i % PreviewImageOverlay.ColumnDefinitions.Count); // Tag is a Tuple<row, column>
|
||||
cellButton.Click += CellButton_Click;
|
||||
_ = cells[i].Children.Add(cellButton);
|
||||
_ = PreviewImageOverlay.Children.Add(cells[i]);
|
||||
Grid.SetColumn(cells[i], i % PreviewImageOverlay.ColumnDefinitions.Count);
|
||||
Grid.SetRow(cells[i], i / PreviewImageOverlay.ColumnDefinitions.Count);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Represents a cell on the board
|
||||
/// </summary>
|
||||
class BoardCell
|
||||
{
|
||||
// brush the button should have
|
||||
static SolidColorBrush buttonBrush = new(Color.FromArgb(1, 255, 255, 255));
|
||||
|
||||
// Each cell has a uniform grid that
|
||||
UniformGrid cellStack = new()
|
||||
{
|
||||
Columns = 4,
|
||||
HorizontalAlignment = HorizontalAlignment.Stretch,
|
||||
VerticalAlignment = VerticalAlignment.Stretch
|
||||
};
|
||||
Button cellButton = new()
|
||||
{
|
||||
Content = ""
|
||||
};
|
||||
Queue<Player> removedPlayers = new();
|
||||
Grid parentGrid;
|
||||
|
||||
public BoardCell(Grid parentGrid)
|
||||
{
|
||||
this.parentGrid = parentGrid;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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];
|
||||
List<Player> unaddedPlayers = players;
|
||||
for (int i = 0; i < cells.Length; i++)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void CellButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
Button cellButton = sender as Button;
|
||||
Console.WriteLine("Button at {0}, {1} was clicked", ((Tuple<int, int>)cellButton.Tag).Item2, ((Tuple<int, int>)cellButton.Tag).Item1);
|
||||
}
|
||||
|
||||
private void Sliders_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
|
||||
{
|
||||
if (IsInitialized)
|
||||
{
|
||||
RedrawGrid();
|
||||
}
|
||||
}
|
||||
|
||||
private void GridVisibilityToggleButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
PreviewImageOverlay.Visibility = PreviewImageOverlay.Visibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
|
||||
}
|
||||
|
||||
private static readonly string[] sprites = new string[]
|
||||
{
|
||||
"01.png",
|
||||
"02.png",
|
||||
"03.png",
|
||||
"04.png",
|
||||
};
|
||||
private void DrawRandomizedPlayers_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
List<Player> randPlayers = new();
|
||||
Random rand = new();
|
||||
int count = rand.Next(1, 10);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
// Create a new player at a random location
|
||||
Player p = new("Player " + i, rand.Next(0, PreviewImageOverlay.RowDefinitions.Count), rand.Next(PreviewImageOverlay.ColumnDefinitions.Count));
|
||||
// Load the sprite
|
||||
BitmapSource pSprite = new BitmapImage(new Uri("pack://application:,,,/Sprites/" + sprites[rand.Next(0, sprites.Length)]));
|
||||
// Recolour:
|
||||
if (rand.Next(1, 4) == 1)
|
||||
{
|
||||
pSprite = RBG.Helpers.ImageProcessing.UpdatePixelColours(pSprite, 0.0, 0.0, 1.0);
|
||||
}
|
||||
p.Source = pSprite;
|
||||
randPlayers.Add(p);
|
||||
}
|
||||
|
||||
GenerateGameBoard(randPlayers);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net5.0-windows10.0.19041.0</TargetFramework>
|
||||
<UseWPF>true</UseWPF>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="..\.editorconfig" Link=".editorconfig" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\RBG.Helpers\RBG.Helpers.csproj" />
|
||||
<ProjectReference Include="..\RBG_Server.Core\RBG_Server.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user