public MainForm() { InitializeComponent(); // Добавляем список камер в комбобокс webCamsList = WebCam.GetDevicesList(); webCamChoiceComboBox.Items.AddRange(webCamsList); // Добавляем разрешения камеры в комбобокс foreach (CvSize size in webCamFrameSizes) { string s = size.Width.ToString() + " X " + size.Height.ToString(); webCamFrameSizeComboBox.Items.Add(s); } // Наблюдаем за состоянием формы observer = new MainFormStateObserver(); observer.ChangeStateEvent += onFormStateChange; observer.State = MainFormStateObserver.FormState.WebCamNotChosen; }
/// <summary> /// Срабатывает при изменении состояния формы /// </summary> /// <param name="formState"></param> private bool onFormStateChange(MainFormStateObserver.FormState oldState, MainFormStateObserver.FormState newState) { if ( oldState == MainFormStateObserver.FormState.WebCamNotChosen && newState != MainFormStateObserver.FormState.WebCamChosen ) { return false; } switch(newState) { // Выбор камеры case MainFormStateObserver.FormState.WebCamNotChosen: { ((Control)measurementTabPage).Enabled = false; ((Control)findContourTabPage).Enabled = false; ((Control)calibrationTabPage).Enabled = false; webCamChoiceComboBox.Enabled = true; webCamFrameSizeComboBox.Enabled = true; StartCameraButton.Enabled = true; StopCameraButton.Enabled = false; break; } // Состояние возникшее после выбора камеры case MainFormStateObserver.FormState.WebCamChosen: { ((Control)measurementTabPage).Enabled = true; ((Control)findContourTabPage).Enabled = true; ((Control)calibrationTabPage).Enabled = true; webCamChoiceComboBox.Enabled = false; webCamFrameSizeComboBox.Enabled = false; StartCameraButton.Enabled = false; StopCameraButton.Enabled = true; observer.State = MainFormStateObserver.FormState.Settings; routine.Run(ImageProcessingRoutine.RoutineAction.GetImage); break; } // Настройка камеры case MainFormStateObserver.FormState.Settings: { routine.Run(ImageProcessingRoutine.RoutineAction.GetImage); break; } // Процесс измерения case MainFormStateObserver.FormState.Measurement: { routine.Run(ImageProcessingRoutine.RoutineAction.Measure); break; } // Подбор фона case MainFormStateObserver.FormState.FindContour: { routine.Run(ImageProcessingRoutine.RoutineAction.FindContour); break; } // Калибровка case MainFormStateObserver.FormState.Calibration: { routine.Run(ImageProcessingRoutine.RoutineAction.DrawCorners); break; } } return true; }