using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace DM2DMH
{
	/// <summary>
	/// Form1 の概要の説明です。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ImageList imageList1;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.TextBox textBox5;
		private System.Windows.Forms.TextBox textBox4;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.TextBox textBox6;
		private System.Windows.Forms.TextBox textBox7;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.Button button4;
		private System.Windows.Forms.CheckBox checkBox1;
		private System.Windows.Forms.ComboBox comboBox1;
		private System.Windows.Forms.CheckBox checkBox2;
		private System.Windows.Forms.ComboBox comboBox2;
		private System.Windows.Forms.TextBox textBox8;
		private System.Windows.Forms.TextBox textBox9;
		private System.Windows.Forms.TextBox textBox10;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.TextBox textBox11;
		private System.Windows.Forms.ComboBox comboBox3;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.Label label10;
		private System.Windows.Forms.Label label11;
		private System.ComponentModel.IContainer components;

		public Form1()
		{
			//
			// Windows フォーム デザイナ サポートに必要です。
			//
			InitializeComponent();

			//
			// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
			//
		}

		/// <summary>
		/// 使用されているリソースに後処理を実行します。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
		/// コード エディタで変更しないでください。
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			this.imageList1 = new System.Windows.Forms.ImageList(this.components);
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.label9 = new System.Windows.Forms.Label();
			this.label8 = new System.Windows.Forms.Label();
			this.label7 = new System.Windows.Forms.Label();
			this.label6 = new System.Windows.Forms.Label();
			this.comboBox3 = new System.Windows.Forms.ComboBox();
			this.textBox11 = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.button2 = new System.Windows.Forms.Button();
			this.textBox5 = new System.Windows.Forms.TextBox();
			this.textBox4 = new System.Windows.Forms.TextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.label11 = new System.Windows.Forms.Label();
			this.label10 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.textBox10 = new System.Windows.Forms.TextBox();
			this.textBox9 = new System.Windows.Forms.TextBox();
			this.textBox8 = new System.Windows.Forms.TextBox();
			this.comboBox2 = new System.Windows.Forms.ComboBox();
			this.checkBox2 = new System.Windows.Forms.CheckBox();
			this.comboBox1 = new System.Windows.Forms.ComboBox();
			this.checkBox1 = new System.Windows.Forms.CheckBox();
			this.button4 = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			this.textBox7 = new System.Windows.Forms.TextBox();
			this.textBox6 = new System.Windows.Forms.TextBox();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.SuspendLayout();
			// 
			// imageList1
			// 
			this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
			this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
			this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
			this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.label9,
																					this.label8,
																					this.label7,
																					this.label6,
																					this.comboBox3,
																					this.textBox11,
																					this.label2,
																					this.label1,
																					this.button2,
																					this.textBox5,
																					this.textBox4,
																					this.button1,
																					this.textBox3,
																					this.textBox2,
																					this.textBox1});
			this.groupBox1.Location = new System.Drawing.Point(8, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(208, 296);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "ACTU";
			// 
			// label9
			// 
			this.label9.AutoSize = true;
			this.label9.Location = new System.Drawing.Point(136, 136);
			this.label9.Name = "label9";
			this.label9.Size = new System.Drawing.Size(26, 19);
			this.label9.TabIndex = 14;
			this.label9.Text = "?:";
			// 
			// label8
			// 
			this.label8.AutoSize = true;
			this.label8.Location = new System.Drawing.Point(136, 16);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(26, 19);
			this.label8.TabIndex = 6;
			this.label8.Text = "3:";
			// 
			// label7
			// 
			this.label7.AutoSize = true;
			this.label7.Location = new System.Drawing.Point(72, 16);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(26, 19);
			this.label7.TabIndex = 4;
			this.label7.Text = "2:";
			// 
			// label6
			// 
			this.label6.AutoSize = true;
			this.label6.Location = new System.Drawing.Point(8, 16);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(26, 19);
			this.label6.TabIndex = 2;
			this.label6.Text = "1:";
			// 
			// comboBox3
			// 
			this.comboBox3.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboBox3.Items.AddRange(new object[] {
														   "N",
														   "E",
														   "S",
														   "W"});
			this.comboBox3.Location = new System.Drawing.Point(8, 200);
			this.comboBox3.Name = "comboBox3";
			this.comboBox3.Size = new System.Drawing.Size(120, 26);
			this.comboBox3.TabIndex = 16;
			// 
			// textBox11
			// 
			this.textBox11.Location = new System.Drawing.Point(136, 160);
			this.textBox11.Name = "textBox11";
			this.textBox11.Size = new System.Drawing.Size(56, 26);
			this.textBox11.TabIndex = 15;
			this.textBox11.Text = "00";
			this.textBox11.Enter += new System.EventHandler(this.textBox1_Enter);
			// 
			// label2
			// 
			this.label2.AutoSize = true;
			this.label2.Location = new System.Drawing.Point(72, 136);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(26, 19);
			this.label2.TabIndex = 12;
			this.label2.Text = "y:";
			// 
			// label1
			// 
			this.label1.AutoSize = true;
			this.label1.Location = new System.Drawing.Point(8, 136);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(26, 19);
			this.label1.TabIndex = 10;
			this.label1.Text = "x:";
			// 
			// button2
			// 
			this.button2.Image = ((System.Drawing.Bitmap)(resources.GetObject("button2.Image")));
			this.button2.ImageIndex = 1;
			this.button2.ImageList = this.imageList1;
			this.button2.Location = new System.Drawing.Point(80, 80);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(48, 48);
			this.button2.TabIndex = 9;
			this.button2.Click += new System.EventHandler(this.button2_Click_1);
			// 
			// textBox5
			// 
			this.textBox5.Location = new System.Drawing.Point(72, 160);
			this.textBox5.Name = "textBox5";
			this.textBox5.Size = new System.Drawing.Size(56, 26);
			this.textBox5.TabIndex = 13;
			this.textBox5.Text = "00";
			this.textBox5.Enter += new System.EventHandler(this.textBox1_Enter);
			// 
			// textBox4
			// 
			this.textBox4.Location = new System.Drawing.Point(8, 160);
			this.textBox4.Name = "textBox4";
			this.textBox4.Size = new System.Drawing.Size(56, 26);
			this.textBox4.TabIndex = 11;
			this.textBox4.Text = "00";
			this.textBox4.Enter += new System.EventHandler(this.textBox1_Enter);
			// 
			// button1
			// 
			this.button1.Image = ((System.Drawing.Bitmap)(resources.GetObject("button1.Image")));
			this.button1.ImageIndex = 0;
			this.button1.ImageList = this.imageList1;
			this.button1.Location = new System.Drawing.Point(16, 80);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(48, 48);
			this.button1.TabIndex = 8;
			this.button1.Click += new System.EventHandler(this.button1_Click_1);
			// 
			// textBox3
			// 
			this.textBox3.Location = new System.Drawing.Point(136, 40);
			this.textBox3.Name = "textBox3";
			this.textBox3.Size = new System.Drawing.Size(56, 26);
			this.textBox3.TabIndex = 7;
			this.textBox3.Text = "0000";
			this.textBox3.Enter += new System.EventHandler(this.textBox1_Enter);
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(72, 40);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(56, 26);
			this.textBox2.TabIndex = 5;
			this.textBox2.Text = "0000";
			this.textBox2.Enter += new System.EventHandler(this.textBox1_Enter);
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(8, 40);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(56, 26);
			this.textBox1.TabIndex = 3;
			this.textBox1.Text = "0000";
			this.textBox1.Enter += new System.EventHandler(this.textBox1_Enter);
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.label11,
																					this.label10,
																					this.label5,
																					this.label4,
																					this.label3,
																					this.textBox10,
																					this.textBox9,
																					this.textBox8,
																					this.comboBox2,
																					this.checkBox2,
																					this.comboBox1,
																					this.checkBox1,
																					this.button4,
																					this.button3,
																					this.textBox7,
																					this.textBox6});
			this.groupBox2.Location = new System.Drawing.Point(224, 8);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(248, 296);
			this.groupBox2.TabIndex = 1;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "TELE";
			// 
			// label11
			// 
			this.label11.AutoSize = true;
			this.label11.Location = new System.Drawing.Point(72, 16);
			this.label11.Name = "label11";
			this.label11.Size = new System.Drawing.Size(26, 19);
			this.label11.TabIndex = 19;
			this.label11.Text = "2:";
			// 
			// label10
			// 
			this.label10.AutoSize = true;
			this.label10.Location = new System.Drawing.Point(8, 16);
			this.label10.Name = "label10";
			this.label10.Size = new System.Drawing.Size(26, 19);
			this.label10.TabIndex = 17;
			this.label10.Text = "1:";
			// 
			// label5
			// 
			this.label5.AutoSize = true;
			this.label5.Location = new System.Drawing.Point(136, 232);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(26, 19);
			this.label5.TabIndex = 31;
			this.label5.Text = "z:";
			// 
			// label4
			// 
			this.label4.AutoSize = true;
			this.label4.Location = new System.Drawing.Point(72, 232);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(26, 19);
			this.label4.TabIndex = 29;
			this.label4.Text = "y:";
			// 
			// label3
			// 
			this.label3.AutoSize = true;
			this.label3.Location = new System.Drawing.Point(8, 232);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(26, 19);
			this.label3.TabIndex = 27;
			this.label3.Text = "x:";
			// 
			// textBox10
			// 
			this.textBox10.Location = new System.Drawing.Point(136, 256);
			this.textBox10.Name = "textBox10";
			this.textBox10.Size = new System.Drawing.Size(56, 26);
			this.textBox10.TabIndex = 32;
			this.textBox10.Text = "000";
			this.textBox10.Enter += new System.EventHandler(this.textBox1_Enter);
			// 
			// textBox9
			// 
			this.textBox9.Location = new System.Drawing.Point(72, 256);
			this.textBox9.Name = "textBox9";
			this.textBox9.Size = new System.Drawing.Size(56, 26);
			this.textBox9.TabIndex = 30;
			this.textBox9.Text = "00";
			this.textBox9.Enter += new System.EventHandler(this.textBox1_Enter);
			// 
			// textBox8
			// 
			this.textBox8.Location = new System.Drawing.Point(8, 256);
			this.textBox8.Name = "textBox8";
			this.textBox8.Size = new System.Drawing.Size(56, 26);
			this.textBox8.TabIndex = 28;
			this.textBox8.Text = "00";
			this.textBox8.Enter += new System.EventHandler(this.textBox1_Enter);
			// 
			// comboBox2
			// 
			this.comboBox2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboBox2.Items.AddRange(new object[] {
														   "\'00\':Noop/N",
														   "\'01\':Clockwise/E",
														   "\'10\':Anti-Clockwise/S",
														   "\'11\':180°/W"});
			this.comboBox2.Location = new System.Drawing.Point(8, 200);
			this.comboBox2.Name = "comboBox2";
			this.comboBox2.Size = new System.Drawing.Size(224, 26);
			this.comboBox2.TabIndex = 26;
			// 
			// checkBox2
			// 
			this.checkBox2.Location = new System.Drawing.Point(8, 184);
			this.checkBox2.Name = "checkBox2";
			this.checkBox2.Size = new System.Drawing.Size(96, 16);
			this.checkBox2.TabIndex = 25;
			this.checkBox2.Text = "ROTATE ABS";
			// 
			// comboBox1
			// 
			this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboBox1.Items.AddRange(new object[] {
														   "\'00\':Items",
														   "\'01\':Creatures",
														   "\'10\':Items/Squad",
														   "\'11\':Anything"});
			this.comboBox1.Location = new System.Drawing.Point(8, 152);
			this.comboBox1.Name = "comboBox1";
			this.comboBox1.Size = new System.Drawing.Size(224, 26);
			this.comboBox1.TabIndex = 24;
			// 
			// checkBox1
			// 
			this.checkBox1.Location = new System.Drawing.Point(8, 136);
			this.checkBox1.Name = "checkBox1";
			this.checkBox1.Size = new System.Drawing.Size(120, 16);
			this.checkBox1.TabIndex = 23;
			this.checkBox1.Text = "BEEP";
			// 
			// button4
			// 
			this.button4.Image = ((System.Drawing.Bitmap)(resources.GetObject("button4.Image")));
			this.button4.ImageIndex = 1;
			this.button4.ImageList = this.imageList1;
			this.button4.Location = new System.Drawing.Point(80, 80);
			this.button4.Name = "button4";
			this.button4.Size = new System.Drawing.Size(48, 48);
			this.button4.TabIndex = 22;
			this.button4.Click += new System.EventHandler(this.button4_Click);
			// 
			// button3
			// 
			this.button3.Image = ((System.Drawing.Bitmap)(resources.GetObject("button3.Image")));
			this.button3.ImageIndex = 0;
			this.button3.ImageList = this.imageList1;
			this.button3.Location = new System.Drawing.Point(16, 80);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(48, 48);
			this.button3.TabIndex = 21;
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// textBox7
			// 
			this.textBox7.Location = new System.Drawing.Point(72, 40);
			this.textBox7.Name = "textBox7";
			this.textBox7.Size = new System.Drawing.Size(56, 26);
			this.textBox7.TabIndex = 20;
			this.textBox7.Text = "0000";
			this.textBox7.Enter += new System.EventHandler(this.textBox1_Enter);
			// 
			// textBox6
			// 
			this.textBox6.Location = new System.Drawing.Point(8, 40);
			this.textBox6.Name = "textBox6";
			this.textBox6.Size = new System.Drawing.Size(56, 26);
			this.textBox6.TabIndex = 18;
			this.textBox6.Text = "0000";
			this.textBox6.Enter += new System.EventHandler(this.textBox1_Enter);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(10, 19);
			this.ClientSize = new System.Drawing.Size(488, 317);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.groupBox2,
																		  this.groupBox1});
			this.Font = new System.Drawing.Font("Courier New", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Name = "Form1";
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// アプリケーションのメイン エントリ ポイントです。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
		}

		private uint[] GetActuVals()
		{
			uint[] nVal = new uint[] {
				uint.Parse(textBox1.Text, System.Globalization.NumberStyles.HexNumber),
				uint.Parse(textBox2.Text, System.Globalization.NumberStyles.HexNumber),
				uint.Parse(textBox3.Text, System.Globalization.NumberStyles.HexNumber)
			};
			return nVal;
		}
		private void SetActuVals(uint[] nVal)
		{
			textBox1.Text = nVal[0].ToString("X4");
			textBox2.Text = nVal[1].ToString("X4");
			textBox3.Text = nVal[2].ToString("X4");
		}
		private uint[] GetTeleVals()
		{
			uint[] nVal = new uint[] {
				uint.Parse(textBox6.Text, System.Globalization.NumberStyles.HexNumber),
				uint.Parse(textBox7.Text, System.Globalization.NumberStyles.HexNumber)
			};
			return nVal;
		}
		private void SetTeleVals(uint[] nVal)
		{
			textBox6.Text = nVal[0].ToString("X4");
			textBox7.Text = nVal[1].ToString("X4");
		}
		private uint GetSetVal(int iOff, int nLen)
		{
			return (uint)((1 << nLen) - 1) << iOff;
		}
		private uint GetBitVal(uint nVal, int iOff, int nLen)
		{
			return (nVal >> iOff) & GetSetVal(0, nLen);
		}

		private void button1_Click_1(object sender, System.EventArgs e)
		{
			uint[] nVal = GetActuVals();
			// X---XY---YDD????
			textBox4.Text           = GetBitVal(nVal[2],11, 5).ToString();
			textBox5.Text           = GetBitVal(nVal[2], 6, 5).ToString();
			comboBox3.SelectedIndex = (int)GetBitVal(nVal[2], 4, 2);
			textBox11.Text          = GetBitVal(nVal[2], 0, 4).ToString();
		}

		private void button2_Click_1(object sender, System.EventArgs e)
		{
			uint[] nVal = GetActuVals();
			//
			uint nX = uint.Parse(textBox4.Text);
			uint nY = uint.Parse(textBox5.Text);
			uint nDir = (uint)comboBox3.SelectedIndex;
			uint n0 = uint.Parse(textBox11.Text);
			nVal[2] = 0;
			nVal[2]|= (uint)GetBitVal(nX  , 0, 5) <<11;
			nVal[2]|= (uint)GetBitVal(nY  , 0, 5) << 6;
			nVal[2]|= (uint)GetBitVal(nDir, 0, 2) << 4;
			nVal[2]|= (uint)GetBitVal(n0  , 0, 4) << 0;
			//
			SetActuVals(nVal);
		}

		private void selectAll(
			System.Windows.Forms.TextBox x)
		{
			x.SelectAll();
		}

		private void textBox1_Enter(object sender, System.EventArgs e)
		{
			if (sender.GetType().Name == "TextBox")
			{
				selectAll((System.Windows.Forms.TextBox)sender);
			}
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
			comboBox1.SelectedIndex = 0;
			comboBox2.SelectedIndex = 0;
			comboBox3.SelectedIndex = 0;
		}

		private void button3_Click(object sender, System.EventArgs e)
		{
			uint[] nVal = GetTeleVals();
			// BTTR DDXX XXXY YYYY
			checkBox1.Checked       = GetBitVal(nVal[0],15, 1) != 0 ? true : false;
			comboBox1.SelectedIndex = (int)GetBitVal(nVal[0],13, 2);
			checkBox2.Checked       = GetBitVal(nVal[0],12, 1) != 0 ? true : false;
			comboBox2.SelectedIndex = (int)GetBitVal(nVal[0],10, 3);
			textBox8.Text           = GetBitVal(nVal[0], 5, 5).ToString();
			textBox9.Text           = GetBitVal(nVal[0], 0, 5).ToString();
			textBox10.Text          = GetBitVal(nVal[1], 8, 8).ToString();
			//
		}

		private void button4_Click(object sender, System.EventArgs e)
		{
			uint[] nVal = GetTeleVals();

			nVal[0] = 0;
			nVal[0]|= (uint)(checkBox1.Checked ? 0x8000 : 0x0000);
			nVal[0]|= GetBitVal((uint)(comboBox1.SelectedIndex), 0, 3) << 13;
			nVal[0]|= (uint)(checkBox2.Checked ? 0x1000 : 0x0000);
			nVal[0]|= GetBitVal((uint)(comboBox2.SelectedIndex), 0, 3) << 10;
			nVal[0]|= GetBitVal(uint.Parse(textBox8.Text), 0, 5) << 5;
			nVal[0]|= GetBitVal(uint.Parse(textBox9.Text), 0, 5) << 0;

			nVal[1]&= 0x00FF;
			nVal[1]|= GetBitVal(uint.Parse(textBox10.Text), 0, 8) << 8;

			SetTeleVals(nVal);
		}
	}
}
