~ubuntu-branches/ubuntu/trusty/xpilot-ng/trusty

« back to all changes in this revision

Viewing changes to contrib/jxpmap/org/xpilot/jxpmap/Cannon.java

  • Committer: Bazaar Package Importer
  • Author(s): Ben Armstrong
  • Date: 2008-03-18 12:33:36 UTC
  • mfrom: (1.1.2 lenny)
  • Revision ID: james.westby@ubuntu.com-20080318123336-x1qjwdauqfe2g215
Tags: 1:4.7.3~cvs20080224-1
* CVS snapshot, preparing for 4.7.3 release.
* Fix dependencies to make binNMU safe. (Closes: #435967)
* Moved maps and server configs from -common to -server and dropped -server
  dependency on -common. (Closes: #436061)
* Added README.Debian explaining change in default key mapping from
  classic XPilot. (Closes: #324413)
* Changed encoding in VERSION string to UTF-8. (Closes: #387170)
* Added LSB formatted dependency info in init.d script. (Closes: #460522)
* Must build conflict with x11proto-xf86misc-dev so that HAVE_XF86MISC
  will not be set, as otherwise XF86MiscGetMouseSettings fails on the new
  xorg, crashing the client.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.xpilot.jxpmap;
 
2
 
 
3
import java.awt.Color;
 
4
import java.awt.Graphics2D;
 
5
import java.awt.Point;
 
6
import java.awt.Polygon;
 
7
import java.awt.Rectangle;
 
8
import java.awt.Shape;
 
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;
 
17
import java.util.Map;
 
18
 
 
19
public class Cannon extends Group {
 
20
    
 
21
    private static final GeneralPath ARROW = new GeneralPath();
 
22
    static {
 
23
        ARROW.moveTo(0, 0);
 
24
        ARROW.lineTo(27, 0);
 
25
        ARROW.lineTo(27, 3);
 
26
        ARROW.lineTo(30, 0);
 
27
        ARROW.lineTo(27, -3);
 
28
        ARROW.lineTo(27, 0);
 
29
    }
 
30
    private static final Point HEAD = new Point(30, 0);
 
31
 
 
32
    private int x, y, dir;
 
33
    private Shape arrow;
 
34
    private Point2D head;
 
35
    
 
36
    public MapObject copy() {
 
37
        Cannon copy = (Cannon)super.copy();
 
38
        copy.arrow = new GeneralPath(arrow);
 
39
        return copy;
 
40
    }
 
41
 
 
42
    public Object deepClone(Map context) {
 
43
        Cannon clone = (Cannon)super.deepClone(context);
 
44
        clone.arrow = new GeneralPath(arrow);
 
45
        return clone;
 
46
    }
 
47
    
 
48
    public Cannon() {
 
49
        super();
 
50
        setTeam(-1);
 
51
        updateArrow();
 
52
    }
 
53
    
 
54
    public Cannon(Collection c) {
 
55
        super(c);
 
56
        x = (int)getBounds().getCenterX();
 
57
        y = (int)getBounds().getCenterY();
 
58
        updateArrow();
 
59
    }
 
60
 
 
61
    public Cannon(Collection c, int team, int x, int y, int dir) {
 
62
        super(c);
 
63
        setTeam(team);
 
64
        this.dir = dir;
 
65
        this.x = x;
 
66
        this.y = y;
 
67
        updateArrow();
 
68
    }
 
69
 
 
70
 
 
71
    public int getDir() {
 
72
        return dir;
 
73
    }
 
74
 
 
75
 
 
76
    public void setDir(int dir) {
 
77
        if (dir > 127) dir = 127;
 
78
        if (dir < 0) dir = 0;
 
79
        this.dir = dir;
 
80
        updateArrow();
 
81
    }
 
82
    
 
83
    
 
84
    public void setPoint(Point p) {
 
85
        setPoint(p.x, p.y);
 
86
    }
 
87
    
 
88
    public void setPoint(int x, int y) {
 
89
        this.x = x;
 
90
        this.y = y;
 
91
        updateArrow();
 
92
    }
 
93
 
 
94
    
 
95
    
 
96
    public Point getPoint() {
 
97
        return new Point(x, y);
 
98
    }
 
99
    
 
100
 
 
101
    public void moveTo(int x, int y) {
 
102
        Rectangle r = getBounds();
 
103
        this.x += x - r.x;
 
104
        this.y += y - r.y;
 
105
        updateArrow();        
 
106
        super.moveTo(x, y);
 
107
    }
 
108
    
 
109
    protected void updateArrow() {
 
110
        AffineTransform at = 
 
111
            computeTransform(x, y, 2 * Math.PI * getDir() / 128.0);
 
112
        arrow = at.createTransformedShape(ARROW);
 
113
        head = at.transform(HEAD, new Point());
 
114
    }
 
115
        
 
116
    protected AffineTransform computeTransform(int x, int y, double a) {
 
117
        AffineTransform at = new AffineTransform();
 
118
        at.translate(x, y);
 
119
        at.rotate(a);
 
120
        at.scale(64, 64);
 
121
        return at;
 
122
    }
 
123
 
 
124
    public int getTeam() {
 
125
        return team;
 
126
    }
 
127
 
 
128
 
 
129
    public void setTeam(int team) {
 
130
        if (team < -1 || team > 10)
 
131
            throw new IllegalArgumentException
 
132
                ("illegal team: " + team);
 
133
        this.team = team;
 
134
    }
 
135
 
 
136
    
 
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>");
 
142
    }
 
143
 
 
144
    
 
145
    public EditorPanel getPropertyEditor(MapCanvas c) {
 
146
        return new TeamEditor("Cannon", c, this);
 
147
    }
 
148
 
 
149
    public void paint(Graphics2D g, float scale) {
 
150
        super.paint(g, scale);
 
151
        g.setColor(Color.yellow);
 
152
        g.draw(arrow);
 
153
    }
 
154
    
 
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;
 
159
            if (
 
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));
 
165
                    return true;
 
166
                } else if (Point2D.distanceSq(head.getX(), head.getY(), 
 
167
                    me.getX(), me.getY()) < thSq) {
 
168
                    canvas.setCanvasEventHandler(
 
169
                        new ArrowRotateHandler(me));
 
170
                    return true;
 
171
                }
 
172
            }
 
173
        }
 
174
        return super.checkAwtEvent(canvas, evt);
 
175
    }
 
176
    
 
177
    protected class ArrowMoveHandler extends CanvasEventAdapter {
 
178
        
 
179
        private Shape preview;
 
180
        private double angle;
 
181
        private boolean clear;
 
182
        
 
183
        public ArrowMoveHandler(MouseEvent evt) {
 
184
            preview = new GeneralPath(Cannon.this.arrow);
 
185
            angle = 2 * Math.PI * getDir() / 128.0;
 
186
        }
 
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());
 
191
            c.repaint();            
 
192
        }
 
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);
 
202
            clear = true;            
 
203
        }                
 
204
    }
 
205
    
 
206
    protected class ArrowRotateHandler extends CanvasEventAdapter {
 
207
        
 
208
        private Shape preview;
 
209
        private boolean clear;
 
210
        private double angle;
 
211
 
 
212
        public ArrowRotateHandler(MouseEvent evt) {
 
213
            preview = new GeneralPath(Cannon.this.arrow);
 
214
        }        
 
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));
 
220
            c.repaint();
 
221
        }
 
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);
 
232
            clear = true;
 
233
        }
 
234
    }
 
235
}