Imports C1.Silverlight.Maps
Imports VectorLayer.VectorLayer

Partial Public Class Marks
    Inherits UserControl
    Private vl As C1VectorLayer
    Private rnd As New Random()
    Private current As C1VectorPlacemark = Nothing
    Private offset As New Point()
    Private idx As Integer = 1

    Public Sub New()
        InitializeComponent()

        AddHandler Me.Loaded, AddressOf Marks_Loaded
        AddHandler Me.Unloaded, AddressOf Marks_Unloaded
    End Sub

    Private Sub Marks_Unloaded(sender As Object, e As RoutedEventArgs)
        ' Release memory
        idx = 1
        maps.Layers.Clear()
        RemoveHandler maps.MouseLeftButtonUp, AddressOf maps_MouseLeftButtonUp
        For Each mark As C1VectorPlacemark In vl.Children
            RemoveHandler mark.MouseLeftButtonDown, AddressOf mark_MouseLeftButtonDown
            RemoveHandler mark.MouseLeftButtonUp, AddressOf mark_MouseLeftButtonUp
            RemoveHandler mark.MouseMove, AddressOf mark_MouseMove
        Next
        vl.Children.Clear()
        maps.Layers.Clear()
    End Sub

    Private Sub Marks_Loaded(sender As Object, e As RoutedEventArgs)
        vl = New C1VectorLayer()
        maps.Layers.Add(vl)
        AddHandler maps.MouseLeftButtonUp, AddressOf maps_MouseLeftButtonUp

        For i As Integer = 0 To 9
            ' create random coordinates
            Dim pt As New Point(-80 + rnd.[Next](160), -80 + rnd.[Next](160))
            AddMark(pt)
        Next
    End Sub

    Private Sub maps_MouseLeftButtonUp(sender As Object, e As MouseButtonEventArgs)
        If Keyboard.Modifiers = ModifierKeys.Control Then
            AddMark(maps.ScreenToGeographic(e.GetPosition(maps)))
        End If
    End Sub

    Private Sub mark_MouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs)
        Dim pm As C1VectorPlacemark = DirectCast(sender, C1VectorPlacemark)

        If Keyboard.Modifiers = ModifierKeys.Shift Then
            e.Handled = True
            If pm.CaptureMouse() Then
                current = pm
                offset = e.GetPosition(current)
            End If
        ElseIf Keyboard.Modifiers = ModifierKeys.Alt Then
            e.Handled = True
            vl.Children.Remove(pm)
        End If
    End Sub

    Private Sub AddMark(pt As Point)
        Dim clr As Color = Utils.GetRandomColor(128, 192)
        Dim mark As New C1VectorPlacemark() With {
            .GeoPoint = pt,
            .Label = New TextBlock() With {
                .RenderTransform = New TranslateTransform() With {
                    .Y = -5
                },
                .IsHitTestVisible = False,
                .Text = idx.ToString()
            },
            .LabelPosition = LabelPosition.Top,
            .Geometry = Utils.CreateBaloon(),
            .Stroke = New SolidColorBrush(Colors.DarkGray),
            .Fill = New SolidColorBrush(clr)
        }

        AddHandler mark.MouseLeftButtonDown, AddressOf mark_MouseLeftButtonDown
        AddHandler mark.MouseLeftButtonUp, AddressOf mark_MouseLeftButtonUp
        AddHandler mark.MouseMove, AddressOf mark_MouseMove
        vl.Children.Add(mark)
        vl.LabelVisibility = LabelVisibility.Visible
        idx += 1
    End Sub

    Private Sub mark_MouseMove(sender As Object, e As MouseEventArgs)
        If current IsNot Nothing Then
            Dim pt As Point = e.GetPosition(maps)
            pt.X -= offset.X
            pt.Y -= offset.Y
            current.GeoPoint = maps.ScreenToGeographic(pt)
        End If
    End Sub

    Private Sub mark_MouseLeftButtonUp(sender As Object, e As MouseButtonEventArgs)
        Dim pm As C1VectorPlacemark = DirectCast(sender, C1VectorPlacemark)
        If pm IsNot Nothing Then
            pm.ReleaseMouseCapture()
        End If
        current = Nothing
    End Sub
End Class

