scg/ch02/MouseTrackingLine

From FANG

Jump to: navigation, search

001 package scg.ch02;
002 
003 import fang2.attributes.Location2D;
004 import fang2.attributes.Vector2D;
005 import fang2.sprites.LineSprite;
006 import fang2.transformers.MouseClickTransformer;
007 import fang2.transformers.MouseClickTransformerListener;
008 import fang2.transformers.MouseMovementTransformer;
009 import fang2.transformers.MouseMovementTransformerListener;
010 
011 public class MouseTrackingLine
012   extends LineSprite
013   implements MouseMovementTransformerListener,
014     MouseClickTransformerListener {
015   /** true during the frame after moving goes to false */
016   private boolean justClicked;
017 
018   /** is this line currently tracking? True if yes, false otherwise */
019   private boolean moving;
020 
021   /** which player's input is being tracked by this line? */
022   private int playerId;
023 
024   /**
025    * Create a new {@link MouseTrackingLine} with its anchor at the given
026    * point. The new line is created in the moving state. The line will
027    * track the default player's mouse.
028    *
029    @param  x  x-coordinate of the anchor point
030    @param  y  y-coordinate of the anchor point
031    */
032   public MouseTrackingLine(double x, double y{
033     this(new Location2D(x, y));
034   }
035 
036   /**
037    * Create a new {@link MouseTrackingLine} with the given anchor and
038    * moving state. Line will track the default player's mouse.
039    *
040    @param  x       x-coordinate of the anchor point
041    @param  y       y-coordinate of the anchor point
042    @param  moving  true if line should track mouse now, false
043    *                 otherwise.
044    */
045   public MouseTrackingLine(double x, double y, boolean moving{
046     this(new Location2D(x, y), moving);
047   }
048   /**
049    * Create a new {@link MouseTrackingLine} with its beginning anchor at
050    * the given point and in the given moving state.
051    *
052    @param  x         x-coordinate of the anchor point
053    @param  y         y-coordinate of the anchor point
054    @param  moving    true if line should track mouse now, false
055    *                   otherwise.
056    @param  playerId  identifier of player whose mouse should be
057    *                   tracked
058    */
059   public MouseTrackingLine(double x, double y, boolean moving,
060     int playerId{
061     this(new Location2D(x, y), moving, playerId);
062   }
063 
064   /**
065    * Create a new {@link MouseTrackingLine} with its anchor at the given
066    * point. The new line is created in the moving state. The line will
067    * track the default player's mouse.
068    *
069    @param  anchor  the location (screen coordinates) where the
070    *                 beginning of the line should be anchored
071    */
072   public MouseTrackingLine(Location2D anchor{
073     this(anchor, true);
074   }
075 
076   /**
077    * Create a new {@link MouseTrackingLine} with the given anchor and
078    * moving state. Line will track the default player's mouse.
079    *
080    @param  anchor  screen location to anchor beginning of line.
081    @param  moving  true if line should track mouse now, false
082    *                 otherwise.
083    */
084   public MouseTrackingLine(Location2D anchor, boolean moving{
085     this(anchor, moving, 0);
086   }
087 
088   /**
089    * Create a new {@link MouseTrackingLine} with its beginning anchor at
090    * the given point and in the given moving state.
091    *
092    @param  anchor    screen location to anchor beginning of line.
093    @param  moving    true if line should track mouse now, false
094    *                   otherwise
095    @param  playerId  identifier of player whose mouse should be
096    *                   tracked
097    */
098   public MouseTrackingLine(Location2D anchor, boolean moving,
099     int playerId{
100     super(anchor, anchor);
101     this.moving = moving;
102     this.playerId = playerId;
103     MouseMovementTransformer mmt = new MouseMovementTransformer(
104         playerId);
105     mmt.add(this);
106     MouseClickTransformer mct = new MouseClickTransformer(playerId);
107     mct.add(this);
108   }
109 
110   /**
111    * Get the point which is anchored (the start of the line).
112    @return the anchored point of the line segment
113    */
114   public Location2D getAnchor() {
115     return getStart();
116   }
117 
118   /**
119    @return  the playerId
120    */
121   public int getPlayerId() {
122     return playerId;
123   }
124 
125   /**
126    * Get the vector represented by this line.
127    *
128    @return  a vector going from start to end in screen coordinates.
129    */
130   public Vector2D getVector() {
131     return new Vector2D(getStart(), getEnd());
132   }
133 
134   /**
135    * Get and clear the justClicked flag. 
136    @return true if the line was just clicked, false otherwise.
137    */
138   public boolean isJustClicked() {
139     boolean is = justClicked;
140     justClicked = false;
141     return is;
142   }
143   /**
144    @return  the moving
145    */
146   public boolean isMoving() {
147     return moving;
148   }
149 
150   /**
151    * Respond to any mouse button being clicked. Just change the moving state to not moving.
152    */
153   public void mouseClickedAt(Location2D location, int mouseButton{
154     justClicked = moving;
155     moving = false;
156   }
157 
158   /**
159    * Listener method called by the mouse movement transformer.
160    */
161   public void mouseMovedTo(Location2D location{
162     if (moving{
163       this.setEnd(location);
164     } 
165   }
166 
167   /**
168    @param  moving  the moving to set
169    */
170   public void setMoving(boolean moving{
171     this.moving = moving;
172   }
173 
174   /**
175    @param  playerId  the playerId to set
176    */
177   public void setPlayerId(int playerId{
178     this.playerId = playerId;
179   }
180 }
181 
182 //Uploaded on Mon Mar 29 21:38:32 EDT 2010


Download/View scg/ch02/MouseTrackingLine.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter

Games
Games