PageRenderTime 23ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/StiriVVrsto/Form1.cs

https://gitlab.com/seckmaster/StiriVVrsto
C# | 255 lines | 183 code | 41 blank | 31 comment | 65 complexity | 6585bd32dd86d4bd35283c0c1006cb36 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Diagnostics;
  10. using System.IO;
  11. using System.Threading;
  12. namespace StiriVVrsto
  13. {
  14. public partial class Form1 : Form
  15. {
  16. // igra
  17. Game game;
  18. // za risanje
  19. Bitmap bmp;
  20. Graphics g;
  21. // lokacije slik in imena slik
  22. string location = Environment.GetEnvironmentVariable("TEMP");
  23. string bg = @"\bg_classic.png";
  24. string d1 = @"\disc1_classic.png";
  25. string d2 = @"\disc2_classic.png";
  26. // nizi za prikaz sporočil ob napaki
  27. string dataNotFoundError = "";
  28. string imagesNotFoundError = "";
  29. // sliki za krogce
  30. Image disc1, disc2;
  31. public Form1()
  32. {
  33. InitializeComponent();
  34. // default lokacija cfg datoteke
  35. string cfg = "../../config.cfg";
  36. LoadUIFromConfigFile(cfg);
  37. // ekstrahira teksture iz privzetega argiva
  38. bool success = Configuration.LoadZip("../../classic.zip");
  39. if (success) ReloadImage();
  40. else
  41. {
  42. // če je prišlo do napake pri branju tekstur
  43. MessageBox.Show(dataNotFoundError);
  44. Application.Exit();
  45. }
  46. }
  47. // klik na picturebox
  48. private void pictureBox1_Click(object sender, EventArgs e)
  49. {
  50. if (game != null && !game.game.End)
  51. {
  52. // naslednja poteza
  53. game.NextMove((e as MouseEventArgs).X);
  54. // posodobimo picturebox
  55. pictureBox1.Image = bmp;
  56. }
  57. }
  58. // klik Nova igra
  59. private void button1_Click(object sender, EventArgs e)
  60. {
  61. // ugotovimo kdo naj začne igro glede na radiobuttone
  62. Begins b = 0;
  63. if (radioButton1.Checked) b = Begins.Red;
  64. else if (radioButton2.Checked) b = Begins.Yellow;
  65. else if (radioButton3.Checked) b = Begins.RY;
  66. // če objekt igra še ni bil kreiran
  67. if (game == null)
  68. {
  69. Versus versus = radioButton7.Checked ? Versus.Player : Versus.Ai;
  70. // kreiramo objekt
  71. game = new Game(g, b, versus, disc1, disc2, pictureBox1.Width, pictureBox1.Height);
  72. }
  73. // začni novo igro
  74. game.game.starts = b;
  75. game.game.StartNewGame();
  76. // zbriši picturebox
  77. ReloadImage();
  78. }
  79. // naloži UI iz cfg datoteke
  80. private void LoadUIFromConfigFile(string cfg)
  81. {
  82. try
  83. {
  84. List<config> config = Configuration.LoadCFG(cfg);
  85. foreach (config c in config)
  86. {
  87. if (c.Name.Equals("Form")) this.Text = c.Value;
  88. else if (c.Name.Equals("Button 1")) button1.Text = c.Value;
  89. else if (c.Name.Equals("Button 5")) button5.Text = c.Value;
  90. else if (c.Name.Equals("save")) button2.Text = c.Value;
  91. else if (c.Name.Equals("load")) button3.Text = c.Value;
  92. else if (c.Name.Equals("nextMove")) button4.Text = c.Value;
  93. else if (c.Name.Equals("Radio 1")) radioButton1.Text = c.Value;
  94. else if (c.Name.Equals("Radio 2")) radioButton2.Text = c.Value;
  95. else if (c.Name.Equals("Radio 3")) radioButton3.Text = c.Value;
  96. else if (c.Name.Equals("Radio 7")) radioButton7.Text = c.Value;
  97. else if (c.Name.Equals("Radio 8")) radioButton8.Text = c.Value;
  98. else if (c.Name.Equals("Label 1")) label1.Text = c.Value;
  99. else if (c.Name.Equals("dataError")) dataNotFoundError = c.Value;
  100. else if (c.Name.Equals("imagesError")) imagesNotFoundError = c.Value;
  101. else if (c.Name.Equals("groupBox1")) groupBox1.Text = c.Value;
  102. else if (c.Name.Equals("groupBox2")) groupBox2.Text = c.Value;
  103. else if (c.Name.Equals("groupBox3")) groupBox3.Text = c.Value;
  104. else if (c.Name.Equals("groupBox4")) groupBox4.Text = c.Value;
  105. else if (c.Name.Equals("dialogFilter"))
  106. {
  107. saveFileDialog1.Filter = c.Value.Replace('#', '|');
  108. openFileDialog1.Filter = c.Value.Replace('#', '|');
  109. }
  110. }
  111. }
  112. catch (Exception e)
  113. {
  114. MessageBox.Show("Error loading cfg! Game will close now!");
  115. Application.Exit();
  116. }
  117. }
  118. // ponovno naloži vse slike
  119. private void ReloadImage()
  120. {
  121. try
  122. {
  123. // naložimo slike iz datotek
  124. Image b = Image.FromFile(location + bg);
  125. pictureBox1.Image = b;
  126. disc1 = Image.FromFile(location + d1);
  127. disc2 = Image.FromFile(location + d2);
  128. // če smo v igri, posodobimo slike
  129. if (game != null)
  130. {
  131. game.disc1 = disc1;
  132. game.disc2 = disc2;
  133. }
  134. bmp = new Bitmap(pictureBox1.Image);
  135. g = Graphics.FromImage(bmp);
  136. }
  137. catch (Exception e)
  138. {
  139. MessageBox.Show(imagesNotFoundError);
  140. Application.Exit();
  141. }
  142. }
  143. // shrani igro
  144. private void button2_Click(object sender, EventArgs e)
  145. {
  146. if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  147. {
  148. game.camera.SaveToFile(saveFileDialog1.FileName);
  149. }
  150. }
  151. // naloži igro
  152. private void button3_Click(object sender, EventArgs e)
  153. {
  154. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  155. {
  156. game.camera.LoadFromFile(openFileDialog1.FileName);
  157. game = new Game(g, game.camera.v, Versus.Player, disc1, disc2, pictureBox1.Width, pictureBox1.Height);
  158. ReloadImage();
  159. button4.Enabled = true;
  160. next = 0;
  161. }
  162. }
  163. // naslednja poteza pri prikazovanju igre iz datoteke
  164. int next = 0; // naslednja poteza iz recorderja
  165. private void button4_Click(object sender, EventArgs e)
  166. {
  167. if (next < game.camera.columns.Count)
  168. {
  169. // naredi naslednjo potezo
  170. game.NextMove(game.camera.columns[next++]);
  171. pictureBox1.Image = bmp;
  172. // če je konec posnetka onemogoči gumb
  173. if (next == game.camera.columns.Count)
  174. button4.Enabled = false;
  175. }
  176. }
  177. // sprememba teme
  178. private void button5_Click(object sender, EventArgs e)
  179. {
  180. // katera tema je izbrana
  181. if (radioButton4.Checked)
  182. {
  183. bg = @"\bg_classic.png";
  184. d1 = @"\disc1_classic.png";
  185. d2 = @"\disc2_classic.png";
  186. Configuration.LoadZip("../../classic.zip");
  187. }
  188. else if (radioButton5.Checked)
  189. {
  190. bg = @"\bg_plastic.png";
  191. d1 = @"\disc1_plastic.png";
  192. d2 = @"\disc2_plastic.png";
  193. Configuration.LoadZip("../../plastic.zip");
  194. }
  195. else if (radioButton6.Checked)
  196. {
  197. bg = @"\bg_metal.png";
  198. d1 = @"\disc1_metal.png";
  199. d2 = @"\disc2_metal.png";
  200. Configuration.LoadZip("../../metal.zip");
  201. }
  202. // posodobimo teksture z novimi slikami
  203. ReloadImage();
  204. if (game != null)
  205. {
  206. // resetiramo igro
  207. game.game.StartNewGame();
  208. // zmanjšamo števec, če prejšnja igra ni dokončana
  209. if(!game.game.End)
  210. game.game.GamesPlayed--;
  211. }
  212. }
  213. }
  214. }