Advertisement
Jong

Progress Bar HUD - AndEngine

Dec 19th, 2011
1,488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.74 KB | None | 0 0
  1. package com.jong.rpg;
  2.  
  3. import org.anddev.andengine.engine.camera.Camera;
  4. import org.anddev.andengine.engine.camera.hud.HUD;
  5. import org.anddev.andengine.entity.primitive.Line;
  6. import org.anddev.andengine.entity.primitive.Rectangle;
  7.  
  8. /**
  9.  * @author Jong - Yonatan
  10.  *
  11.  */
  12. public class ProgressBar extends HUD {
  13.     // ===========================================================
  14.     // Constants          
  15.     // ===========================================================
  16.     private static final float FRAME_LINE_WIDTH = 5f;
  17.     // ===========================================================          
  18.     // Fields        
  19.     // ===========================================================
  20.     private final Line[] mFrameLines = new Line[4];
  21.     private final Rectangle mBackgroundRectangle;
  22.     private final Rectangle mProgressRectangle;
  23.    
  24.     private final float mPixelsPerPercentRatio;
  25.     // ===========================================================          
  26.     // Constructors          
  27.     // ===========================================================
  28.     public ProgressBar(final Camera pCamera, final float pX, final float pY, final float pWidth, final float pHeight) {
  29.         super();
  30.         super.setCamera(pCamera);
  31.        
  32.         this.mBackgroundRectangle = new Rectangle(pX, pY, pWidth, pHeight);
  33.        
  34.         this.mFrameLines[0] = new Line(pX, pY, pX+pWidth, pY, FRAME_LINE_WIDTH); //Top line.
  35.         this.mFrameLines[1] = new Line(pX + pWidth, pY, pX + pWidth, pY + pHeight, FRAME_LINE_WIDTH); //Right line.
  36.         this.mFrameLines[2] = new Line(pX + pWidth, pY + pHeight, pX, pY + pHeight, FRAME_LINE_WIDTH); //Bottom line.
  37.         this.mFrameLines[3] = new Line(pX, pY + pHeight, pX, pY, FRAME_LINE_WIDTH); //Left line.
  38.        
  39.         this.mProgressRectangle = new Rectangle(pX, pY, pWidth, pHeight);
  40.        
  41.         super.attachChild(this.mBackgroundRectangle); //This one is drawn first.
  42.         super.attachChild(this.mProgressRectangle); //The progress is drawn afterwards.
  43.         for(int i = 0; i < this.mFrameLines.length; i++)
  44.             super.attachChild(this.mFrameLines[i]); //Lines are drawn last, so they'll override everything.
  45.        
  46.         this.mPixelsPerPercentRatio = pWidth / 100;
  47.     }
  48.     // ===========================================================          
  49.     // Getter & Setter          
  50.     // ===========================================================
  51.     public void setBackColor(final float pRed, final float pGreen, final float pBlue, final float pAlpha) {
  52.         this.mBackgroundRectangle.setColor(pRed, pGreen, pBlue, pAlpha);
  53.     }
  54.     public void setFrameColor(final float pRed, final float pGreen, final float pBlue, final float pAlpha) {
  55.         for(int i = 0; i < this.mFrameLines.length; i++)
  56.             this.mFrameLines[i].setColor(pRed, pGreen, pBlue, pAlpha);
  57.     }
  58.     public void setProgressColor(final float pRed, final float pGreen, final float pBlue, final float pAlpha) {
  59.         this.mProgressRectangle.setColor(pRed, pGreen, pBlue, pAlpha);
  60.     }
  61.     /**
  62.      * Set the current progress of this progress bar.
  63.      * @param pProgress is <b> BETWEEN </b> 0 - 100.
  64.      */
  65.     public void setProgress(final float pProgress) {
  66.         if(pProgress < 0)
  67.             this.mProgressRectangle.setWidth(0); //This is an internal check for my specific game, you can remove it.
  68.         this.mProgressRectangle.setWidth(this.mPixelsPerPercentRatio * pProgress);
  69.     }
  70.     // ===========================================================          
  71.     // Methods for/from SuperClass/Interfaces          
  72.     // ===========================================================  
  73.    
  74.     // ===========================================================          
  75.     // Methods          
  76.     // ===========================================================  
  77.    
  78.     // ===========================================================          
  79.     // Inner and Anonymous Classes          
  80.     // ===========================================================  
  81.    
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement