scg/ch13/highscore/ScrollingMessageBox

From FANG

Jump to: navigation, search

001 package scg.ch13.highscore;
002 
003 import java.awt.Color;
004 import java.util.ArrayList;
005 
006 import fang2.sprites.CompositeSprite;
007 import fang2.sprites.StringSprite;
008 
009 public class ScrollingMessageBox
010   extends CompositeSprite {
011   /** extra lines between last item and first item again */
012   public static final int DEFAULT_GAP = 1;
013 
014   /** vertical distance between adjacent messages; internal screens */
015   public static final double DEFAULT_LINE_HEIGHT = 0.2;
016 
017   /** speed, screens/second, of scrolling (if no other value set) */
018   public static final double DEFAULT_VELOCITY = -0.25;
019 
020   /** coordinate, in sprite coordinate screens, top edge of messages */
021   public static final double TOP_EDGE = -0.5;
022 
023   /** gap between last entry and first entry when cycling; in lines */
024   private int gap;
025 
026   /** the index of the top (on the screen) display StringSprite */
027   private int indexOfTopLineOnScreen;
028 
029   /** the distance between the lines, in sprite screens */
030   private double lineHeight;
031 
032   /** the displayed list of StringSprite */
033   private final ArrayList<StringSprite> messages;
034 
035   /** scrolling speed in screens/second */
036   private double velocity;
037 
038   /**
039    * Create a new {@link ScrollingMessageBox} with all default values.
040    */
041   public ScrollingMessageBox() {
042     this(new ArrayList<String>(), DEFAULT_VELOCITY, DEFAULT_GAP);
043   }
044 
045   /**
046    * Create new {@link ScrollingMessageBox} with given strings in it.
047    * The scrolling velocity and gap are also set here.
048    *
049    @param  messageStrings  list of strings to display
050    @param  velocity        scrolling velocity in screens/second
051    @param  gap             distance, in lines, between last and first
052    *                         entries cycling
053    */
054   public ScrollingMessageBox(ArrayList<String> messageStrings,
055     double velocity, int gap{
056     this.velocity = velocity;
057     this.gap = gap;
058     lineHeight = DEFAULT_LINE_HEIGHT;
059 
060     this.messages = new ArrayList<StringSprite>(messageStrings.size());
061     for (int = 0; i != messageStrings.size()++i{
062       add(messageStrings.get(i));
063     }
064   }
065 
066   /**
067    * Add the given string to the message list. Will fix up the location.
068    * Intended to be called before scrolling begins. Will not handle
069    * adding while scrolling well.
070    *
071    @param  msg  the string to add to the list of strings scrolling; a
072    *              new last string
073    */
074   public void add(String msg{
075     StringSprite messageLine = makeMessage(msg);
076     double yCoordinate = TOP_EDGE;
077     if (!messages.isEmpty()) {
078       yCoordinate = messages.get(messages.size() 1).getY() +
079         lineHeight;
080     }
081     messageLine.setLocation(0.0, yCoordinate);
082     messages.add(messageLine);
083     addSprite(messageLine);
084   }
085 
086   /**
087    * Scroll the displayed text up according to the speed of the
088    * animation.
089    *
090    @param  dT  time, in seconds, since last advance call
091    */
092   public void advance(double dT{
093     scrollMessages(dT);
094     wrapIfNecessary();
095   }
096 
097   /**
098    * Get the current gap (in lines)
099    *
100    @return  the gap
101    */
102   public int getGap() {
103     return gap;
104   }
105 
106   /**
107    * Get the current line height (in sprite screens)
108    *
109    @return  the lineHeight in internal coordinate screens
110    */
111   public double getLineHeight() {
112     return lineHeight;
113   }
114 
115   /**
116    * Get the scrolling velocity
117    *
118    @return  the velocity in screens/second
119    */
120   public double getVelocity() {
121     return velocity;
122   }
123 
124   /**
125    * Pass the color down to the individual lines in the message window.
126    *
127    @param  color  the color to set
128    */
129   @Override
130   public void setColor(Color color{
131     super.setColor(color);// set the color
132     for (int = 0; i != messages.size()++i{
133       messages.get(i).setColor(color);
134     }
135   }
136 
137   /**
138    Set the gap to the given value
139    *
140    @param  gap  the gap to set negative numbers ignored.
141    */
142   public void setGap(int gap{
143     if (gap >= 0{
144       this.gap = gap;
145     }
146   }
147 
148   /**
149    Set the distance between the lines; will take effect next wrap
150    * around the set
151    *
152    @param  lineHeight  the lineHeight to set
153    */
154   public void setLineHeight(double lineHeight{
155     this.lineHeight = lineHeight;
156   }
157 
158   /**
159    Set the scrolling velocity
160    *
161    @param  velocity  new velocity in screens/second
162    */
163   public void setVelocity(double velocity{
164     this.velocity = velocity;
165   }
166 
167   /**
168    * Create a new message line in the sprite. Given the value, scale it
169    * to fit on the sprite, set color, and drive on
170    *
171    @param   msg  the text of the new line
172    *
173    @return  new {@link StringSprite} with the given text and right
174    *          scale and stuff
175    */
176   private StringSprite makeMessage(String msg{
177     StringSprite messageLine = new StringSprite();
178     messageLine.setText(msg);
179     messageLine.setLineHeight(lineHeight * 0.8);
180     messageLine.centerJustify();
181     messageLine.setColor(getColor());
182     return messageLine;
183   }
184 
185   /**
186    * Scroll the lines. Everybody moves according to the velocity.
187    *
188    @param  dT  time, in seconds, since the last advance
189    */
190   private void scrollMessages(double dT{
191     for (int = 0; i != messages.size()++i{
192       messages.get(i).translateY(dT * velocity);
193     }
194   }
195 
196   /**
197    * If top most line (on the screen) is too high (it has touched
198    * TOP_EDGE), move it down to the bottom of the scrolling list.
199    * indexOfTopScroller tracks topmost {@link StringSprite}.
200    */
201   private void wrapIfNecessary() {
202     StringSprite top = messages.get(indexOfTopLineOnScreen);
203     if (top.getMinY() < TOP_EDGE{
204       int indexOfBottomLineOnScreen = (indexOfTopLineOnScreen +
205           (messages.size() 1)) % messages.size();
206       StringSprite bottom = messages.get(indexOfBottomLineOnScreen);
207       top.setY(bottom.getY() + lineHeight);// top just below bottom
208 
209       if ((indexOfTopLineOnScreen == 0&& (gap > 0)) {
210         top.translateY(lineHeight * gap);
211       }
212 
213       indexOfTopLineOnScreen = (indexOfTopLineOnScreen + 1%
214         messages.size();
215     }
216   }
217 }
218 
219 //Uploaded on Mon Mar 29 21:40:12 EDT 2010


Download/View scg/ch13/highscore/ScrollingMessageBox.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