From 58cf2b32bab0aefbf6f952d317188edc8503d4cc Mon Sep 17 00:00:00 2001 From: Joshua Hegedus <47304430+joshika39@users.noreply.github.com> Date: Mon, 11 Dec 2023 16:35:50 +0100 Subject: [PATCH] General misfunctionings (#27) * started fix * fixed map async calls * incremented versions --- .../Map/Source/JsonMapSource2D.cs | 41 +++++++++---------- GameFramework.UI.Forms/Map/FormsMapControl.cs | 17 +++++--- GameFramework.UI.Maui/Map/AMauiMapControl.cs | 28 ++++++++----- GameFramework.UI.WPF/Map/WpfMapControl.cs | 30 ++++++++------ GameFramework/Visuals/Views/IMapView2D.cs | 1 + Nuget/.projects.conf | 8 ++-- 6 files changed, 70 insertions(+), 55 deletions(-) diff --git a/GameFramework.Impl/Map/Source/JsonMapSource2D.cs b/GameFramework.Impl/Map/Source/JsonMapSource2D.cs index 3393185..8bf4539 100644 --- a/GameFramework.Impl/Map/Source/JsonMapSource2D.cs +++ b/GameFramework.Impl/Map/Source/JsonMapSource2D.cs @@ -13,29 +13,26 @@ namespace GameFramework.Impl.Map.Source { public class JsonMapSource2D : AMapSource2D where T : struct, Enum { - private readonly IReader _reader; - private readonly IPositionFactory _positionFactory; - private readonly string _mapDataBase64; - private readonly IStaticObject2DConverter _tileConverter; - public sealed override IEnumerable MapObjects { get; protected set; } public sealed override ICollection Units { get; protected set; } + protected readonly IReader Reader; protected readonly IConfigurationQuery Query; - + protected readonly IPositionFactory PositionFactory; + protected readonly IStaticObject2DConverter TileConverter; + protected readonly string MapDataBase64; protected JsonMapSource2D(IServiceProvider provider, string filePath, int[,] data, ICollection units, int col, int row) { Units = units ?? throw new ArgumentNullException(nameof(units)); - filePath = filePath ?? throw new ArgumentNullException(nameof(filePath)); Query = provider.GetRequiredService().CreateConfigurationQuery(filePath); - _positionFactory = provider.GetRequiredService(); - _reader = provider.GetRequiredService(); - _tileConverter = provider.GetRequiredService(); + PositionFactory = provider.GetRequiredService(); + Reader = provider.GetRequiredService(); + TileConverter = provider.GetRequiredService(); ColumnCount = col; RowCount = row; - _mapDataBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(GetRawData(data))); + MapDataBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(GetRawData(data))); MapObjects = ConvertDataToObjects(); } @@ -44,12 +41,12 @@ protected JsonMapSource2D(IServiceProvider provider, string filePath) var queryFactory = provider.GetRequiredService(); filePath = filePath ?? throw new ArgumentNullException(nameof(filePath)); Query = queryFactory.CreateConfigurationQuery(filePath); - _positionFactory = provider.GetRequiredService(); - _reader = provider.GetRequiredService(); - _tileConverter = provider.GetRequiredService(); - ColumnCount = Query.GetIntAttribute("row") ?? throw new InvalidOperationException("Draft config is missing a 'row;"); - RowCount = Query.GetIntAttribute("col") ?? throw new InvalidOperationException("Draft config is missing a 'col'"); - _mapDataBase64 = Query.GetStringAttribute("data") ?? throw new InvalidOperationException("Draft config is missing the 'data'"); + Reader = provider.GetRequiredService(); + PositionFactory = provider.GetRequiredService(); + TileConverter = provider.GetRequiredService(); + ColumnCount = Query.GetIntAttribute("col") ?? throw new InvalidOperationException("Draft config is missing a 'row;"); + RowCount = Query.GetIntAttribute("row") ?? throw new InvalidOperationException("Draft config is missing a 'col'"); + MapDataBase64 = Query.GetStringAttribute("data") ?? throw new InvalidOperationException("Draft config is missing the 'data'"); Units = Query.GetObject>("units") ?? new List(); MapObjects = ConvertDataToObjects(); @@ -62,14 +59,14 @@ public override void SaveLayout(IEnumerable updatedMapObjects, Query.SetObject("units", Units); } - private IEnumerable ConvertDataToObjects() + protected IEnumerable ConvertDataToObjects() { var id = Guid.NewGuid(); var tempPath = Path.Join(Path.GetTempPath(), $"{id}.txt"); File.Create(tempPath).Close(); - File.WriteAllText(tempPath, Encoding.UTF8.GetString(Convert.FromBase64String(_mapDataBase64))); + File.WriteAllText(tempPath, Encoding.UTF8.GetString(Convert.FromBase64String(MapDataBase64))); using var stream = new StreamReader(tempPath); - var mapLayout = _reader.ReadAllLines(stream, int.TryParse, ' ').ToList(); + var mapLayout = Reader.ReadAllLines(stream, int.TryParse, ' ').ToList(); var list = new List(); for (var i = 0; i < mapLayout.Count; i++) { @@ -77,12 +74,12 @@ private IEnumerable ConvertDataToObjects() for (var j = 0; j < row.Count; j++) { var value = row[j]; - var position = _positionFactory.CreatePosition(j, i); + var position = PositionFactory.CreatePosition(j, i); if (!Enum.TryParse(value.ToString(), out T type)) { continue; } - list.Add(_tileConverter.FromEnum(type, position)); + list.Add(TileConverter.FromEnum(type, position)); } } return list; diff --git a/GameFramework.UI.Forms/Map/FormsMapControl.cs b/GameFramework.UI.Forms/Map/FormsMapControl.cs index c1275e4..f359130 100644 --- a/GameFramework.UI.Forms/Map/FormsMapControl.cs +++ b/GameFramework.UI.Forms/Map/FormsMapControl.cs @@ -53,11 +53,16 @@ public virtual void Attach(IMouseHandler mouseHandler) _mouseHandlers.Add(mouseHandler); } + public virtual void Clear() + { + Invoke(() => Controls.Clear()); + } + public virtual void OnViewDisposed(IDisposableStaticObjectView view) { if (view is Control control) { - Controls.Remove(control); + Invoke(() => Controls.Remove(control)); } } @@ -89,8 +94,8 @@ private void UpdateEntities() { entityView.Attach(this as IViewLoadedSubscriber); entityView.Attach(this as IViewDisposedSubscriber); - - Controls.Add(shape); + + Invoke(() => Controls.Add(shape)); } } } @@ -101,16 +106,16 @@ private void UpdateMapObjects() { if (mapObject.View is Control shape && !Controls.Contains(shape)) { - Controls.Add(shape); + Invoke(() => Controls.Add(shape)); } } } - public void OnLoaded(IMovingObjectView view) + public virtual void OnLoaded(IMovingObjectView view) { if (view is Control shape && !Controls.Contains(shape)) { - Controls.Add(shape); + Invoke(() => Controls.Add(shape)); } } } diff --git a/GameFramework.UI.Maui/Map/AMauiMapControl.cs b/GameFramework.UI.Maui/Map/AMauiMapControl.cs index d19d347..2e6aab7 100644 --- a/GameFramework.UI.Maui/Map/AMauiMapControl.cs +++ b/GameFramework.UI.Maui/Map/AMauiMapControl.cs @@ -11,7 +11,7 @@ public abstract class AMauiMapControl : AbsoluteLayout, IMapView2D, IViewDispose private ObservableCollection _disposableObjectViews; private ObservableCollection _mapObjects; protected readonly ICollection MouseHandlers = new List(); - + public ObservableCollection DisposableObjectViews { get => _disposableObjectViews; @@ -39,7 +39,7 @@ protected AMauiMapControl() DisposableObjectViews.CollectionChanged += (_, _) => UpdateEntities(); MapObjects.CollectionChanged += (_, _) => UpdateMapObjects(); } - + public void Attach(IMouseHandler mouseHandler) { MouseHandlers.Add(mouseHandler); @@ -49,31 +49,37 @@ public virtual void OnViewDisposed(IDisposableStaticObjectView view) { if (view is BoxView shape) { - Children.Remove(shape); + Dispatcher.Dispatch(() => Children.Remove(shape)); } } - - private void UpdateEntities() + + protected virtual void UpdateEntities() { foreach (var entityView in DisposableObjectViews) { if (entityView is BoxView shape && !Children.Contains(shape)) { - Children.Add(shape); + Dispatcher.Dispatch(() => Children.Add(shape)); } + entityView.Attach(this); } } - - private void UpdateMapObjects() + + protected virtual void UpdateMapObjects() { foreach (var mapObject in MapObjects) { if (mapObject.View is BoxView shape && !Children.Contains(shape)) { - Children.Add(shape); - } + Dispatcher.Dispatch(() => Children.Add(shape)); + } } } + + public new virtual void Clear() + { + Dispatcher.Dispatch(() => Children.Clear()); + } } -} +} \ No newline at end of file diff --git a/GameFramework.UI.WPF/Map/WpfMapControl.cs b/GameFramework.UI.WPF/Map/WpfMapControl.cs index 718d82d..b22bf08 100644 --- a/GameFramework.UI.WPF/Map/WpfMapControl.cs +++ b/GameFramework.UI.WPF/Map/WpfMapControl.cs @@ -16,7 +16,7 @@ public class WpfMapControl : Canvas, IMapView2D, IViewDisposedSubscriber private ObservableCollection _disposableObjectViews; private ObservableCollection _mapObjects; private readonly ICollection _mouseHandlers = new List(); - + public ObservableCollection DisposableObjectViews { get => _disposableObjectViews; @@ -36,7 +36,7 @@ public ObservableCollection MapObjects UpdateMapObjects(); } } - + public WpfMapControl() { DisposableObjectViews = _disposableObjectViews = new ObservableCollection(); @@ -44,20 +44,25 @@ public WpfMapControl() DisposableObjectViews.CollectionChanged += (_, _) => UpdateEntities(); MapObjects.CollectionChanged += (_, _) => UpdateMapObjects(); } - + public void Attach(IMouseHandler mouseHandler) { _mouseHandlers.Add(mouseHandler); } + public virtual void Clear() + { + Dispatcher.BeginInvoke(() => Children.Clear()); + } + public void OnViewDisposed(IDisposableStaticObjectView view) { if (view is Shape shape) { - Children.Remove(shape); + Dispatcher.BeginInvoke(() => Children.Remove(shape)); } } - + protected override void OnPreviewMouseMove(MouseEventArgs e) { base.OnPreviewMouseMove(e); @@ -67,7 +72,7 @@ protected override void OnPreviewMouseMove(MouseEventArgs e) mouseHandler.OnMouseMove(new ScreenSpacePosition(position.X, position.Y)); } } - + protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) { base.OnMouseLeftButtonDown(e); @@ -77,28 +82,29 @@ protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) mouseHandler.OnMouseLeftClick(new ScreenSpacePosition(position.X, position.Y)); } } - + private void UpdateEntities() { foreach (var entityView in DisposableObjectViews) { if (entityView is Shape shape && !Children.Contains(shape)) { - Children.Add(shape); + Dispatcher.BeginInvoke(() => Children.Add(shape)); } + entityView.Attach(this); } } - + private void UpdateMapObjects() { foreach (var mapObject in MapObjects) { if (mapObject.View is Shape shape && !Children.Contains(shape)) { - Children.Add(shape); - } + Dispatcher.BeginInvoke(() => Children.Add(shape)); + } } } } -} +} \ No newline at end of file diff --git a/GameFramework/Visuals/Views/IMapView2D.cs b/GameFramework/Visuals/Views/IMapView2D.cs index f837ce1..b42ef55 100644 --- a/GameFramework/Visuals/Views/IMapView2D.cs +++ b/GameFramework/Visuals/Views/IMapView2D.cs @@ -10,5 +10,6 @@ public interface IMapView2D public ObservableCollection MapObjects { get; set; } public ObservableCollection DisposableObjectViews { get; set; } void Attach(IMouseHandler mouseHandler); + void Clear(); } } diff --git a/Nuget/.projects.conf b/Nuget/.projects.conf index bfa09b3..23b3107 100644 --- a/Nuget/.projects.conf +++ b/Nuget/.projects.conf @@ -1,4 +1,4 @@ -# GameFramework.Core:3.0.0 -GameFramework.UI.WPF:3.0.0 -GameFramework.UI.Forms:3.0.0 -GameFramework.UI.Maui:3.0.0 \ No newline at end of file +GameFramework.Core:3.0.1 +GameFramework.UI.WPF:3.0.1 +GameFramework.UI.Forms:3.0.1 +GameFramework.UI.Maui:3.0.1 \ No newline at end of file