using Microsoft.Win32;
using RBG.Helpers;
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;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace RBG_Server.WPF
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
// Global Styles etc
private static readonly SolidColorBrush borderBrush = new(Color.FromRgb(255, 0, 0));
GameServer gameServer;
// Game board bytes
byte[] gameBoard;
public MainWindow()
{
InitializeComponent();
}
///
/// Draws the overlay grid, clearing existing elements if necessary
///
//private void RedrawGrid()
//{
// 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);
// }
// }
//}
///
/// Generates the game board overlay grid
/// The grid should be appropriately sized before using this
///
private void GenerateGameBoard(List players)
{
// Create an array of BoardCells Rows*Columns in size
BoardCell[] cells = new BoardCell[PreviewImageOverlay.RowDefinitions.Count * PreviewImageOverlay.ColumnDefinitions.Count];
List unaddedPlayers = players;
for (int i = 0; i < cells.Length; i++)
{
cells[i] = new BoardCell();
Queue 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);
_ = cells[i].CellStack.Children.Add(removed);
}
_ = PreviewImageOverlay.Children.Add(cells[i]);
cells[i].CellButton.Tag = new Tuple(i % PreviewImageOverlay.ColumnDefinitions.Count, i / PreviewImageOverlay.ColumnDefinitions.Count);
Grid.SetColumn(cells[i], i % PreviewImageOverlay.ColumnDefinitions.Count);
Grid.SetRow(cells[i], i / PreviewImageOverlay.ColumnDefinitions.Count);
}
}
///
/// Represents a cell on the board; as a grid
///
class BoardCell : Grid
{
// brush the button should have
static SolidColorBrush buttonBrush = new(Color.FromArgb(1, 255, 255, 255));
// Each cell has a uniform grid that
public UniformGrid CellStack { get; } = new()
{
Columns = 4,
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch
};
public Button CellButton { get; } = new()
{
Content = "",
};
Queue removedPlayers = new();
// Grid parentGrid; // Parent grid is now this itself
public BoardCell() : base()
{
CellButton.MouseEnter += CellButton_MouseEnter;
CellButton.MouseLeave += CellButton_MouseLeave;
CellButton.Click += CellButton_Click;
CellButton.Background = buttonBrush;
CellStack.Columns = 2;
CellStack.VerticalAlignment = VerticalAlignment.Top;
// Add our grid and button
_ = Children.Add(CellStack);
_ = Children.Add(CellButton);
}
private void CellButton_Click(object sender, RoutedEventArgs e)
{
Button cellButton = sender as Button;
System.Diagnostics.Debug.WriteLine("Button at {0}, {1} was clicked", ((Tuple)cellButton.Tag).Item2, ((Tuple)cellButton.Tag).Item1);
}
private void CellButton_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
//throw new NotImplementedException();
}
private void CellButton_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
//throw new NotImplementedException();
}
}
}
}