Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

ООП / Project2 / Project2 / Project2 / MyForm1

.h
Скачиваний:
13
Добавлен:
18.02.2017
Размер:
9.19 Кб
Скачать
#pragma once
#include "Scene.h"
#include "Figure.h"
namespace Project2 {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	public ref class MyForm : public System::Windows::Forms::Form
	{
	private: bool isMoving;
		public:
			MyForm(void)
			{
				InitializeComponent();

				this->gapPositionAngle = this->trackBar1->Value*PI/180;
				this->startVelocity = this->trackBar2->Value;
				this->acceleration = this->trackBar3->Value;
				isRunning = false;
				isMoving = true;

				scene = gcnew Scene();
				scene->radius = radius;
				scene->gapPositionAngle = gapPositionAngle;
				scene->gapAngle = gapAngle;
				scene->wallX = wallX;
				scene->Init();

				
				this->pictureBox1->Image = scene->Draw();
			}

		protected:
			~MyForm()
			{
				if (components)
				{
					delete components;
				}
			}
		private: System::Windows::Forms::TrackBar^  trackBar1;
		private: System::Windows::Forms::TrackBar^  trackBar2;
		private: System::Windows::Forms::TrackBar^  trackBar3;
		private: System::Windows::Forms::Label^  label1;
		private: System::Windows::Forms::Label^  label2;
		private: System::Windows::Forms::Label^  label3;
		private: System::Windows::Forms::Button^  button1;
		private: System::Windows::Forms::PictureBox^  pictureBox1;

		private: 
			static const double radius = 50;
			static const double gapAngle = 30*3.1416/180;
			static const double wallX = 100;
			static const double deltaTime = 0.02;

			bool isRunning;

			double gapPositionAngle;
			double startVelocity;
			double acceleration;

			Figure^ figure;
			System::Windows::Forms::Timer^ timer;
			Scene^ scene;

			

		private:
			System::ComponentModel::Container ^components;


			void InitializeComponent(void)
			{
				this->trackBar1 = (gcnew System::Windows::Forms::TrackBar());
				this->trackBar2 = (gcnew System::Windows::Forms::TrackBar());
				this->trackBar3 = (gcnew System::Windows::Forms::TrackBar());
				this->label1 = (gcnew System::Windows::Forms::Label());
				this->label2 = (gcnew System::Windows::Forms::Label());
				this->label3 = (gcnew System::Windows::Forms::Label());
				this->button1 = (gcnew System::Windows::Forms::Button());
				this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
				(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->trackBar1))->BeginInit();
				(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->trackBar2))->BeginInit();
				(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->trackBar3))->BeginInit();
				(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->BeginInit();
				this->SuspendLayout();
				// 
				// trackBar1
				// 
				this->trackBar1->Location = System::Drawing::Point(284, 42);
				this->trackBar1->Maximum = 180;
				this->trackBar1->Minimum = -180;
				this->trackBar1->Name = L"trackBar1";
				this->trackBar1->Size = System::Drawing::Size(184, 42);
				this->trackBar1->TabIndex = 0;
				this->trackBar1->Scroll += gcnew System::EventHandler(this, &MyForm::trackBar1_Scroll);
				// 
				// trackBar2
				// 
				this->trackBar2->Location = System::Drawing::Point(284, 119);
				this->trackBar2->Maximum = 500;
				this->trackBar2->Minimum = 50;
				this->trackBar2->Name = L"trackBar2";
				this->trackBar2->Size = System::Drawing::Size(184, 42);
				this->trackBar2->TabIndex = 1;
				this->trackBar2->Value = 50;
				this->trackBar2->Scroll += gcnew System::EventHandler(this, &MyForm::trackBar2_Scroll);
				// 
				// trackBar3
				// 
				this->trackBar3->Location = System::Drawing::Point(284, 216);
				this->trackBar3->Name = L"trackBar3";
				this->trackBar3->Size = System::Drawing::Size(184, 42);
				this->trackBar3->TabIndex = 2;
				this->trackBar3->Value = 1;
				this->trackBar3->Scroll += gcnew System::EventHandler(this, &MyForm::trackBar3_Scroll);
				// 
				// label1
				// 
				this->label1->AutoSize = true;
				this->label1->Location = System::Drawing::Point(351, 13);
				this->label1->Name = L"label1";
				this->label1->Size = System::Drawing::Size(50, 13);
				this->label1->TabIndex = 3;
				this->label1->Text = L"ArcAngle";
				// 
				// label2
				// 
				this->label2->AutoSize = true;
				this->label2->Location = System::Drawing::Point(351, 101);
				this->label2->Name = L"label2";
				this->label2->Size = System::Drawing::Size(44, 13);
				this->label2->TabIndex = 4;
				this->label2->Text = L"Velocity";
				// 
				// label3
				// 
				this->label3->AutoSize = true;
				this->label3->Location = System::Drawing::Point(351, 178);
				this->label3->Name = L"label3";
				this->label3->Size = System::Drawing::Size(66, 13);
				this->label3->TabIndex = 5;
				this->label3->Text = L"Acceleration";
				// 
				// button1
				// 
				this->button1->Location = System::Drawing::Point(350, 278);
				this->button1->Name = L"button1";
				this->button1->Size = System::Drawing::Size(75, 23);
				this->button1->TabIndex = 6;
				this->button1->Text = L"Go!";
				this->button1->UseVisualStyleBackColor = true;
				this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click);
				// 
				// pictureBox1
				// 
				this->pictureBox1->Location = System::Drawing::Point(13, 13);
				this->pictureBox1->Name = L"pictureBox1";
				this->pictureBox1->Size = System::Drawing::Size(250, 250);
				this->pictureBox1->TabIndex = 7;
				this->pictureBox1->TabStop = false;
				// 
				// MyForm
				// 
				this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
				this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
				this->ClientSize = System::Drawing::Size(503, 337);
				this->Controls->Add(this->pictureBox1);
				this->Controls->Add(this->button1);
				this->Controls->Add(this->label3);
				this->Controls->Add(this->label2);
				this->Controls->Add(this->label1);
				this->Controls->Add(this->trackBar3);
				this->Controls->Add(this->trackBar2);
				this->Controls->Add(this->trackBar1);
				this->Name = L"MyForm";
				this->Text = L"MyForm";
				(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->trackBar1))->EndInit();
				(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->trackBar2))->EndInit();
				(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->trackBar3))->EndInit();
				(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->EndInit();
				this->ResumeLayout(false);
				this->PerformLayout();

			}


	private:
			System::Void trackBar1_Scroll(System::Object^  sender, System::EventArgs^  e)
			{
				this->gapPositionAngle = this->trackBar1->Value*PI/180;
				this->scene->gapPositionAngle = this->gapPositionAngle;

				this->label1->Text = this->trackBar1->Value.ToString() + " (" + (this->trackBar1->Value*PI/180).ToString() + ")";

				this->scene->Init();
				this->pictureBox1->Image = this->scene->Draw();

				Refresh();
			}


			System::Void trackBar2_Scroll(System::Object^  sender, System::EventArgs^  e)
			{
				this->startVelocity = trackBar2->Value;

			}


			System::Void trackBar3_Scroll(System::Object^  sender, System::EventArgs^  e)
			{
				this->acceleration = trackBar3->Value;

			}


			System::Void FigureMove(System::Object^  sender, System::EventArgs^  e)
			{
				this->figure->gapPositionAngle = gapPositionAngle;
				this->figure->ComputeStartEnd();

				if(isMoving)
				{
					try
					{
						this->figure->Tick();
					}
					catch(System::Exception^ e)
					{
						isMoving = false;
						MessageBox::Show(e->Message);
					}
				}

				delete this->pictureBox1->Image;
				this->pictureBox1->Image = this->scene->Draw();
				//this->Refresh();
				this->figure->Draw(this->pictureBox1->Image);

				this->Refresh();
			}


			System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
			{
				isRunning = !isRunning;

				if(isRunning)
				{
					this->trackBar2->Enabled = false;
					this->trackBar3->Enabled = false;

					this->button1->Text = "Stop!";

					this->figure = gcnew Figure();

					this->figure->acceleration = acceleration;
					this->figure->velocity = startVelocity;
					this->figure->deltaTime = deltaTime;
					this->figure->gapPositionAngle = gapPositionAngle;
					this->figure->wallX = wallX;
					this->figure->gapAngle = gapAngle;
					this->figure->radius = radius;
					this->figure->Init();

					this->timer = gcnew System::Windows::Forms::Timer();
					this->timer->Interval = deltaTime*1000;
					this->timer->Tick += gcnew System::EventHandler(this, &MyForm::FigureMove);
					this->timer->Start();

				}
				else
				{
					this->trackBar2->Enabled = true;
					this->trackBar3->Enabled = true;

					isMoving = true;

					this->button1->Text = "Go!";

					this->timer->Stop();

				}
			}
};
}
Соседние файлы в папке Project2