1
package org.xpilot.jxpmap;
4
import java.awt.Graphics2D;
6
import java.awt.Polygon;
7
import java.awt.Rectangle;
9
import java.awt.geom.Point2D;
10
import java.awt.geom.AffineTransform;
11
import java.awt.geom.GeneralPath;
12
import java.awt.AWTEvent;
13
import java.awt.event.MouseEvent;
14
import java.io.IOException;
15
import java.io.PrintWriter;
16
import java.util.Collection;
19
public class Cannon extends Group {
21
private static final GeneralPath ARROW = new GeneralPath();
30
private static final Point HEAD = new Point(30, 0);
32
private int x, y, dir;
36
public MapObject copy() {
37
Cannon copy = (Cannon)super.copy();
38
copy.arrow = new GeneralPath(arrow);
42
public Object deepClone(Map context) {
43
Cannon clone = (Cannon)super.deepClone(context);
44
clone.arrow = new GeneralPath(arrow);
54
public Cannon(Collection c) {
56
x = (int)getBounds().getCenterX();
57
y = (int)getBounds().getCenterY();
61
public Cannon(Collection c, int team, int x, int y, int dir) {
76
public void setDir(int dir) {
77
if (dir > 127) dir = 127;
84
public void setPoint(Point p) {
88
public void setPoint(int x, int y) {
96
public Point getPoint() {
97
return new Point(x, y);
101
public void moveTo(int x, int y) {
102
Rectangle r = getBounds();
109
protected void updateArrow() {
111
computeTransform(x, y, 2 * Math.PI * getDir() / 128.0);
112
arrow = at.createTransformedShape(ARROW);
113
head = at.transform(HEAD, new Point());
116
protected AffineTransform computeTransform(int x, int y, double a) {
117
AffineTransform at = new AffineTransform();
124
public int getTeam() {
129
public void setTeam(int team) {
130
if (team < -1 || team > 10)
131
throw new IllegalArgumentException
132
("illegal team: " + team);
137
public void printXml(PrintWriter out) throws IOException {
138
out.println("<Cannon x=\"" + x + "\" y=\"" + y
139
+ "\" dir=\"" + dir + "\" team=\"" + team + "\">");
140
super.printMemberXml(out);
141
out.println("</Cannon>");
145
public EditorPanel getPropertyEditor(MapCanvas c) {
146
return new TeamEditor("Cannon", c, this);
149
public void paint(Graphics2D g, float scale) {
150
super.paint(g, scale);
151
g.setColor(Color.yellow);
155
public boolean checkAwtEvent(MapCanvas canvas, AWTEvent evt) {
156
if (canvas.getMode() == MapCanvas.MODE_EDIT
157
&& evt.getID() == MouseEvent.MOUSE_PRESSED) {
158
MouseEvent me = (MouseEvent)evt;
160
(me.getModifiers() & MouseEvent.BUTTON1_MASK) != 0) {
161
double thSq = 100 / (canvas.getScale() * canvas.getScale());
162
if (Point2D.distanceSq(x, y, me.getX(), me.getY()) < thSq) {
163
canvas.setCanvasEventHandler(
164
new ArrowMoveHandler(me));
166
} else if (Point2D.distanceSq(head.getX(), head.getY(),
167
me.getX(), me.getY()) < thSq) {
168
canvas.setCanvasEventHandler(
169
new ArrowRotateHandler(me));
174
return super.checkAwtEvent(canvas, evt);
177
protected class ArrowMoveHandler extends CanvasEventAdapter {
179
private Shape preview;
180
private double angle;
181
private boolean clear;
183
public ArrowMoveHandler(MouseEvent evt) {
184
preview = new GeneralPath(Cannon.this.arrow);
185
angle = 2 * Math.PI * getDir() / 128.0;
187
public void mouseReleased(MouseEvent evt) {
188
MapCanvas c = (MapCanvas)evt.getSource();
189
c.setCanvasEventHandler(null);
190
c.setCannonPoint(Cannon.this, evt.getX(), evt.getY());
193
public void mouseDragged(MouseEvent evt) {
194
MapCanvas c = (MapCanvas)evt.getSource();
195
Graphics2D g = (Graphics2D)c.getGraphics();
196
g.setColor(Color.white);
197
g.setXORMode(Color.black);
198
if (clear) c.drawShape(g, preview);
199
preview = computeTransform(evt.getX(), evt.getY(), angle)
200
.createTransformedShape(ARROW);
201
c.drawShape(g, preview);
206
protected class ArrowRotateHandler extends CanvasEventAdapter {
208
private Shape preview;
209
private boolean clear;
210
private double angle;
212
public ArrowRotateHandler(MouseEvent evt) {
213
preview = new GeneralPath(Cannon.this.arrow);
215
public void mouseReleased(MouseEvent evt) {
216
MapCanvas c = (MapCanvas)evt.getSource();
217
c.setCanvasEventHandler(null);
218
if (angle < 0) angle += 2 * Math.PI;
219
c.setCannonDir(Cannon.this, (int)(64 * angle / Math.PI));
222
public void mouseDragged(MouseEvent evt) {
223
MapCanvas c = (MapCanvas)evt.getSource();
224
Graphics2D g = (Graphics2D)c.getGraphics();
225
g.setColor(Color.white);
226
g.setXORMode(Color.black);
227
if (clear) c.drawShape(g, preview);
228
angle = Math.atan2(evt.getY() - y, evt.getX() - x);
229
preview = computeTransform(x, y, angle)
230
.createTransformedShape(ARROW);
231
c.drawShape(g, preview);