View Javadoc

1   /*
2    *   Magic-Project is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   * 
19   */
20  package net.sf.magicproject.clickable.targetable.card;
21  
22  import java.awt.Color;
23  import java.awt.Dimension;
24  import java.awt.Graphics;
25  import java.awt.Graphics2D;
26  import java.awt.Image;
27  import java.awt.event.MouseEvent;
28  import java.awt.event.MouseListener;
29  import java.awt.geom.AffineTransform;
30  import java.io.FileInputStream;
31  import java.io.IOException;
32  
33  import javax.swing.JComponent;
34  
35  import net.sf.magicproject.database.DatabaseFactory;
36  import net.sf.magicproject.tools.Configuration;
37  import net.sf.magicproject.tools.Log;
38  import net.sf.magicproject.tools.MToolKit;
39  import net.sf.magicproject.ui.Reversable;
40  import net.sf.magicproject.ui.Tappable;
41  import net.sf.magicproject.ui.i18n.LanguageManager;
42  import net.sf.magicproject.ui.i18n.LanguageManagerMDB;
43  
44  /***
45   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
46   */
47  public class Damage extends JComponent implements MouseListener, Tappable,
48  		Reversable {
49  
50  	/***
51  	 * creates a new instance of MDamage
52  	 * 
53  	 * @param cardSource
54  	 *          is the card source of this damage
55  	 * @param damageNumber
56  	 *          the amount of damage to do
57  	 * @param damageType
58  	 *          the type of this damage (prevent/dealt, lose life or gain life).
59  	 */
60  	public Damage(MCard cardSource, int damageNumber, int damageType) {
61  		this.cardSource = cardSource;
62  		this.damageType = damageType;
63  		this.damageNumber = damageNumber;
64  		addMouseListener(this);
65  		updateSize();
66  	}
67  
68  	@Override
69  	public void paintComponent(Graphics g) {
70  		// super.paintComponent(g);
71  		Graphics2D g2D = (Graphics2D) g;
72  
73  		// draw the rounded black rectangle
74  		if (isHighLighted) {
75  			g2D.setColor(highLightColor);
76  		} else {
77  			g2D.setColor(Color.BLACK);
78  		}
79  		if (tapped) {
80  			g2D.fillRoundRect(0, 0, CardFactory.cardHeight, CardFactory.cardWidth, 3,
81  					3);
82  		} else {
83  			g2D.fillRoundRect(0, 0, CardFactory.cardWidth, CardFactory.cardHeight, 3,
84  					3);
85  		}
86  
87  		if (reversed && Configuration.getBoolean("reverseArt", true)) {
88  			if (tapped) {
89  				g2D.translate(0, CardFactory.cardWidth);
90  				g2D.rotate(-Math.PI / 2);
91  			} else {
92  				g2D.translate(CardFactory.cardWidth, CardFactory.cardHeight);
93  				g2D.rotate(Math.PI);
94  			}
95  		} else {
96  			if (tapped) {
97  				g2D.translate(CardFactory.cardHeight, 0);
98  				g2D.rotate(Math.PI / 2);
99  			}
100 		}
101 		g2D.drawImage(scaledImage(), 0, 0, null);
102 		g2D.dispose();
103 	}
104 
105 	/***
106 	 * return the card's picture
107 	 * 
108 	 * @return the card's picture
109 	 */
110 	protected Image image() {
111 		return DatabaseFactory.damageImage;
112 	}
113 
114 	/***
115 	 * return the scaled card's picture
116 	 * 
117 	 * @return the scaled card's picture
118 	 */
119 	protected Image scaledImage() {
120 		return DatabaseFactory.damageScaledImage;
121 	}
122 
123 	/***
124 	 * @param idDamageType
125 	 *          the damage type to compare to this
126 	 * @return true if the specified damage type is the same
127 	 */
128 	public boolean isSameDamage(int idDamageType) {
129 		return idDamageType == this.damageType;
130 	}
131 
132 	/***
133 	 * Tap/untap this card
134 	 * 
135 	 * @param tapped
136 	 *          if true the card will be tapped
137 	 */
138 	public void tap(boolean tapped) {
139 		if (this.tapped != tapped) {
140 			this.tapped = tapped;
141 			updateSize();
142 		}
143 	}
144 
145 	/***
146 	 * 
147 	 */
148 	private void updateSize() {
149 		// Update the bounds
150 		if (tapped) {
151 			setPreferredSize(new Dimension(CardFactory.cardHeight,
152 					CardFactory.cardWidth));
153 		} else {
154 			setPreferredSize(new Dimension(CardFactory.cardWidth,
155 					CardFactory.cardHeight));
156 		}
157 		setSize(getPreferredSize());
158 	}
159 
160 	/***
161 	 * is called when mouse is on this card, will disp a preview
162 	 * 
163 	 * @param e
164 	 *          is the mouse event
165 	 * @since 0.71 art author and rules author have been added to the tooltip
166 	 */
167 	public void mouseEntered(MouseEvent e) {
168 		CardFactory.previewCard.setImage(image(), null);
169 		StringBuilder toolTip = new StringBuilder();
170 
171 		// html header and card name
172 		toolTip.append("<html><b>");
173 		toolTip.append(LanguageManager.getString("source"));
174 		toolTip.append(": </b>");
175 		toolTip.append(cardSource);
176 		toolTip.append("<br><b>");
177 		toolTip.append(LanguageManager.getString("amount"));
178 		toolTip.append(": </b>");
179 		toolTip.append(damageNumber);
180 		toolTip.append("<br><b>");
181 		toolTip.append(LanguageManager.getString("damageType"));
182 		toolTip.append(": </b><br>");
183 		for (int i = damageTypes.length; i-- > 0;) {
184 			if (isSameDamage(damageTypes[i])) {
185 				toolTip.append("&nbsp;&nbsp;&nbsp;&nbsp;");
186 				toolTip.append(LanguageManagerMDB.getString(damageTypesName[i]));
187 			}
188 		}
189 
190 		toolTip.append("</html>");
191 		setToolTipText(toolTip.toString());
192 	}
193 
194 	/***
195 	 * @param dbStream
196 	 *          the mdb stream's header.
197 	 * @throws IOException
198 	 *           error during the header read.
199 	 */
200 	public static void init(FileInputStream dbStream) throws IOException {
201 		int count = dbStream.read();
202 		Log.debug("exportedDamageTypeNames (" + count + ")");
203 		damageTypes = new int[count];
204 		damageTypesName = new String[damageTypes.length];
205 		for (int i = count; i-- > 0;) {
206 			damageTypesName[i] = MToolKit.readString(dbStream);
207 			damageTypes[i] = MToolKit.readInt16(dbStream);
208 		}
209 	}
210 
211 	/***
212 	 * The shared scaling transformation for all cards
213 	 */
214 	protected AffineTransform atImageSpace;
215 
216 	public void mouseClicked(MouseEvent e) {
217 		// Ignore this event
218 	}
219 
220 	public void mouseExited(MouseEvent e) {
221 		// Ignore this event
222 	}
223 
224 	public void mousePressed(MouseEvent e) {
225 		// Ignore this event
226 	}
227 
228 	public void mouseReleased(MouseEvent e) {
229 		// Ignore this event
230 	}
231 
232 	public void reverse(boolean reversed) {
233 		this.reversed = reversed;
234 	}
235 
236 	/***
237 	 * Indicates if this card should be tapped or not
238 	 */
239 	public boolean tapped;
240 
241 	/***
242 	 * card dealting this damages
243 	 */
244 	public MCard cardSource;
245 
246 	/***
247 	 * type of damages
248 	 */
249 	public int damageType;
250 
251 	/***
252 	 * type of damages
253 	 */
254 	public int damageNumber;
255 
256 	/***
257 	 * color of current highligth > yellow or red
258 	 */
259 	protected Color highLightColor;
260 
261 	/***
262 	 * is this card is highlighted
263 	 */
264 	public boolean isHighLighted;
265 
266 	private boolean reversed;
267 
268 	private static int[] damageTypes;
269 
270 	private static String[] damageTypesName;
271 }