PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/MWClient/src/main/java/mw/client/managers/DialogManager.java

http://magicwars.googlecode.com/
Java | 605 lines | 418 code | 162 blank | 25 comment | 45 complexity | 8fbefeabe96e0ece9234f1083c3fa543 MD5 | raw file
  1. package mw.client.managers;
  2. import java.awt.Component;
  3. import java.awt.Rectangle;
  4. import java.awt.event.MouseEvent;
  5. import java.awt.event.MouseListener;
  6. import java.awt.event.MouseMotionListener;
  7. import java.awt.event.MouseWheelEvent;
  8. import java.util.ArrayList;
  9. import java.util.HashSet;
  10. import javax.swing.JComponent;
  11. import javax.swing.JOptionPane;
  12. import mw.client.dialogs.MessageDlg;
  13. import mw.client.utils.dialogs.DialogContainer;
  14. import mw.client.utils.dialogs.DlgParams;
  15. import mw.server.list.CardBeanList;
  16. import mw.server.model.DeckInfo;
  17. import mw.server.model.bean.CardBean;
  18. import mw.server.model.bean.SpellBean;
  19. public class DialogManager extends JComponent implements MouseListener,
  20. MouseMotionListener {
  21. private static DialogManager dialogManager = null;
  22. //private static final Logger log = Logger.getLogger(DialogManager.class);
  23. public static DialogManager getManager() {
  24. if (dialogManager == null) {
  25. dialogManager = new DialogManager();
  26. dialogManager.setVisible(true);
  27. }
  28. return dialogManager;
  29. }
  30. public enum MTGDialogs {
  31. none, AboutDialog, MessageDialog, StackDialog, AssignDamageDialog, ManaChoiceDialog, ChoiceDialog, GraveDialog, DialogContainer, CombatDialog,
  32. ChooseDeckDialog, ChooseCommonDialog, RevealDialog
  33. }
  34. private MTGDialogs currentDialog = MTGDialogs.none;
  35. private DialogContainer dialogContainer = null;
  36. private int screen_width = 0;
  37. private int screen_height = 0;
  38. // /////////////////////////////// *** for drag and drop ***
  39. // /////////////////////////////////
  40. private boolean bDragged = false;
  41. private int dx, dy;
  42. private int mx, my;
  43. private Rectangle rec, oldRec;
  44. private JComponent j;
  45. public DialogManager() {
  46. addMouseListener(this);
  47. addMouseMotionListener(this);
  48. //addMouseWheelListener(this);
  49. }
  50. public void setScreenWidth(int screen_width) {
  51. this.screen_width = screen_width;
  52. }
  53. public void setScreenHeight(int screen_height) {
  54. this.screen_height = screen_height;
  55. }
  56. public void showStackDialog() {
  57. int w = (int) (screen_width * 0.7);
  58. int h = (int) (screen_height * 0.5);
  59. if (h < 500) {
  60. h = 500;
  61. }
  62. if (w > 1024) {
  63. w = 1024;
  64. }
  65. int height = getHeight();
  66. int width = getWidth();
  67. int x = ((width - w) / 2);
  68. int y = ((height - h) / 2);
  69. DlgParams params = new DlgParams();
  70. params.rect = new Rectangle(x, y, w, h);
  71. dialogContainer = new DialogContainer(MTGDialogs.StackDialog, params);
  72. dialogContainer.setVisible(true);
  73. add(dialogContainer);
  74. this.currentDialog = MTGDialogs.DialogContainer;
  75. setDlgBounds(new Rectangle(x, y, w, h));
  76. dialogContainer.showDialog(true);
  77. setVisible(true);
  78. }
  79. public void showAboutDialog() {
  80. int w = (int) (screen_width * 0.7);
  81. int h = (int) (screen_height * 0.5);
  82. if (h < 500) {
  83. h = 500;
  84. }
  85. if (w > 1024) {
  86. w = 1024;
  87. }
  88. int height = getHeight();
  89. int width = getWidth();
  90. int x = ((width - w) / 2);
  91. int y = ((height - h) / 2);
  92. DlgParams params = new DlgParams();
  93. params.rect = new Rectangle(x, y, w, h);
  94. dialogContainer = new DialogContainer(MTGDialogs.AboutDialog, params);
  95. dialogContainer.setVisible(true);
  96. add(dialogContainer);
  97. this.currentDialog = MTGDialogs.DialogContainer;
  98. setDlgBounds(new Rectangle(x, y, w, h));
  99. dialogContainer.showDialog(true);
  100. setVisible(true);
  101. }
  102. public void showChoiceDialog(CardBeanList cardList, boolean targetOptional, boolean cancelStopsPlaying) {
  103. int w = (int) (790);
  104. int h = (int) (600);
  105. int height = getHeight();
  106. int width = getWidth();
  107. int x = ((width - w) / 2);
  108. int y = ((height - h) / 2);
  109. DlgParams params = new DlgParams();
  110. params.rect = new Rectangle(x, y, w, h);
  111. params.setCardList(cardList);
  112. params.setOptional(targetOptional);
  113. params.setCancelStopsPlaying(cancelStopsPlaying);
  114. dialogContainer = new DialogContainer(MTGDialogs.ChoiceDialog, params);
  115. dialogContainer.setVisible(true);
  116. add(dialogContainer);
  117. this.currentDialog = MTGDialogs.DialogContainer;
  118. setDlgBounds(new Rectangle(x, y, w, h));
  119. dialogContainer.showDialog(true);
  120. setVisible(true);
  121. }
  122. public void showAbilityChoiceDialog(ArrayList<SpellBean> spellBeanList, boolean targetOptional) {
  123. int w = (int) (790);
  124. int h = (int) (600);
  125. int height = getHeight();
  126. int width = getWidth();
  127. int x = ((width - w) / 2);
  128. int y = ((height - h) / 2);
  129. DlgParams params = new DlgParams();
  130. params.rect = new Rectangle(x, y, w, h);
  131. /**
  132. * Form card bean list from spell bean list
  133. */
  134. CardBeanList cardList = new CardBeanList();
  135. for (SpellBean spellBean : spellBeanList) {
  136. CardBean cardBean = spellBean.getSourceCardBean();
  137. cardBean.setAttachedSpellBean(spellBean);
  138. cardList.add(cardBean);
  139. }
  140. params.setCardList(cardList);
  141. params.setOptional(targetOptional);
  142. params.setChooseAbility(true);
  143. dialogContainer = new DialogContainer(MTGDialogs.ChoiceDialog, params);
  144. dialogContainer.setVisible(true);
  145. add(dialogContainer);
  146. this.currentDialog = MTGDialogs.DialogContainer;
  147. setDlgBounds(new Rectangle(x, y, w, h));
  148. dialogContainer.showDialog(true);
  149. setVisible(true);
  150. }
  151. public void showGraveDialog(int playerID, CardBeanList cardList) {
  152. int w = (int) (790);
  153. int h = (int) (600);
  154. int height = getHeight();
  155. int width = getWidth();
  156. int x = ((width - w) / 2);
  157. int y = ((height - h) / 2);
  158. DlgParams params = new DlgParams();
  159. params.rect = new Rectangle(x, y, w, h);
  160. params.setCardList(cardList);
  161. params.setPlayerID(playerID);
  162. dialogContainer = new DialogContainer(MTGDialogs.GraveDialog, params);
  163. dialogContainer.setVisible(true);
  164. add(dialogContainer);
  165. this.currentDialog = MTGDialogs.DialogContainer;
  166. setDlgBounds(new Rectangle(x, y, w, h));
  167. dialogContainer.showDialog(true);
  168. setVisible(true);
  169. }
  170. public void showRevealDialog(int playerID, CardBeanList cardList) {
  171. int w = (int) (790);
  172. int h = (int) (600);
  173. int height = getHeight();
  174. int width = getWidth();
  175. int x = ((width - w) / 2);
  176. int y = ((height - h) / 2);
  177. DlgParams params = new DlgParams();
  178. params.rect = new Rectangle(x, y, w, h);
  179. params.setCardList(cardList);
  180. params.setPlayerID(playerID);
  181. dialogContainer = new DialogContainer(MTGDialogs.RevealDialog, params);
  182. dialogContainer.setVisible(true);
  183. add(dialogContainer);
  184. this.currentDialog = MTGDialogs.DialogContainer;
  185. setDlgBounds(new Rectangle(x, y, w, h));
  186. dialogContainer.showDialog(true);
  187. setVisible(true);
  188. }
  189. public void showManaChoiceDialog(HashSet<String> manaSet) {
  190. removeAll();
  191. int w = (int) (350);
  192. int h = (int) (330);
  193. int height = getHeight();
  194. int width = getWidth();
  195. int x = ((width - w) / 2);
  196. int y = ((height - h) / 2);
  197. DlgParams params = new DlgParams();
  198. params.rect = new Rectangle(x, y, w, h);
  199. params.setManaChoices(manaSet);
  200. dialogContainer = new DialogContainer(MTGDialogs.ManaChoiceDialog, params);
  201. dialogContainer.setVisible(true);
  202. add(dialogContainer);
  203. this.currentDialog = MTGDialogs.DialogContainer;
  204. setDlgBounds(new Rectangle(x, y, w, h));
  205. dialogContainer.showDialog(true);
  206. setVisible(true);
  207. }
  208. public void showCombatDialog() {
  209. removeAll();
  210. int w = (int) (screen_width * 0.7);
  211. int h = (int) (screen_height * 0.5);
  212. if (h < 500) {
  213. h = 500;
  214. }
  215. int height = getHeight();
  216. int width = getWidth();
  217. int x = ((width - w) / 2);
  218. int y = ((height - h) / 2);
  219. DlgParams params = new DlgParams();
  220. params.rect = new Rectangle(x, y, w, h);
  221. dialogContainer = new DialogContainer(MTGDialogs.CombatDialog, params);
  222. dialogContainer.setVisible(true);
  223. add(dialogContainer);
  224. this.currentDialog = MTGDialogs.DialogContainer;
  225. setDlgBounds(new Rectangle(x, y, w, h));
  226. dialogContainer.showDialog(true);
  227. setVisible(true);
  228. }
  229. public void showAssignDamageDialog() {
  230. removeAll();
  231. int w = 600;
  232. int h = SettingsManager.getManager().getCardSize().height + 200;
  233. int height = getHeight();
  234. int width = getWidth();
  235. int x = ((width - w) / 2);
  236. int y = ((height - h) / 2);
  237. DlgParams params = new DlgParams();
  238. params.rect = new Rectangle(x, y, w, h);
  239. dialogContainer = new DialogContainer(MTGDialogs.AssignDamageDialog, params);
  240. dialogContainer.setVisible(true);
  241. add(dialogContainer);
  242. this.currentDialog = MTGDialogs.DialogContainer;
  243. setDlgBounds(new Rectangle(x, y, w, h));
  244. dialogContainer.showDialog(true);
  245. setVisible(true);
  246. }
  247. public void showMessageDlgWarning(String title, String message) {
  248. //showMessageDlg(title, message, 350, 88, MessageDlg.Types.Warning);
  249. JOptionPane.showMessageDialog(null, message, title, JOptionPane.WARNING_MESSAGE);
  250. }
  251. public DialogContainer showMessageDlg(String title, String message, int w, int h, MessageDlg.Types type) {
  252. DlgParams params = new DlgParams();
  253. params.setTitle(title);
  254. params.setMessage(message);
  255. params.type = type;
  256. int height = getHeight();
  257. int width = getWidth();
  258. int x = ((width - w) / 2);
  259. int y = ((height - h) / 2);
  260. params.rect = new Rectangle(x, y, w, h);
  261. dialogContainer = new DialogContainer(MTGDialogs.MessageDialog, params);
  262. dialogContainer.setVisible(true);
  263. add(dialogContainer);
  264. this.currentDialog = MTGDialogs.DialogContainer;
  265. setDlgBounds(new Rectangle(x, y, w, h));
  266. dialogContainer.showDialog(true);
  267. setVisible(true);
  268. //log.debug("show dialog: showMessageDlg");
  269. return dialogContainer;
  270. }
  271. public void showChooseDeckDialog(ArrayList<DeckInfo> deckList) {
  272. showChooseDeckDialog(deckList, false);
  273. }
  274. public void showChooseDeckDialog(ArrayList<DeckInfo> deckList, boolean isAI) {
  275. /*int w = (int) (screen_width * 0.7);
  276. int h = (int) (screen_height * 0.5);
  277. if (h < 500) {
  278. h = 500;
  279. }
  280. if (w > 1024) {
  281. w = 1024;
  282. }*/
  283. int w = 480;
  284. int h = 320;
  285. int height = getHeight();
  286. int width = getWidth();
  287. int x = ((width - w) / 2) + 100;
  288. int y = ((height - h) / 2) + 100;
  289. if (isAI) {
  290. x -= 150;
  291. y -= 150;
  292. }
  293. DlgParams params = new DlgParams();
  294. params.rect = new Rectangle(x, y, w, h);
  295. params.setDeckList(deckList);
  296. params.setAI(isAI);
  297. dialogContainer = new DialogContainer(MTGDialogs.ChooseDeckDialog, params);
  298. dialogContainer.setVisible(true);
  299. add(dialogContainer);
  300. this.currentDialog = MTGDialogs.DialogContainer;
  301. setDlgBounds(new Rectangle(x, y, w, h));
  302. dialogContainer.showDialog(true);
  303. setVisible(true);
  304. }
  305. public void showChooseDialog(ArrayList<Object> objectList) {
  306. showChooseDialog(objectList, null);
  307. }
  308. public void showChooseDialog(ArrayList<Object> objectList, String title) {
  309. int w = 440;
  310. int h = 240;
  311. int height = getHeight();
  312. int width = getWidth();
  313. int x = ((width - w) / 2);
  314. //int y = (2*(height - h) / 3);
  315. int y = ((height - h) / 2);
  316. DlgParams params = new DlgParams();
  317. params.rect = new Rectangle(x, y, w, h);
  318. params.setObjectList(objectList);
  319. params.setTitle(title);
  320. dialogContainer = new DialogContainer(MTGDialogs.ChooseCommonDialog, params);
  321. dialogContainer.setVisible(true);
  322. add(dialogContainer);
  323. this.currentDialog = MTGDialogs.DialogContainer;
  324. setDlgBounds(new Rectangle(x, y, w, h));
  325. dialogContainer.showDialog(true);
  326. setVisible(true);
  327. }
  328. public DialogContainer showMessageDlg(String title, String message, MessageDlg.Types type) {
  329. Rectangle r = new Rectangle();
  330. if (type.equals(MessageDlg.Types.Warning)) {
  331. r.width = 350;
  332. r.height = 88;
  333. } else if (type.equals(MessageDlg.Types.FlashInfo)) {
  334. r.width = 350;
  335. r.height = 90;
  336. }
  337. return showMessageDlg(title, message, r.width, r.height, type);
  338. }
  339. public void setDlgBounds(Rectangle r) {
  340. if (currentDialog == MTGDialogs.DialogContainer) {
  341. dialogContainer.setBounds(r.x, r.y, r.width, r.height);
  342. }
  343. }
  344. public void fadeOut() {
  345. if (dialogContainer != null) {
  346. dialogContainer.showDialog(false);
  347. removeAll();
  348. }
  349. this.currentDialog = MTGDialogs.none;
  350. setVisible(false);
  351. repaint();
  352. }
  353. public void fadeOut(DialogContainer dc) {
  354. //log.debug("start:fadeOut:"+dc.toString());
  355. dc.showDialog(false);
  356. remove(dc);
  357. Component[] components = getComponents();
  358. boolean bFound = false;
  359. for (int i = 0; i < components.length; i++) {
  360. if (components[i] instanceof DialogContainer) {
  361. bFound = true;
  362. }
  363. }
  364. if (!bFound) {
  365. setVisible(false);
  366. }
  367. //revalidate();
  368. validate();
  369. }
  370. public void mouseDragged(MouseEvent e) {
  371. if (bDragged == true) {
  372. dx = e.getX() - mx;
  373. dy = e.getY() - my;
  374. rec.x += dx;
  375. rec.y += dy;
  376. mx = e.getX();
  377. my = e.getY();
  378. if (oldRec == null) {
  379. oldRec = new Rectangle(rec);
  380. }
  381. int i = Math.abs(oldRec.x - rec.x) + Math.abs(oldRec.y - rec.y);
  382. if (i > 3) {
  383. oldRec = new Rectangle(rec);
  384. j.setBounds(oldRec);
  385. }
  386. }
  387. }
  388. public void mouseMoved(MouseEvent e) {
  389. }
  390. public void mouseClicked(MouseEvent e) {
  391. }
  392. public void mouseEntered(MouseEvent e) {
  393. }
  394. public void mouseExited(MouseEvent e) {
  395. }
  396. public void mousePressed(MouseEvent e) {
  397. if (e.getButton() == MouseEvent.BUTTON1) {
  398. j = (JComponent) getComponentAt(e.getX(), e.getY());
  399. if (j != null && j instanceof DialogContainer) {
  400. rec = j.getBounds();
  401. bDragged = true;
  402. mx = e.getX();
  403. my = e.getY();
  404. }
  405. }
  406. }
  407. public void mouseReleased(MouseEvent e) {
  408. bDragged = false;
  409. if (j instanceof DialogManager) {
  410. return;
  411. }
  412. if (j != null && rec != null) {
  413. j.setBounds(rec);
  414. }
  415. oldRec = null;
  416. if (rec == null)
  417. return;
  418. if (rec.x < 0) {
  419. rec.x = 0;
  420. if (j != null)
  421. j.setBounds(rec);
  422. }
  423. if (rec.y < 0) {
  424. rec.y = 0;
  425. if (j != null)
  426. j.setBounds(rec);
  427. }
  428. j = null;
  429. }
  430. public void mouseWheelMoved(MouseWheelEvent e) {
  431. int notches = e.getWheelRotation();
  432. System.out.println("outx:"+notches);
  433. if (currentDialog != null && currentDialog.equals(MTGDialogs.ChooseCommonDialog)) {
  434. System.out.println("out:"+1);
  435. }
  436. }
  437. /**
  438. * Default UID.
  439. */
  440. private static final long serialVersionUID = 1L;
  441. }