2017-06-25 23:05:48 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2019-03-27 18:39:59 +01:00
|
|
|
|
// © 2019 Clemens Fischer
|
2012-11-23 16:16:09 +01:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
2018-11-13 21:35:48 +01:00
|
|
|
|
using System;
|
2017-09-05 20:57:17 +02:00
|
|
|
|
#if WINDOWS_UWP
|
2018-11-13 21:35:48 +01:00
|
|
|
|
using Windows.Foundation;
|
2013-08-17 08:41:11 +02:00
|
|
|
|
using Windows.UI.Xaml;
|
|
|
|
|
|
using Windows.UI.Xaml.Controls;
|
2017-09-05 20:57:17 +02:00
|
|
|
|
#else
|
|
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
|
#endif
|
2013-08-17 08:41:11 +02:00
|
|
|
|
|
2012-11-23 16:16:09 +01:00
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2017-09-05 20:57:17 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Manages a collection of selectable items on a Map.
|
2013-08-17 08:41:11 +02:00
|
|
|
|
/// </summary>
|
2018-11-13 21:35:48 +01:00
|
|
|
|
public partial class MapItemsControl : ListBox
|
2012-11-23 16:16:09 +01:00
|
|
|
|
{
|
|
|
|
|
|
public MapItemsControl()
|
|
|
|
|
|
{
|
|
|
|
|
|
DefaultStyleKey = typeof(MapItemsControl);
|
2017-09-05 20:57:17 +02:00
|
|
|
|
|
|
|
|
|
|
MapPanel.InitMapElement(this);
|
2012-11-23 16:16:09 +01:00
|
|
|
|
}
|
2013-08-17 08:41:11 +02:00
|
|
|
|
|
|
|
|
|
|
protected override DependencyObject GetContainerForItemOverride()
|
|
|
|
|
|
{
|
|
|
|
|
|
return new MapItem();
|
|
|
|
|
|
}
|
2014-06-11 23:03:13 +02:00
|
|
|
|
|
|
|
|
|
|
protected override bool IsItemItsOwnContainerOverride(object item)
|
|
|
|
|
|
{
|
|
|
|
|
|
return item is MapItem;
|
|
|
|
|
|
}
|
2018-11-10 22:38:46 +01:00
|
|
|
|
|
2018-11-13 21:35:48 +01:00
|
|
|
|
public void SelectItems(Func<object, bool> predicate)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (SelectionMode == SelectionMode.Single)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new InvalidOperationException("SelectionMode must not be Single");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var item in Items)
|
|
|
|
|
|
{
|
|
|
|
|
|
var selected = predicate(item);
|
|
|
|
|
|
|
|
|
|
|
|
if (selected != SelectedItems.Contains(item))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (selected)
|
|
|
|
|
|
{
|
|
|
|
|
|
SelectedItems.Add(item);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
SelectedItems.Remove(item);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void SelectItemsByLocation(Func<Location, bool> predicate)
|
|
|
|
|
|
{
|
|
|
|
|
|
SelectItems(item =>
|
|
|
|
|
|
{
|
2018-11-14 17:53:37 +01:00
|
|
|
|
var loc = MapPanel.GetLocation(ContainerFromItem(item));
|
2018-11-13 21:35:48 +01:00
|
|
|
|
return loc != null && predicate(loc);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void SelectItemsByPosition(Func<Point, bool> predicate)
|
|
|
|
|
|
{
|
|
|
|
|
|
SelectItems(item =>
|
|
|
|
|
|
{
|
2018-11-14 17:53:37 +01:00
|
|
|
|
var pos = MapPanel.GetViewportPosition(ContainerFromItem(item));
|
2018-11-13 21:35:48 +01:00
|
|
|
|
return pos.HasValue && predicate(pos.Value);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void SelectItemsInRect(Rect rect)
|
|
|
|
|
|
{
|
|
|
|
|
|
SelectItemsByPosition(p => rect.Contains(p));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-14 17:53:37 +01:00
|
|
|
|
protected internal void OnItemClicked(FrameworkElement mapItem, bool controlKey, bool shiftKey)
|
2018-11-10 22:38:46 +01:00
|
|
|
|
{
|
2018-11-14 17:53:37 +01:00
|
|
|
|
var item = ItemFromContainer(mapItem);
|
2018-11-13 21:35:48 +01:00
|
|
|
|
|
|
|
|
|
|
if (SelectionMode == SelectionMode.Single)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Single -> set only SelectedItem
|
|
|
|
|
|
|
|
|
|
|
|
if (SelectedItem != item)
|
|
|
|
|
|
{
|
|
|
|
|
|
SelectedItem = item;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (controlKey)
|
|
|
|
|
|
{
|
|
|
|
|
|
SelectedItem = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (SelectionMode == SelectionMode.Multiple || controlKey)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Multiple or Extended with Ctrl -> toggle item in SelectedItems
|
|
|
|
|
|
|
|
|
|
|
|
if (SelectedItems.Contains(item))
|
|
|
|
|
|
{
|
|
|
|
|
|
SelectedItems.Remove(item);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
SelectedItems.Add(item);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (shiftKey && SelectedItem != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Extended with Shift -> select items in viewport rectangle
|
|
|
|
|
|
|
2018-11-14 17:53:37 +01:00
|
|
|
|
var p1 = MapPanel.GetViewportPosition(ContainerFromItem(SelectedItem));
|
2018-11-13 21:35:48 +01:00
|
|
|
|
var p2 = MapPanel.GetViewportPosition(mapItem);
|
|
|
|
|
|
|
|
|
|
|
|
if (p1.HasValue && p2.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
SelectItemsInRect(new Rect(p1.Value, p2.Value));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (SelectedItem != item)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Extended without Control or Shift -> set selected item
|
|
|
|
|
|
|
|
|
|
|
|
SelectedItem = item;
|
|
|
|
|
|
}
|
2018-11-10 22:38:46 +01:00
|
|
|
|
}
|
2012-11-23 16:16:09 +01:00
|
|
|
|
}
|
2013-08-17 08:41:11 +02:00
|
|
|
|
}
|