
Imports System.Globalization
Imports System.IO
Imports System.Math
Imports System.EventArgs
Imports System.Threading
Imports System.Net
Imports System.Drawing


Public Class classScanner

    Private Sub frmScannerConfigure_Load(sender As Object, e As EventArgs) Handles Me.Load
        Try
            Me.Visible = True
            updnOffsetX.Value = tmcScanVectorOffset.X
            updnOffsetY.Value = tmcScanVectorOffset.Y
            sliderScannerRangeCalibrate.Value = sngScannerCalibrate * 100
            sliderScannerDepthCurveAdjust.Value = (sngScannerCurveAdjust + 1) * 100
            updnScannerOffsetDegrees.Value = intScanOffsetDegrees
            chkContinuous.Checked = False
            updnScannerOffsetDegrees.Value = intScanOffsetDegrees
            If Not boolKinectConnected And Not boolURGConnected Then ftnURGInitializeTop()
            If Not boolKinectConnected And Not boolURGConnected Then ftnKinectInitializeTop()
            subReportScannerStatus()
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in frmScannerConfigure_Load :  " & ex.ToString)
        End Try
    End Sub

    Private Sub updnOffsetX_ValueChanged(sender As Object, e As EventArgs) Handles updnOffsetX.ValueChanged
        Try
            If updnOffsetX.ContainsFocus Then
                tmcScanVectorOffset.X = updnOffsetX.Value
                boolSettingsChanged = True
            End If
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in updnOffsetX_ValueChanged :  " & ex.ToString)
        End Try
    End Sub

    Private Sub updnOffsetY_ValueChanged(sender As Object, e As EventArgs) Handles updnOffsetY.ValueChanged
        Try
            If updnOffsetY.ContainsFocus Then
                tmcScanVectorOffset.Y = updnOffsetY.Value
                boolSettingsChanged = True
            End If
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in updnOffsetY_ValueChanged :  " & ex.ToString)
        End Try
    End Sub

    Private Sub trbRangeCalibrate_Scroll(sender As Object, e As EventArgs) Handles sliderScannerRangeCalibrate.Scroll
        On Error Resume Next
        sngScannerCalibrate = sliderScannerRangeCalibrate.Value / 100
        boolSettingsChanged = True
        subCalculateScanCalCoefficients()
    End Sub


    Sub subRangeCaibrateChanged(sender As Object, e As EventArgs) Handles sliderScannerRangeCalibrate.ValueChanged
        Try
            RangeCalibrateLabel.Text = "Range Calibrate (" & sliderScannerRangeCalibrate.Value & ")"
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in subRangeCaibrateChanged :  " & ex.ToString)
        End Try
    End Sub

    Private Sub updnScannerOffsetDegrees_ValueChanged(sender As Object, e As EventArgs) Handles updnScannerOffsetDegrees.ValueChanged
        Try
            If updnScannerOffsetDegrees.ContainsFocus Then
                intScanOffsetDegrees = updnScannerOffsetDegrees.Value
                subCalculateScanBeamAngles()
                boolSettingsChanged = True
            End If
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in updnScannerOffsetDegrees_ValueChanged :  " & ex.ToString)
        End Try
    End Sub


    Private Sub sliderScannerDepthCurveAdjust_Scroll(sender As Object, e As EventArgs) Handles sliderScannerDepthCurveAdjust.Scroll
        On Error Resume Next
        If sliderScannerDepthCurveAdjust.ContainsFocus Then
            sngScannerCurveAdjust = sliderScannerDepthCurveAdjust.Value / 100 - 1
            boolSettingsChanged = True
            subCalculateScanCalCoefficients()
        End If
    End Sub

    Sub subRangeCurveChanged(sender As Object, e As EventArgs) Handles sliderScannerDepthCurveAdjust.ValueChanged
        Try
            CurveAdjustLabel.Text = "Curve Adjust (" & sliderScannerDepthCurveAdjust.Value & ")"
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in subRangeCurveChanged :  " & ex.ToString)
        End Try
    End Sub


    Private Sub Cancel_Button_Click(sender As Object, e As EventArgs) Handles Cancel_Button.Click
        Try
            Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
            Dim tbpTempPose As typeBotPose = tbpBotPose ' make sure we aren't throwing away important stuff!
            Dim tlmTempLandMarkArray(128) As typeLandMark
            tlmTempLandMarkArray = tlmLandMarkArray
            frmGotABotTop.mnuLoadSettings_Click(sender, e)
            tbpBotPose = tbpTempPose
            tlmLandMarkArray = tlmTempLandMarkArray
            strStatusDriving = ""
            Me.Close()
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in Cancel_Button_Click :  " & ex.ToString)
        End Try
    End Sub

    Private Sub OK_Button_Click(sender As Object, e As EventArgs) Handles OK_Button.Click
        Try
            Me.Visible = False
            strStatusDriving = ""
            subCalculateScanBeamAngles()
            subCalculateScanCalCoefficients()
            Me.Close()
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in OK_Button_Click :  " & ex.ToString)
        End Try
    End Sub

    Private Sub me_exit(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.FormClosed
        chkContinuous.Checked = False
        Me.Visible = False
    End Sub


    '--------------------------------------  MENU BAR   -----------------------------------------------------

    Private Sub mnuDefaultElevation_Click(sender As Object, e As EventArgs) Handles mnuDefaultElevation.Click
        Try
            Dim strDefaultElevation As String
            strDefaultElevation = InputBox("Enter Default Elevation Angle (-27 to 27 degrees) ", "Elevation Angle", Str(intDefaultElevation))
            If strDefaultElevation <> "" And IsNumeric(strDefaultElevation) Then
                intDefaultElevation = Convert.ToInt16(strDefaultElevation)
                If (intDefaultElevation < -27) Then intDefaultElevation = -27
                If (intDefaultElevation > 27) Then intDefaultElevation = 27
                K_Sensor.ElevationAngle() = intDefaultElevation
                boolSettingsChanged = True
            End If
            Me.Refresh()
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in mnuDefaultElevation_Click :  " & ex.ToString)
        End Try
    End Sub



    Delegate Sub DelegateUpdateLabel(ByVal strNewText As String, ByVal colText As Color)
    Public Sub subUpdateScannerStatusLabel(ByVal strNewText As String, ByVal colText As Color)
        Try
            If Me.Visible Then
                If InvokeRequired And Not boolShutDown Then
                    Dim newDelegate As New DelegateUpdateLabel(AddressOf subUpdateScannerStatusLabel)
                    Invoke(newDelegate, New Object() {strNewText, colText})
                ElseIf Not boolShutDown Then
                    tsslScannerStatus.ForeColor = colText
                    tsslScannerStatus.Text = strNewText
                    Me.Refresh()
                End If '
            End If '
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in subUpdateScannerStatusLabel :  " & ex.ToString)
        End Try
    End Sub



    Public Sub subReportScannerStatus()
        Try
            If boolURGConnected Then
                subUpdateScannerStatusLabel("URG Connected on " & comURG.PortName.ToString, Color.Black)
            ElseIf boolKinectConnected Then
                subUpdateScannerStatusLabel("Kinect Connected", Color.Black)
            ElseIf boolHeroSelected Then
                subUpdateScannerStatusLabel("Hero Selected", Color.Black)
            Else
                subUpdateScannerStatusLabel("No Scanner", Color.Red)
            End If
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in subReportScannerStatus :  " & ex.ToString)
        End Try
    End Sub

    Private Sub StatusStrip1_ItemClicked(sender As Object, e As Windows.Forms.ToolStripItemClickedEventArgs) Handles StatusStrip1.ItemClicked
        Try
            If Not boolKinectConnected And Not boolURGConnected Then ftnURGInitializeTop()
            If Not boolKinectConnected And Not boolURGConnected Then ftnKinectInitializeTop()
            subReportScannerStatus()
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in StatusStrip1_ItemClicked :  " & ex.ToString)
        End Try
    End Sub

 End Class