Advertisement
Guest User

Untitled

a guest
Jul 4th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 40.29 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.  
  40. private MyPictureBox PBClose; // the close button, <> needs to be disposed
  41. private Thread t; // Thread to handle messages from the server
  42. private Stream stream; // creates a connection with the server by ip and port
  43. private int alligmentFromSubjectY; // the alligment in y axis from the subject
  44. private string uid; // the username of the signed in user
  45.  
  46. public Form1()
  47. {
  48. //this.stream = new Stream("127.0.0.1", 8686);
  49.  
  50. //t = new Thread(handleMessage);
  51. //t.Start();
  52.  
  53. InitializeComponent();
  54.  
  55. this.Width = Screen.PrimaryScreen.Bounds.Width;
  56. this.Height = Screen.PrimaryScreen.Bounds.Height;
  57.  
  58. InitStaticVars();
  59. InitJoinRoom(null, EventArgs.Empty);
  60. changeRoomsList(new List<string> { "tal", "Dolev", "I", "J", "K", "L", "M", "N" });
  61. changeUsersInRoom(new List<string> { "tal", "yarini" });
  62. changeRoomsList(new List<string> { "tal", "Dolev", "I", "J", "K", "L", "M", "N" });
  63. //InitPersonalStatus(null, EventArgs.Empty);
  64. //ChangePersonalStatus(new string[4] { "4", "10", "5", "3" });
  65. //InitLoginScreen(null, EventArgs.Empty);
  66. //InitSignUp(null, EventArgs.Empty);
  67. //InitMainScreen(null, EventArgs.Empty);
  68. //InitBestScores(null, EventArgs.Empty);
  69. //KeyValuePair<string, string>[] arr = new KeyValuePair<string, string>[3];
  70. //arr[0] = new KeyValuePair<string, string>("tal", "100");
  71. //arr[1] = new KeyValuePair<string, string>("Dolev", "0");
  72. //arr[2] = new KeyValuePair<string, string>("yarin", "10");
  73. //changeBestScores(arr);
  74. //CleanAllControlers();
  75. }
  76.  
  77. /// <summary>
  78. /// initializes all static variables of custom classes, and current class
  79. /// </summary>
  80. public void InitStaticVars()
  81. {
  82. // MyTxtBox Class:
  83. // the default width of the text box
  84. MyTxtBox._width = (this.Width / LOG_IN_FORM_TXT_X_SIZE);
  85.  
  86. // current class:
  87. // sets the close button
  88. PBClose = new MyPictureBox("PBClose", "close", new Size(this.Width / CLOSE_GAME_BUTTON_WIDTH, this.Height / CLOSE_GAME_BUTTON_HEIGHT));
  89. PBClose.Location = new Point(this.Width / 2 - PBClose.Width / 2, this.Height - PBClose.Height - this.Height / ALLIGMENT_FROM_TOP);
  90. PBClose.Click += PBClose_Click;
  91. this.Controls.Add(PBClose);
  92.  
  93. // sets the alligment from top of screen
  94. alligmentFromSubjectY = this.Height / ALLIGMENT_FROM_TOP;
  95.  
  96. uid = "tal";
  97. }
  98.  
  99. /// <summary>
  100. /// makes the login is screen
  101. /// </summary>
  102. public void InitLoginScreen(object sender, EventArgs args)
  103. {
  104. int reminingY = this.Height;
  105. int nextYPos = 0;
  106. int xOfTextBoxes = 0;
  107. int sizeOfPictureBox = 0;
  108. int alligmentFromPictureBox = 0;
  109. int alligmentFromTextBoxes = 0;
  110. int alligmentFromTextBoxesToLabels = this.Width / LOG_IN_FORM_ALLGIMENT_FROM_TEXT_TO_LABEL;
  111. int xOfPicBoxes = 0;
  112. List<Control> cont = new List<Control>();
  113.  
  114. // sets the background of the page
  115. this.BackgroundImage = Client.Properties.Resources.loginBG;
  116.  
  117. // sets the color of the letters
  118. this.ForeColor = Color.White;
  119.  
  120. // sets the subject label
  121. Label LBLSubject = new MyLabel("Please Log In", "LBLSubject"); // i create a label to know the width of its text
  122. LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
  123. cont.Add(LBLSubject);
  124.  
  125. nextYPos = LBLSubject.Location.Y + alligmentFromSubjectY + LBLSubject.Height; // begining of details section
  126. reminingY -= nextYPos; // sets the space of details section
  127.  
  128. // sets the text box of username
  129. TextBox TXTuserName = new MyTxtBox("TXTusername"); // i create a label to know the height of its font
  130. alligmentFromTextBoxes = ((reminingY / LOG_IN_FORM_TEXT_SECTOR_HEIGHT) - (TXTuserName.Height * 2)) / 3; // generates the needed alligment
  131. nextYPos += alligmentFromTextBoxes;
  132. reminingY -= reminingY / LOG_IN_FORM_TEXT_SECTOR_HEIGHT; // sets the space of buttons section
  133.  
  134. TXTuserName.Location = new Point((this.Width / 2) - (TXTuserName.Width / 2), nextYPos);
  135. cont.Add(TXTuserName);
  136.  
  137. // sets the label of username
  138. Label LBLusername = new MyLabel("UserName: ", "LBLusername"); // sets the label of the userName
  139. LBLusername.Location = new Point(TXTuserName.Location.X - LBLusername.Width - alligmentFromTextBoxesToLabels, nextYPos);
  140. cont.Add(LBLusername);
  141.  
  142. nextYPos += (TXTuserName.Height + alligmentFromTextBoxes); // sets the position of the next textBox
  143. xOfTextBoxes = TXTuserName.Location.X;
  144.  
  145. // sets the text box of password
  146. TextBox TXTpsw = new MyTxtBox(new Point(xOfTextBoxes, nextYPos), "TXTpsw");
  147. cont.Add(TXTpsw);
  148.  
  149. // sets the label of password
  150. Label LBLpsw = new MyLabel("password: ", "LBLpsw"); // sets the password label
  151. LBLpsw.Location = new Point(xOfTextBoxes - LBLpsw.Width - alligmentFromTextBoxesToLabels, nextYPos);
  152. cont.Add(LBLpsw);
  153.  
  154. nextYPos += (TXTuserName.Height + alligmentFromTextBoxes); // sets the begining of the buttons sector
  155.  
  156. // sets the buttons section
  157. sizeOfPictureBox = reminingY / LOG_IN_FORM_PICBOX_HEIGHT;
  158. reminingY -= (sizeOfPictureBox * 2);
  159. alligmentFromPictureBox = reminingY / 2;
  160.  
  161. // sets the sign in button
  162. PictureBox PBSignIn = new MyPictureBox("PBSignIn", "SignIn", new Size(TXTuserName.Width, sizeOfPictureBox));
  163. xOfPicBoxes = (this.Width / 2) - (PBSignIn.Width / 2); // sets the x of picture boxes
  164. PBSignIn.Location = new Point(xOfPicBoxes, nextYPos);
  165. PBSignIn.Click += (EventHandler)PBSign_In_Click;
  166. cont.Add(PBSignIn);
  167.  
  168. nextYPos += alligmentFromPictureBox;
  169.  
  170. // sets the signup button
  171. PictureBox PBSignUp = new MyPictureBox("PBSignUp", "SignUp", new Size(PBSignIn.Width, sizeOfPictureBox));
  172. PBSignUp.Location = new Point(xOfPicBoxes, nextYPos);
  173. PBSignUp.Click += (EventHandler)(CleanAllControlers);
  174. PBSignUp.Click += (EventHandler)InitSignUp;
  175. cont.Add(PBSignUp);
  176.  
  177. this.Controls.AddRange(cont.ToArray());
  178. }
  179.  
  180. /// <summary>
  181. /// sets the form to be sign up page
  182. /// </summary>
  183. public void InitSignUp(object sender, EventArgs args)
  184. {
  185. int reminingY = this.Height;
  186. int nextYPos = 0;
  187. int xOfTextBoxes = 0;
  188. int sizeOfPictureBox = 0;
  189. int alligmentFromPictureBox = 0;
  190. int alligmentFromTextBoxes = 0;
  191. int alligmentFromTextBoxesToLabels = this.Width / LOG_IN_FORM_ALLGIMENT_FROM_TEXT_TO_LABEL;
  192. int xOfPicBoxes = 0;
  193. List<Control> cont = new List<Control>();
  194.  
  195. // sets the background
  196. this.BackgroundImage = Properties.Resources.signUpBG;
  197.  
  198. // sets the color of the letters
  199. this.ForeColor = Color.Black;
  200.  
  201. // sets the subject label
  202. Label LBLSubject = new MyLabel("Please Sign Up", "LBLSubject"); // i create a label to know the width of its text
  203. LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
  204. cont.Add(LBLSubject);
  205.  
  206. nextYPos = LBLSubject.Location.Y + alligmentFromSubjectY + LBLSubject.Height; // begining of details section
  207. reminingY -= nextYPos; // sets the space of details section
  208.  
  209. // sets the text box of username
  210. TextBox TXTuserName = new MyTxtBox("TXTusername"); // i create a label to know the height of its font
  211. alligmentFromTextBoxes = ((reminingY / LOG_IN_FORM_TEXT_SECTOR_HEIGHT) - (TXTuserName.Height * 3)) / 4; // generates the needed alligment
  212. nextYPos += alligmentFromTextBoxes;
  213. reminingY -= reminingY / LOG_IN_FORM_TEXT_SECTOR_HEIGHT; // sets the space of buttons section
  214.  
  215. TXTuserName.Location = new Point((this.Width / 2) - (TXTuserName.Width / 2), nextYPos);
  216. cont.Add(TXTuserName);
  217.  
  218. // sets the label of username
  219. Label LBLusername = new MyLabel("UserName: ", "LBLusername"); // sets the label of the userName
  220. LBLusername.Location = new Point(TXTuserName.Location.X - LBLusername.Width - alligmentFromTextBoxesToLabels, nextYPos);
  221. cont.Add(LBLusername);
  222.  
  223. nextYPos += (TXTuserName.Height + alligmentFromTextBoxes); // sets the position of the next textBox
  224. xOfTextBoxes = TXTuserName.Location.X;
  225.  
  226. // sets the text box of password
  227. TextBox TXTpsw = new MyTxtBox(new Point(xOfTextBoxes, nextYPos), "TXTpsw");
  228. cont.Add(TXTpsw);
  229.  
  230. // sets the label of password
  231. Label LBLpsw = new MyLabel("password: ", "LBLpsw"); // sets the password label
  232. LBLpsw.Location = new Point(xOfTextBoxes - LBLpsw.Width - alligmentFromTextBoxesToLabels, nextYPos);
  233. cont.Add(LBLpsw);
  234.  
  235. nextYPos += (TXTuserName.Height + alligmentFromTextBoxes); // sets the position of the next textBox
  236.  
  237. // sets the text box of email
  238. TextBox TXTemail = new MyTxtBox(new Point(xOfTextBoxes, nextYPos), "TXTemail");
  239. cont.Add(TXTemail);
  240.  
  241. // sets the label of email
  242. Label LBLemail = new MyLabel("email: ", "LBLemail");
  243. LBLemail.Location = new Point(xOfTextBoxes - LBLemail.Width - alligmentFromTextBoxesToLabels, nextYPos);
  244. cont.Add(LBLemail);
  245.  
  246. nextYPos += (TXTuserName.Height + alligmentFromTextBoxes); // sets the begining of the buttons sector
  247.  
  248. // sets the buttons section
  249. sizeOfPictureBox = reminingY / LOG_IN_FORM_PICBOX_HEIGHT;
  250. reminingY -= (sizeOfPictureBox * 2);
  251. alligmentFromPictureBox = reminingY / 2;
  252.  
  253. // sets the sign up button
  254. PictureBox PBSignUp = new MyPictureBox("PBSignUp", "SignUp", new Size(TXTemail.Width, sizeOfPictureBox));
  255. xOfPicBoxes = (this.Width / 2) - (PBSignUp.Width / 2); // sets the x of picture boxes
  256. PBSignUp.Location = new Point(xOfPicBoxes, nextYPos);
  257. PBSignUp.Click += (EventHandler)PBSign_Up_Click;
  258. //PBSignUp.Click += (EventHandler)CleanAllControlers;
  259. cont.Add(PBSignUp);
  260.  
  261. // sets the back button
  262. int PCHeight = this.Height / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  263. int PCWidth = this.Width / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  264. PictureBox PBBack = new MyPictureBox("PBBack", "back", new Size(PCWidth, PCHeight));
  265. PBBack.Location = new Point(this.Width - PCWidth, this.Height / ALLIGMENT_FROM_TOP);
  266. PBBack.Click += (EventHandler)CleanAllControlers;
  267. PBBack.Click += (EventHandler)InitLoginScreen;
  268. cont.Add(PBBack);
  269.  
  270. this.Controls.AddRange(cont.ToArray());
  271. }
  272.  
  273. /// <summary>
  274. /// builds the main screen
  275. /// </summary>
  276. /// <param name="sender"></param>
  277. /// <param name="args"></param>
  278. public void InitMainScreen(object sender, EventArgs args)
  279. {
  280. int nextYPos = 0;
  281. int reminingY = PBClose.Location.Y;
  282. int heightOfPB = this.Height / MAIN_FORM_PICBOX_HEIGHT;
  283. int widthOfPB = this.Width / MAIN_FORM_PICBOX_WIDTH;
  284. int alligmentFromButtonsY = 0;
  285. int alligmentFromButtonsX = 0;
  286.  
  287. // sets the background of the screen
  288. this.BackgroundImage = Properties.Resources.mainScreenBG;
  289.  
  290. // sets the font color to black
  291. this.ForeColor = Color.Black;
  292.  
  293. // sets the subject label
  294. Label LBLSubject = new MyLabel("Hello " + uid, "LBLSubject"); // i create a label to know the width of its text
  295. LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
  296. this.Controls.Add(LBLSubject);
  297.  
  298. // configures all the propeties needed for the button
  299. nextYPos = LBLSubject.Location.Y + alligmentFromSubjectY;
  300. reminingY -= (nextYPos - heightOfPB * 2);
  301. alligmentFromButtonsY = reminingY / 3;
  302. nextYPos += (alligmentFromButtonsY / 2);
  303. alligmentFromButtonsX = (this.Width - widthOfPB * 3) / 3;
  304.  
  305. // sets the Join room button
  306. Button BTNJoin = new MyButton("BTNJoin", new Size(widthOfPB, heightOfPB), "Join Room");
  307. BTNJoin.Location = new Point(alligmentFromButtonsX, nextYPos);
  308. this.Controls.Add(BTNJoin);
  309.  
  310. // sets the Create room button
  311. Button BTNCreate = new MyButton("BTNCreate", new Size(widthOfPB, heightOfPB), "Create Room");
  312. BTNCreate.Location = new Point(this.Width - widthOfPB - alligmentFromButtonsX, nextYPos);
  313. this.Controls.Add(BTNCreate);
  314.  
  315. nextYPos += alligmentFromButtonsY;
  316.  
  317. // sets the best scores button
  318. Button BTNBest = new MyButton("BTNBest", new Size(widthOfPB, heightOfPB), "Best Scores");
  319. BTNBest.Location = new Point(alligmentFromButtonsX, nextYPos);
  320. BTNBest.Click += (EventHandler)CleanAllControlers;
  321. BTNBest.Click += (EventHandler)InitBestScores;
  322. BTNBest.Click += (EventHandler)BTNBest_Click;
  323. this.Controls.Add(BTNBest);
  324.  
  325. // sets the personal status
  326. Button BTNPersonal = new MyButton("BTNPersonal", new Size(widthOfPB, heightOfPB), "My Status");
  327. BTNPersonal.Location = new Point(this.Width - widthOfPB - alligmentFromButtonsX, nextYPos);
  328. BTNPersonal.Click += (EventHandler)CleanAllControlers;
  329. BTNPersonal.Click += (EventHandler)InitPersonalStatus;
  330. this.Controls.Add(BTNPersonal);
  331.  
  332. // sets the logout button
  333. PictureBox PBLogOut = new MyPictureBox("PBLogOut", "signOut", new Size(this.Height / MAIN_FORM_LOG_OUT_WIDTH, this.Width / MAIN_FORM_LOG_OUT_HEIGHT));
  334. PBLogOut.Location = new Point(this.Width / ALLIGMENT_FROM_LEFT, this.Height / ALLIGMENT_FROM_TOP);
  335. PBLogOut.Click += (EventHandler)CleanAllControlers;
  336. PBLogOut.Click += (EventHandler)InitLoginScreen;
  337. PBLogOut.Click += (EventHandler)PBLogOut_Click;
  338. this.Controls.Add(PBLogOut);
  339. }
  340.  
  341. /// <summary>
  342. /// inits the join room screen
  343. /// </summary>
  344. /// <param name="sender"></param>
  345. /// <param name="args"></param>
  346. private void InitJoinRoom(object sender, EventArgs args)
  347. {
  348. int nextYPos = 0;
  349. int alligmentFromClose = this.Height - this.PBClose.Location.Y - this.PBClose.Height;
  350. int alligmentFromButtonsY = 0;
  351. int alligmentFromButtonsX = 0;
  352. int buttonSectorY = 0;
  353. int alligmentFromListsX = 0;
  354. int refreshY = 0;
  355. int joinY = 0;
  356. int reminingY = this.PBClose.Location.Y;
  357.  
  358. // sets the form to join room bg
  359. this.BackgroundImage = Properties.Resources.joinRoomBG;
  360.  
  361. // changes the fore color to black
  362. this.ForeColor = Color.Black;
  363.  
  364. // sets the subject label
  365. Label LBLSubject = new MyLabel("Choose Room", "LBLSubject"); // i create a label to know the width of its text
  366. LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
  367. this.Controls.Add(LBLSubject);
  368.  
  369. nextYPos = LBLSubject.Location.Y + LBLSubject.Height + alligmentFromSubjectY;
  370. reminingY -= nextYPos;
  371.  
  372. // configures the button sector properties
  373. buttonSectorY = reminingY / JOIN_ROOM_FORM_BUTTON_SECTOR_Y;
  374. refreshY = buttonSectorY / JOIN_ROOM_FORM_REFRESH_HEIGHT;
  375. joinY = buttonSectorY / JOIN_ROOM_FORM_JOIN_HEIGHT;
  376. alligmentFromButtonsY = (buttonSectorY - refreshY - joinY) / 2;
  377. alligmentFromButtonsX = (this.Width - this.Width / JOIN_ROOM_FORM_REFRESH_WIDTH - this.Width / JOIN_ROOM_FORM_JOIN_WIDTH) / 3;
  378.  
  379. // configures and adds the refresh button
  380. PictureBox PBRefresh = new MyPictureBox("PBRefresh", "refresh", new Size(this.Width / JOIN_ROOM_FORM_REFRESH_WIDTH, refreshY));
  381. PBRefresh.Location = new Point(alligmentFromButtonsX, PBClose.Location.Y - alligmentFromButtonsY - refreshY);
  382. PBRefresh.Click += (EventHandler)PBRefresh_Click;
  383. this.Controls.Add(PBRefresh);
  384.  
  385. // configures and adds the join button
  386. PictureBox PBJoin = new MyPictureBox("PBJoin", "joinBTN", new Size(this.Width / JOIN_ROOM_FORM_JOIN_WIDTH, this.Height / JOIN_ROOM_FORM_JOIN_HEIGHT));
  387. PBJoin.Location = new Point(this.Width - PBJoin.Width - alligmentFromButtonsX, PBRefresh.Location.Y);
  388. PBJoin.Click += (EventHandler)PBJoin_Click;
  389. this.Controls.Add(PBJoin);
  390.  
  391. reminingY = PBRefresh.Location.Y - LBLSubject.Location.Y - LBLSubject.Height - alligmentFromSubjectY;
  392. ListBox LBRooms = new MyListBox("LBRooms", new Size(this.Width / JOIN_ROOM_FORM_ROOMS_LB_WIDTH, reminingY / 4));
  393. ListBox LBUsers = new MyListBox("LBUsers", new Size(this.Width / JOIN_ROOM_FORM_USERS_LB_WIDTH, reminingY / 4));
  394.  
  395. alligmentFromListsX = (this.Width - LBRooms.Width - LBUsers.Width) / 3;
  396.  
  397. Label LBLRooms = new MyLabel("Rooms:", "LBLRooms");
  398. Label LBLUsers = new MyLabel("Users:", "LBLUsers");
  399.  
  400. // adds the rooms list box
  401. LBRooms.Location = new Point(this.Width / 2 - LBRooms.Width / 2, nextYPos + LBLRooms.Height);
  402. this.Controls.Add(LBRooms);
  403.  
  404. // sets the users list box
  405. LBUsers.Location = new Point(alligmentFromListsX, nextYPos + LBLUsers.Height);
  406. LBUsers.Visible = false;
  407. this.Controls.Add((LBUsers));
  408.  
  409. // configures and adds the label of rooms
  410. LBLRooms.Location = new Point(LBRooms.Location.X + LBRooms.Width / 2 - LBLRooms.Width / 2, nextYPos);
  411. this.Controls.Add(LBLRooms);
  412.  
  413. // configures and adds the label of users
  414. LBLUsers.Location = new Point(LBUsers.Location.X + LBUsers.Width / 2 - LBLRooms.Width / 2, nextYPos);
  415. LBLUsers.Visible = false;
  416. this.Controls.Add(LBLUsers);
  417. }
  418.  
  419. /// <summary>
  420. /// changes the room list in the join room form
  421. /// should be called when the join room button or refresh button were clicked
  422. /// </summary>
  423. /// <param name="roomsNames">the names of the rooms that will be shown on the list box</param>
  424. private void changeRoomsList(List<string> roomsNames)
  425. {
  426. if (roomsNames.Count < 1) // so that the list will still be shown even if there are no available rooms
  427. {
  428. roomsNames.Add("");
  429. }
  430.  
  431. ListBox LBUsers = this.Controls.Find("LBUsers", false).FirstOrDefault() as ListBox;
  432. Label LBLUsers = this.Controls.Find("LBLUsers", false).FirstOrDefault() as Label;
  433.  
  434. // makes the users lists to disapear
  435. LBUsers.Visible = false;
  436. LBLUsers.Visible = false;
  437.  
  438. ListBox LBRooms = this.Controls.Find("LBRooms", false).FirstOrDefault() as ListBox;
  439. if (LBRooms != null)
  440. {
  441. LBRooms.Items.Clear();
  442. foreach (string name in roomsNames)
  443. {
  444. LBRooms.Items.Add(name);
  445. }
  446.  
  447. Label LBLRooms = this.Controls.Find("LBLRooms", false).FirstOrDefault() as Label;
  448. PictureBox PBRefresh = this.Controls.Find("PBRefresh", false).FirstOrDefault() as PictureBox;
  449.  
  450. // sets the height of the list box
  451. if (LBLRooms != null && PBRefresh != null)
  452. {
  453. int maxHeight = PBRefresh.Location.Y - LBLRooms.Location.Y - LBLRooms.Height - alligmentFromSubjectY;
  454. int height = TextRenderer.MeasureText("k", LBRooms.Font).Height;
  455. int maxObj = maxHeight / height;
  456.  
  457. LBRooms.Height = roomsNames.Count > maxObj ? height * maxObj : roomsNames.Count * height;
  458. }
  459.  
  460. // moves the list box to its proper place
  461. LBRooms.Location = new Point(this.Width / 2 - LBRooms.Width / 2, LBRooms.Location.Y);
  462. LBLRooms.Location = new Point(LBRooms.Location.X + LBRooms.Width / 2 - LBLRooms.Width / 2, LBLRooms.Location.Y);
  463.  
  464. this.Update();
  465. }
  466. }
  467.  
  468. /// <summary>
  469. /// changes the users in room list box
  470. /// should be called when an item on the rooms list is clicked
  471. /// </summary>
  472. /// <param name="users">the users that will be presented on the users list</param>
  473. private void changeUsersInRoom(List<string> users)
  474. {
  475. if(users.Count < 1)
  476. {
  477. users.Add("");
  478. }
  479.  
  480. ListBox LBUsers = this.Controls.Find("LBUsers", false).FirstOrDefault() as ListBox;
  481. Label LBLUsers = this.Controls.Find("LBLUsers", false).FirstOrDefault() as Label;
  482. Label LBLRooms = this.Controls.Find("LBLRooms", false).FirstOrDefault() as Label;
  483. PictureBox PBRefresh = this.Controls.Find("PBRefresh", false).FirstOrDefault() as PictureBox;
  484. ListBox LBRooms = this.Controls.Find("LBRooms", false).FirstOrDefault() as ListBox;
  485.  
  486. // sets the height of the list box
  487. if (LBLUsers != null && PBRefresh != null && LBUsers != null)
  488. {
  489. int maxHeight = PBRefresh.Location.Y - LBLUsers.Location.Y - LBLUsers.Height - alligmentFromSubjectY;
  490. int height = TextRenderer.MeasureText("k", LBUsers.Font).Height;
  491. int maxObj = maxHeight / height;
  492.  
  493. // changes the items in the list box
  494. LBUsers.Items.Clear();
  495. foreach(string user in users)
  496. {
  497. LBUsers.Items.Add(user);
  498. }
  499.  
  500. LBUsers.Height = users.Count > maxObj ? height * maxObj : users.Count * height;
  501.  
  502. // sets the rooms stuff to their proper place
  503. LBRooms.Location = new Point(this.Width - LBRooms.Width - LBUsers.Location.X, LBRooms.Location.Y);
  504. LBLRooms.Location = new Point(LBRooms.Location.X + LBRooms.Width / 2 - LBLRooms.Width / 2, LBLRooms.Location.Y);
  505.  
  506. LBUsers.Visible = true;
  507. LBLUsers.Visible = true;
  508.  
  509. this.Update();
  510. }
  511. }
  512.  
  513. /// <summary>
  514. /// changes the form to personal status
  515. /// </summary>
  516. /// <param name="sender"></param>
  517. /// <param name="args"></param>
  518. private void InitPersonalStatus(object sender, EventArgs args)
  519. {
  520. int reminingY = this.PBClose.Location.Y;
  521. int nextYPos = 0;
  522. int alligmentFromLabelsY = 0;
  523.  
  524. // sets the background of the form to personal status background
  525. this.BackgroundImage = Properties.Resources.personalStatusBG;
  526.  
  527. // sets the font of the form to black
  528. this.ForeColor = Color.Black;
  529.  
  530. // sets the subject label
  531. Label LBLSubject = new MyLabel("My Performance: ", "LBLSubject"); // i create a label to know the width of its text
  532. LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
  533. this.Controls.Add(LBLSubject);
  534.  
  535. nextYPos = LBLSubject.Location.Y + LBLSubject.Height + alligmentFromSubjectY;
  536. reminingY -= nextYPos;
  537.  
  538. Label LBLnumOfGames = new MyLabel("number of Games: ", "LBLnumOfGames"); // i create a label so i can know the height of it
  539.  
  540. alligmentFromLabelsY = (reminingY - LBLnumOfGames.Height * 4) / 5;
  541. nextYPos += alligmentFromLabelsY;
  542.  
  543. // configures and adds the number of games label
  544. 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
  545. this.Controls.Add(LBLnumOfGames);
  546.  
  547. nextYPos += alligmentFromLabelsY;
  548.  
  549. // configures and adds the number of right answers label
  550. Label LBLnumOfRightAns = new MyLabel("number of right answers: ", "LBLnumOfRightAns");
  551. LBLnumOfRightAns.Location = new Point(0, nextYPos);
  552. this.Controls.Add(LBLnumOfRightAns);
  553.  
  554. nextYPos += alligmentFromLabelsY;
  555.  
  556. // configures and adds the number of wrong answers label
  557. Label LBLnumOfWrongAns = new MyLabel("number of wrong answers: ", "LBLnumOfWrongAns");
  558. LBLnumOfWrongAns.Location = new Point(0, nextYPos);
  559. this.Controls.Add(LBLnumOfWrongAns);
  560.  
  561. nextYPos += alligmentFromLabelsY;
  562.  
  563. // configures and adds the average time per answer
  564. Label LBLavgTimePerAns = new MyLabel("average time for answer: ", "LBLavgTimePerAns");
  565. LBLavgTimePerAns.Location = new Point(0, nextYPos);
  566. this.Controls.Add(LBLavgTimePerAns);
  567.  
  568. // sets the back button
  569. int PCHeight = this.Height / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  570. int PCWidth = this.Width / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  571. PictureBox PBBack = new MyPictureBox("PBBack", "back", new Size(PCWidth, PCHeight));
  572. PBBack.Location = new Point(this.Width - PCWidth, this.Height / ALLIGMENT_FROM_TOP);
  573. PBBack.Click += (EventHandler)CleanAllControlers;
  574. PBBack.Click += (EventHandler)InitMainScreen;
  575. this.Controls.Add(PBBack);
  576. }
  577.  
  578. /// <summary>
  579. /// changes the labels of personal status' form, has to be be used after InitPersonalStatus function used
  580. /// </summary>
  581. /// <param name="arr">an array that contains the values of the fields example { 4, 5, 10, 3 }</param>
  582. private void ChangePersonalStatus(string[] arr)
  583. {
  584. if(arr.Length > 3)
  585. {
  586. Label[] labels = new Label[4];
  587. labels[0] = this.Controls.Find("LBLnumOfGames", false).FirstOrDefault() as Label;
  588. labels[1] = this.Controls.Find("LBLnumOfRightAns", false).FirstOrDefault() as Label;
  589. labels[2] = this.Controls.Find("LBLnumOfWrongAns", false).FirstOrDefault() as Label;
  590. labels[3] = this.Controls.Find("LBLavgTimePerAns", false).FirstOrDefault() as Label;
  591.  
  592. for(int i = 0; i < 4; i++)
  593. {
  594. if(labels[i] != null)
  595. {
  596. labels[i].Text += arr[i];
  597. labels[i].Width = TextRenderer.MeasureText(labels[i].Text, labels[i].Font).Width;
  598. labels[i].Location = new Point(this.Width / 2 - labels[i].Width / 2, labels[i].Location.Y);
  599. }
  600. }
  601. }
  602. }
  603.  
  604. /// <summary>
  605. /// changes the form to show the best scores
  606. /// </summary>
  607. /// <param name="sender"></param>
  608. /// <param name="args"></param>
  609. private void InitBestScores(object sender, EventArgs args)
  610. {
  611. int reminingY = PBClose.Location.Y;
  612. int nextYPos = 0;
  613. int pictureBoxHeight = 0;
  614. int pictureBoxWidth = 0;
  615. int alligmentFromPicturesY = 0;
  616. int alligmentFromPictureX = 0;
  617.  
  618. // sets the background image to best scores BG
  619. this.BackgroundImage = Properties.Resources.winners;
  620.  
  621. // sets the font of the form to black
  622. this.ForeColor = Color.Black;
  623.  
  624. // sets the subject label
  625. Label LBLSubject = new MyLabel("Best Scores: ", "LBLSubject"); // i create a label to know the width of its text
  626. LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
  627. this.Controls.Add(LBLSubject);
  628.  
  629. // creates the label that contains the first player's scores
  630. Label LBL1st = new MyLabel("ABCD", "LBL1st"); // i put text in the label so i can know its properties
  631.  
  632. // sets the next position for the picture boxes sector
  633. nextYPos += (LBLSubject.Location.Y + LBLSubject.Height + alligmentFromSubjectY);
  634. reminingY -= nextYPos;
  635.  
  636. // configures the properties of the picture boxes
  637. pictureBoxHeight = reminingY / BEST_SCORES_FORM_PB_HEIGHT;
  638. pictureBoxWidth = this.Width / BEST_SCORES_FORM_PB_WIDTH;
  639.  
  640. // configures the picture box sector
  641. alligmentFromPicturesY = (reminingY - pictureBoxHeight * 2 - LBL1st.Height * 2) / 2;
  642. alligmentFromPictureX = (this.Width - pictureBoxWidth * 3) / 4;
  643. nextYPos += alligmentFromPicturesY;
  644.  
  645. // sets the first place picture box
  646. PictureBox PB1st = new MyPictureBox("PB1st", "_1st", new Size(pictureBoxWidth, pictureBoxHeight));
  647. PB1st.Location = new Point(alligmentFromPictureX * 2 + pictureBoxWidth, nextYPos);
  648. this.Controls.Add(PB1st);
  649.  
  650. LBL1st.Location = new Point(PB1st.Location.X + PB1st.Width / 2, nextYPos - LBL1st.Height);
  651. this.Controls.Add(LBL1st);
  652.  
  653. // sets the postion to the next picture box
  654. nextYPos += alligmentFromPicturesY;
  655.  
  656. // sets the second best score picture box
  657. PictureBox PB2nd = new MyPictureBox("PB2nd", "_2nd", new Size(pictureBoxWidth, pictureBoxHeight));
  658. PB2nd.Location = new Point(alligmentFromPictureX, nextYPos);
  659. this.Controls.Add(PB2nd);
  660.  
  661. // sets the label of the second best score
  662. Label LBL2nd = new MyLabel("ABCD", "LBL2nd"); // i put text in the label so i can know its properties
  663. LBL2nd.Location = new Point(PB2nd.Location.X + PB2nd.Width / 2, PB2nd.Location.Y - LBL2nd.Height);
  664. this.Controls.Add(LBL2nd);
  665.  
  666. // sets the picture box of the second best score
  667. PictureBox PB3rd = new MyPictureBox("PB3rd", "_3rd", new Size(pictureBoxWidth, pictureBoxHeight));
  668. PB3rd.Location = new Point(this.Width - alligmentFromPictureX - PB3rd.Width, nextYPos);
  669. this.Controls.Add(PB3rd);
  670.  
  671. // sets the label of th ethird best score
  672. Label LBL3rd = new MyLabel("ABCD", "LBL3rd"); // i put text in the label so i can know its properties
  673. LBL3rd.Location = new Point(PB3rd.Location.X + PB3rd.Width / 2, nextYPos - LBL3rd.Height);
  674. this.Controls.Add(LBL3rd);
  675.  
  676. // sets the back button
  677. int PCHeight = this.Height / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  678. int PCWidth = this.Width / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
  679. PictureBox PBBack = new MyPictureBox("PBBack", "back", new Size(PCWidth, PCHeight));
  680. PBBack.Location = new Point(this.Width - PCWidth, this.Height / ALLIGMENT_FROM_TOP);
  681. PBBack.Click += (EventHandler)CleanAllControlers;
  682. PBBack.Click += (EventHandler)InitMainScreen;
  683. this.Controls.Add(PBBack);
  684. }
  685.  
  686. /// <summary>
  687. /// changes the best scores labels
  688. /// should be used when clicked on get best scores button, after InitBestScores was called
  689. /// </summary>
  690. /// <param name="arr">an array of pairs in which the key is the name of the user and the value is his score</param>
  691. private void changeBestScores(KeyValuePair<string, string>[] arr)
  692. {
  693. Label[] labels = new Label[3];
  694.  
  695. labels[0] = this.Controls.Find("LBL1st", false).FirstOrDefault() as Label;
  696. labels[1] = this.Controls.Find("LBL2nd", false).FirstOrDefault() as Label;
  697. labels[2] = this.Controls.Find("LBL3rd", false).FirstOrDefault() as Label;
  698.  
  699. for (int i = 0; i < 3; i++ )
  700. {
  701. if(labels[i] != null)
  702. {
  703. labels[i].Text = arr[i].Key + ": " + arr[i].Value;
  704. labels[i].Width = TextRenderer.MeasureText(labels[i].Text, labels[i].Font).Width;
  705. labels[i].Location = new Point(labels[i].Location.X - labels[i].Width / 2, labels[i].Location.Y);
  706. }
  707. }
  708. }
  709.  
  710. /// <summary>
  711. /// cleans the screen from all the controls on it
  712. /// </summary>
  713. private void CleanAllControlers(object sender, EventArgs args)
  714. {
  715. //foreach (Control c in this.Controls)
  716. //{
  717. // MessageBox.Show(c.GetType().ToString());
  718. // this.Controls.Remove(c);
  719. //}
  720.  
  721. this.Controls.Clear();
  722. this.Controls.Add(PBClose);
  723. }
  724.  
  725. private void PBClose_Click(object sender, EventArgs args)
  726. {
  727. this.Close();
  728. }
  729.  
  730. private void BTNBest_Click(object sender, EventArgs args)
  731. {
  732. // ... executed when best scores button clicked
  733. }
  734.  
  735. private void PBLogOut_Click(object sender, EventArgs args)
  736. {
  737. // ... executed when log out button is clicked
  738. }
  739.  
  740. private void PBRefresh_Click(object sender, EventArgs args)
  741. {
  742. // .. executed when refresh button is clicked
  743. }
  744.  
  745. private void PBJoin_Click(object sender, EventArgs args)
  746. {
  747. //... executed when join button clicked (on join rooms form)
  748. }
  749.  
  750. /// <summary>
  751. /// sends the username, password and email to server according to the format for sign up
  752. /// </summary>
  753. private void PBSign_Up_Click(object sender, EventArgs args)
  754. {
  755. TextBox txtUserName = (TextBox)this.Controls.Find("TXTusername", false)[0];
  756. TextBox txtPassword = (TextBox)this.Controls.Find("TXTpsw", false)[0];
  757. TextBox txtEmail = (TextBox)this.Controls.Find("TXTemail", false)[0];
  758.  
  759. if (txtUserName != null && txtPassword != null && txtEmail != null)
  760. {
  761. 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));
  762. }
  763. }
  764.  
  765. /// <summary>
  766. /// sends the username and password to server according to the format for sign in
  767. /// </summary>
  768. private void PBSign_In_Click(object sender, EventArgs e)
  769. {
  770. TextBox txtUserName = (TextBox)this.Controls.Find("TXTusername", false)[0];
  771. TextBox txtPassword = (TextBox)this.Controls.Find("TXTpsw", false)[0];
  772.  
  773. if (txtUserName != null && txtPassword != null)
  774. {
  775. this.stream.sendData(String.Format("200{0:00}{1}{2:00}{3}", txtUserName.Text.Length, txtUserName.Text, txtPassword.Text.Length, txtPassword.Text));
  776. }
  777. }
  778.  
  779. private void handleMessage()
  780. {
  781. string msg = "";
  782. while (true)
  783. {
  784. msg = this.stream.recvData();
  785.  
  786. switch (int.Parse(msg))
  787. {
  788. case 1020:
  789. MessageBox.Show("Success Sign in");
  790. uid = ((TextBox)this.Controls.Find("TXTusername", false)[0]).Text;
  791. Invoke((MethodInvoker) (() => CleanAllControlers(null, EventArgs.Empty)));
  792. Invoke((MethodInvoker) (() => InitMainScreen(null, EventArgs.Empty)));
  793. break;
  794. case 1021:
  795. MessageBox.Show("Wrong Details Sign in");
  796. break;
  797. case 1022:
  798. MessageBox.Show("User is already connected Sign in");
  799. break;
  800. case 1040:
  801. MessageBox.Show("Success Sign up");
  802. Invoke((MethodInvoker) (() => CleanAllControlers(null, EventArgs.Empty)));
  803. Invoke((MethodInvoker) (() => this.InitLoginScreen(null, EventArgs.Empty)));
  804. break;
  805. case 1041:
  806. MessageBox.Show("WPass illegal Sign up");
  807. break;
  808. case 1042:
  809. MessageBox.Show("Username is already exists Sign up");
  810. break;
  811. case 1043:
  812. MessageBox.Show("Username is illegal Sign up");
  813. break;
  814. case 1044:
  815. MessageBox.Show("Other Sign up");
  816. break;
  817. }
  818. }
  819. }
  820.  
  821. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  822. {
  823. //this.t.Abort();
  824. //this.stream.sendData("299");
  825. //this.stream.CloseStream();
  826. this.PBClose.Dispose();
  827. }
  828. }
  829. }
  830.  
  831.  
  832. // TODO:
  833. // set font according to resulution
  834. // remove the size of the Close button from the login and signup pages
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement