Advertisement
Guest User

Untitled

a guest
Sep 18th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 31.56 KB | None | 0 0
  1. package progettoas.hearts.client;
  2.  
  3. import progettoas.hearts.controller.Controller;
  4.  
  5. import com.google.gwt.core.client.EntryPoint;
  6. import com.google.gwt.event.dom.client.ChangeEvent;
  7. import com.google.gwt.event.dom.client.ChangeHandler;
  8. import com.google.gwt.event.dom.client.ClickEvent;
  9. import com.google.gwt.event.dom.client.ClickHandler;
  10. import com.google.gwt.event.dom.client.KeyCodes;
  11. import com.google.gwt.event.dom.client.KeyUpEvent;
  12. import com.google.gwt.event.dom.client.KeyUpHandler;
  13. import com.google.gwt.http.client.Request;
  14. import com.google.gwt.user.client.Command;
  15. import com.google.gwt.user.client.DOM;
  16. import com.google.gwt.user.client.Window;
  17. import com.google.gwt.user.client.ui.Button;
  18. import com.google.gwt.user.client.ui.DockPanel;
  19. import com.google.gwt.user.client.ui.Grid;
  20. import com.google.gwt.user.client.ui.HTML;
  21. import com.google.gwt.user.client.ui.Label;
  22. import com.google.gwt.user.client.ui.ListBox;
  23. import com.google.gwt.user.client.ui.MenuBar;
  24. import com.google.gwt.user.client.ui.MenuItem;
  25. import com.google.gwt.user.client.ui.Panel;
  26. import com.google.gwt.user.client.ui.PasswordTextBox;
  27. import com.google.gwt.user.client.ui.RadioButton;
  28. import com.google.gwt.user.client.ui.RootPanel;
  29. import com.google.gwt.user.client.ui.SimplePanel;
  30. import com.google.gwt.user.client.ui.SourcesTableEvents;
  31. import com.google.gwt.user.client.ui.TableListener;
  32. import com.google.gwt.user.client.ui.TextArea;
  33. import com.google.gwt.user.client.ui.TextBox;
  34. import com.google.gwt.user.client.ui.VerticalPanel;
  35. import com.google.gwt.user.client.ui.HasHorizontalAlignment;
  36. import com.google.gwt.user.client.ui.HasVerticalAlignment;
  37.  
  38. /**
  39. * Entry point classes define <code>onModuleLoad()</code>.
  40. */
  41. public class Hearts implements EntryPoint {
  42. static final int chatW = 300;
  43. static final int menuH = 40;
  44. static final int marginSize = 8;
  45. static final int cardW = 50;
  46. static final int cardH = 80;
  47. static final int nCard = 13;
  48. static final int LOGIN = 1;
  49. static final int WELCOME = 2;
  50. static final int PLAY_GAME = 3;
  51. static final int WATCH_GAME = 4;
  52. static final int STARTED_TAB = 5;
  53. static final int TOPSCORES = 6;
  54. static final int LOGUSR = 7;
  55. static final int PROFILE = 8;
  56. static final int HELP = 9;
  57. static final int ABOUT = 10;
  58. static final int STATE = 11;
  59. static final int NEWGAME = 12;
  60. static final int GAMEEND = 13;
  61. static final int TABLELIST_W = 14;
  62. static final int TABLELIST_P = 15;
  63.  
  64. int currentPanel = 0;
  65. Boolean isAuthenticated = false;
  66. Boolean isPlaying = false;
  67. int nbot;
  68. int NTable = 0;
  69. Controller controller = new Controller();
  70. SimplePanel init = new SimplePanel();
  71. DockPanel panel = new DockPanel();
  72. MenuBar menutop = new MenuBar();
  73. SimplePanel chatPanel = new SimplePanel();
  74. VerticalPanel loginPanel = new VerticalPanel();
  75. VerticalPanel welcomePanel = new VerticalPanel();
  76. VerticalPanel topscorePanel = new VerticalPanel();
  77. VerticalPanel logUsrPanel = new VerticalPanel();
  78. VerticalPanel profilePanel = new VerticalPanel();
  79. VerticalPanel helpPanel = new VerticalPanel();
  80. VerticalPanel aboutPanel = new VerticalPanel();
  81. VerticalPanel statePanel = new VerticalPanel();
  82. DockPanel gamePanel = new DockPanel();
  83. VerticalPanel newGamePanel = new VerticalPanel();
  84. VerticalPanel gameEndPanel = new VerticalPanel();
  85. VerticalPanel wTableListPanel = new VerticalPanel();
  86. VerticalPanel pTableListPanel = new VerticalPanel();
  87. DockPanel watchGamePanel = new DockPanel();
  88.  
  89. /**
  90. * The message displayed to the user when the server cannot be reached or
  91. * returns an error.
  92. */
  93. private static final String SERVER_ERROR = "An error occurred while "
  94. + "attempting to contact the server. Please check your network "
  95. + "connection and try again.";
  96.  
  97. /**
  98. * Create a remote service proxy to talk to the server-side Greeting service.
  99. */
  100. // private final GreetingServiceAsync greetingService = GWT
  101. // .create(GreetingService.class);
  102.  
  103. /**
  104. * This is the entry point method.
  105. */
  106. public void onModuleLoad() {
  107.  
  108. panel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
  109. panel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
  110. panel.setPixelSize(Window.getClientWidth()-marginSize, Window.getClientHeight()-marginSize);
  111. panel.addStyleName("panel");
  112. panel.add(init,DockPanel.WEST);
  113.  
  114. drawMenu();
  115. drawChatPanel();
  116.  
  117.  
  118. panel.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
  119. panel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
  120. panel.add(menutop, DockPanel.NORTH);
  121.  
  122.  
  123. panel.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
  124. panel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
  125. panel.add(chatPanel, DockPanel.WEST);
  126.  
  127.  
  128. panel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
  129. panel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
  130.  
  131. if (!isAuthenticated) {
  132. drawLoginPanel();
  133. renderNewPanel(loginPanel);
  134. currentPanel = LOGIN;
  135. } else {
  136. drawWelcomePanel();
  137. renderNewPanel(welcomePanel);
  138. currentPanel = WELCOME;
  139. }
  140.  
  141. RootPanel rootPanel = RootPanel.get("panelContainer");
  142. rootPanel.add(panel);
  143. }
  144.  
  145.  
  146.  
  147. private void renderNewPanel(Panel p) {
  148. Panel pp = null;
  149.  
  150. switch(currentPanel) {
  151. case LOGIN: pp = loginPanel; break;
  152. case WELCOME: pp = welcomePanel; break;
  153. case TOPSCORES: pp = topscorePanel; break;
  154. case LOGUSR: pp = logUsrPanel; break;
  155. case PROFILE: pp = profilePanel; break;
  156. case HELP: pp = helpPanel; break;
  157. case ABOUT: pp = aboutPanel; break;
  158. case STATE: pp = statePanel; break;
  159. case PLAY_GAME: pp = gamePanel; break;
  160. case NEWGAME: pp = newGamePanel; break;
  161. case GAMEEND: pp = gameEndPanel; break;
  162. case TABLELIST_W: pp = wTableListPanel; break;
  163. case TABLELIST_P: pp = pTableListPanel; break;
  164. case WATCH_GAME: pp = watchGamePanel; break;
  165. default: pp = init; break;
  166. }
  167.  
  168. pp.clear();
  169. panel.remove(pp);
  170. panel.add(p,DockPanel.WEST);
  171. }
  172.  
  173.  
  174.  
  175. private void drawMenu() {
  176. // Definition of commands to perform for each menu choice
  177. Command newGameCmd = new Command() {
  178. public void execute() {
  179. if (isPlaying) {
  180. Window.alert("You are playing game!");
  181. } else if (isAuthenticated) {
  182. drawNewGamePanel();
  183. renderNewPanel(newGamePanel);
  184. currentPanel = NEWGAME;
  185. } else {
  186. Window.alert("You must be logged to start a new game!");
  187. }
  188. }
  189. };
  190. Command joinGameCmd = new Command() {
  191. public void execute() {
  192. if (isPlaying) {
  193. Window.alert("You are playing a game!");
  194. } else if (isAuthenticated) {
  195. //TODO: ask model table list (not playing yet)
  196. int ntable = 8;
  197. drawPTableListPanel(ntable);
  198. renderNewPanel(pTableListPanel);
  199. currentPanel = TABLELIST_P;
  200. } else {
  201. Window.alert("You must be logged to join a table!");
  202. }
  203. }
  204. };
  205. Command leaveGameCmd = new Command() {
  206. public void execute() {
  207. if (!isPlaying) {
  208. Window.alert("You are not playing a game!");
  209. } else if (isAuthenticated) {
  210. isPlaying = false;
  211. drawWelcomePanel();
  212. renderNewPanel(welcomePanel);
  213. currentPanel = WELCOME;
  214. } else {
  215. Window.alert("You must be logged to leave a game!");
  216. }
  217. }
  218. };
  219. Command watchGameCmd = new Command() {
  220. public void execute() {
  221. if (isPlaying) {
  222. Window.alert("You are playing a game!");
  223. } else if (isAuthenticated) {
  224. //TODO: ask model table list
  225. int ntable = 5;
  226. drawWTableListPanel(ntable);
  227. renderNewPanel(wTableListPanel);
  228. currentPanel = TABLELIST_W;
  229. } else {
  230. Window.alert("You must be logged to watch a game!");
  231. }
  232. }
  233. };
  234.  
  235. Command showTopCmd = new Command() {
  236. public void execute() {
  237. if (isPlaying) {
  238. Window.alert("You are playing a game!");
  239. } else {
  240. //TODO action: ask model for info about top 10 players
  241. drawTopScoresPanel();
  242. renderNewPanel(topscorePanel);
  243. currentPanel = TOPSCORES;
  244. }
  245. }
  246. };
  247. Command showUsrCmd = new Command() {
  248. public void execute() {
  249. if (isPlaying) {
  250. Window.alert("You are playing a game!");
  251. } else if (isAuthenticated) {
  252. //TODO action: ask model list of logged users
  253. int nusr = 5;
  254. drawLoggedUsersPanel(nusr);
  255. renderNewPanel(logUsrPanel);
  256. currentPanel = LOGUSR;
  257. } else {
  258. Window.alert("You must be logged to see online users!");
  259. }
  260. }
  261. };
  262. Command gameStateCmd = new Command() {
  263. public void execute() {
  264. if (isPlaying) {
  265. Window.alert("You are playing a game!");
  266. } else if (isAuthenticated) {
  267. //TODO action: ask model game state
  268. drawStatePanel();
  269. renderNewPanel(statePanel);
  270. currentPanel = STATE;
  271. } else {
  272. Window.alert("You must be logged to see game state!");
  273. }
  274. }
  275. };
  276.  
  277. Command showInfoCmd = new Command() {
  278. public void execute() {
  279. if (isPlaying) {
  280. Window.alert("You are playing a game!");
  281. } else if (isAuthenticated) {
  282. //TODO action: ask model player's profile info
  283. drawProfilePanel();
  284. renderNewPanel(profilePanel);
  285. currentPanel = PROFILE;
  286. } else {
  287. Window.alert("You must be logged to see your profile!");
  288. }
  289. }
  290. };
  291. Command logOutCmd = new Command() {
  292. public void execute() {
  293. if (isPlaying) {
  294. Window.alert("You are playing a game!");
  295. } else if (isAuthenticated) {
  296. Window.alert("LOG OUT!");
  297. isAuthenticated = false;
  298. drawLoginPanel();
  299. renderNewPanel(loginPanel);
  300. currentPanel = LOGIN;
  301. } else {
  302. Window.alert("You must be logged to log out!");
  303. }
  304. }
  305. };
  306. Command logInCmd = new Command() {
  307. public void execute() {
  308. if (isPlaying) {
  309. Window.alert("You are playing a game!");
  310. } else if (isAuthenticated) {
  311. Window.alert("You are already logged!");
  312. } else {
  313. drawLoginPanel();
  314. renderNewPanel(loginPanel);
  315. currentPanel = LOGIN;
  316. }
  317. }
  318. };
  319.  
  320. Command rulesCmd = new Command() {
  321. public void execute() {
  322. if (isPlaying) {
  323. Window.alert("You are playing a game!");
  324. } else {
  325. drawHelpPanel();
  326. renderNewPanel(helpPanel);
  327. currentPanel = HELP;
  328. }
  329. }
  330. };
  331. Command aboutCmd = new Command() {
  332. public void execute() {
  333. if (isPlaying) {
  334. Window.alert("You are playing a game!");
  335. } else {
  336.  
  337. drawAboutPanel();
  338. renderNewPanel(aboutPanel);
  339. currentPanel = ABOUT;
  340. }
  341. }
  342. };
  343.  
  344.  
  345. // Definition of menu and menu items
  346. menutop.addStyleName("menuItem");
  347.  
  348.  
  349. MenuBar gameMenu = new MenuBar(true);
  350. MenuItem newGame = new MenuItem("New Game", newGameCmd);
  351. MenuItem joinGame = new MenuItem("Join Game", joinGameCmd);
  352. MenuItem leaveGame = new MenuItem("Leave Game", leaveGameCmd);
  353. MenuItem watchGame = new MenuItem("Watch Game", watchGameCmd);
  354. gameMenu.addItem(newGame);
  355. gameMenu.addItem(joinGame);
  356. gameMenu.addItem(leaveGame);
  357. gameMenu.addItem(watchGame);
  358. gameMenu.addStyleName("menuItem");
  359. menutop.addItem("Game", gameMenu);
  360.  
  361.  
  362. MenuBar statMenu = new MenuBar(true);
  363. MenuItem showTop = new MenuItem("Top 10 players", showTopCmd);
  364. MenuItem showUsr = new MenuItem("Logged Users", showUsrCmd);
  365. MenuItem showState = new MenuItem("Game State", gameStateCmd);
  366. statMenu.addItem(showTop);
  367. statMenu.addItem(showUsr);
  368. statMenu.addItem(showState);
  369. statMenu.addStyleName("menuItem");
  370. menutop.addItem("Stats", statMenu);
  371.  
  372.  
  373. MenuBar profMenu = new MenuBar(true);
  374. MenuItem showInfo = new MenuItem("Show Profile Info", showInfoCmd);
  375. MenuItem logOut = new MenuItem("Logout", logOutCmd);
  376. MenuItem logIn = new MenuItem("Login", logInCmd);
  377. profMenu.addItem(showInfo);
  378. profMenu.addItem(logOut);
  379. profMenu.addItem(logIn);
  380. profMenu.addStyleName("menuItem");
  381. menutop.addItem("Profile", profMenu);
  382.  
  383.  
  384. MenuBar helpMenu = new MenuBar(true);
  385. MenuItem rules = new MenuItem("Game Rules", rulesCmd);
  386. MenuItem about = new MenuItem("About", aboutCmd);
  387. helpMenu.addItem(rules);
  388. helpMenu.addItem(about);
  389. helpMenu.addStyleName("menuItem");
  390. menutop.addItem("Help", helpMenu);
  391. }
  392.  
  393. private void drawChatPanel() {
  394. final Label title = new Label("CHAT");
  395.  
  396. chatPanel.add(title);
  397. chatPanel.addStyleName("chatPanel");
  398. chatPanel.setPixelSize(chatW, Window.getClientHeight()-menuH-marginSize);
  399. }
  400.  
  401. private void drawLoginPanel() {
  402. final Grid gr = new Grid(3,2);
  403. final Label loginLab = new Label("Please login to play Hearts!");
  404. final Label userLab = new Label("User:");
  405. final Label passLab = new Label("Password:");
  406. final TextBox userField = new TextBox();
  407. final PasswordTextBox passField = new PasswordTextBox();
  408. final Button loginButton = new Button("Login");
  409. loginButton.addStyleName("button");
  410. loginButton.setFocus(true);
  411.  
  412. loginLab.addStyleName("title");
  413.  
  414. gr.setWidget(0,0, userLab);
  415. gr.setWidget(0,1, userField);
  416. gr.setWidget(1,0, passLab);
  417. gr.setWidget(1,1, passField);
  418. gr.setWidget(2,1, loginButton);
  419.  
  420. loginPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
  421. loginPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
  422. loginPanel.add(loginLab);
  423. loginPanel.add(gr);
  424.  
  425.  
  426. loginButton.addClickHandler(new ClickHandler() {
  427. public void onClick(ClickEvent event) {
  428. checkAuthentication(userField, passField);
  429. }
  430. });
  431. passField.addKeyUpHandler(new KeyUpHandler() {
  432. public void onKeyUp(KeyUpEvent event) {
  433. if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
  434. checkAuthentication(userField, passField);
  435. }
  436. }
  437. });
  438.  
  439.  
  440.  
  441. }
  442.  
  443. private void checkAuthentication(TextBox user,
  444. PasswordTextBox pass) {
  445. //TODO: connect to server and check if user,pass are ok
  446.  
  447. isAuthenticated = true;
  448. user.setText("");
  449. pass.setText("");
  450.  
  451. drawWelcomePanel();
  452. renderNewPanel(welcomePanel);
  453. currentPanel = WELCOME;
  454. }
  455.  
  456.  
  457.  
  458. private void drawWelcomePanel() {
  459. Label welcome = new Label("WELCOME TO HEARTS GAME:");
  460. Label welcome2 = new Label("ENJOY!!");
  461. welcome.addStyleName("title");
  462. welcome2.addStyleName("title");
  463.  
  464. welcomePanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
  465. welcomePanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
  466.  
  467. welcomePanel.add(welcome);
  468. welcomePanel.add(welcome2);
  469. }
  470.  
  471. private void drawTopScoresPanel() {
  472. Grid gr = new Grid(11,5);
  473. Label pos = new Label("Position");
  474. Label name = new Label("Username");
  475. Label played = new Label("Played Games");
  476. Label won = new Label("Won Games");
  477. Label score = new Label("Score");
  478. gr.setWidget(0, 0, pos);
  479. gr.setWidget(0,1, name);
  480. gr.setWidget(0,2, played);
  481. gr.setWidget(0,3, won);
  482. gr.setWidget(0,4, score);
  483. for(int i=1;i<=10;i++){ gr.setWidget(i, 0, new Label(Integer.toString(i))); }
  484. gr.setSize("700px", "500px");
  485. gr.addStyleName("table-center");
  486. gr.setCellSpacing(3);
  487.  
  488. Label title = new Label("TOP SCORES");
  489. title.addStyleName("title");
  490.  
  491. topscorePanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
  492. topscorePanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
  493. topscorePanel.add(title);
  494. topscorePanel.add(gr);
  495. }
  496.  
  497.  
  498. private void drawLoggedUsersPanel(int n_usr) {
  499. Grid gr = new Grid(n_usr,3);
  500. Label name = new Label("Username");
  501. Label state = new Label("User State");
  502. Label score = new Label("Score");
  503. gr.setWidget(0,0, name);
  504. gr.setWidget(0,1, state);
  505. gr.setWidget(0,2, score);
  506. //for(int i=1;i<n_usr;i++){ gr.setWidget(i, 0, new Label()); } //write user infoin the grid
  507. gr.setSize("400px", Integer.toString(n_usr*50)+"px");
  508. gr.addStyleName("table-center");
  509. gr.setCellSpacing(3);
  510. gr.addStyleName("grid");
  511.  
  512. Label title = new Label("USERS ONLINE");
  513. title.addStyleName("title");
  514.  
  515. logUsrPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
  516. logUsrPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
  517. logUsrPanel.add(title);
  518. logUsrPanel.add(gr);
  519. }
  520.  
  521.  
  522.  
  523. private void drawProfilePanel() {
  524. Grid gr = new Grid(5,2);
  525. Label name = new Label("Username");
  526. Label state = new Label("User State");
  527. Label played = new Label("Played Games");
  528. Label won = new Label("Won Games");
  529. Label score = new Label("Score");
  530. gr.setWidget(0,0, name);
  531. gr.setWidget(1,0, state);
  532. gr.setWidget(2,0, played);
  533. gr.setWidget(3,0, won);
  534. gr.setWidget(4,0, score);
  535. //for(int i=1;i<n_usr;i++){ gr.setWidget(i, 0, new Label()); } //write user info in the grid
  536. gr.setSize("300px", "400px");
  537. gr.addStyleName("table-center");
  538. gr.setCellSpacing(3);
  539. gr.addStyleName("grid");
  540.  
  541. Label title = new Label("PROFILE");
  542. title.addStyleName("title");
  543.  
  544. profilePanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
  545. profilePanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
  546. profilePanel.add(title);
  547. profilePanel.add(gr);
  548. }
  549.  
  550. private void drawHelpPanel() {
  551. TextArea text = new TextArea();
  552. text.setSize("500px", "400px");
  553. text.setText("Testo di esempio per provare se funziona.\nA capo\nprova\nHELP sul gioco");
  554. text.addStyleName("gwt-TextArea-readonly");
  555.  
  556. Label title = new Label("HELP");
  557. title.addStyleName("title");
  558.  
  559. helpPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
  560. helpPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
  561. helpPanel.add(title);
  562. helpPanel.add(text);
  563. }
  564.  
  565.  
  566. private void drawAboutPanel() {
  567. TextArea text = new TextArea();
  568. text.setSize("500px", "400px");
  569. text.setText("Testo di esempio per provare se funziona.\nA capo\nprova\nINFO about the developers");
  570. text.addStyleName("gwt-TextArea-readonly");
  571.  
  572. Label title = new Label("ABOUT");
  573. title.addStyleName("title");
  574.  
  575. aboutPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
  576. aboutPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
  577. aboutPanel.add(title);
  578. aboutPanel.add(text);
  579. }
  580.  
  581.  
  582. private void drawStatePanel() {
  583. Grid gr = new Grid(4,2);
  584. Label onlineUsr = new Label("Number of Logged Users");
  585. Label playingUsr = new Label("Number of Playing Users");
  586. Label createdTbl = new Label("Number of Created Tables");
  587. Label playingTbl = new Label("Number of Started Tables");
  588. gr.setWidget(0, 0, onlineUsr);
  589. gr.setWidget(1, 0, playingUsr);
  590. gr.setWidget(2, 0, createdTbl);
  591. gr.setWidget(3, 0, playingTbl);
  592. gr.setSize("600px", "300px");
  593. gr.addStyleName("table-center");
  594. gr.setCellSpacing(3);
  595. gr.addStyleName("grid");
  596.  
  597. Label title = new Label("GAME STATE");
  598. title.addStyleName("title");
  599.  
  600. statePanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
  601. statePanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
  602. statePanel.add(title);
  603. statePanel.add(gr);
  604. }
  605.  
  606. @SuppressWarnings("deprecation")
  607. private void drawGamePanel(){
  608. gamePanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
  609. gamePanel.setPixelSize(Window.getClientWidth()-chatW-marginSize,Window.getClientHeight()-menuH-marginSize);
  610. gamePanel.addStyleName("game");
  611. VerticalPanel vPanel = new VerticalPanel();
  612. vPanel.setPixelSize(Window.getClientWidth()-chatW-marginSize,200);
  613. final Grid gr = new Grid(1,nCard);
  614. gr.setPixelSize(cardW*nCard, cardH);
  615. for (int i=0;i<nCard;i++){
  616. gr.getCellFormatter().setStyleName(0, i, "card");
  617. DOM.setStyleAttribute(gr.getCellFormatter().getElement(0, i),
  618. "border", "1px solid #000");
  619. }
  620.  
  621.  
  622. Label plName = new Label("Logged User"); //TODO: get player name from the model
  623. vPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
  624. vPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
  625. vPanel.add(gr);
  626. vPanel.add(plName);
  627.  
  628. gamePanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
  629. gamePanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
  630. gamePanel.add(vPanel,DockPanel.SOUTH);
  631.  
  632. Label pl2 = new Label("Player2");
  633. Label pl3 = new Label("Player3");
  634. Label pl4 = new Label("Player4");
  635. final SimplePanel card1 = new SimplePanel(); card1.setPixelSize(cardW, cardH); card1.addStyleName("card");
  636. final SimplePanel card2 = new SimplePanel(); card2.setPixelSize(cardW, cardH); card2.addStyleName("card");
  637. final SimplePanel card3 = new SimplePanel(); card3.setPixelSize(cardW, cardH); card3.addStyleName("card");
  638. final SimplePanel card4 = new SimplePanel(); card4.setPixelSize(cardW, cardH); card4.addStyleName("card");
  639. gamePanel.add(pl2, DockPanel.WEST);
  640. gamePanel.setCellVerticalAlignment(pl2, HasVerticalAlignment.ALIGN_MIDDLE);
  641. gamePanel.setCellHorizontalAlignment(pl2, HasHorizontalAlignment.ALIGN_CENTER);
  642. gamePanel.add(pl4, DockPanel.EAST);
  643. gamePanel.setCellVerticalAlignment(pl4, HasVerticalAlignment.ALIGN_MIDDLE);
  644. gamePanel.setCellHorizontalAlignment(pl4, HasHorizontalAlignment.ALIGN_CENTER);
  645. gamePanel.add(pl3, DockPanel.NORTH);
  646. gamePanel.setCellVerticalAlignment(pl3, HasVerticalAlignment.ALIGN_MIDDLE);
  647. gamePanel.setCellHorizontalAlignment(pl3, HasHorizontalAlignment.ALIGN_CENTER);
  648. gamePanel.add(card1, DockPanel.SOUTH);
  649. gamePanel.setCellVerticalAlignment(card1, HasVerticalAlignment.ALIGN_BOTTOM);
  650. gamePanel.setCellHorizontalAlignment(card1, HasHorizontalAlignment.ALIGN_CENTER);
  651. gamePanel.add(card2, DockPanel.WEST);
  652. gamePanel.setCellVerticalAlignment(card2, HasVerticalAlignment.ALIGN_BOTTOM);
  653. gamePanel.setCellHorizontalAlignment(card2, HasHorizontalAlignment.ALIGN_CENTER);
  654. gamePanel.add(card4, DockPanel.EAST);
  655. gamePanel.setCellVerticalAlignment(card4, HasVerticalAlignment.ALIGN_BOTTOM);
  656. gamePanel.setCellHorizontalAlignment(card4, HasHorizontalAlignment.ALIGN_CENTER);
  657. gamePanel.add(card3, DockPanel.NORTH);
  658. gamePanel.setCellHorizontalAlignment(card3, HasHorizontalAlignment.ALIGN_CENTER);
  659.  
  660. gr.addTableListener(new TableListener(){
  661. @Override
  662. public void onCellClicked(SourcesTableEvents sender, int row, int cell){
  663. DOM.setStyleAttribute(((Grid) sender).getCellFormatter().getElement(row, cell),
  664. "border", "1px solid #f00");
  665. for (int i=0;i<nCard;i++){
  666. if (i!=cell) {
  667. DOM.setStyleAttribute(((Grid) sender).getCellFormatter().getElement(row, i),
  668. "border", "1px solid #000");
  669. }
  670. }
  671. controller.playACard("pietro", "aCard", "aTable");
  672. // TODO: mostra in card1 la carta selezionata
  673. System.out.println("scelta carta "+cell);
  674. }
  675. });
  676.  
  677. }
  678.  
  679. private void drawNewGamePanel(){
  680. Grid gr = new Grid(1,2);
  681. Label nbotLbl = new Label("Select the namber of authomatic players (bot):");
  682. final ListBox nbotList = new ListBox();
  683. nbotList.addItem("None");
  684. nbotList.addItem("One");
  685. nbotList.addItem("Two");
  686. nbotList.addItem("Three");
  687.  
  688. nbotList.addChangeHandler(new ChangeHandler(){
  689. @Override
  690. public void onChange(ChangeEvent event) {
  691. nbot = nbotList.getSelectedIndex();
  692. }
  693. });
  694.  
  695. gr.setWidget(0, 0, nbotLbl);
  696. gr.setWidget(0, 1, nbotList);
  697. gr.addStyleName("table-center");
  698. gr.setCellSpacing(3);
  699. //gr.addStyleName("grid");
  700.  
  701. Label title = new Label("CREATE NEW TABLE");
  702. title.addStyleName("title");
  703.  
  704. Button button = new Button("Create");
  705.  
  706. newGamePanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
  707. newGamePanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
  708. newGamePanel.add(title);
  709. newGamePanel.add(gr);
  710. newGamePanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
  711. newGamePanel.add(button);
  712.  
  713. button.addClickHandler(new ClickHandler() {
  714. public void onClick(ClickEvent event) {
  715. String[] ai = new String[nbot];
  716. for (int i=0;i<nbot;i++){ ai[i] = "a"; }
  717. String user = "mycol";
  718. controller.createATable(user, nbot, ai);
  719. drawGamePanel();
  720. renderNewPanel(gamePanel);
  721. currentPanel = PLAY_GAME;
  722. isPlaying = true;
  723. }
  724. });
  725.  
  726. }
  727.  
  728. /* alla fine della partita: mostra punteggi e chiede se vuoi ricominciare */
  729. private void drawGameEndPanel(){
  730. isPlaying = false;
  731. Grid gr = new Grid(2, 5);
  732. Label pl1 = new Label("Player 1");
  733. Label pl2 = new Label("Player 2");
  734. Label pl3 = new Label("Player 3");
  735. Label pl4 = new Label("Player 4");
  736. Label name = new Label("Username");
  737. Label score = new Label("Score");
  738. gr.setWidget(0, 0, name);
  739. gr.setWidget(0, 1, score);
  740. gr.setWidget(1, 0, pl1);
  741. gr.setWidget(2, 0, pl2);
  742. gr.setWidget(3, 0, pl3);
  743. gr.setWidget(4, 0, pl4);
  744.  
  745. gr.addStyleName("table-center");
  746. gr.setCellSpacing(3);
  747.  
  748. Label title = new Label("GAME FINISHED");
  749. title.addStyleName("title");
  750.  
  751. Button button = new Button("Play Again");
  752.  
  753. gameEndPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
  754. gameEndPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
  755. gameEndPanel.add(title);
  756. gameEndPanel.add(gr);
  757. gameEndPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
  758. gameEndPanel.add(button);
  759.  
  760. button.addClickHandler(new ClickHandler() {
  761. public void onClick(ClickEvent event) {
  762. drawGamePanel();
  763. renderNewPanel(gamePanel);
  764. currentPanel = PLAY_GAME;
  765. isPlaying = true;
  766. }
  767. });
  768.  
  769. }
  770.  
  771.  
  772.  
  773. private void drawWTableListPanel(int nTable){
  774. NTable = nTable;
  775. Grid gr = new Grid(nTable+1, 3);
  776. Label idL = new Label("Table ID");
  777. Label botL = new Label("Bot Number");
  778. Label freeL = new Label("Number of free spots");
  779. final String id[] = new String[nTable];
  780. int nbot[] = new int[nTable];
  781. int free[] = new int[nTable];
  782. Button button = new Button("Watch");
  783. final RadioButton rb[] = new RadioButton[nTable];
  784.  
  785.  
  786. gr.setWidget(0, 0, idL);
  787. gr.setWidget(0, 1, botL);
  788. gr.setWidget(0, 2, freeL);
  789.  
  790. for (int i=0;i<nTable;i++){
  791. //TODO: get table info from model
  792. id[i] = "id";
  793. nbot[i] = 0;
  794. free[i] = 1;
  795. rb[i] = new RadioButton("radioGroup",id[i]);
  796. gr.setWidget(i+1, 0, rb[i]);
  797. gr.setWidget(i+1, 1, new Label(Integer.toString(nbot[i])));
  798. gr.setWidget(i+1, 2, new Label(Integer.toString(free[i])));
  799. }
  800.  
  801.  
  802. gr.setPixelSize(500, nTable*50);
  803. gr.addStyleName("table-center");
  804. gr.setCellSpacing(3);
  805.  
  806. Label title = new Label("SELECT A TABLE:");
  807. title.addStyleName("title");
  808.  
  809.  
  810. wTableListPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
  811. wTableListPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
  812. wTableListPanel.add(title);
  813. wTableListPanel.add(gr);
  814. wTableListPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
  815. wTableListPanel.add(button);
  816.  
  817. button.addClickHandler(new ClickHandler() {
  818. public void onClick(ClickEvent event) {
  819. int tableid = -1;
  820. for (int i=0;i<NTable;i++){
  821. if (rb[i].getValue()) {
  822. tableid = i;
  823. break;
  824. }
  825. }
  826. if (tableid == -1) {
  827. Window.alert("Select a table!!");
  828. } else {
  829. System.out.println("index = "+tableid+" ID ="+id[tableid]);
  830. drawWatchGamePanel();
  831. renderNewPanel(watchGamePanel);
  832. currentPanel = WATCH_GAME;
  833. }
  834. }
  835. });
  836.  
  837. }
  838.  
  839. private void drawPTableListPanel(int nTable){
  840. NTable = nTable;
  841. Grid gr = new Grid(nTable+1, 3);
  842. Label idL = new Label("Table ID");
  843. Label botL = new Label("Bot Number");
  844. Label freeL = new Label("Number of free spots");
  845. final String id[] = new String[nTable];
  846. int nbot[] = new int[nTable];
  847. int free[] = new int[nTable];
  848. Button button = new Button("Play");
  849. final RadioButton rb[] = new RadioButton[nTable];
  850.  
  851.  
  852. gr.setWidget(0, 0, idL);
  853. gr.setWidget(0, 1, botL);
  854. gr.setWidget(0, 2, freeL);
  855.  
  856. for (int i=0;i<nTable;i++){
  857. //TODO: get table info from model
  858. id[i] = "tavolo";
  859. nbot[i] = 0;
  860. free[i] = 1;
  861. rb[i] = new RadioButton("radioGroup",id[i]);
  862. gr.setWidget(i+1, 0, rb[i]);
  863. gr.setWidget(i+1, 1, new Label(Integer.toString(nbot[i])));
  864. gr.setWidget(i+1, 2, new Label(Integer.toString(free[i])));
  865. }
  866.  
  867.  
  868. gr.setPixelSize(500, nTable*50);
  869. gr.addStyleName("table-center");
  870. gr.setCellSpacing(3);
  871.  
  872. Label title = new Label("SELECT A TABLE:");
  873. title.addStyleName("title");
  874.  
  875.  
  876. pTableListPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
  877. pTableListPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
  878. pTableListPanel.add(title);
  879. pTableListPanel.add(gr);
  880. pTableListPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
  881. pTableListPanel.add(button);
  882.  
  883. button.addClickHandler(new ClickHandler() {
  884. public void onClick(ClickEvent event) {
  885. int tableid = -1;
  886. for (int i=0;i<NTable;i++){
  887. if (rb[i].getValue()) {
  888. tableid = i;
  889. break;
  890. }
  891. }
  892. if (tableid == -1) {
  893. Window.alert("Select a table!!");
  894. } else {
  895. System.out.println("index = "+tableid+" ID ="+id[tableid]);
  896. controller.joinATable("pietro", id[tableid]);
  897. drawGamePanel();
  898. renderNewPanel(gamePanel);
  899. currentPanel = PLAY_GAME;
  900. isPlaying = true;
  901. }
  902. }
  903. });
  904.  
  905. }
  906.  
  907.  
  908. private void drawWatchGamePanel() {
  909. watchGamePanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
  910. watchGamePanel.setPixelSize(Window.getClientWidth()-chatW-marginSize,Window.getClientHeight()-menuH-marginSize);
  911. watchGamePanel.addStyleName("game");
  912.  
  913. Label pl1 = new Label("Player1");
  914. Label pl2 = new Label("Player2");
  915. Label pl3 = new Label("Player3");
  916. Label pl4 = new Label("Player4");
  917.  
  918. SimplePanel card1 = new SimplePanel(); card1.setPixelSize(cardW, cardH); card1.addStyleName("card");
  919. SimplePanel card2 = new SimplePanel(); card2.setPixelSize(cardW, cardH); card2.addStyleName("card");
  920. SimplePanel card3 = new SimplePanel(); card3.setPixelSize(cardW, cardH); card3.addStyleName("card");
  921. SimplePanel card4 = new SimplePanel(); card4.setPixelSize(cardW, cardH); card4.addStyleName("card");
  922.  
  923. watchGamePanel.add(pl1,DockPanel.SOUTH);
  924. watchGamePanel.setCellVerticalAlignment(pl1, HasVerticalAlignment.ALIGN_MIDDLE);
  925. watchGamePanel.setCellHorizontalAlignment(pl1, HasHorizontalAlignment.ALIGN_CENTER);
  926. watchGamePanel.add(pl2, DockPanel.WEST);
  927. watchGamePanel.setCellVerticalAlignment(pl2, HasVerticalAlignment.ALIGN_MIDDLE);
  928. watchGamePanel.setCellHorizontalAlignment(pl2, HasHorizontalAlignment.ALIGN_CENTER);
  929. watchGamePanel.add(pl4, DockPanel.EAST);
  930. watchGamePanel.setCellVerticalAlignment(pl4, HasVerticalAlignment.ALIGN_MIDDLE);
  931. watchGamePanel.setCellHorizontalAlignment(pl4, HasHorizontalAlignment.ALIGN_CENTER);
  932. watchGamePanel.add(pl3, DockPanel.NORTH);
  933. watchGamePanel.setCellVerticalAlignment(pl3, HasVerticalAlignment.ALIGN_MIDDLE);
  934. watchGamePanel.setCellHorizontalAlignment(pl3, HasHorizontalAlignment.ALIGN_CENTER);
  935. watchGamePanel.add(card1, DockPanel.SOUTH);
  936. watchGamePanel.setCellVerticalAlignment(card1, HasVerticalAlignment.ALIGN_BOTTOM);
  937. watchGamePanel.setCellHorizontalAlignment(card1, HasHorizontalAlignment.ALIGN_CENTER);
  938. watchGamePanel.add(card2, DockPanel.WEST);
  939. watchGamePanel.setCellVerticalAlignment(card2, HasVerticalAlignment.ALIGN_BOTTOM);
  940. watchGamePanel.setCellHorizontalAlignment(card2, HasHorizontalAlignment.ALIGN_CENTER);
  941. watchGamePanel.add(card4, DockPanel.EAST);
  942. watchGamePanel.setCellVerticalAlignment(card4, HasVerticalAlignment.ALIGN_BOTTOM);
  943. watchGamePanel.setCellHorizontalAlignment(card4, HasHorizontalAlignment.ALIGN_CENTER);
  944. watchGamePanel.add(card3, DockPanel.NORTH);
  945. watchGamePanel.setCellHorizontalAlignment(card3, HasHorizontalAlignment.ALIGN_CENTER);
  946.  
  947.  
  948.  
  949. }
  950.  
  951.  
  952.  
  953. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement