1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package net.sf.magicproject.tools;
19
20 import java.awt.Color;
21 import java.awt.Graphics;
22 import java.awt.Image;
23 import java.io.IOException;
24 import java.io.InputStream;
25
26 import net.sf.magicproject.clickable.targetable.card.MCard;
27 import net.sf.magicproject.test.Test;
28 import net.sf.magicproject.test.TestFactory;
29
30 /***
31 * StatePicture.java Created on 3 mars 2004 Followin the state, draw a picture
32 * at a determined position.
33 *
34 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
35 * @since 0.60
36 * @since 0.82 the register index is now used. If state value is 0, the string
37 * representing the value of register is written on the graphic.
38 */
39 public class StatePicture {
40
41 /***
42 * read from the specified stream the state picture options. The current
43 * offset of the stream must pointing on the number of state pictures. <br>
44 * <ul>
45 * Structure of InputStream : Data[size]
46 * <li>picture name + '\0' [...]</li>
47 * <li>state name + '\0' [...]</li>
48 * <li>state value [2]</li>
49 * <li>state index [1]</li>
50 * <li>x [2]</li>
51 * <li>y [2]</li>
52 * <li>width [2]</li>
53 * <li>height [2]</li>
54 * <li>display test [...]
55 * </ul>
56 *
57 * @param inputFile
58 * file containing the states picture
59 * @throws IOException
60 * If some other I/O error occurs
61 */
62 public StatePicture(InputStream inputFile) throws IOException {
63 picture = Picture.loadImage(MToolKit.getTbsPicture("properties/"
64 + MToolKit.readString(inputFile)));
65 name = MToolKit.readString(inputFile);
66 state = MToolKit.readInt16(inputFile);
67 index = inputFile.read();
68 x = MToolKit.getConstant(MToolKit.readInt16(inputFile));
69 y = MToolKit.getConstant(MToolKit.readInt16(inputFile));
70 width = MToolKit.readInt16(inputFile);
71 height = MToolKit.readInt16(inputFile);
72 displayTest = TestFactory.readNextTest(inputFile);
73 }
74
75 /***
76 * Draw this state on the specified card using the given graphics. If the test
77 * of this state is evaluated to <code>false</code>, no picture and no text
78 * will be drawn and false value will be returned.
79 *
80 * @param owner
81 * the card this state is applied on.
82 * @param g
83 * the graphics.
84 * @param currentX
85 * current relative X position of cursor
86 * @param currentY
87 * current relative Y position of cursor
88 * @return true if this state picture has been drawn just after the given
89 * relative poistion of cursor.
90 * @see java.awt.Component#paint(java.awt.Graphics)
91 */
92 public boolean paint(MCard owner, Graphics g, int currentX, int currentY) {
93 final int regValue = owner.getValue(index);
94 if (regValue > 0 && displayTest.test(null, owner)) {
95 if (state == 0) {
96
97 g.drawImage(picture, x == -1 ? currentX : x, y == -1 ? currentY : y,
98 width, height, null);
99 g.setColor(Color.BLACK);
100 g.drawString(String.valueOf(regValue), x, y);
101 g.setColor(Color.RED);
102 g.drawString(String.valueOf(regValue), x + 1, y);
103 return x == -1;
104 } else if (hasState(regValue)) {
105 g.drawImage(picture, x == -1 ? currentX : x, y == -1 ? currentY : y,
106 width, height, null);
107 return x == -1;
108 }
109 }
110 return false;
111 }
112
113 /***
114 * Indicates if this state suits to the specified stete
115 *
116 * @param fullState
117 * the full state number
118 * @return true if this state suits to the specified stete
119 */
120 public boolean hasState(int fullState) {
121 return state != 0 && (fullState & state) == state;
122 }
123
124 /***
125 * The picture to draw when the state value is the right one
126 */
127 private Image picture;
128
129 /***
130 * The state name
131 */
132 private String name;
133
134 /***
135 * this state value is used to determine if the picture must be drawn or not
136 */
137 private int state;
138
139 /***
140 * x where picture would be drawn
141 */
142 private int x;
143
144 /***
145 * y where picture would be drawn
146 */
147 private int y;
148
149 /***
150 * picture's width
151 */
152 private int width;
153
154 /***
155 * picture's height
156 */
157 private int height;
158
159 /***
160 * The register index
161 */
162 private int index;
163
164 /***
165 * The test determining if the picture associated to this state will be
166 * displayed or not.
167 */
168 private Test displayTest;
169
170 /***
171 * Return the state name.
172 *
173 * @return the state name.
174 */
175 @Override
176 public String toString() {
177 return name;
178 }
179 }