ReversiForm  1.0.0
ReversiForm
FormMain.cs
Go to the documentation of this file.
1 
18 using System;
19 using System.Collections.Generic;
20 using System.ComponentModel;
21 using System.Data;
22 using System.Drawing;
23 using System.Drawing.Drawing2D;
24 using System.IO;
25 using System.Linq;
26 using System.Reflection;
27 using System.Text;
28 using System.Threading.Tasks;
29 using System.Timers;
30 using System.Windows.Forms;
31 using System.Xml.Serialization;
32 
33 namespace ReversiForm
34 {
40  public partial class Reversi : Form
41  {
42  delegate void ViewMsgDlgDelegate(string title , string msg);
43  delegate void DrawSingleDelegate(int y, int x, int sts, int bk, string text);
44  delegate void CurColMsgDelegate(string text);
45  delegate void CurStsMsgDelegate(string text);
46  delegate void Reversi_ResizeEndDelegate(object sender, EventArgs e);
47 
50  private static System.Timers.Timer aTimer;
51  private Size oldSize;
52 
61  public Reversi()
62  {
64  oldSize.Width = 0;
65  oldSize.Height = 0;
66 
67  System.IO.Directory.CreateDirectory(System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\Y.Y Magic\\ReversiForm");
68  string setPath = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\Y.Y Magic\\ReversiForm\\" + "AppSetting.xml";
69  try
70  {
71  this.m_AppSettings = this.LoadSettingXml(setPath);
72  if(this.m_AppSettings == null)
73  {
74  this.m_AppSettings = new ReversiSetting();
75  this.SaveSettingXml(setPath,ref this.m_AppSettings);
76  }
77  this.m_ReversiPlay = new ReversiPlay();
78  this.m_ReversiPlay.mSetting = this.m_AppSettings;
79  this.m_ReversiPlay.viewMsgDlg = this.ViewMsgDlg;
80  this.m_ReversiPlay.drawSingle = this.DrawSingle;
81  this.m_ReversiPlay.curColMsg = this.CurColMsg;
82  this.m_ReversiPlay.curStsMsg = this.CurStsMsg;
83  this.appInit();
84  Task newTask = new Task( () => { this.m_ReversiPlay.reset(); } );
85  newTask.Start();
86  }
87  catch (Exception ex)
88  {
89  System.Console.WriteLine("FormMain(1) : " + ex.Message);
90  System.Console.WriteLine("FormMain(1) : " + ex.StackTrace);
91  }
92  }
93 
103  public ReversiSetting LoadSettingXml(string path)
104  {
105  ReversiSetting ret = null;
106  try
107  {
108  // *** XMLをReversiSettingオブジェクトに読み込む *** //
109  XmlSerializer serializer = new XmlSerializer(typeof(ReversiSetting));
110  ret = new ReversiSetting();
111 
112  FileStream fsr = new FileStream(path, FileMode.Open);
113 
114  // *** XMLファイルを読み込み、逆シリアル化(復元)する *** //
115  ret = (ReversiSetting)serializer.Deserialize(fsr);
116  fsr.Close();
117  }
118  catch (Exception ex)
119  {
120  System.Console.WriteLine("LoadSettingXml() : " + ex.Message);
121  System.Console.WriteLine("LoadSettingXml() : " + ex.StackTrace);
122  ret = null;
123  }
124  return ret;
125  }
126 
137  public int SaveSettingXml(string path,ref ReversiSetting appSet)
138  {
139  int ret = 0;
140  try
141  {
142  // *** XMLをReversiSettingオブジェクトに読み込む *** //
143  XmlSerializer serializer = new XmlSerializer(typeof(ReversiSetting));
144 
145  // *** カレントディレクトリに"AppSetting.xml"というファイルで書き出す *** //
146  FileStream fsw = new FileStream(path, FileMode.Create);
147 
148  // *** オブジェクトをシリアル化してXMLファイルに書き込む *** //
149  serializer.Serialize(fsw, appSet);
150  fsw.Close();
151  }
152  catch (Exception exl)
153  {
154  System.Console.WriteLine("SaveSettingXml() : " + exl.Message);
155  System.Console.WriteLine("SaveSettingXml() : " + exl.StackTrace);
156  ret = -1;
157  }
158  return ret;
159  }
160 
169  public void appInit()
170  {
171  // *** tableLayoutPanelの最適化 *** //
172  Size formSize = this.Size;
173  Size tblSize = this.tableLayoutPanel1.Size;
174  int startX = 45;
175  int startY = 45;
176  // *** 各種オフセットを設定 *** //
177  formSize.Height = this.label1.Top;
178  formSize.Height -= startX << 1;
179  formSize.Width -= startY << 1;
180  int refSize = formSize.Height;
181  if (formSize.Width < refSize) refSize = formSize.Width;
182  double tmpD = (double)refSize / this.m_AppSettings.mMasuCnt;
183  refSize = (int)Math.Ceiling(tmpD);
184  refSize *= this.m_AppSettings.mMasuCnt;
185 
186  this.tableLayoutPanel1.Top = startX;
187  this.tableLayoutPanel1.Left = ( ( formSize.Width + ( startY << 1 ) ) - refSize ) >> 1;
188  tblSize.Height = refSize;
189  tblSize.Width = refSize;
190  this.tableLayoutPanel1.Height = refSize;
191  this.tableLayoutPanel1.Width = refSize;
192 
193  Size curSize = tblSize;
194  float cellSizeAll = (float)curSize.Height;
195  if (curSize.Width < cellSizeAll) cellSizeAll = (float)curSize.Width;
196  float cellSize = cellSizeAll / (float)this.m_AppSettings.mMasuCnt;
197  float per = cellSize / cellSizeAll * 100F;
198  this.tableLayoutPanel1.Visible = false;
199  for (int i = 0; i < ReversiConst.DEF_MASU_CNT_MAX_VAL;i++)
200  {
201  for (int j = 0; j < ReversiConst.DEF_MASU_CNT_MAX_VAL;j++)
202  {
203  int curIdx = (i * ReversiConst.DEF_MASU_CNT_MAX_VAL) + j + 1;
204  string curIdxStr = "pictureBox" + curIdx.ToString();
205  Control c = this.tableLayoutPanel1.Controls[curIdxStr];
206  if (c != null)
207  {
208  if( i < this.m_AppSettings.mMasuCnt && j < this.m_AppSettings.mMasuCnt)
209  {
210  c.Visible = true;
211  }
212  else
213  {
214  c.Visible = false;
215  }
216  }
217  // *** テーブルの列サイズを調整 *** //
218  if(j < this.m_AppSettings.mMasuCnt)
219  {
220  this.tableLayoutPanel1.ColumnStyles[j] = new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, cellSize);
221  }
222  else
223  {
224  this.tableLayoutPanel1.ColumnStyles[j] = new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F);
225  }
226  }
227  // *** テーブルの行サイズを調整 *** //
228  if(i < this.m_AppSettings.mMasuCnt)
229  {
230  this.tableLayoutPanel1.RowStyles[i] = new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, cellSize);
231  }
232  else
233  {
234  this.tableLayoutPanel1.RowStyles[i] = new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 0F);
235  }
236  }
237  this.tableLayoutPanel1.Visible = true;
238  }
239 
250  public void ViewMsgDlg(string title , string msg)
251  {
252  Invoke(new ViewMsgDlgDelegate(ViewMsgDlgLocal), title, msg);
253  }
254 
265  public void ViewMsgDlgLocal(string title , string msg)
266  {
267  MessageBox.Show(msg, title, MessageBoxButtons.OK, MessageBoxIcon.Information);
268  }
269 
283  public void DrawSingle(int y, int x, int sts, int bk, string text)
284  {
285  Invoke(new DrawSingleDelegate(DrawSingleLocal), y, x, sts, bk, text);
286  }
287 
301  public void DrawSingleLocal(int y, int x, int sts, int bk, string text)
302  {
303  PictureBox curPict = (PictureBox) this.tableLayoutPanel1.GetControlFromPosition(x,y);
304  if(curPict != null)
305  {
306  // 描画先とするImageオブジェクトを作成する
307  Bitmap canvas = new Bitmap(curPict.Width, curPict.Height);
308  // ImageオブジェクトのGraphicsオブジェクトを作成する
309  Graphics g = Graphics.FromImage(canvas);
310  g.SmoothingMode = SmoothingMode.HighQuality;
311  Pen curPen1 = new Pen(m_AppSettings.mBorderColor,2);
312  // Brushオブジェクトの作成
313  SolidBrush curBru1 = null;
314  SolidBrush curBru2 = null;
315  SolidBrush curBru3 = null;
316  Color curBkColor = m_AppSettings.mBackGroundColor;
317  byte tmpA = curBkColor.A;
318  byte tmpR = curBkColor.R;
319  byte tmpG = curBkColor.G;
320  byte tmpB = curBkColor.B;
321  Color curBkColorRev;
322 
323  if (bk == 1) {
324  // *** cell_back_blue *** //
325  tmpG -= 127;
326  if (tmpG < 0) tmpG += 255;
327  tmpB += 255;
328  if (255 < tmpB) tmpB -= 255;
329  curBkColor = Color.FromArgb(tmpA, tmpR, tmpG, tmpB);
330  }
331  else if (bk == 2) {
332  // *** cell_back_red *** //
333  tmpR += 255;
334  if(255 < tmpR) tmpR -= 255;
335  tmpG -= 127;
336  if(tmpG < 0) tmpG += 255;
337  tmpB -= 127;
338  if(tmpB < 0) tmpB += 255;
339  curBkColor = Color.FromArgb(tmpA, tmpR, tmpG, tmpB);
340  } else if (bk == 3) {
341  // *** cell_back_magenta *** //
342  tmpR += 255;
343  if(255 < tmpR) tmpR -= 255;
344  tmpB += 255;
345  if(255 < tmpB) tmpB -= 255;
346  curBkColor = Color.FromArgb(tmpA, tmpR, tmpG, tmpB);
347  } else {
348  // *** cell_back_green *** //
349  curBkColor = Color.FromArgb(tmpA, tmpR, tmpG, tmpB);
350  }
351  curBru1 = new SolidBrush(curBkColor);
352  HslColor workCol = HslColor.FromRgb(curBkColor);
353  float h = workCol.H + 180F;
354  if (359F < h) h -= 360F;
355  curBkColorRev = HslColor.ToRgb(new HslColor(h, workCol.S, workCol.L));
356  curBru3 = new SolidBrush(curBkColorRev);
357 
358  if (sts == ReversiConst.REVERSI_STS_NONE)
359  {
360  }
361  else if (sts == ReversiConst.REVERSI_STS_BLACK)
362  {
363  curBru2 = new SolidBrush(m_AppSettings.mPlayerColor1);
364  }
365  else if (sts == ReversiConst.REVERSI_STS_WHITE)
366  {
367  curBru2 = new SolidBrush(m_AppSettings.mPlayerColor2);
368  }
369 
370  // 位置(x, y)にWidth x Heightの四角を描く
371  g.FillRectangle(curBru1, 0, 0, curPict.Width, curPict.Height);
372  // 位置(x, y)にWidth x Heightの四角を描く
373  g.DrawRectangle(curPen1, 0, 0, curPict.Width, curPict.Height);
374  // 先に描いた四角に内接する楕円を黒で描く
375  if(curBru2 != null) g.FillEllipse(curBru2, 2, 2, curPict.Width - 4, curPict.Height - 4);
376 
377  if (text != null && text.Length != 0 && text != "0")
378  {
379  // フォントオブジェクトの作成
380  int fntSize = curPict.Width;
381  if(curPict.Height < fntSize) fntSize = curPict.Height;
382  fntSize = (int)((double)fntSize * 0.75);
383  fntSize /= text.Length;
384  if (fntSize < 8) fntSize = 8;
385  Font fnt = new Font("MS UI Gothic", fntSize);
386  Rectangle rect1 = new Rectangle(0, 0, curPict.Width, curPict.Height);
387  StringFormat stringFormat = new StringFormat();
388  stringFormat.Alignment = StringAlignment.Center;
389  stringFormat.LineAlignment = StringAlignment.Center;
390  g.DrawString(text, fnt, curBru3, rect1, stringFormat);
391  //リソースを解放する
392  fnt.Dispose();
393  }
394  // リソースを解放する
395  g.Dispose();
396  // curPictに表示する
397  curPict.Image = canvas;
398  }
399  }
400 
410  public void CurColMsg(string text)
411  {
412  Invoke(new CurColMsgDelegate(CurColMsgLocal), text);
413  }
414 
424  public void CurColMsgLocal(string text)
425  {
426  this.label1.Text = text;
427  }
428 
438  public void CurStsMsg(string text)
439  {
440  Invoke(new CurStsMsgDelegate(CurStsMsgLocal), text);
441  }
442 
452  public void CurStsMsgLocal(string text)
453  {
454  this.label2.Text = text;
455  }
456 
467  private void pictureBox_Click(object sender, EventArgs e)
468  {
469  TableLayoutPanelCellPosition pos = this.tableLayoutPanel1.GetCellPosition((Control)sender);
470 
471  Console.WriteLine("click y=" + pos.Row + " x=" + pos.Column);
472 
473  Task newTask = new Task( () => { this.m_ReversiPlay.reversiPlay(pos.Row, pos.Column); } );
474  newTask.Start();
475  }
476 
487  private void button1_Click(object sender, EventArgs e)
488  {
489  this.appInit();
490  Task newTask = new Task( () => { this.m_ReversiPlay.reset(); } );
491  newTask.Start();
492  }
493 
504  private void button2_Click(object sender, EventArgs e)
505  {
506  System.IO.Directory.CreateDirectory(System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\Y.Y Magic\\ReversiForm");
507  string setPath = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\Y.Y Magic\\ReversiForm\\" + "AppSetting.xml";
508 
509  SettingForm form = new SettingForm(this.m_AppSettings);
510  // *** オーナーウィンドウにthisを指定する *** //
511  form.ShowDialog(this);
512 
513  this.m_AppSettings = form.mSetting;
514  SaveSettingXml(setPath,ref this.m_AppSettings);
515 
516  // *** フォームが必要なくなったところで、Disposeを呼び出す *** //
517  form.Dispose();
518  this.m_ReversiPlay.mSetting = this.m_AppSettings;
519  this.appInit();
520  Task newTask = new Task( () => { this.m_ReversiPlay.reset(); } );
521  newTask.Start();
522  }
523 
534  private void Reversi_Resize(object sender, EventArgs e)
535  {
536  System.Console.WriteLine("Reversi_Resize() : ");
537 
538  // Create a timer with a 0.1 second interval.
539  double interval = 100.0;
540  if(aTimer != null)
541  {
542  // *** タイマーキャンセル *** //
543  aTimer.Enabled = false;
544  }
545  aTimer = new System.Timers.Timer(interval);
546  // Hook up the event handler for the Elapsed event.
547  aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
548  // Only raise the event the first time Interval elapses.
549  aTimer.AutoReset = false;
550  aTimer.Enabled = true;
551  }
552 
563  private void Reversi_ResizeEnd(object sender, EventArgs e)
564  {
565  // *** リサイズ終了後、再描画とレイアウトロジックを実行する *** //
566  System.Console.WriteLine("Reversi_ResizeEnd() : ");
567  this.Invalidate();
568  this.PerformLayout();
569 
570  Size curSize = this.Size;
571  if(oldSize.Width != curSize.Width || oldSize.Height != curSize.Height)
572  {
573  // *** ちらつき防止のためウィンドウサイズ変更されたときのみ再描画 *** //
574  oldSize.Width = curSize.Width;
575  oldSize.Height = curSize.Height;
576  this.appInit();
577  Task newTask = new Task( () => { this.m_ReversiPlay.drawUpdateForcibly(this.m_AppSettings.mAssist); } );
578  newTask.Start();
579  }
580  }
581 
592  private void OnTimedEvent(object source, ElapsedEventArgs e)
593  {
594  Invoke(new Reversi_ResizeEndDelegate(this.Reversi_ResizeEnd), source, e);
595  }
596  }
597 }
HSL (HLS) カラーを表す
Definition: HslColor.cs:28
void DrawSingle(int y, int x, int sts, int bk, string text)
1マス描画
Definition: FormMain.cs:283
const int REVERSI_STS_WHITE
Definition: ReversiConst.cs:82
アプリ設定クラス
Definition: ReversiConst.cs:30
const int REVERSI_STS_BLACK
Definition: ReversiConst.cs:81
void reset()
リセット処理
Definition: ReversiPlay.cs:678
void CurColMsg(string text)
現在の色メッセージ
Definition: FormMain.cs:410
void pictureBox_Click(object sender, EventArgs e)
マスクリック
Definition: FormMain.cs:467
void button2_Click(object sender, EventArgs e)
セッティングクリック
Definition: FormMain.cs:504
ReversiSetting m_AppSettings
アプリ設定
Definition: FormMain.cs:48
override void Dispose(bool disposing)
Clean up any resources being used.
ReversiSetting LoadSettingXml(string path)
設定XMLファイルロード
Definition: FormMain.cs:103
ReversiPlay m_ReversiPlay
リバーシ本体
Definition: FormMain.cs:49
void ViewMsgDlg(string title, string msg)
メッセージダイアログ
Definition: FormMain.cs:250
void ViewMsgDlgLocal(string title, string msg)
メッセージダイアログ
Definition: FormMain.cs:265
void reversiPlay(int y, int x)
リバーシプレイ
Definition: ReversiPlay.cs:167
void CurStsMsg(string text)
現在のステータスメッセージ
Definition: FormMain.cs:438
void CurColMsgLocal(string text)
現在の色メッセージ
Definition: FormMain.cs:424
float S
彩度 (Saturation)
Definition: HslColor.cs:44
リバーシプレイクラス
Definition: ReversiPlay.cs:30
float L
輝度 (Lightness)
Definition: HslColor.cs:53
const int REVERSI_STS_NONE
コマ無し
Definition: ReversiConst.cs:80
static System.Timers.Timer aTimer
タイマー
Definition: FormMain.cs:50
void DrawSingleLocal(int y, int x, int sts, int bk, string text)
1マス描画
Definition: FormMain.cs:301
static Color ToRgb(HslColor hsl)
指定したHslColorからColorを作成する
Definition: HslColor.cs:141
void OnTimedEvent(object source, ElapsedEventArgs e)
タイマーイベント
Definition: FormMain.cs:592
void CurStsMsgLocal(string text)
現在のステータスメッセージ
Definition: FormMain.cs:452
Size oldSize
リサイズ前のサイズ
Definition: FormMain.cs:51
void button1_Click(object sender, EventArgs e)
リセットクリック
Definition: FormMain.cs:487
int SaveSettingXml(string path, ref ReversiSetting appSet)
設定XMLファイルセーブ
Definition: FormMain.cs:137
アプリ設定クラス
void Reversi_Resize(object sender, EventArgs e)
リサイズイベント
Definition: FormMain.cs:534
const int DEF_MASU_CNT_MAX_VAL
マス縦横最大
Definition: ReversiConst.cs:78
void drawUpdateForcibly(int assist)
マス描画強制更新
Definition: ReversiPlay.cs:653
SettingFormクラス
Definition: SettingForm.cs:35
void InitializeComponent()
デザイナー サポートに必要なメソッドです。このメソッドの内容を コード エディターで変更しないでください...
void appInit()
アプリ初期化
Definition: FormMain.cs:169
float H
色相 (Hue)
Definition: HslColor.cs:35
static HslColor FromRgb(Color rgb)
指定したColorからHslColorを作成する
Definition: HslColor.cs:82