// PostureDlg.cpp : Implementation file
//

#include "stdafx.h"
#include "RemoteTest.h"
#include "PostureDlg.h"
#include "CameraPosWnd.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPostureDlg  dialog


CPostureDlg::CPostureDlg(CVAIBO *vaibo, CWnd* pParent /*=NULL*/)
    : CDialog(CPostureDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(CPostureDlg)
    //}}AFX_DATA_INIT
    m_vaibo = vaibo;
}


void CPostureDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CPostureDlg)
        DDX_Control(pDX, IDC_COMBO_NECK_SPEED, m_ComboNeckSpeed);
        DDX_Control(pDX, IDC_EDIT_HEAD_X, m_EditHeadX);
        DDX_Control(pDX, IDC_EDIT_HEAD_Y, m_EditHeadY);
    //}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPostureDlg, CDialog)
    //{{AFX_MSG_MAP(CPostureDlg)
    ON_BN_CLICKED(IDC_BTN_SLEEP, OnBtnSleep)
    ON_BN_CLICKED(IDC_BTN_STAND, OnBtnStand)
    ON_BN_CLICKED(IDC_BTN_SIT, OnBtnSit)
    ON_BN_CLICKED(IDC_BTN_WALK, OnBtnWalk)
    ON_BN_CLICKED(IDC_BTN_SLEEP_V, OnBtnSleepV)
    ON_BN_CLICKED(IDC_BTN_STAND_V, OnBtnStandV)
    ON_BN_CLICKED(IDC_BTN_SIT_V, OnBtnSitV)
    ON_BN_CLICKED(IDC_BTN_TURN_BODY, OnBtnTurnBody)
    ON_WM_DESTROY()
    ON_WM_LBUTTONDOWN()
    ON_BN_CLICKED(IDC_BUTTON_HEAD, OnButtonHead)
    ON_BN_CLICKED(IDC_BTN_STATION, OnBtnStation)
    ON_BN_CLICKED(IDC_BTN_STATION_GOFF, OnBtnStationGoff)
    ON_BN_CLICKED(IDC_BTN_STATION_GOFF_D, OnBtnStationGoffD)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPostureDlg message handlers

BOOL CPostureDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    // Initialize neck control
    CWnd    *wnd = GetDlgItem(IDC_STATIC_NECK);
    CRect   rect;

    wnd->GetWindowRect(&rect);
    this->ScreenToClient(&rect);
    m_pCameraView = new CCameraPosWnd( this );
    m_pCameraView->Create(NULL, "CameraControl",
                            WS_CHILD | WS_VISIBLE,
                            rect, this, NULL);

    m_nNeckAngle = 0;   // Neck angle

    m_ComboNeckSpeed.SetCurSel( 2 );    // Speed -> normal
    m_EditHeadX.LimitText( 3 );
    m_EditHeadY.LimitText( 3 );
    m_EditHeadX.SetWindowText( "0" );   // X = 0
    m_EditHeadY.SetWindowText( "0" );   // Y = 0

    return TRUE;   // return TRUE  unless you set the focus to a control
}

void CPostureDlg::OnBtnSleep()
{
    m_vaibo->SetPosture( POS_SLEEP );
}

void CPostureDlg::OnBtnSit()
{
    m_vaibo->SetPosture( POS_SIT );
}

void CPostureDlg::OnBtnStand()
{
    m_vaibo->SetPosture( POS_STAND );
}

void CPostureDlg::OnBtnWalk()
{
    m_vaibo->SetPosture( POS_WALK );
}

void CPostureDlg::OnBtnSleepV()
{
    m_vaibo->SetPosture( POS_SLEEP_V );
}

void CPostureDlg::OnBtnSitV()
{
    m_vaibo->SetPosture( POS_SIT_V );
}

void CPostureDlg::OnBtnStandV()
{
    m_vaibo->SetPosture( POS_STAND_V );
}

void CPostureDlg::OnBtnStation()
{
    m_vaibo->SetPosture( POS_STATION );
}

void CPostureDlg::OnBtnStationGoff()
{
    m_vaibo->SetPosture( POS_STATION_GOFF );
    m_vaibo->PlayMotion(0x0304F000, true, false, false);
       /* Set weak gain head joints on the station */
       /* This command should wait_flag = false */
}

void CPostureDlg::OnBtnStationGoffD()
{
    m_vaibo->SetPosture( POS_STATION_GOFF_D );
    m_vaibo->PlayMotion(0x0304F000, true, false, false);
       /* Set weak gain head joints on the station */
       /* This command should wait_flag = false */
}


void CPostureDlg::OnDestroy()
{
    CDialog::OnDestroy();

    // Destroy the camera view (neck control) window.
    if ( m_pCameraView ) {
        m_pCameraView->DestroyWindow();
        delete m_pCameraView;
    }
}

void CPostureDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
    // Get the neck speed
    int     nSpeed;
    switch ( m_ComboNeckSpeed.GetCurSel() ) {
        case 0:
            nSpeed = 4;     // Very Quick
            break;
        case 1:
            nSpeed = 3;     // Quick
            break;
        case 2:
            nSpeed = 2;     // Normal
            break;
        case 3:
            nSpeed = 1;     // Slow
            break;
        default:
            nSpeed = 2;     // Normal
            break;
    }


    if ( m_pCameraView  ) {
        CRect   rect;
        CPoint  pos, camera;

        m_pCameraView->GetWindowRect(&rect);
        ScreenToClient(&rect);
        if ( rect.PtInRect(point) ) {       // When clicked inside the camera view window.
            if ( !m_pCameraView->IsWindowEnabled() ) return;    // Return when the window is not enabled.
            ClientToScreen(&point);
            m_pCameraView->GetMousePos( point, m_vaibo->GetPosture(), pos );
            TRACE2("Camera = %d, %d\n", pos.x, pos.y );
            m_vaibo->ChangeHeadAngle( pos.x, pos.y, nSpeed );
            m_pCameraView->SetCameraPos( pos, m_vaibo->GetPosture() );
            m_nNeckAngle = pos.x;   // Neck angle
            return;
        }
    }

    CDialog::OnLButtonDown(nFlags, point);
}

void    CPostureDlg::OnBtnTurnBody()
{
    if ( m_vaibo ) {
        m_vaibo->TurnBody( m_nNeckAngle );
    }
}

void    CPostureDlg::OnButtonHead()
{
    char    strX[ 4 ], strY[ 4 ];

    m_EditHeadX.GetWindowText( strX, 4 );
    m_EditHeadY.GetWindowText( strY, 4 );

    int     nX, nY;
    nX = atoi( strX );
    nY = atoi( strY );

    // Get the neck speed
    int     nSpeed;
    switch ( m_ComboNeckSpeed.GetCurSel() ) {
        case 0:
            nSpeed = 4;     // Very Quick
            break;
        case 1:
            nSpeed = 3;     // Quick
            break;
        case 2:
            nSpeed = 2;     // Normal
            break;
        case 3:
            nSpeed = 1;     // Slow
            break;
        default:
            nSpeed = 2;     // Normal
            break;
    }

    m_vaibo->ChangeHeadAngle( nX, nY, nSpeed );

}

