Advertisement
Guest User

Untitled

a guest
Jul 5th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 66.48 KB | None | 0 0
  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.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Threading;
  11.  
  12. namespace Client
  13. {
  14. public partial class Form1 : Form
  15. {
  16. // note !-> all the consts' values mean this.width (OR height) / value
  17. private const int ALLIGMENT_FROM_TOP = 20; // the alligment from the top of the screen
  18. private const int ALLIGMENT_FROM_LEFT = 20; // the alligment from the Left of the screen
  19. private const int LOG_IN_FORM_TXT_X_SIZE = 4; // the size of the text boxes in x axis which will be shown on the screen
  20. private const int LOG_IN_FORM_TEXT_SECTOR_HEIGHT = 2; // the height of the username password and email text boxes
  21. private const int LOG_IN_FORM_ALLGIMENT_FROM_TEXT_TO_LABEL = 50; // alligment from label to textbox
  22. private const int LOG_IN_FORM_PICBOX_HEIGHT = 4; // the height of the buttons (width / 4)
  23. private const int SIGN_OUT_FORM_BACK_HEIGHT_WIDTH = 6; // the height and width of the back button
  24. private const int CLOSE_GAME_BUTTON_WIDTH = 7; // the width of the close button
  25. private const int CLOSE_GAME_BUTTON_HEIGHT = 16; // the height of the close button
  26. private const int MAIN_FORM_PICBOX_HEIGHT = 10; // the height of the picture boxes in main form
  27. private const int MAIN_FORM_PICBOX_WIDTH = 5; // the width of the picture boxes in the main form
  28. private const int MAIN_FORM_LOG_OUT_HEIGHT = 12; // the height of the log out button
  29. private const int MAIN_FORM_LOG_OUT_WIDTH = 7; // the width of the log out button
  30. private const int BEST_SCORES_FORM_PB_HEIGHT = 4; // the height of the best scores pictures
  31. private const int BEST_SCORES_FORM_PB_WIDTH = 5; // the width of the best scores picture
  32. private const int JOIN_ROOM_FORM_ROOMS_LB_WIDTH = 4; // the width of the rooms list
  33. private const int JOIN_ROOM_FORM_USERS_LB_WIDTH = 5; // the width of the users list
  34. private const int JOIN_ROOM_FORM_BUTTON_SECTOR_Y = 4; // the height of the button sector
  35. private const int JOIN_ROOM_FORM_REFRESH_HEIGHT = 2; // the height of the refresh button
  36. private const int JOIN_ROOM_FORM_REFRESH_WIDTH = 8; // the width of the refresh button
  37. private const int JOIN_ROOM_FORM_JOIN_HEIGHT = 8; // the height of the join button
  38. private const int JOIN_ROOM_FORM_JOIN_WIDTH = 4; // the width of the join button
  39. private const int IN_ROOM_FORM_BUTTON_SECTOR_Y = 6; // the height of the button sector
  40. private const int IN_ROOM_FORM_BUTTON_HEIGHT = 8; // the height of the button
  41. private const int IN_ROOM_FORM_BUTTON_WIDTH = 3; // the width of the button
  42. private const int IN_ROOM_FORM_LB_WIDTH = 4; // the width of the list box
  43. private const int CREATE_ROOM_FORM_BUTTON_HEIGHT = 10; // the height of create button
  44. private const int CREATE_ROOM_FORM_BUTTON_WIDTH = 3; // the width of the create button
  45.  
  46. //private static int yAxisSpace; // the space remains after the alligment is substracted in y axis
  47. //private static int xAxisSpace; // the space remains after the alligment is substracted in x axis
  48.  
  49. private MyPictureBox PBClose; // the close button, <> needs to be disposed
  50. private Thread t; // Thread to handle messages from the server
  51. private Stream stream; // creates a connection with the server by ip and port
  52. private int alligmentFromSubjectY; // the alligment in y axis from the subject
  53. private string uid; // the username of the signed in user
  54. private string roomName; // the name of the room the user is currently at
  55. private List<string> roomProperties; // the properties of the room the user is currently at
  56. private List<string> roomsID; // all the rooms id which the user can connect them
  57.  
  58. public Form1()
  59. {
  60. InitializeComponent();
  61.  
  62. this.Width = Screen.PrimaryScreen.Bounds.Width;
  63. this.Height = Screen.PrimaryScreen.Bounds.Height;
  64.  
  65. InitStaticVars();
  66. //InitCreateRoom(null, EventArgs.Empty);
  67. //InitInsideRoom(null, EventArgs.Empty);
  68. //ChangeLabels(new List<string> { "4", "5" });
  69. //ChangeLBpart(new List<string> { "tal", "dolev" });
  70. //InitPersonalStatus(null, EventArgs.Empty);
  71. //ChangePersonalStatus(new string[4] { "4", "10", "5", "3" });
  72. InitLoginScreen(null, EventArgs.Empty);
  73. //InitSignUp(null, EventArgs.Empty);
  74. //InitMainScreen(null, EventArgs.Empty);
  75. //InitBestScores(null, EventArgs.Empty);
  76. //KeyValuePair<string, string>[] arr = new KeyValuePair<string, string>[3];
  77. //arr[0] = new KeyValuePair<string, string>("tal", "100");
  78. //arr[1] = new KeyValuePair<string, string>("Dolev", "0");
  79. //arr[2] = new KeyValuePair<string, string>("yarin", "10");
  80. //changeBestScores(arr);
  81. //CleanAllControlers();
  82. }
  83.  
  84. /// <summary>
  85. /// initializes all static variables of custom classes, and current class
  86. /// </summary>
  87. public void InitStaticVars()
  88. {
  89. this.roomsID = new List<string>();
  90. this.roomProperties = new List<string>();
  91.  
  92. this.stream = new Stream("127.0.0.1", 8686);
  93.  
  94. t = new Thread(handleMessage);
  95. t.Start();
  96.  
  97. // MyTxtBox Class:
  98. // the default width of the text box
  99. MyTxtBox._width = (this.Width / LOG_IN_FORM_TXT_X_SIZE);
  100.  
  101. // current class:
  102. // sets the close button
  103. PBClose = new MyPictureBox("PBClose", "close", new Size(this.Width / CLOSE_GAME_BUTTON_WIDTH, this.Height / CLOSE_GAME_BUTTON_HEIGHT));
  104. PBClose.Location = new Point(this.Width / 2 - PBClose.Width / 2, this.Height - PBClose.Height - this.Height / ALLIGMENT_FROM_TOP);
  105. PBClose.Click += PBClose_Click;
  106. this.Controls.Add(PBClose);
  107.  
  108. // sets the alligment from top of screen
  109. alligmentFromSubjectY = this.Height / ALLIGMENT_FROM_TOP;
  110.  
  111. uid = "tal";
  112. }
  113.  
  114. /// <summary>
  115. /// makes the login is screen
  116. /// </summary>
  117. public void InitLoginScreen(object sender, EventArgs args)
  118. {
  119. int reminingY = this.Height;
  120. int nextYPos = 0;
  121. int xOfTextBoxes = 0;
  122. int sizeOfPictureBox = 0;
  123. int alligmentFromPictureBox = 0;
  124. int alligmentFromTextBoxes = 0;
  125. int alligmentFromTextBoxesToLabels = this.Width / LOG_IN_FORM_ALLGIMENT_FROM_TEXT_TO_LABEL;
  126. int xOfPicBoxes = 0;
  127. List<Control> cont = new List<Control>();
  128.  
  129. // sets the background of the page
  130. this.BackgroundImage = Client.Properties.Resources.loginBG;
  131.  
  132. // sets the color of the letters
  133. this.ForeColor = Color.White;
  134.  
  135. // sets the subject label
  136. Label LBLSubject = new MyLabel("Please Log In", "LBLSubject"); // i create a label to know the width of its text
  137. LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
  138. cont.Add(LBLSubject);
  139.  
  140. nextYPos = LBLSubject.Location.Y + alligmentFromSubjectY + LBLSubject.Height; // begining of details section
  141. reminingY -= nextYPos; // sets the space of details section
  142.  
  143. // sets the text box of username
  144. TextBox TXTuserName = new MyTxtBox("TXTusername"); // i create a label to know the height of its font
  145. alligmentFromTextBoxes = ((reminingY / LOG_IN_FORM_TEXT_SECTOR_HEIGHT) - (TXTuserName.Height * 2)) / 3; // generates the needed alligment
  146. nextYPos += alligmentFromTextBoxes;
  147. reminingY -= reminingY / LOG_IN_FORM_TEXT_SECTOR_HEIGHT; // sets the space of buttons section
  148.  
  149. TXTuserName.Location = new Point((this.Width / 2) - (TXTuserName.Width / 2), nextYPos);
  150. cont.Add(TXTuserName);
  151.  
  152. // sets the label of username
  153. Label LBLusername = new MyLabel("UserName: ", "LBLusername"); // sets the label of the userName
  154. LBLusername.Location = new Point(TXTuserName.Location.X - LBLusername.Width - alligmentFromTextBoxesToLabels, nextYPos);
  155. cont.Add(LBLusername);
  156.  
  157. nextYPos += (TXTuserName.Height + alligmentFromTextBoxes); // sets the position of the next textBox
  158. xOfTextBoxes = TXTuserName.Location.X;
  159.  
  160. // sets the text box of password
  161. TextBox TXTpsw = new MyTxtBox(new Point(xOfTextBoxes, nextYPos), "TXTpsw");
  162. cont.Add(TXTpsw);
  163.  
  164. // sets the label of password
  165. Label LBLpsw = new MyLabel("password: ", "LBLpsw"); // sets the password label
  166. LBLpsw.Location = new Point(xOfTextBoxes - LBLpsw.Width - alligmentFromTextBoxesToLabels, nextYPos);
  167. cont.Add(LBLpsw);
  168.  
  169. nextYPos += (TXTuserName.Height + alligmentFromTextBoxes); // sets the begining of the buttons sector
  170.  
  171. // sets the buttons section
  172. sizeOfPictureBox = reminingY / LOG_IN_FORM_PICBOX_HEIGHT;
  173. reminingY -= (sizeOfPictureBox * 2);
  174. alligmentFromPictureBox = reminingY / 2;
  175.  
  176. // sets the sign in button
  177. PictureBox PBSignIn = new MyPictureBox("PBSignIn", "SignIn", new Size(TXTuserName.Width, sizeOfPictureBox));
  178. xOfPicBoxes = (this.Width / 2) - (PBSignIn.Width / 2); // sets the x of picture boxes
  179. PBSignIn.Location = new Point(xOfPicBoxes, nextYPos);
  180. PBSignIn.Click += (EventHandler)PBSign_In_Click;
  181. cont.Add(PBSignIn);
  182.  
  183. nextYPos += alligmentFromPictureBox;
  184.  
  185. // sets the signup button
  186. PictureBox PBSignUp = new MyPictureBox("PBSignUp", "SignUp", new Size(PBSignIn.Width, sizeOfPictureBox));
  187. PBSignUp.Location = new Point(xOfPicBoxes, nextYPos);
  188. PBSignUp.Click += (EventHandler)(CleanAllControlers);
  189. PBSignUp.Click += (EventHandler)InitSignUp;
  190. cont.Add(PBSignUp);
  191.  
  192. this.Controls.AddRange(cont.ToArray());
  193. }
  194.  
  195. /// <summary>
  196. /// sets the form to be sign up page
  197. /// </summary>
  198. public void InitSignUp(object sender, EventArgs args)
  199. {
  200. int reminingY = this.Height;
  201. int nextYPos = 0;
  202. int xOfTextBoxes = 0;
  203. int sizeOfPictureBox = 0;
  204. int alligmentFromPictureBox = 0;
  205. int alligmentFromTextBoxes = 0;
  206. int alligmentFromTextBoxesToLabels = this.Width / LOG_IN_FORM_ALLGIMENT_FROM_TEXT_TO_LABEL;
  207. int xOfPicBoxes = 0;
  208. List<Control> cont = new List<Control>();
  209.  
  210. // sets the background
  211. this.BackgroundImage = Properties.Resources.signUpBG;
  212.  
  213. // sets the color of the letters
  214. this.ForeColor = Color.Black;
  215.  
  216. // sets the subject label
  217. Label LBLSubject = new MyLabel("Please Sign Up", "LBLSubject"); // i create a label to know the width of its text
  218. LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
  219. cont.Add(LBLSubject);
  220.  
  221. nextYPos = LBLSubject.Location.Y + alligmentFromSubjectY + LBLSubject.Height; // begining of details section
  222. reminingY -= nextYPos; // sets the space of details section
  223.  
  224. // sets the text box of username
  225. TextBox TXTuserName = new MyTxtBox("TXTusername"); // i create a label to know the height of its font
  226. alligmentFromTextBoxes = ((reminingY / LOG_IN_FORM_TEXT_SECTOR_HEIGHT) - (TXTuserName.Height * 3)) / 4; // generates the needed alligment
  227. nextYPos += alligmentFromTextBoxes;
  228. reminingY -= reminingY / LOG_IN_FORM_TEXT_SECTOR_HEIGHT; // sets the space of buttons section
  229.  
  230. TXTuserName.Location = new Point((this.Width / 2) - (TXTuserName.Width / 2), nextYPos);
  231. cont.Add(TXTuserName);
  232.  
  233. // sets the label of username
  234. Label LBLusername = new MyLabel("UserName: ", "LBLusername"); // sets the label of the userName
  235. LBLusername.Location = new Point(TXTuserName.Location.X - LBLusername.Width - alligmentFromTextBoxesToLabels, nextYPos);
  236. cont.Add(LBLusername);
  237.  
  238. nextYPos += (TXTuserName.Height + alligmentFromTextBoxes); // sets the position of the next textBox
  239. xOfTextBoxes = TXTuserName.Location.X;
  240.  
  241. // sets the text box of password
  242. TextBox TXTpsw = new MyTxtBox(new Point(xOfTextBoxes, nextYPos), "TXTpsw");
  243. cont.Add(TXTpsw);
  244.  
  245. // sets the label of password
  246. Label LBLpsw = new MyLabel("password: ", "LBLpsw"); // sets the password label
  247. LBLpsw.Location = new Point(xOfTextBoxes - LBLpsw.Width - alligmentFromTextBoxesToLabels, nextYPos);
  248. cont.Add(LBLpsw);
  249.  
  250. nextYPos += (TXTuserName.Height + alligmentFromTextBoxes); // sets the position of the next textBox
  251.  
  252. // sets the text box of email
  253. TextBox TXTemail = new MyTxtBox(new Point(xOfTextBoxes, nextYPos), "TXTemail");
  254. cont.Add(TXTemail);
  255.  
  256. // sets the label of email
  257. Label LBLemail = new MyLabel("email: ", "LBLemail");
  258. LBLemail.Location = new Point(xOfTextBoxes - LBLemail.Width - alligmentFromTextBoxesToLabels, nextYPos);
  259. cont.Add(LBLemail);
  260.  
  261. nextYPos += (TXTuserName.Height + alligmentFromTextBoxes); // sets the begining of the buttons sector
  262.  
  263. // sets the buttons section
  264. sizeOfPictureBox = reminingY / LOG_IN_FORM_PICBOX_HEIGHT;
  265. reminingY -= (sizeOfPictureBox * 2);
  266. alligmentFromPictureBox = reminingY / 2;
  267.  
  268. // sets the sign up button
  269. PictureBox PBSignUp = new MyPictureBox("PBSignUp", "SignUp", new Size(TXTemail.Width, sizeOfPictureBox));
  270. xOfPicBoxes = (this.Width / 2) - (PBSignUp.Width / 2); // sets the x of picture boxes
  271. PBSignUp.Location = new Point(xOfPicBoxes, nextYPos);
  272. PBSignUp.Click += (EventHandler)PBSign_Up_Click;
  273. //PBSignUp.Click += (EventHandler)CleanAllControlers;
  274. cont.Add(PBSignUp);
  275.  
  276. // sets the back button
  277. int PCHeight = this.Height / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  278. int PCWidth = this.Width / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  279. PictureBox PBBack = new MyPictureBox("PBBack", "back", new Size(PCWidth, PCHeight));
  280. PBBack.Location = new Point(this.Width - PCWidth, this.Height / ALLIGMENT_FROM_TOP);
  281. PBBack.Click += (EventHandler)CleanAllControlers;
  282. PBBack.Click += (EventHandler)InitLoginScreen;
  283. cont.Add(PBBack);
  284.  
  285. this.Controls.AddRange(cont.ToArray());
  286. }
  287.  
  288. /// <summary>
  289. /// builds the main screen
  290. /// </summary>
  291. /// <param name="sender"></param>
  292. /// <param name="args"></param>
  293. public void InitMainScreen(object sender, EventArgs args)
  294. {
  295. int nextYPos = 0;
  296. int reminingY = PBClose.Location.Y;
  297. int heightOfPB = this.Height / MAIN_FORM_PICBOX_HEIGHT;
  298. int widthOfPB = this.Width / MAIN_FORM_PICBOX_WIDTH;
  299. int alligmentFromButtonsY = 0;
  300. int alligmentFromButtonsX = 0;
  301.  
  302. // sets the background of the screen
  303. this.BackgroundImage = Properties.Resources.mainScreenBG;
  304.  
  305. // sets the font color to black
  306. this.ForeColor = Color.Black;
  307.  
  308. // sets the subject label
  309. Label LBLSubject = new MyLabel("Hello " + uid, "LBLSubject"); // i create a label to know the width of its text
  310. LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
  311. this.Controls.Add(LBLSubject);
  312.  
  313. // configures all the propeties needed for the button
  314. nextYPos = LBLSubject.Location.Y + alligmentFromSubjectY;
  315. reminingY -= (nextYPos - heightOfPB * 2);
  316. alligmentFromButtonsY = reminingY / 3;
  317. nextYPos += (alligmentFromButtonsY / 2);
  318. alligmentFromButtonsX = (this.Width - widthOfPB * 3) / 3;
  319.  
  320. // sets the Join room button
  321. Button BTNJoin = new MyButton("BTNJoin", new Size(widthOfPB, heightOfPB), "Join Room");
  322. BTNJoin.Location = new Point(alligmentFromButtonsX, nextYPos);
  323. BTNJoin.Click += (EventHandler)CleanAllControlers;
  324. BTNJoin.Click += (EventHandler)InitJoinRoom;
  325. BTNJoin.Click += (EventHandler)BTNJoin_Click;
  326. this.Controls.Add(BTNJoin);
  327.  
  328. // sets the Create room button
  329. Button BTNCreate = new MyButton("BTNCreate", new Size(widthOfPB, heightOfPB), "Create Room");
  330. BTNCreate.Location = new Point(this.Width - widthOfPB - alligmentFromButtonsX, nextYPos);
  331. BTNCreate.Click += (EventHandler)CleanAllControlers;
  332. BTNCreate.Click += (EventHandler)InitCreateRoom;
  333. this.Controls.Add(BTNCreate);
  334.  
  335. nextYPos += alligmentFromButtonsY;
  336.  
  337. // sets the best scores button
  338. Button BTNBest = new MyButton("BTNBest", new Size(widthOfPB, heightOfPB), "Best Scores");
  339. BTNBest.Location = new Point(alligmentFromButtonsX, nextYPos);
  340. BTNBest.Click += (EventHandler)CleanAllControlers;
  341. BTNBest.Click += (EventHandler)InitBestScores;
  342. BTNBest.Click += (EventHandler)BTNBest_Click;
  343. this.Controls.Add(BTNBest);
  344.  
  345. // sets the personal status
  346. Button BTNPersonal = new MyButton("BTNPersonal", new Size(widthOfPB, heightOfPB), "My Status");
  347. BTNPersonal.Location = new Point(this.Width - widthOfPB - alligmentFromButtonsX, nextYPos);
  348. BTNPersonal.Click += (EventHandler)CleanAllControlers;
  349. BTNPersonal.Click += (EventHandler)InitPersonalStatus;
  350. BTNPersonal.Click += (EventHandler)BTNPersonal_Click;
  351.  
  352. this.Controls.Add(BTNPersonal);
  353.  
  354. // sets the logout button
  355. PictureBox PBLogOut = new MyPictureBox("PBLogOut", "signOut", new Size(this.Height / MAIN_FORM_LOG_OUT_WIDTH, this.Width / MAIN_FORM_LOG_OUT_HEIGHT));
  356. PBLogOut.Location = new Point(this.Width / ALLIGMENT_FROM_LEFT, this.Height / ALLIGMENT_FROM_TOP);
  357. PBLogOut.Click += (EventHandler)CleanAllControlers;
  358. PBLogOut.Click += (EventHandler)InitLoginScreen;
  359. PBLogOut.Click += (EventHandler)PBLogOut_Click;
  360. this.Controls.Add(PBLogOut);
  361. }
  362.  
  363. /// <summary>
  364. /// inits the join room screen
  365. /// </summary>
  366. /// <param name="sender"></param>
  367. /// <param name="args"></param>
  368. private void InitJoinRoom(object sender, EventArgs args)
  369. {
  370. int nextYPos = 0;
  371. int alligmentFromClose = this.Height - this.PBClose.Location.Y - this.PBClose.Height;
  372. int alligmentFromButtonsY = 0;
  373. int alligmentFromButtonsX = 0;
  374. int buttonSectorY = 0;
  375. int alligmentFromListsX = 0;
  376. int refreshY = 0;
  377. int joinY = 0;
  378. int reminingY = this.PBClose.Location.Y;
  379.  
  380. // sets the form to join room bg
  381. this.BackgroundImage = Properties.Resources.joinRoomBG;
  382.  
  383. // changes the fore color to black
  384. this.ForeColor = Color.Black;
  385.  
  386. // sets the subject label
  387. Label LBLSubject = new MyLabel("Choose Room", "LBLSubject"); // i create a label to know the width of its text
  388. LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
  389. this.Controls.Add(LBLSubject);
  390.  
  391. nextYPos = LBLSubject.Location.Y + LBLSubject.Height + alligmentFromSubjectY;
  392. reminingY -= nextYPos;
  393.  
  394. // configures the button sector properties
  395. buttonSectorY = reminingY / JOIN_ROOM_FORM_BUTTON_SECTOR_Y;
  396. refreshY = buttonSectorY / JOIN_ROOM_FORM_REFRESH_HEIGHT;
  397. joinY = buttonSectorY / JOIN_ROOM_FORM_JOIN_HEIGHT;
  398. alligmentFromButtonsY = (buttonSectorY - refreshY - joinY) / 2;
  399. alligmentFromButtonsX = (this.Width - this.Width / JOIN_ROOM_FORM_REFRESH_WIDTH - this.Width / JOIN_ROOM_FORM_JOIN_WIDTH) / 3;
  400.  
  401. // configures and adds the refresh button
  402. PictureBox PBRefresh = new MyPictureBox("PBRefresh", "refresh", new Size(this.Width / JOIN_ROOM_FORM_REFRESH_WIDTH, refreshY));
  403. PBRefresh.Location = new Point(alligmentFromButtonsX, PBClose.Location.Y - alligmentFromButtonsY - refreshY);
  404. PBRefresh.Click += (EventHandler)PBRefresh_Click;
  405. this.Controls.Add(PBRefresh);
  406.  
  407. // configures and adds the join button
  408. PictureBox PBJoin = new MyPictureBox("PBJoin", "joinBTN", new Size(this.Width / JOIN_ROOM_FORM_JOIN_WIDTH, this.Height / JOIN_ROOM_FORM_JOIN_HEIGHT));
  409. PBJoin.Location = new Point(this.Width - PBJoin.Width - alligmentFromButtonsX, PBRefresh.Location.Y);
  410. PBJoin.Click += (EventHandler)PBJoin_Click;
  411. //PBJoin.Click += (EventHandler)CleanAllControlers;
  412. //PBJoin.Click += (EventHandler)InitInsideRoom;
  413. this.Controls.Add(PBJoin);
  414.  
  415. reminingY = PBRefresh.Location.Y - LBLSubject.Location.Y - LBLSubject.Height - alligmentFromSubjectY;
  416. ListBox LBRooms = new MyListBox("LBRooms", new Size(this.Width / JOIN_ROOM_FORM_ROOMS_LB_WIDTH, reminingY / 4));
  417. ListBox LBUsers = new MyListBox("LBUsers", new Size(this.Width / JOIN_ROOM_FORM_USERS_LB_WIDTH, reminingY / 4));
  418.  
  419. alligmentFromListsX = (this.Width - LBRooms.Width - LBUsers.Width) / 3;
  420.  
  421. Label LBLRooms = new MyLabel("Rooms:", "LBLRooms");
  422. Label LBLUsers = new MyLabel("Users:", "LBLUsers");
  423.  
  424. // adds the rooms list box
  425. LBRooms.Location = new Point(this.Width / 2 - LBRooms.Width / 2, nextYPos + LBLRooms.Height);
  426. LBRooms.SelectedIndexChanged += (EventHandler)LBRooms_SelectedIndexChanged;
  427. this.Controls.Add(LBRooms);
  428.  
  429. // sets the users list box
  430. LBUsers.Location = new Point(alligmentFromListsX, nextYPos + LBLUsers.Height);
  431. LBUsers.Visible = false;
  432. this.Controls.Add((LBUsers));
  433.  
  434. // configures and adds the label of rooms
  435. LBLRooms.Location = new Point(LBRooms.Location.X + LBRooms.Width / 2 - LBLRooms.Width / 2, nextYPos);
  436. this.Controls.Add(LBLRooms);
  437.  
  438. // configures and adds the label of users
  439. LBLUsers.Location = new Point(LBUsers.Location.X + LBUsers.Width / 2 - LBLRooms.Width / 2, nextYPos);
  440. LBLUsers.Visible = false;
  441. this.Controls.Add(LBLUsers);
  442.  
  443. // sets the back button
  444. int PCHeight = this.Height / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  445. int PCWidth = this.Width / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  446. PictureBox PBBack = new MyPictureBox("PBBack", "back", new Size(PCWidth, PCHeight));
  447. PBBack.Location = new Point(this.Width - PCWidth, this.Height / ALLIGMENT_FROM_TOP);
  448. PBBack.Click += (EventHandler)CleanAllControlers;
  449. PBBack.Click += (EventHandler)InitMainScreen;
  450. this.Controls.Add(PBBack);
  451. }
  452.  
  453. /// <summary>
  454. /// changes the room list in the join room form
  455. /// should be called when the join room button or refresh button were clicked
  456. /// </summary>
  457. /// <param name="roomsNames">the names of the rooms that will be shown on the list box</param>
  458. private void changeRoomsList(List<string> roomsNames)
  459. {
  460. if (roomsNames.Count < 1) // so that the list will still be shown even if there are no available rooms
  461. {
  462. roomsNames.Add("");
  463. roomsID.Add("-1"); // so the user cant join this fake room
  464. }
  465.  
  466. ListBox LBUsers = this.Controls.Find("LBUsers", false).FirstOrDefault() as ListBox;
  467. Label LBLUsers = this.Controls.Find("LBLUsers", false).FirstOrDefault() as Label;
  468.  
  469. // makes the users lists to disapear
  470. LBUsers.Visible = false;
  471. LBLUsers.Visible = false;
  472.  
  473. ListBox LBRooms = this.Controls.Find("LBRooms", false).FirstOrDefault() as ListBox;
  474. if (LBRooms != null)
  475. {
  476. LBRooms.Items.Clear();
  477. foreach (string name in roomsNames)
  478. {
  479. LBRooms.Items.Add(name);
  480. }
  481.  
  482. Label LBLRooms = this.Controls.Find("LBLRooms", false).FirstOrDefault() as Label;
  483. PictureBox PBRefresh = this.Controls.Find("PBRefresh", false).FirstOrDefault() as PictureBox;
  484.  
  485. // sets the height of the list box
  486. if (LBLRooms != null && PBRefresh != null)
  487. {
  488. int maxHeight = PBRefresh.Location.Y - LBLRooms.Location.Y - LBLRooms.Height - alligmentFromSubjectY;
  489. int height = LBRooms.ItemHeight;
  490. int maxObj = maxHeight / height;
  491.  
  492. LBRooms.Height = roomsNames.Count > maxObj ? height * maxObj : roomsNames.Count * height;
  493. }
  494.  
  495. // moves the list box to its proper place
  496. LBRooms.Location = new Point(this.Width / 2 - LBRooms.Width / 2, LBRooms.Location.Y);
  497. LBLRooms.Location = new Point(LBRooms.Location.X + LBRooms.Width / 2 - LBLRooms.Width / 2, LBLRooms.Location.Y);
  498.  
  499. this.Update();
  500. }
  501. }
  502.  
  503. /// <summary>
  504. /// changes the users in room list box
  505. /// should be called when an item on the rooms list is clicked
  506. /// </summary>
  507. /// <param name="users">the users that will be presented on the users list</param>
  508. private void changeUsersInRoom(List<string> users)
  509. {
  510. if (users.Count < 1)
  511. {
  512. users.Add("");
  513. }
  514.  
  515. ListBox LBUsers = this.Controls.Find("LBUsers", false).FirstOrDefault() as ListBox;
  516. Label LBLUsers = this.Controls.Find("LBLUsers", false).FirstOrDefault() as Label;
  517. Label LBLRooms = this.Controls.Find("LBLRooms", false).FirstOrDefault() as Label;
  518. PictureBox PBRefresh = this.Controls.Find("PBRefresh", false).FirstOrDefault() as PictureBox;
  519. ListBox LBRooms = this.Controls.Find("LBRooms", false).FirstOrDefault() as ListBox;
  520.  
  521. // sets the height of the list box
  522. if (LBLUsers != null && PBRefresh != null && LBUsers != null)
  523. {
  524. int maxHeight = PBRefresh.Location.Y - LBLUsers.Location.Y - LBLUsers.Height - alligmentFromSubjectY;
  525. int height = LBUsers.ItemHeight;
  526. int maxObj = maxHeight / height;
  527.  
  528. // changes the items in the list box
  529. LBUsers.Items.Clear();
  530. foreach (string user in users)
  531. {
  532. LBUsers.Items.Add(user);
  533. }
  534.  
  535. LBUsers.Height = users.Count > maxObj ? height * maxObj : users.Count * height;
  536.  
  537. // sets the rooms stuff to their proper place
  538. LBRooms.Location = new Point(this.Width - LBRooms.Width - LBUsers.Location.X, LBRooms.Location.Y);
  539. LBLRooms.Location = new Point(LBRooms.Location.X + LBRooms.Width / 2 - LBLRooms.Width / 2, LBLRooms.Location.Y);
  540.  
  541. LBUsers.Visible = true;
  542. LBLUsers.Visible = true;
  543. }
  544. }
  545.  
  546. private void InitCreateRoom(object sender, EventArgs args)
  547. {
  548. int alligmentFromCloseButton = this.Height - this.PBClose.Location.Y - this.PBClose.Height;
  549. int nextYPos = 0;
  550. int buttonHeight = this.Height / CREATE_ROOM_FORM_BUTTON_HEIGHT;
  551. int buttonWidth = this.Width / CREATE_ROOM_FORM_BUTTON_WIDTH;
  552. int reminingY = 0;
  553. int alligmentFromTextBoxes = 0;
  554.  
  555. this.BackgroundImage = Properties.Resources.createRoom;
  556. this.ForeColor = Color.Black;
  557.  
  558. // sets the subject label
  559. Label LBLSubject = new MyLabel("Create New Room:", "LBLSubject"); // i create a label to know the width of its text
  560. LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
  561. this.Controls.Add(LBLSubject);
  562.  
  563. // configures and adds the create room button
  564. Button BTNCreateRoom = new MyButton("BTNCreateRoom", new Size(buttonWidth, buttonHeight), "Create");
  565. BTNCreateRoom.Location = new Point(this.Width / 2 - buttonWidth / 2, this.PBClose.Location.Y - alligmentFromCloseButton - buttonHeight);
  566. BTNCreateRoom.Click += (EventHandler)BTNCreateRoom_Click;
  567. this.Controls.Add(BTNCreateRoom);
  568.  
  569. // sets all the required properties for textbox sector
  570. nextYPos = LBLSubject.Location.Y + alligmentFromSubjectY + LBLSubject.Height;
  571. reminingY = BTNCreateRoom.Location.Y - nextYPos;
  572. TextBox TXTRoomName = new MyTxtBox("TXTRoomName"); // i create a text box so i can know its height and width
  573. alligmentFromTextBoxes = (reminingY - TXTRoomName.Height * 4) / 4 + TXTRoomName.Height;
  574.  
  575. // configures and adds the room name textbox
  576. TXTRoomName.Location = new Point(this.Width / 2 - TXTRoomName.Width / 2, nextYPos);
  577. this.Controls.Add(TXTRoomName);
  578.  
  579. // configures and adds the room name label
  580. Label LBLRoomName = new MyLabel("Room Name: ", "LBLRoomName");
  581. LBLRoomName.Location = new Point(TXTRoomName.Location.X - LBLRoomName.Width, nextYPos);
  582. this.Controls.Add(LBLRoomName);
  583.  
  584. nextYPos += alligmentFromTextBoxes;
  585.  
  586. // configures and adds the number of players text box
  587. TextBox TXTNumOfPlayers = new MyTxtBox(new Point(TXTRoomName.Location.X, nextYPos), "TXTNumOfPlayers");
  588. this.Controls.Add(TXTNumOfPlayers);
  589.  
  590. // configures and adds the number of players label
  591. Label LBLNumOfPlayers = new MyLabel("Number of players: ", "LBLNumOfPlayers");
  592. LBLNumOfPlayers.Location = new Point(TXTNumOfPlayers.Location.X - LBLNumOfPlayers.Width, nextYPos);
  593. this.Controls.Add(LBLNumOfPlayers);
  594.  
  595. nextYPos += alligmentFromTextBoxes;
  596.  
  597. // configures and adds the number of questions text box
  598. TextBox TXTNumOfQuestions = new MyTxtBox(new Point(TXTRoomName.Location.X, nextYPos), "TXTNumOfQuestions");
  599. this.Controls.Add(TXTNumOfQuestions);
  600.  
  601. // configures and adds the number of question label
  602. Label LBLNumOfQuestions = new MyLabel("Number of questions: ", "LBLNumOfQuestions");
  603. LBLNumOfQuestions.Location = new Point(TXTNumOfQuestions.Location.X - LBLNumOfQuestions.Width, nextYPos);
  604. this.Controls.Add(LBLNumOfQuestions);
  605.  
  606. nextYPos += alligmentFromTextBoxes;
  607.  
  608. // configures and adds the number of questions text box
  609. TextBox TXTTimePerAns = new MyTxtBox(new Point(TXTRoomName.Location.X, nextYPos), "TXTTimePerAns");
  610. this.Controls.Add(TXTTimePerAns);
  611.  
  612. // configures and adds the time per answer label
  613. Label LBLTimePerAns = new MyLabel("Time Per answer: ", "LBLTimePerAns");
  614. LBLTimePerAns.Location = new Point(TXTTimePerAns.Location.X - LBLTimePerAns.Width, nextYPos);
  615. this.Controls.Add(LBLTimePerAns);
  616.  
  617. // sets the back button
  618. int PCHeight = this.Height / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  619. int PCWidth = this.Width / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  620. PictureBox PBBack = new MyPictureBox("PBBack", "back", new Size(PCWidth, PCHeight));
  621. PBBack.Location = new Point(this.Width - PCWidth, this.Height / ALLIGMENT_FROM_TOP);
  622. PBBack.Click += (EventHandler)CleanAllControlers;
  623. PBBack.Click += (EventHandler)InitMainScreen;
  624. this.Controls.Add(PBBack);
  625. }
  626.  
  627. /// <summary>
  628. /// changes the form to inside room performance
  629. /// </summary>
  630. /// <param name="sender"></param>
  631. /// <param name="args"></param>
  632. private void InitInsideRoom(object sender, EventArgs args)
  633. {
  634. int reminingY = this.Height;
  635. int nextYPos = 0;
  636. int buttonSectorY = 0;
  637. int buttonsHeight = this.Height / IN_ROOM_FORM_BUTTON_HEIGHT;
  638. int buttonWidth = this.Width / IN_ROOM_FORM_BUTTON_WIDTH;
  639. int alligmentFromButtonsY = 0;
  640. int alligmentFromLabelsY = 0;
  641. List<Label> labels = new List<Label>();
  642.  
  643. // sets the background to be inside room background
  644. this.BackgroundImage = Properties.Resources.inRoom;
  645.  
  646. // sets the font color to black
  647. this.ForeColor = Color.Black;
  648.  
  649. this.PBClose.Visible = false;
  650.  
  651. // sets the subject label
  652. Label LBLSubject = new MyLabel("You are in " + this.roomName + " room", "LBLSubject"); // i create a label to know the width of its text
  653. LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
  654. this.Controls.Add(LBLSubject);
  655.  
  656. nextYPos = LBLSubject.Location.Y + LBLSubject.Height + alligmentFromSubjectY;
  657. reminingY -= nextYPos;
  658. buttonSectorY = reminingY / IN_ROOM_FORM_BUTTON_SECTOR_Y;
  659. reminingY -= buttonSectorY;
  660. alligmentFromButtonsY = (buttonSectorY - buttonsHeight) / 2;
  661.  
  662. if (sender is PictureBox) // in case its a user presentation
  663. {
  664. Button BTNLeave = new MyButton("BTNLeave", new Size(buttonWidth, buttonsHeight), "Leave Room");
  665. BTNLeave.Location = new Point(this.Width / 2 - buttonWidth / 2, this.Height - buttonsHeight - alligmentFromButtonsY);
  666. BTNLeave.Click += (EventHandler)BTNLeave_Click;
  667. this.Controls.Add(BTNLeave);
  668. }
  669. else if (sender is Button) // in case its admin presentation
  670. {
  671. int alligmentFromButtonsX = (this.Width - buttonWidth * 2) / 3;
  672.  
  673. // configures and adds the close room button
  674. Button BTNCloseRoom = new MyButton("BTNCloseRoom", new Size(buttonWidth, buttonsHeight), "Close Room");
  675. BTNCloseRoom.Location = new Point(alligmentFromButtonsX, this.Height - BTNCloseRoom.Height - alligmentFromButtonsY);
  676. BTNCloseRoom.Click += (EventHandler)BTNCloseRoom_Click;
  677. this.Controls.Add(BTNCloseRoom);
  678.  
  679. // configures and adds the start game button
  680. Button BTNStartGame = new MyButton("BTNStartGame", new Size(buttonWidth, buttonsHeight), "Start Game");
  681. BTNStartGame.Location = new Point(this.Width - buttonWidth - alligmentFromButtonsX, BTNCloseRoom.Location.Y);
  682. BTNStartGame.Click += (EventHandler)BTNStartGame_Click;
  683. this.Controls.Add(BTNStartGame);
  684.  
  685. labels.Add(new MyLabel("Max number of users: ", "LBLMaxUsers"));
  686. }
  687.  
  688. Label LBLparticipates = new MyLabel("current participates are: ", "LBLparticipates");
  689.  
  690. // configures and adds the participates list box
  691. ListBox LBpart = new MyListBox("LBpart", new Size(this.Width / IN_ROOM_FORM_LB_WIDTH, reminingY / 4));
  692. LBpart.Location = new Point(this.Width / 2 - LBpart.Width / 2, nextYPos + LBLparticipates.Height);
  693. this.Controls.Add(LBpart);
  694.  
  695. // configures and adds the label of participates
  696. LBLparticipates.Location = new Point(LBpart.Location.X + LBpart.Width / 2 - LBLparticipates.Width / 2, nextYPos);
  697. this.Controls.Add(LBLparticipates);
  698.  
  699. // sets the room properties labels
  700. labels.Add(new MyLabel("Number of questions: ", "LBLNumOfQuestions"));
  701. labels.Add(new MyLabel("Time per question: ", "LBLTimePerQuestion"));
  702.  
  703. // calculates the needed alligment
  704. alligmentFromLabelsY = (reminingY - labels[0].Height * labels.Count) / (labels.Count + 1);
  705. nextYPos += alligmentFromLabelsY;
  706.  
  707. foreach (Label l in labels)
  708. {
  709. l.Location = new Point(0, nextYPos);
  710. nextYPos += alligmentFromLabelsY;
  711. this.Controls.Add(l);
  712. }
  713. }
  714.  
  715. /// <summary>
  716. /// changes the list box of participates
  717. /// </summary>
  718. /// <param name="users">the users to be added to the list box</param>
  719. private void ChangeLBpart(List<string> users)
  720. {
  721. Label LBLparticipates = this.Controls.Find("LBLparticipates", false).FirstOrDefault() as Label;
  722. ListBox LBpart = this.Controls.Find("LBpart", false).FirstOrDefault() as ListBox;
  723.  
  724. if (users.Count < 1)
  725. {
  726. users.Add("");
  727. }
  728.  
  729. if (LBLparticipates != null && LBpart != null)
  730. {
  731. int maxHeight = this.Height - this.Height / IN_ROOM_FORM_BUTTON_SECTOR_Y - LBLparticipates.Location.Y - LBLparticipates.Height;
  732. int height = LBpart.ItemHeight;
  733. int maxObj = maxHeight / height;
  734.  
  735. // changes the height of the list box according to the items in it
  736. LBpart.Height = users.Count > maxObj ? height * maxObj : height * users.Count;
  737.  
  738. LBpart.Items.Clear();
  739. foreach (string user in users)
  740. {
  741. LBpart.Items.Add(user);
  742. }
  743. }
  744. }
  745.  
  746. /// <summary>
  747. /// changes the labels of in room properties
  748. /// in case of user presentation the properties param should be made out of the string the server sent
  749. /// in case of admin presentation the properties param should be the global parameter roomProperties
  750. /// </summary>
  751. /// <param name="properties">the properties that would be in the labels, the order should be maxusers(if exists), numberOfQuestions, time per questions</param>
  752. private void ChangeLabels(List<string> properties)
  753. {
  754. Label[] labels = new Label[properties.Count];
  755.  
  756. if (properties.Count > 2)
  757. {
  758. labels[0] = this.Controls.Find("LBLMaxUsers", false).FirstOrDefault() as Label;
  759. }
  760.  
  761. labels[labels.Length - 2] = this.Controls.Find("LBLNumOfQuestions", false).FirstOrDefault() as Label;
  762. labels[labels.Length - 1] = this.Controls.Find("LBLTimePerQuestion", false).FirstOrDefault() as Label;
  763.  
  764. for (int i = 0; i < labels.Length; i++)
  765. {
  766. if (labels[i] != null)
  767. {
  768. labels[i].Text += (int.Parse(properties[i]).ToString());
  769. labels[i].Width = TextRenderer.MeasureText(labels[i].Text, labels[i].Font).Width;
  770. }
  771. }
  772. }
  773.  
  774. /// <summary>
  775. /// changes the form to personal status
  776. /// </summary>
  777. /// <param name="sender"></param>
  778. /// <param name="args"></param>
  779. private void InitPersonalStatus(object sender, EventArgs args)
  780. {
  781. int reminingY = this.PBClose.Location.Y;
  782. int nextYPos = 0;
  783. int alligmentFromLabelsY = 0;
  784.  
  785. // sets the background of the form to personal status background
  786. this.BackgroundImage = Properties.Resources.personalStatusBG;
  787.  
  788. // sets the font of the form to black
  789. this.ForeColor = Color.Black;
  790.  
  791. // sets the subject label
  792. Label LBLSubject = new MyLabel("My Performance: ", "LBLSubject"); // i create a label to know the width of its text
  793. LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
  794. this.Controls.Add(LBLSubject);
  795.  
  796. nextYPos = LBLSubject.Location.Y + LBLSubject.Height + alligmentFromSubjectY;
  797. reminingY -= nextYPos;
  798.  
  799. Label LBLnumOfGames = new MyLabel("number of Games: ", "LBLnumOfGames"); // i create a label so i can know the height of it
  800.  
  801. alligmentFromLabelsY = (reminingY - LBLnumOfGames.Height * 4) / 5;
  802. nextYPos += alligmentFromLabelsY;
  803.  
  804. // configures and adds the number of games label
  805. LBLnumOfGames.Location = new Point(0, nextYPos); // i put 0 in x of all labels because i change it to the middle when the real text arrives from server
  806. this.Controls.Add(LBLnumOfGames);
  807.  
  808. nextYPos += alligmentFromLabelsY;
  809.  
  810. // configures and adds the number of right answers label
  811. Label LBLnumOfRightAns = new MyLabel("number of right answers: ", "LBLnumOfRightAns");
  812. LBLnumOfRightAns.Location = new Point(0, nextYPos);
  813. this.Controls.Add(LBLnumOfRightAns);
  814.  
  815. nextYPos += alligmentFromLabelsY;
  816.  
  817. // configures and adds the number of wrong answers label
  818. Label LBLnumOfWrongAns = new MyLabel("number of wrong answers: ", "LBLnumOfWrongAns");
  819. LBLnumOfWrongAns.Location = new Point(0, nextYPos);
  820. this.Controls.Add(LBLnumOfWrongAns);
  821.  
  822. nextYPos += alligmentFromLabelsY;
  823.  
  824. // configures and adds the average time per answer
  825. Label LBLavgTimePerAns = new MyLabel("average time for answer: ", "LBLavgTimePerAns");
  826. LBLavgTimePerAns.Location = new Point(0, nextYPos);
  827. this.Controls.Add(LBLavgTimePerAns);
  828.  
  829. // sets the back button
  830. int PCHeight = this.Height / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  831. int PCWidth = this.Width / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  832. PictureBox PBBack = new MyPictureBox("PBBack", "back", new Size(PCWidth, PCHeight));
  833. PBBack.Location = new Point(this.Width - PCWidth, this.Height / ALLIGMENT_FROM_TOP);
  834. PBBack.Click += (EventHandler)CleanAllControlers;
  835. PBBack.Click += (EventHandler)InitMainScreen;
  836. this.Controls.Add(PBBack);
  837. }
  838.  
  839. /// <summary>
  840. /// changes the labels of personal status' form, has to be be used after InitPersonalStatus function used
  841. /// </summary>
  842. /// <param name="arr">an array that contains the values of the fields example { 4, 5, 10, 3 }</param>
  843. private void ChangePersonalStatus(string[] arr)
  844. {
  845. if (arr.Length > 3)
  846. {
  847. Label[] labels = new Label[4];
  848. labels[0] = this.Controls.Find("LBLnumOfGames", false).FirstOrDefault() as Label;
  849. labels[1] = this.Controls.Find("LBLnumOfRightAns", false).FirstOrDefault() as Label;
  850. labels[2] = this.Controls.Find("LBLnumOfWrongAns", false).FirstOrDefault() as Label;
  851. labels[3] = this.Controls.Find("LBLavgTimePerAns", false).FirstOrDefault() as Label;
  852.  
  853. for (int i = 0; i < 4; i++)
  854. {
  855. if (labels[i] != null)
  856. {
  857. if (i == 3)
  858. {
  859. arr[i] = String.Format("{0}.{1}", (int.Parse(arr[i]) / 100).ToString(), (int.Parse(arr[i]) % 100).ToString());
  860. Invoke((MethodInvoker)(() => labels[i].Text += arr[i]));
  861. }
  862. else
  863. {
  864. Invoke((MethodInvoker)(() => labels[i].Text += (int.Parse(arr[i])).ToString()));
  865. }
  866. Invoke((MethodInvoker)(() => labels[i].Width = TextRenderer.MeasureText(labels[i].Text, labels[i].Font).Width));
  867. Invoke((MethodInvoker)(() => labels[i].Location = new Point(this.Width / 2 - labels[i].Width / 2, labels[i].Location.Y)));
  868. }
  869. }
  870. }
  871. }
  872.  
  873. /// <summary>
  874. /// changes the form to show the best scores
  875. /// </summary>
  876. /// <param name="sender"></param>
  877. /// <param name="args"></param>
  878. private void InitBestScores(object sender, EventArgs args)
  879. {
  880. int reminingY = PBClose.Location.Y;
  881. int nextYPos = 0;
  882. int pictureBoxHeight = 0;
  883. int pictureBoxWidth = 0;
  884. int alligmentFromPicturesY = 0;
  885. int alligmentFromPictureX = 0;
  886.  
  887. // sets the background image to best scores BG
  888. this.BackgroundImage = Properties.Resources.winners;
  889.  
  890. // sets the font of the form to black
  891. this.ForeColor = Color.Black;
  892.  
  893. // sets the subject label
  894. Label LBLSubject = new MyLabel("Best Scores: ", "LBLSubject"); // i create a label to know the width of its text
  895. LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
  896. this.Controls.Add(LBLSubject);
  897.  
  898. // creates the label that contains the first player's scores
  899. Label LBL1st = new MyLabel("ABCD", "LBL1st"); // i put text in the label so i can know its properties
  900.  
  901. // sets the next position for the picture boxes sector
  902. nextYPos += (LBLSubject.Location.Y + LBLSubject.Height + alligmentFromSubjectY);
  903. reminingY -= nextYPos;
  904.  
  905. // configures the properties of the picture boxes
  906. pictureBoxHeight = reminingY / BEST_SCORES_FORM_PB_HEIGHT;
  907. pictureBoxWidth = this.Width / BEST_SCORES_FORM_PB_WIDTH;
  908.  
  909. // configures the picture box sector
  910. alligmentFromPicturesY = (reminingY - pictureBoxHeight * 2 - LBL1st.Height * 2) / 2;
  911. alligmentFromPictureX = (this.Width - pictureBoxWidth * 3) / 4;
  912. nextYPos += alligmentFromPicturesY;
  913.  
  914. // sets the first place picture box
  915. PictureBox PB1st = new MyPictureBox("PB1st", "_1st", new Size(pictureBoxWidth, pictureBoxHeight));
  916. PB1st.Location = new Point(alligmentFromPictureX * 2 + pictureBoxWidth, nextYPos);
  917. this.Controls.Add(PB1st);
  918.  
  919. LBL1st.Location = new Point(PB1st.Location.X + PB1st.Width / 2, nextYPos - LBL1st.Height);
  920. this.Controls.Add(LBL1st);
  921.  
  922. // sets the postion to the next picture box
  923. nextYPos += alligmentFromPicturesY;
  924.  
  925. // sets the second best score picture box
  926. PictureBox PB2nd = new MyPictureBox("PB2nd", "_2nd", new Size(pictureBoxWidth, pictureBoxHeight));
  927. PB2nd.Location = new Point(alligmentFromPictureX, nextYPos);
  928. this.Controls.Add(PB2nd);
  929.  
  930. // sets the label of the second best score
  931. Label LBL2nd = new MyLabel("ABCD", "LBL2nd"); // i put text in the label so i can know its properties
  932. LBL2nd.Location = new Point(PB2nd.Location.X + PB2nd.Width / 2, PB2nd.Location.Y - LBL2nd.Height);
  933. this.Controls.Add(LBL2nd);
  934.  
  935. // sets the picture box of the second best score
  936. PictureBox PB3rd = new MyPictureBox("PB3rd", "_3rd", new Size(pictureBoxWidth, pictureBoxHeight));
  937. PB3rd.Location = new Point(this.Width - alligmentFromPictureX - PB3rd.Width, nextYPos);
  938. this.Controls.Add(PB3rd);
  939.  
  940. // sets the label of th ethird best score
  941. Label LBL3rd = new MyLabel("ABCD", "LBL3rd"); // i put text in the label so i can know its properties
  942. LBL3rd.Location = new Point(PB3rd.Location.X + PB3rd.Width / 2, nextYPos - LBL3rd.Height);
  943. this.Controls.Add(LBL3rd);
  944.  
  945. // sets the back button
  946. int PCHeight = this.Height / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  947. int PCWidth = this.Width / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  948. PictureBox PBBack = new MyPictureBox("PBBack", "back", new Size(PCWidth, PCHeight));
  949. PBBack.Location = new Point(this.Width - PCWidth, this.Height / ALLIGMENT_FROM_TOP);
  950. PBBack.Click += (EventHandler)CleanAllControlers;
  951. PBBack.Click += (EventHandler)InitMainScreen;
  952. this.Controls.Add(PBBack);
  953. }
  954.  
  955. /// <summary>
  956. /// changes the best scores labels
  957. /// should be used when clicked on get best scores button, after InitBestScores was called
  958. /// </summary>
  959. /// <param name="arr">an array of pairs in which the key is the name of the user and the value is his score</param>
  960. private void changeBestScores(KeyValuePair<string, string>[] arr)
  961. {
  962. Label[] labels = new Label[3];
  963.  
  964. labels[0] = this.Controls.Find("LBL1st", false).FirstOrDefault() as Label;
  965. labels[1] = this.Controls.Find("LBL2nd", false).FirstOrDefault() as Label;
  966. labels[2] = this.Controls.Find("LBL3rd", false).FirstOrDefault() as Label;
  967.  
  968. for (int i = 0; i < 3; i++)
  969. {
  970. if (labels[i] != null)
  971. {
  972. labels[i].Text = arr[i].Key + ": " + arr[i].Value;
  973. labels[i].Width = TextRenderer.MeasureText(labels[i].Text, labels[i].Font).Width;
  974. labels[i].Location = new Point(labels[i].Location.X - labels[i].Width / 2, labels[i].Location.Y);
  975. }
  976. }
  977. }
  978.  
  979. /// <summary>
  980. /// cleans the screen from all the controls on it
  981. /// </summary>
  982. private void CleanAllControlers(object sender, EventArgs args)
  983. {
  984. //foreach (Control c in this.Controls)
  985. //{
  986. // MessageBox.Show(c.GetType().ToString());
  987. // this.Controls.Remove(c);
  988. //}
  989.  
  990. this.Controls.Clear();
  991. this.Controls.Add(PBClose);
  992. }
  993.  
  994. private void PBClose_Click(object sender, EventArgs args)
  995. {
  996. this.Close();
  997. }
  998.  
  999. private void BTNBest_Click(object sender, EventArgs args)
  1000. {
  1001. stream.sendData("223");
  1002. }
  1003.  
  1004. private void PBLogOut_Click(object sender, EventArgs args)
  1005. {
  1006. stream.sendData("201");
  1007. }
  1008.  
  1009. private void BTNPersonal_Click(object sender, EventArgs args)
  1010. {
  1011. stream.sendData("225");
  1012. }
  1013.  
  1014. private void BTNJoin_Click(object sender, EventArgs args)
  1015. {
  1016. stream.sendData("205");
  1017. }
  1018.  
  1019. private void PBRefresh_Click(object sender, EventArgs args)
  1020. {
  1021. stream.sendData("205");
  1022. }
  1023.  
  1024. private void BTNCreateRoom_Click(object sender, EventArgs args)
  1025. {
  1026. TextBox[] txtBoxes = new TextBox[3];
  1027.  
  1028. TextBox TXTRoomName = this.Controls.Find("TXTRoomName", false).FirstOrDefault() as TextBox;
  1029. txtBoxes[0] = this.Controls.Find("TXTNumOfPlayers", false).FirstOrDefault() as TextBox;
  1030. txtBoxes[1] = this.Controls.Find("TXTNumOfQuestions", false).FirstOrDefault() as TextBox;
  1031. txtBoxes[2] = this.Controls.Find("TXTTimePerAns", false).FirstOrDefault() as TextBox;
  1032.  
  1033. // changes the room name to the wanted room name
  1034. if (TXTRoomName != null && TXTRoomName.Text != "")
  1035. {
  1036. this.roomName = TXTRoomName.Text;
  1037. }
  1038. else
  1039. {
  1040. return;
  1041. }
  1042.  
  1043. // adds the properties of the new room to the roomProperties, which then will be used to initializa the inroom form
  1044. this.roomProperties.Clear();
  1045. foreach (TextBox t in txtBoxes)
  1046. {
  1047. if (t != null && t.Text != "")
  1048. {
  1049. this.roomProperties.Add(t.Text);
  1050. }
  1051. else
  1052. {
  1053. this.roomName = "";
  1054. return;
  1055. }
  1056. }
  1057.  
  1058. stream.sendData(String.Format("213{0:00}{1}{2}{3:00}{4:00}", this.roomName.Length, this.roomName, roomProperties[0], int.Parse(roomProperties[1]), int.Parse(roomProperties[2])));
  1059. }
  1060.  
  1061. private void PBJoin_Click(object sender, EventArgs args)
  1062. {
  1063. ListBox LBRooms = this.Controls.Find("LBRooms", false).FirstOrDefault() as ListBox;
  1064.  
  1065. if (LBRooms.SelectedIndex >= 0 && int.Parse(roomsID[LBRooms.SelectedIndex]) >= 0)
  1066. {
  1067. this.roomName = LBRooms.Items[LBRooms.SelectedIndex] as string;
  1068. this.stream.sendData("209" + this.roomsID[LBRooms.SelectedIndex]);
  1069. }
  1070. }
  1071.  
  1072. private void LBRooms_SelectedIndexChanged(object sender, System.EventArgs e)
  1073. {
  1074. ListBox LBRooms = this.Controls.Find("LBRooms", false).FirstOrDefault() as ListBox;
  1075.  
  1076. if (int.Parse(this.roomsID[LBRooms.SelectedIndex]) >= 0)
  1077. {
  1078. this.stream.sendData("207" + this.roomsID[LBRooms.SelectedIndex]);
  1079. }
  1080. }
  1081.  
  1082. /// <summary>
  1083. /// sends the username, password and email to server according to the format for sign up
  1084. /// </summary>
  1085. private void PBSign_Up_Click(object sender, EventArgs args)
  1086. {
  1087. TextBox txtUserName = (TextBox)this.Controls.Find("TXTusername", false)[0];
  1088. TextBox txtPassword = (TextBox)this.Controls.Find("TXTpsw", false)[0];
  1089. TextBox txtEmail = (TextBox)this.Controls.Find("TXTemail", false)[0];
  1090.  
  1091. if (txtUserName != null && txtPassword != null && txtEmail != null)
  1092. {
  1093. this.stream.sendData(String.Format("203{0:00}{1}{2:00}{3}{4:00}{5}", txtUserName.Text.Length, txtUserName.Text, txtPassword.Text.Length, txtPassword.Text, txtEmail.Text.Length, txtEmail.Text));
  1094. }
  1095. }
  1096.  
  1097. /// <summary>
  1098. /// sends the username and password to server according to the format for sign in
  1099. /// </summary>
  1100. private void PBSign_In_Click(object sender, EventArgs e)
  1101. {
  1102. TextBox txtUserName = (TextBox)this.Controls.Find("TXTusername", false)[0];
  1103. TextBox txtPassword = (TextBox)this.Controls.Find("TXTpsw", false)[0];
  1104.  
  1105. if (txtUserName != null && txtPassword != null)
  1106. {
  1107. this.stream.sendData(String.Format("200{0:00}{1}{2:00}{3}", txtUserName.Text.Length, txtUserName.Text, txtPassword.Text.Length, txtPassword.Text));
  1108. }
  1109. }
  1110.  
  1111. private void BTNLeave_Click(object sender, EventArgs args)
  1112. {
  1113. stream.sendData("211");
  1114. }
  1115.  
  1116. private void BTNCloseRoom_Click(object sender, EventArgs args)
  1117. {
  1118. stream.sendData("215");
  1119. }
  1120.  
  1121. private void BTNStartGame_Click(object sender, EventArgs args)
  1122. {
  1123. stream.sendData("217");
  1124. }
  1125.  
  1126. /// <summary>
  1127. /// handles the messags from server
  1128. /// </summary>
  1129. private void handleMessage()
  1130. {
  1131. string msg = "";
  1132. while (true)
  1133. {
  1134. msg = this.stream.recvData(); // the message from the server
  1135.  
  1136. switch (int.Parse(msg.Substring(0, 3)))
  1137. {
  1138. case 102:
  1139. this.HandleSignIn(msg.Substring(3, 4)); // in case of sign in
  1140. break;
  1141. case 104:
  1142. this.HandleSignUp(msg.Substring(3, 4)); // in case of sign up
  1143. break;
  1144. case 124:
  1145. this.HandleBestScores(msg.Substring(3)); // in case of best scores
  1146. break;
  1147. case 126:
  1148. this.HandlePersonalStatus(msg.Substring(3)); // in case of personal status
  1149. break;
  1150. case 106:
  1151. this.HandleGetRooms(msg.Substring(3)); // in case of get rooms
  1152. break;
  1153. case 108:
  1154. this.HandleGetUsers(msg.Substring(3)); // in case of get users
  1155. break;
  1156. case 110:
  1157. this.HandleJoinRoon(msg.Substring(3)); // in case of join room
  1158. break;
  1159. case 112:
  1160. this.HandleLeaveRoom(msg.Substring(3, 1)); // in case of leave room
  1161. break;
  1162. case 116:
  1163. this.HandleCloseRoom(); // in case of close room
  1164. break;
  1165. case 118:
  1166. this.HandleStartGame(msg.Substring(3)); // in case of start game
  1167. break;
  1168. case 114:
  1169. this.handleCreateRoom(msg.Substring(3, 1)); // in case of create room
  1170. break;
  1171. }
  1172. }
  1173. }
  1174.  
  1175. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  1176. {
  1177. this.t.Abort();
  1178. this.stream.sendData("299");
  1179. this.stream.CloseStream();
  1180. this.PBClose.Dispose();
  1181. }
  1182.  
  1183. /// <summary>
  1184. /// handles sign in request
  1185. /// </summary>
  1186. /// <param name="msg"> the message from the server </param>
  1187. private void HandleSignIn(string msg)
  1188. {
  1189. switch (int.Parse(msg))
  1190. {
  1191. case 0:
  1192. uid = ((TextBox)this.Controls.Find("TXTusername", false).FirstOrDefault()).Text; // to show his username in main screen
  1193. Invoke((MethodInvoker)(() => CleanAllControlers(null, EventArgs.Empty)));
  1194. Invoke((MethodInvoker)(() => InitMainScreen(null, EventArgs.Empty)));
  1195. break;
  1196. case 1:
  1197. MessageBox.Show("Wrong Details");
  1198. break;
  1199. case 2:
  1200. MessageBox.Show("User is already connected");
  1201. break;
  1202. }
  1203. }
  1204.  
  1205. /// <summary>
  1206. /// handles sign up request
  1207. /// </summary>
  1208. /// <param name="msg"> message from the server </param>
  1209. private void HandleSignUp(string msg)
  1210. {
  1211. switch (int.Parse(msg))
  1212. {
  1213. case 0:
  1214. Invoke((MethodInvoker)(() => CleanAllControlers(null, EventArgs.Empty)));
  1215. Invoke((MethodInvoker)(() => this.InitLoginScreen(null, EventArgs.Empty)));
  1216. break;
  1217. case 1:
  1218. MessageBox.Show("Pass illegal");
  1219. break;
  1220. case 2:
  1221. MessageBox.Show("Username is already exists");
  1222. break;
  1223. case 3:
  1224. MessageBox.Show("Username is illegal");
  1225. break;
  1226. case 4:
  1227. MessageBox.Show("Other");
  1228. break;
  1229. }
  1230. }
  1231.  
  1232. /// <summary>
  1233. /// handles best scores request
  1234. /// </summary>
  1235. /// <param name="msg"> message from the server </param>
  1236. private void HandleBestScores(string msg)
  1237. {
  1238. List<string> elem = new List<string>();
  1239.  
  1240. int[] arr = new int[6] { 2, -6, 2, -6, 2, -6 };
  1241.  
  1242. Helper.convertBytes(arr, elem, msg);
  1243.  
  1244. KeyValuePair<string, string>[] arrPairs = new KeyValuePair<string, string>[3];
  1245.  
  1246. if (elem.Count > 5)
  1247. {
  1248. for (int i = 0; i < 3; i++)
  1249. {
  1250. // arrPpairs is array of pairs that key is the name of the user and the value is his score
  1251. arrPairs[i] = new KeyValuePair<string, string>(elem[i * 2], (int.Parse(elem[i * 2 + 1])).ToString());
  1252. }
  1253. }
  1254.  
  1255. Invoke((MethodInvoker)(() => this.changeBestScores(arrPairs)));
  1256. }
  1257.  
  1258. /// <summary>
  1259. /// handles personal status request
  1260. /// </summary>
  1261. /// <param name="msg"> message from the server </param>
  1262. private void HandlePersonalStatus(string msg)
  1263. {
  1264. List<string> elem = new List<string>();
  1265.  
  1266. int[] arr = new int[4] { -4, -6, -6, -4 };
  1267.  
  1268. Helper.convertBytes(arr, elem, msg);
  1269.  
  1270. this.ChangePersonalStatus(elem.ToArray());
  1271. }
  1272.  
  1273. /// <summary>
  1274. /// handle get rooms
  1275. /// </summary>
  1276. /// <param name="msg"> the message from the server </param>
  1277. private void HandleGetRooms(string msg)
  1278. {
  1279. List<string> elem = new List<string>();
  1280.  
  1281. int numberOfRooms = int.Parse(msg.Substring(0, 4));
  1282.  
  1283. int[] arr = new int[numberOfRooms * 2]; // because there are 2 parameters of each room (id and name)
  1284.  
  1285. for (int i = 0; i < numberOfRooms; i++)
  1286. {
  1287. arr[i * 2] = -4;
  1288. arr[i * 2 + 1] = 2;
  1289. }
  1290.  
  1291. Helper.convertBytes(arr, elem, msg.Substring(4));
  1292.  
  1293. roomsID.Clear();
  1294. for (int i = 0; i < elem.Count; i += 2)
  1295. {
  1296. /* the variable elem consists of id and name, for exemple {5, Room1, 5, Room2} that is why i only added to
  1297. * the variable roomsID only the items in even places and remove them for the variable elem becase the
  1298. * function how get the variable elem needs only the names
  1299. */
  1300. this.roomsID.Add(elem[i]);
  1301. elem.RemoveAt(i);
  1302. }
  1303.  
  1304. Invoke((MethodInvoker)(() => this.changeRoomsList(elem)));
  1305. }
  1306.  
  1307. /// <summary>
  1308. /// handle get users
  1309. /// </summary>
  1310. /// <param name="msg"> the message from the server</param>
  1311. private void HandleGetUsers(string msg)
  1312. {
  1313. if (msg == "0") // in case of the game already start
  1314. {
  1315. MessageBox.Show("The game already start, please refresh");
  1316. return;
  1317. }
  1318.  
  1319. List<string> elem = new List<string>();
  1320.  
  1321. int numberOfUsers = int.Parse(msg.Substring(0, 1));
  1322.  
  1323. int[] arr = new int[numberOfUsers];
  1324.  
  1325. for (int i = 0; i < numberOfUsers; i++)
  1326. {
  1327. arr[i] = 2;
  1328. }
  1329.  
  1330. Helper.convertBytes(arr, elem, msg.Substring(1));
  1331.  
  1332. /* the can be called from choosing room and inside room that why the condition (the if)
  1333. * if the condition != null its mean that we are inside room and we souhld call the function ChangeLBpart
  1334. * else mean that we are chossing room and we should call function changeUsersInRoom
  1335. */
  1336.  
  1337. if (this.Controls.Find("LBLparticipates", false).FirstOrDefault() as Label != null)
  1338. {
  1339. Invoke((MethodInvoker)(() => this.ChangeLBpart(elem)));
  1340. }
  1341. else
  1342. {
  1343. Invoke((MethodInvoker)(() => this.changeUsersInRoom(elem)));
  1344. }
  1345. }
  1346.  
  1347. /// <summary>
  1348. /// handle join room
  1349. /// </summary>
  1350. /// <param name="msg"> the message from the server</param>
  1351. private void HandleJoinRoon(string msg)
  1352. {
  1353. if (msg.Substring(0, 1) == "0") // in case of succses
  1354. {
  1355. Invoke((MethodInvoker)(() => this.CleanAllControlers(null, EventArgs.Empty)));
  1356. // i create new button so i can identify whether the page should be admin or user presentation
  1357. Invoke((MethodInvoker)(() => this.InitInsideRoom(new PictureBox(), EventArgs.Empty)));
  1358.  
  1359. List<string> elem = new List<string>();
  1360.  
  1361. int[] arr = new int[2] { -2, -2 };
  1362.  
  1363. Helper.convertBytes(arr, elem, msg.Substring(1));
  1364.  
  1365. Invoke((MethodInvoker)(() => this.ChangeLabels(elem)));
  1366. }
  1367. else if (msg.Substring(0, 1) == "1") // in case of full room
  1368. {
  1369. MessageBox.Show("failed - room is full");
  1370. }
  1371. else // in case of room not exist or other reason
  1372. {
  1373. MessageBox.Show("failed - room not exist or other reason");
  1374. }
  1375. }
  1376.  
  1377. /// <summary>
  1378. /// handle leave room by clean all the controlers in the form and call InitMainScreen
  1379. /// </summary>
  1380. /// <param name="msg"> the message from the server</param>
  1381. private void HandleLeaveRoom(string msg)
  1382. {
  1383. if (msg == "0")
  1384. {
  1385. this.roomName = ""; // delete the name of room
  1386. Invoke((MethodInvoker)(() => CleanAllControlers(null, EventArgs.Empty)));
  1387. Invoke((MethodInvoker)(() => InitMainScreen(null, EventArgs.Empty)));
  1388. Invoke((MethodInvoker)(() => this.PBClose.Visible = true)); // turns on the close picture box
  1389. }
  1390. }
  1391.  
  1392. /// <summary>
  1393. /// handle close room by clean all the controlers in the form and call InitMainScreen
  1394. /// </summary>
  1395. private void HandleCloseRoom()
  1396. {
  1397. MessageBox.Show("The admin close the room");
  1398. this.roomName = ""; // delete the name of room
  1399. Invoke((MethodInvoker)(() => CleanAllControlers(null, EventArgs.Empty)));
  1400. Invoke((MethodInvoker)(() => InitMainScreen(null, EventArgs.Empty)));
  1401. Invoke((MethodInvoker)(() => this.PBClose.Visible = true)); // turns on the close picture box
  1402. }
  1403.  
  1404. private void handleCreateRoom(string msg)
  1405. {
  1406. if (msg == "0")
  1407. {
  1408. Invoke((MethodInvoker)(() => CleanAllControlers(null, EventArgs.Empty)));
  1409. Invoke((MethodInvoker)(() =>InitInsideRoom(new Button(), EventArgs.Empty)));
  1410. Invoke((MethodInvoker)(() => ChangeLabels(this.roomProperties)));
  1411. Invoke((MethodInvoker)(() => ChangeLBpart(new List<string> { uid } ))); // adds the current user to the users list
  1412. }
  1413. else
  1414. {
  1415. MessageBox.Show("Could not open the room");
  1416. }
  1417. }
  1418.  
  1419. private void HandleStartGame(string msg)
  1420. {
  1421.  
  1422. }
  1423. }
  1424. }
  1425.  
  1426. // TODO:
  1427. // set font according to resulution
  1428. // remove the size of the Close button from the login and signup pages
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement