1 package org.argosfields.widget;
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.model.Tile;
8 import org.argosfields.model.Unit;
9 import org.argosfields.resource.ResourceManager;
10 import org.eclipse.swt.events.PaintEvent;
11 import org.eclipse.swt.events.PaintListener;
12 import org.eclipse.swt.graphics.Image;
13 import org.eclipse.swt.graphics.Point;
14 import org.eclipse.swt.graphics.Rectangle;
15 import org.eclipse.swt.widgets.Canvas;
16 import org.eclipse.swt.widgets.Composite;
17
18 /***
19 * TerrainSet.java
20 *
21 * @author Xavier Cho
22 * @version $Revision: 1.2 $ $Date: 2003/12/01 07:38:04 $
23 */
24 public class TerrainSet extends Canvas {
25 private static Log log = LogFactory.getLog(TerrainSet.class);
26
27 private ImageSet tileSet;
28 private Image terrainMask;
29 private Unit unit;
30 private Tile plains;
31 private Tile forrest;
32 private Tile mountain;
33 private Tile trench;
34 private Tile rail;
35 private Tile water;
36 private Tile deepWater;
37 private int width;
38 private int height;
39
40 /***
41 * @param parent
42 * @param style
43 */
44 public TerrainSet(final Composite parent, final int style) {
45 super(parent, style);
46
47 addPaintListener(new PaintListener() {
48 public void paintControl(final PaintEvent event) {
49 onPaint(event);
50 }
51 });
52
53 ResourceManager resources = ResourceManager.getInstance();
54
55 this.terrainMask = resources.getImage("image.terrain-mask");
56
57 this.width = Tile.WIDTH * 4;
58 this.height = Tile.HEIGHT * 2;
59
60 setSize(width, height);
61
62 this.plains = new Tile(30);
63 this.forrest = new Tile(81);
64 this.mountain = new Tile(117);
65 this.trench = new Tile(150);
66 this.rail = new Tile(250);
67 this.water = new Tile(360);
68 this.deepWater = new Tile(362);
69
70 this.tileSet = TileImageSet.getInstance();
71 }
72
73 /***
74 * @see org.eclipse.swt.widgets.Control#computeSize(int, int)
75 */
76 public Point computeSize(final int wHint, final int hHint) {
77 return new Point(width, height);
78 }
79
80 /***
81 * @param unit
82 * The unit to set.
83 */
84 protected void setUnit(final Unit unit) {
85 this.unit = unit;
86 redraw();
87 }
88
89 private void onPaint(final PaintEvent event) {
90 if (unit != null) {
91 Rectangle bounds = new Rectangle(0, 0, Tile.WIDTH, Tile.HEIGHT);
92 tileSet.drawIcon(plains.getIconIndex(), bounds, this, event.gc);
93 if (!unit.canTraverse(plains)) {
94 event.gc.drawImage(terrainMask, bounds.x, bounds.y);
95 }
96
97 bounds = new Rectangle(Tile.WIDTH, 0, Tile.WIDTH, Tile.HEIGHT);
98 tileSet.drawIcon(forrest.getIconIndex(), bounds, this, event.gc);
99 if (!unit.canTraverse(forrest)) {
100 event.gc.drawImage(terrainMask, bounds.x, bounds.y);
101 }
102
103 bounds = new Rectangle(Tile.WIDTH * 2, 0, Tile.WIDTH, Tile.HEIGHT);
104 tileSet.drawIcon(mountain.getIconIndex(), bounds, this, event.gc);
105 if (!unit.canTraverse(mountain)) {
106 event.gc.drawImage(terrainMask, bounds.x, bounds.y);
107 }
108
109 bounds = new Rectangle(Tile.WIDTH * 3, 0, Tile.WIDTH, Tile.HEIGHT);
110 tileSet.drawIcon(trench.getIconIndex(), bounds, this, event.gc);
111 if (!unit.canTraverse(trench)) {
112 event.gc.drawImage(terrainMask, bounds.x, bounds.y);
113 }
114
115 bounds =
116 new Rectangle(
117 (int) (Tile.WIDTH * 0.5),
118 Tile.HEIGHT,
119 Tile.WIDTH,
120 Tile.HEIGHT);
121 tileSet.drawIcon(rail.getIconIndex(), bounds, this, event.gc);
122 if (!unit.canTraverse(rail)) {
123 event.gc.drawImage(terrainMask, bounds.x, bounds.y);
124 }
125
126 bounds =
127 new Rectangle(
128 (int) (Tile.WIDTH * 1.5),
129 Tile.HEIGHT,
130 Tile.WIDTH,
131 Tile.HEIGHT);
132 tileSet.drawIcon(water.getIconIndex(), bounds, this, event.gc);
133 if (!unit.canTraverse(water)) {
134 event.gc.drawImage(terrainMask, bounds.x, bounds.y);
135 }
136
137 bounds =
138 new Rectangle(
139 (int) (Tile.WIDTH * 2.5),
140 Tile.HEIGHT,
141 Tile.WIDTH,
142 Tile.HEIGHT);
143 tileSet.drawIcon(deepWater.getIconIndex(), bounds, this, event.gc);
144 if (!unit.canTraverse(deepWater)) {
145 event.gc.drawImage(terrainMask, bounds.x, bounds.y);
146 }
147 }
148 }
149 }
This page was automatically generated by Maven