Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Mappy/Mappy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,14 @@
<Compile Include="IO\SectionLoadingUtils.cs" />
<Compile Include="MappySettings.cs" />
<Compile Include="Maybe.cs" />
<Compile Include="Models\BandboxBehaviours\FreeBandboxBehaviour.cs" />
<Compile Include="Models\BandboxBehaviours\IBandboxBehaviour.cs" />
<Compile Include="Models\BandboxBehaviours\IBandboxModel.cs" />
<Compile Include="Models\BandboxBehaviours\TileBandboxBehaviour.cs" />
<Compile Include="Models\BandboxMode.cs" />
<Compile Include="Models\ComboBoxViewModel.cs" />
<Compile Include="Models\CoreModel.cs" />
<Compile Include="Models\Enums\GUITab.cs" />
<Compile Include="Models\FeatureClipboardRecord.cs" />
<Compile Include="Models\FeatureInstance.cs" />
<Compile Include="Models\FeatureInstanceEventArgs.cs" />
Expand Down
88 changes: 88 additions & 0 deletions Mappy/Models/BandboxBehaviours/FreeBandboxBehaviour.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
namespace Mappy.Models.BandboxBehaviours
{
using System;
using System.Drawing;

using Mappy.Util;

/// <summary>
/// Defines a free-form bandbox behaviour.
/// In this mode, dragging out a bandbox selects features within an
/// area of the map
/// </summary>
public class FreeBandboxBehaviour : Notifier, IBandboxBehaviour
{
private readonly IBandboxModel model;

private Rectangle bandboxRectangle;

private Point startPoint;

private Point finishPoint;

public FreeBandboxBehaviour(IBandboxModel model)
{
this.model = model;
}

public Rectangle BandboxRectangle
{
get => this.bandboxRectangle;
private set => this.SetField(ref this.bandboxRectangle, value, nameof(this.BandboxRectangle));
}

public void StartBandbox(int x, int y)
{
var p = new Point(x, y);
this.startPoint = p;
this.finishPoint = p;
this.UpdateBandboxRectangle();
}

public void GrowBandbox(int x, int y)
{
this.finishPoint.X += x;
this.finishPoint.Y += y;

this.UpdateBandboxRectangle();
}

public void CommitBandbox()
{
try
{
var width = this.BandboxRectangle.Width;
var height = this.BandboxRectangle.Height;

if (width == 0 || height == 0)
{
return;
}

this.model.LiftAndSelectArea(
this.BandboxRectangle.X,
this.BandboxRectangle.Y,
this.BandboxRectangle.Width,
this.bandboxRectangle.Height);
}
finally
{
this.BandboxRectangle = Rectangle.Empty;
}
}

private void UpdateBandboxRectangle()
{
var minX = Math.Min(this.startPoint.X, this.finishPoint.X);
var minY = Math.Min(this.startPoint.Y, this.finishPoint.Y);

var maxX = Math.Max(this.startPoint.X, this.finishPoint.X);
var maxY = Math.Max(this.startPoint.Y, this.finishPoint.Y);

var width = maxX - minX;
var height = maxY - minY;

this.BandboxRectangle = new Rectangle(minX, minY, width, height);
}
}
}
14 changes: 12 additions & 2 deletions Mappy/Models/CoreModel.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
namespace Mappy.Models
{
using System.ComponentModel;
using System.Drawing;

using Mappy.Models.Enums;
using Mappy.Util;

public class CoreModel : Notifier, IReadOnlyApplicationModel
Expand All @@ -20,6 +19,8 @@ public class CoreModel : Notifier, IReadOnlyApplicationModel
private Size gridSize = new Size(16, 16);
private Color gridColor = MappySettings.Settings.GridColor;

private GUITab guiTab = GUITab.Sections;

private int viewportWidth;
private int viewportHeight;

Expand Down Expand Up @@ -94,6 +95,15 @@ public Color GridColor
}
}

public GUITab SelectedGUITab
{
get => this.guiTab;
set
{
this.SetField(ref this.guiTab, value, nameof(this.SelectedGUITab));
}
}

public void SetViewportLocation(Point location)
{
this.Map.IfSome(
Expand Down
11 changes: 11 additions & 0 deletions Mappy/Models/Enums/GUITab.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Mappy.Models.Enums
{
public enum GUITab
{
Sections,
Features,
Starts,
Attributes,
Other
}
}
17 changes: 16 additions & 1 deletion Mappy/Models/FeatureInstance.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
namespace Mappy.Models
{
using System;

using System.Drawing;
using Mappy.Collections;
using Mappy.Data;
using Mappy.Services;

public sealed class FeatureInstance
{
private static readonly Feature DefaultFeatureRecord = new Feature
{
Name = "default",
Offset = new Point(0, 0),
Footprint = new Size(1, 1),
Image = Mappy.Properties.Resources.nofeature
};

public FeatureInstance(Guid id, string featureName, int x, int y)
: this(id, featureName, new GridCoordinates(x, y))
{
Expand All @@ -16,10 +26,15 @@ public FeatureInstance(Guid id, string featureName, GridCoordinates location)
this.Id = id;
this.FeatureName = featureName;
this.Location = location;
this.BaseFeature = FeatureService.TryGetFeature(featureName).Or(DefaultFeatureRecord);
}

public static FeatureService FeatureService { get; set; }

public Guid Id { get; }

public Feature BaseFeature { get; }

public string FeatureName { get; }

public GridCoordinates Location { get; }
Expand Down
3 changes: 3 additions & 0 deletions Mappy/Models/IMainFormViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using System;
using System.Drawing;
using Mappy.Models.Enums;

public interface IMainFormViewModel
{
Expand Down Expand Up @@ -132,5 +133,7 @@ public interface IMainFormViewModel
void ImportCustomSectionMenuItemClick();

void Load();

void ChangeSelectedTabType(GUITab tabType);
}
}
8 changes: 7 additions & 1 deletion Mappy/Models/IMapModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Mappy.Models
{
using System;
using System.Collections.Generic;
using System.Drawing;

public interface IMapModel : IReadOnlyMapModel
Expand All @@ -24,7 +25,12 @@ public interface IMapModel : IReadOnlyMapModel
/// If the ID is not present already, an exception is raised.
/// </summary>
/// <param name="instance">The instance to update.</param>
void UpdateFeatureInstance(FeatureInstance instance);
/// <param name="otherUpdatingFeatures">
/// The list of FeatureInstance GUIDs that are being updated.
/// The positions of these FeatureInstances will be ignored when
/// calculating if a feature is already at destination coordinates.
/// </param>
void UpdateFeatureInstance(FeatureInstance instance, ISet<Guid> otherUpdatingFeatures = null);

/// <summary>
/// Removes the feature instance with the given ID.
Expand Down
3 changes: 3 additions & 0 deletions Mappy/Models/IReadOnlyApplicationModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using System.ComponentModel;
using System.Drawing;
using Mappy.Models.Enums;

public interface IReadOnlyApplicationModel : INotifyPropertyChanged
{
Expand All @@ -26,5 +27,7 @@ public interface IReadOnlyApplicationModel : INotifyPropertyChanged
Size GridSize { get; }

Color GridColor { get; }

GUITab SelectedGUITab { get; }
}
}
7 changes: 6 additions & 1 deletion Mappy/Models/MainFormViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Drawing;
using System.Linq;
using System.Reactive.Linq;

using Mappy.Models.Enums;
using Mappy.Services;

public class MainFormViewModel : IMainFormViewModel
Expand Down Expand Up @@ -341,5 +341,10 @@ public void Load()
{
this.dispatcher.Initialize();
}

public void ChangeSelectedTabType(GUITab tabType)
{
this.dispatcher.ChangeSelectedTab(tabType);
}
}
}
13 changes: 8 additions & 5 deletions Mappy/Models/MapModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,15 @@ public FeatureInstance GetFeatureInstanceAt(int x, int y)
/// <summary>
/// <see cref="IMapModel.UpdateFeatureInstance"/>
/// </summary>
public void UpdateFeatureInstance(FeatureInstance instance)
public void UpdateFeatureInstance(FeatureInstance instance, ISet<Guid> featureIgnoreList = null)
{
if (!this.featureInstances.ContainsKey(instance.Id))
{
throw new ArgumentException("No existing FeatureInstance with this ID.");
}

this.RemoveFeatureInstanceInternal(instance.Id);
this.AddFeatureInstanceInternal(instance);
this.AddFeatureInstanceInternal(instance, featureIgnoreList);

var arg = new FeatureInstanceEventArgs(
FeatureInstanceEventArgs.ActionType.Move,
Expand Down Expand Up @@ -330,16 +330,19 @@ public void DeselectAll()
this.DeselectTile();
}

private void AddFeatureInstanceInternal(FeatureInstance instance)
private void AddFeatureInstanceInternal(FeatureInstance instance, ISet<Guid> featureIgnoreList = null)
{
if (this.featureInstances.ContainsKey(instance.Id))
{
throw new ArgumentException("A FeatureInstance with the given ID already exists.");
}

if (this.featureLocationIndex.HasValue(instance.X, instance.Y))
if (this.featureLocationIndex.TryGetValue(instance.X, instance.Y, out FeatureInstance val))
{
throw new ArgumentException("A FeatureInstance is already present at the target location.");
if (featureIgnoreList != null && !featureIgnoreList.Contains(val.Id))
{
throw new ArgumentException("A FeatureInstance is already present at the target location.");
}
}

this.featureInstances[instance.Id] = instance;
Expand Down
Loading