diff options
Diffstat (limited to 'src/com/fourisland/fourpuzzle/transition/FadeTransition.java')
-rw-r--r-- | src/com/fourisland/fourpuzzle/transition/FadeTransition.java | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/com/fourisland/fourpuzzle/transition/FadeTransition.java b/src/com/fourisland/fourpuzzle/transition/FadeTransition.java new file mode 100644 index 0000000..a571791 --- /dev/null +++ b/src/com/fourisland/fourpuzzle/transition/FadeTransition.java | |||
@@ -0,0 +1,92 @@ | |||
1 | /* | ||
2 | * To change this template, choose Tools | Templates | ||
3 | * and open the template in the editor. | ||
4 | */ | ||
5 | |||
6 | package com.fourisland.fourpuzzle.transition; | ||
7 | |||
8 | import com.fourisland.fourpuzzle.Display; | ||
9 | import com.fourisland.fourpuzzle.Game; | ||
10 | import com.fourisland.fourpuzzle.util.PauseTimer; | ||
11 | import java.awt.Color; | ||
12 | import java.awt.Graphics2D; | ||
13 | import java.awt.image.BufferedImage; | ||
14 | |||
15 | /** | ||
16 | * | ||
17 | * @author hatkirby | ||
18 | */ | ||
19 | public class FadeTransition implements MultidirectionalTransition { | ||
20 | |||
21 | private TransitionDirection direction; | ||
22 | public FadeTransition(TransitionDirection direction) | ||
23 | { | ||
24 | this.direction = direction; | ||
25 | } | ||
26 | |||
27 | public TransitionDirection getDirection() | ||
28 | { | ||
29 | return direction; | ||
30 | } | ||
31 | |||
32 | private int ticks = 750 / Game.FPS; | ||
33 | private PauseTimer pt = new PauseTimer(ticks); | ||
34 | public boolean render(Graphics2D g) | ||
35 | { | ||
36 | BufferedImage temp = Display.createCanvas(Game.WIDTH, Game.HEIGHT); | ||
37 | double alpha = 1.0 * pt.getTimer() / ticks * 0.75; | ||
38 | |||
39 | for (int i=0;i<Game.WIDTH;i++) | ||
40 | { | ||
41 | for (int j=0;j<Game.HEIGHT;j++) | ||
42 | { | ||
43 | if (direction == TransitionDirection.Out) | ||
44 | { | ||
45 | temp.setRGB(i, j, combine(preTransition.getRGB(i, j), Color.BLACK.getRGB(), alpha)); | ||
46 | } else if (direction == TransitionDirection.In) | ||
47 | { | ||
48 | temp.setRGB(i, j, combine(preTransition.getRGB(i, j), postTransition.getRGB(i, j), alpha)); | ||
49 | } | ||
50 | } | ||
51 | } | ||
52 | |||
53 | g.drawImage(temp, 0, 0, null); | ||
54 | |||
55 | if (pt.isElapsed()) | ||
56 | { | ||
57 | return true; | ||
58 | } | ||
59 | |||
60 | return false; | ||
61 | } | ||
62 | |||
63 | public static int combine(int c1, int c2, double alpha) | ||
64 | { | ||
65 | Color co1 = new Color(c1); | ||
66 | Color co2 = new Color(c2); | ||
67 | |||
68 | int r = (int) (alpha * co1.getRed() + (1 - alpha) * co2.getRed()); | ||
69 | int g = (int) (alpha * co1.getGreen() + (1 - alpha) * co2.getGreen()); | ||
70 | int b = (int) (alpha * co1.getBlue() + (1 - alpha) * co2.getBlue()); | ||
71 | |||
72 | return new Color(r, g, b).getRGB(); | ||
73 | } | ||
74 | |||
75 | private BufferedImage preTransition; | ||
76 | public void setPreTransition(BufferedImage preTransition) | ||
77 | { | ||
78 | this.preTransition = preTransition; | ||
79 | } | ||
80 | |||
81 | public Transition copy() | ||
82 | { | ||
83 | return new FadeTransition(direction); | ||
84 | } | ||
85 | |||
86 | private BufferedImage postTransition; | ||
87 | public void setPostTransition(BufferedImage postTransition) | ||
88 | { | ||
89 | this.postTransition = postTransition; | ||
90 | } | ||
91 | |||
92 | } | ||