Mercurial > hg > ristipolku
comparison game/Engine.java @ 75:b586ce4f6d97
Large GUI code cleanup.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 03 Mar 2011 04:04:57 +0200 |
parents | 855757ab8580 |
children | 43f68fe605de |
comparison
equal
deleted
inserted
replaced
74:b7adeae80363 | 75:b586ce4f6d97 |
---|---|
16 import java.io.*; | 16 import java.io.*; |
17 import game.*; | 17 import game.*; |
18 import javax.sound.sampled.*; | 18 import javax.sound.sampled.*; |
19 | 19 |
20 | 20 |
21 /* | 21 class AboutBox extends IDMWidget |
22 class AnimatedElement | |
23 { | 22 { |
24 float x, y, stime, value; | 23 BufferedImage aboutImg; |
25 Interpolate lerp; | 24 boolean aboutSecret; |
26 boolean active; | 25 |
27 | 26 public AboutBox() |
28 public AnimatedElement(float x, float y, ) | 27 { |
29 { | 28 try { |
30 stime = 0; | 29 ResourceLoader res = new ResourceLoader("graphics/girl.jpg"); |
31 this.x = x; | 30 aboutImg = ImageIO.read(res.getStream()); |
32 this.y = y; | 31 } |
33 | 32 catch (IOException e) |
34 } | 33 { |
35 | 34 } |
36 public animate(float time) | 35 |
37 { | 36 aboutSecret = false; |
38 if (!active) | 37 |
39 { | 38 setPos(150f, 150f); |
40 active = true; | 39 setSize(600f, 400f); |
41 stime = time; | 40 } |
42 } | 41 |
43 | 42 IDMPoint currPos, currOffs; |
44 float t = (time - stime) / 10.0f; | 43 Paint textPaint; |
45 if (t < 100) | 44 Font textFont; |
46 value = lerp.getValue(t); | 45 FontMetrics textMetrics; |
47 else | 46 |
48 { | 47 public void setTextFont(Font font, FontMetrics metrics) |
48 { | |
49 textFont = font; | |
50 textMetrics = metrics; | |
51 } | |
52 | |
53 public void setTextPaint(Paint paint) | |
54 { | |
55 textPaint = paint; | |
56 } | |
57 | |
58 public void setCurrPos(IDMPoint npos) | |
59 { | |
60 currPos = npos; | |
61 currOffs = new IDMPoint(0, 0); | |
62 } | |
63 | |
64 public void setCurrPos(float x, float y) | |
65 { | |
66 setCurrPos(new IDMPoint(x, y)); | |
67 } | |
68 | |
69 public void drawString(Graphics2D g, String text) | |
70 { | |
71 Paint savePaint = g.getPaint(); | |
72 g.setPaint(textPaint); | |
73 | |
74 int i = 0; | |
75 while (i < text.length()) | |
76 { | |
77 int p = text.indexOf("\n", i); | |
78 boolean linefeed; | |
79 String str; | |
80 if (p > i) | |
81 { | |
82 str = text.substring(i, p); | |
83 i = p + 1; | |
84 linefeed = true; | |
85 } | |
86 else | |
87 { | |
88 str = text.substring(i); | |
89 i += str.length(); | |
90 linefeed = false; | |
91 } | |
92 | |
93 g.drawString(str, currPos.x + currOffs.x, currPos.y + currOffs.y); | |
49 | 94 |
50 } | 95 if (linefeed) |
51 } | 96 { |
52 | 97 currOffs.x = 0; |
53 public paint(Graphics2D g, ); | 98 currOffs.y += textMetrics.getMaxDescent();; |
54 { | 99 } |
100 else | |
101 { | |
102 currOffs.x += textMetrics.stringWidth(str); | |
103 } | |
104 } | |
105 | |
106 g.setPaint(savePaint); | |
107 } | |
108 | |
109 public void paint(Graphics2D g) | |
110 { | |
111 int x = getScaledX(), y = getScaledY(), | |
112 w = getScaledWidth(), h = getScaledHeight(); | |
113 | |
114 g.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.6f)); | |
115 g.fill(new RoundRectangle2D.Float(x, y, w, h, 10, 10)); | |
116 | |
117 setTextFont(G.fonts[1], G.metrics[1]); | |
118 setTextPaint(Color.white); | |
119 | |
120 setCurrPos(x + 15, y + 15); | |
121 drawString(g, "RistiPolku (CrossPaths) v0.5"); | |
122 | |
123 if (aboutSecret) | |
124 { | |
125 g.drawImage(aboutImg, x + 15, y + 35, aboutImg.getWidth(), aboutImg.getHeight(), null); | |
126 | |
127 setCurrPos(x + 225, y + 55); | |
128 drawString(g, | |
129 "Dedicated to my\n" + | |
130 "favorite woman" + | |
131 "in the world."); | |
132 | |
133 setCurrPos(x + 350, y + 155); | |
134 drawString(g, "- Matti"); | |
135 } | |
136 else | |
137 { | |
138 setTextPaint(Color.yellow); | |
139 drawString(g, "(c) Copyright 2011 Matti 'ccr' Hämäläinen\n"); | |
140 | |
141 setTextPaint(Color.white); | |
142 drawString(g, "Programming project for Java-course\n" + | |
143 "T740306 taught by Kari Laitinen."); | |
144 } | |
145 } | |
146 | |
147 public boolean keyPressed(KeyEvent e) | |
148 { | |
149 if (e.getKeyCode() == KeyEvent.VK_L) | |
150 { | |
151 aboutSecret = true; | |
152 } | |
153 else | |
154 { | |
155 clicked(); | |
156 aboutSecret = false; | |
157 } | |
158 return true; | |
159 } | |
160 | |
161 public void clicked() | |
162 { | |
163 parent.remove(this); | |
55 } | 164 } |
56 } | 165 } |
57 */ | 166 |
58 | 167 |
59 class GameBoard extends IDMWidget | 168 class GameBoard extends IDMWidget |
60 { | 169 { |
61 public static final int boardSize = 9; | 170 static final int boardSize = 9; |
62 public static final int boardMiddle = 4; | 171 static final int boardMiddle = 4; |
63 Piece[][] board; | 172 Piece[][] board; |
64 float pscale; | 173 float pscale; |
65 | 174 |
66 public boolean flagGameOver; | 175 public boolean flagGameOver; |
67 public int gameScore; | 176 public int gameScore; |
68 | 177 |
69 public Piece currPiece, nextPiece; | 178 public Piece currPiece, nextPiece; |
70 int currX, currY, currPoint; | 179 int currX, currY, currPoint; |
71 | 180 |
72 | |
73 SoundManager soundManager; | |
74 Sound sndPlaced; | 181 Sound sndPlaced; |
75 | 182 |
76 public GameBoard(IDMPoint pos, SoundManager smgr, float pscale) | 183 public GameBoard(IDMPoint pos, float ps) |
77 { | 184 { |
78 super(pos); | 185 super(pos); |
79 this.pscale = pscale; | 186 pscale = ps; |
80 | 187 |
81 soundManager = smgr; | 188 // sndPlaced = smgr.getSound("sounds/placed.wav"); |
82 // sndPlaced = soundManager.getSound("sounds/placed.wav"); | |
83 | 189 |
84 startNewGame(); | 190 startNewGame(); |
85 } | 191 } |
86 | 192 |
87 public void startNewGame() | 193 public void startNewGame() |
120 getScaledX() + (x * pscale), | 226 getScaledX() + (x * pscale), |
121 getScaledY() + (y * pscale), | 227 getScaledY() + (y * pscale), |
122 pscale - pscale / 10); | 228 pscale - pscale / 10); |
123 } | 229 } |
124 } | 230 } |
125 | 231 |
126 public boolean contains(Point where) | 232 public boolean contains(float x, float y) |
127 { | 233 { |
128 return (where.x >= getScaledX() && where.y >= getScaledY() && | 234 boolean foo = (x >= getScaledX() && |
129 where.x < getScaledX() + boardSize * pscale && | 235 y >= getScaledY() && |
130 where.y < getScaledY() + boardSize * pscale); | 236 x < getScaledX() + boardSize * pscale && |
237 y < getScaledY() + boardSize * pscale); | |
238 System.out.print("contains "+x+", "+y+" ("+getScaledX()+", "+getScaledY()+", "+(getScaledX() + boardSize * pscale)+"): "+foo+"\n"); | |
239 return foo; | |
131 } | 240 } |
132 | 241 |
133 public void animate(float time) | 242 public void animate(float time) |
134 { | 243 { |
135 for (int y = 0; y < boardSize; y++) | 244 for (int y = 0; y < boardSize; y++) |
146 { | 255 { |
147 currPiece.rotate(dir); | 256 currPiece.rotate(dir); |
148 } | 257 } |
149 } | 258 } |
150 | 259 |
260 // Change coordinates based on the "outgoing" | |
261 // piece connection point. | |
151 private void pieceMoveTo(int point) | 262 private void pieceMoveTo(int point) |
152 { | 263 { |
153 switch (point) | 264 switch (point) |
154 { | 265 { |
155 case 0: currY--; break; | 266 case 0: currY--; break; |
181 nextPiece = tmp; | 292 nextPiece = tmp; |
182 board[currX][currY] = currPiece; | 293 board[currX][currY] = currPiece; |
183 } | 294 } |
184 } | 295 } |
185 | 296 |
186 public boolean pieceCheck(Piece piece) | 297 // Check one piece, set connections, find the new placement |
298 // based on piece rotations etc. | |
299 private boolean pieceCheck(Piece piece) | |
187 { | 300 { |
188 if (piece == null) | 301 if (piece == null) |
189 { | 302 { |
190 // Create new piece | 303 // Create new piece |
191 pieceCreateNew(); | 304 pieceCreateNew(); |
226 // Move to next position accordingly | 339 // Move to next position accordingly |
227 pieceMoveTo(currPoint); | 340 pieceMoveTo(currPoint); |
228 return false; | 341 return false; |
229 } | 342 } |
230 | 343 |
344 // Finish one move/turn of the game, resolve path and find placement | |
345 // of the next piece, or set "game over" state if required. | |
231 public void pieceFinishTurn() | 346 public void pieceFinishTurn() |
232 { | 347 { |
233 boolean finished = false; | 348 boolean finished = false; |
234 int connections = 0; | 349 int connections = 0; |
235 | 350 |
236 if (currPiece != null) | 351 if (currPiece != null) |
237 { | 352 { |
238 soundManager.play(sndPlaced); | 353 G.smgr.play(sndPlaced); |
239 } | 354 } |
240 | 355 |
241 while (!finished) | 356 while (!finished) |
242 { | 357 { |
243 if (currX >= 0 && currX < boardSize && currY >= 0 && currY < boardSize) | 358 if (currX >= 0 && currX < boardSize && currY >= 0 && currY < boardSize) |
273 pieceRotate(Piece.RotateDir.RIGHT); | 388 pieceRotate(Piece.RotateDir.RIGHT); |
274 | 389 |
275 return true; | 390 return true; |
276 } | 391 } |
277 | 392 |
393 /* | |
278 public boolean mouseClicked(MouseEvent e) | 394 public boolean mouseClicked(MouseEvent e) |
279 { | 395 { |
280 if (flagGameOver) | 396 if (flagGameOver) |
281 return false; | 397 return false; |
282 | 398 |
285 pieceFinishTurn(); | 401 pieceFinishTurn(); |
286 return true; | 402 return true; |
287 } | 403 } |
288 else | 404 else |
289 return false; | 405 return false; |
406 } | |
407 */ | |
408 | |
409 public void clicked() | |
410 { | |
411 pieceFinishTurn(); | |
290 } | 412 } |
291 | 413 |
292 public boolean keyPressed(KeyEvent e) | 414 public boolean keyPressed(KeyEvent e) |
293 { | 415 { |
294 if (flagGameOver) | 416 if (flagGameOver) |
319 implements Runnable, KeyListener, | 441 implements Runnable, KeyListener, |
320 MouseListener, MouseWheelListener | 442 MouseListener, MouseWheelListener |
321 { | 443 { |
322 long startTime; | 444 long startTime; |
323 float gameClock, gameFrames; | 445 float gameClock, gameFrames; |
446 | |
324 Thread animThread; | 447 Thread animThread; |
325 boolean animEnable = false; | 448 boolean animEnable = false; |
326 | 449 |
327 Font fontMain, font1, font2; | |
328 FontMetrics metrics1, metrics2; | |
329 | |
330 GameBoard lauta = null; | 450 GameBoard lauta = null; |
331 BufferedImage lautaBG = null, lautaBGScaled = null; | |
332 Dimension lautaDim; | |
333 | |
334 static final AudioFormat sfmt = new AudioFormat(22050, 16, 1, true, false); | |
335 SoundManager soundManager; | |
336 InputStream musa; | 451 InputStream musa; |
337 | |
338 boolean aboutEnabled = false, aboutSecret = false; | |
339 BufferedImage aboutImg = null; | |
340 | |
341 IDMContainer widgets; | 452 IDMContainer widgets; |
453 AboutBox aboutBox; | |
342 | 454 |
343 public Engine() | 455 public Engine() |
344 { | 456 { |
345 // Initialize globals | 457 // Initialize globals |
346 System.out.print("Engine() constructor\n"); | 458 System.out.print("Engine() constructor\n"); |
347 | 459 |
348 // Sound system | 460 // Sound system |
349 soundManager = new SoundManager(sfmt, 16); | 461 G.smgr = new SoundManager(new AudioFormat(22050, 16, 1, true, false), 4); |
350 | 462 |
351 // Load resources | 463 // Load resources |
352 try | 464 try |
353 { | 465 { |
354 ResourceLoader res = new ResourceLoader("graphics/board.jpg"); | 466 ResourceLoader res = new ResourceLoader("graphics/board.jpg"); |
355 lautaBG = ImageIO.read(res.getStream()); | 467 G.lautaBG = ImageIO.read(res.getStream()); |
356 | |
357 res = new ResourceLoader("graphics/girl.jpg"); | |
358 aboutImg = ImageIO.read(res.getStream()); | |
359 | 468 |
360 try { | 469 try { |
361 res = new ResourceLoader("graphics/font.ttf"); | 470 res = new ResourceLoader("graphics/font.ttf"); |
362 fontMain = Font.createFont(Font.TRUETYPE_FONT, res.getStream()); | |
363 | 471 |
364 font1 = fontMain.deriveFont(24f); | 472 |
365 font2 = fontMain.deriveFont(64f); | 473 G.fonts = new Font[G.numFonts]; |
474 G.fonts[0] = Font.createFont(Font.TRUETYPE_FONT, res.getStream()); | |
475 G.fonts[1] = G.fonts[0].deriveFont(24f); | |
476 G.fonts[2] = G.fonts[0].deriveFont(64f); | |
477 | |
366 } | 478 } |
367 catch (FontFormatException e) | 479 catch (FontFormatException e) |
368 { | 480 { |
369 System.out.print("Could not initialize fonts.\n"); | 481 System.out.print("Could not initialize fonts.\n"); |
370 } | 482 } |
383 } | 495 } |
384 | 496 |
385 // Create IDM GUI widgets | 497 // Create IDM GUI widgets |
386 widgets = new IDMContainer(); | 498 widgets = new IDMContainer(); |
387 | 499 |
388 widgets.add(new BtnSwapPiece(0.75f, 0.60f)); | 500 widgets.add(new BtnSwapPiece(767f, 450f)); |
389 widgets.add(new BtnAbout(0.75f, 0.75f)); | 501 widgets.add(new BtnAbout (767f, 570f)); |
390 widgets.add(new BtnNewGame(0.75f, 0.85f)); | 502 widgets.add(new BtnNewGame (767f, 650f)); |
391 | 503 |
392 lauta = new GameBoard(new IDMPoint(0.09f, 0.18f), soundManager, 63); | 504 lauta = new GameBoard(new IDMPoint(95, 130), 63); |
393 widgets.add(lauta); | 505 widgets.add(lauta); |
394 | 506 |
507 aboutBox = new AboutBox(); | |
508 | |
395 // Game | 509 // Game |
396 aboutEnabled = false; | |
397 startNewGame(); | 510 startNewGame(); |
398 | 511 |
399 // Initialize event listeners | 512 // Initialize event listeners |
400 addKeyListener(this); | 513 addKeyListener(this); |
401 addMouseListener(this); | 514 addMouseListener(this); |
406 { | 519 { |
407 System.out.print("Engine(): requesting focus\n"); | 520 System.out.print("Engine(): requesting focus\n"); |
408 requestFocus(); | 521 requestFocus(); |
409 } | 522 } |
410 | 523 |
411 // soundManager.play(musa); | 524 // G.smgr.play(musa); |
412 } | 525 } |
413 | 526 |
414 public void startNewGame() | 527 public void startNewGame() |
415 { | 528 { |
416 gameClock = 0; | 529 gameClock = 0; |
417 gameFrames = 0; | 530 gameFrames = 0; |
418 startTime = new Date().getTime(); | 531 startTime = new Date().getTime(); |
419 lauta.startNewGame(); | 532 lauta.startNewGame(); |
420 } | 533 } |
421 | 534 |
422 public void paintAbout(Graphics2D g) | |
423 { | |
424 int width = lautaDim.width / 2, | |
425 height = lautaDim.height / 4; | |
426 | |
427 int x = (lautaDim.width - width) / 2, | |
428 y = (lautaDim.height - height) / 2; | |
429 | |
430 g.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.6f)); | |
431 g.fill(new RoundRectangle2D.Float(x, y, width, height, 10, 10)); | |
432 | |
433 | |
434 g.setFont(font1); | |
435 g.setPaint(Color.white); | |
436 | |
437 g.drawString("RistiPolku (CrossPaths) v0.5", x + 10, y + 25); | |
438 if (aboutSecret) | |
439 { | |
440 g.drawImage(aboutImg, x + 15, y + 35, aboutImg.getWidth(), aboutImg.getHeight(), null); | |
441 g.drawString("Dedicated to my", x + 225, y + 55); | |
442 g.drawString("favorite woman", x + 225, y + 85); | |
443 g.drawString("in the world.", x + 225, y + 115); | |
444 g.drawString("- Matti", x + 350, y + 155); | |
445 } | |
446 else | |
447 { | |
448 g.setPaint(Color.yellow); | |
449 g.drawString("(c) Copyright 2011 Matti 'ccr' Hämäläinen", x + 10, y + 55); | |
450 | |
451 g.setPaint(Color.white); | |
452 g.drawString("Programming project for Java-course", x + 10, y + 105); | |
453 g.drawString("T740306 taught by Kari Laitinen.", x + 10, y + 135); | |
454 } | |
455 } | |
456 | |
457 public void paintComponent(Graphics g) | 535 public void paintComponent(Graphics g) |
458 { | 536 { |
459 Graphics2D g2 = (Graphics2D) g; | 537 Graphics2D g2 = (Graphics2D) g; |
460 | 538 |
461 // Use antialiasing when rendering the game elements | 539 // Use antialiasing when rendering the game elements |
466 RenderingHints.VALUE_TEXT_ANTIALIAS_ON); | 544 RenderingHints.VALUE_TEXT_ANTIALIAS_ON); |
467 | 545 |
468 | 546 |
469 // Rescale if parent component size has changed | 547 // Rescale if parent component size has changed |
470 Dimension dim = getSize(); | 548 Dimension dim = getSize(); |
471 if (lautaDim == null || !dim.equals(lautaDim)) | 549 if (G.screenDim == null || !dim.equals(G.screenDim)) |
472 { | 550 { |
473 // Rescale IDM GUI widgets | 551 // Rescale IDM GUI widgets |
474 widgets.setScale(dim.width, dim.height); | 552 widgets.setScale(dim.width / 1024.0f, dim.height / 768.0f); |
553 G.screenDim = dim; | |
475 | 554 |
476 // Rescale background image | 555 // Rescale background image |
477 lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB); | 556 G.lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB); |
478 Graphics2D gimg = lautaBGScaled.createGraphics(); | 557 Graphics2D gimg = G.lautaBGScaled.createGraphics(); |
479 gimg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, | 558 gimg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, |
480 RenderingHints.VALUE_INTERPOLATION_BICUBIC); | 559 RenderingHints.VALUE_INTERPOLATION_BICUBIC); |
481 | 560 |
482 gimg.drawImage(lautaBG, 0, 0, dim.width, dim.height, null); | 561 gimg.drawImage(G.lautaBG, 0, 0, dim.width, dim.height, null); |
483 lauta.paintBackPlate(gimg); | 562 lauta.paintBackPlate(gimg); |
484 lautaDim = dim; | |
485 | 563 |
486 System.out.print("scale changed\n"); | 564 System.out.print("scale changed\n"); |
487 } | 565 } |
488 | 566 |
489 // Get font metrics against current Graphics2D context | 567 // Get font metrics against current Graphics2D context |
490 if (metrics1 == null) | 568 if (G.metrics == null) |
491 metrics1 = g2.getFontMetrics(font1); | 569 { |
492 | 570 G.metrics = new FontMetrics[G.numFonts]; |
493 if (metrics2 == null) | 571 for (int i = 0; i < G.numFonts; i++) |
494 metrics2 = g2.getFontMetrics(font2); | 572 G.metrics[i] = g2.getFontMetrics(G.fonts[i]); |
495 | 573 } |
496 | 574 |
497 // Draw background image, pieces, widgets | 575 // Draw background image, pieces, widgets |
498 g2.drawImage(lautaBGScaled, 0, 0, null); | 576 g2.drawImage(G.lautaBGScaled, 0, 0, null); |
499 widgets.paint(g2); | 577 widgets.paint(g2); |
500 | 578 |
501 if (!lauta.flagGameOver) | 579 if (!lauta.flagGameOver) |
502 { | 580 { |
503 if (lauta.nextPiece != null) | 581 if (lauta.nextPiece != null) |
510 } | 588 } |
511 else | 589 else |
512 { | 590 { |
513 // Game over text | 591 // Game over text |
514 String text = "Game Over!"; | 592 String text = "Game Over!"; |
515 int textWidth = metrics2.stringWidth(text); | 593 int textWidth = G.metrics[2].stringWidth(text); |
516 g2.setFont(font2); | 594 g2.setFont(G.fonts[2]); |
517 | 595 |
518 g2.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.5f)); | 596 g2.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.5f)); |
519 g2.drawString(text, (dim.width - textWidth) / 2 + 5, dim.height / 2 + 5); | 597 g2.drawString(text, (dim.width - textWidth) / 2 + 5, dim.height / 2 + 5); |
520 | 598 |
521 double f = Math.sin(gameClock * 0.1) * 4.0; | 599 double f = Math.sin(gameClock * 0.1) * 4.0; |
522 g2.setPaint(Color.white); | 600 g2.setPaint(Color.white); |
523 g2.drawString(text, (dim.width - textWidth) / 2 + (float) f, dim.height / 2 + (float) f); | 601 g2.drawString(text, (dim.width - textWidth) / 2 + (float) f, dim.height / 2 + (float) f); |
524 } | 602 } |
525 | 603 |
526 // Scores, etc | 604 // Score |
527 g2.setFont(font2); | 605 g2.setFont(G.fonts[2]); |
528 g2.setPaint(Color.white); | 606 g2.setPaint(Color.white); |
529 | |
530 g2.drawString(""+ String.format("%05d", lauta.gameScore), dim.width - 230, 220); | 607 g2.drawString(""+ String.format("%05d", lauta.gameScore), dim.width - 230, 220); |
531 | 608 |
532 g2.setFont(font1); | 609 |
610 // Frames per second counter | |
611 g2.setFont(G.fonts[1]); | |
533 long currTime = new Date().getTime(); | 612 long currTime = new Date().getTime(); |
534 g2.drawString("fps = "+ ((gameFrames * 1000) / (currTime - startTime)), dim.width - 120, 20); | 613 g2.drawString("fps = "+ ((gameFrames * 1000) / (currTime - startTime)), dim.width - 120, 20); |
535 | 614 |
536 if (aboutEnabled) | 615 gameFrames++; |
537 { | |
538 paintAbout(g2); | |
539 } | |
540 } | 616 } |
541 | 617 |
542 public void startThreads() | 618 public void startThreads() |
543 { | 619 { |
544 System.out.print("startThreads()\n"); | 620 System.out.print("startThreads()\n"); |
561 animEnable = false; | 637 animEnable = false; |
562 animThread = null; | 638 animThread = null; |
563 } | 639 } |
564 | 640 |
565 // Shut down sound manager | 641 // Shut down sound manager |
566 soundManager.close(); | 642 G.smgr.close(); |
567 } | 643 } |
568 | 644 |
569 public void mouseEntered(MouseEvent e) { } | 645 public void mouseEntered(MouseEvent e) |
570 public void mouseExited(MouseEvent e) { } | 646 { |
647 widgets.mouseEntered(e); | |
648 } | |
649 public void mouseExited(MouseEvent e) | |
650 { | |
651 widgets.mouseExited(e); | |
652 } | |
571 | 653 |
572 public void mousePressed(MouseEvent e) | 654 public void mousePressed(MouseEvent e) |
573 { | 655 { |
574 if (!aboutEnabled) | 656 if (widgets.containsObject(aboutBox)) |
657 aboutBox.mousePressed(e); | |
658 else | |
575 widgets.mousePressed(e); | 659 widgets.mousePressed(e); |
576 } | 660 } |
577 | 661 |
578 public void mouseReleased(MouseEvent e) | 662 public void mouseReleased(MouseEvent e) |
579 { | 663 { |
580 if (!aboutEnabled) | 664 if (widgets.containsObject(aboutBox)) |
665 aboutBox.mouseReleased(e); | |
666 else | |
581 widgets.mouseReleased(e); | 667 widgets.mouseReleased(e); |
582 } | 668 } |
583 | 669 |
584 public void mouseClicked(MouseEvent e) | 670 public void mouseClicked(MouseEvent e) |
585 { | 671 { |
586 if (!hasFocus()) | 672 if (!hasFocus()) |
587 { | 673 { |
588 System.out.print("Requesting focus\n"); | 674 System.out.print("Requesting focus\n"); |
589 requestFocus(); | 675 requestFocus(); |
590 } | 676 } |
591 if (!aboutEnabled) | |
592 { | |
593 lauta.mouseClicked(e); | |
594 } | |
595 } | 677 } |
596 | 678 |
597 public void mouseWheelMoved(MouseWheelEvent e) | 679 public void mouseWheelMoved(MouseWheelEvent e) |
598 { | 680 { |
599 lauta.mouseWheelMoved(e); | 681 lauta.mouseWheelMoved(e); |
600 } | 682 } |
601 | 683 |
602 public void keyTyped(KeyEvent e) | 684 public void keyTyped(KeyEvent e) { } |
603 { | 685 public void keyReleased(KeyEvent e) { } |
604 } | |
605 | |
606 public void keyReleased(KeyEvent e) | |
607 { | |
608 } | |
609 | 686 |
610 public void keyPressed(KeyEvent e) | 687 public void keyPressed(KeyEvent e) |
611 { | 688 { |
612 if (aboutEnabled) | 689 // Handle keyboard input |
613 { | 690 if (widgets.containsObject(aboutBox)) |
614 if (e.getKeyCode() == KeyEvent.VK_L) | 691 aboutBox.keyPressed(e); |
615 { | 692 else |
616 aboutSecret = true; | |
617 } | |
618 else | |
619 { | |
620 aboutEnabled = false; | |
621 aboutSecret = false; | |
622 } | |
623 } | |
624 else | |
625 { | |
626 // Handle keyboard input | |
627 if (lauta.keyPressed(e)) | |
628 return; | |
629 | |
630 widgets.keyPressed(e); | 693 widgets.keyPressed(e); |
631 } | |
632 } | 694 } |
633 | 695 |
634 public void run() | 696 public void run() |
635 { | 697 { |
636 while (animEnable) | 698 while (animEnable) |
643 if (lauta.nextPiece != null) | 705 if (lauta.nextPiece != null) |
644 lauta.nextPiece.animate(gameClock); | 706 lauta.nextPiece.animate(gameClock); |
645 | 707 |
646 // Repaint with a frame limiter | 708 // Repaint with a frame limiter |
647 if (gameClock % 4 == 1) | 709 if (gameClock % 4 == 1) |
648 { | |
649 repaint(); | 710 repaint(); |
650 gameFrames++; | |
651 } | |
652 | 711 |
653 // Sleep for a moment | 712 // Sleep for a moment |
654 try { | 713 try { |
655 Thread.sleep(10); | 714 Thread.sleep(10); |
656 } | 715 } |
661 | 720 |
662 class BtnNewGame extends IDMButton | 721 class BtnNewGame extends IDMButton |
663 { | 722 { |
664 public BtnNewGame(float x, float y) | 723 public BtnNewGame(float x, float y) |
665 { | 724 { |
666 super(x, y, KeyEvent.VK_ESCAPE, font1, "New Game"); | 725 super(x, y, KeyEvent.VK_ESCAPE, G.fonts[1], "New Game"); |
667 } | 726 } |
668 | 727 |
669 public void clicked() | 728 public void clicked() |
670 { | 729 { |
671 startNewGame(); | 730 startNewGame(); |
674 | 733 |
675 class BtnSwapPiece extends IDMButton | 734 class BtnSwapPiece extends IDMButton |
676 { | 735 { |
677 public BtnSwapPiece(float x, float y) | 736 public BtnSwapPiece(float x, float y) |
678 { | 737 { |
679 super(x, y, KeyEvent.VK_SPACE, font1, "Swap"); | 738 super(x, y, KeyEvent.VK_SPACE, G.fonts[1], "Swap"); |
680 } | 739 } |
681 | 740 |
682 public void clicked() | 741 public void clicked() |
683 { | 742 { |
684 lauta.pieceSwapCurrent(); | 743 lauta.pieceSwapCurrent(); |
687 | 746 |
688 class BtnAbout extends IDMButton | 747 class BtnAbout extends IDMButton |
689 { | 748 { |
690 public BtnAbout(float x, float y) | 749 public BtnAbout(float x, float y) |
691 { | 750 { |
692 super(x, y, KeyEvent.VK_A, font1, "About"); | 751 super(x, y, KeyEvent.VK_A, G.fonts[1], "About"); |
693 } | 752 } |
694 | 753 |
695 public void clicked() | 754 public void clicked() |
696 { | 755 { |
697 aboutEnabled = true; | 756 if (!widgets.containsObject(aboutBox)) |
757 widgets.add(aboutBox); | |
698 } | 758 } |
699 } | 759 } |
700 } | 760 } |