Skip to content

Commit

Permalink
General misfunctionings (#27)
Browse files Browse the repository at this point in the history
* started fix

* fixed map async calls

* incremented versions
  • Loading branch information
joshika39 authored Dec 11, 2023
1 parent 4ae10a0 commit 58cf2b3
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 55 deletions.
41 changes: 19 additions & 22 deletions GameFramework.Impl/Map/Source/JsonMapSource2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,26 @@ namespace GameFramework.Impl.Map.Source
{
public class JsonMapSource2D<T> : 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<IStaticObject2D> MapObjects { get; protected set; }
public sealed override ICollection<IInteractableObject2D> 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<IInteractableObject2D> units, int col, int row)
{
Units = units ?? throw new ArgumentNullException(nameof(units));

filePath = filePath ?? throw new ArgumentNullException(nameof(filePath));
Query = provider.GetRequiredService<IConfigurationQueryFactory>().CreateConfigurationQuery(filePath);
_positionFactory = provider.GetRequiredService<IPositionFactory>();
_reader = provider.GetRequiredService<IReader>();
_tileConverter = provider.GetRequiredService<IStaticObject2DConverter>();
PositionFactory = provider.GetRequiredService<IPositionFactory>();
Reader = provider.GetRequiredService<IReader>();
TileConverter = provider.GetRequiredService<IStaticObject2DConverter>();
ColumnCount = col;
RowCount = row;
_mapDataBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(GetRawData(data)));
MapDataBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(GetRawData(data)));
MapObjects = ConvertDataToObjects();
}

Expand All @@ -44,12 +41,12 @@ protected JsonMapSource2D(IServiceProvider provider, string filePath)
var queryFactory = provider.GetRequiredService<IConfigurationQueryFactory>();
filePath = filePath ?? throw new ArgumentNullException(nameof(filePath));
Query = queryFactory.CreateConfigurationQuery(filePath);
_positionFactory = provider.GetRequiredService<IPositionFactory>();
_reader = provider.GetRequiredService<IReader>();
_tileConverter = provider.GetRequiredService<IStaticObject2DConverter>();
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<IReader>();
PositionFactory = provider.GetRequiredService<IPositionFactory>();
TileConverter = provider.GetRequiredService<IStaticObject2DConverter>();
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<List<IInteractableObject2D>>("units") ?? new List<IInteractableObject2D>();
MapObjects = ConvertDataToObjects();
Expand All @@ -62,27 +59,27 @@ public override void SaveLayout(IEnumerable<IStaticObject2D> updatedMapObjects,
Query.SetObject("units", Units);
}

private IEnumerable<IStaticObject2D> ConvertDataToObjects()
protected IEnumerable<IStaticObject2D> 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<int>(stream, int.TryParse, ' ').ToList();
var mapLayout = Reader.ReadAllLines<int>(stream, int.TryParse, ' ').ToList();
var list = new List<IStaticObject2D>();
for (var i = 0; i < mapLayout.Count; i++)
{
var row = mapLayout[i].ToList();
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;
Expand Down
17 changes: 11 additions & 6 deletions GameFramework.UI.Forms/Map/FormsMapControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -89,8 +94,8 @@ private void UpdateEntities()
{
entityView.Attach(this as IViewLoadedSubscriber);
entityView.Attach(this as IViewDisposedSubscriber);
Controls.Add(shape);

Invoke(() => Controls.Add(shape));
}
}
}
Expand All @@ -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));
}
}
}
Expand Down
28 changes: 17 additions & 11 deletions GameFramework.UI.Maui/Map/AMauiMapControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public abstract class AMauiMapControl : AbsoluteLayout, IMapView2D, IViewDispose
private ObservableCollection<IDisposableStaticObjectView> _disposableObjectViews;
private ObservableCollection<IStaticObject2D> _mapObjects;
protected readonly ICollection<IMouseHandler> MouseHandlers = new List<IMouseHandler>();

public ObservableCollection<IDisposableStaticObjectView> DisposableObjectViews
{
get => _disposableObjectViews;
Expand Down Expand Up @@ -39,7 +39,7 @@ protected AMauiMapControl()
DisposableObjectViews.CollectionChanged += (_, _) => UpdateEntities();
MapObjects.CollectionChanged += (_, _) => UpdateMapObjects();
}

public void Attach(IMouseHandler mouseHandler)
{
MouseHandlers.Add(mouseHandler);
Expand All @@ -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());
}
}
}
}
30 changes: 18 additions & 12 deletions GameFramework.UI.WPF/Map/WpfMapControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class WpfMapControl : Canvas, IMapView2D, IViewDisposedSubscriber
private ObservableCollection<IDisposableStaticObjectView> _disposableObjectViews;
private ObservableCollection<IStaticObject2D> _mapObjects;
private readonly ICollection<IMouseHandler> _mouseHandlers = new List<IMouseHandler>();

public ObservableCollection<IDisposableStaticObjectView> DisposableObjectViews
{
get => _disposableObjectViews;
Expand All @@ -36,28 +36,33 @@ public ObservableCollection<IStaticObject2D> MapObjects
UpdateMapObjects();
}
}

public WpfMapControl()
{
DisposableObjectViews = _disposableObjectViews = new ObservableCollection<IDisposableStaticObjectView>();
MapObjects = _mapObjects = new ObservableCollection<IStaticObject2D>();
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);
Expand All @@ -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);
Expand All @@ -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));
}
}
}
}
}
}
1 change: 1 addition & 0 deletions GameFramework/Visuals/Views/IMapView2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public interface IMapView2D
public ObservableCollection<IStaticObject2D> MapObjects { get; set; }
public ObservableCollection<IDisposableStaticObjectView> DisposableObjectViews { get; set; }
void Attach(IMouseHandler mouseHandler);
void Clear();
}
}
8 changes: 4 additions & 4 deletions Nuget/.projects.conf
Original file line number Diff line number Diff line change
@@ -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
GameFramework.Core:3.0.1
GameFramework.UI.WPF:3.0.1
GameFramework.UI.Forms:3.0.1
GameFramework.UI.Maui:3.0.1

0 comments on commit 58cf2b3

Please sign in to comment.