From 2aeca40aa4281ece095268aa18412fda618d4856 Mon Sep 17 00:00:00 2001 From: Lars Date: Tue, 20 Oct 2020 23:52:10 +0200 Subject: [PATCH] its not working yet, its getting a System.OutOfMemoryException, needs to be fixed --- Client/ViewModels/ViewModelGame.cs | 61 +++++++++++++++++++++++++++++- Client/Views/GameWindow.xaml.cs | 49 ++++-------------------- Server/Models/ServerClient.cs | 2 + 3 files changed, 68 insertions(+), 44 deletions(-) diff --git a/Client/ViewModels/ViewModelGame.cs b/Client/ViewModels/ViewModelGame.cs index 2d12d4f..bf2a0a6 100644 --- a/Client/ViewModels/ViewModelGame.cs +++ b/Client/ViewModels/ViewModelGame.cs @@ -1,10 +1,67 @@ -using System.Collections.ObjectModel; +using Client.Views; +using SharedClientServer; +using System.Collections.ObjectModel; using System.ComponentModel; - +using System.Windows; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Shapes; + namespace Client.ViewModels { class ViewModelGame : INotifyPropertyChanged { + private ClientData data = ClientData.Instance; + public event PropertyChangedEventHandler PropertyChanged; + + private Point currentPoint = new Point(); + private Color color; + + public void Canvas_MouseDown(MouseButtonEventArgs e, GameWindow window) + { + if (e.ButtonState == MouseButtonState.Pressed) + { + currentPoint = e.GetPosition(window.CanvasForPaint); + } + } + + public void Canvas_MouseMove(MouseEventArgs e, GameWindow window) + { + if (e.LeftButton == MouseButtonState.Pressed) + { + double[] coordinates = new double[4]; + Line line = new Line(); + + line.Stroke = new SolidColorBrush(color); + //line.Stroke = SystemColors.WindowFrameBrush; + line.X1 = currentPoint.X; + line.Y1 = currentPoint.Y; + line.X2 = e.GetPosition(window.CanvasForPaint).X; + line.Y2 = e.GetPosition(window.CanvasForPaint).Y; + coordinates[0] = line.X1; + coordinates[1] = line.Y1; + coordinates[2] = line.X2; + coordinates[3] = line.Y2; + currentPoint = e.GetPosition(window.CanvasForPaint); + + window.CanvasForPaint.Children.Add(line); + data.Client.SendMessage(JSONConvert.GetMessageToSend(0x04, coordinates)); + } + } + + public void Color_Picker(RoutedPropertyChangedEventArgs e, GameWindow window) + { + Color colorSelected = new Color(); + colorSelected.A = 255; + colorSelected.R = window.ClrPcker_Background.SelectedColor.Value.R; + colorSelected.G = window.ClrPcker_Background.SelectedColor.Value.G; + colorSelected.B = window.ClrPcker_Background.SelectedColor.Value.B; + color = colorSelected; + } + + + + } } diff --git a/Client/Views/GameWindow.xaml.cs b/Client/Views/GameWindow.xaml.cs index aacc67e..70e6c63 100644 --- a/Client/Views/GameWindow.xaml.cs +++ b/Client/Views/GameWindow.xaml.cs @@ -19,54 +19,24 @@ namespace Client.Views public partial class GameWindow : Window { ClientData data = ClientData.Instance; + private ViewModelGame viewModel; public GameWindow() { - DataContext = new ViewModelGame(); + this.viewModel = new ViewModelGame(); + DataContext = this.viewModel; InitializeComponent(); } - Point currentPoint = new Point(); + private void CanvasForPaint_MouseDown(object sender, MouseButtonEventArgs e) { - if (e.ButtonState == MouseButtonState.Pressed) - { - currentPoint = e.GetPosition(CanvasForPaint); - } - + this.viewModel.Canvas_MouseDown(e, this); } private void CanvasForPaint_MouseMove(object sender, MouseEventArgs e) { - if (e.LeftButton == MouseButtonState.Pressed) - { - Line line = new Line(); - - - line.Stroke = new SolidColorBrush(color); - //line.Stroke = SystemColors.WindowFrameBrush; - line.X1 = currentPoint.X; - line.Y1 = currentPoint.Y; - line.X2 = e.GetPosition(CanvasForPaint).X; - line.Y2 = e.GetPosition(CanvasForPaint).Y; - - currentPoint = e.GetPosition(CanvasForPaint); - - CanvasForPaint.Children.Add(line); - } - - } - - private Color color; - - private void ClrPcker_Background_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs e) - { - Color colorSelected = new Color(); - colorSelected.A = 255; - colorSelected.R = ClrPcker_Background.SelectedColor.Value.R; - colorSelected.G = ClrPcker_Background.SelectedColor.Value.G; - colorSelected.B = ClrPcker_Background.SelectedColor.Value.B; - color = colorSelected; + viewModel.Canvas_MouseMove(e, this); } private void CanvasReset_Click(object sender, RoutedEventArgs e) @@ -87,12 +57,7 @@ namespace Client.Views private void ClrPcker_Background_SelectedColorChanged_1(object sender, RoutedPropertyChangedEventArgs e) { - Color colorSelected = new Color(); - colorSelected.A = 255; - colorSelected.R = ClrPcker_Background.SelectedColor.Value.R; - colorSelected.G = ClrPcker_Background.SelectedColor.Value.G; - colorSelected.B = ClrPcker_Background.SelectedColor.Value.B; - color = colorSelected; + viewModel.Color_Picker(e, this); } private void ChatBox_KeyDown(object sender, KeyEventArgs e) diff --git a/Server/Models/ServerClient.cs b/Server/Models/ServerClient.cs index b10639b..9b07b52 100644 --- a/Server/Models/ServerClient.cs +++ b/Server/Models/ServerClient.cs @@ -82,6 +82,7 @@ namespace Server.Models // start reading for a new message stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null); + } /// @@ -123,6 +124,7 @@ namespace Server.Models // lobby data break; case JSONConvert.CANVAS: + Debug.WriteLine("GOT A MESSAGE FROM THE CLIENT ABOUT THE CANVAS!!!"); // canvas data // todo send canvas data to all other serverclients in lobby break;