///////////////////////////////////////////////////////////////////////////////
// TGEライブラリー (TGE.dll)
// Copyright (c) 2026 by Ysama
//
// 【概要】
//LOGOベースの描画命令を纏めた「命令セット(TGSet)」、その一覧である「命令リス
//ト(TGList)」、「命令リスト」から指定した命令セットで、繰り返しを含め実行順を
//定める「実行リスト(TGList)」及び編集ダイアログをまとめたNakov.TurtleGraphics
//の命令編集ライブラリー
///////////////////////////////////////////////////////////////////////////////
(続きの続き)
///////////////////
//CmdSetダイアログ
///////////////////
public class CmdSetDlg : Form
{
//クラスメンバーフィールド
public TGSet Tgs; //TGSetクラスインスタンス
Button Btn_Add; //命令追加ボタン
Button Btn_Del; //命令削除ボタン
Button Btn_Init; //初期化ボタン
Button Btn_OK; //OKボタン
Button Btn_Cancel; //Cancelボタン
ListView listView; //リストビュー
Label lblTimes; //繰り返し回数ラベル
TextBox txbTimes; //繰り返し回数入力用
Button Btn_Name; //名前・説明ボタン
Label lblName; //命令セット名ラベル
TextBox txbName; //命令セット名入力用
Label lblDescription; //説明ラベル
TextBox txbDescription; //説明入力用
//コンストラクター(1)
public CmdSetDlg()
{
this.ShowIcon = false;
this.Text = "命令セット入力ダイアログ";
this.ClientSize = new Size(594, 320);
this.MinimumSize = new Size(430, 340);
this.BackColor = SystemColors.Window;
this.Load += new EventHandler(CmdSetDlg_OnLoad);
this.Tgs = new TGSet(); //命令セット
this.Tgs.AddCmd(new int[] {-1,1,0,0}); //命令セット開始
}
//コンストラクター(2)
public CmdSetDlg(TGSet tgs)
{
this.ShowIcon = false;
this.Text = "命令セット編集ダイアログ";
this.ClientSize = new Size(594, 320);
this.MinimumSize = new Size(430, 340);
this.BackColor = SystemColors.Window;
this.Load += new EventHandler(CmdSetDlg_OnLoad);
this.Tgs = tgs; //命令セット参照渡し
}
//OnLoad時処理
protected void CmdSetDlg_OnLoad(object sender, EventArgs e)
{
Btn_Add = new Button();
Btn_Add.Text = "命令追加";
Btn_Add.TextAlign = ContentAlignment.MiddleCenter;
Btn_Add.Font = new Font("MS 明朝", 8);
Btn_Add.ForeColor = Color.Black;
Btn_Add.Size = new Size(80, 28);
Btn_Add.BackColor = SystemColors.Control;
Btn_Add.Location = new Point(ClientSize.Width - Btn_Add.Width - 10, 10);
Btn_Add.Anchor = (AnchorStyles.Top | AnchorStyles.Right);
Btn_Add.Click += new EventHandler(OnClick_Add);
Btn_Del = new Button();
Btn_Del.Text = "命令削除";
Btn_Del.TextAlign = ContentAlignment.MiddleCenter;
Btn_Del.Font = new Font("MS 明朝", 8);
Btn_Del.ForeColor = Color.Black;
Btn_Del.Size = new Size(80, 28);
Btn_Del.BackColor = SystemColors.Control;
Btn_Del.Location = new Point(ClientSize.Width - Btn_Del.Width - 10, 20 + Btn_Add.Height);
Btn_Del.Anchor = (AnchorStyles.Top | AnchorStyles.Right);
Btn_Del.Click += new EventHandler(OnClick_Del);
Btn_Init = new Button();
Btn_Init.Text = "命令初期化";
Btn_Init.TextAlign = ContentAlignment.MiddleCenter;
Btn_Init.Font = new Font("MS 明朝", 8);
Btn_Init.ForeColor = Color.Black;
Btn_Init.Size = new Size(80, 28);
Btn_Init.BackColor = SystemColors.Control;
Btn_Init.Location = new Point(ClientSize.Width - Btn_Init.Width - 10, 30 + Btn_Add.Height + Btn_Del.Height);
Btn_Init.Anchor = (AnchorStyles.Top | AnchorStyles.Right);
Btn_Init.Click += new EventHandler(OnClick_Init);
Btn_OK = new Button();
Btn_OK.Text = "OK";
Btn_OK.TextAlign = ContentAlignment.MiddleCenter;
Btn_OK.Font = new Font("MS 明朝", 8);
Btn_OK.ForeColor = Color.Black;
Btn_OK.Size = new Size(80, 28);
Btn_OK.BackColor = SystemColors.Control;
Btn_OK.Location = new Point(ClientSize.Width - Btn_OK.Width - 10, ClientSize.Height - Btn_OK.Height - 10);
Btn_OK.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);
Btn_OK.Click += new EventHandler(OnClick_OK);
Btn_Cancel = new Button();
Btn_Cancel.Text = "Cancel";
Btn_Cancel.TextAlign = ContentAlignment.MiddleCenter;
Btn_Cancel.Font = new Font("MS 明朝", 8);
Btn_Cancel.ForeColor = Color.Black;
Btn_Cancel.Size = new Size(80, 28);
Btn_Cancel.BackColor = SystemColors.Control;
Btn_Cancel.Location = new Point(ClientSize.Width - Btn_Cancel.Width - 10, ClientSize.Height - Btn_Cancel.Height - Btn_OK.Height - 20);
Btn_Cancel.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);
Btn_Cancel.Click += new EventHandler(OnClick_Cancel);
lblTimes = new Label();
lblTimes.Size = new Size(80, 28);
lblTimes.Location = new Point(ClientSize.Width - lblTimes.Width - 10, 40 + Btn_Add.Height + Btn_Del.Height + Btn_Init.Height);
lblTimes.Anchor = (AnchorStyles.Top | AnchorStyles.Right);
lblTimes.AutoSize = false;
lblTimes.TextAlign = ContentAlignment.MiddleCenter;
lblTimes.Font = new Font("MS 明朝", 8);
lblTimes.Text = "繰り返し回数";
txbTimes = new TextBox();
txbTimes.Size = new Size(80, 28);
txbTimes.Location = new Point(ClientSize.Width - lblTimes.Width - 10, 40 + Btn_Add.Height + Btn_Del.Height + Btn_Init.Height + lblTimes.Height);
txbTimes.Anchor = (AnchorStyles.Top | AnchorStyles.Right);
txbTimes.TextAlign = HorizontalAlignment.Right;
txbTimes.Text ="1";
Btn_Name = new Button();
Btn_Name.Text = "名前・説明";
Btn_Name.TextAlign = ContentAlignment.MiddleCenter;
Btn_Name.Font = new Font("MS 明朝", 8);
Btn_Name.ForeColor = Color.Black;
Btn_Name.Size = new Size(80, 28);
Btn_Name.BackColor = SystemColors.Control;
Btn_Name.Location = new Point(ClientSize.Width - Btn_Init.Width - 10, 50 + Btn_Add.Height + Btn_Del.Height + Btn_Init.Height + lblTimes.Height + txbTimes.Height);
Btn_Name.Anchor = (AnchorStyles.Top | AnchorStyles.Right);
Btn_Name.Click += new EventHandler(OnClick_Name);
lblName = new Label();
lblName.Size = new Size(80, 18);
lblName.Location = new Point(10, 10);
lblName.Anchor = (AnchorStyles.Top | AnchorStyles.Left);
lblName.AutoSize = false;
lblName.Font = new Font("MS 明朝", 8);
lblName.Text = "命令セット名";
lblName.Visible = false;
txbName = new TextBox();
txbName.Size = new Size(ClientSize.Width - Btn_Add.Width - 30, 28);
txbName.Location = new Point(10, lblName.Height + 10);
txbName.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
txbName.Visible = false;
lblDescription = new Label();
lblDescription.Size = new Size(80, 18);
lblDescription.Location = new Point(10, lblName.Height + txbName.Height + 20);
lblDescription.Anchor = (AnchorStyles.Top | AnchorStyles.Left);
lblDescription.AutoSize = false;
lblDescription.Font = new Font("MS 明朝", 8);
lblDescription.Text = "説明";
lblDescription.Visible = false;
txbDescription = new TextBox();
txbDescription.Size = new Size(ClientSize.Width - Btn_Add.Width - 30, ClientSize.Height - lblName.Height - txbName.Height - lblDescription.Height - 30);
txbDescription.Location = new Point(10, lblName.Height + txbName.Height + lblDescription.Height + 20);
txbDescription.Multiline = true;
txbDescription.WordWrap = true;
txbDescription.ScrollBars = ScrollBars.Vertical;
txbDescription.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right);
txbDescription.Visible = false;
this.Controls.Add(Btn_Add);
this.Controls.Add(Btn_Del);
this.Controls.Add(Btn_Init);
this.Controls.Add(lblTimes);
this.Controls.Add(txbTimes);
this.Controls.Add(Btn_Name);
this.Controls.Add(Btn_Cancel);
this.Controls.Add(Btn_OK);
this.Controls.Add(lblName);
this.Controls.Add(txbName);
this.Controls.Add(lblDescription);
this.Controls.Add(txbDescription);
//リストビュー
listView = new ListView();
listView.BorderStyle = BorderStyle.Fixed3D; //境界
listView.Location = new Point(10, 10); //位置
listView.Width = ClientSize.Width - Btn_Add.Width - 30; //サイズ
listView.Height = ClientSize.Height - 20;
listView.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right); //アンカー固定
listView.FullRowSelect = true; //一行選択
listView.MultiSelect = false; //複数選択不可
listView.GridLines = true; //グリッドラインの表示
listView.HoverSelection = false; //ポイントで選択できるようにする
listView.Activation = ItemActivation.OneClick; //シングルクリック選択;
listView.CheckBoxes = false; //チェックボックス無効化
listView.View = View.Details; //リストビューの表示方法
listView.AllowDrop = false; //listViewへのドロップを受け入れない
//イベントハンドラー
listView.MouseDoubleClick+= new MouseEventHandler(ListView_DoubleClick);
//ヘッダー定義
ColumnHeader columnCmd = new ColumnHeader{Text = "命令", Width = 80};
ColumnHeader columnExp = new ColumnHeader{Text = "説明", Width = 220};
ColumnHeader columnArg1 = new ColumnHeader{Text = "引数1", Width = 60};
columnArg1.TextAlign = HorizontalAlignment.Center;
ColumnHeader columnArg2 = new ColumnHeader{Text = "引数2", Width = 60};
columnArg2.TextAlign = HorizontalAlignment.Center;
ColumnHeader columnArg3 = new ColumnHeader{Text = "引数3", Width = 60};
columnArg3.TextAlign = HorizontalAlignment.Center;
ColumnHeader columnCmdNo = new ColumnHeader{Text = "命令番号", Width = 60};
columnCmdNo.TextAlign = HorizontalAlignment.Center;
ColumnHeader[] colHeaderRegValue = {columnCmd, columnExp, columnArg1, columnArg2, columnArg3, columnCmdNo}; //ヘッダー設定
listView.Columns.AddRange(colHeaderRegValue); //ヘッダー追加
//編集ダイアログの場合の初期設定
if(Tgs.Count() > 1)
{
string[] CmdNameList = new string[15] {"Nop", "Forward()", "Backward()",
"MoveTo()", "Rotate()", "RotateTo()",
"PenUp()", "PenDown()", "X",
"Y", "Angle", "PenColor",
"PenSize", "PenVisible", "Delay"};
string[] CmdExplList = new string[15] {"何もしない", "前進する", "後退する",
"絶対座標へ移動する", "現在の方向から指定角度回転", "絶対角度へ回転(↑0→90↓180←270)",
"描画停止", "描画開始", "X座標指定(移動)",
"X座標指定(移動)", "角度指定(↑0→90↓180←270)",
"描画色指定(Color)", "描画幅指定", "可視可(1)、不可(0)", "中断時間(ms)"};
for(int i = 1; i < Tgs.Count(); i++)
{
int[] cmd = Tgs.GetCmd(i);
string[] item = {CmdNameList[cmd[0]], CmdExplList[cmd[0]],
cmd[1].ToString(), cmd[2].ToString(),
cmd[3].ToString(), cmd[0].ToString()};
listView.Items.Add(new ListViewItem(item));
}
txbName.Text = Tgs.Name; //命令セット名
txbDescription.Text = Tgs.Description; //説明
int[] hedder = Tgs.GetCmd(0); //"-1"のある最初の命令行を取得
txbTimes.Text = hedder[1].ToString(); //"-1"の次が繰り返し回数
}
//FormにListViewを追加
this.Controls.Add(listView);
}
//終了時処理
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
DialogResult dr = MessageBox.Show("終了しますか?", "確認", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if(dr == DialogResult.No)
{
e.Cancel = true;
}
}
//Addボタンがクリックされた時
protected void OnClick_Add(object sender, EventArgs e)
{
//入力ダイアログによる入力
InputDlg inpDlg = new InputDlg();
inpDlg.ShowDialog();
if(inpDlg.dtData[0] == -1)
MessageBox.Show("入力がキャンセルされました。", "注意", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
else
{
//原本のリストに追加
Tgs.AddCmd(inpDlg.dtData);
//その内容をListViewに表示
string[] item = {inpDlg.dtCmd[0], inpDlg.dtCmd[1],
inpDlg.dtData[1].ToString(), inpDlg.dtData[2].ToString(),
inpDlg.dtData[3].ToString(), inpDlg.dtData[0].ToString()};
listView.Items.Add(new ListViewItem(item));
}
inpDlg.Dispose();
}
//Delボタンがクリックされた時
protected void OnClick_Del(object sender, EventArgs e)
{
//選択された行を確認する
if(listView.SelectedItems.Count == 0)
MessageBox.Show("データが選択されていません", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
else
{
int selected_index = listView.SelectedIndices[0];
Tgs.RemoveAt(selected_index + 1); //Tgs.CmdSetはlistViewよりも1行多い
listView.Items.RemoveAt(selected_index);
}
}
//Initボタンがクリックされた時
protected void OnClick_Init(object sender, EventArgs e)
{
Tgs.Clear();
listView.Items.Clear();
}
//OKボタンがクリックされた時
protected void OnClick_OK(object sender, EventArgs e)
{
int times = 0;
if(int.TryParse(txbTimes.Text, out times))
Tgs.SetCmd(0, -1, times, 0, 0); //TgsのCmdSet[0]に"-1,回数,0,0"を代入"
else
{ //繰り返し回数が異常値の場合は終了させない
MessageBox.Show("繰り返し回数が整数ではありません。", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
//受け渡しデータに値を入れる
Tgs.Name = txbName.Text;
Tgs.Description = txbDescription.Text;
//このフォームの戻り値
DialogResult = DialogResult.OK;
Close();
}
//Cancelボタンがクリックされた時
protected void OnClick_Cancel(object sender, EventArgs e)
{
//受け渡しデータを初期化する(編集モードでTGSetの参照入力をしている場合、注意が必要!)
Tgs.Clear();
Tgs.Name = String.Empty;
Tgs.Description = String.Empty;
//このフォームの戻り値
DialogResult = DialogResult.Cancel;
Close();
}
//Namelボタンがクリックされた時(解説:これが「早変わり」の舞台裏)
protected void OnClick_Name(object sender, EventArgs e)
{
if(Btn_Name.Text == "名前・説明")
{
Btn_Name.Text = "命令セット";
listView.Visible = false;
Btn_Add.Visible = false;
Btn_Del.Visible = false;
Btn_Init.Visible = false;
lblTimes.Visible = false;
txbTimes.Visible = false;
lblName.Visible = true;
txbName.Visible = true;
lblDescription.Visible = true;
txbDescription.Visible = true;
}
else
{
Btn_Name.Text = "名前・説明";
listView.Visible = true;
Btn_Add.Visible = true;
Btn_Del.Visible = true;
Btn_Init.Visible = true;
lblTimes.Visible = true;
txbTimes.Visible = true;
lblName.Visible = false;
txbName.Visible = false;
lblDescription.Visible = false;
txbDescription.Visible = false;
}
}
//listViewがダブルクリックされた時
protected void ListView_DoubleClick(object sender, MouseEventArgs e)
{
//選択された項目があるか確認
if(listView.SelectedIndices.Count > 0)
{
//入力ダイアログによる入力
int selected_index = listView.SelectedIndices[0];
InputDlg inpDlg = new InputDlg(Tgs.GetCmd(selected_index + 1));
inpDlg.ShowDialog(this);
//入力ダイアログによる入力
if(inpDlg.dtData[0] == -1)
MessageBox.Show("入力がキャンセルされました。", "注意", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
else
{
Tgs.SetCmd(selected_index + 1, (int[])inpDlg.dtData.Clone()); //Tgs.CmdSetはlistViewよりも1行多い
listView.Items[selected_index].SubItems[0].Text = inpDlg.dtCmd[0];
listView.Items[selected_index].SubItems[1].Text = inpDlg.dtCmd[1];
listView.Items[selected_index].SubItems[2].Text = inpDlg.dtData[1].ToString();
listView.Items[selected_index].SubItems[3].Text = inpDlg.dtData[2].ToString();
listView.Items[selected_index].SubItems[4].Text = inpDlg.dtData[3].ToString();
listView.Items[selected_index].SubItems[5].Text = inpDlg.dtData[0].ToString();
}
inpDlg.Dispose();
}
}
}