Skip to content

Commit

Permalink
Add AvailableFor layer filter for selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyvyduykin committed Oct 3, 2022
1 parent b1e03c9 commit d94638e
Show file tree
Hide file tree
Showing 11 changed files with 229 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public MainWindowViewModel()
radioButtonList.Register(new RadioButtonItem("Circle"), DrawingCircleCommand, Reset);
radioButtonList.Register(new RadioButtonItem("Polygon"), DrawingPolygonCommand, Reset);
radioButtonList.Register(new RadioButtonItem("Route"), DrawingRouteCommand, Reset);
radioButtonList.Register(new RadioButtonItem("Select|Filter"), SelectWithFilterCommand, Reset);
radioButtonList.Register(new RadioButtonItem("Translate|Filter"), TranslateWithFilterCommand, Reset);

RadioButtons = new List<RadioButtonItem>(radioButtonList.Items);

Expand Down Expand Up @@ -115,18 +117,64 @@ public void PointeroverLeaveImpl()

private void Reset()
{
_selector?.Unselected();
Interactive?.Cancel();
Interactive = null;

_selector = null;

Interactive = null;
State = States.Default;

Tip = string.Empty;
}

private void SelectCommand()
{
_selector = new InteractiveBuilder().SelectSelector<Selector>().Build();
_selector = new InteractiveBuilder()
.SelectSelector<Selector>()
.AttachTo(Map)
.Build();

_selector.Select.Subscribe(s =>
{
Tip = $"Select{Environment.NewLine}{s.SelectedFeature?.ToFeatureInfo()}";

if (IsWktInfo == true)
{
WktInfo = s.SelectedFeature?.ToWkt();
}
});

_selector.Unselect.Subscribe(s =>
{
Tip = string.Empty;

if (IsWktInfo == true)
{
WktInfo = string.Empty;
}
});

_selector.HoverBegin.Subscribe(s =>
{
Tip = $"HoveringBegin{Environment.NewLine}{s.HoveringFeature?.ToFeatureInfo()}";
});

_selector.HoverEnd.Subscribe(s =>
{
Tip = string.Empty;
});

Interactive = _selector;
State = States.Selecting;
}

private void SelectWithFilterCommand()
{
_selector = new InteractiveBuilder()
.SelectSelector<Selector>()
.AttachTo(Map)
.AvailableFor(_userLayer)
.Build();

_selector.Select.Subscribe(s =>
{
Expand Down Expand Up @@ -188,6 +236,33 @@ private void TranslateCommand()
State = States.Selecting;
}

private void TranslateWithFilterCommand()
{
_selector = new InteractiveBuilder()
.SelectDecorator<TranslateDecorator>()
.AttachTo(Map)
.WithSelector<Selector>()
.AvailableFor(_userLayer)
.Build();

((IDecoratorSelector)_selector).DecoratorSelecting.Subscribe(s =>
{
Interactive = s;
State = States.Editing;
Tip = $"Translate mode";
});

_selector.Unselect.Subscribe(s =>
{
Interactive = s;
State = States.Selecting;
Tip = String.Empty;
});

Interactive = _selector;
State = States.Selecting;
}

private void ScaleCommand()
{
_selector = new InteractiveBuilder()
Expand Down
3 changes: 3 additions & 0 deletions samples/MapsuiInteractivitySample/Views/MainView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@
<Style Selector="ItemsControl > ContentPresenter:nth-child(5)">
<Setter Property="Margin" Value="0,0,0,10"></Setter>
</Style>
<Style Selector="ItemsControl > ContentPresenter:nth-child(10)">
<Setter Property="Margin" Value="0,0,0,10"></Setter>
</Style>
</ItemsControl.Styles>

<ItemsControl.ItemTemplate>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public override void Completed(MouseEventArgs e)
&& IsClick(clickPoint, _clickPoint) == true
&& IFeature.Equals(_clickFeature, clickFeature) == true)
{
View.Interactive.Canceling.Execute().Subscribe();
View.Interactive.Cancel();
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/Mapsui.Interactivity/BaseInteractive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,10 @@ public virtual void Hovering(MapInfo? mapInfo) { }
public virtual void HoveringBegin(MapInfo? mapInfo) { }

public virtual void HoveringEnd() { }

public virtual void Cancel()
{
Canceling.Execute().Subscribe();
}
}
}
1 change: 1 addition & 0 deletions src/Mapsui.Interactivity/InteractiveBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Mapsui.Interactivity
{
public class InteractiveBuilder
{
// TODO: to IDictionary<Type, Func<IArgs, IInteractive>> _cache
private static readonly IDictionary<Type, Func<IInteractive>> _cache1 = new Dictionary<Type, Func<IInteractive>>();
private static readonly IDictionary<Type, Func<GeometryFeature, IDecorator>> _cache2 = new Dictionary<Type, Func<GeometryFeature, IDecorator>>();

Expand Down
11 changes: 9 additions & 2 deletions src/Mapsui.Interactivity/Interfaces/IBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ public interface IAttachable<T> where T : IBuilder
T AttachTo(IMap map);
}

public interface ISelectorBuilder : IBuilder<ISelector> { }
public interface IFilterable<T> where T : IBuilder
{
T AvailableFor(ILayer[] layers);

T AvailableFor(ILayer layer);
}

public interface ISelectorBuilder : IBuilder<ISelector>, IAttachable<ISelectorBuilder>, IFilterable<ISelectorBuilder> { }

public interface IDesignerBuilder : IBuilder<IDesigner>, IAttachable<IDesignerBuilder> { }

Expand All @@ -28,5 +35,5 @@ public interface IDecoratorBuilder : IBuilder<IDecorator>, IAttachable<IDecorato
IDecoratorBuilder WithFeature(GeometryFeature feature);
}

public interface ISelectorWithDecoratorBuilder : ISelectorBuilder { }
public interface ISelectorWithDecoratorBuilder : IBuilder<ISelector>, IFilterable<ISelectorWithDecoratorBuilder> { }
}
2 changes: 2 additions & 0 deletions src/Mapsui.Interactivity/Interfaces/IInteractive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ public interface IInteractive
void HoveringBegin(MapInfo? mapInfo);

void HoveringEnd();

void Cancel();
}
}
72 changes: 71 additions & 1 deletion src/Mapsui.Interactivity/SelectorBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,89 @@
using Mapsui.Interactivity.Interfaces;
using Mapsui.Layers;

namespace Mapsui.Interactivity
{
internal class SelectorBuilder : ISelectorBuilder
{
private readonly Func<IInteractive> _builder;
private IList<string>? _availableLayers;
private readonly IList<ILayer> _dirtyLayers = new List<ILayer>();

public SelectorBuilder(Func<IInteractive> builder)
{
_builder = builder;
}

internal LayerCollection? Layers { get; set; }

public ISelectorBuilder AvailableFor(ILayer[] layers)
{
_availableLayers = layers.Select(s => s.Name).ToList();

return this;
}

public ISelectorBuilder AvailableFor(ILayer layer)
{
_availableLayers = new List<string>() { layer.Name };

return this;
}

public ISelectorBuilder AttachTo(LayerCollection layers)
{
Layers = layers;

return this;
}

public ISelectorBuilder AttachTo(IMap map)
{
Layers = map.Layers;

return this;
}

public ISelector Build()
{
return (ISelector)_builder.Invoke();
var selector = (ISelector)_builder.Invoke();

if (_availableLayers != null && Layers != null)
{
foreach (var item in Layers)
{
if (_availableLayers.Contains(item.Name) == true)
{
if (item.IsMapInfoLayer == false)
{
item.IsMapInfoLayer = true;

_dirtyLayers.Add(item);
}
}
else
{
if (item.IsMapInfoLayer == true)
{
item.IsMapInfoLayer = false;

_dirtyLayers.Add(item);
}
}
}
}

selector.Canceling.Subscribe(s =>
{
foreach (var item in _dirtyLayers)
{
item.IsMapInfoLayer = !item.IsMapInfoLayer;
}

_dirtyLayers.Clear();
});

return selector;
}
}
}
51 changes: 51 additions & 0 deletions src/Mapsui.Interactivity/SelectorWithDecoratorBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,29 @@ internal class SelectorWithDecoratorBuilder : ISelectorWithDecoratorBuilder
{
private readonly Func<GeometryFeature, IDecorator> _builder;
private readonly LayerCollection? _layers;
private IList<string>? _availableLayers;
private readonly IList<ILayer> _dirtyLayers = new List<ILayer>();

public SelectorWithDecoratorBuilder(LayerCollection? layers, Func<GeometryFeature, IDecorator> builder)
{
_builder = builder;
_layers = layers;
}

public ISelectorWithDecoratorBuilder AvailableFor(ILayer[] layers)
{
_availableLayers = layers.Select(s => s.Name).ToList();

return this;
}

public ISelectorWithDecoratorBuilder AvailableFor(ILayer layer)
{
_availableLayers = new List<string>() { layer.Name };

return this;
}

public ISelector Build()
{
ISelector selector = new DecoratorSelector(_builder);
Expand All @@ -29,6 +45,41 @@ public ISelector Build()
selector.Unselect.Subscribe(_ => _layers.RemoveInteractiveLayer());
}

if (_availableLayers != null && _layers != null)
{
foreach (var item in _layers)
{
if (_availableLayers.Contains(item.Name) == true)
{
if (item.IsMapInfoLayer == false)
{
item.IsMapInfoLayer = true;

_dirtyLayers.Add(item);
}
}
else
{
if (item.IsMapInfoLayer == true)
{
item.IsMapInfoLayer = false;

_dirtyLayers.Add(item);
}
}
}
}

selector.Canceling.Subscribe(s =>
{
foreach (var item in _dirtyLayers)
{
item.IsMapInfoLayer = !item.IsMapInfoLayer;
}

_dirtyLayers.Clear();
});

return selector;
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/Mapsui.Interactivity/Selectors/DecoratorSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ internal DecoratorSelector(Func<GeometryFeature, IDecorator> builder)

public override void Unselected()
{
_decorator?.Canceling.Execute().Subscribe();

_decorator?.Cancel();
_decorator = null;

base.Unselected();
Expand Down
7 changes: 7 additions & 0 deletions src/Mapsui.Interactivity/Selectors/Selector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,12 @@ public override void HoveringEnd()
HoverEnd?.Execute().Subscribe();
}
}

public override void Cancel()
{
Unselected();

base.Cancel();
}
}
}

0 comments on commit d94638e

Please sign in to comment.