View Javadoc
1 package org.argosfields.battlefield; 2 3 import org.argosfields.image.ImageSet; 4 import org.argosfields.image.TileImageSet; 5 import org.argosfields.image.UnitImageSet; 6 import org.argosfields.model.Tile; 7 import org.argosfields.model.Unit; 8 import org.eclipse.jface.util.Assert; 9 import org.eclipse.swt.SWT; 10 import org.eclipse.swt.events.KeyEvent; 11 import org.eclipse.swt.events.MouseEvent; 12 import org.eclipse.swt.events.PaintEvent; 13 import org.eclipse.swt.graphics.Image; 14 import org.eclipse.swt.graphics.Rectangle; 15 16 /*** 17 * SelectDestinationState.java 18 * 19 * @author Xavier Cho 20 * @version $Revision: 1.7 $ $Date: 2003/12/01 07:38:05 $ 21 */ 22 public class SelectDestinationState extends State { 23 private BattleFieldView battleFieldView; 24 private Unit unit; 25 private Tile tile; 26 private int unitIndex; 27 private int[] recheableIndexes; 28 private Simulator simulator; 29 30 /*** 31 * @param context 32 */ 33 public SelectDestinationState(final int index, final Context context) { 34 super(context); 35 36 this.battleFieldView = context.getBattleFieldView(); 37 this.unitIndex = index; 38 39 if (unitIndex == -1) { 40 SWT.error(SWT.ERROR_INVALID_ARGUMENT); 41 } 42 43 BattleField battleField = battleFieldView.getBattleField(); 44 45 this.tile = battleField.getTile(unitIndex); 46 if (tile == null) { 47 SWT.error(SWT.ERROR_INVALID_ARGUMENT); 48 } 49 50 this.unit = tile.getUnit(); 51 52 if (unit == null) { 53 SWT.error(SWT.ERROR_INVALID_ARGUMENT); 54 } 55 56 this.simulator = new Simulator(battleField, unitIndex); 57 this.recheableIndexes = simulator.getReachableTileIndexes(); 58 59 battleFieldView.redraw(); 60 } 61 62 /*** 63 * @see org.argosfields.widget.state.State#getName() 64 */ 65 public String getName() { 66 return "SelectDestination"; 67 } 68 69 /*** 70 * @see org.eclipse.swt.events.MouseListener#mouseDoubleClick(org.eclipse.swt.events.MouseEvent) 71 */ 72 public void mouseDoubleClick(final MouseEvent event) { 73 BattleField battleField = battleFieldView.getBattleField(); 74 int index = battleField.getIndexFromPoint(event.x, event.y); 75 76 Context context = getContext(); 77 78 if (index != unitIndex) { 79 boolean found = false; 80 81 for (int i = 0; i < recheableIndexes.length; i++) { 82 if (recheableIndexes[i] == index) { 83 found = true; 84 break; 85 } 86 } 87 88 if (found) { 89 int[] path = simulator.getShortestPath(index); 90 Assert.isNotNull(path); 91 92 context.setCurrentState(new MovingState(unit, path, context)); 93 } else { 94 context.setCurrentState(new DefaultState(context)); 95 } 96 } else { 97 context.setCurrentState(new DefaultState(context)); 98 } 99 } 100 101 /*** 102 * @see org.eclipse.swt.events.MouseListener#mouseDown(org.eclipse.swt.events.MouseEvent) 103 */ 104 public void mouseDown(final MouseEvent event) { 105 BattleField battleField = battleFieldView.getBattleField(); 106 107 int previousIndex = battleFieldView.getSelectedIndex(); 108 int index = battleField.getIndexFromPoint(event.x, event.y); 109 110 battleFieldView.setSelectedIndex(index); 111 112 if (previousIndex != -1) { 113 Rectangle bounds = battleField.getTileBounds(previousIndex); 114 battleFieldView.redraw( 115 bounds.x, 116 bounds.y, 117 bounds.width, 118 bounds.height, 119 false); 120 } 121 122 if (index != -1) { 123 Rectangle bounds = battleField.getTileBounds(index); 124 battleFieldView.redraw( 125 bounds.x, 126 bounds.y, 127 bounds.width, 128 bounds.height, 129 false); 130 } 131 } 132 133 public void mouseUp(final MouseEvent event) { 134 } 135 136 public void keyPressed(final KeyEvent event) { 137 BattleFieldView view = getContext().getBattleFieldView(); 138 139 int index = view.getSelectedIndex(); 140 BattleField field = view.getBattleField(); 141 } 142 143 public void keyReleased(final KeyEvent event) { 144 } 145 146 public void paintControl(final PaintEvent event) { 147 Image image = battleFieldView.getGrayMapImage(); 148 149 if (image != null && image != null) { 150 event.gc.drawImage( 151 image, 152 event.x, 153 event.y, 154 event.width, 155 event.height, 156 event.x, 157 event.y, 158 event.width, 159 event.height); 160 161 int selectedIndex = battleFieldView.getSelectedIndex(); 162 163 BattleField battleField = battleFieldView.getBattleField(); 164 Rectangle paintArea = 165 new Rectangle(event.x, event.y, event.width, event.height); 166 167 for (int i = 0; i < recheableIndexes.length; i++) { 168 int index = recheableIndexes[i]; 169 170 Rectangle bounds = battleField.getTileBounds(index); 171 if (bounds.intersects(paintArea)) { 172 ImageSet unitSet = UnitImageSet.getInstance(); 173 ImageSet tileSet = TileImageSet.getInstance(); 174 175 Tile t = battleField.getTile(index); 176 177 tileSet.drawIcon( 178 t.getIconIndex(), 179 bounds, 180 battleFieldView, 181 event.gc); 182 183 Unit u = battleField.getUnit(index); 184 if (u != null) { 185 unitSet.drawIcon( 186 u.getIconIndex(), 187 bounds, 188 battleFieldView, 189 event.gc); 190 } 191 } 192 } 193 194 if (selectedIndex != -1) { 195 Rectangle bounds = battleField.getTileBounds(selectedIndex); 196 197 if (bounds.intersects(paintArea)) { 198 ImageSet tileSet = TileImageSet.getInstance(); 199 200 tileSet.drawIcon( 201 TileImageSet.OVERLAY_DESTINATION, 202 bounds, 203 battleFieldView, 204 event.gc); 205 } 206 } 207 } 208 } 209 }

This page was automatically generated by Maven