Imports System.Collections.ObjectModel
Imports ThinkGeo.Core
Imports ThinkGeo.UI.WinForms

Public Class CustomExtentInteractiveOverlay
  Inherits ExtentInteractiveOverlay
  Private moSavedCursor As System.Windows.Input.Cursor
  Private meSavedTrackMode As TrackMode
  Private meSavedPanMode As MapPanMode = MapPanMode.Disabled
  'Private meSavedMapTool As Enums.EnumMapTool
  Private mbPKeyDown As Boolean = False
  Private mbPlusKeyDown As Boolean = False
  Private mbMinusKeyDown As Boolean = False
  Private moMap As CustomMapView
  Public Sub New(oMap As CustomMapView)
    MyBase.New
    moMap = oMap
  End Sub
  Protected Overrides Sub OnMapKeyDown(e As MapKeyDownInteractiveOverlayEventArgs)

    '***** If the user is pressing the 'p' key then allow them to pan
    '***** the map
    '***** QB #475
    '***** SET 08/01/2011
    If Not (mbPKeyDown Or mbPlusKeyDown Or mbMinusKeyDown) Then
      Select Case e.InteractionArguments.Key
        Case System.Windows.Input.Key.P
          '***** Pan the map
          '***** Save the current state so that it can be restored in the OnKeyUp sub
          moSavedCursor = momap.MapCursor
          meSavedTrackMode = moMap.TrackOverlay.TrackMode
          meSavedPanMode = PanMode
          '***** Switch to the pan cursor
          'momap.MapCursor = ArcGisRestDemo.Utils.MapCursorCollection.MapCursor(My.Resources.IDPanCursor)
          '***** Turn of any kind of tracking
          moMap.TrackOverlay.TrackMode = TrackMode.None
          ''EditOverlay.CanReshape = False
          'CustomEditOverlay.SuspendVertexEditing()
          '***** Turn on panning
          PanMode = MapPanMode.StandardPanning
          'meSavedMapTool = oCustomTrackOverlay.MapTool
          'oCustomTrackOverlay.MapTool = Enums.EnumMapTool.Pan
          mbPKeyDown = True
        Case System.Windows.Input.Key.Add, System.Windows.Input.Key.OemPlus ' Keys.Add, Keys.Oemplus
          '***** Zoom In
          '***** Save the current state so that it can be restored in the OnKeyUp sub
          moSavedCursor = moMap.MapCursor
          meSavedTrackMode = moMap.TrackOverlay.TrackMode
          meSavedPanMode = PanMode
          '***** Switch to the Zoom In cursor
          'Me.MapCursor = ArcGisRestDemo.Utils.MapCursorCollection.MapCursor(My.Resources.IDZoomInCursor)
          '***** Turn on zooming in
          PanMode = MapPanMode.Disabled
          moMap.TrackOverlay.TrackMode = TrackMode.Rectangle
          'meSavedMapTool = oCustomTrackOverlay.MapTool
          'oCustomTrackOverlay.MapTool = Enums.EnumMapTool.ZoomIn
          mbPlusKeyDown = True
        Case System.Windows.Input.Key.Subtract, System.Windows.Input.Key.OemMinus ' Keys.Subtract, Keys.OemMinus
          '***** Zoom Out
          '***** Save the current state so that it can be restored in the OnKeyUp sub
          moSavedCursor = moMap.MapCursor
          meSavedTrackMode = moMap.TrackOverlay.TrackMode
          meSavedPanMode = PanMode
          '***** Switch to the Zoom Out cursor
          'Me.MapCursor = ArcGisRestDemo.Utils.MapCursorCollection.MapCursor(My.Resources.IDZoomOutCursor)
          '***** Turn on zooming out
          PanMode = MapPanMode.Disabled
          moMap.TrackOverlay.TrackMode = TrackMode.Rectangle
          'meSavedMapTool = oCustomTrackOverlay.MapTool
          'oCustomTrackOverlay.MapTool = Enums.EnumMapTool.ZoomOut
          mbMinusKeyDown = True
      End Select
    End If

    MyBase.OnMapKeyDown(e)
  End Sub
  Protected Overrides Sub OnMapKeyUp(e As MapKeyUpInteractiveOverlayEventArgs)

    'Dim oDrawingOverlay As CustomDrawingOverlay = DrawingOverlay
    'Dim oMeasureOverlay As CustomMeasureOverlay = MeasureOverlay
    If e.InteractionArguments.Key = Windows.Input.Key.P Or
       e.InteractionArguments.Key = Windows.Input.Key.Add Or
       e.InteractionArguments.Key = Windows.Input.Key.Subtract Or
       e.InteractionArguments.Key = Windows.Input.Key.OemPlus Or
       e.InteractionArguments.Key = Windows.Input.Key.OemMinus Then
      '***** Restore the state that existing when the 'p' key was pressed
      'Me.MapCursor = moSavedCursor
      PanMode = meSavedPanMode
      moMap.TrackOverlay.TrackMode = meSavedTrackMode
      mbPKeyDown = False
      mbPlusKeyDown = False
      mbMinusKeyDown = False
    End If

    MyBase.OnMapKeyUp(e)
  End Sub

  Protected Overrides Sub OnMapMouseDown(e As MapMouseDownInteractiveOverlayEventArgs)
    If e.InteractionArguments.MouseButton = MapMouseButton.Middle Then ' Windows.Forms.MouseButtons.Middle Then
      e.InteractionArguments.MouseButton = MapMouseButton.Left
      '***** Call OnKeyDown and simulate a keypress with the 'P' key
      Dim oMapArgs As New KeyEventInteractionArguments(Windows.Input.Key.P, False, False, False)
      Dim oArgs As New MapKeyDownInteractiveOverlayEventArgs(oMapArgs)
      OnMapKeyDown(oArgs)
      '***** Call the base OnMouseDown, but fool it into thinking
      '***** the left mouse button is pressed, otherwize the map won't pan
    End If

    Debug.WriteLine("ExtentOverlayMapMouseDown")
    MyBase.OnMapMouseDown(e)
  End Sub
  Protected Overrides Sub OnMapMouseUp(e As MapMouseUpInteractiveOverlayEventArgs)
    If e.InteractionArguments.MouseButton = MapMouseButton.Middle Then ' Windows.Forms.MouseButtons.Middle Then
      e.InteractionArguments.MouseButton = MapMouseButton.Left
      '***** Call OnKeyup and simulate a keypress with the 'P' key
      Dim oMapArgs As New KeyEventInteractionArguments(Windows.Input.Key.P, False, False, False)
      Dim oArgs As New MapKeyUpInteractiveOverlayEventArgs(oMapArgs)
      OnMapKeyUp(oArgs)
      '***** Call the base OnMouseDown, but fool it into thinking
      '***** the left mouse button is pressed, otherwize the map won't pan
    End If

    MyBase.OnMapMouseUp(e)
  End Sub
End Class
