1 /***************************************************************************
2  *                 (C) Copyright 2003-2014 - Faiumoni e.V.                 *
3  ***************************************************************************
4  ***************************************************************************
5  *                                                                         *
6  *   This program is free software; you can redistribute it and/or modify  *
7  *   it under the terms of the GNU General Public License as published by  *
8  *   the Free Software Foundation; either version 2 of the License, or     *
9  *   (at your option) any later version.                                   *
10  *                                                                         *
11  ***************************************************************************/
12 package games.stendhal.client.gui;
13 
14 import java.awt.Graphics;
15 import java.awt.Transparency;
16 
17 import games.stendhal.client.IGameScreen;
18 import games.stendhal.client.LayerRenderer;
19 import games.stendhal.client.sprite.Tileset;
20 
21 /**
22  * A renderer for map wide effects. These effects are normally temporary and
23  * they are automatically removed as soon as {@link #isExpired()} returns
24  * <code>true</code>.
25  *
26  */
27 public abstract class EffectLayer extends LayerRenderer {
28 	/** Duration of the effect. The actual meaning is effect dependent. */
29 	final long duration;
30 	/** Time stamp of the creation of the effect. */
31 	final long timestamp;
32 
33 	/**
34 	 * Create an EffectLayer for specified effect duration.
35 	 *
36 	 * @param duration effect duration in milliseconds
37 	 */
EffectLayer(int duration)38 	public EffectLayer(int duration) {
39 		this.duration = duration;
40 		timestamp = System.currentTimeMillis();
41 	}
42 
43 	@Override
setTileset(Tileset tileset)44 	public void setTileset(Tileset tileset) {
45 		throw new UnsupportedOperationException();
46 	}
47 
48 	@Override
draw(Graphics g, int x, int y, int w, int h)49 	public void draw(Graphics g, int x, int y, int w, int h) {
50 		int s = IGameScreen.SIZE_UNIT_PIXELS;
51 		drawScreen(g, x * s, y * s, w * s, h* s);
52 	}
53 
54 	/**
55 	 * A convenience method for drawing in screen coordinates. The parameters
56 	 * correspond to drawing the whole game screen. If world units are wanted,
57 	 * override {@link #draw(Graphics, int, int, int, int)} instead.
58 	 * @param g graphics
59 	 * @param x x coordinate
60 	 * @param y y coordinate
61 	 * @param w screen width
62 	 * @param h screen height
63 	 */
drawScreen(Graphics g, int x, int y, int w, int h)64 	abstract void drawScreen(Graphics g, int x, int y, int w, int h);
65 
66 	/**
67 	 * Check if the effect is old enough to have expired so that it should be removed.
68 	 *
69 	 * @return <code>true</code> if the effect has expired, otherwise <code>false</code>
70 	 */
isExpired()71 	public boolean isExpired() {
72 		// Default implementation: remove after the time of duration has passed
73 		return System.currentTimeMillis() > timestamp + duration;
74 	}
75 
76 	/**
77 	 * A convenience method for getting the opaque or fully transparent alpha
78 	 * values on systems that need it.
79 	 *
80 	 * @param originalAlpha original alpha value
81 	 * @return original alpha, or its value rounded to fully opaque or
82 	 * 	transparent on systems that have the translucency turned off
83 	 */
alpha(int originalAlpha)84 	int alpha(int originalAlpha) {
85 		if (TransparencyMode.TRANSPARENCY == Transparency.BITMASK) {
86 			return originalAlpha > 127 ? 255 : 0;
87 		}
88 		return originalAlpha;
89 	}
90 }
91