1 package org.argosfields.battlefield;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.argosfields.image.ImageSet;
6 import org.argosfields.image.TileImageSet;
7 import org.argosfields.image.UnitImageSet;
8 import org.argosfields.model.Tile;
9 import org.argosfields.model.Unit;
10 import org.eclipse.swt.SWT;
11 import org.eclipse.swt.events.DisposeEvent;
12 import org.eclipse.swt.events.DisposeListener;
13 import org.eclipse.swt.graphics.Color;
14 import org.eclipse.swt.graphics.GC;
15 import org.eclipse.swt.graphics.Image;
16 import org.eclipse.swt.graphics.Point;
17 import org.eclipse.swt.graphics.Rectangle;
18 import org.eclipse.swt.widgets.Canvas;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Event;
21
22 /***
23 * BattleFieldView.java
24 *
25 * @author Xavier Cho
26 * @version $Revision: 1.6 $ $Date: 2003/12/01 07:38:04 $
27 */
28 public class BattleFieldView extends Canvas {
29 private static Log log = LogFactory.getLog(BattleFieldView.class);
30
31 private Image mapImage;
32 private Image grayMapImage;
33 private ImageSet tileSet;
34 private ImageSet unitSet;
35 private BattleField battleField;
36 private Context context;
37 private int width;
38 private int height;
39 private int selectedIndex = -1;
40
41 /***
42 * @param parent
43 * @param style
44 */
45 public BattleFieldView(final Composite parent, final int style) {
46 super(parent, style);
47
48 addDisposeListener(new DisposeListener() {
49 public void widgetDisposed(final DisposeEvent event) {
50 onDispose(event);
51 }
52 });
53
54 this.tileSet = TileImageSet.getInstance();
55 this.unitSet = UnitImageSet.getInstance();
56 this.context = new Context(this);
57 }
58
59 public Unit getSelectedUnit() {
60 Unit unit = null;
61
62 if (selectedIndex != -1) {
63 unit = battleField.getUnit(selectedIndex);
64 }
65
66 return unit;
67 }
68
69 public int getSelectedIndex() {
70 return selectedIndex;
71 }
72
73 public void setSelectedIndex(final int selectedIndex) {
74 this.selectedIndex = selectedIndex;
75
76 Event event = new Event();
77
78 event.item = this;
79 event.data = new Integer(selectedIndex);
80
81 this.notifyListeners(SWT.Selection, event);
82 }
83
84 private void onDispose(final DisposeEvent event) {
85 disposeResources();
86 }
87
88 private void disposeResources() {
89 if (mapImage != null) {
90 mapImage.dispose();
91 }
92
93 if (grayMapImage != null) {
94 grayMapImage.dispose();
95 }
96 }
97
98 /***
99 * @return Returns the grayMapImage.
100 */
101 protected Image getGrayMapImage() {
102 return grayMapImage;
103 }
104
105 /***
106 * @return Returns the mapImage.
107 */
108 protected Image getMapImage() {
109 return mapImage;
110 }
111
112 /***
113 * @return
114 */
115 public BattleField getBattleField() {
116 return battleField;
117 }
118
119 /***
120 * @param field
121 */
122 public void setBattleField(final BattleField field) {
123 battleField = field;
124
125 int columns = battleField.getColumns();
126 int rows = battleField.getRows();
127
128 if (columns % 2 == 0) {
129 this.width = (int) ((2f / 3f * columns) * Tile.WIDTH);
130 } else {
131 this.width =
132 (int) ((2f / 3f * (columns - 1) + 2f / 3f) * Tile.WIDTH);
133 }
134
135 this.height = (int) ((rows + 0.5) * Tile.HEIGHT);
136
137 updateMapImages();
138 }
139
140 /***
141 * @see org.eclipse.swt.widgets.Control#computeSize(int, int)
142 */
143 public Point computeSize(final int wHint, final int hHint) {
144 Point size = null;
145
146 if (battleField != null) {
147 size = new Point(width, height);
148 } else {
149 size = super.computeSize(wHint, hHint);
150 }
151
152 return size;
153 }
154
155 protected void updateMapImages() {
156 if (battleField == null) {
157 return;
158 }
159
160 Rectangle bounds = new Rectangle(0, 0, width, height);
161
162 int columns = battleField.getColumns();
163 int rows = battleField.getRows();
164
165 disposeResources();
166
167 Color color = getDisplay().getSystemColor(SWT.COLOR_BLACK);
168
169 this.mapImage = new Image(getDisplay(), bounds);
170 mapImage.setBackground(color);
171
172 GC gc = null;
173 try {
174 gc = new GC(mapImage);
175
176 for (int y = 0; y < rows; y++) {
177 for (int x = 0; x < columns; x++) {
178 Tile tile = battleField.getTile(x, y);
179
180 Rectangle target = battleField.getTileBounds(x, y);
181 tileSet.drawIcon(tile.getIconIndex(), target, mapImage, gc);
182
183 Unit unit = tile.getUnit();
184 if (unit != null) {
185 unitSet.drawIcon(
186 unit.getIconIndex(),
187 target,
188 mapImage,
189 gc);
190 }
191 }
192 }
193
194 this.grayMapImage =
195 new Image(getDisplay(), mapImage, SWT.IMAGE_GRAY);
196 } finally {
197 gc.dispose();
198 }
199
200 redraw();
201 }
202
203 protected void updateMapImages(final int index) {
204 if (battleField == null) {
205 return;
206 }
207
208 Point position = battleField.getPositionFromIndex(index);
209
210 GC gc = null;
211
212 Image image = null;
213 Image grayImage = null;
214
215 try {
216 gc = new GC(mapImage);
217
218 Tile tile = battleField.getTile(position.x, position.y);
219
220 Rectangle target =
221 battleField.getTileBounds(position.x, position.y);
222
223 tileSet.drawIcon(tile.getIconIndex(), target, mapImage, gc);
224
225 Unit unit = tile.getUnit();
226 if (unit != null) {
227 unitSet.drawIcon(unit.getIconIndex(), target, mapImage, gc);
228 }
229
230 gc.dispose();
231
232 image = new Image(getDisplay(), target.width, target.height);
233
234 gc = new GC(image);
235 gc.drawImage(
236 mapImage,
237 target.x,
238 target.y,
239 target.width,
240 target.height,
241 0,
242 0,
243 target.width,
244 target.height);
245
246 gc.dispose();
247
248 grayImage = new Image(getDisplay(), image, SWT.IMAGE_GRAY);
249
250 gc = new GC(grayMapImage);
251
252 gc.drawImage(
253 grayImage,
254 0,
255 0,
256 target.width,
257 target.height,
258 target.x,
259 target.y,
260 target.width,
261 target.height);
262 } finally {
263 gc.dispose();
264 image.dispose();
265 grayImage.dispose();
266 }
267 }
268 }
This page was automatically generated by Maven