//--------------------------------------------------------------------------- // Copyright (c) Jim Wright 2004 // // MarkovRoomDlg.cpp // This is the header for the diaglog based application this was basically // from the Project Wizard. // //--------------------------------------------------------------------------- #include "stdafx.h" #include "MarkovRoom.h" #include "MarkovRoomDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #endif #define POSITION_ROBOT_STATE 0 #define GET_FIRST_READING_STATE 1 #define TURN_THE_ROBOT 2 #define READ_A_SECOND_TIME 3 #define FINISHED 4 // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data enum { IDD = IDD_ABOUTBOX }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support // Implementation protected: DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) END_MESSAGE_MAP() // CMarkovRoomDlg dialog CMarkovRoomDlg::CMarkovRoomDlg(CWnd* pParent /*=NULL*/) : CDialog(CMarkovRoomDlg::IDD, pParent) , m_MainRobot(10,10,380,280) , m_State(POSITION_ROBOT_STATE) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CMarkovRoomDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CMarkovRoomDlg, CDialog) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() //}}AFX_MSG_MAP ON_BN_CLICKED(IDC_BRunMarkov, OnBnClickedBrunmarkov) ON_NOTIFY(NM_CUSTOMDRAW, IDC_InitalAngle, OnNMCustomdrawInitalangle) END_MESSAGE_MAP() // CMarkovRoomDlg message handlers BOOL CMarkovRoomDlg::OnInitDialog() { CDialog::OnInitDialog(); CStatic* myStatic; // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon /* ** Add the walls for the MArkov room */ m_MainRobot.AddWall(new CJimLine(CJimPoint(0,0),CJimPoint(0,18))); m_MainRobot.AddWall(new CJimLine(CJimPoint(0,18),CJimPoint(6,18))); m_MainRobot.AddWall(new CJimLine(CJimPoint(6,18),CJimPoint(18,6))); m_MainRobot.AddWall(new CJimLine(CJimPoint(18,6),CJimPoint(18,0))); m_MainRobot.AddWall(new CJimLine(CJimPoint(18,0),CJimPoint(0,0))); /* ** Sets the robot and gets it ready for the simulation */ m_MainRobot.SetLocation(CJimPoint(5,5)); m_MainRobot.RunWallData(); /* ** Sets up the slider */ CSliderCtrl* mySlider = (CSliderCtrl*) this->GetDlgItem(IDC_InitalAngle); mySlider->SetRange(0,359); mySlider->SetTicFreq(1); CString myText; CStatic* myInitTheta = (CStatic*) this->GetDlgItem(IDC_InitalTheta); CStatic* myInitX = (CStatic*) this->GetDlgItem(IDC_InitX); CStatic* myInitY = (CStatic*) this->GetDlgItem(IDC_InitY); myText = "5"; myInitX->SetWindowText(myText); myInitY->SetWindowText(myText); myText = "0"; myInitTheta->SetWindowText(myText); /* ** Clear the statics on the screen */ myText = ""; myStatic = (CStatic*) this->GetDlgItem(IDC_SSensor1); myStatic->SetWindowText(myText); myStatic = (CStatic*) this->GetDlgItem(IDC_SSensor2); myStatic->SetWindowText(myText); myStatic = (CStatic*) this->GetDlgItem(IDC_SAngle1); myStatic->SetWindowText(myText); return TRUE; // return TRUE unless you set the focus to a control } void CMarkovRoomDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CMarkovRoomDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, reinterpret_cast(dc.GetSafeHdc()), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); m_MainRobot.DrawPage(this->GetDC()); } } // The system calls this function to obtain the cursor to display while the user drags // the minimized window. HCURSOR CMarkovRoomDlg::OnQueryDragIcon() { return static_cast(m_hIcon); } void CMarkovRoomDlg::OnBnClickedBrunmarkov() { CSliderCtrl* mySlider = (CSliderCtrl*) this->GetDlgItem(IDC_InitalAngle); CEdit* myEditBox; CStatic* myStatic; int angle; double sensorreading; CString myString; switch(m_State) { /* ** This is the read the slider state to get the sensor data. */ case(POSITION_ROBOT_STATE): angle = mySlider->GetPos(); m_MainRobot.SetTheta(this->GetDC(),angle); sensorreading = m_MainRobot.GetSensorData(angle); myEditBox = (CEdit*) this->GetDlgItem(IDC_ESensor1); myString.Format("%1.4f",sensorreading); myEditBox->SetWindowText(myString); m_State=GET_FIRST_READING_STATE; break; /* ** Once the sensor is read then find the porbabilities of the directions that the robot ** could be facing. */ case(GET_FIRST_READING_STATE): myEditBox = (CEdit*) this->GetDlgItem(IDC_ESensor1); myStatic = (CStatic*) this->GetDlgItem(IDC_SSensor1); myEditBox->GetWindowText(myString); myStatic->SetWindowText(myString); sensorreading = atof(myString.GetBuffer()); m_MainRobot.RunMarkovSensor(this->GetDC(),sensorreading,RGB(0,128,0)); myEditBox = (CEdit*) this->GetDlgItem(IDC_EAngle1); myString.Format("50"); myEditBox->SetWindowText(myString); m_State=TURN_THE_ROBOT; break; /* ** Turn the robot */ case(TURN_THE_ROBOT): myEditBox = (CEdit*) this->GetDlgItem(IDC_EAngle1); myStatic = (CStatic*) this->GetDlgItem(IDC_SAngle1); myEditBox->GetWindowText(myString); angle = (int)atof(myString.GetBuffer()); m_MainRobot.RunMarkovEncoder(this->GetDC(),angle,RGB(0,0,128)); angle += (int)m_MainRobot.GetTheta(); m_MainRobot.SetTheta(this->GetDC(),angle); myString.Format("%1.4f",angle); myStatic->SetWindowText(myString); sensorreading = m_MainRobot.GetSensorData(angle); myEditBox = (CEdit*) this->GetDlgItem(IDC_ESensor2); myString.Format("%1.4f",sensorreading); myEditBox->SetWindowText(myString); m_State=READ_A_SECOND_TIME; break; /* ** Read the sensor again and then find the porbabilities of the directions that the robot ** could be facing. */ case(READ_A_SECOND_TIME): myEditBox = (CEdit*) this->GetDlgItem(IDC_ESensor2); myStatic = (CStatic*) this->GetDlgItem(IDC_SSensor2); myEditBox->GetWindowText(myString); myStatic->SetWindowText(myString); sensorreading = atof(myString.GetBuffer()); m_MainRobot.RunMarkovSensor(this->GetDC(),sensorreading,RGB(255,255,0)); m_State=FINISHED; break; default: break; } } /* ** The method called from the slider */ void CMarkovRoomDlg::OnNMCustomdrawInitalangle(NMHDR *pNMHDR, LRESULT *pResult) { CString myString; LPNMCUSTOMDRAW pNMCD = reinterpret_cast(pNMHDR); CSliderCtrl* mySlider = (CSliderCtrl*) this->GetDlgItem(IDC_InitalAngle); CStatic* myInitTheta = (CStatic*) this->GetDlgItem(IDC_InitalTheta); myString.Format("%d",mySlider->GetPos()); myInitTheta->SetWindowText(myString); m_MainRobot.SetTheta(this->GetDC(),mySlider->GetPos()); *pResult = 0; }